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/vmalloc.h>
24 #include <linux/jbd2.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/blkdev.h>
28 #include <linux/parser.h>
29 #include <linux/smp_lock.h>
30 #include <linux/buffer_head.h>
31 #include <linux/exportfs.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>
38 #include <linux/proc_fs.h>
39 #include <linux/ctype.h>
40 #include <linux/log2.h>
41 #include <linux/crc16.h>
42 #include <asm/uaccess.h>
45 #include "ext4_jbd2.h"
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/ext4.h>
52 static int default_mb_history_length = 1000;
54 module_param_named(default_mb_history_length, default_mb_history_length,
56 MODULE_PARM_DESC(default_mb_history_length,
57 "Default number of entries saved for mb_history");
59 struct proc_dir_entry *ext4_proc_root;
60 static struct kset *ext4_kset;
62 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
63 unsigned long journal_devnum);
64 static int ext4_commit_super(struct super_block *sb, int sync);
65 static void ext4_mark_recovery_complete(struct super_block *sb,
66 struct ext4_super_block *es);
67 static void ext4_clear_journal_err(struct super_block *sb,
68 struct ext4_super_block *es);
69 static int ext4_sync_fs(struct super_block *sb, int wait);
70 static const char *ext4_decode_error(struct super_block *sb, int errno,
72 static int ext4_remount(struct super_block *sb, int *flags, char *data);
73 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
74 static int ext4_unfreeze(struct super_block *sb);
75 static void ext4_write_super(struct super_block *sb);
76 static int ext4_freeze(struct super_block *sb);
79 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
80 struct ext4_group_desc *bg)
82 return le32_to_cpu(bg->bg_block_bitmap_lo) |
83 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
84 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
87 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
88 struct ext4_group_desc *bg)
90 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
91 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
92 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
95 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
96 struct ext4_group_desc *bg)
98 return le32_to_cpu(bg->bg_inode_table_lo) |
99 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
100 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
103 __u32 ext4_free_blks_count(struct super_block *sb,
104 struct ext4_group_desc *bg)
106 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
107 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
108 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
111 __u32 ext4_free_inodes_count(struct super_block *sb,
112 struct ext4_group_desc *bg)
114 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
115 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
116 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
119 __u32 ext4_used_dirs_count(struct super_block *sb,
120 struct ext4_group_desc *bg)
122 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
123 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
124 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
127 __u32 ext4_itable_unused_count(struct super_block *sb,
128 struct ext4_group_desc *bg)
130 return le16_to_cpu(bg->bg_itable_unused_lo) |
131 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
132 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
135 void ext4_block_bitmap_set(struct super_block *sb,
136 struct ext4_group_desc *bg, ext4_fsblk_t blk)
138 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
139 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
140 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
143 void ext4_inode_bitmap_set(struct super_block *sb,
144 struct ext4_group_desc *bg, ext4_fsblk_t blk)
146 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
147 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
148 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
151 void ext4_inode_table_set(struct super_block *sb,
152 struct ext4_group_desc *bg, ext4_fsblk_t blk)
154 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
155 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
156 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
159 void ext4_free_blks_set(struct super_block *sb,
160 struct ext4_group_desc *bg, __u32 count)
162 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
163 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
164 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
167 void ext4_free_inodes_set(struct super_block *sb,
168 struct ext4_group_desc *bg, __u32 count)
170 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
171 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
172 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
175 void ext4_used_dirs_set(struct super_block *sb,
176 struct ext4_group_desc *bg, __u32 count)
178 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
179 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
180 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
183 void ext4_itable_unused_set(struct super_block *sb,
184 struct ext4_group_desc *bg, __u32 count)
186 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
187 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
188 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
192 * Wrappers for jbd2_journal_start/end.
194 * The only special thing we need to do here is to make sure that all
195 * journal_end calls result in the superblock being marked dirty, so
196 * that sync() will call the filesystem's write_super callback if
199 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
203 if (sb->s_flags & MS_RDONLY)
204 return ERR_PTR(-EROFS);
206 /* Special case here: if the journal has aborted behind our
207 * backs (eg. EIO in the commit thread), then we still need to
208 * take the FS itself readonly cleanly. */
209 journal = EXT4_SB(sb)->s_journal;
211 if (is_journal_aborted(journal)) {
212 ext4_abort(sb, __func__, "Detected aborted journal");
213 return ERR_PTR(-EROFS);
215 return jbd2_journal_start(journal, nblocks);
218 * We're not journaling, return the appropriate indication.
220 current->journal_info = EXT4_NOJOURNAL_HANDLE;
221 return current->journal_info;
225 * The only special thing we need to do here is to make sure that all
226 * jbd2_journal_stop calls result in the superblock being marked dirty, so
227 * that sync() will call the filesystem's write_super callback if
230 int __ext4_journal_stop(const char *where, handle_t *handle)
232 struct super_block *sb;
236 if (!ext4_handle_valid(handle)) {
238 * Do this here since we don't call jbd2_journal_stop() in
241 current->journal_info = NULL;
244 sb = handle->h_transaction->t_journal->j_private;
246 rc = jbd2_journal_stop(handle);
251 __ext4_std_error(sb, where, err);
255 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
256 struct buffer_head *bh, handle_t *handle, int err)
259 const char *errstr = ext4_decode_error(NULL, err, nbuf);
261 BUG_ON(!ext4_handle_valid(handle));
264 BUFFER_TRACE(bh, "abort");
269 if (is_handle_aborted(handle))
272 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
273 caller, errstr, err_fn);
275 jbd2_journal_abort_handle(handle);
278 /* Deal with the reporting of failure conditions on a filesystem such as
279 * inconsistencies detected or read IO failures.
281 * On ext2, we can store the error state of the filesystem in the
282 * superblock. That is not possible on ext4, because we may have other
283 * write ordering constraints on the superblock which prevent us from
284 * writing it out straight away; and given that the journal is about to
285 * be aborted, we can't rely on the current, or future, transactions to
286 * write out the superblock safely.
288 * We'll just use the jbd2_journal_abort() error code to record an error in
289 * the journal instead. On recovery, the journal will compain about
290 * that error until we've noted it down and cleared it.
293 static void ext4_handle_error(struct super_block *sb)
295 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
297 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
298 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
300 if (sb->s_flags & MS_RDONLY)
303 if (!test_opt(sb, ERRORS_CONT)) {
304 journal_t *journal = EXT4_SB(sb)->s_journal;
306 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
308 jbd2_journal_abort(journal, -EIO);
310 if (test_opt(sb, ERRORS_RO)) {
311 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
312 sb->s_flags |= MS_RDONLY;
314 ext4_commit_super(sb, 1);
315 if (test_opt(sb, ERRORS_PANIC))
316 panic("EXT4-fs (device %s): panic forced after error\n",
320 void ext4_error(struct super_block *sb, const char *function,
321 const char *fmt, ...)
326 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
331 ext4_handle_error(sb);
334 static const char *ext4_decode_error(struct super_block *sb, int errno,
341 errstr = "IO failure";
344 errstr = "Out of memory";
347 if (!sb || (EXT4_SB(sb)->s_journal &&
348 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
349 errstr = "Journal has aborted";
351 errstr = "Readonly filesystem";
354 /* If the caller passed in an extra buffer for unknown
355 * errors, textualise them now. Else we just return
358 /* Check for truncated error codes... */
359 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
368 /* __ext4_std_error decodes expected errors from journaling functions
369 * automatically and invokes the appropriate error response. */
371 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
376 /* Special case: if the error is EROFS, and we're not already
377 * inside a transaction, then there's really no point in logging
379 if (errno == -EROFS && journal_current_handle() == NULL &&
380 (sb->s_flags & MS_RDONLY))
383 errstr = ext4_decode_error(sb, errno, nbuf);
384 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
385 sb->s_id, function, errstr);
387 ext4_handle_error(sb);
391 * ext4_abort is a much stronger failure handler than ext4_error. The
392 * abort function may be used to deal with unrecoverable failures such
393 * as journal IO errors or ENOMEM at a critical moment in log management.
395 * We unconditionally force the filesystem into an ABORT|READONLY state,
396 * unless the error response on the fs has been set to panic in which
397 * case we take the easy way out and panic immediately.
400 void ext4_abort(struct super_block *sb, const char *function,
401 const char *fmt, ...)
406 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
411 if (test_opt(sb, ERRORS_PANIC))
412 panic("EXT4-fs panic from previous error\n");
414 if (sb->s_flags & MS_RDONLY)
417 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
418 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
419 sb->s_flags |= MS_RDONLY;
420 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
421 if (EXT4_SB(sb)->s_journal)
422 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
425 void ext4_msg (struct super_block * sb, const char *prefix,
426 const char *fmt, ...)
431 printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
437 void ext4_warning(struct super_block *sb, const char *function,
438 const char *fmt, ...)
443 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
450 void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
451 const char *function, const char *fmt, ...)
456 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
459 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
464 if (test_opt(sb, ERRORS_CONT)) {
465 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
466 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
467 ext4_commit_super(sb, 0);
470 ext4_unlock_group(sb, grp);
471 ext4_handle_error(sb);
473 * We only get here in the ERRORS_RO case; relocking the group
474 * may be dangerous, but nothing bad will happen since the
475 * filesystem will have already been marked read/only and the
476 * journal has been aborted. We return 1 as a hint to callers
477 * who might what to use the return value from
478 * ext4_grp_locked_error() to distinguish beween the
479 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
480 * aggressively from the ext4 function in question, with a
481 * more appropriate error code.
483 ext4_lock_group(sb, grp);
487 void ext4_update_dynamic_rev(struct super_block *sb)
489 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
491 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
494 ext4_warning(sb, __func__,
495 "updating to rev %d because of new feature flag, "
496 "running e2fsck is recommended",
499 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
500 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
501 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
502 /* leave es->s_feature_*compat flags alone */
503 /* es->s_uuid will be set by e2fsck if empty */
506 * The rest of the superblock fields should be zero, and if not it
507 * means they are likely already in use, so leave them alone. We
508 * can leave it up to e2fsck to clean up any inconsistencies there.
513 * Open the external journal device
515 static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
517 struct block_device *bdev;
518 char b[BDEVNAME_SIZE];
520 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
526 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
527 __bdevname(dev, b), PTR_ERR(bdev));
532 * Release the journal device
534 static int ext4_blkdev_put(struct block_device *bdev)
537 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
540 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
542 struct block_device *bdev;
545 bdev = sbi->journal_bdev;
547 ret = ext4_blkdev_put(bdev);
548 sbi->journal_bdev = NULL;
553 static inline struct inode *orphan_list_entry(struct list_head *l)
555 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
558 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
562 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
563 le32_to_cpu(sbi->s_es->s_last_orphan));
565 printk(KERN_ERR "sb_info orphan list:\n");
566 list_for_each(l, &sbi->s_orphan) {
567 struct inode *inode = orphan_list_entry(l);
569 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
570 inode->i_sb->s_id, inode->i_ino, inode,
571 inode->i_mode, inode->i_nlink,
576 static void ext4_put_super(struct super_block *sb)
578 struct ext4_sb_info *sbi = EXT4_SB(sb);
579 struct ext4_super_block *es = sbi->s_es;
585 ext4_commit_super(sb, 1);
587 ext4_release_system_zone(sb);
589 ext4_ext_release(sb);
590 ext4_xattr_put_super(sb);
591 if (sbi->s_journal) {
592 err = jbd2_journal_destroy(sbi->s_journal);
593 sbi->s_journal = NULL;
595 ext4_abort(sb, __func__,
596 "Couldn't clean up the journal");
598 if (!(sb->s_flags & MS_RDONLY)) {
599 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
600 es->s_state = cpu_to_le16(sbi->s_mount_state);
601 ext4_commit_super(sb, 1);
604 remove_proc_entry(sb->s_id, ext4_proc_root);
606 kobject_del(&sbi->s_kobj);
608 for (i = 0; i < sbi->s_gdb_count; i++)
609 brelse(sbi->s_group_desc[i]);
610 kfree(sbi->s_group_desc);
611 if (is_vmalloc_addr(sbi->s_flex_groups))
612 vfree(sbi->s_flex_groups);
614 kfree(sbi->s_flex_groups);
615 percpu_counter_destroy(&sbi->s_freeblocks_counter);
616 percpu_counter_destroy(&sbi->s_freeinodes_counter);
617 percpu_counter_destroy(&sbi->s_dirs_counter);
618 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
621 for (i = 0; i < MAXQUOTAS; i++)
622 kfree(sbi->s_qf_names[i]);
625 /* Debugging code just in case the in-memory inode orphan list
626 * isn't empty. The on-disk one can be non-empty if we've
627 * detected an error and taken the fs readonly, but the
628 * in-memory list had better be clean by this point. */
629 if (!list_empty(&sbi->s_orphan))
630 dump_orphan_list(sb, sbi);
631 J_ASSERT(list_empty(&sbi->s_orphan));
633 invalidate_bdev(sb->s_bdev);
634 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
636 * Invalidate the journal device's buffers. We don't want them
637 * floating about in memory - the physical journal device may
638 * hotswapped, and it breaks the `ro-after' testing code.
640 sync_blockdev(sbi->journal_bdev);
641 invalidate_bdev(sbi->journal_bdev);
642 ext4_blkdev_remove(sbi);
644 sb->s_fs_info = NULL;
646 * Now that we are completely done shutting down the
647 * superblock, we need to actually destroy the kobject.
651 kobject_put(&sbi->s_kobj);
652 wait_for_completion(&sbi->s_kobj_unregister);
653 kfree(sbi->s_blockgroup_lock);
657 static struct kmem_cache *ext4_inode_cachep;
660 * Called inside transaction, so use GFP_NOFS
662 static struct inode *ext4_alloc_inode(struct super_block *sb)
664 struct ext4_inode_info *ei;
666 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
670 ei->vfs_inode.i_version = 1;
671 ei->vfs_inode.i_data.writeback_index = 0;
672 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
673 INIT_LIST_HEAD(&ei->i_prealloc_list);
674 spin_lock_init(&ei->i_prealloc_lock);
676 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
677 * therefore it can be null here. Don't check it, just initialize
680 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
681 ei->i_reserved_data_blocks = 0;
682 ei->i_reserved_meta_blocks = 0;
683 ei->i_allocated_meta_blocks = 0;
684 ei->i_delalloc_reserved_flag = 0;
685 spin_lock_init(&(ei->i_block_reservation_lock));
687 return &ei->vfs_inode;
690 static void ext4_destroy_inode(struct inode *inode)
692 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
693 ext4_msg(inode->i_sb, KERN_ERR,
694 "Inode %lu (%p): orphan list check failed!",
695 inode->i_ino, EXT4_I(inode));
696 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
697 EXT4_I(inode), sizeof(struct ext4_inode_info),
701 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
704 static void init_once(void *foo)
706 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
708 INIT_LIST_HEAD(&ei->i_orphan);
709 #ifdef CONFIG_EXT4_FS_XATTR
710 init_rwsem(&ei->xattr_sem);
712 init_rwsem(&ei->i_data_sem);
713 inode_init_once(&ei->vfs_inode);
716 static int init_inodecache(void)
718 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
719 sizeof(struct ext4_inode_info),
720 0, (SLAB_RECLAIM_ACCOUNT|
723 if (ext4_inode_cachep == NULL)
728 static void destroy_inodecache(void)
730 kmem_cache_destroy(ext4_inode_cachep);
733 static void ext4_clear_inode(struct inode *inode)
735 ext4_discard_preallocations(inode);
736 if (EXT4_JOURNAL(inode))
737 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
738 &EXT4_I(inode)->jinode);
741 static inline void ext4_show_quota_options(struct seq_file *seq,
742 struct super_block *sb)
744 #if defined(CONFIG_QUOTA)
745 struct ext4_sb_info *sbi = EXT4_SB(sb);
747 if (sbi->s_jquota_fmt)
748 seq_printf(seq, ",jqfmt=%s",
749 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
751 if (sbi->s_qf_names[USRQUOTA])
752 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
754 if (sbi->s_qf_names[GRPQUOTA])
755 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
757 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
758 seq_puts(seq, ",usrquota");
760 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
761 seq_puts(seq, ",grpquota");
767 * - it's set to a non-default value OR
768 * - if the per-sb default is different from the global default
770 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
773 unsigned long def_mount_opts;
774 struct super_block *sb = vfs->mnt_sb;
775 struct ext4_sb_info *sbi = EXT4_SB(sb);
776 struct ext4_super_block *es = sbi->s_es;
778 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
779 def_errors = le16_to_cpu(es->s_errors);
781 if (sbi->s_sb_block != 1)
782 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
783 if (test_opt(sb, MINIX_DF))
784 seq_puts(seq, ",minixdf");
785 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
786 seq_puts(seq, ",grpid");
787 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
788 seq_puts(seq, ",nogrpid");
789 if (sbi->s_resuid != EXT4_DEF_RESUID ||
790 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
791 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
793 if (sbi->s_resgid != EXT4_DEF_RESGID ||
794 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
795 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
797 if (test_opt(sb, ERRORS_RO)) {
798 if (def_errors == EXT4_ERRORS_PANIC ||
799 def_errors == EXT4_ERRORS_CONTINUE) {
800 seq_puts(seq, ",errors=remount-ro");
803 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
804 seq_puts(seq, ",errors=continue");
805 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
806 seq_puts(seq, ",errors=panic");
807 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
808 seq_puts(seq, ",nouid32");
809 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
810 seq_puts(seq, ",debug");
811 if (test_opt(sb, OLDALLOC))
812 seq_puts(seq, ",oldalloc");
813 #ifdef CONFIG_EXT4_FS_XATTR
814 if (test_opt(sb, XATTR_USER) &&
815 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
816 seq_puts(seq, ",user_xattr");
817 if (!test_opt(sb, XATTR_USER) &&
818 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
819 seq_puts(seq, ",nouser_xattr");
822 #ifdef CONFIG_EXT4_FS_POSIX_ACL
823 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
824 seq_puts(seq, ",acl");
825 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
826 seq_puts(seq, ",noacl");
828 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
829 seq_printf(seq, ",commit=%u",
830 (unsigned) (sbi->s_commit_interval / HZ));
832 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
833 seq_printf(seq, ",min_batch_time=%u",
834 (unsigned) sbi->s_min_batch_time);
836 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
837 seq_printf(seq, ",max_batch_time=%u",
838 (unsigned) sbi->s_min_batch_time);
842 * We're changing the default of barrier mount option, so
843 * let's always display its mount state so it's clear what its
846 seq_puts(seq, ",barrier=");
847 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
848 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
849 seq_puts(seq, ",journal_async_commit");
850 if (test_opt(sb, NOBH))
851 seq_puts(seq, ",nobh");
852 if (test_opt(sb, I_VERSION))
853 seq_puts(seq, ",i_version");
854 if (!test_opt(sb, DELALLOC))
855 seq_puts(seq, ",nodelalloc");
859 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
861 * journal mode get enabled in different ways
862 * So just print the value even if we didn't specify it
864 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
865 seq_puts(seq, ",data=journal");
866 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
867 seq_puts(seq, ",data=ordered");
868 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
869 seq_puts(seq, ",data=writeback");
871 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
872 seq_printf(seq, ",inode_readahead_blks=%u",
873 sbi->s_inode_readahead_blks);
875 if (test_opt(sb, DATA_ERR_ABORT))
876 seq_puts(seq, ",data_err=abort");
878 if (test_opt(sb, NO_AUTO_DA_ALLOC))
879 seq_puts(seq, ",noauto_da_alloc");
881 ext4_show_quota_options(seq, sb);
886 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
887 u64 ino, u32 generation)
891 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
892 return ERR_PTR(-ESTALE);
893 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
894 return ERR_PTR(-ESTALE);
896 /* iget isn't really right if the inode is currently unallocated!!
898 * ext4_read_inode will return a bad_inode if the inode had been
899 * deleted, so we should be safe.
901 * Currently we don't know the generation for parent directory, so
902 * a generation of 0 means "accept any"
904 inode = ext4_iget(sb, ino);
906 return ERR_CAST(inode);
907 if (generation && inode->i_generation != generation) {
909 return ERR_PTR(-ESTALE);
915 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
916 int fh_len, int fh_type)
918 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
922 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
923 int fh_len, int fh_type)
925 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
930 * Try to release metadata pages (indirect blocks, directories) which are
931 * mapped via the block device. Since these pages could have journal heads
932 * which would prevent try_to_free_buffers() from freeing them, we must use
933 * jbd2 layer's try_to_free_buffers() function to release them.
935 static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
938 journal_t *journal = EXT4_SB(sb)->s_journal;
940 WARN_ON(PageChecked(page));
941 if (!page_has_buffers(page))
944 return jbd2_journal_try_to_free_buffers(journal, page,
946 return try_to_free_buffers(page);
950 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
951 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
953 static int ext4_write_dquot(struct dquot *dquot);
954 static int ext4_acquire_dquot(struct dquot *dquot);
955 static int ext4_release_dquot(struct dquot *dquot);
956 static int ext4_mark_dquot_dirty(struct dquot *dquot);
957 static int ext4_write_info(struct super_block *sb, int type);
958 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
959 char *path, int remount);
960 static int ext4_quota_on_mount(struct super_block *sb, int type);
961 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
962 size_t len, loff_t off);
963 static ssize_t ext4_quota_write(struct super_block *sb, int type,
964 const char *data, size_t len, loff_t off);
966 static struct dquot_operations ext4_quota_operations = {
967 .initialize = dquot_initialize,
969 .alloc_space = dquot_alloc_space,
970 .reserve_space = dquot_reserve_space,
971 .claim_space = dquot_claim_space,
972 .release_rsv = dquot_release_reserved_space,
973 .get_reserved_space = ext4_get_reserved_space,
974 .alloc_inode = dquot_alloc_inode,
975 .free_space = dquot_free_space,
976 .free_inode = dquot_free_inode,
977 .transfer = dquot_transfer,
978 .write_dquot = ext4_write_dquot,
979 .acquire_dquot = ext4_acquire_dquot,
980 .release_dquot = ext4_release_dquot,
981 .mark_dirty = ext4_mark_dquot_dirty,
982 .write_info = ext4_write_info,
983 .alloc_dquot = dquot_alloc,
984 .destroy_dquot = dquot_destroy,
987 static struct quotactl_ops ext4_qctl_operations = {
988 .quota_on = ext4_quota_on,
989 .quota_off = vfs_quota_off,
990 .quota_sync = vfs_quota_sync,
991 .get_info = vfs_get_dqinfo,
992 .set_info = vfs_set_dqinfo,
993 .get_dqblk = vfs_get_dqblk,
994 .set_dqblk = vfs_set_dqblk
998 static const struct super_operations ext4_sops = {
999 .alloc_inode = ext4_alloc_inode,
1000 .destroy_inode = ext4_destroy_inode,
1001 .write_inode = ext4_write_inode,
1002 .dirty_inode = ext4_dirty_inode,
1003 .delete_inode = ext4_delete_inode,
1004 .put_super = ext4_put_super,
1005 .sync_fs = ext4_sync_fs,
1006 .freeze_fs = ext4_freeze,
1007 .unfreeze_fs = ext4_unfreeze,
1008 .statfs = ext4_statfs,
1009 .remount_fs = ext4_remount,
1010 .clear_inode = ext4_clear_inode,
1011 .show_options = ext4_show_options,
1013 .quota_read = ext4_quota_read,
1014 .quota_write = ext4_quota_write,
1016 .bdev_try_to_free_page = bdev_try_to_free_page,
1019 static const struct super_operations ext4_nojournal_sops = {
1020 .alloc_inode = ext4_alloc_inode,
1021 .destroy_inode = ext4_destroy_inode,
1022 .write_inode = ext4_write_inode,
1023 .dirty_inode = ext4_dirty_inode,
1024 .delete_inode = ext4_delete_inode,
1025 .write_super = ext4_write_super,
1026 .put_super = ext4_put_super,
1027 .statfs = ext4_statfs,
1028 .remount_fs = ext4_remount,
1029 .clear_inode = ext4_clear_inode,
1030 .show_options = ext4_show_options,
1032 .quota_read = ext4_quota_read,
1033 .quota_write = ext4_quota_write,
1035 .bdev_try_to_free_page = bdev_try_to_free_page,
1038 static const struct export_operations ext4_export_ops = {
1039 .fh_to_dentry = ext4_fh_to_dentry,
1040 .fh_to_parent = ext4_fh_to_parent,
1041 .get_parent = ext4_get_parent,
1045 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1046 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
1047 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1048 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1049 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
1050 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1051 Opt_journal_update, Opt_journal_dev,
1052 Opt_journal_checksum, Opt_journal_async_commit,
1053 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1054 Opt_data_err_abort, Opt_data_err_ignore, Opt_mb_history_length,
1055 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1056 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
1057 Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
1058 Opt_usrquota, Opt_grpquota, Opt_i_version,
1059 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
1060 Opt_block_validity, Opt_noblock_validity,
1061 Opt_inode_readahead_blks, Opt_journal_ioprio
1064 static const match_table_t tokens = {
1065 {Opt_bsd_df, "bsddf"},
1066 {Opt_minix_df, "minixdf"},
1067 {Opt_grpid, "grpid"},
1068 {Opt_grpid, "bsdgroups"},
1069 {Opt_nogrpid, "nogrpid"},
1070 {Opt_nogrpid, "sysvgroups"},
1071 {Opt_resgid, "resgid=%u"},
1072 {Opt_resuid, "resuid=%u"},
1074 {Opt_err_cont, "errors=continue"},
1075 {Opt_err_panic, "errors=panic"},
1076 {Opt_err_ro, "errors=remount-ro"},
1077 {Opt_nouid32, "nouid32"},
1078 {Opt_debug, "debug"},
1079 {Opt_oldalloc, "oldalloc"},
1080 {Opt_orlov, "orlov"},
1081 {Opt_user_xattr, "user_xattr"},
1082 {Opt_nouser_xattr, "nouser_xattr"},
1084 {Opt_noacl, "noacl"},
1085 {Opt_noload, "noload"},
1088 {Opt_commit, "commit=%u"},
1089 {Opt_min_batch_time, "min_batch_time=%u"},
1090 {Opt_max_batch_time, "max_batch_time=%u"},
1091 {Opt_journal_update, "journal=update"},
1092 {Opt_journal_dev, "journal_dev=%u"},
1093 {Opt_journal_checksum, "journal_checksum"},
1094 {Opt_journal_async_commit, "journal_async_commit"},
1095 {Opt_abort, "abort"},
1096 {Opt_data_journal, "data=journal"},
1097 {Opt_data_ordered, "data=ordered"},
1098 {Opt_data_writeback, "data=writeback"},
1099 {Opt_data_err_abort, "data_err=abort"},
1100 {Opt_data_err_ignore, "data_err=ignore"},
1101 {Opt_mb_history_length, "mb_history_length=%u"},
1102 {Opt_offusrjquota, "usrjquota="},
1103 {Opt_usrjquota, "usrjquota=%s"},
1104 {Opt_offgrpjquota, "grpjquota="},
1105 {Opt_grpjquota, "grpjquota=%s"},
1106 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1107 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1108 {Opt_grpquota, "grpquota"},
1109 {Opt_noquota, "noquota"},
1110 {Opt_quota, "quota"},
1111 {Opt_usrquota, "usrquota"},
1112 {Opt_barrier, "barrier=%u"},
1113 {Opt_barrier, "barrier"},
1114 {Opt_nobarrier, "nobarrier"},
1115 {Opt_i_version, "i_version"},
1116 {Opt_stripe, "stripe=%u"},
1117 {Opt_resize, "resize"},
1118 {Opt_delalloc, "delalloc"},
1119 {Opt_nodelalloc, "nodelalloc"},
1120 {Opt_block_validity, "block_validity"},
1121 {Opt_noblock_validity, "noblock_validity"},
1122 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1123 {Opt_journal_ioprio, "journal_ioprio=%u"},
1124 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
1125 {Opt_auto_da_alloc, "auto_da_alloc"},
1126 {Opt_noauto_da_alloc, "noauto_da_alloc"},
1130 static ext4_fsblk_t get_sb_block(void **data)
1132 ext4_fsblk_t sb_block;
1133 char *options = (char *) *data;
1135 if (!options || strncmp(options, "sb=", 3) != 0)
1136 return 1; /* Default location */
1139 /* TODO: use simple_strtoll with >32bit ext4 */
1140 sb_block = simple_strtoul(options, &options, 0);
1141 if (*options && *options != ',') {
1142 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1146 if (*options == ',')
1148 *data = (void *) options;
1153 #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1155 static int parse_options(char *options, struct super_block *sb,
1156 unsigned long *journal_devnum,
1157 unsigned int *journal_ioprio,
1158 ext4_fsblk_t *n_blocks_count, int is_remount)
1160 struct ext4_sb_info *sbi = EXT4_SB(sb);
1162 substring_t args[MAX_OPT_ARGS];
1173 while ((p = strsep(&options, ",")) != NULL) {
1178 token = match_token(p, tokens, args);
1181 clear_opt(sbi->s_mount_opt, MINIX_DF);
1184 set_opt(sbi->s_mount_opt, MINIX_DF);
1187 set_opt(sbi->s_mount_opt, GRPID);
1190 clear_opt(sbi->s_mount_opt, GRPID);
1193 if (match_int(&args[0], &option))
1195 sbi->s_resuid = option;
1198 if (match_int(&args[0], &option))
1200 sbi->s_resgid = option;
1203 /* handled by get_sb_block() instead of here */
1204 /* *sb_block = match_int(&args[0]); */
1207 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1208 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1209 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1212 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1213 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1214 set_opt(sbi->s_mount_opt, ERRORS_RO);
1217 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1218 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1219 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1222 set_opt(sbi->s_mount_opt, NO_UID32);
1225 set_opt(sbi->s_mount_opt, DEBUG);
1228 set_opt(sbi->s_mount_opt, OLDALLOC);
1231 clear_opt(sbi->s_mount_opt, OLDALLOC);
1233 #ifdef CONFIG_EXT4_FS_XATTR
1234 case Opt_user_xattr:
1235 set_opt(sbi->s_mount_opt, XATTR_USER);
1237 case Opt_nouser_xattr:
1238 clear_opt(sbi->s_mount_opt, XATTR_USER);
1241 case Opt_user_xattr:
1242 case Opt_nouser_xattr:
1243 ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1246 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1248 set_opt(sbi->s_mount_opt, POSIX_ACL);
1251 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1256 ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1259 case Opt_journal_update:
1261 /* Eventually we will want to be able to create
1262 a journal file here. For now, only allow the
1263 user to specify an existing inode to be the
1266 ext4_msg(sb, KERN_ERR,
1267 "Cannot specify journal on remount");
1270 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1272 case Opt_journal_dev:
1274 ext4_msg(sb, KERN_ERR,
1275 "Cannot specify journal on remount");
1278 if (match_int(&args[0], &option))
1280 *journal_devnum = option;
1282 case Opt_journal_checksum:
1283 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1285 case Opt_journal_async_commit:
1286 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1287 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1290 set_opt(sbi->s_mount_opt, NOLOAD);
1293 if (match_int(&args[0], &option))
1298 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1299 sbi->s_commit_interval = HZ * option;
1301 case Opt_max_batch_time:
1302 if (match_int(&args[0], &option))
1307 option = EXT4_DEF_MAX_BATCH_TIME;
1308 sbi->s_max_batch_time = option;
1310 case Opt_min_batch_time:
1311 if (match_int(&args[0], &option))
1315 sbi->s_min_batch_time = option;
1317 case Opt_data_journal:
1318 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1320 case Opt_data_ordered:
1321 data_opt = EXT4_MOUNT_ORDERED_DATA;
1323 case Opt_data_writeback:
1324 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1327 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1329 ext4_msg(sb, KERN_ERR,
1330 "Cannot change data mode on remount");
1334 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1335 sbi->s_mount_opt |= data_opt;
1338 case Opt_data_err_abort:
1339 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1341 case Opt_data_err_ignore:
1342 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1344 case Opt_mb_history_length:
1345 if (match_int(&args[0], &option))
1349 sbi->s_mb_history_max = option;
1358 if (sb_any_quota_loaded(sb) &&
1359 !sbi->s_qf_names[qtype]) {
1360 ext4_msg(sb, KERN_ERR,
1361 "Cannot change journaled "
1362 "quota options when quota turned on");
1365 qname = match_strdup(&args[0]);
1367 ext4_msg(sb, KERN_ERR,
1368 "Not enough memory for "
1369 "storing quotafile name");
1372 if (sbi->s_qf_names[qtype] &&
1373 strcmp(sbi->s_qf_names[qtype], qname)) {
1374 ext4_msg(sb, KERN_ERR,
1375 "%s quota file already "
1376 "specified", QTYPE2NAME(qtype));
1380 sbi->s_qf_names[qtype] = qname;
1381 if (strchr(sbi->s_qf_names[qtype], '/')) {
1382 ext4_msg(sb, KERN_ERR,
1383 "quotafile must be on "
1385 kfree(sbi->s_qf_names[qtype]);
1386 sbi->s_qf_names[qtype] = NULL;
1389 set_opt(sbi->s_mount_opt, QUOTA);
1391 case Opt_offusrjquota:
1394 case Opt_offgrpjquota:
1397 if (sb_any_quota_loaded(sb) &&
1398 sbi->s_qf_names[qtype]) {
1399 ext4_msg(sb, KERN_ERR, "Cannot change "
1400 "journaled quota options when "
1405 * The space will be released later when all options
1406 * are confirmed to be correct
1408 sbi->s_qf_names[qtype] = NULL;
1410 case Opt_jqfmt_vfsold:
1411 qfmt = QFMT_VFS_OLD;
1413 case Opt_jqfmt_vfsv0:
1416 if (sb_any_quota_loaded(sb) &&
1417 sbi->s_jquota_fmt != qfmt) {
1418 ext4_msg(sb, KERN_ERR, "Cannot change "
1419 "journaled quota options when "
1423 sbi->s_jquota_fmt = qfmt;
1427 set_opt(sbi->s_mount_opt, QUOTA);
1428 set_opt(sbi->s_mount_opt, USRQUOTA);
1431 set_opt(sbi->s_mount_opt, QUOTA);
1432 set_opt(sbi->s_mount_opt, GRPQUOTA);
1435 if (sb_any_quota_loaded(sb)) {
1436 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1437 "options when quota turned on");
1440 clear_opt(sbi->s_mount_opt, QUOTA);
1441 clear_opt(sbi->s_mount_opt, USRQUOTA);
1442 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1448 ext4_msg(sb, KERN_ERR,
1449 "quota options not supported");
1453 case Opt_offusrjquota:
1454 case Opt_offgrpjquota:
1455 case Opt_jqfmt_vfsold:
1456 case Opt_jqfmt_vfsv0:
1457 ext4_msg(sb, KERN_ERR,
1458 "journaled quota options not supported");
1464 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1467 clear_opt(sbi->s_mount_opt, BARRIER);
1470 if (match_int(&args[0], &option)) {
1471 set_opt(sbi->s_mount_opt, BARRIER);
1475 set_opt(sbi->s_mount_opt, BARRIER);
1477 clear_opt(sbi->s_mount_opt, BARRIER);
1483 ext4_msg(sb, KERN_ERR,
1484 "resize option only available "
1488 if (match_int(&args[0], &option) != 0)
1490 *n_blocks_count = option;
1493 set_opt(sbi->s_mount_opt, NOBH);
1496 clear_opt(sbi->s_mount_opt, NOBH);
1499 set_opt(sbi->s_mount_opt, I_VERSION);
1500 sb->s_flags |= MS_I_VERSION;
1502 case Opt_nodelalloc:
1503 clear_opt(sbi->s_mount_opt, DELALLOC);
1506 if (match_int(&args[0], &option))
1510 sbi->s_stripe = option;
1513 set_opt(sbi->s_mount_opt, DELALLOC);
1515 case Opt_block_validity:
1516 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1518 case Opt_noblock_validity:
1519 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1521 case Opt_inode_readahead_blks:
1522 if (match_int(&args[0], &option))
1524 if (option < 0 || option > (1 << 30))
1526 if (!is_power_of_2(option)) {
1527 ext4_msg(sb, KERN_ERR,
1528 "EXT4-fs: inode_readahead_blks"
1529 " must be a power of 2");
1532 sbi->s_inode_readahead_blks = option;
1534 case Opt_journal_ioprio:
1535 if (match_int(&args[0], &option))
1537 if (option < 0 || option > 7)
1539 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1542 case Opt_noauto_da_alloc:
1543 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1545 case Opt_auto_da_alloc:
1546 if (match_int(&args[0], &option)) {
1547 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1551 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1553 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1556 ext4_msg(sb, KERN_ERR,
1557 "Unrecognized mount option \"%s\" "
1558 "or missing value", p);
1563 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1564 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1565 sbi->s_qf_names[USRQUOTA])
1566 clear_opt(sbi->s_mount_opt, USRQUOTA);
1568 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1569 sbi->s_qf_names[GRPQUOTA])
1570 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1572 if ((sbi->s_qf_names[USRQUOTA] &&
1573 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1574 (sbi->s_qf_names[GRPQUOTA] &&
1575 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1576 ext4_msg(sb, KERN_ERR, "old and new quota "
1581 if (!sbi->s_jquota_fmt) {
1582 ext4_msg(sb, KERN_ERR, "journaled quota format "
1587 if (sbi->s_jquota_fmt) {
1588 ext4_msg(sb, KERN_ERR, "journaled quota format "
1589 "specified with no journaling "
1598 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1601 struct ext4_sb_info *sbi = EXT4_SB(sb);
1604 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1605 ext4_msg(sb, KERN_ERR, "revision level too high, "
1606 "forcing read-only mode");
1611 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1612 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1613 "running e2fsck is recommended");
1614 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1615 ext4_msg(sb, KERN_WARNING,
1616 "warning: mounting fs with errors, "
1617 "running e2fsck is recommended");
1618 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1619 le16_to_cpu(es->s_mnt_count) >=
1620 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1621 ext4_msg(sb, KERN_WARNING,
1622 "warning: maximal mount count reached, "
1623 "running e2fsck is recommended");
1624 else if (le32_to_cpu(es->s_checkinterval) &&
1625 (le32_to_cpu(es->s_lastcheck) +
1626 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1627 ext4_msg(sb, KERN_WARNING,
1628 "warning: checktime reached, "
1629 "running e2fsck is recommended");
1630 if (!sbi->s_journal)
1631 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1632 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1633 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1634 le16_add_cpu(&es->s_mnt_count, 1);
1635 es->s_mtime = cpu_to_le32(get_seconds());
1636 ext4_update_dynamic_rev(sb);
1638 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1640 ext4_commit_super(sb, 1);
1641 if (test_opt(sb, DEBUG))
1642 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1643 "bpg=%lu, ipg=%lu, mo=%04x]\n",
1645 sbi->s_groups_count,
1646 EXT4_BLOCKS_PER_GROUP(sb),
1647 EXT4_INODES_PER_GROUP(sb),
1650 if (EXT4_SB(sb)->s_journal) {
1651 ext4_msg(sb, KERN_INFO, "%s journal on %s",
1652 EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1653 "external", EXT4_SB(sb)->s_journal->j_devname);
1655 ext4_msg(sb, KERN_INFO, "no journal");
1660 static int ext4_fill_flex_info(struct super_block *sb)
1662 struct ext4_sb_info *sbi = EXT4_SB(sb);
1663 struct ext4_group_desc *gdp = NULL;
1664 ext4_group_t flex_group_count;
1665 ext4_group_t flex_group;
1666 int groups_per_flex = 0;
1670 if (!sbi->s_es->s_log_groups_per_flex) {
1671 sbi->s_log_groups_per_flex = 0;
1675 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1676 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1678 /* We allocate both existing and potentially added groups */
1679 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1680 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1681 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1682 size = flex_group_count * sizeof(struct flex_groups);
1683 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1684 if (sbi->s_flex_groups == NULL) {
1685 sbi->s_flex_groups = vmalloc(size);
1686 if (sbi->s_flex_groups)
1687 memset(sbi->s_flex_groups, 0, size);
1689 if (sbi->s_flex_groups == NULL) {
1690 ext4_msg(sb, KERN_ERR, "not enough memory for "
1691 "%u flex groups", flex_group_count);
1695 for (i = 0; i < sbi->s_groups_count; i++) {
1696 gdp = ext4_get_group_desc(sb, i, NULL);
1698 flex_group = ext4_flex_group(sbi, i);
1699 atomic_set(&sbi->s_flex_groups[flex_group].free_inodes,
1700 ext4_free_inodes_count(sb, gdp));
1701 atomic_set(&sbi->s_flex_groups[flex_group].free_blocks,
1702 ext4_free_blks_count(sb, gdp));
1703 atomic_set(&sbi->s_flex_groups[flex_group].used_dirs,
1704 ext4_used_dirs_count(sb, gdp));
1712 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1713 struct ext4_group_desc *gdp)
1717 if (sbi->s_es->s_feature_ro_compat &
1718 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1719 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1720 __le32 le_group = cpu_to_le32(block_group);
1722 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1723 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1724 crc = crc16(crc, (__u8 *)gdp, offset);
1725 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1726 /* for checksum of struct ext4_group_desc do the rest...*/
1727 if ((sbi->s_es->s_feature_incompat &
1728 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1729 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1730 crc = crc16(crc, (__u8 *)gdp + offset,
1731 le16_to_cpu(sbi->s_es->s_desc_size) -
1735 return cpu_to_le16(crc);
1738 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1739 struct ext4_group_desc *gdp)
1741 if ((sbi->s_es->s_feature_ro_compat &
1742 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1743 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1749 /* Called at mount-time, super-block is locked */
1750 static int ext4_check_descriptors(struct super_block *sb)
1752 struct ext4_sb_info *sbi = EXT4_SB(sb);
1753 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1754 ext4_fsblk_t last_block;
1755 ext4_fsblk_t block_bitmap;
1756 ext4_fsblk_t inode_bitmap;
1757 ext4_fsblk_t inode_table;
1758 int flexbg_flag = 0;
1761 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1764 ext4_debug("Checking group descriptors");
1766 for (i = 0; i < sbi->s_groups_count; i++) {
1767 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1769 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1770 last_block = ext4_blocks_count(sbi->s_es) - 1;
1772 last_block = first_block +
1773 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1775 block_bitmap = ext4_block_bitmap(sb, gdp);
1776 if (block_bitmap < first_block || block_bitmap > last_block) {
1777 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1778 "Block bitmap for group %u not in group "
1779 "(block %llu)!", i, block_bitmap);
1782 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1783 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1784 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1785 "Inode bitmap for group %u not in group "
1786 "(block %llu)!", i, inode_bitmap);
1789 inode_table = ext4_inode_table(sb, gdp);
1790 if (inode_table < first_block ||
1791 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1792 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1793 "Inode table for group %u not in group "
1794 "(block %llu)!", i, inode_table);
1797 ext4_lock_group(sb, i);
1798 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1799 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1800 "Checksum for group %u failed (%u!=%u)",
1801 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1802 gdp)), le16_to_cpu(gdp->bg_checksum));
1803 if (!(sb->s_flags & MS_RDONLY)) {
1804 ext4_unlock_group(sb, i);
1808 ext4_unlock_group(sb, i);
1810 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1813 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1814 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
1818 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1819 * the superblock) which were deleted from all directories, but held open by
1820 * a process at the time of a crash. We walk the list and try to delete these
1821 * inodes at recovery time (only with a read-write filesystem).
1823 * In order to keep the orphan inode chain consistent during traversal (in
1824 * case of crash during recovery), we link each inode into the superblock
1825 * orphan list_head and handle it the same way as an inode deletion during
1826 * normal operation (which journals the operations for us).
1828 * We only do an iget() and an iput() on each inode, which is very safe if we
1829 * accidentally point at an in-use or already deleted inode. The worst that
1830 * can happen in this case is that we get a "bit already cleared" message from
1831 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1832 * e2fsck was run on this filesystem, and it must have already done the orphan
1833 * inode cleanup for us, so we can safely abort without any further action.
1835 static void ext4_orphan_cleanup(struct super_block *sb,
1836 struct ext4_super_block *es)
1838 unsigned int s_flags = sb->s_flags;
1839 int nr_orphans = 0, nr_truncates = 0;
1843 if (!es->s_last_orphan) {
1844 jbd_debug(4, "no orphan inodes to clean up\n");
1848 if (bdev_read_only(sb->s_bdev)) {
1849 ext4_msg(sb, KERN_ERR, "write access "
1850 "unavailable, skipping orphan cleanup");
1854 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1855 if (es->s_last_orphan)
1856 jbd_debug(1, "Errors on filesystem, "
1857 "clearing orphan list.\n");
1858 es->s_last_orphan = 0;
1859 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1863 if (s_flags & MS_RDONLY) {
1864 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1865 sb->s_flags &= ~MS_RDONLY;
1868 /* Needed for iput() to work correctly and not trash data */
1869 sb->s_flags |= MS_ACTIVE;
1870 /* Turn on quotas so that they are updated correctly */
1871 for (i = 0; i < MAXQUOTAS; i++) {
1872 if (EXT4_SB(sb)->s_qf_names[i]) {
1873 int ret = ext4_quota_on_mount(sb, i);
1875 ext4_msg(sb, KERN_ERR,
1876 "Cannot turn on journaled "
1877 "quota: error %d", ret);
1882 while (es->s_last_orphan) {
1883 struct inode *inode;
1885 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1886 if (IS_ERR(inode)) {
1887 es->s_last_orphan = 0;
1891 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1893 if (inode->i_nlink) {
1894 ext4_msg(sb, KERN_DEBUG,
1895 "%s: truncating inode %lu to %lld bytes",
1896 __func__, inode->i_ino, inode->i_size);
1897 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1898 inode->i_ino, inode->i_size);
1899 ext4_truncate(inode);
1902 ext4_msg(sb, KERN_DEBUG,
1903 "%s: deleting unreferenced inode %lu",
1904 __func__, inode->i_ino);
1905 jbd_debug(2, "deleting unreferenced inode %lu\n",
1909 iput(inode); /* The delete magic happens here! */
1912 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1915 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1916 PLURAL(nr_orphans));
1918 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1919 PLURAL(nr_truncates));
1921 /* Turn quotas off */
1922 for (i = 0; i < MAXQUOTAS; i++) {
1923 if (sb_dqopt(sb)->files[i])
1924 vfs_quota_off(sb, i, 0);
1927 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1931 * Maximal extent format file size.
1932 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1933 * extent format containers, within a sector_t, and within i_blocks
1934 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1935 * so that won't be a limiting factor.
1937 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1939 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1942 loff_t upper_limit = MAX_LFS_FILESIZE;
1944 /* small i_blocks in vfs inode? */
1945 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1947 * CONFIG_LBDAF is not enabled implies the inode
1948 * i_block represent total blocks in 512 bytes
1949 * 32 == size of vfs inode i_blocks * 8
1951 upper_limit = (1LL << 32) - 1;
1953 /* total blocks in file system block size */
1954 upper_limit >>= (blkbits - 9);
1955 upper_limit <<= blkbits;
1958 /* 32-bit extent-start container, ee_block */
1963 /* Sanity check against vm- & vfs- imposed limits */
1964 if (res > upper_limit)
1971 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
1972 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1973 * We need to be 1 filesystem block less than the 2^48 sector limit.
1975 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1977 loff_t res = EXT4_NDIR_BLOCKS;
1980 /* This is calculated to be the largest file size for a dense, block
1981 * mapped file such that the file's total number of 512-byte sectors,
1982 * including data and all indirect blocks, does not exceed (2^48 - 1).
1984 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
1985 * number of 512-byte sectors of the file.
1988 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1990 * !has_huge_files or CONFIG_LBDAF not enabled implies that
1991 * the inode i_block field represents total file blocks in
1992 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
1994 upper_limit = (1LL << 32) - 1;
1996 /* total blocks in file system block size */
1997 upper_limit >>= (bits - 9);
2001 * We use 48 bit ext4_inode i_blocks
2002 * With EXT4_HUGE_FILE_FL set the i_blocks
2003 * represent total number of blocks in
2004 * file system block size
2006 upper_limit = (1LL << 48) - 1;
2010 /* indirect blocks */
2012 /* double indirect blocks */
2013 meta_blocks += 1 + (1LL << (bits-2));
2014 /* tripple indirect blocks */
2015 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2017 upper_limit -= meta_blocks;
2018 upper_limit <<= bits;
2020 res += 1LL << (bits-2);
2021 res += 1LL << (2*(bits-2));
2022 res += 1LL << (3*(bits-2));
2024 if (res > upper_limit)
2027 if (res > MAX_LFS_FILESIZE)
2028 res = MAX_LFS_FILESIZE;
2033 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
2034 ext4_fsblk_t logical_sb_block, int nr)
2036 struct ext4_sb_info *sbi = EXT4_SB(sb);
2037 ext4_group_t bg, first_meta_bg;
2040 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2042 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2044 return logical_sb_block + nr + 1;
2045 bg = sbi->s_desc_per_block * nr;
2046 if (ext4_bg_has_super(sb, bg))
2049 return (has_super + ext4_group_first_block_no(sb, bg));
2053 * ext4_get_stripe_size: Get the stripe size.
2054 * @sbi: In memory super block info
2056 * If we have specified it via mount option, then
2057 * use the mount option value. If the value specified at mount time is
2058 * greater than the blocks per group use the super block value.
2059 * If the super block value is greater than blocks per group return 0.
2060 * Allocator needs it be less than blocks per group.
2063 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2065 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2066 unsigned long stripe_width =
2067 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2069 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2070 return sbi->s_stripe;
2072 if (stripe_width <= sbi->s_blocks_per_group)
2073 return stripe_width;
2075 if (stride <= sbi->s_blocks_per_group)
2084 struct attribute attr;
2085 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2086 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2087 const char *, size_t);
2091 static int parse_strtoul(const char *buf,
2092 unsigned long max, unsigned long *value)
2096 while (*buf && isspace(*buf))
2098 *value = simple_strtoul(buf, &endp, 0);
2099 while (*endp && isspace(*endp))
2101 if (*endp || *value > max)
2107 static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2108 struct ext4_sb_info *sbi,
2111 return snprintf(buf, PAGE_SIZE, "%llu\n",
2112 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2115 static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2116 struct ext4_sb_info *sbi, char *buf)
2118 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2120 return snprintf(buf, PAGE_SIZE, "%lu\n",
2121 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2122 sbi->s_sectors_written_start) >> 1);
2125 static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2126 struct ext4_sb_info *sbi, char *buf)
2128 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2130 return snprintf(buf, PAGE_SIZE, "%llu\n",
2131 sbi->s_kbytes_written +
2132 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2133 EXT4_SB(sb)->s_sectors_written_start) >> 1));
2136 static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2137 struct ext4_sb_info *sbi,
2138 const char *buf, size_t count)
2142 if (parse_strtoul(buf, 0x40000000, &t))
2145 if (!is_power_of_2(t))
2148 sbi->s_inode_readahead_blks = t;
2152 static ssize_t sbi_ui_show(struct ext4_attr *a,
2153 struct ext4_sb_info *sbi, char *buf)
2155 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2157 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2160 static ssize_t sbi_ui_store(struct ext4_attr *a,
2161 struct ext4_sb_info *sbi,
2162 const char *buf, size_t count)
2164 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2167 if (parse_strtoul(buf, 0xffffffff, &t))
2173 #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2174 static struct ext4_attr ext4_attr_##_name = { \
2175 .attr = {.name = __stringify(_name), .mode = _mode }, \
2178 .offset = offsetof(struct ext4_sb_info, _elname), \
2180 #define EXT4_ATTR(name, mode, show, store) \
2181 static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2183 #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2184 #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2185 #define EXT4_RW_ATTR_SBI_UI(name, elname) \
2186 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2187 #define ATTR_LIST(name) &ext4_attr_##name.attr
2189 EXT4_RO_ATTR(delayed_allocation_blocks);
2190 EXT4_RO_ATTR(session_write_kbytes);
2191 EXT4_RO_ATTR(lifetime_write_kbytes);
2192 EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2193 inode_readahead_blks_store, s_inode_readahead_blks);
2194 EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
2195 EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2196 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2197 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2198 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2199 EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2200 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2202 static struct attribute *ext4_attrs[] = {
2203 ATTR_LIST(delayed_allocation_blocks),
2204 ATTR_LIST(session_write_kbytes),
2205 ATTR_LIST(lifetime_write_kbytes),
2206 ATTR_LIST(inode_readahead_blks),
2207 ATTR_LIST(inode_goal),
2208 ATTR_LIST(mb_stats),
2209 ATTR_LIST(mb_max_to_scan),
2210 ATTR_LIST(mb_min_to_scan),
2211 ATTR_LIST(mb_order2_req),
2212 ATTR_LIST(mb_stream_req),
2213 ATTR_LIST(mb_group_prealloc),
2217 static ssize_t ext4_attr_show(struct kobject *kobj,
2218 struct attribute *attr, char *buf)
2220 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2222 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2224 return a->show ? a->show(a, sbi, buf) : 0;
2227 static ssize_t ext4_attr_store(struct kobject *kobj,
2228 struct attribute *attr,
2229 const char *buf, size_t len)
2231 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2233 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2235 return a->store ? a->store(a, sbi, buf, len) : 0;
2238 static void ext4_sb_release(struct kobject *kobj)
2240 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2242 complete(&sbi->s_kobj_unregister);
2246 static struct sysfs_ops ext4_attr_ops = {
2247 .show = ext4_attr_show,
2248 .store = ext4_attr_store,
2251 static struct kobj_type ext4_ktype = {
2252 .default_attrs = ext4_attrs,
2253 .sysfs_ops = &ext4_attr_ops,
2254 .release = ext4_sb_release,
2258 * Check whether this filesystem can be mounted based on
2259 * the features present and the RDONLY/RDWR mount requested.
2260 * Returns 1 if this filesystem can be mounted as requested,
2261 * 0 if it cannot be.
2263 static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2265 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2266 ext4_msg(sb, KERN_ERR,
2267 "Couldn't mount because of "
2268 "unsupported optional features (%x)",
2269 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2270 ~EXT4_FEATURE_INCOMPAT_SUPP));
2277 /* Check that feature set is OK for a read-write mount */
2278 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2279 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2280 "unsupported optional features (%x)",
2281 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2282 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2286 * Large file size enabled file system can only be mounted
2287 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2289 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2290 if (sizeof(blkcnt_t) < sizeof(u64)) {
2291 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2292 "cannot be mounted RDWR without "
2300 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
2301 __releases(kernel_lock)
2302 __acquires(kernel_lock)
2304 struct buffer_head *bh;
2305 struct ext4_super_block *es = NULL;
2306 struct ext4_sb_info *sbi;
2308 ext4_fsblk_t sb_block = get_sb_block(&data);
2309 ext4_fsblk_t logical_sb_block;
2310 unsigned long offset = 0;
2311 unsigned long journal_devnum = 0;
2312 unsigned long def_mount_opts;
2318 unsigned int db_count;
2320 int needs_recovery, has_huge_files;
2323 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2325 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2329 sbi->s_blockgroup_lock =
2330 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2331 if (!sbi->s_blockgroup_lock) {
2335 sb->s_fs_info = sbi;
2336 sbi->s_mount_opt = 0;
2337 sbi->s_resuid = EXT4_DEF_RESUID;
2338 sbi->s_resgid = EXT4_DEF_RESGID;
2339 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2340 sbi->s_sb_block = sb_block;
2341 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2346 /* Cleanup superblock name */
2347 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2350 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2352 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
2357 * The ext4 superblock will not be buffer aligned for other than 1kB
2358 * block sizes. We need to calculate the offset from buffer start.
2360 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
2361 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2362 offset = do_div(logical_sb_block, blocksize);
2364 logical_sb_block = sb_block;
2367 if (!(bh = sb_bread(sb, logical_sb_block))) {
2368 ext4_msg(sb, KERN_ERR, "unable to read superblock");
2372 * Note: s_es must be initialized as soon as possible because
2373 * some ext4 macro-instructions depend on its value
2375 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2377 sb->s_magic = le16_to_cpu(es->s_magic);
2378 if (sb->s_magic != EXT4_SUPER_MAGIC)
2380 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2382 /* Set defaults before we parse the mount options */
2383 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2384 if (def_mount_opts & EXT4_DEFM_DEBUG)
2385 set_opt(sbi->s_mount_opt, DEBUG);
2386 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2387 set_opt(sbi->s_mount_opt, GRPID);
2388 if (def_mount_opts & EXT4_DEFM_UID16)
2389 set_opt(sbi->s_mount_opt, NO_UID32);
2390 #ifdef CONFIG_EXT4_FS_XATTR
2391 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2392 set_opt(sbi->s_mount_opt, XATTR_USER);
2394 #ifdef CONFIG_EXT4_FS_POSIX_ACL
2395 if (def_mount_opts & EXT4_DEFM_ACL)
2396 set_opt(sbi->s_mount_opt, POSIX_ACL);
2398 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2399 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2400 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2401 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2402 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2403 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2405 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2406 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2407 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2408 set_opt(sbi->s_mount_opt, ERRORS_CONT);
2410 set_opt(sbi->s_mount_opt, ERRORS_RO);
2412 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2413 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2414 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2415 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2416 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2417 sbi->s_mb_history_max = default_mb_history_length;
2419 set_opt(sbi->s_mount_opt, BARRIER);
2422 * enable delayed allocation by default
2423 * Use -o nodelalloc to turn it off
2425 set_opt(sbi->s_mount_opt, DELALLOC);
2427 if (!parse_options((char *) data, sb, &journal_devnum,
2428 &journal_ioprio, NULL, 0))
2431 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2432 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2434 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2435 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2436 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2437 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2438 ext4_msg(sb, KERN_WARNING,
2439 "feature flags set on rev 0 fs, "
2440 "running e2fsck is recommended");
2443 * Check feature flags regardless of the revision level, since we
2444 * previously didn't change the revision level when setting the flags,
2445 * so there is a chance incompat flags are set on a rev 0 filesystem.
2447 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
2450 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2452 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2453 blocksize > EXT4_MAX_BLOCK_SIZE) {
2454 ext4_msg(sb, KERN_ERR,
2455 "Unsupported filesystem blocksize %d", blocksize);
2459 if (sb->s_blocksize != blocksize) {
2460 /* Validate the filesystem blocksize */
2461 if (!sb_set_blocksize(sb, blocksize)) {
2462 ext4_msg(sb, KERN_ERR, "bad block size %d",
2468 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2469 offset = do_div(logical_sb_block, blocksize);
2470 bh = sb_bread(sb, logical_sb_block);
2472 ext4_msg(sb, KERN_ERR,
2473 "Can't read superblock on 2nd try");
2476 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2478 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2479 ext4_msg(sb, KERN_ERR,
2480 "Magic mismatch, very weird!");
2485 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2486 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2487 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2489 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2491 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2492 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2493 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2495 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2496 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2497 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2498 (!is_power_of_2(sbi->s_inode_size)) ||
2499 (sbi->s_inode_size > blocksize)) {
2500 ext4_msg(sb, KERN_ERR,
2501 "unsupported inode size: %d",
2505 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2506 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2509 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2510 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2511 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2512 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2513 !is_power_of_2(sbi->s_desc_size)) {
2514 ext4_msg(sb, KERN_ERR,
2515 "unsupported descriptor size %lu",
2520 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2522 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2523 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2524 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2527 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2528 if (sbi->s_inodes_per_block == 0)
2530 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2531 sbi->s_inodes_per_block;
2532 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2534 sbi->s_mount_state = le16_to_cpu(es->s_state);
2535 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2536 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2538 for (i = 0; i < 4; i++)
2539 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2540 sbi->s_def_hash_version = es->s_def_hash_version;
2541 i = le32_to_cpu(es->s_flags);
2542 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2543 sbi->s_hash_unsigned = 3;
2544 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2545 #ifdef __CHAR_UNSIGNED__
2546 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2547 sbi->s_hash_unsigned = 3;
2549 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2554 if (sbi->s_blocks_per_group > blocksize * 8) {
2555 ext4_msg(sb, KERN_ERR,
2556 "#blocks per group too big: %lu",
2557 sbi->s_blocks_per_group);
2560 if (sbi->s_inodes_per_group > blocksize * 8) {
2561 ext4_msg(sb, KERN_ERR,
2562 "#inodes per group too big: %lu",
2563 sbi->s_inodes_per_group);
2568 * Test whether we have more sectors than will fit in sector_t,
2569 * and whether the max offset is addressable by the page cache.
2571 if ((ext4_blocks_count(es) >
2572 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2573 (ext4_blocks_count(es) >
2574 (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
2575 ext4_msg(sb, KERN_ERR, "filesystem"
2576 " too large to mount safely on this system");
2577 if (sizeof(sector_t) < 8)
2578 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
2583 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2586 /* check blocks count against device size */
2587 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2588 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2589 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2590 "exceeds size of device (%llu blocks)",
2591 ext4_blocks_count(es), blocks_count);
2596 * It makes no sense for the first data block to be beyond the end
2597 * of the filesystem.
2599 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2600 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2601 "block %u is beyond end of filesystem (%llu)",
2602 le32_to_cpu(es->s_first_data_block),
2603 ext4_blocks_count(es));
2606 blocks_count = (ext4_blocks_count(es) -
2607 le32_to_cpu(es->s_first_data_block) +
2608 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2609 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2610 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2611 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
2612 "(block count %llu, first data block %u, "
2613 "blocks per group %lu)", sbi->s_groups_count,
2614 ext4_blocks_count(es),
2615 le32_to_cpu(es->s_first_data_block),
2616 EXT4_BLOCKS_PER_GROUP(sb));
2619 sbi->s_groups_count = blocks_count;
2620 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2621 EXT4_DESC_PER_BLOCK(sb);
2622 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2624 if (sbi->s_group_desc == NULL) {
2625 ext4_msg(sb, KERN_ERR, "not enough memory");
2629 #ifdef CONFIG_PROC_FS
2631 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2634 bgl_lock_init(sbi->s_blockgroup_lock);
2636 for (i = 0; i < db_count; i++) {
2637 block = descriptor_loc(sb, logical_sb_block, i);
2638 sbi->s_group_desc[i] = sb_bread(sb, block);
2639 if (!sbi->s_group_desc[i]) {
2640 ext4_msg(sb, KERN_ERR,
2641 "can't read group descriptor %d", i);
2646 if (!ext4_check_descriptors(sb)) {
2647 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
2650 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2651 if (!ext4_fill_flex_info(sb)) {
2652 ext4_msg(sb, KERN_ERR,
2653 "unable to initialize "
2654 "flex_bg meta info!");
2658 sbi->s_gdb_count = db_count;
2659 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2660 spin_lock_init(&sbi->s_next_gen_lock);
2662 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2663 ext4_count_free_blocks(sb));
2665 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2666 ext4_count_free_inodes(sb));
2669 err = percpu_counter_init(&sbi->s_dirs_counter,
2670 ext4_count_dirs(sb));
2673 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2676 ext4_msg(sb, KERN_ERR, "insufficient memory");
2680 sbi->s_stripe = ext4_get_stripe_size(sbi);
2683 * set up enough so that it can read an inode
2685 if (!test_opt(sb, NOLOAD) &&
2686 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2687 sb->s_op = &ext4_sops;
2689 sb->s_op = &ext4_nojournal_sops;
2690 sb->s_export_op = &ext4_export_ops;
2691 sb->s_xattr = ext4_xattr_handlers;
2693 sb->s_qcop = &ext4_qctl_operations;
2694 sb->dq_op = &ext4_quota_operations;
2696 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2697 mutex_init(&sbi->s_orphan_lock);
2698 mutex_init(&sbi->s_resize_lock);
2702 needs_recovery = (es->s_last_orphan != 0 ||
2703 EXT4_HAS_INCOMPAT_FEATURE(sb,
2704 EXT4_FEATURE_INCOMPAT_RECOVER));
2707 * The first inode we look at is the journal inode. Don't try
2708 * root first: it may be modified in the journal!
2710 if (!test_opt(sb, NOLOAD) &&
2711 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2712 if (ext4_load_journal(sb, es, journal_devnum))
2714 if (!(sb->s_flags & MS_RDONLY) &&
2715 EXT4_SB(sb)->s_journal->j_failed_commit) {
2716 ext4_msg(sb, KERN_CRIT, "error: "
2717 "ext4_fill_super: Journal transaction "
2719 EXT4_SB(sb)->s_journal->j_failed_commit);
2720 if (test_opt(sb, ERRORS_RO)) {
2721 ext4_msg(sb, KERN_CRIT,
2722 "Mounting filesystem read-only");
2723 sb->s_flags |= MS_RDONLY;
2724 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2725 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2727 if (test_opt(sb, ERRORS_PANIC)) {
2728 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2729 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2730 ext4_commit_super(sb, 1);
2734 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2735 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2736 ext4_msg(sb, KERN_ERR, "required journal recovery "
2737 "suppressed and not mounted read-only");
2740 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2741 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2742 sbi->s_journal = NULL;
2747 if (ext4_blocks_count(es) > 0xffffffffULL &&
2748 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2749 JBD2_FEATURE_INCOMPAT_64BIT)) {
2750 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
2754 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2755 jbd2_journal_set_features(sbi->s_journal,
2756 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2757 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2758 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2759 jbd2_journal_set_features(sbi->s_journal,
2760 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2761 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2762 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2764 jbd2_journal_clear_features(sbi->s_journal,
2765 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2766 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2769 /* We have now updated the journal if required, so we can
2770 * validate the data journaling mode. */
2771 switch (test_opt(sb, DATA_FLAGS)) {
2773 /* No mode set, assume a default based on the journal
2774 * capabilities: ORDERED_DATA if the journal can
2775 * cope, else JOURNAL_DATA
2777 if (jbd2_journal_check_available_features
2778 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2779 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2781 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2784 case EXT4_MOUNT_ORDERED_DATA:
2785 case EXT4_MOUNT_WRITEBACK_DATA:
2786 if (!jbd2_journal_check_available_features
2787 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2788 ext4_msg(sb, KERN_ERR, "Journal does not support "
2789 "requested data journaling mode");
2795 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2799 if (test_opt(sb, NOBH)) {
2800 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2801 ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2802 "its supported only with writeback mode");
2803 clear_opt(sbi->s_mount_opt, NOBH);
2807 * The jbd2_journal_load will have done any necessary log recovery,
2808 * so we can safely mount the rest of the filesystem now.
2811 root = ext4_iget(sb, EXT4_ROOT_INO);
2813 ext4_msg(sb, KERN_ERR, "get root inode failed");
2814 ret = PTR_ERR(root);
2817 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2819 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
2822 sb->s_root = d_alloc_root(root);
2824 ext4_msg(sb, KERN_ERR, "get root dentry failed");
2830 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2832 /* determine the minimum size of new large inodes, if present */
2833 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2834 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2835 EXT4_GOOD_OLD_INODE_SIZE;
2836 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2837 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2838 if (sbi->s_want_extra_isize <
2839 le16_to_cpu(es->s_want_extra_isize))
2840 sbi->s_want_extra_isize =
2841 le16_to_cpu(es->s_want_extra_isize);
2842 if (sbi->s_want_extra_isize <
2843 le16_to_cpu(es->s_min_extra_isize))
2844 sbi->s_want_extra_isize =
2845 le16_to_cpu(es->s_min_extra_isize);
2848 /* Check if enough inode space is available */
2849 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2850 sbi->s_inode_size) {
2851 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2852 EXT4_GOOD_OLD_INODE_SIZE;
2853 ext4_msg(sb, KERN_INFO, "required extra inode space not"
2857 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2858 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2859 "requested data journaling mode");
2860 clear_opt(sbi->s_mount_opt, DELALLOC);
2861 } else if (test_opt(sb, DELALLOC))
2862 ext4_msg(sb, KERN_INFO, "delayed allocation enabled");
2864 err = ext4_setup_system_zone(sb);
2866 ext4_msg(sb, KERN_ERR, "failed to initialize system "
2867 "zone (%d)\n", err);
2872 err = ext4_mb_init(sb, needs_recovery);
2874 ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2879 sbi->s_kobj.kset = ext4_kset;
2880 init_completion(&sbi->s_kobj_unregister);
2881 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
2884 ext4_mb_release(sb);
2885 ext4_ext_release(sb);
2889 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2890 ext4_orphan_cleanup(sb, es);
2891 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2892 if (needs_recovery) {
2893 ext4_msg(sb, KERN_INFO, "recovery complete");
2894 ext4_mark_recovery_complete(sb, es);
2896 if (EXT4_SB(sb)->s_journal) {
2897 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2898 descr = " journalled data mode";
2899 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2900 descr = " ordered data mode";
2902 descr = " writeback data mode";
2904 descr = "out journal";
2906 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
2913 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
2917 ext4_msg(sb, KERN_ERR, "mount failed");
2918 ext4_release_system_zone(sb);
2919 if (sbi->s_journal) {
2920 jbd2_journal_destroy(sbi->s_journal);
2921 sbi->s_journal = NULL;
2924 if (sbi->s_flex_groups) {
2925 if (is_vmalloc_addr(sbi->s_flex_groups))
2926 vfree(sbi->s_flex_groups);
2928 kfree(sbi->s_flex_groups);
2930 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2931 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2932 percpu_counter_destroy(&sbi->s_dirs_counter);
2933 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2935 for (i = 0; i < db_count; i++)
2936 brelse(sbi->s_group_desc[i]);
2937 kfree(sbi->s_group_desc);
2940 remove_proc_entry(sb->s_id, ext4_proc_root);
2943 for (i = 0; i < MAXQUOTAS; i++)
2944 kfree(sbi->s_qf_names[i]);
2946 ext4_blkdev_remove(sbi);
2949 sb->s_fs_info = NULL;
2950 kfree(sbi->s_blockgroup_lock);
2957 * Setup any per-fs journal parameters now. We'll do this both on
2958 * initial mount, once the journal has been initialised but before we've
2959 * done any recovery; and again on any subsequent remount.
2961 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2963 struct ext4_sb_info *sbi = EXT4_SB(sb);
2965 journal->j_commit_interval = sbi->s_commit_interval;
2966 journal->j_min_batch_time = sbi->s_min_batch_time;
2967 journal->j_max_batch_time = sbi->s_max_batch_time;
2969 spin_lock(&journal->j_state_lock);
2970 if (test_opt(sb, BARRIER))
2971 journal->j_flags |= JBD2_BARRIER;
2973 journal->j_flags &= ~JBD2_BARRIER;
2974 if (test_opt(sb, DATA_ERR_ABORT))
2975 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2977 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2978 spin_unlock(&journal->j_state_lock);
2981 static journal_t *ext4_get_journal(struct super_block *sb,
2982 unsigned int journal_inum)
2984 struct inode *journal_inode;
2987 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2989 /* First, test for the existence of a valid inode on disk. Bad
2990 * things happen if we iget() an unused inode, as the subsequent
2991 * iput() will try to delete it. */
2993 journal_inode = ext4_iget(sb, journal_inum);
2994 if (IS_ERR(journal_inode)) {
2995 ext4_msg(sb, KERN_ERR, "no journal found");
2998 if (!journal_inode->i_nlink) {
2999 make_bad_inode(journal_inode);
3000 iput(journal_inode);
3001 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3005 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3006 journal_inode, journal_inode->i_size);
3007 if (!S_ISREG(journal_inode->i_mode)) {
3008 ext4_msg(sb, KERN_ERR, "invalid journal inode");
3009 iput(journal_inode);
3013 journal = jbd2_journal_init_inode(journal_inode);
3015 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3016 iput(journal_inode);
3019 journal->j_private = sb;
3020 ext4_init_journal_params(sb, journal);
3024 static journal_t *ext4_get_dev_journal(struct super_block *sb,
3027 struct buffer_head *bh;
3031 int hblock, blocksize;
3032 ext4_fsblk_t sb_block;
3033 unsigned long offset;
3034 struct ext4_super_block *es;
3035 struct block_device *bdev;
3037 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3039 bdev = ext4_blkdev_get(j_dev, sb);
3043 if (bd_claim(bdev, sb)) {
3044 ext4_msg(sb, KERN_ERR,
3045 "failed to claim external journal device");
3046 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3050 blocksize = sb->s_blocksize;
3051 hblock = bdev_logical_block_size(bdev);
3052 if (blocksize < hblock) {
3053 ext4_msg(sb, KERN_ERR,
3054 "blocksize too small for journal device");
3058 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3059 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3060 set_blocksize(bdev, blocksize);
3061 if (!(bh = __bread(bdev, sb_block, blocksize))) {
3062 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3063 "external journal");
3067 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3068 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3069 !(le32_to_cpu(es->s_feature_incompat) &
3070 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3071 ext4_msg(sb, KERN_ERR, "external journal has "
3077 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3078 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3083 len = ext4_blocks_count(es);
3084 start = sb_block + 1;
3085 brelse(bh); /* we're done with the superblock */
3087 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3088 start, len, blocksize);
3090 ext4_msg(sb, KERN_ERR, "failed to create device journal");
3093 journal->j_private = sb;
3094 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3095 wait_on_buffer(journal->j_sb_buffer);
3096 if (!buffer_uptodate(journal->j_sb_buffer)) {
3097 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3100 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3101 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3102 "user (unsupported) - %d",
3103 be32_to_cpu(journal->j_superblock->s_nr_users));
3106 EXT4_SB(sb)->journal_bdev = bdev;
3107 ext4_init_journal_params(sb, journal);
3111 jbd2_journal_destroy(journal);
3113 ext4_blkdev_put(bdev);
3117 static int ext4_load_journal(struct super_block *sb,
3118 struct ext4_super_block *es,
3119 unsigned long journal_devnum)
3122 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3125 int really_read_only;
3127 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3129 if (journal_devnum &&
3130 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3131 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3132 "numbers have changed");
3133 journal_dev = new_decode_dev(journal_devnum);
3135 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3137 really_read_only = bdev_read_only(sb->s_bdev);
3140 * Are we loading a blank journal or performing recovery after a
3141 * crash? For recovery, we need to check in advance whether we
3142 * can get read-write access to the device.
3144 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3145 if (sb->s_flags & MS_RDONLY) {
3146 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3147 "required on readonly filesystem");
3148 if (really_read_only) {
3149 ext4_msg(sb, KERN_ERR, "write access "
3150 "unavailable, cannot proceed");
3153 ext4_msg(sb, KERN_INFO, "write access will "
3154 "be enabled during recovery");
3158 if (journal_inum && journal_dev) {
3159 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3160 "and inode journals!");
3165 if (!(journal = ext4_get_journal(sb, journal_inum)))
3168 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3172 if (journal->j_flags & JBD2_BARRIER)
3173 ext4_msg(sb, KERN_INFO, "barriers enabled");
3175 ext4_msg(sb, KERN_INFO, "barriers disabled");
3177 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3178 err = jbd2_journal_update_format(journal);
3180 ext4_msg(sb, KERN_ERR, "error updating journal");
3181 jbd2_journal_destroy(journal);
3186 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3187 err = jbd2_journal_wipe(journal, !really_read_only);
3189 err = jbd2_journal_load(journal);
3192 ext4_msg(sb, KERN_ERR, "error loading journal");
3193 jbd2_journal_destroy(journal);
3197 EXT4_SB(sb)->s_journal = journal;
3198 ext4_clear_journal_err(sb, es);
3200 if (journal_devnum &&
3201 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3202 es->s_journal_dev = cpu_to_le32(journal_devnum);
3204 /* Make sure we flush the recovery flag to disk. */
3205 ext4_commit_super(sb, 1);
3211 static int ext4_commit_super(struct super_block *sb, int sync)
3213 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3214 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3219 if (buffer_write_io_error(sbh)) {
3221 * Oh, dear. A previous attempt to write the
3222 * superblock failed. This could happen because the
3223 * USB device was yanked out. Or it could happen to
3224 * be a transient write error and maybe the block will
3225 * be remapped. Nothing we can do but to retry the
3226 * write and hope for the best.
3228 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3229 "superblock detected");
3230 clear_buffer_write_io_error(sbh);
3231 set_buffer_uptodate(sbh);
3233 es->s_wtime = cpu_to_le32(get_seconds());
3234 es->s_kbytes_written =
3235 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3236 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3237 EXT4_SB(sb)->s_sectors_written_start) >> 1));
3238 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3239 &EXT4_SB(sb)->s_freeblocks_counter));
3240 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3241 &EXT4_SB(sb)->s_freeinodes_counter));
3243 BUFFER_TRACE(sbh, "marking dirty");
3244 mark_buffer_dirty(sbh);
3246 error = sync_dirty_buffer(sbh);
3250 error = buffer_write_io_error(sbh);
3252 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3254 clear_buffer_write_io_error(sbh);
3255 set_buffer_uptodate(sbh);
3262 * Have we just finished recovery? If so, and if we are mounting (or
3263 * remounting) the filesystem readonly, then we will end up with a
3264 * consistent fs on disk. Record that fact.
3266 static void ext4_mark_recovery_complete(struct super_block *sb,
3267 struct ext4_super_block *es)
3269 journal_t *journal = EXT4_SB(sb)->s_journal;
3271 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3272 BUG_ON(journal != NULL);
3275 jbd2_journal_lock_updates(journal);
3276 if (jbd2_journal_flush(journal) < 0)
3279 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3280 sb->s_flags & MS_RDONLY) {
3281 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3282 ext4_commit_super(sb, 1);
3286 jbd2_journal_unlock_updates(journal);
3290 * If we are mounting (or read-write remounting) a filesystem whose journal
3291 * has recorded an error from a previous lifetime, move that error to the
3292 * main filesystem now.
3294 static void ext4_clear_journal_err(struct super_block *sb,
3295 struct ext4_super_block *es)
3301 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3303 journal = EXT4_SB(sb)->s_journal;
3306 * Now check for any error status which may have been recorded in the
3307 * journal by a prior ext4_error() or ext4_abort()
3310 j_errno = jbd2_journal_errno(journal);
3314 errstr = ext4_decode_error(sb, j_errno, nbuf);
3315 ext4_warning(sb, __func__, "Filesystem error recorded "
3316 "from previous mount: %s", errstr);
3317 ext4_warning(sb, __func__, "Marking fs in need of "
3318 "filesystem check.");
3320 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3321 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3322 ext4_commit_super(sb, 1);
3324 jbd2_journal_clear_err(journal);
3329 * Force the running and committing transactions to commit,
3330 * and wait on the commit.
3332 int ext4_force_commit(struct super_block *sb)
3337 if (sb->s_flags & MS_RDONLY)
3340 journal = EXT4_SB(sb)->s_journal;
3342 ret = ext4_journal_force_commit(journal);
3347 static void ext4_write_super(struct super_block *sb)
3350 ext4_commit_super(sb, 1);
3354 static int ext4_sync_fs(struct super_block *sb, int wait)
3359 trace_ext4_sync_fs(sb, wait);
3360 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3362 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
3368 * LVM calls this function before a (read-only) snapshot is created. This
3369 * gives us a chance to flush the journal completely and mark the fs clean.
3371 static int ext4_freeze(struct super_block *sb)
3376 if (sb->s_flags & MS_RDONLY)
3379 journal = EXT4_SB(sb)->s_journal;
3381 /* Now we set up the journal barrier. */
3382 jbd2_journal_lock_updates(journal);
3385 * Don't clear the needs_recovery flag if we failed to flush
3388 error = jbd2_journal_flush(journal);
3391 jbd2_journal_unlock_updates(journal);
3395 /* Journal blocked and flushed, clear needs_recovery flag. */
3396 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3397 error = ext4_commit_super(sb, 1);
3404 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3405 * flag here, even though the filesystem is not technically dirty yet.
3407 static int ext4_unfreeze(struct super_block *sb)
3409 if (sb->s_flags & MS_RDONLY)
3413 /* Reset the needs_recovery flag before the fs is unlocked. */
3414 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3415 ext4_commit_super(sb, 1);
3417 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3421 static int ext4_remount(struct super_block *sb, int *flags, char *data)
3423 struct ext4_super_block *es;
3424 struct ext4_sb_info *sbi = EXT4_SB(sb);
3425 ext4_fsblk_t n_blocks_count = 0;
3426 unsigned long old_sb_flags;
3427 struct ext4_mount_options old_opts;
3429 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3437 /* Store the original options */
3439 old_sb_flags = sb->s_flags;
3440 old_opts.s_mount_opt = sbi->s_mount_opt;
3441 old_opts.s_resuid = sbi->s_resuid;
3442 old_opts.s_resgid = sbi->s_resgid;
3443 old_opts.s_commit_interval = sbi->s_commit_interval;
3444 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3445 old_opts.s_max_batch_time = sbi->s_max_batch_time;
3447 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3448 for (i = 0; i < MAXQUOTAS; i++)
3449 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3451 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3452 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3455 * Allow the "check" option to be passed as a remount option.
3457 if (!parse_options(data, sb, NULL, &journal_ioprio,
3458 &n_blocks_count, 1)) {
3463 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
3464 ext4_abort(sb, __func__, "Abort forced by user");
3466 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3467 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3471 if (sbi->s_journal) {
3472 ext4_init_journal_params(sb, sbi->s_journal);
3473 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3476 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3477 n_blocks_count > ext4_blocks_count(es)) {
3478 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
3483 if (*flags & MS_RDONLY) {
3485 * First of all, the unconditional stuff we have to do
3486 * to disable replay of the journal when we next remount
3488 sb->s_flags |= MS_RDONLY;
3491 * OK, test if we are remounting a valid rw partition
3492 * readonly, and if so set the rdonly flag and then
3493 * mark the partition as valid again.
3495 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3496 (sbi->s_mount_state & EXT4_VALID_FS))
3497 es->s_state = cpu_to_le16(sbi->s_mount_state);
3500 ext4_mark_recovery_complete(sb, es);
3502 /* Make sure we can mount this feature set readwrite */
3503 if (!ext4_feature_set_ok(sb, 0)) {
3508 * Make sure the group descriptor checksums
3509 * are sane. If they aren't, refuse to remount r/w.
3511 for (g = 0; g < sbi->s_groups_count; g++) {
3512 struct ext4_group_desc *gdp =
3513 ext4_get_group_desc(sb, g, NULL);
3515 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3516 ext4_msg(sb, KERN_ERR,
3517 "ext4_remount: Checksum for group %u failed (%u!=%u)",
3518 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3519 le16_to_cpu(gdp->bg_checksum));
3526 * If we have an unprocessed orphan list hanging
3527 * around from a previously readonly bdev mount,
3528 * require a full umount/remount for now.
3530 if (es->s_last_orphan) {
3531 ext4_msg(sb, KERN_WARNING, "Couldn't "
3532 "remount RDWR because of unprocessed "
3533 "orphan inode list. Please "
3534 "umount/remount instead");
3540 * Mounting a RDONLY partition read-write, so reread
3541 * and store the current valid flag. (It may have
3542 * been changed by e2fsck since we originally mounted
3546 ext4_clear_journal_err(sb, es);
3547 sbi->s_mount_state = le16_to_cpu(es->s_state);
3548 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3550 if (!ext4_setup_super(sb, es, 0))
3551 sb->s_flags &= ~MS_RDONLY;
3554 ext4_setup_system_zone(sb);
3555 if (sbi->s_journal == NULL)
3556 ext4_commit_super(sb, 1);
3559 /* Release old quota file names */
3560 for (i = 0; i < MAXQUOTAS; i++)
3561 if (old_opts.s_qf_names[i] &&
3562 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3563 kfree(old_opts.s_qf_names[i]);
3570 sb->s_flags = old_sb_flags;
3571 sbi->s_mount_opt = old_opts.s_mount_opt;
3572 sbi->s_resuid = old_opts.s_resuid;
3573 sbi->s_resgid = old_opts.s_resgid;
3574 sbi->s_commit_interval = old_opts.s_commit_interval;
3575 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3576 sbi->s_max_batch_time = old_opts.s_max_batch_time;
3578 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3579 for (i = 0; i < MAXQUOTAS; i++) {
3580 if (sbi->s_qf_names[i] &&
3581 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3582 kfree(sbi->s_qf_names[i]);
3583 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3591 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3593 struct super_block *sb = dentry->d_sb;
3594 struct ext4_sb_info *sbi = EXT4_SB(sb);
3595 struct ext4_super_block *es = sbi->s_es;
3598 if (test_opt(sb, MINIX_DF)) {
3599 sbi->s_overhead_last = 0;
3600 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3601 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3602 ext4_fsblk_t overhead = 0;
3605 * Compute the overhead (FS structures). This is constant
3606 * for a given filesystem unless the number of block groups
3607 * changes so we cache the previous value until it does.
3611 * All of the blocks before first_data_block are
3614 overhead = le32_to_cpu(es->s_first_data_block);
3617 * Add the overhead attributed to the superblock and
3618 * block group descriptors. If the sparse superblocks
3619 * feature is turned on, then not all groups have this.
3621 for (i = 0; i < ngroups; i++) {
3622 overhead += ext4_bg_has_super(sb, i) +
3623 ext4_bg_num_gdb(sb, i);
3628 * Every block group has an inode bitmap, a block
3629 * bitmap, and an inode table.
3631 overhead += ngroups * (2 + sbi->s_itb_per_group);
3632 sbi->s_overhead_last = overhead;
3634 sbi->s_blocks_last = ext4_blocks_count(es);
3637 buf->f_type = EXT4_SUPER_MAGIC;
3638 buf->f_bsize = sb->s_blocksize;
3639 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3640 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3641 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3642 ext4_free_blocks_count_set(es, buf->f_bfree);
3643 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3644 if (buf->f_bfree < ext4_r_blocks_count(es))
3646 buf->f_files = le32_to_cpu(es->s_inodes_count);
3647 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3648 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3649 buf->f_namelen = EXT4_NAME_LEN;
3650 fsid = le64_to_cpup((void *)es->s_uuid) ^
3651 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3652 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3653 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3658 /* Helper function for writing quotas on sync - we need to start transaction
3659 * before quota file is locked for write. Otherwise the are possible deadlocks:
3660 * Process 1 Process 2
3661 * ext4_create() quota_sync()
3662 * jbd2_journal_start() write_dquot()
3663 * vfs_dq_init() down(dqio_mutex)
3664 * down(dqio_mutex) jbd2_journal_start()
3670 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3672 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3675 static int ext4_write_dquot(struct dquot *dquot)
3679 struct inode *inode;
3681 inode = dquot_to_inode(dquot);
3682 handle = ext4_journal_start(inode,
3683 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3685 return PTR_ERR(handle);
3686 ret = dquot_commit(dquot);
3687 err = ext4_journal_stop(handle);
3693 static int ext4_acquire_dquot(struct dquot *dquot)
3698 handle = ext4_journal_start(dquot_to_inode(dquot),
3699 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3701 return PTR_ERR(handle);
3702 ret = dquot_acquire(dquot);
3703 err = ext4_journal_stop(handle);
3709 static int ext4_release_dquot(struct dquot *dquot)
3714 handle = ext4_journal_start(dquot_to_inode(dquot),
3715 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3716 if (IS_ERR(handle)) {
3717 /* Release dquot anyway to avoid endless cycle in dqput() */
3718 dquot_release(dquot);
3719 return PTR_ERR(handle);
3721 ret = dquot_release(dquot);
3722 err = ext4_journal_stop(handle);
3728 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3730 /* Are we journaling quotas? */
3731 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3732 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3733 dquot_mark_dquot_dirty(dquot);
3734 return ext4_write_dquot(dquot);
3736 return dquot_mark_dquot_dirty(dquot);
3740 static int ext4_write_info(struct super_block *sb, int type)
3745 /* Data block + inode block */
3746 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3748 return PTR_ERR(handle);
3749 ret = dquot_commit_info(sb, type);
3750 err = ext4_journal_stop(handle);
3757 * Turn on quotas during mount time - we need to find
3758 * the quota file and such...
3760 static int ext4_quota_on_mount(struct super_block *sb, int type)
3762 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3763 EXT4_SB(sb)->s_jquota_fmt, type);
3767 * Standard function to be called on quota_on
3769 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3770 char *name, int remount)
3775 if (!test_opt(sb, QUOTA))
3777 /* When remounting, no checks are needed and in fact, name is NULL */
3779 return vfs_quota_on(sb, type, format_id, name, remount);
3781 err = kern_path(name, LOOKUP_FOLLOW, &path);
3785 /* Quotafile not on the same filesystem? */
3786 if (path.mnt->mnt_sb != sb) {
3790 /* Journaling quota? */
3791 if (EXT4_SB(sb)->s_qf_names[type]) {
3792 /* Quotafile not in fs root? */
3793 if (path.dentry->d_parent != sb->s_root)
3794 ext4_msg(sb, KERN_WARNING,
3795 "Quota file not on filesystem root. "
3796 "Journaled quota will not work");
3800 * When we journal data on quota file, we have to flush journal to see
3801 * all updates to the file when we bypass pagecache...
3803 if (EXT4_SB(sb)->s_journal &&
3804 ext4_should_journal_data(path.dentry->d_inode)) {
3806 * We don't need to lock updates but journal_flush() could
3807 * otherwise be livelocked...
3809 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3810 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3811 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3818 err = vfs_quota_on_path(sb, type, format_id, &path);
3823 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3824 * acquiring the locks... As quota files are never truncated and quota code
3825 * itself serializes the operations (and noone else should touch the files)
3826 * we don't have to be afraid of races */
3827 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3828 size_t len, loff_t off)
3830 struct inode *inode = sb_dqopt(sb)->files[type];
3831 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3833 int offset = off & (sb->s_blocksize - 1);
3836 struct buffer_head *bh;
3837 loff_t i_size = i_size_read(inode);
3841 if (off+len > i_size)
3844 while (toread > 0) {
3845 tocopy = sb->s_blocksize - offset < toread ?
3846 sb->s_blocksize - offset : toread;
3847 bh = ext4_bread(NULL, inode, blk, 0, &err);
3850 if (!bh) /* A hole? */
3851 memset(data, 0, tocopy);
3853 memcpy(data, bh->b_data+offset, tocopy);
3863 /* Write to quotafile (we know the transaction is already started and has
3864 * enough credits) */
3865 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3866 const char *data, size_t len, loff_t off)
3868 struct inode *inode = sb_dqopt(sb)->files[type];
3869 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3871 int offset = off & (sb->s_blocksize - 1);
3873 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3874 size_t towrite = len;
3875 struct buffer_head *bh;
3876 handle_t *handle = journal_current_handle();
3878 if (EXT4_SB(sb)->s_journal && !handle) {
3879 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3880 " cancelled because transaction is not started",
3881 (unsigned long long)off, (unsigned long long)len);
3884 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3885 while (towrite > 0) {
3886 tocopy = sb->s_blocksize - offset < towrite ?
3887 sb->s_blocksize - offset : towrite;
3888 bh = ext4_bread(handle, inode, blk, 1, &err);
3891 if (journal_quota) {
3892 err = ext4_journal_get_write_access(handle, bh);
3899 memcpy(bh->b_data+offset, data, tocopy);
3900 flush_dcache_page(bh->b_page);
3903 err = ext4_handle_dirty_metadata(handle, NULL, bh);
3905 /* Always do at least ordered writes for quotas */
3906 err = ext4_jbd2_file_inode(handle, inode);
3907 mark_buffer_dirty(bh);
3918 if (len == towrite) {
3919 mutex_unlock(&inode->i_mutex);
3922 if (inode->i_size < off+len-towrite) {
3923 i_size_write(inode, off+len-towrite);
3924 EXT4_I(inode)->i_disksize = inode->i_size;
3926 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3927 ext4_mark_inode_dirty(handle, inode);
3928 mutex_unlock(&inode->i_mutex);
3929 return len - towrite;
3934 static int ext4_get_sb(struct file_system_type *fs_type, int flags,
3935 const char *dev_name, void *data, struct vfsmount *mnt)
3937 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
3940 static struct file_system_type ext4_fs_type = {
3941 .owner = THIS_MODULE,
3943 .get_sb = ext4_get_sb,
3944 .kill_sb = kill_block_super,
3945 .fs_flags = FS_REQUIRES_DEV,
3948 #ifdef CONFIG_EXT4DEV_COMPAT
3949 static int ext4dev_get_sb(struct file_system_type *fs_type, int flags,
3950 const char *dev_name, void *data,struct vfsmount *mnt)
3952 printk(KERN_WARNING "EXT4-fs (%s): Update your userspace programs "
3953 "to mount using ext4\n", dev_name);
3954 printk(KERN_WARNING "EXT4-fs (%s): ext4dev backwards compatibility "
3955 "will go away by 2.6.31\n", dev_name);
3956 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
3959 static struct file_system_type ext4dev_fs_type = {
3960 .owner = THIS_MODULE,
3962 .get_sb = ext4dev_get_sb,
3963 .kill_sb = kill_block_super,
3964 .fs_flags = FS_REQUIRES_DEV,
3966 MODULE_ALIAS("ext4dev");
3969 static int __init init_ext4_fs(void)
3973 err = init_ext4_system_zone();
3976 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
3979 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3980 err = init_ext4_mballoc();
3984 err = init_ext4_xattr();
3987 err = init_inodecache();
3990 err = register_filesystem(&ext4_fs_type);
3993 #ifdef CONFIG_EXT4DEV_COMPAT
3994 err = register_filesystem(&ext4dev_fs_type);
3996 unregister_filesystem(&ext4_fs_type);
4002 destroy_inodecache();
4006 exit_ext4_mballoc();
4008 remove_proc_entry("fs/ext4", NULL);
4009 kset_unregister(ext4_kset);
4011 exit_ext4_system_zone();
4015 static void __exit exit_ext4_fs(void)
4017 unregister_filesystem(&ext4_fs_type);
4018 #ifdef CONFIG_EXT4DEV_COMPAT
4019 unregister_filesystem(&ext4dev_fs_type);
4021 destroy_inodecache();
4023 exit_ext4_mballoc();
4024 remove_proc_entry("fs/ext4", NULL);
4025 kset_unregister(ext4_kset);
4026 exit_ext4_system_zone();
4029 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
4030 MODULE_DESCRIPTION("Fourth Extended Filesystem");
4031 MODULE_LICENSE("GPL");
4032 module_init(init_ext4_fs)
4033 module_exit(exit_ext4_fs)