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/exportfs.h>
33 #include <linux/vfs.h>
34 #include <linux/random.h>
35 #include <linux/mount.h>
36 #include <linux/namei.h>
37 #include <linux/quotaops.h>
38 #include <linux/seq_file.h>
40 #include <asm/uaccess.h>
46 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
47 unsigned long journal_devnum);
48 static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
50 static void ext4_commit_super (struct super_block * sb,
51 struct ext4_super_block * es,
53 static void ext4_mark_recovery_complete(struct super_block * sb,
54 struct ext4_super_block * es);
55 static void ext4_clear_journal_err(struct super_block * sb,
56 struct ext4_super_block * es);
57 static int ext4_sync_fs(struct super_block *sb, int wait);
58 static const char *ext4_decode_error(struct super_block * sb, int errno,
60 static int ext4_remount (struct super_block * sb, int * flags, char * data);
61 static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
62 static void ext4_unlockfs(struct super_block *sb);
63 static void ext4_write_super (struct super_block * sb);
64 static void ext4_write_super_lockfs(struct super_block *sb);
67 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
68 struct ext4_group_desc *bg)
70 return le32_to_cpu(bg->bg_block_bitmap) |
71 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
72 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
75 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
76 struct ext4_group_desc *bg)
78 return le32_to_cpu(bg->bg_inode_bitmap) |
79 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
80 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
83 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
84 struct ext4_group_desc *bg)
86 return le32_to_cpu(bg->bg_inode_table) |
87 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
88 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
91 void ext4_block_bitmap_set(struct super_block *sb,
92 struct ext4_group_desc *bg, ext4_fsblk_t blk)
94 bg->bg_block_bitmap = cpu_to_le32((u32)blk);
95 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
96 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
99 void ext4_inode_bitmap_set(struct super_block *sb,
100 struct ext4_group_desc *bg, ext4_fsblk_t blk)
102 bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
103 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
104 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
107 void ext4_inode_table_set(struct super_block *sb,
108 struct ext4_group_desc *bg, ext4_fsblk_t blk)
110 bg->bg_inode_table = cpu_to_le32((u32)blk);
111 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
112 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
116 * Wrappers for jbd2_journal_start/end.
118 * The only special thing we need to do here is to make sure that all
119 * journal_end calls result in the superblock being marked dirty, so
120 * that sync() will call the filesystem's write_super callback if
123 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
127 if (sb->s_flags & MS_RDONLY)
128 return ERR_PTR(-EROFS);
130 /* Special case here: if the journal has aborted behind our
131 * backs (eg. EIO in the commit thread), then we still need to
132 * take the FS itself readonly cleanly. */
133 journal = EXT4_SB(sb)->s_journal;
134 if (is_journal_aborted(journal)) {
135 ext4_abort(sb, __FUNCTION__,
136 "Detected aborted journal");
137 return ERR_PTR(-EROFS);
140 return jbd2_journal_start(journal, nblocks);
144 * The only special thing we need to do here is to make sure that all
145 * jbd2_journal_stop calls result in the superblock being marked dirty, so
146 * that sync() will call the filesystem's write_super callback if
149 int __ext4_journal_stop(const char *where, handle_t *handle)
151 struct super_block *sb;
155 sb = handle->h_transaction->t_journal->j_private;
157 rc = jbd2_journal_stop(handle);
162 __ext4_std_error(sb, where, err);
166 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
167 struct buffer_head *bh, handle_t *handle, int err)
170 const char *errstr = ext4_decode_error(NULL, err, nbuf);
173 BUFFER_TRACE(bh, "abort");
178 if (is_handle_aborted(handle))
181 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
182 caller, errstr, err_fn);
184 jbd2_journal_abort_handle(handle);
187 /* Deal with the reporting of failure conditions on a filesystem such as
188 * inconsistencies detected or read IO failures.
190 * On ext2, we can store the error state of the filesystem in the
191 * superblock. That is not possible on ext4, because we may have other
192 * write ordering constraints on the superblock which prevent us from
193 * writing it out straight away; and given that the journal is about to
194 * be aborted, we can't rely on the current, or future, transactions to
195 * write out the superblock safely.
197 * We'll just use the jbd2_journal_abort() error code to record an error in
198 * the journal instead. On recovery, the journal will compain about
199 * that error until we've noted it down and cleared it.
202 static void ext4_handle_error(struct super_block *sb)
204 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
206 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
207 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
209 if (sb->s_flags & MS_RDONLY)
212 if (!test_opt (sb, ERRORS_CONT)) {
213 journal_t *journal = EXT4_SB(sb)->s_journal;
215 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
217 jbd2_journal_abort(journal, -EIO);
219 if (test_opt (sb, ERRORS_RO)) {
220 printk (KERN_CRIT "Remounting filesystem read-only\n");
221 sb->s_flags |= MS_RDONLY;
223 ext4_commit_super(sb, es, 1);
224 if (test_opt(sb, ERRORS_PANIC))
225 panic("EXT4-fs (device %s): panic forced after error\n",
229 void ext4_error (struct super_block * sb, const char * function,
230 const char * fmt, ...)
235 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
240 ext4_handle_error(sb);
243 static const char *ext4_decode_error(struct super_block * sb, int errno,
250 errstr = "IO failure";
253 errstr = "Out of memory";
256 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
257 errstr = "Journal has aborted";
259 errstr = "Readonly filesystem";
262 /* If the caller passed in an extra buffer for unknown
263 * errors, textualise them now. Else we just return
266 /* Check for truncated error codes... */
267 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
276 /* __ext4_std_error decodes expected errors from journaling functions
277 * automatically and invokes the appropriate error response. */
279 void __ext4_std_error (struct super_block * sb, const char * function,
285 /* Special case: if the error is EROFS, and we're not already
286 * inside a transaction, then there's really no point in logging
288 if (errno == -EROFS && journal_current_handle() == NULL &&
289 (sb->s_flags & MS_RDONLY))
292 errstr = ext4_decode_error(sb, errno, nbuf);
293 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
294 sb->s_id, function, errstr);
296 ext4_handle_error(sb);
300 * ext4_abort is a much stronger failure handler than ext4_error. The
301 * abort function may be used to deal with unrecoverable failures such
302 * as journal IO errors or ENOMEM at a critical moment in log management.
304 * We unconditionally force the filesystem into an ABORT|READONLY state,
305 * unless the error response on the fs has been set to panic in which
306 * case we take the easy way out and panic immediately.
309 void ext4_abort (struct super_block * sb, const char * function,
310 const char * fmt, ...)
314 printk (KERN_CRIT "ext4_abort called.\n");
317 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
322 if (test_opt(sb, ERRORS_PANIC))
323 panic("EXT4-fs panic from previous error\n");
325 if (sb->s_flags & MS_RDONLY)
328 printk(KERN_CRIT "Remounting filesystem read-only\n");
329 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
330 sb->s_flags |= MS_RDONLY;
331 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
332 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
335 void ext4_warning (struct super_block * sb, const char * function,
336 const char * fmt, ...)
341 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
348 void ext4_update_dynamic_rev(struct super_block *sb)
350 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
352 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
355 ext4_warning(sb, __FUNCTION__,
356 "updating to rev %d because of new feature flag, "
357 "running e2fsck is recommended",
360 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
361 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
362 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
363 /* leave es->s_feature_*compat flags alone */
364 /* es->s_uuid will be set by e2fsck if empty */
367 * The rest of the superblock fields should be zero, and if not it
368 * means they are likely already in use, so leave them alone. We
369 * can leave it up to e2fsck to clean up any inconsistencies there.
374 * Open the external journal device
376 static struct block_device *ext4_blkdev_get(dev_t dev)
378 struct block_device *bdev;
379 char b[BDEVNAME_SIZE];
381 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
387 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
388 __bdevname(dev, b), PTR_ERR(bdev));
393 * Release the journal device
395 static int ext4_blkdev_put(struct block_device *bdev)
398 return blkdev_put(bdev);
401 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
403 struct block_device *bdev;
406 bdev = sbi->journal_bdev;
408 ret = ext4_blkdev_put(bdev);
409 sbi->journal_bdev = NULL;
414 static inline struct inode *orphan_list_entry(struct list_head *l)
416 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
419 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
423 printk(KERN_ERR "sb orphan head is %d\n",
424 le32_to_cpu(sbi->s_es->s_last_orphan));
426 printk(KERN_ERR "sb_info orphan list:\n");
427 list_for_each(l, &sbi->s_orphan) {
428 struct inode *inode = orphan_list_entry(l);
430 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
431 inode->i_sb->s_id, inode->i_ino, inode,
432 inode->i_mode, inode->i_nlink,
437 static void ext4_put_super (struct super_block * sb)
439 struct ext4_sb_info *sbi = EXT4_SB(sb);
440 struct ext4_super_block *es = sbi->s_es;
443 ext4_ext_release(sb);
444 ext4_xattr_put_super(sb);
445 jbd2_journal_destroy(sbi->s_journal);
446 if (!(sb->s_flags & MS_RDONLY)) {
447 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
448 es->s_state = cpu_to_le16(sbi->s_mount_state);
449 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
450 mark_buffer_dirty(sbi->s_sbh);
451 ext4_commit_super(sb, es, 1);
454 for (i = 0; i < sbi->s_gdb_count; i++)
455 brelse(sbi->s_group_desc[i]);
456 kfree(sbi->s_group_desc);
457 percpu_counter_destroy(&sbi->s_freeblocks_counter);
458 percpu_counter_destroy(&sbi->s_freeinodes_counter);
459 percpu_counter_destroy(&sbi->s_dirs_counter);
462 for (i = 0; i < MAXQUOTAS; i++)
463 kfree(sbi->s_qf_names[i]);
466 /* Debugging code just in case the in-memory inode orphan list
467 * isn't empty. The on-disk one can be non-empty if we've
468 * detected an error and taken the fs readonly, but the
469 * in-memory list had better be clean by this point. */
470 if (!list_empty(&sbi->s_orphan))
471 dump_orphan_list(sb, sbi);
472 J_ASSERT(list_empty(&sbi->s_orphan));
474 invalidate_bdev(sb->s_bdev);
475 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
477 * Invalidate the journal device's buffers. We don't want them
478 * floating about in memory - the physical journal device may
479 * hotswapped, and it breaks the `ro-after' testing code.
481 sync_blockdev(sbi->journal_bdev);
482 invalidate_bdev(sbi->journal_bdev);
483 ext4_blkdev_remove(sbi);
485 sb->s_fs_info = NULL;
490 static struct kmem_cache *ext4_inode_cachep;
493 * Called inside transaction, so use GFP_NOFS
495 static struct inode *ext4_alloc_inode(struct super_block *sb)
497 struct ext4_inode_info *ei;
499 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
502 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
503 ei->i_acl = EXT4_ACL_NOT_CACHED;
504 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
506 ei->i_block_alloc_info = NULL;
507 ei->vfs_inode.i_version = 1;
508 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
509 return &ei->vfs_inode;
512 static void ext4_destroy_inode(struct inode *inode)
514 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
515 printk("EXT4 Inode %p: orphan list check failed!\n",
517 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
518 EXT4_I(inode), sizeof(struct ext4_inode_info),
522 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
525 static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
527 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
529 INIT_LIST_HEAD(&ei->i_orphan);
530 #ifdef CONFIG_EXT4DEV_FS_XATTR
531 init_rwsem(&ei->xattr_sem);
533 mutex_init(&ei->truncate_mutex);
534 inode_init_once(&ei->vfs_inode);
537 static int init_inodecache(void)
539 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
540 sizeof(struct ext4_inode_info),
541 0, (SLAB_RECLAIM_ACCOUNT|
544 if (ext4_inode_cachep == NULL)
549 static void destroy_inodecache(void)
551 kmem_cache_destroy(ext4_inode_cachep);
554 static void ext4_clear_inode(struct inode *inode)
556 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
557 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
558 if (EXT4_I(inode)->i_acl &&
559 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
560 posix_acl_release(EXT4_I(inode)->i_acl);
561 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
563 if (EXT4_I(inode)->i_default_acl &&
564 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
565 posix_acl_release(EXT4_I(inode)->i_default_acl);
566 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
569 ext4_discard_reservation(inode);
570 EXT4_I(inode)->i_block_alloc_info = NULL;
575 static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
577 #if defined(CONFIG_QUOTA)
578 struct ext4_sb_info *sbi = EXT4_SB(sb);
580 if (sbi->s_jquota_fmt)
581 seq_printf(seq, ",jqfmt=%s",
582 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
584 if (sbi->s_qf_names[USRQUOTA])
585 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
587 if (sbi->s_qf_names[GRPQUOTA])
588 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
590 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
591 seq_puts(seq, ",usrquota");
593 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
594 seq_puts(seq, ",grpquota");
598 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
600 struct super_block *sb = vfs->mnt_sb;
602 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
603 seq_puts(seq, ",data=journal");
604 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
605 seq_puts(seq, ",data=ordered");
606 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
607 seq_puts(seq, ",data=writeback");
609 ext4_show_quota_options(seq, sb);
615 static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
618 unsigned long ino = objp[0];
619 __u32 generation = objp[1];
621 struct dentry *result;
623 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
624 return ERR_PTR(-ESTALE);
625 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
626 return ERR_PTR(-ESTALE);
628 /* iget isn't really right if the inode is currently unallocated!!
630 * ext4_read_inode will return a bad_inode if the inode had been
631 * deleted, so we should be safe.
633 * Currently we don't know the generation for parent directory, so
634 * a generation of 0 means "accept any"
636 inode = iget(sb, ino);
638 return ERR_PTR(-ENOMEM);
639 if (is_bad_inode(inode) ||
640 (generation && inode->i_generation != generation)) {
642 return ERR_PTR(-ESTALE);
644 /* now to find a dentry.
645 * If possible, get a well-connected one
647 result = d_alloc_anon(inode);
650 return ERR_PTR(-ENOMEM);
656 #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
657 #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
659 static int ext4_dquot_initialize(struct inode *inode, int type);
660 static int ext4_dquot_drop(struct inode *inode);
661 static int ext4_write_dquot(struct dquot *dquot);
662 static int ext4_acquire_dquot(struct dquot *dquot);
663 static int ext4_release_dquot(struct dquot *dquot);
664 static int ext4_mark_dquot_dirty(struct dquot *dquot);
665 static int ext4_write_info(struct super_block *sb, int type);
666 static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
667 static int ext4_quota_on_mount(struct super_block *sb, int type);
668 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
669 size_t len, loff_t off);
670 static ssize_t ext4_quota_write(struct super_block *sb, int type,
671 const char *data, size_t len, loff_t off);
673 static struct dquot_operations ext4_quota_operations = {
674 .initialize = ext4_dquot_initialize,
675 .drop = ext4_dquot_drop,
676 .alloc_space = dquot_alloc_space,
677 .alloc_inode = dquot_alloc_inode,
678 .free_space = dquot_free_space,
679 .free_inode = dquot_free_inode,
680 .transfer = dquot_transfer,
681 .write_dquot = ext4_write_dquot,
682 .acquire_dquot = ext4_acquire_dquot,
683 .release_dquot = ext4_release_dquot,
684 .mark_dirty = ext4_mark_dquot_dirty,
685 .write_info = ext4_write_info
688 static struct quotactl_ops ext4_qctl_operations = {
689 .quota_on = ext4_quota_on,
690 .quota_off = vfs_quota_off,
691 .quota_sync = vfs_quota_sync,
692 .get_info = vfs_get_dqinfo,
693 .set_info = vfs_set_dqinfo,
694 .get_dqblk = vfs_get_dqblk,
695 .set_dqblk = vfs_set_dqblk
699 static const struct super_operations ext4_sops = {
700 .alloc_inode = ext4_alloc_inode,
701 .destroy_inode = ext4_destroy_inode,
702 .read_inode = ext4_read_inode,
703 .write_inode = ext4_write_inode,
704 .dirty_inode = ext4_dirty_inode,
705 .delete_inode = ext4_delete_inode,
706 .put_super = ext4_put_super,
707 .write_super = ext4_write_super,
708 .sync_fs = ext4_sync_fs,
709 .write_super_lockfs = ext4_write_super_lockfs,
710 .unlockfs = ext4_unlockfs,
711 .statfs = ext4_statfs,
712 .remount_fs = ext4_remount,
713 .clear_inode = ext4_clear_inode,
714 .show_options = ext4_show_options,
716 .quota_read = ext4_quota_read,
717 .quota_write = ext4_quota_write,
721 static struct export_operations ext4_export_ops = {
722 .get_parent = ext4_get_parent,
723 .get_dentry = ext4_get_dentry,
727 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
728 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
729 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
730 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
731 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
732 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
733 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
734 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
735 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
736 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
737 Opt_grpquota, Opt_extents, Opt_noextents,
740 static match_table_t tokens = {
741 {Opt_bsd_df, "bsddf"},
742 {Opt_minix_df, "minixdf"},
743 {Opt_grpid, "grpid"},
744 {Opt_grpid, "bsdgroups"},
745 {Opt_nogrpid, "nogrpid"},
746 {Opt_nogrpid, "sysvgroups"},
747 {Opt_resgid, "resgid=%u"},
748 {Opt_resuid, "resuid=%u"},
750 {Opt_err_cont, "errors=continue"},
751 {Opt_err_panic, "errors=panic"},
752 {Opt_err_ro, "errors=remount-ro"},
753 {Opt_nouid32, "nouid32"},
754 {Opt_nocheck, "nocheck"},
755 {Opt_nocheck, "check=none"},
756 {Opt_debug, "debug"},
757 {Opt_oldalloc, "oldalloc"},
758 {Opt_orlov, "orlov"},
759 {Opt_user_xattr, "user_xattr"},
760 {Opt_nouser_xattr, "nouser_xattr"},
762 {Opt_noacl, "noacl"},
763 {Opt_reservation, "reservation"},
764 {Opt_noreservation, "noreservation"},
765 {Opt_noload, "noload"},
768 {Opt_commit, "commit=%u"},
769 {Opt_journal_update, "journal=update"},
770 {Opt_journal_inum, "journal=%u"},
771 {Opt_journal_dev, "journal_dev=%u"},
772 {Opt_abort, "abort"},
773 {Opt_data_journal, "data=journal"},
774 {Opt_data_ordered, "data=ordered"},
775 {Opt_data_writeback, "data=writeback"},
776 {Opt_offusrjquota, "usrjquota="},
777 {Opt_usrjquota, "usrjquota=%s"},
778 {Opt_offgrpjquota, "grpjquota="},
779 {Opt_grpjquota, "grpjquota=%s"},
780 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
781 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
782 {Opt_grpquota, "grpquota"},
783 {Opt_noquota, "noquota"},
784 {Opt_quota, "quota"},
785 {Opt_usrquota, "usrquota"},
786 {Opt_barrier, "barrier=%u"},
787 {Opt_extents, "extents"},
788 {Opt_noextents, "noextents"},
790 {Opt_resize, "resize"},
793 static ext4_fsblk_t get_sb_block(void **data)
795 ext4_fsblk_t sb_block;
796 char *options = (char *) *data;
798 if (!options || strncmp(options, "sb=", 3) != 0)
799 return 1; /* Default location */
801 /*todo: use simple_strtoll with >32bit ext4 */
802 sb_block = simple_strtoul(options, &options, 0);
803 if (*options && *options != ',') {
804 printk("EXT4-fs: Invalid sb specification: %s\n",
810 *data = (void *) options;
814 static int parse_options (char *options, struct super_block *sb,
815 unsigned int *inum, unsigned long *journal_devnum,
816 ext4_fsblk_t *n_blocks_count, int is_remount)
818 struct ext4_sb_info *sbi = EXT4_SB(sb);
820 substring_t args[MAX_OPT_ARGS];
831 while ((p = strsep (&options, ",")) != NULL) {
836 token = match_token(p, tokens, args);
839 clear_opt (sbi->s_mount_opt, MINIX_DF);
842 set_opt (sbi->s_mount_opt, MINIX_DF);
845 set_opt (sbi->s_mount_opt, GRPID);
848 clear_opt (sbi->s_mount_opt, GRPID);
851 if (match_int(&args[0], &option))
853 sbi->s_resuid = option;
856 if (match_int(&args[0], &option))
858 sbi->s_resgid = option;
861 /* handled by get_sb_block() instead of here */
862 /* *sb_block = match_int(&args[0]); */
865 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
866 clear_opt (sbi->s_mount_opt, ERRORS_RO);
867 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
870 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
871 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
872 set_opt (sbi->s_mount_opt, ERRORS_RO);
875 clear_opt (sbi->s_mount_opt, ERRORS_RO);
876 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
877 set_opt (sbi->s_mount_opt, ERRORS_CONT);
880 set_opt (sbi->s_mount_opt, NO_UID32);
883 clear_opt (sbi->s_mount_opt, CHECK);
886 set_opt (sbi->s_mount_opt, DEBUG);
889 set_opt (sbi->s_mount_opt, OLDALLOC);
892 clear_opt (sbi->s_mount_opt, OLDALLOC);
894 #ifdef CONFIG_EXT4DEV_FS_XATTR
896 set_opt (sbi->s_mount_opt, XATTR_USER);
898 case Opt_nouser_xattr:
899 clear_opt (sbi->s_mount_opt, XATTR_USER);
903 case Opt_nouser_xattr:
904 printk("EXT4 (no)user_xattr options not supported\n");
907 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
909 set_opt(sbi->s_mount_opt, POSIX_ACL);
912 clear_opt(sbi->s_mount_opt, POSIX_ACL);
917 printk("EXT4 (no)acl options not supported\n");
920 case Opt_reservation:
921 set_opt(sbi->s_mount_opt, RESERVATION);
923 case Opt_noreservation:
924 clear_opt(sbi->s_mount_opt, RESERVATION);
926 case Opt_journal_update:
928 /* Eventually we will want to be able to create
929 a journal file here. For now, only allow the
930 user to specify an existing inode to be the
933 printk(KERN_ERR "EXT4-fs: cannot specify "
934 "journal on remount\n");
937 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
939 case Opt_journal_inum:
941 printk(KERN_ERR "EXT4-fs: cannot specify "
942 "journal on remount\n");
945 if (match_int(&args[0], &option))
949 case Opt_journal_dev:
951 printk(KERN_ERR "EXT4-fs: cannot specify "
952 "journal on remount\n");
955 if (match_int(&args[0], &option))
957 *journal_devnum = option;
960 set_opt (sbi->s_mount_opt, NOLOAD);
963 if (match_int(&args[0], &option))
968 option = JBD_DEFAULT_MAX_COMMIT_AGE;
969 sbi->s_commit_interval = HZ * option;
971 case Opt_data_journal:
972 data_opt = EXT4_MOUNT_JOURNAL_DATA;
974 case Opt_data_ordered:
975 data_opt = EXT4_MOUNT_ORDERED_DATA;
977 case Opt_data_writeback:
978 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
981 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
984 "EXT4-fs: cannot change data "
985 "mode on remount\n");
989 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
990 sbi->s_mount_opt |= data_opt;
1000 if (sb_any_quota_enabled(sb)) {
1002 "EXT4-fs: Cannot change journalled "
1003 "quota options when quota turned on.\n");
1006 qname = match_strdup(&args[0]);
1009 "EXT4-fs: not enough memory for "
1010 "storing quotafile name.\n");
1013 if (sbi->s_qf_names[qtype] &&
1014 strcmp(sbi->s_qf_names[qtype], qname)) {
1016 "EXT4-fs: %s quota file already "
1017 "specified.\n", QTYPE2NAME(qtype));
1021 sbi->s_qf_names[qtype] = qname;
1022 if (strchr(sbi->s_qf_names[qtype], '/')) {
1024 "EXT4-fs: quotafile must be on "
1025 "filesystem root.\n");
1026 kfree(sbi->s_qf_names[qtype]);
1027 sbi->s_qf_names[qtype] = NULL;
1030 set_opt(sbi->s_mount_opt, QUOTA);
1032 case Opt_offusrjquota:
1035 case Opt_offgrpjquota:
1038 if (sb_any_quota_enabled(sb)) {
1039 printk(KERN_ERR "EXT4-fs: Cannot change "
1040 "journalled quota options when "
1041 "quota turned on.\n");
1045 * The space will be released later when all options
1046 * are confirmed to be correct
1048 sbi->s_qf_names[qtype] = NULL;
1050 case Opt_jqfmt_vfsold:
1051 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1053 case Opt_jqfmt_vfsv0:
1054 sbi->s_jquota_fmt = QFMT_VFS_V0;
1058 set_opt(sbi->s_mount_opt, QUOTA);
1059 set_opt(sbi->s_mount_opt, USRQUOTA);
1062 set_opt(sbi->s_mount_opt, QUOTA);
1063 set_opt(sbi->s_mount_opt, GRPQUOTA);
1066 if (sb_any_quota_enabled(sb)) {
1067 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1068 "options when quota turned on.\n");
1071 clear_opt(sbi->s_mount_opt, QUOTA);
1072 clear_opt(sbi->s_mount_opt, USRQUOTA);
1073 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1081 case Opt_offusrjquota:
1082 case Opt_offgrpjquota:
1083 case Opt_jqfmt_vfsold:
1084 case Opt_jqfmt_vfsv0:
1086 "EXT4-fs: journalled quota options not "
1093 set_opt(sbi->s_mount_opt, ABORT);
1096 if (match_int(&args[0], &option))
1099 set_opt(sbi->s_mount_opt, BARRIER);
1101 clear_opt(sbi->s_mount_opt, BARRIER);
1107 printk("EXT4-fs: resize option only available "
1111 if (match_int(&args[0], &option) != 0)
1113 *n_blocks_count = option;
1116 set_opt(sbi->s_mount_opt, NOBH);
1119 clear_opt(sbi->s_mount_opt, NOBH);
1122 set_opt (sbi->s_mount_opt, EXTENTS);
1125 clear_opt (sbi->s_mount_opt, EXTENTS);
1129 "EXT4-fs: Unrecognized mount option \"%s\" "
1130 "or missing value\n", p);
1135 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1136 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1137 sbi->s_qf_names[USRQUOTA])
1138 clear_opt(sbi->s_mount_opt, USRQUOTA);
1140 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1141 sbi->s_qf_names[GRPQUOTA])
1142 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1144 if ((sbi->s_qf_names[USRQUOTA] &&
1145 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1146 (sbi->s_qf_names[GRPQUOTA] &&
1147 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1148 printk(KERN_ERR "EXT4-fs: old and new quota "
1149 "format mixing.\n");
1153 if (!sbi->s_jquota_fmt) {
1154 printk(KERN_ERR "EXT4-fs: journalled quota format "
1155 "not specified.\n");
1159 if (sbi->s_jquota_fmt) {
1160 printk(KERN_ERR "EXT4-fs: journalled quota format "
1161 "specified with no journalling "
1170 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1173 struct ext4_sb_info *sbi = EXT4_SB(sb);
1176 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1177 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
1178 "forcing read-only mode\n");
1183 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1184 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1185 "running e2fsck is recommended\n");
1186 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1187 printk (KERN_WARNING
1188 "EXT4-fs warning: mounting fs with errors, "
1189 "running e2fsck is recommended\n");
1190 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1191 le16_to_cpu(es->s_mnt_count) >=
1192 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1193 printk (KERN_WARNING
1194 "EXT4-fs warning: maximal mount count reached, "
1195 "running e2fsck is recommended\n");
1196 else if (le32_to_cpu(es->s_checkinterval) &&
1197 (le32_to_cpu(es->s_lastcheck) +
1198 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1199 printk (KERN_WARNING
1200 "EXT4-fs warning: checktime reached, "
1201 "running e2fsck is recommended\n");
1203 /* @@@ We _will_ want to clear the valid bit if we find
1204 * inconsistencies, to force a fsck at reboot. But for
1205 * a plain journaled filesystem we can keep it set as
1208 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
1210 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1211 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1212 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1213 es->s_mtime = cpu_to_le32(get_seconds());
1214 ext4_update_dynamic_rev(sb);
1215 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1217 ext4_commit_super(sb, es, 1);
1218 if (test_opt(sb, DEBUG))
1219 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1220 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1222 sbi->s_groups_count,
1223 EXT4_BLOCKS_PER_GROUP(sb),
1224 EXT4_INODES_PER_GROUP(sb),
1227 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1228 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
1229 char b[BDEVNAME_SIZE];
1231 printk("external journal on %s\n",
1232 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
1234 printk("internal journal\n");
1239 /* Called at mount-time, super-block is locked */
1240 static int ext4_check_descriptors (struct super_block * sb)
1242 struct ext4_sb_info *sbi = EXT4_SB(sb);
1243 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1244 ext4_fsblk_t last_block;
1245 ext4_fsblk_t block_bitmap;
1246 ext4_fsblk_t inode_bitmap;
1247 ext4_fsblk_t inode_table;
1248 struct ext4_group_desc * gdp = NULL;
1252 ext4_debug ("Checking group descriptors");
1254 for (i = 0; i < sbi->s_groups_count; i++)
1256 if (i == sbi->s_groups_count - 1)
1257 last_block = ext4_blocks_count(sbi->s_es) - 1;
1259 last_block = first_block +
1260 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1262 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1263 gdp = (struct ext4_group_desc *)
1264 sbi->s_group_desc[desc_block++]->b_data;
1265 block_bitmap = ext4_block_bitmap(sb, gdp);
1266 if (block_bitmap < first_block || block_bitmap > last_block)
1268 ext4_error (sb, "ext4_check_descriptors",
1269 "Block bitmap for group %d"
1270 " not in group (block %llu)!",
1274 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1275 if (inode_bitmap < first_block || inode_bitmap > last_block)
1277 ext4_error (sb, "ext4_check_descriptors",
1278 "Inode bitmap for group %d"
1279 " not in group (block %llu)!",
1283 inode_table = ext4_inode_table(sb, gdp);
1284 if (inode_table < first_block ||
1285 inode_table + sbi->s_itb_per_group > last_block)
1287 ext4_error (sb, "ext4_check_descriptors",
1288 "Inode table for group %d"
1289 " not in group (block %llu)!",
1293 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1294 gdp = (struct ext4_group_desc *)
1295 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
1298 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1299 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
1304 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1305 * the superblock) which were deleted from all directories, but held open by
1306 * a process at the time of a crash. We walk the list and try to delete these
1307 * inodes at recovery time (only with a read-write filesystem).
1309 * In order to keep the orphan inode chain consistent during traversal (in
1310 * case of crash during recovery), we link each inode into the superblock
1311 * orphan list_head and handle it the same way as an inode deletion during
1312 * normal operation (which journals the operations for us).
1314 * We only do an iget() and an iput() on each inode, which is very safe if we
1315 * accidentally point at an in-use or already deleted inode. The worst that
1316 * can happen in this case is that we get a "bit already cleared" message from
1317 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1318 * e2fsck was run on this filesystem, and it must have already done the orphan
1319 * inode cleanup for us, so we can safely abort without any further action.
1321 static void ext4_orphan_cleanup (struct super_block * sb,
1322 struct ext4_super_block * es)
1324 unsigned int s_flags = sb->s_flags;
1325 int nr_orphans = 0, nr_truncates = 0;
1329 if (!es->s_last_orphan) {
1330 jbd_debug(4, "no orphan inodes to clean up\n");
1334 if (bdev_read_only(sb->s_bdev)) {
1335 printk(KERN_ERR "EXT4-fs: write access "
1336 "unavailable, skipping orphan cleanup.\n");
1340 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1341 if (es->s_last_orphan)
1342 jbd_debug(1, "Errors on filesystem, "
1343 "clearing orphan list.\n");
1344 es->s_last_orphan = 0;
1345 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1349 if (s_flags & MS_RDONLY) {
1350 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1352 sb->s_flags &= ~MS_RDONLY;
1355 /* Needed for iput() to work correctly and not trash data */
1356 sb->s_flags |= MS_ACTIVE;
1357 /* Turn on quotas so that they are updated correctly */
1358 for (i = 0; i < MAXQUOTAS; i++) {
1359 if (EXT4_SB(sb)->s_qf_names[i]) {
1360 int ret = ext4_quota_on_mount(sb, i);
1363 "EXT4-fs: Cannot turn on journalled "
1364 "quota: error %d\n", ret);
1369 while (es->s_last_orphan) {
1370 struct inode *inode;
1373 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
1374 es->s_last_orphan = 0;
1378 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1380 if (inode->i_nlink) {
1382 "%s: truncating inode %lu to %Ld bytes\n",
1383 __FUNCTION__, inode->i_ino, inode->i_size);
1384 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1385 inode->i_ino, inode->i_size);
1386 ext4_truncate(inode);
1390 "%s: deleting unreferenced inode %lu\n",
1391 __FUNCTION__, inode->i_ino);
1392 jbd_debug(2, "deleting unreferenced inode %lu\n",
1396 iput(inode); /* The delete magic happens here! */
1399 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1402 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1403 sb->s_id, PLURAL(nr_orphans));
1405 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1406 sb->s_id, PLURAL(nr_truncates));
1408 /* Turn quotas off */
1409 for (i = 0; i < MAXQUOTAS; i++) {
1410 if (sb_dqopt(sb)->files[i])
1411 vfs_quota_off(sb, i);
1414 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1417 #define log2(n) ffz(~(n))
1420 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1421 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1422 * We need to be 1 filesystem block less than the 2^32 sector limit.
1424 static loff_t ext4_max_size(int bits)
1426 loff_t res = EXT4_NDIR_BLOCKS;
1427 /* This constant is calculated to be the largest file size for a
1428 * dense, 4k-blocksize file such that the total number of
1429 * sectors in the file, including data and all indirect blocks,
1430 * does not exceed 2^32. */
1431 const loff_t upper_limit = 0x1ff7fffd000LL;
1433 res += 1LL << (bits-2);
1434 res += 1LL << (2*(bits-2));
1435 res += 1LL << (3*(bits-2));
1437 if (res > upper_limit)
1442 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1443 ext4_fsblk_t logical_sb_block, int nr)
1445 struct ext4_sb_info *sbi = EXT4_SB(sb);
1446 unsigned long bg, first_meta_bg;
1449 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1451 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1453 return logical_sb_block + nr + 1;
1454 bg = sbi->s_desc_per_block * nr;
1455 if (ext4_bg_has_super(sb, bg))
1457 return (has_super + ext4_group_first_block_no(sb, bg));
1461 static int ext4_fill_super (struct super_block *sb, void *data, int silent)
1463 struct buffer_head * bh;
1464 struct ext4_super_block *es = NULL;
1465 struct ext4_sb_info *sbi;
1467 ext4_fsblk_t sb_block = get_sb_block(&data);
1468 ext4_fsblk_t logical_sb_block;
1469 unsigned long offset = 0;
1470 unsigned int journal_inum = 0;
1471 unsigned long journal_devnum = 0;
1472 unsigned long def_mount_opts;
1482 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1485 sb->s_fs_info = sbi;
1486 sbi->s_mount_opt = 0;
1487 sbi->s_resuid = EXT4_DEF_RESUID;
1488 sbi->s_resgid = EXT4_DEF_RESGID;
1492 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1494 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1499 * The ext4 superblock will not be buffer aligned for other than 1kB
1500 * block sizes. We need to calculate the offset from buffer start.
1502 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1503 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1504 offset = do_div(logical_sb_block, blocksize);
1506 logical_sb_block = sb_block;
1509 if (!(bh = sb_bread(sb, logical_sb_block))) {
1510 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
1514 * Note: s_es must be initialized as soon as possible because
1515 * some ext4 macro-instructions depend on its value
1517 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1519 sb->s_magic = le16_to_cpu(es->s_magic);
1520 if (sb->s_magic != EXT4_SUPER_MAGIC)
1523 /* Set defaults before we parse the mount options */
1524 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1525 if (def_mount_opts & EXT4_DEFM_DEBUG)
1526 set_opt(sbi->s_mount_opt, DEBUG);
1527 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1528 set_opt(sbi->s_mount_opt, GRPID);
1529 if (def_mount_opts & EXT4_DEFM_UID16)
1530 set_opt(sbi->s_mount_opt, NO_UID32);
1531 #ifdef CONFIG_EXT4DEV_FS_XATTR
1532 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1533 set_opt(sbi->s_mount_opt, XATTR_USER);
1535 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1536 if (def_mount_opts & EXT4_DEFM_ACL)
1537 set_opt(sbi->s_mount_opt, POSIX_ACL);
1539 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1540 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1541 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1542 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1543 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1544 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1546 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1547 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1548 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
1549 set_opt(sbi->s_mount_opt, ERRORS_RO);
1551 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1553 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1554 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1556 set_opt(sbi->s_mount_opt, RESERVATION);
1559 * turn on extents feature by default in ext4 filesystem
1560 * User -o noextents to turn it off
1562 set_opt(sbi->s_mount_opt, EXTENTS);
1564 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1568 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1569 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1571 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1572 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1573 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1574 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1576 "EXT4-fs warning: feature flags set on rev 0 fs, "
1577 "running e2fsck is recommended\n");
1579 * Check feature flags regardless of the revision level, since we
1580 * previously didn't change the revision level when setting the flags,
1581 * so there is a chance incompat flags are set on a rev 0 filesystem.
1583 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
1585 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
1586 "unsupported optional features (%x).\n",
1587 sb->s_id, le32_to_cpu(features));
1590 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
1591 if (!(sb->s_flags & MS_RDONLY) && features) {
1592 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
1593 "unsupported optional features (%x).\n",
1594 sb->s_id, le32_to_cpu(features));
1597 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1599 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1600 blocksize > EXT4_MAX_BLOCK_SIZE) {
1602 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
1603 blocksize, sb->s_id);
1607 hblock = bdev_hardsect_size(sb->s_bdev);
1608 if (sb->s_blocksize != blocksize) {
1610 * Make sure the blocksize for the filesystem is larger
1611 * than the hardware sectorsize for the machine.
1613 if (blocksize < hblock) {
1614 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
1615 "device blocksize %d.\n", blocksize, hblock);
1620 sb_set_blocksize(sb, blocksize);
1621 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1622 offset = do_div(logical_sb_block, blocksize);
1623 bh = sb_bread(sb, logical_sb_block);
1626 "EXT4-fs: Can't read superblock on 2nd try.\n");
1629 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
1631 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
1633 "EXT4-fs: Magic mismatch, very weird !\n");
1638 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
1640 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1641 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1642 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
1644 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1645 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1646 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1647 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1648 (sbi->s_inode_size > blocksize)) {
1650 "EXT4-fs: unsupported inode size: %d\n",
1654 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
1655 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
1657 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
1658 le32_to_cpu(es->s_log_frag_size);
1659 if (blocksize != sbi->s_frag_size) {
1661 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
1662 sbi->s_frag_size, blocksize);
1665 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
1666 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
1667 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
1668 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
1669 sbi->s_desc_size & (sbi->s_desc_size - 1)) {
1671 "EXT4-fs: unsupported descriptor size %lu\n",
1676 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
1677 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1678 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1679 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1680 if (EXT4_INODE_SIZE(sb) == 0)
1682 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
1683 if (sbi->s_inodes_per_block == 0)
1685 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1686 sbi->s_inodes_per_block;
1687 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
1689 sbi->s_mount_state = le16_to_cpu(es->s_state);
1690 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1691 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
1692 for (i=0; i < 4; i++)
1693 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1694 sbi->s_def_hash_version = es->s_def_hash_version;
1696 if (sbi->s_blocks_per_group > blocksize * 8) {
1698 "EXT4-fs: #blocks per group too big: %lu\n",
1699 sbi->s_blocks_per_group);
1702 if (sbi->s_frags_per_group > blocksize * 8) {
1704 "EXT4-fs: #fragments per group too big: %lu\n",
1705 sbi->s_frags_per_group);
1708 if (sbi->s_inodes_per_group > blocksize * 8) {
1710 "EXT4-fs: #inodes per group too big: %lu\n",
1711 sbi->s_inodes_per_group);
1715 if (ext4_blocks_count(es) >
1716 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1717 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
1718 " too large to mount safely\n", sb->s_id);
1719 if (sizeof(sector_t) < 8)
1720 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
1725 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1727 blocks_count = (ext4_blocks_count(es) -
1728 le32_to_cpu(es->s_first_data_block) +
1729 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1730 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1731 sbi->s_groups_count = blocks_count;
1732 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1733 EXT4_DESC_PER_BLOCK(sb);
1734 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1736 if (sbi->s_group_desc == NULL) {
1737 printk (KERN_ERR "EXT4-fs: not enough memory\n");
1741 bgl_lock_init(&sbi->s_blockgroup_lock);
1743 for (i = 0; i < db_count; i++) {
1744 block = descriptor_loc(sb, logical_sb_block, i);
1745 sbi->s_group_desc[i] = sb_bread(sb, block);
1746 if (!sbi->s_group_desc[i]) {
1747 printk (KERN_ERR "EXT4-fs: "
1748 "can't read group descriptor %d\n", i);
1753 if (!ext4_check_descriptors (sb)) {
1754 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
1757 sbi->s_gdb_count = db_count;
1758 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1759 spin_lock_init(&sbi->s_next_gen_lock);
1761 percpu_counter_init(&sbi->s_freeblocks_counter,
1762 ext4_count_free_blocks(sb));
1763 percpu_counter_init(&sbi->s_freeinodes_counter,
1764 ext4_count_free_inodes(sb));
1765 percpu_counter_init(&sbi->s_dirs_counter,
1766 ext4_count_dirs(sb));
1768 /* per fileystem reservation list head & lock */
1769 spin_lock_init(&sbi->s_rsv_window_lock);
1770 sbi->s_rsv_window_root = RB_ROOT;
1771 /* Add a single, static dummy reservation to the start of the
1772 * reservation window list --- it gives us a placeholder for
1773 * append-at-start-of-list which makes the allocation logic
1774 * _much_ simpler. */
1775 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1776 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1777 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1778 sbi->s_rsv_window_head.rsv_goal_size = 0;
1779 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
1782 * set up enough so that it can read an inode
1784 sb->s_op = &ext4_sops;
1785 sb->s_export_op = &ext4_export_ops;
1786 sb->s_xattr = ext4_xattr_handlers;
1788 sb->s_qcop = &ext4_qctl_operations;
1789 sb->dq_op = &ext4_quota_operations;
1791 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1795 needs_recovery = (es->s_last_orphan != 0 ||
1796 EXT4_HAS_INCOMPAT_FEATURE(sb,
1797 EXT4_FEATURE_INCOMPAT_RECOVER));
1800 * The first inode we look at is the journal inode. Don't try
1801 * root first: it may be modified in the journal!
1803 if (!test_opt(sb, NOLOAD) &&
1804 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1805 if (ext4_load_journal(sb, es, journal_devnum))
1807 } else if (journal_inum) {
1808 if (ext4_create_journal(sb, es, journal_inum))
1813 "ext4: No journal on filesystem on %s\n",
1818 if (ext4_blocks_count(es) > 0xffffffffULL &&
1819 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
1820 JBD2_FEATURE_INCOMPAT_64BIT)) {
1821 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
1825 /* We have now updated the journal if required, so we can
1826 * validate the data journaling mode. */
1827 switch (test_opt(sb, DATA_FLAGS)) {
1829 /* No mode set, assume a default based on the journal
1830 * capabilities: ORDERED_DATA if the journal can
1831 * cope, else JOURNAL_DATA
1833 if (jbd2_journal_check_available_features
1834 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
1835 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1837 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1840 case EXT4_MOUNT_ORDERED_DATA:
1841 case EXT4_MOUNT_WRITEBACK_DATA:
1842 if (!jbd2_journal_check_available_features
1843 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
1844 printk(KERN_ERR "EXT4-fs: Journal does not support "
1845 "requested data journaling mode\n");
1852 if (test_opt(sb, NOBH)) {
1853 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1854 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
1855 "its supported only with writeback mode\n");
1856 clear_opt(sbi->s_mount_opt, NOBH);
1860 * The jbd2_journal_load will have done any necessary log recovery,
1861 * so we can safely mount the rest of the filesystem now.
1864 root = iget(sb, EXT4_ROOT_INO);
1865 sb->s_root = d_alloc_root(root);
1867 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
1871 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1874 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
1878 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1880 /* determine the minimum size of new large inodes, if present */
1881 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
1882 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
1883 EXT4_GOOD_OLD_INODE_SIZE;
1884 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
1885 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
1886 if (sbi->s_want_extra_isize <
1887 le16_to_cpu(es->s_want_extra_isize))
1888 sbi->s_want_extra_isize =
1889 le16_to_cpu(es->s_want_extra_isize);
1890 if (sbi->s_want_extra_isize <
1891 le16_to_cpu(es->s_min_extra_isize))
1892 sbi->s_want_extra_isize =
1893 le16_to_cpu(es->s_min_extra_isize);
1896 /* Check if enough inode space is available */
1897 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
1898 sbi->s_inode_size) {
1899 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
1900 EXT4_GOOD_OLD_INODE_SIZE;
1901 printk(KERN_INFO "EXT4-fs: required extra inode space not"
1906 * akpm: core read_super() calls in here with the superblock locked.
1907 * That deadlocks, because orphan cleanup needs to lock the superblock
1908 * in numerous places. Here we just pop the lock - it's relatively
1909 * harmless, because we are now ready to accept write_super() requests,
1910 * and aviro says that's the only reason for hanging onto the
1913 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1914 ext4_orphan_cleanup(sb, es);
1915 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
1917 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1918 ext4_mark_recovery_complete(sb, es);
1919 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1920 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1921 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
1931 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
1936 jbd2_journal_destroy(sbi->s_journal);
1938 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1939 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1940 percpu_counter_destroy(&sbi->s_dirs_counter);
1942 for (i = 0; i < db_count; i++)
1943 brelse(sbi->s_group_desc[i]);
1944 kfree(sbi->s_group_desc);
1947 for (i = 0; i < MAXQUOTAS; i++)
1948 kfree(sbi->s_qf_names[i]);
1950 ext4_blkdev_remove(sbi);
1953 sb->s_fs_info = NULL;
1960 * Setup any per-fs journal parameters now. We'll do this both on
1961 * initial mount, once the journal has been initialised but before we've
1962 * done any recovery; and again on any subsequent remount.
1964 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
1966 struct ext4_sb_info *sbi = EXT4_SB(sb);
1968 if (sbi->s_commit_interval)
1969 journal->j_commit_interval = sbi->s_commit_interval;
1970 /* We could also set up an ext4-specific default for the commit
1971 * interval here, but for now we'll just fall back to the jbd
1974 spin_lock(&journal->j_state_lock);
1975 if (test_opt(sb, BARRIER))
1976 journal->j_flags |= JBD2_BARRIER;
1978 journal->j_flags &= ~JBD2_BARRIER;
1979 spin_unlock(&journal->j_state_lock);
1982 static journal_t *ext4_get_journal(struct super_block *sb,
1983 unsigned int journal_inum)
1985 struct inode *journal_inode;
1988 /* First, test for the existence of a valid inode on disk. Bad
1989 * things happen if we iget() an unused inode, as the subsequent
1990 * iput() will try to delete it. */
1992 journal_inode = iget(sb, journal_inum);
1993 if (!journal_inode) {
1994 printk(KERN_ERR "EXT4-fs: no journal found.\n");
1997 if (!journal_inode->i_nlink) {
1998 make_bad_inode(journal_inode);
1999 iput(journal_inode);
2000 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2004 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2005 journal_inode, journal_inode->i_size);
2006 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
2007 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2008 iput(journal_inode);
2012 journal = jbd2_journal_init_inode(journal_inode);
2014 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2015 iput(journal_inode);
2018 journal->j_private = sb;
2019 ext4_init_journal_params(sb, journal);
2023 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2026 struct buffer_head * bh;
2030 int hblock, blocksize;
2031 ext4_fsblk_t sb_block;
2032 unsigned long offset;
2033 struct ext4_super_block * es;
2034 struct block_device *bdev;
2036 bdev = ext4_blkdev_get(j_dev);
2040 if (bd_claim(bdev, sb)) {
2042 "EXT4: failed to claim external journal device.\n");
2047 blocksize = sb->s_blocksize;
2048 hblock = bdev_hardsect_size(bdev);
2049 if (blocksize < hblock) {
2051 "EXT4-fs: blocksize too small for journal device.\n");
2055 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2056 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2057 set_blocksize(bdev, blocksize);
2058 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2059 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2060 "external journal\n");
2064 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2065 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2066 !(le32_to_cpu(es->s_feature_incompat) &
2067 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2068 printk(KERN_ERR "EXT4-fs: external journal has "
2069 "bad superblock\n");
2074 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2075 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2080 len = ext4_blocks_count(es);
2081 start = sb_block + 1;
2082 brelse(bh); /* we're done with the superblock */
2084 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2085 start, len, blocksize);
2087 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2090 journal->j_private = sb;
2091 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2092 wait_on_buffer(journal->j_sb_buffer);
2093 if (!buffer_uptodate(journal->j_sb_buffer)) {
2094 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2097 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2098 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2099 "user (unsupported) - %d\n",
2100 be32_to_cpu(journal->j_superblock->s_nr_users));
2103 EXT4_SB(sb)->journal_bdev = bdev;
2104 ext4_init_journal_params(sb, journal);
2107 jbd2_journal_destroy(journal);
2109 ext4_blkdev_put(bdev);
2113 static int ext4_load_journal(struct super_block *sb,
2114 struct ext4_super_block *es,
2115 unsigned long journal_devnum)
2118 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2121 int really_read_only;
2123 if (journal_devnum &&
2124 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2125 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2126 "numbers have changed\n");
2127 journal_dev = new_decode_dev(journal_devnum);
2129 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2131 really_read_only = bdev_read_only(sb->s_bdev);
2134 * Are we loading a blank journal or performing recovery after a
2135 * crash? For recovery, we need to check in advance whether we
2136 * can get read-write access to the device.
2139 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2140 if (sb->s_flags & MS_RDONLY) {
2141 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2142 "required on readonly filesystem.\n");
2143 if (really_read_only) {
2144 printk(KERN_ERR "EXT4-fs: write access "
2145 "unavailable, cannot proceed.\n");
2148 printk (KERN_INFO "EXT4-fs: write access will "
2149 "be enabled during recovery.\n");
2153 if (journal_inum && journal_dev) {
2154 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2155 "and inode journals!\n");
2160 if (!(journal = ext4_get_journal(sb, journal_inum)))
2163 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2167 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2168 err = jbd2_journal_update_format(journal);
2170 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2171 jbd2_journal_destroy(journal);
2176 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2177 err = jbd2_journal_wipe(journal, !really_read_only);
2179 err = jbd2_journal_load(journal);
2182 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2183 jbd2_journal_destroy(journal);
2187 EXT4_SB(sb)->s_journal = journal;
2188 ext4_clear_journal_err(sb, es);
2190 if (journal_devnum &&
2191 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2192 es->s_journal_dev = cpu_to_le32(journal_devnum);
2195 /* Make sure we flush the recovery flag to disk. */
2196 ext4_commit_super(sb, es, 1);
2202 static int ext4_create_journal(struct super_block * sb,
2203 struct ext4_super_block * es,
2204 unsigned int journal_inum)
2209 if (sb->s_flags & MS_RDONLY) {
2210 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2211 "create journal.\n");
2215 journal = ext4_get_journal(sb, journal_inum);
2219 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2222 err = jbd2_journal_create(journal);
2224 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2225 jbd2_journal_destroy(journal);
2229 EXT4_SB(sb)->s_journal = journal;
2231 ext4_update_dynamic_rev(sb);
2232 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2233 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2235 es->s_journal_inum = cpu_to_le32(journal_inum);
2238 /* Make sure we flush the recovery flag to disk. */
2239 ext4_commit_super(sb, es, 1);
2244 static void ext4_commit_super (struct super_block * sb,
2245 struct ext4_super_block * es,
2248 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2252 es->s_wtime = cpu_to_le32(get_seconds());
2253 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2254 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2255 BUFFER_TRACE(sbh, "marking dirty");
2256 mark_buffer_dirty(sbh);
2258 sync_dirty_buffer(sbh);
2263 * Have we just finished recovery? If so, and if we are mounting (or
2264 * remounting) the filesystem readonly, then we will end up with a
2265 * consistent fs on disk. Record that fact.
2267 static void ext4_mark_recovery_complete(struct super_block * sb,
2268 struct ext4_super_block * es)
2270 journal_t *journal = EXT4_SB(sb)->s_journal;
2272 jbd2_journal_lock_updates(journal);
2273 jbd2_journal_flush(journal);
2275 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2276 sb->s_flags & MS_RDONLY) {
2277 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2279 ext4_commit_super(sb, es, 1);
2282 jbd2_journal_unlock_updates(journal);
2286 * If we are mounting (or read-write remounting) a filesystem whose journal
2287 * has recorded an error from a previous lifetime, move that error to the
2288 * main filesystem now.
2290 static void ext4_clear_journal_err(struct super_block * sb,
2291 struct ext4_super_block * es)
2297 journal = EXT4_SB(sb)->s_journal;
2300 * Now check for any error status which may have been recorded in the
2301 * journal by a prior ext4_error() or ext4_abort()
2304 j_errno = jbd2_journal_errno(journal);
2308 errstr = ext4_decode_error(sb, j_errno, nbuf);
2309 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
2310 "from previous mount: %s", errstr);
2311 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
2312 "filesystem check.");
2314 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2315 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2316 ext4_commit_super (sb, es, 1);
2318 jbd2_journal_clear_err(journal);
2323 * Force the running and committing transactions to commit,
2324 * and wait on the commit.
2326 int ext4_force_commit(struct super_block *sb)
2331 if (sb->s_flags & MS_RDONLY)
2334 journal = EXT4_SB(sb)->s_journal;
2336 ret = ext4_journal_force_commit(journal);
2341 * Ext4 always journals updates to the superblock itself, so we don't
2342 * have to propagate any other updates to the superblock on disk at this
2343 * point. Just start an async writeback to get the buffers on their way
2346 * This implicitly triggers the writebehind on sync().
2349 static void ext4_write_super (struct super_block * sb)
2351 if (mutex_trylock(&sb->s_lock) != 0)
2356 static int ext4_sync_fs(struct super_block *sb, int wait)
2361 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2363 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2369 * LVM calls this function before a (read-only) snapshot is created. This
2370 * gives us a chance to flush the journal completely and mark the fs clean.
2372 static void ext4_write_super_lockfs(struct super_block *sb)
2376 if (!(sb->s_flags & MS_RDONLY)) {
2377 journal_t *journal = EXT4_SB(sb)->s_journal;
2379 /* Now we set up the journal barrier. */
2380 jbd2_journal_lock_updates(journal);
2381 jbd2_journal_flush(journal);
2383 /* Journal blocked and flushed, clear needs_recovery flag. */
2384 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2385 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2390 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2391 * flag here, even though the filesystem is not technically dirty yet.
2393 static void ext4_unlockfs(struct super_block *sb)
2395 if (!(sb->s_flags & MS_RDONLY)) {
2397 /* Reser the needs_recovery flag before the fs is unlocked. */
2398 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2399 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2401 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2405 static int ext4_remount (struct super_block * sb, int * flags, char * data)
2407 struct ext4_super_block * es;
2408 struct ext4_sb_info *sbi = EXT4_SB(sb);
2409 ext4_fsblk_t n_blocks_count = 0;
2410 unsigned long old_sb_flags;
2411 struct ext4_mount_options old_opts;
2417 /* Store the original options */
2418 old_sb_flags = sb->s_flags;
2419 old_opts.s_mount_opt = sbi->s_mount_opt;
2420 old_opts.s_resuid = sbi->s_resuid;
2421 old_opts.s_resgid = sbi->s_resgid;
2422 old_opts.s_commit_interval = sbi->s_commit_interval;
2424 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2425 for (i = 0; i < MAXQUOTAS; i++)
2426 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2430 * Allow the "check" option to be passed as a remount option.
2432 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2437 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2438 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
2440 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2441 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2445 ext4_init_journal_params(sb, sbi->s_journal);
2447 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2448 n_blocks_count > ext4_blocks_count(es)) {
2449 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
2454 if (*flags & MS_RDONLY) {
2456 * First of all, the unconditional stuff we have to do
2457 * to disable replay of the journal when we next remount
2459 sb->s_flags |= MS_RDONLY;
2462 * OK, test if we are remounting a valid rw partition
2463 * readonly, and if so set the rdonly flag and then
2464 * mark the partition as valid again.
2466 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2467 (sbi->s_mount_state & EXT4_VALID_FS))
2468 es->s_state = cpu_to_le16(sbi->s_mount_state);
2471 * We have to unlock super so that we can wait for
2475 ext4_mark_recovery_complete(sb, es);
2479 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2480 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2481 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2482 "remount RDWR because of unsupported "
2483 "optional features (%x).\n",
2484 sb->s_id, le32_to_cpu(ret));
2490 * If we have an unprocessed orphan list hanging
2491 * around from a previously readonly bdev mount,
2492 * require a full umount/remount for now.
2494 if (es->s_last_orphan) {
2495 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
2496 "remount RDWR because of unprocessed "
2497 "orphan inode list. Please "
2498 "umount/remount instead.\n",
2505 * Mounting a RDONLY partition read-write, so reread
2506 * and store the current valid flag. (It may have
2507 * been changed by e2fsck since we originally mounted
2510 ext4_clear_journal_err(sb, es);
2511 sbi->s_mount_state = le16_to_cpu(es->s_state);
2512 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
2514 if (!ext4_setup_super (sb, es, 0))
2515 sb->s_flags &= ~MS_RDONLY;
2519 /* Release old quota file names */
2520 for (i = 0; i < MAXQUOTAS; i++)
2521 if (old_opts.s_qf_names[i] &&
2522 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2523 kfree(old_opts.s_qf_names[i]);
2527 sb->s_flags = old_sb_flags;
2528 sbi->s_mount_opt = old_opts.s_mount_opt;
2529 sbi->s_resuid = old_opts.s_resuid;
2530 sbi->s_resgid = old_opts.s_resgid;
2531 sbi->s_commit_interval = old_opts.s_commit_interval;
2533 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2534 for (i = 0; i < MAXQUOTAS; i++) {
2535 if (sbi->s_qf_names[i] &&
2536 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2537 kfree(sbi->s_qf_names[i]);
2538 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2544 static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
2546 struct super_block *sb = dentry->d_sb;
2547 struct ext4_sb_info *sbi = EXT4_SB(sb);
2548 struct ext4_super_block *es = sbi->s_es;
2551 if (test_opt(sb, MINIX_DF)) {
2552 sbi->s_overhead_last = 0;
2553 } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2554 unsigned long ngroups = sbi->s_groups_count, i;
2555 ext4_fsblk_t overhead = 0;
2559 * Compute the overhead (FS structures). This is constant
2560 * for a given filesystem unless the number of block groups
2561 * changes so we cache the previous value until it does.
2565 * All of the blocks before first_data_block are
2568 overhead = le32_to_cpu(es->s_first_data_block);
2571 * Add the overhead attributed to the superblock and
2572 * block group descriptors. If the sparse superblocks
2573 * feature is turned on, then not all groups have this.
2575 for (i = 0; i < ngroups; i++) {
2576 overhead += ext4_bg_has_super(sb, i) +
2577 ext4_bg_num_gdb(sb, i);
2582 * Every block group has an inode bitmap, a block
2583 * bitmap, and an inode table.
2585 overhead += ngroups * (2 + sbi->s_itb_per_group);
2586 sbi->s_overhead_last = overhead;
2588 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
2591 buf->f_type = EXT4_SUPER_MAGIC;
2592 buf->f_bsize = sb->s_blocksize;
2593 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
2594 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
2595 es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
2596 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2597 if (buf->f_bfree < ext4_r_blocks_count(es))
2599 buf->f_files = le32_to_cpu(es->s_inodes_count);
2600 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
2601 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
2602 buf->f_namelen = EXT4_NAME_LEN;
2603 fsid = le64_to_cpup((void *)es->s_uuid) ^
2604 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2605 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2606 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
2610 /* Helper function for writing quotas on sync - we need to start transaction before quota file
2611 * is locked for write. Otherwise the are possible deadlocks:
2612 * Process 1 Process 2
2613 * ext4_create() quota_sync()
2614 * jbd2_journal_start() write_dquot()
2615 * DQUOT_INIT() down(dqio_mutex)
2616 * down(dqio_mutex) jbd2_journal_start()
2622 static inline struct inode *dquot_to_inode(struct dquot *dquot)
2624 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2627 static int ext4_dquot_initialize(struct inode *inode, int type)
2632 /* We may create quota structure so we need to reserve enough blocks */
2633 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
2635 return PTR_ERR(handle);
2636 ret = dquot_initialize(inode, type);
2637 err = ext4_journal_stop(handle);
2643 static int ext4_dquot_drop(struct inode *inode)
2648 /* We may delete quota structure so we need to reserve enough blocks */
2649 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
2651 return PTR_ERR(handle);
2652 ret = dquot_drop(inode);
2653 err = ext4_journal_stop(handle);
2659 static int ext4_write_dquot(struct dquot *dquot)
2663 struct inode *inode;
2665 inode = dquot_to_inode(dquot);
2666 handle = ext4_journal_start(inode,
2667 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2669 return PTR_ERR(handle);
2670 ret = dquot_commit(dquot);
2671 err = ext4_journal_stop(handle);
2677 static int ext4_acquire_dquot(struct dquot *dquot)
2682 handle = ext4_journal_start(dquot_to_inode(dquot),
2683 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2685 return PTR_ERR(handle);
2686 ret = dquot_acquire(dquot);
2687 err = ext4_journal_stop(handle);
2693 static int ext4_release_dquot(struct dquot *dquot)
2698 handle = ext4_journal_start(dquot_to_inode(dquot),
2699 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2701 return PTR_ERR(handle);
2702 ret = dquot_release(dquot);
2703 err = ext4_journal_stop(handle);
2709 static int ext4_mark_dquot_dirty(struct dquot *dquot)
2711 /* Are we journalling quotas? */
2712 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2713 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2714 dquot_mark_dquot_dirty(dquot);
2715 return ext4_write_dquot(dquot);
2717 return dquot_mark_dquot_dirty(dquot);
2721 static int ext4_write_info(struct super_block *sb, int type)
2726 /* Data block + inode block */
2727 handle = ext4_journal_start(sb->s_root->d_inode, 2);
2729 return PTR_ERR(handle);
2730 ret = dquot_commit_info(sb, type);
2731 err = ext4_journal_stop(handle);
2738 * Turn on quotas during mount time - we need to find
2739 * the quota file and such...
2741 static int ext4_quota_on_mount(struct super_block *sb, int type)
2743 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2744 EXT4_SB(sb)->s_jquota_fmt, type);
2748 * Standard function to be called on quota_on
2750 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
2754 struct nameidata nd;
2756 if (!test_opt(sb, QUOTA))
2758 /* Not journalling quota? */
2759 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2760 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
2761 return vfs_quota_on(sb, type, format_id, path);
2762 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2765 /* Quotafile not on the same filesystem? */
2766 if (nd.mnt->mnt_sb != sb) {
2770 /* Quotafile not of fs root? */
2771 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2773 "EXT4-fs: Quota file not on filesystem root. "
2774 "Journalled quota will not work.\n");
2776 return vfs_quota_on(sb, type, format_id, path);
2779 /* Read data from quotafile - avoid pagecache and such because we cannot afford
2780 * acquiring the locks... As quota files are never truncated and quota code
2781 * itself serializes the operations (and noone else should touch the files)
2782 * we don't have to be afraid of races */
2783 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
2784 size_t len, loff_t off)
2786 struct inode *inode = sb_dqopt(sb)->files[type];
2787 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2789 int offset = off & (sb->s_blocksize - 1);
2792 struct buffer_head *bh;
2793 loff_t i_size = i_size_read(inode);
2797 if (off+len > i_size)
2800 while (toread > 0) {
2801 tocopy = sb->s_blocksize - offset < toread ?
2802 sb->s_blocksize - offset : toread;
2803 bh = ext4_bread(NULL, inode, blk, 0, &err);
2806 if (!bh) /* A hole? */
2807 memset(data, 0, tocopy);
2809 memcpy(data, bh->b_data+offset, tocopy);
2819 /* Write to quotafile (we know the transaction is already started and has
2820 * enough credits) */
2821 static ssize_t ext4_quota_write(struct super_block *sb, int type,
2822 const char *data, size_t len, loff_t off)
2824 struct inode *inode = sb_dqopt(sb)->files[type];
2825 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
2827 int offset = off & (sb->s_blocksize - 1);
2829 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
2830 size_t towrite = len;
2831 struct buffer_head *bh;
2832 handle_t *handle = journal_current_handle();
2834 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2835 while (towrite > 0) {
2836 tocopy = sb->s_blocksize - offset < towrite ?
2837 sb->s_blocksize - offset : towrite;
2838 bh = ext4_bread(handle, inode, blk, 1, &err);
2841 if (journal_quota) {
2842 err = ext4_journal_get_write_access(handle, bh);
2849 memcpy(bh->b_data+offset, data, tocopy);
2850 flush_dcache_page(bh->b_page);
2853 err = ext4_journal_dirty_metadata(handle, bh);
2855 /* Always do at least ordered writes for quotas */
2856 err = ext4_journal_dirty_data(handle, bh);
2857 mark_buffer_dirty(bh);
2870 if (inode->i_size < off+len-towrite) {
2871 i_size_write(inode, off+len-towrite);
2872 EXT4_I(inode)->i_disksize = inode->i_size;
2875 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2876 ext4_mark_inode_dirty(handle, inode);
2877 mutex_unlock(&inode->i_mutex);
2878 return len - towrite;
2883 static int ext4_get_sb(struct file_system_type *fs_type,
2884 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2886 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
2889 static struct file_system_type ext4dev_fs_type = {
2890 .owner = THIS_MODULE,
2892 .get_sb = ext4_get_sb,
2893 .kill_sb = kill_block_super,
2894 .fs_flags = FS_REQUIRES_DEV,
2897 static int __init init_ext4_fs(void)
2899 int err = init_ext4_xattr();
2902 err = init_inodecache();
2905 err = register_filesystem(&ext4dev_fs_type);
2910 destroy_inodecache();
2916 static void __exit exit_ext4_fs(void)
2918 unregister_filesystem(&ext4dev_fs_type);
2919 destroy_inodecache();
2923 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2924 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
2925 MODULE_LICENSE("GPL");
2926 module_init(init_ext4_fs)
2927 module_exit(exit_ext4_fs)