2 * linux/fs/ext4/super.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
19 #include <linux/module.h>
20 #include <linux/string.h>
22 #include <linux/time.h>
23 #include <linux/jbd2.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/blkdev.h>
27 #include <linux/parser.h>
28 #include <linux/smp_lock.h>
29 #include <linux/buffer_head.h>
30 #include <linux/exportfs.h>
31 #include <linux/vfs.h>
32 #include <linux/random.h>
33 #include <linux/mount.h>
34 #include <linux/namei.h>
35 #include <linux/quotaops.h>
36 #include <linux/seq_file.h>
37 #include <linux/proc_fs.h>
38 #include <linux/marker.h>
39 #include <linux/log2.h>
40 #include <linux/crc16.h>
41 #include <asm/uaccess.h>
44 #include "ext4_jbd2.h"
50 struct proc_dir_entry *ext4_proc_root;
52 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
53 unsigned long journal_devnum);
54 static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
56 static void ext4_commit_super(struct super_block *sb,
57 struct ext4_super_block *es, int sync);
58 static void ext4_mark_recovery_complete(struct super_block *sb,
59 struct ext4_super_block *es);
60 static void ext4_clear_journal_err(struct super_block *sb,
61 struct ext4_super_block *es);
62 static int ext4_sync_fs(struct super_block *sb, int wait);
63 static const char *ext4_decode_error(struct super_block *sb, int errno,
65 static int ext4_remount(struct super_block *sb, int *flags, char *data);
66 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
67 static void ext4_unlockfs(struct super_block *sb);
68 static void ext4_write_super(struct super_block *sb);
69 static void ext4_write_super_lockfs(struct super_block *sb);
72 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
73 struct ext4_group_desc *bg)
75 return le32_to_cpu(bg->bg_block_bitmap_lo) |
76 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
77 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
80 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
81 struct ext4_group_desc *bg)
83 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
84 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
85 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
88 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
89 struct ext4_group_desc *bg)
91 return le32_to_cpu(bg->bg_inode_table_lo) |
92 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
93 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
96 __u32 ext4_free_blks_count(struct super_block *sb,
97 struct ext4_group_desc *bg)
99 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
100 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
101 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
104 __u32 ext4_free_inodes_count(struct super_block *sb,
105 struct ext4_group_desc *bg)
107 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
108 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
109 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
112 __u32 ext4_used_dirs_count(struct super_block *sb,
113 struct ext4_group_desc *bg)
115 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
116 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
117 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
120 __u32 ext4_itable_unused_count(struct super_block *sb,
121 struct ext4_group_desc *bg)
123 return le16_to_cpu(bg->bg_itable_unused_lo) |
124 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
125 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
128 void ext4_block_bitmap_set(struct super_block *sb,
129 struct ext4_group_desc *bg, ext4_fsblk_t blk)
131 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
132 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
133 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
136 void ext4_inode_bitmap_set(struct super_block *sb,
137 struct ext4_group_desc *bg, ext4_fsblk_t blk)
139 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
140 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
141 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
144 void ext4_inode_table_set(struct super_block *sb,
145 struct ext4_group_desc *bg, ext4_fsblk_t blk)
147 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
148 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
149 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
152 void ext4_free_blks_set(struct super_block *sb,
153 struct ext4_group_desc *bg, __u32 count)
155 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
156 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
157 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
160 void ext4_free_inodes_set(struct super_block *sb,
161 struct ext4_group_desc *bg, __u32 count)
163 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
164 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
165 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
168 void ext4_used_dirs_set(struct super_block *sb,
169 struct ext4_group_desc *bg, __u32 count)
171 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
172 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
173 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
176 void ext4_itable_unused_set(struct super_block *sb,
177 struct ext4_group_desc *bg, __u32 count)
179 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
180 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
181 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
185 * Wrappers for jbd2_journal_start/end.
187 * The only special thing we need to do here is to make sure that all
188 * journal_end calls result in the superblock being marked dirty, so
189 * that sync() will call the filesystem's write_super callback if
192 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
196 if (sb->s_flags & MS_RDONLY)
197 return ERR_PTR(-EROFS);
199 /* Special case here: if the journal has aborted behind our
200 * backs (eg. EIO in the commit thread), then we still need to
201 * take the FS itself readonly cleanly. */
202 journal = EXT4_SB(sb)->s_journal;
204 if (is_journal_aborted(journal)) {
205 ext4_abort(sb, __func__,
206 "Detected aborted journal");
207 return ERR_PTR(-EROFS);
209 return jbd2_journal_start(journal, nblocks);
212 * We're not journaling, return the appropriate indication.
214 current->journal_info = EXT4_NOJOURNAL_HANDLE;
215 return current->journal_info;
219 * The only special thing we need to do here is to make sure that all
220 * jbd2_journal_stop calls result in the superblock being marked dirty, so
221 * that sync() will call the filesystem's write_super callback if
224 int __ext4_journal_stop(const char *where, handle_t *handle)
226 struct super_block *sb;
230 if (!ext4_handle_valid(handle)) {
232 * Do this here since we don't call jbd2_journal_stop() in
235 current->journal_info = NULL;
238 sb = handle->h_transaction->t_journal->j_private;
240 rc = jbd2_journal_stop(handle);
245 __ext4_std_error(sb, where, err);
249 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
250 struct buffer_head *bh, handle_t *handle, int err)
253 const char *errstr = ext4_decode_error(NULL, err, nbuf);
255 BUG_ON(!ext4_handle_valid(handle));
258 BUFFER_TRACE(bh, "abort");
263 if (is_handle_aborted(handle))
266 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
267 caller, errstr, err_fn);
269 jbd2_journal_abort_handle(handle);
272 /* Deal with the reporting of failure conditions on a filesystem such as
273 * inconsistencies detected or read IO failures.
275 * On ext2, we can store the error state of the filesystem in the
276 * superblock. That is not possible on ext4, because we may have other
277 * write ordering constraints on the superblock which prevent us from
278 * writing it out straight away; and given that the journal is about to
279 * be aborted, we can't rely on the current, or future, transactions to
280 * write out the superblock safely.
282 * We'll just use the jbd2_journal_abort() error code to record an error in
283 * the journal instead. On recovery, the journal will compain about
284 * that error until we've noted it down and cleared it.
287 static void ext4_handle_error(struct super_block *sb)
289 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
291 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
292 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
294 if (sb->s_flags & MS_RDONLY)
297 if (!test_opt(sb, ERRORS_CONT)) {
298 journal_t *journal = EXT4_SB(sb)->s_journal;
300 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
302 jbd2_journal_abort(journal, -EIO);
304 if (test_opt(sb, ERRORS_RO)) {
305 printk(KERN_CRIT "Remounting filesystem read-only\n");
306 sb->s_flags |= MS_RDONLY;
308 ext4_commit_super(sb, es, 1);
309 if (test_opt(sb, ERRORS_PANIC))
310 panic("EXT4-fs (device %s): panic forced after error\n",
314 void ext4_error(struct super_block *sb, const char *function,
315 const char *fmt, ...)
320 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
325 ext4_handle_error(sb);
328 static const char *ext4_decode_error(struct super_block *sb, int errno,
335 errstr = "IO failure";
338 errstr = "Out of memory";
341 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
342 errstr = "Journal has aborted";
344 errstr = "Readonly filesystem";
347 /* If the caller passed in an extra buffer for unknown
348 * errors, textualise them now. Else we just return
351 /* Check for truncated error codes... */
352 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
361 /* __ext4_std_error decodes expected errors from journaling functions
362 * automatically and invokes the appropriate error response. */
364 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
369 /* Special case: if the error is EROFS, and we're not already
370 * inside a transaction, then there's really no point in logging
372 if (errno == -EROFS && journal_current_handle() == NULL &&
373 (sb->s_flags & MS_RDONLY))
376 errstr = ext4_decode_error(sb, errno, nbuf);
377 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
378 sb->s_id, function, errstr);
380 ext4_handle_error(sb);
384 * ext4_abort is a much stronger failure handler than ext4_error. The
385 * abort function may be used to deal with unrecoverable failures such
386 * as journal IO errors or ENOMEM at a critical moment in log management.
388 * We unconditionally force the filesystem into an ABORT|READONLY state,
389 * unless the error response on the fs has been set to panic in which
390 * case we take the easy way out and panic immediately.
393 void ext4_abort(struct super_block *sb, const char *function,
394 const char *fmt, ...)
398 printk(KERN_CRIT "ext4_abort called.\n");
401 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
406 if (test_opt(sb, ERRORS_PANIC))
407 panic("EXT4-fs panic from previous error\n");
409 if (sb->s_flags & MS_RDONLY)
412 printk(KERN_CRIT "Remounting filesystem read-only\n");
413 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
414 sb->s_flags |= MS_RDONLY;
415 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
416 if (EXT4_SB(sb)->s_journal)
417 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
420 void ext4_warning(struct super_block *sb, const char *function,
421 const char *fmt, ...)
426 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
433 void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
434 const char *function, const char *fmt, ...)
439 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
442 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
447 if (test_opt(sb, ERRORS_CONT)) {
448 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
449 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
450 ext4_commit_super(sb, es, 0);
453 ext4_unlock_group(sb, grp);
454 ext4_handle_error(sb);
456 * We only get here in the ERRORS_RO case; relocking the group
457 * may be dangerous, but nothing bad will happen since the
458 * filesystem will have already been marked read/only and the
459 * journal has been aborted. We return 1 as a hint to callers
460 * who might what to use the return value from
461 * ext4_grp_locked_error() to distinguish beween the
462 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
463 * aggressively from the ext4 function in question, with a
464 * more appropriate error code.
466 ext4_lock_group(sb, grp);
471 void ext4_update_dynamic_rev(struct super_block *sb)
473 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
475 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
478 ext4_warning(sb, __func__,
479 "updating to rev %d because of new feature flag, "
480 "running e2fsck is recommended",
483 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
484 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
485 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
486 /* leave es->s_feature_*compat flags alone */
487 /* es->s_uuid will be set by e2fsck if empty */
490 * The rest of the superblock fields should be zero, and if not it
491 * means they are likely already in use, so leave them alone. We
492 * can leave it up to e2fsck to clean up any inconsistencies there.
497 * Open the external journal device
499 static struct block_device *ext4_blkdev_get(dev_t dev)
501 struct block_device *bdev;
502 char b[BDEVNAME_SIZE];
504 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
510 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
511 __bdevname(dev, b), PTR_ERR(bdev));
516 * Release the journal device
518 static int ext4_blkdev_put(struct block_device *bdev)
521 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
524 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
526 struct block_device *bdev;
529 bdev = sbi->journal_bdev;
531 ret = ext4_blkdev_put(bdev);
532 sbi->journal_bdev = NULL;
537 static inline struct inode *orphan_list_entry(struct list_head *l)
539 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
542 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
546 printk(KERN_ERR "sb orphan head is %d\n",
547 le32_to_cpu(sbi->s_es->s_last_orphan));
549 printk(KERN_ERR "sb_info orphan list:\n");
550 list_for_each(l, &sbi->s_orphan) {
551 struct inode *inode = orphan_list_entry(l);
553 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
554 inode->i_sb->s_id, inode->i_ino, inode,
555 inode->i_mode, inode->i_nlink,
560 static void ext4_put_super(struct super_block *sb)
562 struct ext4_sb_info *sbi = EXT4_SB(sb);
563 struct ext4_super_block *es = sbi->s_es;
567 ext4_ext_release(sb);
568 ext4_xattr_put_super(sb);
569 if (sbi->s_journal) {
570 err = jbd2_journal_destroy(sbi->s_journal);
571 sbi->s_journal = NULL;
573 ext4_abort(sb, __func__,
574 "Couldn't clean up the journal");
576 if (!(sb->s_flags & MS_RDONLY)) {
577 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
578 es->s_state = cpu_to_le16(sbi->s_mount_state);
579 ext4_commit_super(sb, es, 1);
582 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
583 remove_proc_entry(sb->s_id, ext4_proc_root);
586 for (i = 0; i < sbi->s_gdb_count; i++)
587 brelse(sbi->s_group_desc[i]);
588 kfree(sbi->s_group_desc);
589 kfree(sbi->s_flex_groups);
590 percpu_counter_destroy(&sbi->s_freeblocks_counter);
591 percpu_counter_destroy(&sbi->s_freeinodes_counter);
592 percpu_counter_destroy(&sbi->s_dirs_counter);
593 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
596 for (i = 0; i < MAXQUOTAS; i++)
597 kfree(sbi->s_qf_names[i]);
600 /* Debugging code just in case the in-memory inode orphan list
601 * isn't empty. The on-disk one can be non-empty if we've
602 * detected an error and taken the fs readonly, but the
603 * in-memory list had better be clean by this point. */
604 if (!list_empty(&sbi->s_orphan))
605 dump_orphan_list(sb, sbi);
606 J_ASSERT(list_empty(&sbi->s_orphan));
608 invalidate_bdev(sb->s_bdev);
609 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
611 * Invalidate the journal device's buffers. We don't want them
612 * floating about in memory - the physical journal device may
613 * hotswapped, and it breaks the `ro-after' testing code.
615 sync_blockdev(sbi->journal_bdev);
616 invalidate_bdev(sbi->journal_bdev);
617 ext4_blkdev_remove(sbi);
619 sb->s_fs_info = NULL;
624 static struct kmem_cache *ext4_inode_cachep;
627 * Called inside transaction, so use GFP_NOFS
629 static struct inode *ext4_alloc_inode(struct super_block *sb)
631 struct ext4_inode_info *ei;
633 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
636 #ifdef CONFIG_EXT4_FS_POSIX_ACL
637 ei->i_acl = EXT4_ACL_NOT_CACHED;
638 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
640 ei->vfs_inode.i_version = 1;
641 ei->vfs_inode.i_data.writeback_index = 0;
642 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
643 INIT_LIST_HEAD(&ei->i_prealloc_list);
644 spin_lock_init(&ei->i_prealloc_lock);
646 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
647 * therefore it can be null here. Don't check it, just initialize
650 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
651 ei->i_reserved_data_blocks = 0;
652 ei->i_reserved_meta_blocks = 0;
653 ei->i_allocated_meta_blocks = 0;
654 ei->i_delalloc_reserved_flag = 0;
655 spin_lock_init(&(ei->i_block_reservation_lock));
656 return &ei->vfs_inode;
659 static void ext4_destroy_inode(struct inode *inode)
661 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
662 printk("EXT4 Inode %p: orphan list check failed!\n",
664 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
665 EXT4_I(inode), sizeof(struct ext4_inode_info),
669 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
672 static void init_once(void *foo)
674 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
676 INIT_LIST_HEAD(&ei->i_orphan);
677 #ifdef CONFIG_EXT4_FS_XATTR
678 init_rwsem(&ei->xattr_sem);
680 init_rwsem(&ei->i_data_sem);
681 inode_init_once(&ei->vfs_inode);
684 static int init_inodecache(void)
686 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
687 sizeof(struct ext4_inode_info),
688 0, (SLAB_RECLAIM_ACCOUNT|
691 if (ext4_inode_cachep == NULL)
696 static void destroy_inodecache(void)
698 kmem_cache_destroy(ext4_inode_cachep);
701 static void ext4_clear_inode(struct inode *inode)
703 #ifdef CONFIG_EXT4_FS_POSIX_ACL
704 if (EXT4_I(inode)->i_acl &&
705 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
706 posix_acl_release(EXT4_I(inode)->i_acl);
707 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
709 if (EXT4_I(inode)->i_default_acl &&
710 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
711 posix_acl_release(EXT4_I(inode)->i_default_acl);
712 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
715 ext4_discard_preallocations(inode);
716 if (EXT4_JOURNAL(inode))
717 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
718 &EXT4_I(inode)->jinode);
721 static inline void ext4_show_quota_options(struct seq_file *seq,
722 struct super_block *sb)
724 #if defined(CONFIG_QUOTA)
725 struct ext4_sb_info *sbi = EXT4_SB(sb);
727 if (sbi->s_jquota_fmt)
728 seq_printf(seq, ",jqfmt=%s",
729 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
731 if (sbi->s_qf_names[USRQUOTA])
732 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
734 if (sbi->s_qf_names[GRPQUOTA])
735 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
737 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
738 seq_puts(seq, ",usrquota");
740 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
741 seq_puts(seq, ",grpquota");
747 * - it's set to a non-default value OR
748 * - if the per-sb default is different from the global default
750 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
753 unsigned long def_mount_opts;
754 struct super_block *sb = vfs->mnt_sb;
755 struct ext4_sb_info *sbi = EXT4_SB(sb);
756 struct ext4_super_block *es = sbi->s_es;
758 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
759 def_errors = le16_to_cpu(es->s_errors);
761 if (sbi->s_sb_block != 1)
762 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
763 if (test_opt(sb, MINIX_DF))
764 seq_puts(seq, ",minixdf");
765 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
766 seq_puts(seq, ",grpid");
767 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
768 seq_puts(seq, ",nogrpid");
769 if (sbi->s_resuid != EXT4_DEF_RESUID ||
770 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
771 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
773 if (sbi->s_resgid != EXT4_DEF_RESGID ||
774 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
775 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
777 if (test_opt(sb, ERRORS_RO)) {
778 if (def_errors == EXT4_ERRORS_PANIC ||
779 def_errors == EXT4_ERRORS_CONTINUE) {
780 seq_puts(seq, ",errors=remount-ro");
783 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
784 seq_puts(seq, ",errors=continue");
785 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
786 seq_puts(seq, ",errors=panic");
787 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
788 seq_puts(seq, ",nouid32");
789 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
790 seq_puts(seq, ",debug");
791 if (test_opt(sb, OLDALLOC))
792 seq_puts(seq, ",oldalloc");
793 #ifdef CONFIG_EXT4_FS_XATTR
794 if (test_opt(sb, XATTR_USER) &&
795 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
796 seq_puts(seq, ",user_xattr");
797 if (!test_opt(sb, XATTR_USER) &&
798 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
799 seq_puts(seq, ",nouser_xattr");
802 #ifdef CONFIG_EXT4_FS_POSIX_ACL
803 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
804 seq_puts(seq, ",acl");
805 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
806 seq_puts(seq, ",noacl");
808 if (!test_opt(sb, RESERVATION))
809 seq_puts(seq, ",noreservation");
810 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
811 seq_printf(seq, ",commit=%u",
812 (unsigned) (sbi->s_commit_interval / HZ));
814 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
815 seq_printf(seq, ",min_batch_time=%u",
816 (unsigned) sbi->s_min_batch_time);
818 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
819 seq_printf(seq, ",max_batch_time=%u",
820 (unsigned) sbi->s_min_batch_time);
824 * We're changing the default of barrier mount option, so
825 * let's always display its mount state so it's clear what its
828 seq_puts(seq, ",barrier=");
829 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
830 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
831 seq_puts(seq, ",journal_async_commit");
832 if (test_opt(sb, NOBH))
833 seq_puts(seq, ",nobh");
834 if (!test_opt(sb, EXTENTS))
835 seq_puts(seq, ",noextents");
836 if (test_opt(sb, I_VERSION))
837 seq_puts(seq, ",i_version");
838 if (!test_opt(sb, DELALLOC))
839 seq_puts(seq, ",nodelalloc");
843 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
845 * journal mode get enabled in different ways
846 * So just print the value even if we didn't specify it
848 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
849 seq_puts(seq, ",data=journal");
850 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
851 seq_puts(seq, ",data=ordered");
852 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
853 seq_puts(seq, ",data=writeback");
855 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
856 seq_printf(seq, ",inode_readahead_blks=%u",
857 sbi->s_inode_readahead_blks);
859 if (test_opt(sb, DATA_ERR_ABORT))
860 seq_puts(seq, ",data_err=abort");
862 ext4_show_quota_options(seq, sb);
867 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
868 u64 ino, u32 generation)
872 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
873 return ERR_PTR(-ESTALE);
874 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
875 return ERR_PTR(-ESTALE);
877 /* iget isn't really right if the inode is currently unallocated!!
879 * ext4_read_inode will return a bad_inode if the inode had been
880 * deleted, so we should be safe.
882 * Currently we don't know the generation for parent directory, so
883 * a generation of 0 means "accept any"
885 inode = ext4_iget(sb, ino);
887 return ERR_CAST(inode);
888 if (generation && inode->i_generation != generation) {
890 return ERR_PTR(-ESTALE);
896 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
897 int fh_len, int fh_type)
899 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
903 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
904 int fh_len, int fh_type)
906 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
911 * Try to release metadata pages (indirect blocks, directories) which are
912 * mapped via the block device. Since these pages could have journal heads
913 * which would prevent try_to_free_buffers() from freeing them, we must use
914 * jbd2 layer's try_to_free_buffers() function to release them.
916 static int bdev_try_to_free_page(struct super_block *sb, struct page *page, gfp_t wait)
918 journal_t *journal = EXT4_SB(sb)->s_journal;
920 WARN_ON(PageChecked(page));
921 if (!page_has_buffers(page))
924 return jbd2_journal_try_to_free_buffers(journal, page,
926 return try_to_free_buffers(page);
930 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
931 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
933 static int ext4_dquot_initialize(struct inode *inode, int type);
934 static int ext4_dquot_drop(struct inode *inode);
935 static int ext4_write_dquot(struct dquot *dquot);
936 static int ext4_acquire_dquot(struct dquot *dquot);
937 static int ext4_release_dquot(struct dquot *dquot);
938 static int ext4_mark_dquot_dirty(struct dquot *dquot);
939 static int ext4_write_info(struct super_block *sb, int type);
940 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
941 char *path, int remount);
942 static int ext4_quota_on_mount(struct super_block *sb, int type);
943 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
944 size_t len, loff_t off);
945 static ssize_t ext4_quota_write(struct super_block *sb, int type,
946 const char *data, size_t len, loff_t off);
948 static struct dquot_operations ext4_quota_operations = {
949 .initialize = ext4_dquot_initialize,
950 .drop = ext4_dquot_drop,
951 .alloc_space = dquot_alloc_space,
952 .alloc_inode = dquot_alloc_inode,
953 .free_space = dquot_free_space,
954 .free_inode = dquot_free_inode,
955 .transfer = dquot_transfer,
956 .write_dquot = ext4_write_dquot,
957 .acquire_dquot = ext4_acquire_dquot,
958 .release_dquot = ext4_release_dquot,
959 .mark_dirty = ext4_mark_dquot_dirty,
960 .write_info = ext4_write_info
963 static struct quotactl_ops ext4_qctl_operations = {
964 .quota_on = ext4_quota_on,
965 .quota_off = vfs_quota_off,
966 .quota_sync = vfs_quota_sync,
967 .get_info = vfs_get_dqinfo,
968 .set_info = vfs_set_dqinfo,
969 .get_dqblk = vfs_get_dqblk,
970 .set_dqblk = vfs_set_dqblk
974 static const struct super_operations ext4_sops = {
975 .alloc_inode = ext4_alloc_inode,
976 .destroy_inode = ext4_destroy_inode,
977 .write_inode = ext4_write_inode,
978 .dirty_inode = ext4_dirty_inode,
979 .delete_inode = ext4_delete_inode,
980 .put_super = ext4_put_super,
981 .write_super = ext4_write_super,
982 .sync_fs = ext4_sync_fs,
983 .write_super_lockfs = ext4_write_super_lockfs,
984 .unlockfs = ext4_unlockfs,
985 .statfs = ext4_statfs,
986 .remount_fs = ext4_remount,
987 .clear_inode = ext4_clear_inode,
988 .show_options = ext4_show_options,
990 .quota_read = ext4_quota_read,
991 .quota_write = ext4_quota_write,
993 .bdev_try_to_free_page = bdev_try_to_free_page,
996 static const struct export_operations ext4_export_ops = {
997 .fh_to_dentry = ext4_fh_to_dentry,
998 .fh_to_parent = ext4_fh_to_parent,
999 .get_parent = ext4_get_parent,
1003 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1004 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
1005 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1006 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1007 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
1008 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1009 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
1010 Opt_journal_checksum, Opt_journal_async_commit,
1011 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1012 Opt_data_err_abort, Opt_data_err_ignore,
1013 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1014 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
1015 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
1016 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
1017 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
1018 Opt_inode_readahead_blks
1021 static const match_table_t tokens = {
1022 {Opt_bsd_df, "bsddf"},
1023 {Opt_minix_df, "minixdf"},
1024 {Opt_grpid, "grpid"},
1025 {Opt_grpid, "bsdgroups"},
1026 {Opt_nogrpid, "nogrpid"},
1027 {Opt_nogrpid, "sysvgroups"},
1028 {Opt_resgid, "resgid=%u"},
1029 {Opt_resuid, "resuid=%u"},
1031 {Opt_err_cont, "errors=continue"},
1032 {Opt_err_panic, "errors=panic"},
1033 {Opt_err_ro, "errors=remount-ro"},
1034 {Opt_nouid32, "nouid32"},
1035 {Opt_debug, "debug"},
1036 {Opt_oldalloc, "oldalloc"},
1037 {Opt_orlov, "orlov"},
1038 {Opt_user_xattr, "user_xattr"},
1039 {Opt_nouser_xattr, "nouser_xattr"},
1041 {Opt_noacl, "noacl"},
1042 {Opt_reservation, "reservation"},
1043 {Opt_noreservation, "noreservation"},
1044 {Opt_noload, "noload"},
1047 {Opt_commit, "commit=%u"},
1048 {Opt_min_batch_time, "min_batch_time=%u"},
1049 {Opt_max_batch_time, "max_batch_time=%u"},
1050 {Opt_journal_update, "journal=update"},
1051 {Opt_journal_inum, "journal=%u"},
1052 {Opt_journal_dev, "journal_dev=%u"},
1053 {Opt_journal_checksum, "journal_checksum"},
1054 {Opt_journal_async_commit, "journal_async_commit"},
1055 {Opt_abort, "abort"},
1056 {Opt_data_journal, "data=journal"},
1057 {Opt_data_ordered, "data=ordered"},
1058 {Opt_data_writeback, "data=writeback"},
1059 {Opt_data_err_abort, "data_err=abort"},
1060 {Opt_data_err_ignore, "data_err=ignore"},
1061 {Opt_offusrjquota, "usrjquota="},
1062 {Opt_usrjquota, "usrjquota=%s"},
1063 {Opt_offgrpjquota, "grpjquota="},
1064 {Opt_grpjquota, "grpjquota=%s"},
1065 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1066 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1067 {Opt_grpquota, "grpquota"},
1068 {Opt_noquota, "noquota"},
1069 {Opt_quota, "quota"},
1070 {Opt_usrquota, "usrquota"},
1071 {Opt_barrier, "barrier=%u"},
1072 {Opt_extents, "extents"},
1073 {Opt_noextents, "noextents"},
1074 {Opt_i_version, "i_version"},
1075 {Opt_stripe, "stripe=%u"},
1076 {Opt_resize, "resize"},
1077 {Opt_delalloc, "delalloc"},
1078 {Opt_nodelalloc, "nodelalloc"},
1079 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1083 static ext4_fsblk_t get_sb_block(void **data)
1085 ext4_fsblk_t sb_block;
1086 char *options = (char *) *data;
1088 if (!options || strncmp(options, "sb=", 3) != 0)
1089 return 1; /* Default location */
1091 /*todo: use simple_strtoll with >32bit ext4 */
1092 sb_block = simple_strtoul(options, &options, 0);
1093 if (*options && *options != ',') {
1094 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1098 if (*options == ',')
1100 *data = (void *) options;
1104 static int parse_options(char *options, struct super_block *sb,
1105 unsigned int *inum, unsigned long *journal_devnum,
1106 ext4_fsblk_t *n_blocks_count, int is_remount)
1108 struct ext4_sb_info *sbi = EXT4_SB(sb);
1110 substring_t args[MAX_OPT_ARGS];
1117 ext4_fsblk_t last_block;
1122 while ((p = strsep(&options, ",")) != NULL) {
1127 token = match_token(p, tokens, args);
1130 clear_opt(sbi->s_mount_opt, MINIX_DF);
1133 set_opt(sbi->s_mount_opt, MINIX_DF);
1136 set_opt(sbi->s_mount_opt, GRPID);
1139 clear_opt(sbi->s_mount_opt, GRPID);
1142 if (match_int(&args[0], &option))
1144 sbi->s_resuid = option;
1147 if (match_int(&args[0], &option))
1149 sbi->s_resgid = option;
1152 /* handled by get_sb_block() instead of here */
1153 /* *sb_block = match_int(&args[0]); */
1156 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1157 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1158 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1161 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1162 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1163 set_opt(sbi->s_mount_opt, ERRORS_RO);
1166 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1167 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1168 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1171 set_opt(sbi->s_mount_opt, NO_UID32);
1174 set_opt(sbi->s_mount_opt, DEBUG);
1177 set_opt(sbi->s_mount_opt, OLDALLOC);
1180 clear_opt(sbi->s_mount_opt, OLDALLOC);
1182 #ifdef CONFIG_EXT4_FS_XATTR
1183 case Opt_user_xattr:
1184 set_opt(sbi->s_mount_opt, XATTR_USER);
1186 case Opt_nouser_xattr:
1187 clear_opt(sbi->s_mount_opt, XATTR_USER);
1190 case Opt_user_xattr:
1191 case Opt_nouser_xattr:
1192 printk(KERN_ERR "EXT4 (no)user_xattr options "
1196 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1198 set_opt(sbi->s_mount_opt, POSIX_ACL);
1201 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1206 printk(KERN_ERR "EXT4 (no)acl options "
1210 case Opt_reservation:
1211 set_opt(sbi->s_mount_opt, RESERVATION);
1213 case Opt_noreservation:
1214 clear_opt(sbi->s_mount_opt, RESERVATION);
1216 case Opt_journal_update:
1218 /* Eventually we will want to be able to create
1219 a journal file here. For now, only allow the
1220 user to specify an existing inode to be the
1223 printk(KERN_ERR "EXT4-fs: cannot specify "
1224 "journal on remount\n");
1227 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1229 case Opt_journal_inum:
1231 printk(KERN_ERR "EXT4-fs: cannot specify "
1232 "journal on remount\n");
1235 if (match_int(&args[0], &option))
1239 case Opt_journal_dev:
1241 printk(KERN_ERR "EXT4-fs: cannot specify "
1242 "journal on remount\n");
1245 if (match_int(&args[0], &option))
1247 *journal_devnum = option;
1249 case Opt_journal_checksum:
1250 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1252 case Opt_journal_async_commit:
1253 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1254 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1257 set_opt(sbi->s_mount_opt, NOLOAD);
1260 if (match_int(&args[0], &option))
1265 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1266 sbi->s_commit_interval = HZ * option;
1268 case Opt_max_batch_time:
1269 if (match_int(&args[0], &option))
1274 option = EXT4_DEF_MAX_BATCH_TIME;
1275 sbi->s_max_batch_time = option;
1277 case Opt_min_batch_time:
1278 if (match_int(&args[0], &option))
1282 sbi->s_min_batch_time = option;
1284 case Opt_data_journal:
1285 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1287 case Opt_data_ordered:
1288 data_opt = EXT4_MOUNT_ORDERED_DATA;
1290 case Opt_data_writeback:
1291 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1294 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1297 "EXT4-fs: cannot change data "
1298 "mode on remount\n");
1302 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1303 sbi->s_mount_opt |= data_opt;
1306 case Opt_data_err_abort:
1307 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1309 case Opt_data_err_ignore:
1310 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1319 if ((sb_any_quota_enabled(sb) ||
1320 sb_any_quota_suspended(sb)) &&
1321 !sbi->s_qf_names[qtype]) {
1323 "EXT4-fs: Cannot change journaled "
1324 "quota options when quota turned on.\n");
1327 qname = match_strdup(&args[0]);
1330 "EXT4-fs: not enough memory for "
1331 "storing quotafile name.\n");
1334 if (sbi->s_qf_names[qtype] &&
1335 strcmp(sbi->s_qf_names[qtype], qname)) {
1337 "EXT4-fs: %s quota file already "
1338 "specified.\n", QTYPE2NAME(qtype));
1342 sbi->s_qf_names[qtype] = qname;
1343 if (strchr(sbi->s_qf_names[qtype], '/')) {
1345 "EXT4-fs: quotafile must be on "
1346 "filesystem root.\n");
1347 kfree(sbi->s_qf_names[qtype]);
1348 sbi->s_qf_names[qtype] = NULL;
1351 set_opt(sbi->s_mount_opt, QUOTA);
1353 case Opt_offusrjquota:
1356 case Opt_offgrpjquota:
1359 if ((sb_any_quota_enabled(sb) ||
1360 sb_any_quota_suspended(sb)) &&
1361 sbi->s_qf_names[qtype]) {
1362 printk(KERN_ERR "EXT4-fs: Cannot change "
1363 "journaled quota options when "
1364 "quota turned on.\n");
1368 * The space will be released later when all options
1369 * are confirmed to be correct
1371 sbi->s_qf_names[qtype] = NULL;
1373 case Opt_jqfmt_vfsold:
1374 qfmt = QFMT_VFS_OLD;
1376 case Opt_jqfmt_vfsv0:
1379 if ((sb_any_quota_enabled(sb) ||
1380 sb_any_quota_suspended(sb)) &&
1381 sbi->s_jquota_fmt != qfmt) {
1382 printk(KERN_ERR "EXT4-fs: Cannot change "
1383 "journaled quota options when "
1384 "quota turned on.\n");
1387 sbi->s_jquota_fmt = qfmt;
1391 set_opt(sbi->s_mount_opt, QUOTA);
1392 set_opt(sbi->s_mount_opt, USRQUOTA);
1395 set_opt(sbi->s_mount_opt, QUOTA);
1396 set_opt(sbi->s_mount_opt, GRPQUOTA);
1399 if (sb_any_quota_enabled(sb)) {
1400 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1401 "options when quota turned on.\n");
1404 clear_opt(sbi->s_mount_opt, QUOTA);
1405 clear_opt(sbi->s_mount_opt, USRQUOTA);
1406 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1413 "EXT4-fs: quota options not supported.\n");
1417 case Opt_offusrjquota:
1418 case Opt_offgrpjquota:
1419 case Opt_jqfmt_vfsold:
1420 case Opt_jqfmt_vfsv0:
1422 "EXT4-fs: journaled quota options not "
1429 set_opt(sbi->s_mount_opt, ABORT);
1432 if (match_int(&args[0], &option))
1435 set_opt(sbi->s_mount_opt, BARRIER);
1437 clear_opt(sbi->s_mount_opt, BARRIER);
1443 printk("EXT4-fs: resize option only available "
1447 if (match_int(&args[0], &option) != 0)
1449 *n_blocks_count = option;
1452 set_opt(sbi->s_mount_opt, NOBH);
1455 clear_opt(sbi->s_mount_opt, NOBH);
1458 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1459 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1460 ext4_warning(sb, __func__,
1461 "extents feature not enabled "
1462 "on this filesystem, use tune2fs");
1465 set_opt(sbi->s_mount_opt, EXTENTS);
1469 * When e2fsprogs support resizing an already existing
1470 * ext3 file system to greater than 2**32 we need to
1471 * add support to block allocator to handle growing
1472 * already existing block mapped inode so that blocks
1473 * allocated for them fall within 2**32
1475 last_block = ext4_blocks_count(sbi->s_es) - 1;
1476 if (last_block > 0xffffffffULL) {
1477 printk(KERN_ERR "EXT4-fs: Filesystem too "
1478 "large to mount with "
1479 "-o noextents options\n");
1482 clear_opt(sbi->s_mount_opt, EXTENTS);
1485 set_opt(sbi->s_mount_opt, I_VERSION);
1486 sb->s_flags |= MS_I_VERSION;
1488 case Opt_nodelalloc:
1489 clear_opt(sbi->s_mount_opt, DELALLOC);
1492 if (match_int(&args[0], &option))
1496 sbi->s_stripe = option;
1499 set_opt(sbi->s_mount_opt, DELALLOC);
1501 case Opt_inode_readahead_blks:
1502 if (match_int(&args[0], &option))
1504 if (option < 0 || option > (1 << 30))
1506 sbi->s_inode_readahead_blks = option;
1510 "EXT4-fs: Unrecognized mount option \"%s\" "
1511 "or missing value\n", p);
1516 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1517 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1518 sbi->s_qf_names[USRQUOTA])
1519 clear_opt(sbi->s_mount_opt, USRQUOTA);
1521 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1522 sbi->s_qf_names[GRPQUOTA])
1523 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1525 if ((sbi->s_qf_names[USRQUOTA] &&
1526 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1527 (sbi->s_qf_names[GRPQUOTA] &&
1528 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1529 printk(KERN_ERR "EXT4-fs: old and new quota "
1530 "format mixing.\n");
1534 if (!sbi->s_jquota_fmt) {
1535 printk(KERN_ERR "EXT4-fs: journaled quota format "
1536 "not specified.\n");
1540 if (sbi->s_jquota_fmt) {
1541 printk(KERN_ERR "EXT4-fs: journaled quota format "
1542 "specified with no journaling "
1551 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1554 struct ext4_sb_info *sbi = EXT4_SB(sb);
1557 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1558 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1559 "forcing read-only mode\n");
1564 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1565 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1566 "running e2fsck is recommended\n");
1567 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1569 "EXT4-fs warning: mounting fs with errors, "
1570 "running e2fsck is recommended\n");
1571 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1572 le16_to_cpu(es->s_mnt_count) >=
1573 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1575 "EXT4-fs warning: maximal mount count reached, "
1576 "running e2fsck is recommended\n");
1577 else if (le32_to_cpu(es->s_checkinterval) &&
1578 (le32_to_cpu(es->s_lastcheck) +
1579 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1581 "EXT4-fs warning: checktime reached, "
1582 "running e2fsck is recommended\n");
1583 if (!sbi->s_journal)
1584 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1585 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1586 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1587 le16_add_cpu(&es->s_mnt_count, 1);
1588 es->s_mtime = cpu_to_le32(get_seconds());
1589 ext4_update_dynamic_rev(sb);
1591 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1593 ext4_commit_super(sb, es, 1);
1594 if (test_opt(sb, DEBUG))
1595 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1596 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1598 sbi->s_groups_count,
1599 EXT4_BLOCKS_PER_GROUP(sb),
1600 EXT4_INODES_PER_GROUP(sb),
1603 if (EXT4_SB(sb)->s_journal) {
1604 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1605 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1606 "external", EXT4_SB(sb)->s_journal->j_devname);
1608 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1613 static int ext4_fill_flex_info(struct super_block *sb)
1615 struct ext4_sb_info *sbi = EXT4_SB(sb);
1616 struct ext4_group_desc *gdp = NULL;
1617 struct buffer_head *bh;
1618 ext4_group_t flex_group_count;
1619 ext4_group_t flex_group;
1620 int groups_per_flex = 0;
1623 if (!sbi->s_es->s_log_groups_per_flex) {
1624 sbi->s_log_groups_per_flex = 0;
1628 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1629 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1631 /* We allocate both existing and potentially added groups */
1632 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1633 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1634 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1635 sbi->s_flex_groups = kzalloc(flex_group_count *
1636 sizeof(struct flex_groups), GFP_KERNEL);
1637 if (sbi->s_flex_groups == NULL) {
1638 printk(KERN_ERR "EXT4-fs: not enough memory for "
1639 "%u flex groups\n", flex_group_count);
1643 for (i = 0; i < sbi->s_groups_count; i++) {
1644 gdp = ext4_get_group_desc(sb, i, &bh);
1646 flex_group = ext4_flex_group(sbi, i);
1647 sbi->s_flex_groups[flex_group].free_inodes +=
1648 ext4_free_inodes_count(sb, gdp);
1649 sbi->s_flex_groups[flex_group].free_blocks +=
1650 ext4_free_blks_count(sb, gdp);
1658 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1659 struct ext4_group_desc *gdp)
1663 if (sbi->s_es->s_feature_ro_compat &
1664 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1665 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1666 __le32 le_group = cpu_to_le32(block_group);
1668 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1669 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1670 crc = crc16(crc, (__u8 *)gdp, offset);
1671 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1672 /* for checksum of struct ext4_group_desc do the rest...*/
1673 if ((sbi->s_es->s_feature_incompat &
1674 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1675 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1676 crc = crc16(crc, (__u8 *)gdp + offset,
1677 le16_to_cpu(sbi->s_es->s_desc_size) -
1681 return cpu_to_le16(crc);
1684 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1685 struct ext4_group_desc *gdp)
1687 if ((sbi->s_es->s_feature_ro_compat &
1688 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1689 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1695 /* Called at mount-time, super-block is locked */
1696 static int ext4_check_descriptors(struct super_block *sb)
1698 struct ext4_sb_info *sbi = EXT4_SB(sb);
1699 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1700 ext4_fsblk_t last_block;
1701 ext4_fsblk_t block_bitmap;
1702 ext4_fsblk_t inode_bitmap;
1703 ext4_fsblk_t inode_table;
1704 int flexbg_flag = 0;
1707 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1710 ext4_debug("Checking group descriptors");
1712 for (i = 0; i < sbi->s_groups_count; i++) {
1713 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1715 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1716 last_block = ext4_blocks_count(sbi->s_es) - 1;
1718 last_block = first_block +
1719 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1721 block_bitmap = ext4_block_bitmap(sb, gdp);
1722 if (block_bitmap < first_block || block_bitmap > last_block) {
1723 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1724 "Block bitmap for group %u not in group "
1725 "(block %llu)!\n", i, block_bitmap);
1728 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1729 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1730 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1731 "Inode bitmap for group %u not in group "
1732 "(block %llu)!\n", i, inode_bitmap);
1735 inode_table = ext4_inode_table(sb, gdp);
1736 if (inode_table < first_block ||
1737 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1738 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1739 "Inode table for group %u not in group "
1740 "(block %llu)!\n", i, inode_table);
1743 spin_lock(sb_bgl_lock(sbi, i));
1744 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1745 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1746 "Checksum for group %u failed (%u!=%u)\n",
1747 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1748 gdp)), le16_to_cpu(gdp->bg_checksum));
1749 if (!(sb->s_flags & MS_RDONLY)) {
1750 spin_unlock(sb_bgl_lock(sbi, i));
1754 spin_unlock(sb_bgl_lock(sbi, i));
1756 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1759 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1760 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1764 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1765 * the superblock) which were deleted from all directories, but held open by
1766 * a process at the time of a crash. We walk the list and try to delete these
1767 * inodes at recovery time (only with a read-write filesystem).
1769 * In order to keep the orphan inode chain consistent during traversal (in
1770 * case of crash during recovery), we link each inode into the superblock
1771 * orphan list_head and handle it the same way as an inode deletion during
1772 * normal operation (which journals the operations for us).
1774 * We only do an iget() and an iput() on each inode, which is very safe if we
1775 * accidentally point at an in-use or already deleted inode. The worst that
1776 * can happen in this case is that we get a "bit already cleared" message from
1777 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1778 * e2fsck was run on this filesystem, and it must have already done the orphan
1779 * inode cleanup for us, so we can safely abort without any further action.
1781 static void ext4_orphan_cleanup(struct super_block *sb,
1782 struct ext4_super_block *es)
1784 unsigned int s_flags = sb->s_flags;
1785 int nr_orphans = 0, nr_truncates = 0;
1789 if (!es->s_last_orphan) {
1790 jbd_debug(4, "no orphan inodes to clean up\n");
1794 if (bdev_read_only(sb->s_bdev)) {
1795 printk(KERN_ERR "EXT4-fs: write access "
1796 "unavailable, skipping orphan cleanup.\n");
1800 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1801 if (es->s_last_orphan)
1802 jbd_debug(1, "Errors on filesystem, "
1803 "clearing orphan list.\n");
1804 es->s_last_orphan = 0;
1805 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1809 if (s_flags & MS_RDONLY) {
1810 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1812 sb->s_flags &= ~MS_RDONLY;
1815 /* Needed for iput() to work correctly and not trash data */
1816 sb->s_flags |= MS_ACTIVE;
1817 /* Turn on quotas so that they are updated correctly */
1818 for (i = 0; i < MAXQUOTAS; i++) {
1819 if (EXT4_SB(sb)->s_qf_names[i]) {
1820 int ret = ext4_quota_on_mount(sb, i);
1823 "EXT4-fs: Cannot turn on journaled "
1824 "quota: error %d\n", ret);
1829 while (es->s_last_orphan) {
1830 struct inode *inode;
1832 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1833 if (IS_ERR(inode)) {
1834 es->s_last_orphan = 0;
1838 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1840 if (inode->i_nlink) {
1842 "%s: truncating inode %lu to %lld bytes\n",
1843 __func__, inode->i_ino, inode->i_size);
1844 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1845 inode->i_ino, inode->i_size);
1846 ext4_truncate(inode);
1850 "%s: deleting unreferenced inode %lu\n",
1851 __func__, inode->i_ino);
1852 jbd_debug(2, "deleting unreferenced inode %lu\n",
1856 iput(inode); /* The delete magic happens here! */
1859 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1862 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1863 sb->s_id, PLURAL(nr_orphans));
1865 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1866 sb->s_id, PLURAL(nr_truncates));
1868 /* Turn quotas off */
1869 for (i = 0; i < MAXQUOTAS; i++) {
1870 if (sb_dqopt(sb)->files[i])
1871 vfs_quota_off(sb, i, 0);
1874 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1877 * Maximal extent format file size.
1878 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1879 * extent format containers, within a sector_t, and within i_blocks
1880 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1881 * so that won't be a limiting factor.
1883 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1885 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1888 loff_t upper_limit = MAX_LFS_FILESIZE;
1890 /* small i_blocks in vfs inode? */
1891 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1893 * CONFIG_LBD is not enabled implies the inode
1894 * i_block represent total blocks in 512 bytes
1895 * 32 == size of vfs inode i_blocks * 8
1897 upper_limit = (1LL << 32) - 1;
1899 /* total blocks in file system block size */
1900 upper_limit >>= (blkbits - 9);
1901 upper_limit <<= blkbits;
1904 /* 32-bit extent-start container, ee_block */
1909 /* Sanity check against vm- & vfs- imposed limits */
1910 if (res > upper_limit)
1917 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
1918 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1919 * We need to be 1 filesystem block less than the 2^48 sector limit.
1921 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1923 loff_t res = EXT4_NDIR_BLOCKS;
1926 /* This is calculated to be the largest file size for a
1927 * dense, bitmapped file such that the total number of
1928 * sectors in the file, including data and all indirect blocks,
1929 * does not exceed 2^48 -1
1930 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1931 * total number of 512 bytes blocks of the file
1934 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1936 * !has_huge_files or CONFIG_LBD is not enabled
1937 * implies the inode i_block represent total blocks in
1938 * 512 bytes 32 == size of vfs inode i_blocks * 8
1940 upper_limit = (1LL << 32) - 1;
1942 /* total blocks in file system block size */
1943 upper_limit >>= (bits - 9);
1947 * We use 48 bit ext4_inode i_blocks
1948 * With EXT4_HUGE_FILE_FL set the i_blocks
1949 * represent total number of blocks in
1950 * file system block size
1952 upper_limit = (1LL << 48) - 1;
1956 /* indirect blocks */
1958 /* double indirect blocks */
1959 meta_blocks += 1 + (1LL << (bits-2));
1960 /* tripple indirect blocks */
1961 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1963 upper_limit -= meta_blocks;
1964 upper_limit <<= bits;
1966 res += 1LL << (bits-2);
1967 res += 1LL << (2*(bits-2));
1968 res += 1LL << (3*(bits-2));
1970 if (res > upper_limit)
1973 if (res > MAX_LFS_FILESIZE)
1974 res = MAX_LFS_FILESIZE;
1979 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1980 ext4_fsblk_t logical_sb_block, int nr)
1982 struct ext4_sb_info *sbi = EXT4_SB(sb);
1983 ext4_group_t bg, first_meta_bg;
1986 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1988 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1990 return logical_sb_block + nr + 1;
1991 bg = sbi->s_desc_per_block * nr;
1992 if (ext4_bg_has_super(sb, bg))
1994 return (has_super + ext4_group_first_block_no(sb, bg));
1998 * ext4_get_stripe_size: Get the stripe size.
1999 * @sbi: In memory super block info
2001 * If we have specified it via mount option, then
2002 * use the mount option value. If the value specified at mount time is
2003 * greater than the blocks per group use the super block value.
2004 * If the super block value is greater than blocks per group return 0.
2005 * Allocator needs it be less than blocks per group.
2008 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2010 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2011 unsigned long stripe_width =
2012 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2014 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2015 return sbi->s_stripe;
2017 if (stripe_width <= sbi->s_blocks_per_group)
2018 return stripe_width;
2020 if (stride <= sbi->s_blocks_per_group)
2026 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
2027 __releases(kernel_lock)
2028 __acquires(kernel_lock)
2031 struct buffer_head *bh;
2032 struct ext4_super_block *es = NULL;
2033 struct ext4_sb_info *sbi;
2035 ext4_fsblk_t sb_block = get_sb_block(&data);
2036 ext4_fsblk_t logical_sb_block;
2037 unsigned long offset = 0;
2038 unsigned int journal_inum = 0;
2039 unsigned long journal_devnum = 0;
2040 unsigned long def_mount_opts;
2048 int needs_recovery, has_huge_files;
2053 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2056 sb->s_fs_info = sbi;
2057 sbi->s_mount_opt = 0;
2058 sbi->s_resuid = EXT4_DEF_RESUID;
2059 sbi->s_resgid = EXT4_DEF_RESGID;
2060 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2061 sbi->s_sb_block = sb_block;
2065 /* Cleanup superblock name */
2066 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2069 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2071 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
2076 * The ext4 superblock will not be buffer aligned for other than 1kB
2077 * block sizes. We need to calculate the offset from buffer start.
2079 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
2080 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2081 offset = do_div(logical_sb_block, blocksize);
2083 logical_sb_block = sb_block;
2086 if (!(bh = sb_bread(sb, logical_sb_block))) {
2087 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
2091 * Note: s_es must be initialized as soon as possible because
2092 * some ext4 macro-instructions depend on its value
2094 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2096 sb->s_magic = le16_to_cpu(es->s_magic);
2097 if (sb->s_magic != EXT4_SUPER_MAGIC)
2100 /* Set defaults before we parse the mount options */
2101 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2102 if (def_mount_opts & EXT4_DEFM_DEBUG)
2103 set_opt(sbi->s_mount_opt, DEBUG);
2104 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2105 set_opt(sbi->s_mount_opt, GRPID);
2106 if (def_mount_opts & EXT4_DEFM_UID16)
2107 set_opt(sbi->s_mount_opt, NO_UID32);
2108 #ifdef CONFIG_EXT4_FS_XATTR
2109 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2110 set_opt(sbi->s_mount_opt, XATTR_USER);
2112 #ifdef CONFIG_EXT4_FS_POSIX_ACL
2113 if (def_mount_opts & EXT4_DEFM_ACL)
2114 set_opt(sbi->s_mount_opt, POSIX_ACL);
2116 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2117 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2118 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2119 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2120 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2121 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2123 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2124 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2125 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2126 set_opt(sbi->s_mount_opt, ERRORS_CONT);
2128 set_opt(sbi->s_mount_opt, ERRORS_RO);
2130 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2131 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2132 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2133 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2134 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2136 set_opt(sbi->s_mount_opt, RESERVATION);
2137 set_opt(sbi->s_mount_opt, BARRIER);
2140 * turn on extents feature by default in ext4 filesystem
2141 * only if feature flag already set by mkfs or tune2fs.
2142 * Use -o noextents to turn it off
2144 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2145 set_opt(sbi->s_mount_opt, EXTENTS);
2147 ext4_warning(sb, __func__,
2148 "extents feature not enabled on this filesystem, "
2152 * enable delayed allocation by default
2153 * Use -o nodelalloc to turn it off
2155 set_opt(sbi->s_mount_opt, DELALLOC);
2158 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2162 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2163 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2165 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2166 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2167 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2168 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2170 "EXT4-fs warning: feature flags set on rev 0 fs, "
2171 "running e2fsck is recommended\n");
2174 * Check feature flags regardless of the revision level, since we
2175 * previously didn't change the revision level when setting the flags,
2176 * so there is a chance incompat flags are set on a rev 0 filesystem.
2178 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2180 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2181 "unsupported optional features (%x).\n", sb->s_id,
2182 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2183 ~EXT4_FEATURE_INCOMPAT_SUPP));
2186 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2187 if (!(sb->s_flags & MS_RDONLY) && features) {
2188 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2189 "unsupported optional features (%x).\n", sb->s_id,
2190 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2191 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2194 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2195 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2196 if (has_huge_files) {
2198 * Large file size enabled file system can only be
2199 * mount if kernel is build with CONFIG_LBD
2201 if (sizeof(root->i_blocks) < sizeof(u64) &&
2202 !(sb->s_flags & MS_RDONLY)) {
2203 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2204 "files cannot be mounted read-write "
2205 "without CONFIG_LBD.\n", sb->s_id);
2209 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2211 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2212 blocksize > EXT4_MAX_BLOCK_SIZE) {
2214 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2215 blocksize, sb->s_id);
2219 if (sb->s_blocksize != blocksize) {
2221 /* Validate the filesystem blocksize */
2222 if (!sb_set_blocksize(sb, blocksize)) {
2223 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2229 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2230 offset = do_div(logical_sb_block, blocksize);
2231 bh = sb_bread(sb, logical_sb_block);
2234 "EXT4-fs: Can't read superblock on 2nd try.\n");
2237 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2239 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2241 "EXT4-fs: Magic mismatch, very weird !\n");
2246 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2248 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2250 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2251 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2252 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2254 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2255 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2256 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2257 (!is_power_of_2(sbi->s_inode_size)) ||
2258 (sbi->s_inode_size > blocksize)) {
2260 "EXT4-fs: unsupported inode size: %d\n",
2264 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2265 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2267 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2268 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2269 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2270 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2271 !is_power_of_2(sbi->s_desc_size)) {
2273 "EXT4-fs: unsupported descriptor size %lu\n",
2278 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2279 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2280 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2281 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2283 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2284 if (sbi->s_inodes_per_block == 0)
2286 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2287 sbi->s_inodes_per_block;
2288 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2290 sbi->s_mount_state = le16_to_cpu(es->s_state);
2291 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2292 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2293 for (i = 0; i < 4; i++)
2294 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2295 sbi->s_def_hash_version = es->s_def_hash_version;
2296 i = le32_to_cpu(es->s_flags);
2297 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2298 sbi->s_hash_unsigned = 3;
2299 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2300 #ifdef __CHAR_UNSIGNED__
2301 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2302 sbi->s_hash_unsigned = 3;
2304 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2309 if (sbi->s_blocks_per_group > blocksize * 8) {
2311 "EXT4-fs: #blocks per group too big: %lu\n",
2312 sbi->s_blocks_per_group);
2315 if (sbi->s_inodes_per_group > blocksize * 8) {
2317 "EXT4-fs: #inodes per group too big: %lu\n",
2318 sbi->s_inodes_per_group);
2322 if (ext4_blocks_count(es) >
2323 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2324 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2325 " too large to mount safely\n", sb->s_id);
2326 if (sizeof(sector_t) < 8)
2327 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2332 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2335 /* ensure blocks_count calculation below doesn't sign-extend */
2336 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2337 le32_to_cpu(es->s_first_data_block) + 1) {
2338 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2339 "first data block %u, blocks per group %lu\n",
2340 ext4_blocks_count(es),
2341 le32_to_cpu(es->s_first_data_block),
2342 EXT4_BLOCKS_PER_GROUP(sb));
2345 blocks_count = (ext4_blocks_count(es) -
2346 le32_to_cpu(es->s_first_data_block) +
2347 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2348 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2349 sbi->s_groups_count = blocks_count;
2350 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2351 EXT4_DESC_PER_BLOCK(sb);
2352 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2354 if (sbi->s_group_desc == NULL) {
2355 printk(KERN_ERR "EXT4-fs: not enough memory\n");
2359 #ifdef CONFIG_PROC_FS
2361 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2364 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2366 &sbi->s_inode_readahead_blks);
2369 bgl_lock_init(&sbi->s_blockgroup_lock);
2371 for (i = 0; i < db_count; i++) {
2372 block = descriptor_loc(sb, logical_sb_block, i);
2373 sbi->s_group_desc[i] = sb_bread(sb, block);
2374 if (!sbi->s_group_desc[i]) {
2375 printk(KERN_ERR "EXT4-fs: "
2376 "can't read group descriptor %d\n", i);
2381 if (!ext4_check_descriptors(sb)) {
2382 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2385 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2386 if (!ext4_fill_flex_info(sb)) {
2388 "EXT4-fs: unable to initialize "
2389 "flex_bg meta info!\n");
2393 sbi->s_gdb_count = db_count;
2394 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2395 spin_lock_init(&sbi->s_next_gen_lock);
2397 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2398 ext4_count_free_blocks(sb));
2400 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2401 ext4_count_free_inodes(sb));
2404 err = percpu_counter_init(&sbi->s_dirs_counter,
2405 ext4_count_dirs(sb));
2408 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2411 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2415 sbi->s_stripe = ext4_get_stripe_size(sbi);
2418 * set up enough so that it can read an inode
2420 sb->s_op = &ext4_sops;
2421 sb->s_export_op = &ext4_export_ops;
2422 sb->s_xattr = ext4_xattr_handlers;
2424 sb->s_qcop = &ext4_qctl_operations;
2425 sb->dq_op = &ext4_quota_operations;
2427 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2431 needs_recovery = (es->s_last_orphan != 0 ||
2432 EXT4_HAS_INCOMPAT_FEATURE(sb,
2433 EXT4_FEATURE_INCOMPAT_RECOVER));
2436 * The first inode we look at is the journal inode. Don't try
2437 * root first: it may be modified in the journal!
2439 if (!test_opt(sb, NOLOAD) &&
2440 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2441 if (ext4_load_journal(sb, es, journal_devnum))
2443 if (!(sb->s_flags & MS_RDONLY) &&
2444 EXT4_SB(sb)->s_journal->j_failed_commit) {
2445 printk(KERN_CRIT "EXT4-fs error (device %s): "
2446 "ext4_fill_super: Journal transaction "
2447 "%u is corrupt\n", sb->s_id,
2448 EXT4_SB(sb)->s_journal->j_failed_commit);
2449 if (test_opt(sb, ERRORS_RO)) {
2451 "Mounting filesystem read-only\n");
2452 sb->s_flags |= MS_RDONLY;
2453 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2454 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2456 if (test_opt(sb, ERRORS_PANIC)) {
2457 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2458 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2459 ext4_commit_super(sb, es, 1);
2463 } else if (journal_inum) {
2464 if (ext4_create_journal(sb, es, journal_inum))
2466 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2467 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2468 printk(KERN_ERR "EXT4-fs: required journal recovery "
2469 "suppressed and not mounted read-only\n");
2472 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2473 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2474 sbi->s_journal = NULL;
2479 if (ext4_blocks_count(es) > 0xffffffffULL &&
2480 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2481 JBD2_FEATURE_INCOMPAT_64BIT)) {
2482 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2486 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2487 jbd2_journal_set_features(sbi->s_journal,
2488 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2489 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2490 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2491 jbd2_journal_set_features(sbi->s_journal,
2492 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2493 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2494 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2496 jbd2_journal_clear_features(sbi->s_journal,
2497 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2498 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2501 /* We have now updated the journal if required, so we can
2502 * validate the data journaling mode. */
2503 switch (test_opt(sb, DATA_FLAGS)) {
2505 /* No mode set, assume a default based on the journal
2506 * capabilities: ORDERED_DATA if the journal can
2507 * cope, else JOURNAL_DATA
2509 if (jbd2_journal_check_available_features
2510 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2511 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2513 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2516 case EXT4_MOUNT_ORDERED_DATA:
2517 case EXT4_MOUNT_WRITEBACK_DATA:
2518 if (!jbd2_journal_check_available_features
2519 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2520 printk(KERN_ERR "EXT4-fs: Journal does not support "
2521 "requested data journaling mode\n");
2530 if (test_opt(sb, NOBH)) {
2531 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2532 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2533 "its supported only with writeback mode\n");
2534 clear_opt(sbi->s_mount_opt, NOBH);
2538 * The jbd2_journal_load will have done any necessary log recovery,
2539 * so we can safely mount the rest of the filesystem now.
2542 root = ext4_iget(sb, EXT4_ROOT_INO);
2544 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2545 ret = PTR_ERR(root);
2548 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2550 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2553 sb->s_root = d_alloc_root(root);
2555 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2561 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2563 /* determine the minimum size of new large inodes, if present */
2564 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2565 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2566 EXT4_GOOD_OLD_INODE_SIZE;
2567 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2568 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2569 if (sbi->s_want_extra_isize <
2570 le16_to_cpu(es->s_want_extra_isize))
2571 sbi->s_want_extra_isize =
2572 le16_to_cpu(es->s_want_extra_isize);
2573 if (sbi->s_want_extra_isize <
2574 le16_to_cpu(es->s_min_extra_isize))
2575 sbi->s_want_extra_isize =
2576 le16_to_cpu(es->s_min_extra_isize);
2579 /* Check if enough inode space is available */
2580 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2581 sbi->s_inode_size) {
2582 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2583 EXT4_GOOD_OLD_INODE_SIZE;
2584 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2588 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2589 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2590 "requested data journaling mode\n");
2591 clear_opt(sbi->s_mount_opt, DELALLOC);
2592 } else if (test_opt(sb, DELALLOC))
2593 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2596 err = ext4_mb_init(sb, needs_recovery);
2598 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2604 * akpm: core read_super() calls in here with the superblock locked.
2605 * That deadlocks, because orphan cleanup needs to lock the superblock
2606 * in numerous places. Here we just pop the lock - it's relatively
2607 * harmless, because we are now ready to accept write_super() requests,
2608 * and aviro says that's the only reason for hanging onto the
2611 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2612 ext4_orphan_cleanup(sb, es);
2613 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2614 if (needs_recovery) {
2615 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2616 ext4_mark_recovery_complete(sb, es);
2618 if (EXT4_SB(sb)->s_journal) {
2619 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2620 descr = " journalled data mode";
2621 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2622 descr = " ordered data mode";
2624 descr = " writeback data mode";
2626 descr = "out journal";
2628 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2636 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2641 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2642 if (sbi->s_journal) {
2643 jbd2_journal_destroy(sbi->s_journal);
2644 sbi->s_journal = NULL;
2647 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2648 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2649 percpu_counter_destroy(&sbi->s_dirs_counter);
2650 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2652 for (i = 0; i < db_count; i++)
2653 brelse(sbi->s_group_desc[i]);
2654 kfree(sbi->s_group_desc);
2657 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
2658 remove_proc_entry(sb->s_id, ext4_proc_root);
2661 for (i = 0; i < MAXQUOTAS; i++)
2662 kfree(sbi->s_qf_names[i]);
2664 ext4_blkdev_remove(sbi);
2667 sb->s_fs_info = NULL;
2674 * Setup any per-fs journal parameters now. We'll do this both on
2675 * initial mount, once the journal has been initialised but before we've
2676 * done any recovery; and again on any subsequent remount.
2678 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2680 struct ext4_sb_info *sbi = EXT4_SB(sb);
2682 journal->j_commit_interval = sbi->s_commit_interval;
2683 journal->j_min_batch_time = sbi->s_min_batch_time;
2684 journal->j_max_batch_time = sbi->s_max_batch_time;
2686 spin_lock(&journal->j_state_lock);
2687 if (test_opt(sb, BARRIER))
2688 journal->j_flags |= JBD2_BARRIER;
2690 journal->j_flags &= ~JBD2_BARRIER;
2691 if (test_opt(sb, DATA_ERR_ABORT))
2692 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2694 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2695 spin_unlock(&journal->j_state_lock);
2698 static journal_t *ext4_get_journal(struct super_block *sb,
2699 unsigned int journal_inum)
2701 struct inode *journal_inode;
2704 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2706 /* First, test for the existence of a valid inode on disk. Bad
2707 * things happen if we iget() an unused inode, as the subsequent
2708 * iput() will try to delete it. */
2710 journal_inode = ext4_iget(sb, journal_inum);
2711 if (IS_ERR(journal_inode)) {
2712 printk(KERN_ERR "EXT4-fs: no journal found.\n");
2715 if (!journal_inode->i_nlink) {
2716 make_bad_inode(journal_inode);
2717 iput(journal_inode);
2718 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2722 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2723 journal_inode, journal_inode->i_size);
2724 if (!S_ISREG(journal_inode->i_mode)) {
2725 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2726 iput(journal_inode);
2730 journal = jbd2_journal_init_inode(journal_inode);
2732 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2733 iput(journal_inode);
2736 journal->j_private = sb;
2737 ext4_init_journal_params(sb, journal);
2741 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2744 struct buffer_head *bh;
2748 int hblock, blocksize;
2749 ext4_fsblk_t sb_block;
2750 unsigned long offset;
2751 struct ext4_super_block *es;
2752 struct block_device *bdev;
2754 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2756 bdev = ext4_blkdev_get(j_dev);
2760 if (bd_claim(bdev, sb)) {
2762 "EXT4: failed to claim external journal device.\n");
2763 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2767 blocksize = sb->s_blocksize;
2768 hblock = bdev_hardsect_size(bdev);
2769 if (blocksize < hblock) {
2771 "EXT4-fs: blocksize too small for journal device.\n");
2775 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2776 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2777 set_blocksize(bdev, blocksize);
2778 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2779 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2780 "external journal\n");
2784 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2785 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2786 !(le32_to_cpu(es->s_feature_incompat) &
2787 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2788 printk(KERN_ERR "EXT4-fs: external journal has "
2789 "bad superblock\n");
2794 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2795 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2800 len = ext4_blocks_count(es);
2801 start = sb_block + 1;
2802 brelse(bh); /* we're done with the superblock */
2804 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2805 start, len, blocksize);
2807 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2810 journal->j_private = sb;
2811 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2812 wait_on_buffer(journal->j_sb_buffer);
2813 if (!buffer_uptodate(journal->j_sb_buffer)) {
2814 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2817 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2818 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2819 "user (unsupported) - %d\n",
2820 be32_to_cpu(journal->j_superblock->s_nr_users));
2823 EXT4_SB(sb)->journal_bdev = bdev;
2824 ext4_init_journal_params(sb, journal);
2827 jbd2_journal_destroy(journal);
2829 ext4_blkdev_put(bdev);
2833 static int ext4_load_journal(struct super_block *sb,
2834 struct ext4_super_block *es,
2835 unsigned long journal_devnum)
2838 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2841 int really_read_only;
2843 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2845 if (journal_devnum &&
2846 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2847 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2848 "numbers have changed\n");
2849 journal_dev = new_decode_dev(journal_devnum);
2851 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2853 really_read_only = bdev_read_only(sb->s_bdev);
2856 * Are we loading a blank journal or performing recovery after a
2857 * crash? For recovery, we need to check in advance whether we
2858 * can get read-write access to the device.
2861 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2862 if (sb->s_flags & MS_RDONLY) {
2863 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2864 "required on readonly filesystem.\n");
2865 if (really_read_only) {
2866 printk(KERN_ERR "EXT4-fs: write access "
2867 "unavailable, cannot proceed.\n");
2870 printk(KERN_INFO "EXT4-fs: write access will "
2871 "be enabled during recovery.\n");
2875 if (journal_inum && journal_dev) {
2876 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2877 "and inode journals!\n");
2882 if (!(journal = ext4_get_journal(sb, journal_inum)))
2885 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2889 if (journal->j_flags & JBD2_BARRIER)
2890 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2892 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2894 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2895 err = jbd2_journal_update_format(journal);
2897 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2898 jbd2_journal_destroy(journal);
2903 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2904 err = jbd2_journal_wipe(journal, !really_read_only);
2906 err = jbd2_journal_load(journal);
2909 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2910 jbd2_journal_destroy(journal);
2914 EXT4_SB(sb)->s_journal = journal;
2915 ext4_clear_journal_err(sb, es);
2917 if (journal_devnum &&
2918 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2919 es->s_journal_dev = cpu_to_le32(journal_devnum);
2922 /* Make sure we flush the recovery flag to disk. */
2923 ext4_commit_super(sb, es, 1);
2929 static int ext4_create_journal(struct super_block *sb,
2930 struct ext4_super_block *es,
2931 unsigned int journal_inum)
2936 if (sb->s_flags & MS_RDONLY) {
2937 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2938 "create journal.\n");
2942 journal = ext4_get_journal(sb, journal_inum);
2946 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2949 err = jbd2_journal_create(journal);
2951 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2952 jbd2_journal_destroy(journal);
2956 EXT4_SB(sb)->s_journal = journal;
2958 ext4_update_dynamic_rev(sb);
2959 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2960 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2962 es->s_journal_inum = cpu_to_le32(journal_inum);
2965 /* Make sure we flush the recovery flag to disk. */
2966 ext4_commit_super(sb, es, 1);
2971 static void ext4_commit_super(struct super_block *sb,
2972 struct ext4_super_block *es, int sync)
2974 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2978 if (buffer_write_io_error(sbh)) {
2980 * Oh, dear. A previous attempt to write the
2981 * superblock failed. This could happen because the
2982 * USB device was yanked out. Or it could happen to
2983 * be a transient write error and maybe the block will
2984 * be remapped. Nothing we can do but to retry the
2985 * write and hope for the best.
2987 printk(KERN_ERR "ext4: previous I/O error to "
2988 "superblock detected for %s.\n", sb->s_id);
2989 clear_buffer_write_io_error(sbh);
2990 set_buffer_uptodate(sbh);
2992 es->s_wtime = cpu_to_le32(get_seconds());
2993 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
2994 &EXT4_SB(sb)->s_freeblocks_counter));
2995 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
2996 &EXT4_SB(sb)->s_freeinodes_counter));
2998 BUFFER_TRACE(sbh, "marking dirty");
2999 mark_buffer_dirty(sbh);
3001 sync_dirty_buffer(sbh);
3002 if (buffer_write_io_error(sbh)) {
3003 printk(KERN_ERR "ext4: I/O error while writing "
3004 "superblock for %s.\n", sb->s_id);
3005 clear_buffer_write_io_error(sbh);
3006 set_buffer_uptodate(sbh);
3013 * Have we just finished recovery? If so, and if we are mounting (or
3014 * remounting) the filesystem readonly, then we will end up with a
3015 * consistent fs on disk. Record that fact.
3017 static void ext4_mark_recovery_complete(struct super_block *sb,
3018 struct ext4_super_block *es)
3020 journal_t *journal = EXT4_SB(sb)->s_journal;
3022 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3023 BUG_ON(journal != NULL);
3026 jbd2_journal_lock_updates(journal);
3027 if (jbd2_journal_flush(journal) < 0)
3031 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3032 sb->s_flags & MS_RDONLY) {
3033 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3035 ext4_commit_super(sb, es, 1);
3040 jbd2_journal_unlock_updates(journal);
3044 * If we are mounting (or read-write remounting) a filesystem whose journal
3045 * has recorded an error from a previous lifetime, move that error to the
3046 * main filesystem now.
3048 static void ext4_clear_journal_err(struct super_block *sb,
3049 struct ext4_super_block *es)
3055 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3057 journal = EXT4_SB(sb)->s_journal;
3060 * Now check for any error status which may have been recorded in the
3061 * journal by a prior ext4_error() or ext4_abort()
3064 j_errno = jbd2_journal_errno(journal);
3068 errstr = ext4_decode_error(sb, j_errno, nbuf);
3069 ext4_warning(sb, __func__, "Filesystem error recorded "
3070 "from previous mount: %s", errstr);
3071 ext4_warning(sb, __func__, "Marking fs in need of "
3072 "filesystem check.");
3074 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3075 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3076 ext4_commit_super(sb, es, 1);
3078 jbd2_journal_clear_err(journal);
3083 * Force the running and committing transactions to commit,
3084 * and wait on the commit.
3086 int ext4_force_commit(struct super_block *sb)
3091 if (sb->s_flags & MS_RDONLY)
3094 journal = EXT4_SB(sb)->s_journal;
3097 ret = ext4_journal_force_commit(journal);
3104 * Ext4 always journals updates to the superblock itself, so we don't
3105 * have to propagate any other updates to the superblock on disk at this
3106 * point. (We can probably nuke this function altogether, and remove
3107 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
3109 static void ext4_write_super(struct super_block *sb)
3111 if (EXT4_SB(sb)->s_journal) {
3112 if (mutex_trylock(&sb->s_lock) != 0)
3116 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3120 static int ext4_sync_fs(struct super_block *sb, int wait)
3124 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
3126 if (EXT4_SB(sb)->s_journal) {
3128 ret = ext4_force_commit(sb);
3130 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3132 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3138 * LVM calls this function before a (read-only) snapshot is created. This
3139 * gives us a chance to flush the journal completely and mark the fs clean.
3141 static void ext4_write_super_lockfs(struct super_block *sb)
3145 if (!(sb->s_flags & MS_RDONLY)) {
3146 journal_t *journal = EXT4_SB(sb)->s_journal;
3149 /* Now we set up the journal barrier. */
3150 jbd2_journal_lock_updates(journal);
3153 * We don't want to clear needs_recovery flag when we
3154 * failed to flush the journal.
3156 if (jbd2_journal_flush(journal) < 0)
3160 /* Journal blocked and flushed, clear needs_recovery flag. */
3161 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3162 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3167 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3168 * flag here, even though the filesystem is not technically dirty yet.
3170 static void ext4_unlockfs(struct super_block *sb)
3172 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
3174 /* Reser the needs_recovery flag before the fs is unlocked. */
3175 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3176 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3178 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3182 static int ext4_remount(struct super_block *sb, int *flags, char *data)
3184 struct ext4_super_block *es;
3185 struct ext4_sb_info *sbi = EXT4_SB(sb);
3186 ext4_fsblk_t n_blocks_count = 0;
3187 unsigned long old_sb_flags;
3188 struct ext4_mount_options old_opts;
3195 /* Store the original options */
3196 old_sb_flags = sb->s_flags;
3197 old_opts.s_mount_opt = sbi->s_mount_opt;
3198 old_opts.s_resuid = sbi->s_resuid;
3199 old_opts.s_resgid = sbi->s_resgid;
3200 old_opts.s_commit_interval = sbi->s_commit_interval;
3201 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3202 old_opts.s_max_batch_time = sbi->s_max_batch_time;
3204 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3205 for (i = 0; i < MAXQUOTAS; i++)
3206 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3210 * Allow the "check" option to be passed as a remount option.
3212 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3217 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
3218 ext4_abort(sb, __func__, "Abort forced by user");
3220 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3221 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3226 ext4_init_journal_params(sb, sbi->s_journal);
3228 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3229 n_blocks_count > ext4_blocks_count(es)) {
3230 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3235 if (*flags & MS_RDONLY) {
3237 * First of all, the unconditional stuff we have to do
3238 * to disable replay of the journal when we next remount
3240 sb->s_flags |= MS_RDONLY;
3243 * OK, test if we are remounting a valid rw partition
3244 * readonly, and if so set the rdonly flag and then
3245 * mark the partition as valid again.
3247 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3248 (sbi->s_mount_state & EXT4_VALID_FS))
3249 es->s_state = cpu_to_le16(sbi->s_mount_state);
3252 * We have to unlock super so that we can wait for
3255 if (sbi->s_journal) {
3257 ext4_mark_recovery_complete(sb, es);
3262 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3263 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3264 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3265 "remount RDWR because of unsupported "
3266 "optional features (%x).\n", sb->s_id,
3267 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3268 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3274 * Make sure the group descriptor checksums
3275 * are sane. If they aren't, refuse to
3278 for (g = 0; g < sbi->s_groups_count; g++) {
3279 struct ext4_group_desc *gdp =
3280 ext4_get_group_desc(sb, g, NULL);
3282 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3284 "EXT4-fs: ext4_remount: "
3285 "Checksum for group %u failed (%u!=%u)\n",
3286 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3287 le16_to_cpu(gdp->bg_checksum));
3294 * If we have an unprocessed orphan list hanging
3295 * around from a previously readonly bdev mount,
3296 * require a full umount/remount for now.
3298 if (es->s_last_orphan) {
3299 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3300 "remount RDWR because of unprocessed "
3301 "orphan inode list. Please "
3302 "umount/remount instead.\n",
3309 * Mounting a RDONLY partition read-write, so reread
3310 * and store the current valid flag. (It may have
3311 * been changed by e2fsck since we originally mounted
3315 ext4_clear_journal_err(sb, es);
3316 sbi->s_mount_state = le16_to_cpu(es->s_state);
3317 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3319 if (!ext4_setup_super(sb, es, 0))
3320 sb->s_flags &= ~MS_RDONLY;
3323 if (sbi->s_journal == NULL)
3324 ext4_commit_super(sb, es, 1);
3327 /* Release old quota file names */
3328 for (i = 0; i < MAXQUOTAS; i++)
3329 if (old_opts.s_qf_names[i] &&
3330 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3331 kfree(old_opts.s_qf_names[i]);
3335 sb->s_flags = old_sb_flags;
3336 sbi->s_mount_opt = old_opts.s_mount_opt;
3337 sbi->s_resuid = old_opts.s_resuid;
3338 sbi->s_resgid = old_opts.s_resgid;
3339 sbi->s_commit_interval = old_opts.s_commit_interval;
3340 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3341 sbi->s_max_batch_time = old_opts.s_max_batch_time;
3343 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3344 for (i = 0; i < MAXQUOTAS; i++) {
3345 if (sbi->s_qf_names[i] &&
3346 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3347 kfree(sbi->s_qf_names[i]);
3348 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3354 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3356 struct super_block *sb = dentry->d_sb;
3357 struct ext4_sb_info *sbi = EXT4_SB(sb);
3358 struct ext4_super_block *es = sbi->s_es;
3361 if (test_opt(sb, MINIX_DF)) {
3362 sbi->s_overhead_last = 0;
3363 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3364 ext4_group_t ngroups = sbi->s_groups_count, i;
3365 ext4_fsblk_t overhead = 0;
3369 * Compute the overhead (FS structures). This is constant
3370 * for a given filesystem unless the number of block groups
3371 * changes so we cache the previous value until it does.
3375 * All of the blocks before first_data_block are
3378 overhead = le32_to_cpu(es->s_first_data_block);
3381 * Add the overhead attributed to the superblock and
3382 * block group descriptors. If the sparse superblocks
3383 * feature is turned on, then not all groups have this.
3385 for (i = 0; i < ngroups; i++) {
3386 overhead += ext4_bg_has_super(sb, i) +
3387 ext4_bg_num_gdb(sb, i);
3392 * Every block group has an inode bitmap, a block
3393 * bitmap, and an inode table.
3395 overhead += ngroups * (2 + sbi->s_itb_per_group);
3396 sbi->s_overhead_last = overhead;
3398 sbi->s_blocks_last = ext4_blocks_count(es);
3401 buf->f_type = EXT4_SUPER_MAGIC;
3402 buf->f_bsize = sb->s_blocksize;
3403 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3404 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3405 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3406 ext4_free_blocks_count_set(es, buf->f_bfree);
3407 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3408 if (buf->f_bfree < ext4_r_blocks_count(es))
3410 buf->f_files = le32_to_cpu(es->s_inodes_count);
3411 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3412 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3413 buf->f_namelen = EXT4_NAME_LEN;
3414 fsid = le64_to_cpup((void *)es->s_uuid) ^
3415 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3416 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3417 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3421 /* Helper function for writing quotas on sync - we need to start transaction before quota file
3422 * is locked for write. Otherwise the are possible deadlocks:
3423 * Process 1 Process 2
3424 * ext4_create() quota_sync()
3425 * jbd2_journal_start() write_dquot()
3426 * DQUOT_INIT() down(dqio_mutex)
3427 * down(dqio_mutex) jbd2_journal_start()
3433 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3435 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3438 static int ext4_dquot_initialize(struct inode *inode, int type)
3443 /* We may create quota structure so we need to reserve enough blocks */
3444 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3446 return PTR_ERR(handle);
3447 ret = dquot_initialize(inode, type);
3448 err = ext4_journal_stop(handle);
3454 static int ext4_dquot_drop(struct inode *inode)
3459 /* We may delete quota structure so we need to reserve enough blocks */
3460 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3461 if (IS_ERR(handle)) {
3463 * We call dquot_drop() anyway to at least release references
3464 * to quota structures so that umount does not hang.
3467 return PTR_ERR(handle);
3469 ret = dquot_drop(inode);
3470 err = ext4_journal_stop(handle);
3476 static int ext4_write_dquot(struct dquot *dquot)
3480 struct inode *inode;
3482 inode = dquot_to_inode(dquot);
3483 handle = ext4_journal_start(inode,
3484 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3486 return PTR_ERR(handle);
3487 ret = dquot_commit(dquot);
3488 err = ext4_journal_stop(handle);
3494 static int ext4_acquire_dquot(struct dquot *dquot)
3499 handle = ext4_journal_start(dquot_to_inode(dquot),
3500 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3502 return PTR_ERR(handle);
3503 ret = dquot_acquire(dquot);
3504 err = ext4_journal_stop(handle);
3510 static int ext4_release_dquot(struct dquot *dquot)
3515 handle = ext4_journal_start(dquot_to_inode(dquot),
3516 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3517 if (IS_ERR(handle)) {
3518 /* Release dquot anyway to avoid endless cycle in dqput() */
3519 dquot_release(dquot);
3520 return PTR_ERR(handle);
3522 ret = dquot_release(dquot);
3523 err = ext4_journal_stop(handle);
3529 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3531 /* Are we journaling quotas? */
3532 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3533 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3534 dquot_mark_dquot_dirty(dquot);
3535 return ext4_write_dquot(dquot);
3537 return dquot_mark_dquot_dirty(dquot);
3541 static int ext4_write_info(struct super_block *sb, int type)
3546 /* Data block + inode block */
3547 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3549 return PTR_ERR(handle);
3550 ret = dquot_commit_info(sb, type);
3551 err = ext4_journal_stop(handle);
3558 * Turn on quotas during mount time - we need to find
3559 * the quota file and such...
3561 static int ext4_quota_on_mount(struct super_block *sb, int type)
3563 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3564 EXT4_SB(sb)->s_jquota_fmt, type);
3568 * Standard function to be called on quota_on
3570 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3571 char *name, int remount)
3576 if (!test_opt(sb, QUOTA))
3578 /* When remounting, no checks are needed and in fact, name is NULL */
3580 return vfs_quota_on(sb, type, format_id, name, remount);
3582 err = kern_path(name, LOOKUP_FOLLOW, &path);
3586 /* Quotafile not on the same filesystem? */
3587 if (path.mnt->mnt_sb != sb) {
3591 /* Journaling quota? */
3592 if (EXT4_SB(sb)->s_qf_names[type]) {
3593 /* Quotafile not in fs root? */
3594 if (path.dentry->d_parent != sb->s_root)
3596 "EXT4-fs: Quota file not on filesystem root. "
3597 "Journaled quota will not work.\n");
3601 * When we journal data on quota file, we have to flush journal to see
3602 * all updates to the file when we bypass pagecache...
3604 if (EXT4_SB(sb)->s_journal &&
3605 ext4_should_journal_data(path.dentry->d_inode)) {
3607 * We don't need to lock updates but journal_flush() could
3608 * otherwise be livelocked...
3610 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3611 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3612 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3619 err = vfs_quota_on_path(sb, type, format_id, &path);
3624 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3625 * acquiring the locks... As quota files are never truncated and quota code
3626 * itself serializes the operations (and noone else should touch the files)
3627 * we don't have to be afraid of races */
3628 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3629 size_t len, loff_t off)
3631 struct inode *inode = sb_dqopt(sb)->files[type];
3632 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3634 int offset = off & (sb->s_blocksize - 1);
3637 struct buffer_head *bh;
3638 loff_t i_size = i_size_read(inode);
3642 if (off+len > i_size)
3645 while (toread > 0) {
3646 tocopy = sb->s_blocksize - offset < toread ?
3647 sb->s_blocksize - offset : toread;
3648 bh = ext4_bread(NULL, inode, blk, 0, &err);
3651 if (!bh) /* A hole? */
3652 memset(data, 0, tocopy);
3654 memcpy(data, bh->b_data+offset, tocopy);
3664 /* Write to quotafile (we know the transaction is already started and has
3665 * enough credits) */
3666 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3667 const char *data, size_t len, loff_t off)
3669 struct inode *inode = sb_dqopt(sb)->files[type];
3670 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3672 int offset = off & (sb->s_blocksize - 1);
3674 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3675 size_t towrite = len;
3676 struct buffer_head *bh;
3677 handle_t *handle = journal_current_handle();
3679 if (EXT4_SB(sb)->s_journal && !handle) {
3680 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
3681 " cancelled because transaction is not started.\n",
3682 (unsigned long long)off, (unsigned long long)len);
3685 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3686 while (towrite > 0) {
3687 tocopy = sb->s_blocksize - offset < towrite ?
3688 sb->s_blocksize - offset : towrite;
3689 bh = ext4_bread(handle, inode, blk, 1, &err);
3692 if (journal_quota) {
3693 err = ext4_journal_get_write_access(handle, bh);
3700 memcpy(bh->b_data+offset, data, tocopy);
3701 flush_dcache_page(bh->b_page);
3704 err = ext4_handle_dirty_metadata(handle, NULL, bh);
3706 /* Always do at least ordered writes for quotas */
3707 err = ext4_jbd2_file_inode(handle, inode);
3708 mark_buffer_dirty(bh);
3719 if (len == towrite) {
3720 mutex_unlock(&inode->i_mutex);
3723 if (inode->i_size < off+len-towrite) {
3724 i_size_write(inode, off+len-towrite);
3725 EXT4_I(inode)->i_disksize = inode->i_size;
3727 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3728 ext4_mark_inode_dirty(handle, inode);
3729 mutex_unlock(&inode->i_mutex);
3730 return len - towrite;
3735 static int ext4_get_sb(struct file_system_type *fs_type,
3736 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3738 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3741 #ifdef CONFIG_PROC_FS
3742 static int ext4_ui_proc_show(struct seq_file *m, void *v)
3744 unsigned int *p = m->private;
3746 seq_printf(m, "%u\n", *p);
3750 static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3752 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3755 static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3756 size_t cnt, loff_t *ppos)
3758 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
3761 if (cnt >= sizeof(str))
3763 if (copy_from_user(str, buf, cnt))
3766 *p = simple_strtoul(str, NULL, 0);
3770 const struct file_operations ext4_ui_proc_fops = {
3771 .owner = THIS_MODULE,
3772 .open = ext4_ui_proc_open,
3774 .llseek = seq_lseek,
3775 .release = single_release,
3776 .write = ext4_ui_proc_write,
3780 static struct file_system_type ext4_fs_type = {
3781 .owner = THIS_MODULE,
3783 .get_sb = ext4_get_sb,
3784 .kill_sb = kill_block_super,
3785 .fs_flags = FS_REQUIRES_DEV,
3788 #ifdef CONFIG_EXT4DEV_COMPAT
3789 static int ext4dev_get_sb(struct file_system_type *fs_type,
3790 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3792 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3793 "to mount using ext4\n");
3794 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3795 "will go away by 2.6.31\n");
3796 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3799 static struct file_system_type ext4dev_fs_type = {
3800 .owner = THIS_MODULE,
3802 .get_sb = ext4dev_get_sb,
3803 .kill_sb = kill_block_super,
3804 .fs_flags = FS_REQUIRES_DEV,
3806 MODULE_ALIAS("ext4dev");
3809 static int __init init_ext4_fs(void)
3813 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3814 err = init_ext4_mballoc();
3818 err = init_ext4_xattr();
3821 err = init_inodecache();
3824 err = register_filesystem(&ext4_fs_type);
3827 #ifdef CONFIG_EXT4DEV_COMPAT
3828 err = register_filesystem(&ext4dev_fs_type);
3830 unregister_filesystem(&ext4_fs_type);
3836 destroy_inodecache();
3840 exit_ext4_mballoc();
3844 static void __exit exit_ext4_fs(void)
3846 unregister_filesystem(&ext4_fs_type);
3847 #ifdef CONFIG_EXT4DEV_COMPAT
3848 unregister_filesystem(&ext4dev_fs_type);
3850 destroy_inodecache();
3852 exit_ext4_mballoc();
3853 remove_proc_entry("fs/ext4", NULL);
3856 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3857 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3858 MODULE_LICENSE("GPL");
3859 module_init(init_ext4_fs)
3860 module_exit(exit_ext4_fs)