2 * linux/fs/ext4/super.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
19 #include <linux/module.h>
20 #include <linux/string.h>
22 #include <linux/time.h>
23 #include <linux/jbd2.h>
24 #include <linux/ext4_fs.h>
25 #include <linux/ext4_jbd2.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/blkdev.h>
29 #include <linux/parser.h>
30 #include <linux/smp_lock.h>
31 #include <linux/buffer_head.h>
32 #include <linux/vfs.h>
33 #include <linux/random.h>
34 #include <linux/mount.h>
35 #include <linux/namei.h>
36 #include <linux/quotaops.h>
37 #include <linux/seq_file.h>
39 #include <asm/uaccess.h>
45 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
46 unsigned long journal_devnum);
47 static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
49 static void ext4_commit_super (struct super_block * sb,
50 struct ext4_super_block * es,
52 static void ext4_mark_recovery_complete(struct super_block * sb,
53 struct ext4_super_block * es);
54 static void ext4_clear_journal_err(struct super_block * sb,
55 struct ext4_super_block * es);
56 static int ext4_sync_fs(struct super_block *sb, int wait);
57 static const char *ext4_decode_error(struct super_block * sb, int errno,
59 static int ext4_remount (struct super_block * sb, int * flags, char * data);
60 static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
61 static void ext4_unlockfs(struct super_block *sb);
62 static void ext4_write_super (struct super_block * sb);
63 static void ext4_write_super_lockfs(struct super_block *sb);
66 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
67 struct ext4_group_desc *bg)
69 return le32_to_cpu(bg->bg_block_bitmap) |
70 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
71 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
74 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
75 struct ext4_group_desc *bg)
77 return le32_to_cpu(bg->bg_inode_bitmap) |
78 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
79 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
82 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
83 struct ext4_group_desc *bg)
85 return le32_to_cpu(bg->bg_inode_table) |
86 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
87 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
90 void ext4_block_bitmap_set(struct super_block *sb,
91 struct ext4_group_desc *bg, ext4_fsblk_t blk)
93 bg->bg_block_bitmap = cpu_to_le32((u32)blk);
94 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
95 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
98 void ext4_inode_bitmap_set(struct super_block *sb,
99 struct ext4_group_desc *bg, ext4_fsblk_t blk)
101 bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
102 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
103 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
106 void ext4_inode_table_set(struct super_block *sb,
107 struct ext4_group_desc *bg, ext4_fsblk_t blk)
109 bg->bg_inode_table = cpu_to_le32((u32)blk);
110 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
111 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
115 * Wrappers for jbd2_journal_start/end.
117 * The only special thing we need to do here is to make sure that all
118 * journal_end calls result in the superblock being marked dirty, so
119 * that sync() will call the filesystem's write_super callback if
122 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
126 if (sb->s_flags & MS_RDONLY)
127 return ERR_PTR(-EROFS);
129 /* Special case here: if the journal has aborted behind our
130 * backs (eg. EIO in the commit thread), then we still need to
131 * take the FS itself readonly cleanly. */
132 journal = EXT4_SB(sb)->s_journal;
133 if (is_journal_aborted(journal)) {
134 ext4_abort(sb, __FUNCTION__,
135 "Detected aborted journal");
136 return ERR_PTR(-EROFS);
139 return jbd2_journal_start(journal, nblocks);
143 * The only special thing we need to do here is to make sure that all
144 * jbd2_journal_stop calls result in the superblock being marked dirty, so
145 * that sync() will call the filesystem's write_super callback if
148 int __ext4_journal_stop(const char *where, handle_t *handle)
150 struct super_block *sb;
154 sb = handle->h_transaction->t_journal->j_private;
156 rc = jbd2_journal_stop(handle);
161 __ext4_std_error(sb, where, err);
165 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
166 struct buffer_head *bh, handle_t *handle, int err)
169 const char *errstr = ext4_decode_error(NULL, err, nbuf);
172 BUFFER_TRACE(bh, "abort");
177 if (is_handle_aborted(handle))
180 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
181 caller, errstr, err_fn);
183 jbd2_journal_abort_handle(handle);
186 /* Deal with the reporting of failure conditions on a filesystem such as
187 * inconsistencies detected or read IO failures.
189 * On ext2, we can store the error state of the filesystem in the
190 * superblock. That is not possible on ext4, because we may have other
191 * write ordering constraints on the superblock which prevent us from
192 * writing it out straight away; and given that the journal is about to
193 * be aborted, we can't rely on the current, or future, transactions to
194 * write out the superblock safely.
196 * We'll just use the jbd2_journal_abort() error code to record an error in
197 * the journal instead. On recovery, the journal will compain about
198 * that error until we've noted it down and cleared it.
201 static void ext4_handle_error(struct super_block *sb)
203 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
205 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
206 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
208 if (sb->s_flags & MS_RDONLY)
211 if (!test_opt (sb, ERRORS_CONT)) {
212 journal_t *journal = EXT4_SB(sb)->s_journal;
214 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
216 jbd2_journal_abort(journal, -EIO);
218 if (test_opt (sb, ERRORS_RO)) {
219 printk (KERN_CRIT "Remounting filesystem read-only\n");
220 sb->s_flags |= MS_RDONLY;
222 ext4_commit_super(sb, es, 1);
223 if (test_opt(sb, ERRORS_PANIC))
224 panic("EXT4-fs (device %s): panic forced after error\n",
228 void ext4_error (struct super_block * sb, const char * function,
229 const char * fmt, ...)
234 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
239 ext4_handle_error(sb);
242 static const char *ext4_decode_error(struct super_block * sb, int errno,
249 errstr = "IO failure";
252 errstr = "Out of memory";
255 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
256 errstr = "Journal has aborted";
258 errstr = "Readonly filesystem";
261 /* If the caller passed in an extra buffer for unknown
262 * errors, textualise them now. Else we just return
265 /* Check for truncated error codes... */
266 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
275 /* __ext4_std_error decodes expected errors from journaling functions
276 * automatically and invokes the appropriate error response. */
278 void __ext4_std_error (struct super_block * sb, const char * function,
284 /* Special case: if the error is EROFS, and we're not already
285 * inside a transaction, then there's really no point in logging
287 if (errno == -EROFS && journal_current_handle() == NULL &&
288 (sb->s_flags & MS_RDONLY))
291 errstr = ext4_decode_error(sb, errno, nbuf);
292 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
293 sb->s_id, function, errstr);
295 ext4_handle_error(sb);
299 * ext4_abort is a much stronger failure handler than ext4_error. The
300 * abort function may be used to deal with unrecoverable failures such
301 * as journal IO errors or ENOMEM at a critical moment in log management.
303 * We unconditionally force the filesystem into an ABORT|READONLY state,
304 * unless the error response on the fs has been set to panic in which
305 * case we take the easy way out and panic immediately.
308 void ext4_abort (struct super_block * sb, const char * function,
309 const char * fmt, ...)
313 printk (KERN_CRIT "ext4_abort called.\n");
316 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
321 if (test_opt(sb, ERRORS_PANIC))
322 panic("EXT4-fs panic from previous error\n");
324 if (sb->s_flags & MS_RDONLY)
327 printk(KERN_CRIT "Remounting filesystem read-only\n");
328 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
329 sb->s_flags |= MS_RDONLY;
330 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
331 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
334 void ext4_warning (struct super_block * sb, const char * function,
335 const char * fmt, ...)
340 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
347 void ext4_update_dynamic_rev(struct super_block *sb)
349 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
351 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
354 ext4_warning(sb, __FUNCTION__,
355 "updating to rev %d because of new feature flag, "
356 "running e2fsck is recommended",
359 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
360 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
361 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
362 /* leave es->s_feature_*compat flags alone */
363 /* es->s_uuid will be set by e2fsck if empty */
366 * The rest of the superblock fields should be zero, and if not it
367 * means they are likely already in use, so leave them alone. We
368 * can leave it up to e2fsck to clean up any inconsistencies there.
373 * Open the external journal device
375 static struct block_device *ext4_blkdev_get(dev_t dev)
377 struct block_device *bdev;
378 char b[BDEVNAME_SIZE];
380 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
386 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
387 __bdevname(dev, b), PTR_ERR(bdev));
392 * Release the journal device
394 static int ext4_blkdev_put(struct block_device *bdev)
397 return blkdev_put(bdev);
400 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
402 struct block_device *bdev;
405 bdev = sbi->journal_bdev;
407 ret = ext4_blkdev_put(bdev);
408 sbi->journal_bdev = NULL;
413 static inline struct inode *orphan_list_entry(struct list_head *l)
415 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
418 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
422 printk(KERN_ERR "sb orphan head is %d\n",
423 le32_to_cpu(sbi->s_es->s_last_orphan));
425 printk(KERN_ERR "sb_info orphan list:\n");
426 list_for_each(l, &sbi->s_orphan) {
427 struct inode *inode = orphan_list_entry(l);
429 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
430 inode->i_sb->s_id, inode->i_ino, inode,
431 inode->i_mode, inode->i_nlink,
436 static void ext4_put_super (struct super_block * sb)
438 struct ext4_sb_info *sbi = EXT4_SB(sb);
439 struct ext4_super_block *es = sbi->s_es;
442 ext4_ext_release(sb);
443 ext4_xattr_put_super(sb);
444 jbd2_journal_destroy(sbi->s_journal);
445 if (!(sb->s_flags & MS_RDONLY)) {
446 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
447 es->s_state = cpu_to_le16(sbi->s_mount_state);
448 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
449 mark_buffer_dirty(sbi->s_sbh);
450 ext4_commit_super(sb, es, 1);
453 for (i = 0; i < sbi->s_gdb_count; i++)
454 brelse(sbi->s_group_desc[i]);
455 kfree(sbi->s_group_desc);
456 percpu_counter_destroy(&sbi->s_freeblocks_counter);
457 percpu_counter_destroy(&sbi->s_freeinodes_counter);
458 percpu_counter_destroy(&sbi->s_dirs_counter);
461 for (i = 0; i < MAXQUOTAS; i++)
462 kfree(sbi->s_qf_names[i]);
465 /* Debugging code just in case the in-memory inode orphan list
466 * isn't empty. The on-disk one can be non-empty if we've
467 * detected an error and taken the fs readonly, but the
468 * in-memory list had better be clean by this point. */
469 if (!list_empty(&sbi->s_orphan))
470 dump_orphan_list(sb, sbi);
471 J_ASSERT(list_empty(&sbi->s_orphan));
473 invalidate_bdev(sb->s_bdev, 0);
474 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
476 * Invalidate the journal device's buffers. We don't want them
477 * floating about in memory - the physical journal device may
478 * hotswapped, and it breaks the `ro-after' testing code.
480 sync_blockdev(sbi->journal_bdev);
481 invalidate_bdev(sbi->journal_bdev, 0);
482 ext4_blkdev_remove(sbi);
484 sb->s_fs_info = NULL;
489 static kmem_cache_t *ext4_inode_cachep;
492 * Called inside transaction, so use GFP_NOFS
494 static struct inode *ext4_alloc_inode(struct super_block *sb)
496 struct ext4_inode_info *ei;
498 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
501 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
502 ei->i_acl = EXT4_ACL_NOT_CACHED;
503 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
505 ei->i_block_alloc_info = NULL;
506 ei->vfs_inode.i_version = 1;
507 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
508 return &ei->vfs_inode;
511 static void ext4_destroy_inode(struct inode *inode)
513 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
516 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
518 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
520 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
521 SLAB_CTOR_CONSTRUCTOR) {
522 INIT_LIST_HEAD(&ei->i_orphan);
523 #ifdef CONFIG_EXT4DEV_FS_XATTR
524 init_rwsem(&ei->xattr_sem);
526 mutex_init(&ei->truncate_mutex);
527 inode_init_once(&ei->vfs_inode);
531 static int init_inodecache(void)
533 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
534 sizeof(struct ext4_inode_info),
535 0, (SLAB_RECLAIM_ACCOUNT|
538 if (ext4_inode_cachep == NULL)
543 static void destroy_inodecache(void)
545 kmem_cache_destroy(ext4_inode_cachep);
548 static void ext4_clear_inode(struct inode *inode)
550 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
551 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
552 if (EXT4_I(inode)->i_acl &&
553 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
554 posix_acl_release(EXT4_I(inode)->i_acl);
555 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
557 if (EXT4_I(inode)->i_default_acl &&
558 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
559 posix_acl_release(EXT4_I(inode)->i_default_acl);
560 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
563 ext4_discard_reservation(inode);
564 EXT4_I(inode)->i_block_alloc_info = NULL;
569 static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
571 #if defined(CONFIG_QUOTA)
572 struct ext4_sb_info *sbi = EXT4_SB(sb);
574 if (sbi->s_jquota_fmt)
575 seq_printf(seq, ",jqfmt=%s",
576 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
578 if (sbi->s_qf_names[USRQUOTA])
579 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
581 if (sbi->s_qf_names[GRPQUOTA])
582 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
584 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
585 seq_puts(seq, ",usrquota");
587 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
588 seq_puts(seq, ",grpquota");
592 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
594 struct super_block *sb = vfs->mnt_sb;
596 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
597 seq_puts(seq, ",data=journal");
598 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
599 seq_puts(seq, ",data=ordered");
600 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
601 seq_puts(seq, ",data=writeback");
603 ext4_show_quota_options(seq, sb);
609 static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
612 unsigned long ino = objp[0];
613 __u32 generation = objp[1];
615 struct dentry *result;
617 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
618 return ERR_PTR(-ESTALE);
619 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
620 return ERR_PTR(-ESTALE);
622 /* iget isn't really right if the inode is currently unallocated!!
624 * ext4_read_inode will return a bad_inode if the inode had been
625 * deleted, so we should be safe.
627 * Currently we don't know the generation for parent directory, so
628 * a generation of 0 means "accept any"
630 inode = iget(sb, ino);
632 return ERR_PTR(-ENOMEM);
633 if (is_bad_inode(inode) ||
634 (generation && inode->i_generation != generation)) {
636 return ERR_PTR(-ESTALE);
638 /* now to find a dentry.
639 * If possible, get a well-connected one
641 result = d_alloc_anon(inode);
644 return ERR_PTR(-ENOMEM);
650 #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
651 #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
653 static int ext4_dquot_initialize(struct inode *inode, int type);
654 static int ext4_dquot_drop(struct inode *inode);
655 static int ext4_write_dquot(struct dquot *dquot);
656 static int ext4_acquire_dquot(struct dquot *dquot);
657 static int ext4_release_dquot(struct dquot *dquot);
658 static int ext4_mark_dquot_dirty(struct dquot *dquot);
659 static int ext4_write_info(struct super_block *sb, int type);
660 static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
661 static int ext4_quota_on_mount(struct super_block *sb, int type);
662 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
663 size_t len, loff_t off);
664 static ssize_t ext4_quota_write(struct super_block *sb, int type,
665 const char *data, size_t len, loff_t off);
667 static struct dquot_operations ext4_quota_operations = {
668 .initialize = ext4_dquot_initialize,
669 .drop = ext4_dquot_drop,
670 .alloc_space = dquot_alloc_space,
671 .alloc_inode = dquot_alloc_inode,
672 .free_space = dquot_free_space,
673 .free_inode = dquot_free_inode,
674 .transfer = dquot_transfer,
675 .write_dquot = ext4_write_dquot,
676 .acquire_dquot = ext4_acquire_dquot,
677 .release_dquot = ext4_release_dquot,
678 .mark_dirty = ext4_mark_dquot_dirty,
679 .write_info = ext4_write_info
682 static struct quotactl_ops ext4_qctl_operations = {
683 .quota_on = ext4_quota_on,
684 .quota_off = vfs_quota_off,
685 .quota_sync = vfs_quota_sync,
686 .get_info = vfs_get_dqinfo,
687 .set_info = vfs_set_dqinfo,
688 .get_dqblk = vfs_get_dqblk,
689 .set_dqblk = vfs_set_dqblk
693 static struct super_operations ext4_sops = {
694 .alloc_inode = ext4_alloc_inode,
695 .destroy_inode = ext4_destroy_inode,
696 .read_inode = ext4_read_inode,
697 .write_inode = ext4_write_inode,
698 .dirty_inode = ext4_dirty_inode,
699 .delete_inode = ext4_delete_inode,
700 .put_super = ext4_put_super,
701 .write_super = ext4_write_super,
702 .sync_fs = ext4_sync_fs,
703 .write_super_lockfs = ext4_write_super_lockfs,
704 .unlockfs = ext4_unlockfs,
705 .statfs = ext4_statfs,
706 .remount_fs = ext4_remount,
707 .clear_inode = ext4_clear_inode,
708 .show_options = ext4_show_options,
710 .quota_read = ext4_quota_read,
711 .quota_write = ext4_quota_write,
715 static struct export_operations ext4_export_ops = {
716 .get_parent = ext4_get_parent,
717 .get_dentry = ext4_get_dentry,
721 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
722 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
723 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
724 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
725 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
726 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
727 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
728 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
729 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
730 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
731 Opt_grpquota, Opt_extents,
734 static match_table_t tokens = {
735 {Opt_bsd_df, "bsddf"},
736 {Opt_minix_df, "minixdf"},
737 {Opt_grpid, "grpid"},
738 {Opt_grpid, "bsdgroups"},
739 {Opt_nogrpid, "nogrpid"},
740 {Opt_nogrpid, "sysvgroups"},
741 {Opt_resgid, "resgid=%u"},
742 {Opt_resuid, "resuid=%u"},
744 {Opt_err_cont, "errors=continue"},
745 {Opt_err_panic, "errors=panic"},
746 {Opt_err_ro, "errors=remount-ro"},
747 {Opt_nouid32, "nouid32"},
748 {Opt_nocheck, "nocheck"},
749 {Opt_nocheck, "check=none"},
750 {Opt_debug, "debug"},
751 {Opt_oldalloc, "oldalloc"},
752 {Opt_orlov, "orlov"},
753 {Opt_user_xattr, "user_xattr"},
754 {Opt_nouser_xattr, "nouser_xattr"},
756 {Opt_noacl, "noacl"},
757 {Opt_reservation, "reservation"},
758 {Opt_noreservation, "noreservation"},
759 {Opt_noload, "noload"},
762 {Opt_commit, "commit=%u"},
763 {Opt_journal_update, "journal=update"},
764 {Opt_journal_inum, "journal=%u"},
765 {Opt_journal_dev, "journal_dev=%u"},
766 {Opt_abort, "abort"},
767 {Opt_data_journal, "data=journal"},
768 {Opt_data_ordered, "data=ordered"},
769 {Opt_data_writeback, "data=writeback"},
770 {Opt_offusrjquota, "usrjquota="},
771 {Opt_usrjquota, "usrjquota=%s"},
772 {Opt_offgrpjquota, "grpjquota="},
773 {Opt_grpjquota, "grpjquota=%s"},
774 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
775 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
776 {Opt_grpquota, "grpquota"},
777 {Opt_noquota, "noquota"},
778 {Opt_quota, "quota"},
779 {Opt_usrquota, "usrquota"},
780 {Opt_barrier, "barrier=%u"},
781 {Opt_extents, "extents"},
783 {Opt_resize, "resize"},
786 static ext4_fsblk_t get_sb_block(void **data)
788 ext4_fsblk_t sb_block;
789 char *options = (char *) *data;
791 if (!options || strncmp(options, "sb=", 3) != 0)
792 return 1; /* Default location */
794 /*todo: use simple_strtoll with >32bit ext4 */
795 sb_block = simple_strtoul(options, &options, 0);
796 if (*options && *options != ',') {
797 printk("EXT4-fs: Invalid sb specification: %s\n",
803 *data = (void *) options;
807 static int parse_options (char *options, struct super_block *sb,
808 unsigned int *inum, unsigned long *journal_devnum,
809 ext4_fsblk_t *n_blocks_count, int is_remount)
811 struct ext4_sb_info *sbi = EXT4_SB(sb);
813 substring_t args[MAX_OPT_ARGS];
824 while ((p = strsep (&options, ",")) != NULL) {
829 token = match_token(p, tokens, args);
832 clear_opt (sbi->s_mount_opt, MINIX_DF);
835 set_opt (sbi->s_mount_opt, MINIX_DF);
838 set_opt (sbi->s_mount_opt, GRPID);
841 clear_opt (sbi->s_mount_opt, GRPID);
844 if (match_int(&args[0], &option))
846 sbi->s_resuid = option;
849 if (match_int(&args[0], &option))
851 sbi->s_resgid = option;
854 /* handled by get_sb_block() instead of here */
855 /* *sb_block = match_int(&args[0]); */
858 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
859 clear_opt (sbi->s_mount_opt, ERRORS_RO);
860 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
863 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
864 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
865 set_opt (sbi->s_mount_opt, ERRORS_RO);
868 clear_opt (sbi->s_mount_opt, ERRORS_RO);
869 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
870 set_opt (sbi->s_mount_opt, ERRORS_CONT);
873 set_opt (sbi->s_mount_opt, NO_UID32);
876 clear_opt (sbi->s_mount_opt, CHECK);
879 set_opt (sbi->s_mount_opt, DEBUG);
882 set_opt (sbi->s_mount_opt, OLDALLOC);
885 clear_opt (sbi->s_mount_opt, OLDALLOC);
887 #ifdef CONFIG_EXT4DEV_FS_XATTR
889 set_opt (sbi->s_mount_opt, XATTR_USER);
891 case Opt_nouser_xattr:
892 clear_opt (sbi->s_mount_opt, XATTR_USER);
896 case Opt_nouser_xattr:
897 printk("EXT4 (no)user_xattr options not supported\n");
900 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
902 set_opt(sbi->s_mount_opt, POSIX_ACL);
905 clear_opt(sbi->s_mount_opt, POSIX_ACL);
910 printk("EXT4 (no)acl options not supported\n");
913 case Opt_reservation:
914 set_opt(sbi->s_mount_opt, RESERVATION);
916 case Opt_noreservation:
917 clear_opt(sbi->s_mount_opt, RESERVATION);
919 case Opt_journal_update:
921 /* Eventually we will want to be able to create
922 a journal file here. For now, only allow the
923 user to specify an existing inode to be the
926 printk(KERN_ERR "EXT4-fs: cannot specify "
927 "journal on remount\n");
930 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
932 case Opt_journal_inum:
934 printk(KERN_ERR "EXT4-fs: cannot specify "
935 "journal on remount\n");
938 if (match_int(&args[0], &option))
942 case Opt_journal_dev:
944 printk(KERN_ERR "EXT4-fs: cannot specify "
945 "journal on remount\n");
948 if (match_int(&args[0], &option))
950 *journal_devnum = option;
953 set_opt (sbi->s_mount_opt, NOLOAD);
956 if (match_int(&args[0], &option))
961 option = JBD_DEFAULT_MAX_COMMIT_AGE;
962 sbi->s_commit_interval = HZ * option;
964 case Opt_data_journal:
965 data_opt = EXT4_MOUNT_JOURNAL_DATA;
967 case Opt_data_ordered:
968 data_opt = EXT4_MOUNT_ORDERED_DATA;
970 case Opt_data_writeback:
971 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
974 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
977 "EXT4-fs: cannot change data "
978 "mode on remount\n");
982 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
983 sbi->s_mount_opt |= data_opt;
993 if (sb_any_quota_enabled(sb)) {
995 "EXT4-fs: Cannot change journalled "
996 "quota options when quota turned on.\n");
999 qname = match_strdup(&args[0]);
1002 "EXT4-fs: not enough memory for "
1003 "storing quotafile name.\n");
1006 if (sbi->s_qf_names[qtype] &&
1007 strcmp(sbi->s_qf_names[qtype], qname)) {
1009 "EXT4-fs: %s quota file already "
1010 "specified.\n", QTYPE2NAME(qtype));
1014 sbi->s_qf_names[qtype] = qname;
1015 if (strchr(sbi->s_qf_names[qtype], '/')) {
1017 "EXT4-fs: quotafile must be on "
1018 "filesystem root.\n");
1019 kfree(sbi->s_qf_names[qtype]);
1020 sbi->s_qf_names[qtype] = NULL;
1023 set_opt(sbi->s_mount_opt, QUOTA);
1025 case Opt_offusrjquota:
1028 case Opt_offgrpjquota:
1031 if (sb_any_quota_enabled(sb)) {
1032 printk(KERN_ERR "EXT4-fs: Cannot change "
1033 "journalled quota options when "
1034 "quota turned on.\n");
1038 * The space will be released later when all options
1039 * are confirmed to be correct
1041 sbi->s_qf_names[qtype] = NULL;
1043 case Opt_jqfmt_vfsold:
1044 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1046 case Opt_jqfmt_vfsv0:
1047 sbi->s_jquota_fmt = QFMT_VFS_V0;
1051 set_opt(sbi->s_mount_opt, QUOTA);
1052 set_opt(sbi->s_mount_opt, USRQUOTA);
1055 set_opt(sbi->s_mount_opt, QUOTA);
1056 set_opt(sbi->s_mount_opt, GRPQUOTA);
1059 if (sb_any_quota_enabled(sb)) {
1060 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1061 "options when quota turned on.\n");
1064 clear_opt(sbi->s_mount_opt, QUOTA);
1065 clear_opt(sbi->s_mount_opt, USRQUOTA);
1066 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1074 case Opt_offusrjquota:
1075 case Opt_offgrpjquota:
1076 case Opt_jqfmt_vfsold:
1077 case Opt_jqfmt_vfsv0:
1079 "EXT4-fs: journalled quota options not "
1086 set_opt(sbi->s_mount_opt, ABORT);
1089 if (match_int(&args[0], &option))
1092 set_opt(sbi->s_mount_opt, BARRIER);
1094 clear_opt(sbi->s_mount_opt, BARRIER);
1100 printk("EXT4-fs: resize option only available "
1104 if (match_int(&args[0], &option) != 0)
1106 *n_blocks_count = option;
1109 set_opt(sbi->s_mount_opt, NOBH);
1112 clear_opt(sbi->s_mount_opt, NOBH);
1115 set_opt (sbi->s_mount_opt, EXTENTS);
1119 "EXT4-fs: Unrecognized mount option \"%s\" "
1120 "or missing value\n", p);
1125 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1126 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1127 sbi->s_qf_names[USRQUOTA])
1128 clear_opt(sbi->s_mount_opt, USRQUOTA);
1130 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1131 sbi->s_qf_names[GRPQUOTA])
1132 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1134 if ((sbi->s_qf_names[USRQUOTA] &&
1135 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1136 (sbi->s_qf_names[GRPQUOTA] &&
1137 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1138 printk(KERN_ERR "EXT4-fs: old and new quota "
1139 "format mixing.\n");
1143 if (!sbi->s_jquota_fmt) {
1144 printk(KERN_ERR "EXT4-fs: journalled quota format "
1145 "not specified.\n");
1149 if (sbi->s_jquota_fmt) {
1150 printk(KERN_ERR "EXT4-fs: journalled quota format "
1151 "specified with no journalling "
1160 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1163 struct ext4_sb_info *sbi = EXT4_SB(sb);
1166 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1167 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
1168 "forcing read-only mode\n");
1173 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1174 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1175 "running e2fsck is recommended\n");
1176 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1177 printk (KERN_WARNING
1178 "EXT4-fs warning: mounting fs with errors, "
1179 "running e2fsck is recommended\n");
1180 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1181 le16_to_cpu(es->s_mnt_count) >=
1182 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1183 printk (KERN_WARNING
1184 "EXT4-fs warning: maximal mount count reached, "
1185 "running e2fsck is recommended\n");
1186 else if (le32_to_cpu(es->s_checkinterval) &&
1187 (le32_to_cpu(es->s_lastcheck) +
1188 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1189 printk (KERN_WARNING
1190 "EXT4-fs warning: checktime reached, "
1191 "running e2fsck is recommended\n");
1193 /* @@@ We _will_ want to clear the valid bit if we find
1194 * inconsistencies, to force a fsck at reboot. But for
1195 * a plain journaled filesystem we can keep it set as
1198 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
1200 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1201 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1202 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1203 es->s_mtime = cpu_to_le32(get_seconds());
1204 ext4_update_dynamic_rev(sb);
1205 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1207 ext4_commit_super(sb, es, 1);
1208 if (test_opt(sb, DEBUG))
1209 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1210 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1212 sbi->s_groups_count,
1213 EXT4_BLOCKS_PER_GROUP(sb),
1214 EXT4_INODES_PER_GROUP(sb),
1217 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1218 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
1219 char b[BDEVNAME_SIZE];
1221 printk("external journal on %s\n",
1222 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
1224 printk("internal journal\n");
1229 /* Called at mount-time, super-block is locked */
1230 static int ext4_check_descriptors (struct super_block * sb)
1232 struct ext4_sb_info *sbi = EXT4_SB(sb);
1233 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1234 ext4_fsblk_t last_block;
1235 ext4_fsblk_t block_bitmap;
1236 ext4_fsblk_t inode_bitmap;
1237 ext4_fsblk_t inode_table;
1238 struct ext4_group_desc * gdp = NULL;
1242 ext4_debug ("Checking group descriptors");
1244 for (i = 0; i < sbi->s_groups_count; i++)
1246 if (i == sbi->s_groups_count - 1)
1247 last_block = ext4_blocks_count(sbi->s_es) - 1;
1249 last_block = first_block +
1250 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1252 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1253 gdp = (struct ext4_group_desc *)
1254 sbi->s_group_desc[desc_block++]->b_data;
1255 block_bitmap = ext4_block_bitmap(sb, gdp);
1256 if (block_bitmap < first_block || block_bitmap > last_block)
1258 ext4_error (sb, "ext4_check_descriptors",
1259 "Block bitmap for group %d"
1260 " not in group (block %llu)!",
1264 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1265 if (inode_bitmap < first_block || inode_bitmap > last_block)
1267 ext4_error (sb, "ext4_check_descriptors",
1268 "Inode bitmap for group %d"
1269 " not in group (block %llu)!",
1273 inode_table = ext4_inode_table(sb, gdp);
1274 if (inode_table < first_block ||
1275 inode_table + sbi->s_itb_per_group > last_block)
1277 ext4_error (sb, "ext4_check_descriptors",
1278 "Inode table for group %d"
1279 " not in group (block %llu)!",
1283 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1284 gdp = (struct ext4_group_desc *)
1285 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
1288 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1289 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
1294 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1295 * the superblock) which were deleted from all directories, but held open by
1296 * a process at the time of a crash. We walk the list and try to delete these
1297 * inodes at recovery time (only with a read-write filesystem).
1299 * In order to keep the orphan inode chain consistent during traversal (in
1300 * case of crash during recovery), we link each inode into the superblock
1301 * orphan list_head and handle it the same way as an inode deletion during
1302 * normal operation (which journals the operations for us).
1304 * We only do an iget() and an iput() on each inode, which is very safe if we
1305 * accidentally point at an in-use or already deleted inode. The worst that
1306 * can happen in this case is that we get a "bit already cleared" message from
1307 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1308 * e2fsck was run on this filesystem, and it must have already done the orphan
1309 * inode cleanup for us, so we can safely abort without any further action.
1311 static void ext4_orphan_cleanup (struct super_block * sb,
1312 struct ext4_super_block * es)
1314 unsigned int s_flags = sb->s_flags;
1315 int nr_orphans = 0, nr_truncates = 0;
1319 if (!es->s_last_orphan) {
1320 jbd_debug(4, "no orphan inodes to clean up\n");
1324 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1325 if (es->s_last_orphan)
1326 jbd_debug(1, "Errors on filesystem, "
1327 "clearing orphan list.\n");
1328 es->s_last_orphan = 0;
1329 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1333 if (s_flags & MS_RDONLY) {
1334 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1336 sb->s_flags &= ~MS_RDONLY;
1339 /* Needed for iput() to work correctly and not trash data */
1340 sb->s_flags |= MS_ACTIVE;
1341 /* Turn on quotas so that they are updated correctly */
1342 for (i = 0; i < MAXQUOTAS; i++) {
1343 if (EXT4_SB(sb)->s_qf_names[i]) {
1344 int ret = ext4_quota_on_mount(sb, i);
1347 "EXT4-fs: Cannot turn on journalled "
1348 "quota: error %d\n", ret);
1353 while (es->s_last_orphan) {
1354 struct inode *inode;
1357 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
1358 es->s_last_orphan = 0;
1362 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1364 if (inode->i_nlink) {
1366 "%s: truncating inode %lu to %Ld bytes\n",
1367 __FUNCTION__, inode->i_ino, inode->i_size);
1368 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1369 inode->i_ino, inode->i_size);
1370 ext4_truncate(inode);
1374 "%s: deleting unreferenced inode %lu\n",
1375 __FUNCTION__, inode->i_ino);
1376 jbd_debug(2, "deleting unreferenced inode %lu\n",
1380 iput(inode); /* The delete magic happens here! */
1383 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1386 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1387 sb->s_id, PLURAL(nr_orphans));
1389 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1390 sb->s_id, PLURAL(nr_truncates));
1392 /* Turn quotas off */
1393 for (i = 0; i < MAXQUOTAS; i++) {
1394 if (sb_dqopt(sb)->files[i])
1395 vfs_quota_off(sb, i);
1398 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1401 #define log2(n) ffz(~(n))
1404 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1405 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1406 * We need to be 1 filesystem block less than the 2^32 sector limit.
1408 static loff_t ext4_max_size(int bits)
1410 loff_t res = EXT4_NDIR_BLOCKS;
1411 /* This constant is calculated to be the largest file size for a
1412 * dense, 4k-blocksize file such that the total number of
1413 * sectors in the file, including data and all indirect blocks,
1414 * does not exceed 2^32. */
1415 const loff_t upper_limit = 0x1ff7fffd000LL;
1417 res += 1LL << (bits-2);
1418 res += 1LL << (2*(bits-2));
1419 res += 1LL << (3*(bits-2));
1421 if (res > upper_limit)
1426 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1427 ext4_fsblk_t logical_sb_block, int nr)
1429 struct ext4_sb_info *sbi = EXT4_SB(sb);
1430 unsigned long bg, first_meta_bg;
1433 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1435 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1437 return logical_sb_block + nr + 1;
1438 bg = sbi->s_desc_per_block * nr;
1439 if (ext4_bg_has_super(sb, bg))
1441 return (has_super + ext4_group_first_block_no(sb, bg));
1445 static int ext4_fill_super (struct super_block *sb, void *data, int silent)
1447 struct buffer_head * bh;
1448 struct ext4_super_block *es = NULL;
1449 struct ext4_sb_info *sbi;
1451 ext4_fsblk_t sb_block = get_sb_block(&data);
1452 ext4_fsblk_t logical_sb_block;
1453 unsigned long offset = 0;
1454 unsigned int journal_inum = 0;
1455 unsigned long journal_devnum = 0;
1456 unsigned long def_mount_opts;
1466 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1469 sb->s_fs_info = sbi;
1470 sbi->s_mount_opt = 0;
1471 sbi->s_resuid = EXT4_DEF_RESUID;
1472 sbi->s_resgid = EXT4_DEF_RESGID;
1476 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1478 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1483 * The ext4 superblock will not be buffer aligned for other than 1kB
1484 * block sizes. We need to calculate the offset from buffer start.
1486 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1487 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1488 offset = do_div(logical_sb_block, blocksize);
1490 logical_sb_block = sb_block;
1493 if (!(bh = sb_bread(sb, logical_sb_block))) {
1494 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
1498 * Note: s_es must be initialized as soon as possible because
1499 * some ext4 macro-instructions depend on its value
1501 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1503 sb->s_magic = le16_to_cpu(es->s_magic);
1504 if (sb->s_magic != EXT4_SUPER_MAGIC)
1507 /* Set defaults before we parse the mount options */
1508 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1509 if (def_mount_opts & EXT4_DEFM_DEBUG)
1510 set_opt(sbi->s_mount_opt, DEBUG);
1511 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1512 set_opt(sbi->s_mount_opt, GRPID);
1513 if (def_mount_opts & EXT4_DEFM_UID16)
1514 set_opt(sbi->s_mount_opt, NO_UID32);
1515 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1516 set_opt(sbi->s_mount_opt, XATTR_USER);
1517 if (def_mount_opts & EXT4_DEFM_ACL)
1518 set_opt(sbi->s_mount_opt, POSIX_ACL);
1519 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1520 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1521 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1522 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1523 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1524 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1526 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1527 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1528 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
1529 set_opt(sbi->s_mount_opt, ERRORS_RO);
1531 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1533 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1534 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1536 set_opt(sbi->s_mount_opt, RESERVATION);
1538 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1542 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1543 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1545 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1546 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1547 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1548 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1550 "EXT4-fs warning: feature flags set on rev 0 fs, "
1551 "running e2fsck is recommended\n");
1553 * Check feature flags regardless of the revision level, since we
1554 * previously didn't change the revision level when setting the flags,
1555 * so there is a chance incompat flags are set on a rev 0 filesystem.
1557 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
1559 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
1560 "unsupported optional features (%x).\n",
1561 sb->s_id, le32_to_cpu(features));
1564 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
1565 if (!(sb->s_flags & MS_RDONLY) && features) {
1566 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
1567 "unsupported optional features (%x).\n",
1568 sb->s_id, le32_to_cpu(features));
1571 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1573 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1574 blocksize > EXT4_MAX_BLOCK_SIZE) {
1576 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
1577 blocksize, sb->s_id);
1581 hblock = bdev_hardsect_size(sb->s_bdev);
1582 if (sb->s_blocksize != blocksize) {
1584 * Make sure the blocksize for the filesystem is larger
1585 * than the hardware sectorsize for the machine.
1587 if (blocksize < hblock) {
1588 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
1589 "device blocksize %d.\n", blocksize, hblock);
1594 sb_set_blocksize(sb, blocksize);
1595 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1596 offset = do_div(logical_sb_block, blocksize);
1597 bh = sb_bread(sb, logical_sb_block);
1600 "EXT4-fs: Can't read superblock on 2nd try.\n");
1603 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
1605 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
1607 "EXT4-fs: Magic mismatch, very weird !\n");
1612 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
1614 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1615 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1616 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
1618 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1619 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1620 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1621 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1622 (sbi->s_inode_size > blocksize)) {
1624 "EXT4-fs: unsupported inode size: %d\n",
1629 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
1630 le32_to_cpu(es->s_log_frag_size);
1631 if (blocksize != sbi->s_frag_size) {
1633 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
1634 sbi->s_frag_size, blocksize);
1637 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
1638 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
1639 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
1640 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1641 sbi->s_desc_size & (sbi->s_desc_size - 1)) {
1643 "EXT4-fs: unsupported descriptor size %lu\n",
1648 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
1649 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1650 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1651 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1652 if (EXT4_INODE_SIZE(sb) == 0)
1654 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
1655 if (sbi->s_inodes_per_block == 0)
1657 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1658 sbi->s_inodes_per_block;
1659 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
1661 sbi->s_mount_state = le16_to_cpu(es->s_state);
1662 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1663 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
1664 for (i=0; i < 4; i++)
1665 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1666 sbi->s_def_hash_version = es->s_def_hash_version;
1668 if (sbi->s_blocks_per_group > blocksize * 8) {
1670 "EXT4-fs: #blocks per group too big: %lu\n",
1671 sbi->s_blocks_per_group);
1674 if (sbi->s_frags_per_group > blocksize * 8) {
1676 "EXT4-fs: #fragments per group too big: %lu\n",
1677 sbi->s_frags_per_group);
1680 if (sbi->s_inodes_per_group > blocksize * 8) {
1682 "EXT4-fs: #inodes per group too big: %lu\n",
1683 sbi->s_inodes_per_group);
1687 if (ext4_blocks_count(es) >
1688 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1689 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
1690 " too large to mount safely\n", sb->s_id);
1691 if (sizeof(sector_t) < 8)
1692 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
1697 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1699 blocks_count = (ext4_blocks_count(es) -
1700 le32_to_cpu(es->s_first_data_block) +
1701 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1702 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1703 sbi->s_groups_count = blocks_count;
1704 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1705 EXT4_DESC_PER_BLOCK(sb);
1706 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1708 if (sbi->s_group_desc == NULL) {
1709 printk (KERN_ERR "EXT4-fs: not enough memory\n");
1713 bgl_lock_init(&sbi->s_blockgroup_lock);
1715 for (i = 0; i < db_count; i++) {
1716 block = descriptor_loc(sb, logical_sb_block, i);
1717 sbi->s_group_desc[i] = sb_bread(sb, block);
1718 if (!sbi->s_group_desc[i]) {
1719 printk (KERN_ERR "EXT4-fs: "
1720 "can't read group descriptor %d\n", i);
1725 if (!ext4_check_descriptors (sb)) {
1726 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
1729 sbi->s_gdb_count = db_count;
1730 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1731 spin_lock_init(&sbi->s_next_gen_lock);
1733 percpu_counter_init(&sbi->s_freeblocks_counter,
1734 ext4_count_free_blocks(sb));
1735 percpu_counter_init(&sbi->s_freeinodes_counter,
1736 ext4_count_free_inodes(sb));
1737 percpu_counter_init(&sbi->s_dirs_counter,
1738 ext4_count_dirs(sb));
1740 /* per fileystem reservation list head & lock */
1741 spin_lock_init(&sbi->s_rsv_window_lock);
1742 sbi->s_rsv_window_root = RB_ROOT;
1743 /* Add a single, static dummy reservation to the start of the
1744 * reservation window list --- it gives us a placeholder for
1745 * append-at-start-of-list which makes the allocation logic
1746 * _much_ simpler. */
1747 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1748 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1749 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1750 sbi->s_rsv_window_head.rsv_goal_size = 0;
1751 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
1754 * set up enough so that it can read an inode
1756 sb->s_op = &ext4_sops;
1757 sb->s_export_op = &ext4_export_ops;
1758 sb->s_xattr = ext4_xattr_handlers;
1760 sb->s_qcop = &ext4_qctl_operations;
1761 sb->dq_op = &ext4_quota_operations;
1763 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1767 needs_recovery = (es->s_last_orphan != 0 ||
1768 EXT4_HAS_INCOMPAT_FEATURE(sb,
1769 EXT4_FEATURE_INCOMPAT_RECOVER));
1772 * The first inode we look at is the journal inode. Don't try
1773 * root first: it may be modified in the journal!
1775 if (!test_opt(sb, NOLOAD) &&
1776 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1777 if (ext4_load_journal(sb, es, journal_devnum))
1779 } else if (journal_inum) {
1780 if (ext4_create_journal(sb, es, journal_inum))
1785 "ext4: No journal on filesystem on %s\n",
1790 /* We have now updated the journal if required, so we can
1791 * validate the data journaling mode. */
1792 switch (test_opt(sb, DATA_FLAGS)) {
1794 /* No mode set, assume a default based on the journal
1795 * capabilities: ORDERED_DATA if the journal can
1796 * cope, else JOURNAL_DATA
1798 if (jbd2_journal_check_available_features
1799 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
1800 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1802 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1805 case EXT4_MOUNT_ORDERED_DATA:
1806 case EXT4_MOUNT_WRITEBACK_DATA:
1807 if (!jbd2_journal_check_available_features
1808 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
1809 printk(KERN_ERR "EXT4-fs: Journal does not support "
1810 "requested data journaling mode\n");
1817 if (test_opt(sb, NOBH)) {
1818 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1819 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
1820 "its supported only with writeback mode\n");
1821 clear_opt(sbi->s_mount_opt, NOBH);
1825 * The jbd2_journal_load will have done any necessary log recovery,
1826 * so we can safely mount the rest of the filesystem now.
1829 root = iget(sb, EXT4_ROOT_INO);
1830 sb->s_root = d_alloc_root(root);
1832 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
1836 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1839 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
1843 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1845 * akpm: core read_super() calls in here with the superblock locked.
1846 * That deadlocks, because orphan cleanup needs to lock the superblock
1847 * in numerous places. Here we just pop the lock - it's relatively
1848 * harmless, because we are now ready to accept write_super() requests,
1849 * and aviro says that's the only reason for hanging onto the
1852 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1853 ext4_orphan_cleanup(sb, es);
1854 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
1856 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1857 ext4_mark_recovery_complete(sb, es);
1858 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1859 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1860 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
1870 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
1875 jbd2_journal_destroy(sbi->s_journal);
1877 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1878 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1879 percpu_counter_destroy(&sbi->s_dirs_counter);
1881 for (i = 0; i < db_count; i++)
1882 brelse(sbi->s_group_desc[i]);
1883 kfree(sbi->s_group_desc);
1886 for (i = 0; i < MAXQUOTAS; i++)
1887 kfree(sbi->s_qf_names[i]);
1889 ext4_blkdev_remove(sbi);
1892 sb->s_fs_info = NULL;
1899 * Setup any per-fs journal parameters now. We'll do this both on
1900 * initial mount, once the journal has been initialised but before we've
1901 * done any recovery; and again on any subsequent remount.
1903 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
1905 struct ext4_sb_info *sbi = EXT4_SB(sb);
1907 if (sbi->s_commit_interval)
1908 journal->j_commit_interval = sbi->s_commit_interval;
1909 /* We could also set up an ext4-specific default for the commit
1910 * interval here, but for now we'll just fall back to the jbd
1913 spin_lock(&journal->j_state_lock);
1914 if (test_opt(sb, BARRIER))
1915 journal->j_flags |= JBD2_BARRIER;
1917 journal->j_flags &= ~JBD2_BARRIER;
1918 spin_unlock(&journal->j_state_lock);
1921 static journal_t *ext4_get_journal(struct super_block *sb,
1922 unsigned int journal_inum)
1924 struct inode *journal_inode;
1927 /* First, test for the existence of a valid inode on disk. Bad
1928 * things happen if we iget() an unused inode, as the subsequent
1929 * iput() will try to delete it. */
1931 journal_inode = iget(sb, journal_inum);
1932 if (!journal_inode) {
1933 printk(KERN_ERR "EXT4-fs: no journal found.\n");
1936 if (!journal_inode->i_nlink) {
1937 make_bad_inode(journal_inode);
1938 iput(journal_inode);
1939 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
1943 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1944 journal_inode, journal_inode->i_size);
1945 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
1946 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
1947 iput(journal_inode);
1951 journal = jbd2_journal_init_inode(journal_inode);
1953 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
1954 iput(journal_inode);
1957 journal->j_private = sb;
1958 ext4_init_journal_params(sb, journal);
1962 static journal_t *ext4_get_dev_journal(struct super_block *sb,
1965 struct buffer_head * bh;
1969 int hblock, blocksize;
1970 ext4_fsblk_t sb_block;
1971 unsigned long offset;
1972 struct ext4_super_block * es;
1973 struct block_device *bdev;
1975 bdev = ext4_blkdev_get(j_dev);
1979 if (bd_claim(bdev, sb)) {
1981 "EXT4: failed to claim external journal device.\n");
1986 blocksize = sb->s_blocksize;
1987 hblock = bdev_hardsect_size(bdev);
1988 if (blocksize < hblock) {
1990 "EXT4-fs: blocksize too small for journal device.\n");
1994 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
1995 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
1996 set_blocksize(bdev, blocksize);
1997 if (!(bh = __bread(bdev, sb_block, blocksize))) {
1998 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
1999 "external journal\n");
2003 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2004 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2005 !(le32_to_cpu(es->s_feature_incompat) &
2006 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2007 printk(KERN_ERR "EXT4-fs: external journal has "
2008 "bad superblock\n");
2013 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2014 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2019 len = ext4_blocks_count(es);
2020 start = sb_block + 1;
2021 brelse(bh); /* we're done with the superblock */
2023 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2024 start, len, blocksize);
2026 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2029 journal->j_private = sb;
2030 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2031 wait_on_buffer(journal->j_sb_buffer);
2032 if (!buffer_uptodate(journal->j_sb_buffer)) {
2033 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2036 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2037 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2038 "user (unsupported) - %d\n",
2039 be32_to_cpu(journal->j_superblock->s_nr_users));
2042 EXT4_SB(sb)->journal_bdev = bdev;
2043 ext4_init_journal_params(sb, journal);
2046 jbd2_journal_destroy(journal);
2048 ext4_blkdev_put(bdev);
2052 static int ext4_load_journal(struct super_block *sb,
2053 struct ext4_super_block *es,
2054 unsigned long journal_devnum)
2057 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2060 int really_read_only;
2062 if (journal_devnum &&
2063 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2064 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2065 "numbers have changed\n");
2066 journal_dev = new_decode_dev(journal_devnum);
2068 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2070 really_read_only = bdev_read_only(sb->s_bdev);
2073 * Are we loading a blank journal or performing recovery after a
2074 * crash? For recovery, we need to check in advance whether we
2075 * can get read-write access to the device.
2078 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2079 if (sb->s_flags & MS_RDONLY) {
2080 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2081 "required on readonly filesystem.\n");
2082 if (really_read_only) {
2083 printk(KERN_ERR "EXT4-fs: write access "
2084 "unavailable, cannot proceed.\n");
2087 printk (KERN_INFO "EXT4-fs: write access will "
2088 "be enabled during recovery.\n");
2092 if (journal_inum && journal_dev) {
2093 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2094 "and inode journals!\n");
2099 if (!(journal = ext4_get_journal(sb, journal_inum)))
2102 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2106 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2107 err = jbd2_journal_update_format(journal);
2109 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2110 jbd2_journal_destroy(journal);
2115 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2116 err = jbd2_journal_wipe(journal, !really_read_only);
2118 err = jbd2_journal_load(journal);
2121 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2122 jbd2_journal_destroy(journal);
2126 EXT4_SB(sb)->s_journal = journal;
2127 ext4_clear_journal_err(sb, es);
2129 if (journal_devnum &&
2130 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2131 es->s_journal_dev = cpu_to_le32(journal_devnum);
2134 /* Make sure we flush the recovery flag to disk. */
2135 ext4_commit_super(sb, es, 1);
2141 static int ext4_create_journal(struct super_block * sb,
2142 struct ext4_super_block * es,
2143 unsigned int journal_inum)
2147 if (sb->s_flags & MS_RDONLY) {
2148 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2149 "create journal.\n");
2153 if (!(journal = ext4_get_journal(sb, journal_inum)))
2156 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2159 if (jbd2_journal_create(journal)) {
2160 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2161 jbd2_journal_destroy(journal);
2165 EXT4_SB(sb)->s_journal = journal;
2167 ext4_update_dynamic_rev(sb);
2168 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2169 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2171 es->s_journal_inum = cpu_to_le32(journal_inum);
2174 /* Make sure we flush the recovery flag to disk. */
2175 ext4_commit_super(sb, es, 1);
2180 static void ext4_commit_super (struct super_block * sb,
2181 struct ext4_super_block * es,
2184 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2188 es->s_wtime = cpu_to_le32(get_seconds());
2189 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2190 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2191 BUFFER_TRACE(sbh, "marking dirty");
2192 mark_buffer_dirty(sbh);
2194 sync_dirty_buffer(sbh);
2199 * Have we just finished recovery? If so, and if we are mounting (or
2200 * remounting) the filesystem readonly, then we will end up with a
2201 * consistent fs on disk. Record that fact.
2203 static void ext4_mark_recovery_complete(struct super_block * sb,
2204 struct ext4_super_block * es)
2206 journal_t *journal = EXT4_SB(sb)->s_journal;
2208 jbd2_journal_lock_updates(journal);
2209 jbd2_journal_flush(journal);
2210 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2211 sb->s_flags & MS_RDONLY) {
2212 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2214 ext4_commit_super(sb, es, 1);
2216 jbd2_journal_unlock_updates(journal);
2220 * If we are mounting (or read-write remounting) a filesystem whose journal
2221 * has recorded an error from a previous lifetime, move that error to the
2222 * main filesystem now.
2224 static void ext4_clear_journal_err(struct super_block * sb,
2225 struct ext4_super_block * es)
2231 journal = EXT4_SB(sb)->s_journal;
2234 * Now check for any error status which may have been recorded in the
2235 * journal by a prior ext4_error() or ext4_abort()
2238 j_errno = jbd2_journal_errno(journal);
2242 errstr = ext4_decode_error(sb, j_errno, nbuf);
2243 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
2244 "from previous mount: %s", errstr);
2245 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
2246 "filesystem check.");
2248 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2249 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2250 ext4_commit_super (sb, es, 1);
2252 jbd2_journal_clear_err(journal);
2257 * Force the running and committing transactions to commit,
2258 * and wait on the commit.
2260 int ext4_force_commit(struct super_block *sb)
2265 if (sb->s_flags & MS_RDONLY)
2268 journal = EXT4_SB(sb)->s_journal;
2270 ret = ext4_journal_force_commit(journal);
2275 * Ext4 always journals updates to the superblock itself, so we don't
2276 * have to propagate any other updates to the superblock on disk at this
2277 * point. Just start an async writeback to get the buffers on their way
2280 * This implicitly triggers the writebehind on sync().
2283 static void ext4_write_super (struct super_block * sb)
2285 if (mutex_trylock(&sb->s_lock) != 0)
2290 static int ext4_sync_fs(struct super_block *sb, int wait)
2295 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2297 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2303 * LVM calls this function before a (read-only) snapshot is created. This
2304 * gives us a chance to flush the journal completely and mark the fs clean.
2306 static void ext4_write_super_lockfs(struct super_block *sb)
2310 if (!(sb->s_flags & MS_RDONLY)) {
2311 journal_t *journal = EXT4_SB(sb)->s_journal;
2313 /* Now we set up the journal barrier. */
2314 jbd2_journal_lock_updates(journal);
2315 jbd2_journal_flush(journal);
2317 /* Journal blocked and flushed, clear needs_recovery flag. */
2318 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2319 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2324 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2325 * flag here, even though the filesystem is not technically dirty yet.
2327 static void ext4_unlockfs(struct super_block *sb)
2329 if (!(sb->s_flags & MS_RDONLY)) {
2331 /* Reser the needs_recovery flag before the fs is unlocked. */
2332 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2333 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2335 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2339 static int ext4_remount (struct super_block * sb, int * flags, char * data)
2341 struct ext4_super_block * es;
2342 struct ext4_sb_info *sbi = EXT4_SB(sb);
2343 ext4_fsblk_t n_blocks_count = 0;
2344 unsigned long old_sb_flags;
2345 struct ext4_mount_options old_opts;
2351 /* Store the original options */
2352 old_sb_flags = sb->s_flags;
2353 old_opts.s_mount_opt = sbi->s_mount_opt;
2354 old_opts.s_resuid = sbi->s_resuid;
2355 old_opts.s_resgid = sbi->s_resgid;
2356 old_opts.s_commit_interval = sbi->s_commit_interval;
2358 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2359 for (i = 0; i < MAXQUOTAS; i++)
2360 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2364 * Allow the "check" option to be passed as a remount option.
2366 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2371 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2372 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
2374 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2375 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2379 ext4_init_journal_params(sb, sbi->s_journal);
2381 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2382 n_blocks_count > ext4_blocks_count(es)) {
2383 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
2388 if (*flags & MS_RDONLY) {
2390 * First of all, the unconditional stuff we have to do
2391 * to disable replay of the journal when we next remount
2393 sb->s_flags |= MS_RDONLY;
2396 * OK, test if we are remounting a valid rw partition
2397 * readonly, and if so set the rdonly flag and then
2398 * mark the partition as valid again.
2400 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2401 (sbi->s_mount_state & EXT4_VALID_FS))
2402 es->s_state = cpu_to_le16(sbi->s_mount_state);
2404 ext4_mark_recovery_complete(sb, es);
2407 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2408 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2409 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2410 "remount RDWR because of unsupported "
2411 "optional features (%x).\n",
2412 sb->s_id, le32_to_cpu(ret));
2417 * Mounting a RDONLY partition read-write, so reread
2418 * and store the current valid flag. (It may have
2419 * been changed by e2fsck since we originally mounted
2422 ext4_clear_journal_err(sb, es);
2423 sbi->s_mount_state = le16_to_cpu(es->s_state);
2424 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
2426 if (!ext4_setup_super (sb, es, 0))
2427 sb->s_flags &= ~MS_RDONLY;
2431 /* Release old quota file names */
2432 for (i = 0; i < MAXQUOTAS; i++)
2433 if (old_opts.s_qf_names[i] &&
2434 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2435 kfree(old_opts.s_qf_names[i]);
2439 sb->s_flags = old_sb_flags;
2440 sbi->s_mount_opt = old_opts.s_mount_opt;
2441 sbi->s_resuid = old_opts.s_resuid;
2442 sbi->s_resgid = old_opts.s_resgid;
2443 sbi->s_commit_interval = old_opts.s_commit_interval;
2445 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2446 for (i = 0; i < MAXQUOTAS; i++) {
2447 if (sbi->s_qf_names[i] &&
2448 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2449 kfree(sbi->s_qf_names[i]);
2450 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2456 static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
2458 struct super_block *sb = dentry->d_sb;
2459 struct ext4_sb_info *sbi = EXT4_SB(sb);
2460 struct ext4_super_block *es = sbi->s_es;
2461 ext4_fsblk_t overhead;
2464 if (test_opt (sb, MINIX_DF))
2467 unsigned long ngroups;
2468 ngroups = EXT4_SB(sb)->s_groups_count;
2472 * Compute the overhead (FS structures)
2476 * All of the blocks before first_data_block are
2479 overhead = le32_to_cpu(es->s_first_data_block);
2482 * Add the overhead attributed to the superblock and
2483 * block group descriptors. If the sparse superblocks
2484 * feature is turned on, then not all groups have this.
2486 for (i = 0; i < ngroups; i++) {
2487 overhead += ext4_bg_has_super(sb, i) +
2488 ext4_bg_num_gdb(sb, i);
2493 * Every block group has an inode bitmap, a block
2494 * bitmap, and an inode table.
2496 overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
2499 buf->f_type = EXT4_SUPER_MAGIC;
2500 buf->f_bsize = sb->s_blocksize;
2501 buf->f_blocks = ext4_blocks_count(es) - overhead;
2502 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
2503 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2504 if (buf->f_bfree < ext4_r_blocks_count(es))
2506 buf->f_files = le32_to_cpu(es->s_inodes_count);
2507 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
2508 buf->f_namelen = EXT4_NAME_LEN;
2512 /* Helper function for writing quotas on sync - we need to start transaction before quota file
2513 * is locked for write. Otherwise the are possible deadlocks:
2514 * Process 1 Process 2
2515 * ext4_create() quota_sync()
2516 * jbd2_journal_start() write_dquot()
2517 * DQUOT_INIT() down(dqio_mutex)
2518 * down(dqio_mutex) jbd2_journal_start()
2524 static inline struct inode *dquot_to_inode(struct dquot *dquot)
2526 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2529 static int ext4_dquot_initialize(struct inode *inode, int type)
2534 /* We may create quota structure so we need to reserve enough blocks */
2535 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
2537 return PTR_ERR(handle);
2538 ret = dquot_initialize(inode, type);
2539 err = ext4_journal_stop(handle);
2545 static int ext4_dquot_drop(struct inode *inode)
2550 /* We may delete quota structure so we need to reserve enough blocks */
2551 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
2553 return PTR_ERR(handle);
2554 ret = dquot_drop(inode);
2555 err = ext4_journal_stop(handle);
2561 static int ext4_write_dquot(struct dquot *dquot)
2565 struct inode *inode;
2567 inode = dquot_to_inode(dquot);
2568 handle = ext4_journal_start(inode,
2569 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2571 return PTR_ERR(handle);
2572 ret = dquot_commit(dquot);
2573 err = ext4_journal_stop(handle);
2579 static int ext4_acquire_dquot(struct dquot *dquot)
2584 handle = ext4_journal_start(dquot_to_inode(dquot),
2585 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2587 return PTR_ERR(handle);
2588 ret = dquot_acquire(dquot);
2589 err = ext4_journal_stop(handle);
2595 static int ext4_release_dquot(struct dquot *dquot)
2600 handle = ext4_journal_start(dquot_to_inode(dquot),
2601 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2603 return PTR_ERR(handle);
2604 ret = dquot_release(dquot);
2605 err = ext4_journal_stop(handle);
2611 static int ext4_mark_dquot_dirty(struct dquot *dquot)
2613 /* Are we journalling quotas? */
2614 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2615 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2616 dquot_mark_dquot_dirty(dquot);
2617 return ext4_write_dquot(dquot);
2619 return dquot_mark_dquot_dirty(dquot);
2623 static int ext4_write_info(struct super_block *sb, int type)
2628 /* Data block + inode block */
2629 handle = ext4_journal_start(sb->s_root->d_inode, 2);
2631 return PTR_ERR(handle);
2632 ret = dquot_commit_info(sb, type);
2633 err = ext4_journal_stop(handle);
2640 * Turn on quotas during mount time - we need to find
2641 * the quota file and such...
2643 static int ext4_quota_on_mount(struct super_block *sb, int type)
2645 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2646 EXT4_SB(sb)->s_jquota_fmt, type);
2650 * Standard function to be called on quota_on
2652 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
2656 struct nameidata nd;
2658 if (!test_opt(sb, QUOTA))
2660 /* Not journalling quota? */
2661 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2662 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
2663 return vfs_quota_on(sb, type, format_id, path);
2664 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2667 /* Quotafile not on the same filesystem? */
2668 if (nd.mnt->mnt_sb != sb) {
2672 /* Quotafile not of fs root? */
2673 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2675 "EXT4-fs: Quota file not on filesystem root. "
2676 "Journalled quota will not work.\n");
2678 return vfs_quota_on(sb, type, format_id, path);
2681 /* Read data from quotafile - avoid pagecache and such because we cannot afford
2682 * acquiring the locks... As quota files are never truncated and quota code
2683 * itself serializes the operations (and noone else should touch the files)
2684 * we don't have to be afraid of races */
2685 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
2686 size_t len, loff_t off)
2688 struct inode *inode = sb_dqopt(sb)->files[type];
2689 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2691 int offset = off & (sb->s_blocksize - 1);
2694 struct buffer_head *bh;
2695 loff_t i_size = i_size_read(inode);
2699 if (off+len > i_size)
2702 while (toread > 0) {
2703 tocopy = sb->s_blocksize - offset < toread ?
2704 sb->s_blocksize - offset : toread;
2705 bh = ext4_bread(NULL, inode, blk, 0, &err);
2708 if (!bh) /* A hole? */
2709 memset(data, 0, tocopy);
2711 memcpy(data, bh->b_data+offset, tocopy);
2721 /* Write to quotafile (we know the transaction is already started and has
2722 * enough credits) */
2723 static ssize_t ext4_quota_write(struct super_block *sb, int type,
2724 const char *data, size_t len, loff_t off)
2726 struct inode *inode = sb_dqopt(sb)->files[type];
2727 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2729 int offset = off & (sb->s_blocksize - 1);
2731 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
2732 size_t towrite = len;
2733 struct buffer_head *bh;
2734 handle_t *handle = journal_current_handle();
2736 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2737 while (towrite > 0) {
2738 tocopy = sb->s_blocksize - offset < towrite ?
2739 sb->s_blocksize - offset : towrite;
2740 bh = ext4_bread(handle, inode, blk, 1, &err);
2743 if (journal_quota) {
2744 err = ext4_journal_get_write_access(handle, bh);
2751 memcpy(bh->b_data+offset, data, tocopy);
2752 flush_dcache_page(bh->b_page);
2755 err = ext4_journal_dirty_metadata(handle, bh);
2757 /* Always do at least ordered writes for quotas */
2758 err = ext4_journal_dirty_data(handle, bh);
2759 mark_buffer_dirty(bh);
2772 if (inode->i_size < off+len-towrite) {
2773 i_size_write(inode, off+len-towrite);
2774 EXT4_I(inode)->i_disksize = inode->i_size;
2777 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2778 ext4_mark_inode_dirty(handle, inode);
2779 mutex_unlock(&inode->i_mutex);
2780 return len - towrite;
2785 static int ext4_get_sb(struct file_system_type *fs_type,
2786 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2788 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
2791 static struct file_system_type ext4dev_fs_type = {
2792 .owner = THIS_MODULE,
2794 .get_sb = ext4_get_sb,
2795 .kill_sb = kill_block_super,
2796 .fs_flags = FS_REQUIRES_DEV,
2799 static int __init init_ext4_fs(void)
2801 int err = init_ext4_xattr();
2804 err = init_inodecache();
2807 err = register_filesystem(&ext4dev_fs_type);
2812 destroy_inodecache();
2818 static void __exit exit_ext4_fs(void)
2820 unregister_filesystem(&ext4dev_fs_type);
2821 destroy_inodecache();
2825 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2826 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
2827 MODULE_LICENSE("GPL");
2828 module_init(init_ext4_fs)
2829 module_exit(exit_ext4_fs)