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 void ext4_block_bitmap_set(struct super_block *sb,
97 struct ext4_group_desc *bg, ext4_fsblk_t blk)
99 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
100 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
101 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
104 void ext4_inode_bitmap_set(struct super_block *sb,
105 struct ext4_group_desc *bg, ext4_fsblk_t blk)
107 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
108 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
109 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
112 void ext4_inode_table_set(struct super_block *sb,
113 struct ext4_group_desc *bg, ext4_fsblk_t blk)
115 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
116 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
117 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
121 * Wrappers for jbd2_journal_start/end.
123 * The only special thing we need to do here is to make sure that all
124 * journal_end calls result in the superblock being marked dirty, so
125 * that sync() will call the filesystem's write_super callback if
128 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
132 if (sb->s_flags & MS_RDONLY)
133 return ERR_PTR(-EROFS);
135 /* Special case here: if the journal has aborted behind our
136 * backs (eg. EIO in the commit thread), then we still need to
137 * take the FS itself readonly cleanly. */
138 journal = EXT4_SB(sb)->s_journal;
140 if (is_journal_aborted(journal)) {
141 ext4_abort(sb, __func__,
142 "Detected aborted journal");
143 return ERR_PTR(-EROFS);
145 return jbd2_journal_start(journal, nblocks);
148 * We're not journaling, return the appropriate indication.
150 current->journal_info = EXT4_NOJOURNAL_HANDLE;
151 return current->journal_info;
155 * The only special thing we need to do here is to make sure that all
156 * jbd2_journal_stop calls result in the superblock being marked dirty, so
157 * that sync() will call the filesystem's write_super callback if
160 int __ext4_journal_stop(const char *where, handle_t *handle)
162 struct super_block *sb;
166 if (!ext4_handle_valid(handle)) {
168 * Do this here since we don't call jbd2_journal_stop() in
171 current->journal_info = NULL;
174 sb = handle->h_transaction->t_journal->j_private;
176 rc = jbd2_journal_stop(handle);
181 __ext4_std_error(sb, where, err);
185 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
186 struct buffer_head *bh, handle_t *handle, int err)
189 const char *errstr = ext4_decode_error(NULL, err, nbuf);
191 BUG_ON(!ext4_handle_valid(handle));
194 BUFFER_TRACE(bh, "abort");
199 if (is_handle_aborted(handle))
202 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
203 caller, errstr, err_fn);
205 jbd2_journal_abort_handle(handle);
208 /* Deal with the reporting of failure conditions on a filesystem such as
209 * inconsistencies detected or read IO failures.
211 * On ext2, we can store the error state of the filesystem in the
212 * superblock. That is not possible on ext4, because we may have other
213 * write ordering constraints on the superblock which prevent us from
214 * writing it out straight away; and given that the journal is about to
215 * be aborted, we can't rely on the current, or future, transactions to
216 * write out the superblock safely.
218 * We'll just use the jbd2_journal_abort() error code to record an error in
219 * the journal instead. On recovery, the journal will compain about
220 * that error until we've noted it down and cleared it.
223 static void ext4_handle_error(struct super_block *sb)
225 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
227 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
228 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
230 if (sb->s_flags & MS_RDONLY)
233 if (!test_opt(sb, ERRORS_CONT)) {
234 journal_t *journal = EXT4_SB(sb)->s_journal;
236 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
238 jbd2_journal_abort(journal, -EIO);
240 if (test_opt(sb, ERRORS_RO)) {
241 printk(KERN_CRIT "Remounting filesystem read-only\n");
242 sb->s_flags |= MS_RDONLY;
244 ext4_commit_super(sb, es, 1);
245 if (test_opt(sb, ERRORS_PANIC))
246 panic("EXT4-fs (device %s): panic forced after error\n",
250 void ext4_error(struct super_block *sb, const char *function,
251 const char *fmt, ...)
256 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
261 ext4_handle_error(sb);
264 static const char *ext4_decode_error(struct super_block *sb, int errno,
271 errstr = "IO failure";
274 errstr = "Out of memory";
277 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
278 errstr = "Journal has aborted";
280 errstr = "Readonly filesystem";
283 /* If the caller passed in an extra buffer for unknown
284 * errors, textualise them now. Else we just return
287 /* Check for truncated error codes... */
288 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
297 /* __ext4_std_error decodes expected errors from journaling functions
298 * automatically and invokes the appropriate error response. */
300 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
305 /* Special case: if the error is EROFS, and we're not already
306 * inside a transaction, then there's really no point in logging
308 if (errno == -EROFS && journal_current_handle() == NULL &&
309 (sb->s_flags & MS_RDONLY))
312 errstr = ext4_decode_error(sb, errno, nbuf);
313 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
314 sb->s_id, function, errstr);
316 ext4_handle_error(sb);
320 * ext4_abort is a much stronger failure handler than ext4_error. The
321 * abort function may be used to deal with unrecoverable failures such
322 * as journal IO errors or ENOMEM at a critical moment in log management.
324 * We unconditionally force the filesystem into an ABORT|READONLY state,
325 * unless the error response on the fs has been set to panic in which
326 * case we take the easy way out and panic immediately.
329 void ext4_abort(struct super_block *sb, const char *function,
330 const char *fmt, ...)
334 printk(KERN_CRIT "ext4_abort called.\n");
337 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
342 if (test_opt(sb, ERRORS_PANIC))
343 panic("EXT4-fs panic from previous error\n");
345 if (sb->s_flags & MS_RDONLY)
348 printk(KERN_CRIT "Remounting filesystem read-only\n");
349 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
350 sb->s_flags |= MS_RDONLY;
351 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
352 if (EXT4_SB(sb)->s_journal)
353 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
356 void ext4_warning(struct super_block *sb, const char *function,
357 const char *fmt, ...)
362 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
369 void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
370 const char *function, const char *fmt, ...)
375 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
378 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
383 if (test_opt(sb, ERRORS_CONT)) {
384 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
385 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
386 ext4_commit_super(sb, es, 0);
389 ext4_unlock_group(sb, grp);
390 ext4_handle_error(sb);
392 * We only get here in the ERRORS_RO case; relocking the group
393 * may be dangerous, but nothing bad will happen since the
394 * filesystem will have already been marked read/only and the
395 * journal has been aborted. We return 1 as a hint to callers
396 * who might what to use the return value from
397 * ext4_grp_locked_error() to distinguish beween the
398 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
399 * aggressively from the ext4 function in question, with a
400 * more appropriate error code.
402 ext4_lock_group(sb, grp);
407 void ext4_update_dynamic_rev(struct super_block *sb)
409 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
411 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
414 ext4_warning(sb, __func__,
415 "updating to rev %d because of new feature flag, "
416 "running e2fsck is recommended",
419 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
420 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
421 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
422 /* leave es->s_feature_*compat flags alone */
423 /* es->s_uuid will be set by e2fsck if empty */
426 * The rest of the superblock fields should be zero, and if not it
427 * means they are likely already in use, so leave them alone. We
428 * can leave it up to e2fsck to clean up any inconsistencies there.
433 * Open the external journal device
435 static struct block_device *ext4_blkdev_get(dev_t dev)
437 struct block_device *bdev;
438 char b[BDEVNAME_SIZE];
440 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
446 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
447 __bdevname(dev, b), PTR_ERR(bdev));
452 * Release the journal device
454 static int ext4_blkdev_put(struct block_device *bdev)
457 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
460 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
462 struct block_device *bdev;
465 bdev = sbi->journal_bdev;
467 ret = ext4_blkdev_put(bdev);
468 sbi->journal_bdev = NULL;
473 static inline struct inode *orphan_list_entry(struct list_head *l)
475 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
478 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
482 printk(KERN_ERR "sb orphan head is %d\n",
483 le32_to_cpu(sbi->s_es->s_last_orphan));
485 printk(KERN_ERR "sb_info orphan list:\n");
486 list_for_each(l, &sbi->s_orphan) {
487 struct inode *inode = orphan_list_entry(l);
489 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
490 inode->i_sb->s_id, inode->i_ino, inode,
491 inode->i_mode, inode->i_nlink,
496 static void ext4_put_super(struct super_block *sb)
498 struct ext4_sb_info *sbi = EXT4_SB(sb);
499 struct ext4_super_block *es = sbi->s_es;
503 ext4_ext_release(sb);
504 ext4_xattr_put_super(sb);
505 if (sbi->s_journal) {
506 err = jbd2_journal_destroy(sbi->s_journal);
507 sbi->s_journal = NULL;
509 ext4_abort(sb, __func__,
510 "Couldn't clean up the journal");
512 if (!(sb->s_flags & MS_RDONLY)) {
513 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
514 es->s_state = cpu_to_le16(sbi->s_mount_state);
515 ext4_commit_super(sb, es, 1);
518 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
519 remove_proc_entry(sb->s_id, ext4_proc_root);
522 for (i = 0; i < sbi->s_gdb_count; i++)
523 brelse(sbi->s_group_desc[i]);
524 kfree(sbi->s_group_desc);
525 kfree(sbi->s_flex_groups);
526 percpu_counter_destroy(&sbi->s_freeblocks_counter);
527 percpu_counter_destroy(&sbi->s_freeinodes_counter);
528 percpu_counter_destroy(&sbi->s_dirs_counter);
529 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
532 for (i = 0; i < MAXQUOTAS; i++)
533 kfree(sbi->s_qf_names[i]);
536 /* Debugging code just in case the in-memory inode orphan list
537 * isn't empty. The on-disk one can be non-empty if we've
538 * detected an error and taken the fs readonly, but the
539 * in-memory list had better be clean by this point. */
540 if (!list_empty(&sbi->s_orphan))
541 dump_orphan_list(sb, sbi);
542 J_ASSERT(list_empty(&sbi->s_orphan));
544 invalidate_bdev(sb->s_bdev);
545 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
547 * Invalidate the journal device's buffers. We don't want them
548 * floating about in memory - the physical journal device may
549 * hotswapped, and it breaks the `ro-after' testing code.
551 sync_blockdev(sbi->journal_bdev);
552 invalidate_bdev(sbi->journal_bdev);
553 ext4_blkdev_remove(sbi);
555 sb->s_fs_info = NULL;
560 static struct kmem_cache *ext4_inode_cachep;
563 * Called inside transaction, so use GFP_NOFS
565 static struct inode *ext4_alloc_inode(struct super_block *sb)
567 struct ext4_inode_info *ei;
569 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
572 #ifdef CONFIG_EXT4_FS_POSIX_ACL
573 ei->i_acl = EXT4_ACL_NOT_CACHED;
574 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
576 ei->vfs_inode.i_version = 1;
577 ei->vfs_inode.i_data.writeback_index = 0;
578 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
579 INIT_LIST_HEAD(&ei->i_prealloc_list);
580 spin_lock_init(&ei->i_prealloc_lock);
582 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
583 * therefore it can be null here. Don't check it, just initialize
586 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
587 ei->i_reserved_data_blocks = 0;
588 ei->i_reserved_meta_blocks = 0;
589 ei->i_allocated_meta_blocks = 0;
590 ei->i_delalloc_reserved_flag = 0;
591 spin_lock_init(&(ei->i_block_reservation_lock));
592 return &ei->vfs_inode;
595 static void ext4_destroy_inode(struct inode *inode)
597 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
598 printk("EXT4 Inode %p: orphan list check failed!\n",
600 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
601 EXT4_I(inode), sizeof(struct ext4_inode_info),
605 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
608 static void init_once(void *foo)
610 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
612 INIT_LIST_HEAD(&ei->i_orphan);
613 #ifdef CONFIG_EXT4_FS_XATTR
614 init_rwsem(&ei->xattr_sem);
616 init_rwsem(&ei->i_data_sem);
617 inode_init_once(&ei->vfs_inode);
620 static int init_inodecache(void)
622 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
623 sizeof(struct ext4_inode_info),
624 0, (SLAB_RECLAIM_ACCOUNT|
627 if (ext4_inode_cachep == NULL)
632 static void destroy_inodecache(void)
634 kmem_cache_destroy(ext4_inode_cachep);
637 static void ext4_clear_inode(struct inode *inode)
639 #ifdef CONFIG_EXT4_FS_POSIX_ACL
640 if (EXT4_I(inode)->i_acl &&
641 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
642 posix_acl_release(EXT4_I(inode)->i_acl);
643 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
645 if (EXT4_I(inode)->i_default_acl &&
646 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
647 posix_acl_release(EXT4_I(inode)->i_default_acl);
648 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
651 ext4_discard_preallocations(inode);
652 if (EXT4_JOURNAL(inode))
653 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
654 &EXT4_I(inode)->jinode);
657 static inline void ext4_show_quota_options(struct seq_file *seq,
658 struct super_block *sb)
660 #if defined(CONFIG_QUOTA)
661 struct ext4_sb_info *sbi = EXT4_SB(sb);
663 if (sbi->s_jquota_fmt)
664 seq_printf(seq, ",jqfmt=%s",
665 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
667 if (sbi->s_qf_names[USRQUOTA])
668 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
670 if (sbi->s_qf_names[GRPQUOTA])
671 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
673 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
674 seq_puts(seq, ",usrquota");
676 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
677 seq_puts(seq, ",grpquota");
683 * - it's set to a non-default value OR
684 * - if the per-sb default is different from the global default
686 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
689 unsigned long def_mount_opts;
690 struct super_block *sb = vfs->mnt_sb;
691 struct ext4_sb_info *sbi = EXT4_SB(sb);
692 struct ext4_super_block *es = sbi->s_es;
694 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
695 def_errors = le16_to_cpu(es->s_errors);
697 if (sbi->s_sb_block != 1)
698 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
699 if (test_opt(sb, MINIX_DF))
700 seq_puts(seq, ",minixdf");
701 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
702 seq_puts(seq, ",grpid");
703 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
704 seq_puts(seq, ",nogrpid");
705 if (sbi->s_resuid != EXT4_DEF_RESUID ||
706 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
707 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
709 if (sbi->s_resgid != EXT4_DEF_RESGID ||
710 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
711 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
713 if (test_opt(sb, ERRORS_RO)) {
714 if (def_errors == EXT4_ERRORS_PANIC ||
715 def_errors == EXT4_ERRORS_CONTINUE) {
716 seq_puts(seq, ",errors=remount-ro");
719 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
720 seq_puts(seq, ",errors=continue");
721 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
722 seq_puts(seq, ",errors=panic");
723 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
724 seq_puts(seq, ",nouid32");
725 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
726 seq_puts(seq, ",debug");
727 if (test_opt(sb, OLDALLOC))
728 seq_puts(seq, ",oldalloc");
729 #ifdef CONFIG_EXT4_FS_XATTR
730 if (test_opt(sb, XATTR_USER) &&
731 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
732 seq_puts(seq, ",user_xattr");
733 if (!test_opt(sb, XATTR_USER) &&
734 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
735 seq_puts(seq, ",nouser_xattr");
738 #ifdef CONFIG_EXT4_FS_POSIX_ACL
739 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
740 seq_puts(seq, ",acl");
741 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
742 seq_puts(seq, ",noacl");
744 if (!test_opt(sb, RESERVATION))
745 seq_puts(seq, ",noreservation");
746 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
747 seq_printf(seq, ",commit=%u",
748 (unsigned) (sbi->s_commit_interval / HZ));
750 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
751 seq_printf(seq, ",min_batch_time=%u",
752 (unsigned) sbi->s_min_batch_time);
754 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
755 seq_printf(seq, ",max_batch_time=%u",
756 (unsigned) sbi->s_min_batch_time);
760 * We're changing the default of barrier mount option, so
761 * let's always display its mount state so it's clear what its
764 seq_puts(seq, ",barrier=");
765 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
766 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
767 seq_puts(seq, ",journal_async_commit");
768 if (test_opt(sb, NOBH))
769 seq_puts(seq, ",nobh");
770 if (!test_opt(sb, EXTENTS))
771 seq_puts(seq, ",noextents");
772 if (test_opt(sb, I_VERSION))
773 seq_puts(seq, ",i_version");
774 if (!test_opt(sb, DELALLOC))
775 seq_puts(seq, ",nodelalloc");
779 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
781 * journal mode get enabled in different ways
782 * So just print the value even if we didn't specify it
784 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
785 seq_puts(seq, ",data=journal");
786 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
787 seq_puts(seq, ",data=ordered");
788 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
789 seq_puts(seq, ",data=writeback");
791 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
792 seq_printf(seq, ",inode_readahead_blks=%u",
793 sbi->s_inode_readahead_blks);
795 if (test_opt(sb, DATA_ERR_ABORT))
796 seq_puts(seq, ",data_err=abort");
798 ext4_show_quota_options(seq, sb);
803 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
804 u64 ino, u32 generation)
808 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
809 return ERR_PTR(-ESTALE);
810 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
811 return ERR_PTR(-ESTALE);
813 /* iget isn't really right if the inode is currently unallocated!!
815 * ext4_read_inode will return a bad_inode if the inode had been
816 * deleted, so we should be safe.
818 * Currently we don't know the generation for parent directory, so
819 * a generation of 0 means "accept any"
821 inode = ext4_iget(sb, ino);
823 return ERR_CAST(inode);
824 if (generation && inode->i_generation != generation) {
826 return ERR_PTR(-ESTALE);
832 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
833 int fh_len, int fh_type)
835 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
839 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
840 int fh_len, int fh_type)
842 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
847 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
848 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
850 static int ext4_dquot_initialize(struct inode *inode, int type);
851 static int ext4_dquot_drop(struct inode *inode);
852 static int ext4_write_dquot(struct dquot *dquot);
853 static int ext4_acquire_dquot(struct dquot *dquot);
854 static int ext4_release_dquot(struct dquot *dquot);
855 static int ext4_mark_dquot_dirty(struct dquot *dquot);
856 static int ext4_write_info(struct super_block *sb, int type);
857 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
858 char *path, int remount);
859 static int ext4_quota_on_mount(struct super_block *sb, int type);
860 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
861 size_t len, loff_t off);
862 static ssize_t ext4_quota_write(struct super_block *sb, int type,
863 const char *data, size_t len, loff_t off);
865 static struct dquot_operations ext4_quota_operations = {
866 .initialize = ext4_dquot_initialize,
867 .drop = ext4_dquot_drop,
868 .alloc_space = dquot_alloc_space,
869 .alloc_inode = dquot_alloc_inode,
870 .free_space = dquot_free_space,
871 .free_inode = dquot_free_inode,
872 .transfer = dquot_transfer,
873 .write_dquot = ext4_write_dquot,
874 .acquire_dquot = ext4_acquire_dquot,
875 .release_dquot = ext4_release_dquot,
876 .mark_dirty = ext4_mark_dquot_dirty,
877 .write_info = ext4_write_info
880 static struct quotactl_ops ext4_qctl_operations = {
881 .quota_on = ext4_quota_on,
882 .quota_off = vfs_quota_off,
883 .quota_sync = vfs_quota_sync,
884 .get_info = vfs_get_dqinfo,
885 .set_info = vfs_set_dqinfo,
886 .get_dqblk = vfs_get_dqblk,
887 .set_dqblk = vfs_set_dqblk
891 static const struct super_operations ext4_sops = {
892 .alloc_inode = ext4_alloc_inode,
893 .destroy_inode = ext4_destroy_inode,
894 .write_inode = ext4_write_inode,
895 .dirty_inode = ext4_dirty_inode,
896 .delete_inode = ext4_delete_inode,
897 .put_super = ext4_put_super,
898 .write_super = ext4_write_super,
899 .sync_fs = ext4_sync_fs,
900 .write_super_lockfs = ext4_write_super_lockfs,
901 .unlockfs = ext4_unlockfs,
902 .statfs = ext4_statfs,
903 .remount_fs = ext4_remount,
904 .clear_inode = ext4_clear_inode,
905 .show_options = ext4_show_options,
907 .quota_read = ext4_quota_read,
908 .quota_write = ext4_quota_write,
912 static const struct export_operations ext4_export_ops = {
913 .fh_to_dentry = ext4_fh_to_dentry,
914 .fh_to_parent = ext4_fh_to_parent,
915 .get_parent = ext4_get_parent,
919 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
920 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
921 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
922 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
923 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
924 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
925 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
926 Opt_journal_checksum, Opt_journal_async_commit,
927 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
928 Opt_data_err_abort, Opt_data_err_ignore,
929 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
930 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
931 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
932 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
933 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
934 Opt_inode_readahead_blks
937 static const match_table_t tokens = {
938 {Opt_bsd_df, "bsddf"},
939 {Opt_minix_df, "minixdf"},
940 {Opt_grpid, "grpid"},
941 {Opt_grpid, "bsdgroups"},
942 {Opt_nogrpid, "nogrpid"},
943 {Opt_nogrpid, "sysvgroups"},
944 {Opt_resgid, "resgid=%u"},
945 {Opt_resuid, "resuid=%u"},
947 {Opt_err_cont, "errors=continue"},
948 {Opt_err_panic, "errors=panic"},
949 {Opt_err_ro, "errors=remount-ro"},
950 {Opt_nouid32, "nouid32"},
951 {Opt_debug, "debug"},
952 {Opt_oldalloc, "oldalloc"},
953 {Opt_orlov, "orlov"},
954 {Opt_user_xattr, "user_xattr"},
955 {Opt_nouser_xattr, "nouser_xattr"},
957 {Opt_noacl, "noacl"},
958 {Opt_reservation, "reservation"},
959 {Opt_noreservation, "noreservation"},
960 {Opt_noload, "noload"},
963 {Opt_commit, "commit=%u"},
964 {Opt_min_batch_time, "min_batch_time=%u"},
965 {Opt_max_batch_time, "max_batch_time=%u"},
966 {Opt_journal_update, "journal=update"},
967 {Opt_journal_inum, "journal=%u"},
968 {Opt_journal_dev, "journal_dev=%u"},
969 {Opt_journal_checksum, "journal_checksum"},
970 {Opt_journal_async_commit, "journal_async_commit"},
971 {Opt_abort, "abort"},
972 {Opt_data_journal, "data=journal"},
973 {Opt_data_ordered, "data=ordered"},
974 {Opt_data_writeback, "data=writeback"},
975 {Opt_data_err_abort, "data_err=abort"},
976 {Opt_data_err_ignore, "data_err=ignore"},
977 {Opt_offusrjquota, "usrjquota="},
978 {Opt_usrjquota, "usrjquota=%s"},
979 {Opt_offgrpjquota, "grpjquota="},
980 {Opt_grpjquota, "grpjquota=%s"},
981 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
982 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
983 {Opt_grpquota, "grpquota"},
984 {Opt_noquota, "noquota"},
985 {Opt_quota, "quota"},
986 {Opt_usrquota, "usrquota"},
987 {Opt_barrier, "barrier=%u"},
988 {Opt_extents, "extents"},
989 {Opt_noextents, "noextents"},
990 {Opt_i_version, "i_version"},
991 {Opt_stripe, "stripe=%u"},
992 {Opt_resize, "resize"},
993 {Opt_delalloc, "delalloc"},
994 {Opt_nodelalloc, "nodelalloc"},
995 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
999 static ext4_fsblk_t get_sb_block(void **data)
1001 ext4_fsblk_t sb_block;
1002 char *options = (char *) *data;
1004 if (!options || strncmp(options, "sb=", 3) != 0)
1005 return 1; /* Default location */
1007 /*todo: use simple_strtoll with >32bit ext4 */
1008 sb_block = simple_strtoul(options, &options, 0);
1009 if (*options && *options != ',') {
1010 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1014 if (*options == ',')
1016 *data = (void *) options;
1020 static int parse_options(char *options, struct super_block *sb,
1021 unsigned int *inum, unsigned long *journal_devnum,
1022 ext4_fsblk_t *n_blocks_count, int is_remount)
1024 struct ext4_sb_info *sbi = EXT4_SB(sb);
1026 substring_t args[MAX_OPT_ARGS];
1033 ext4_fsblk_t last_block;
1038 while ((p = strsep(&options, ",")) != NULL) {
1043 token = match_token(p, tokens, args);
1046 clear_opt(sbi->s_mount_opt, MINIX_DF);
1049 set_opt(sbi->s_mount_opt, MINIX_DF);
1052 set_opt(sbi->s_mount_opt, GRPID);
1055 clear_opt(sbi->s_mount_opt, GRPID);
1058 if (match_int(&args[0], &option))
1060 sbi->s_resuid = option;
1063 if (match_int(&args[0], &option))
1065 sbi->s_resgid = option;
1068 /* handled by get_sb_block() instead of here */
1069 /* *sb_block = match_int(&args[0]); */
1072 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1073 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1074 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1077 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1078 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1079 set_opt(sbi->s_mount_opt, ERRORS_RO);
1082 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1083 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1084 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1087 set_opt(sbi->s_mount_opt, NO_UID32);
1090 set_opt(sbi->s_mount_opt, DEBUG);
1093 set_opt(sbi->s_mount_opt, OLDALLOC);
1096 clear_opt(sbi->s_mount_opt, OLDALLOC);
1098 #ifdef CONFIG_EXT4_FS_XATTR
1099 case Opt_user_xattr:
1100 set_opt(sbi->s_mount_opt, XATTR_USER);
1102 case Opt_nouser_xattr:
1103 clear_opt(sbi->s_mount_opt, XATTR_USER);
1106 case Opt_user_xattr:
1107 case Opt_nouser_xattr:
1108 printk(KERN_ERR "EXT4 (no)user_xattr options "
1112 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1114 set_opt(sbi->s_mount_opt, POSIX_ACL);
1117 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1122 printk(KERN_ERR "EXT4 (no)acl options "
1126 case Opt_reservation:
1127 set_opt(sbi->s_mount_opt, RESERVATION);
1129 case Opt_noreservation:
1130 clear_opt(sbi->s_mount_opt, RESERVATION);
1132 case Opt_journal_update:
1134 /* Eventually we will want to be able to create
1135 a journal file here. For now, only allow the
1136 user to specify an existing inode to be the
1139 printk(KERN_ERR "EXT4-fs: cannot specify "
1140 "journal on remount\n");
1143 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1145 case Opt_journal_inum:
1147 printk(KERN_ERR "EXT4-fs: cannot specify "
1148 "journal on remount\n");
1151 if (match_int(&args[0], &option))
1155 case Opt_journal_dev:
1157 printk(KERN_ERR "EXT4-fs: cannot specify "
1158 "journal on remount\n");
1161 if (match_int(&args[0], &option))
1163 *journal_devnum = option;
1165 case Opt_journal_checksum:
1166 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1168 case Opt_journal_async_commit:
1169 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1170 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1173 set_opt(sbi->s_mount_opt, NOLOAD);
1176 if (match_int(&args[0], &option))
1181 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1182 sbi->s_commit_interval = HZ * option;
1184 case Opt_max_batch_time:
1185 if (match_int(&args[0], &option))
1190 option = EXT4_DEF_MAX_BATCH_TIME;
1191 sbi->s_max_batch_time = option;
1193 case Opt_min_batch_time:
1194 if (match_int(&args[0], &option))
1198 sbi->s_min_batch_time = option;
1200 case Opt_data_journal:
1201 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1203 case Opt_data_ordered:
1204 data_opt = EXT4_MOUNT_ORDERED_DATA;
1206 case Opt_data_writeback:
1207 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1210 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1213 "EXT4-fs: cannot change data "
1214 "mode on remount\n");
1218 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1219 sbi->s_mount_opt |= data_opt;
1222 case Opt_data_err_abort:
1223 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1225 case Opt_data_err_ignore:
1226 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1235 if ((sb_any_quota_enabled(sb) ||
1236 sb_any_quota_suspended(sb)) &&
1237 !sbi->s_qf_names[qtype]) {
1239 "EXT4-fs: Cannot change journaled "
1240 "quota options when quota turned on.\n");
1243 qname = match_strdup(&args[0]);
1246 "EXT4-fs: not enough memory for "
1247 "storing quotafile name.\n");
1250 if (sbi->s_qf_names[qtype] &&
1251 strcmp(sbi->s_qf_names[qtype], qname)) {
1253 "EXT4-fs: %s quota file already "
1254 "specified.\n", QTYPE2NAME(qtype));
1258 sbi->s_qf_names[qtype] = qname;
1259 if (strchr(sbi->s_qf_names[qtype], '/')) {
1261 "EXT4-fs: quotafile must be on "
1262 "filesystem root.\n");
1263 kfree(sbi->s_qf_names[qtype]);
1264 sbi->s_qf_names[qtype] = NULL;
1267 set_opt(sbi->s_mount_opt, QUOTA);
1269 case Opt_offusrjquota:
1272 case Opt_offgrpjquota:
1275 if ((sb_any_quota_enabled(sb) ||
1276 sb_any_quota_suspended(sb)) &&
1277 sbi->s_qf_names[qtype]) {
1278 printk(KERN_ERR "EXT4-fs: Cannot change "
1279 "journaled quota options when "
1280 "quota turned on.\n");
1284 * The space will be released later when all options
1285 * are confirmed to be correct
1287 sbi->s_qf_names[qtype] = NULL;
1289 case Opt_jqfmt_vfsold:
1290 qfmt = QFMT_VFS_OLD;
1292 case Opt_jqfmt_vfsv0:
1295 if ((sb_any_quota_enabled(sb) ||
1296 sb_any_quota_suspended(sb)) &&
1297 sbi->s_jquota_fmt != qfmt) {
1298 printk(KERN_ERR "EXT4-fs: Cannot change "
1299 "journaled quota options when "
1300 "quota turned on.\n");
1303 sbi->s_jquota_fmt = qfmt;
1307 set_opt(sbi->s_mount_opt, QUOTA);
1308 set_opt(sbi->s_mount_opt, USRQUOTA);
1311 set_opt(sbi->s_mount_opt, QUOTA);
1312 set_opt(sbi->s_mount_opt, GRPQUOTA);
1315 if (sb_any_quota_enabled(sb)) {
1316 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1317 "options when quota turned on.\n");
1320 clear_opt(sbi->s_mount_opt, QUOTA);
1321 clear_opt(sbi->s_mount_opt, USRQUOTA);
1322 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1329 "EXT4-fs: quota options not supported.\n");
1333 case Opt_offusrjquota:
1334 case Opt_offgrpjquota:
1335 case Opt_jqfmt_vfsold:
1336 case Opt_jqfmt_vfsv0:
1338 "EXT4-fs: journaled quota options not "
1345 set_opt(sbi->s_mount_opt, ABORT);
1348 if (match_int(&args[0], &option))
1351 set_opt(sbi->s_mount_opt, BARRIER);
1353 clear_opt(sbi->s_mount_opt, BARRIER);
1359 printk("EXT4-fs: resize option only available "
1363 if (match_int(&args[0], &option) != 0)
1365 *n_blocks_count = option;
1368 set_opt(sbi->s_mount_opt, NOBH);
1371 clear_opt(sbi->s_mount_opt, NOBH);
1374 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1375 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1376 ext4_warning(sb, __func__,
1377 "extents feature not enabled "
1378 "on this filesystem, use tune2fs");
1381 set_opt(sbi->s_mount_opt, EXTENTS);
1385 * When e2fsprogs support resizing an already existing
1386 * ext3 file system to greater than 2**32 we need to
1387 * add support to block allocator to handle growing
1388 * already existing block mapped inode so that blocks
1389 * allocated for them fall within 2**32
1391 last_block = ext4_blocks_count(sbi->s_es) - 1;
1392 if (last_block > 0xffffffffULL) {
1393 printk(KERN_ERR "EXT4-fs: Filesystem too "
1394 "large to mount with "
1395 "-o noextents options\n");
1398 clear_opt(sbi->s_mount_opt, EXTENTS);
1401 set_opt(sbi->s_mount_opt, I_VERSION);
1402 sb->s_flags |= MS_I_VERSION;
1404 case Opt_nodelalloc:
1405 clear_opt(sbi->s_mount_opt, DELALLOC);
1408 if (match_int(&args[0], &option))
1412 sbi->s_stripe = option;
1415 set_opt(sbi->s_mount_opt, DELALLOC);
1417 case Opt_inode_readahead_blks:
1418 if (match_int(&args[0], &option))
1420 if (option < 0 || option > (1 << 30))
1422 sbi->s_inode_readahead_blks = option;
1426 "EXT4-fs: Unrecognized mount option \"%s\" "
1427 "or missing value\n", p);
1432 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1433 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1434 sbi->s_qf_names[USRQUOTA])
1435 clear_opt(sbi->s_mount_opt, USRQUOTA);
1437 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1438 sbi->s_qf_names[GRPQUOTA])
1439 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1441 if ((sbi->s_qf_names[USRQUOTA] &&
1442 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1443 (sbi->s_qf_names[GRPQUOTA] &&
1444 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1445 printk(KERN_ERR "EXT4-fs: old and new quota "
1446 "format mixing.\n");
1450 if (!sbi->s_jquota_fmt) {
1451 printk(KERN_ERR "EXT4-fs: journaled quota format "
1452 "not specified.\n");
1456 if (sbi->s_jquota_fmt) {
1457 printk(KERN_ERR "EXT4-fs: journaled quota format "
1458 "specified with no journaling "
1467 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1470 struct ext4_sb_info *sbi = EXT4_SB(sb);
1473 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1474 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1475 "forcing read-only mode\n");
1480 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1481 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1482 "running e2fsck is recommended\n");
1483 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1485 "EXT4-fs warning: mounting fs with errors, "
1486 "running e2fsck is recommended\n");
1487 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1488 le16_to_cpu(es->s_mnt_count) >=
1489 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1491 "EXT4-fs warning: maximal mount count reached, "
1492 "running e2fsck is recommended\n");
1493 else if (le32_to_cpu(es->s_checkinterval) &&
1494 (le32_to_cpu(es->s_lastcheck) +
1495 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1497 "EXT4-fs warning: checktime reached, "
1498 "running e2fsck is recommended\n");
1499 if (!sbi->s_journal)
1500 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1501 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1502 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1503 le16_add_cpu(&es->s_mnt_count, 1);
1504 es->s_mtime = cpu_to_le32(get_seconds());
1505 ext4_update_dynamic_rev(sb);
1507 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1509 ext4_commit_super(sb, es, 1);
1510 if (test_opt(sb, DEBUG))
1511 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1512 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1514 sbi->s_groups_count,
1515 EXT4_BLOCKS_PER_GROUP(sb),
1516 EXT4_INODES_PER_GROUP(sb),
1519 if (EXT4_SB(sb)->s_journal) {
1520 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1521 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1522 "external", EXT4_SB(sb)->s_journal->j_devname);
1524 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1529 static int ext4_fill_flex_info(struct super_block *sb)
1531 struct ext4_sb_info *sbi = EXT4_SB(sb);
1532 struct ext4_group_desc *gdp = NULL;
1533 struct buffer_head *bh;
1534 ext4_group_t flex_group_count;
1535 ext4_group_t flex_group;
1536 int groups_per_flex = 0;
1539 if (!sbi->s_es->s_log_groups_per_flex) {
1540 sbi->s_log_groups_per_flex = 0;
1544 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1545 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1547 /* We allocate both existing and potentially added groups */
1548 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1549 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1550 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1551 sbi->s_flex_groups = kzalloc(flex_group_count *
1552 sizeof(struct flex_groups), GFP_KERNEL);
1553 if (sbi->s_flex_groups == NULL) {
1554 printk(KERN_ERR "EXT4-fs: not enough memory for "
1555 "%u flex groups\n", flex_group_count);
1559 for (i = 0; i < sbi->s_groups_count; i++) {
1560 gdp = ext4_get_group_desc(sb, i, &bh);
1562 flex_group = ext4_flex_group(sbi, i);
1563 sbi->s_flex_groups[flex_group].free_inodes +=
1564 le16_to_cpu(gdp->bg_free_inodes_count);
1565 sbi->s_flex_groups[flex_group].free_blocks +=
1566 le16_to_cpu(gdp->bg_free_blocks_count);
1574 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1575 struct ext4_group_desc *gdp)
1579 if (sbi->s_es->s_feature_ro_compat &
1580 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1581 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1582 __le32 le_group = cpu_to_le32(block_group);
1584 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1585 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1586 crc = crc16(crc, (__u8 *)gdp, offset);
1587 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1588 /* for checksum of struct ext4_group_desc do the rest...*/
1589 if ((sbi->s_es->s_feature_incompat &
1590 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1591 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1592 crc = crc16(crc, (__u8 *)gdp + offset,
1593 le16_to_cpu(sbi->s_es->s_desc_size) -
1597 return cpu_to_le16(crc);
1600 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1601 struct ext4_group_desc *gdp)
1603 if ((sbi->s_es->s_feature_ro_compat &
1604 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1605 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1611 /* Called at mount-time, super-block is locked */
1612 static int ext4_check_descriptors(struct super_block *sb)
1614 struct ext4_sb_info *sbi = EXT4_SB(sb);
1615 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1616 ext4_fsblk_t last_block;
1617 ext4_fsblk_t block_bitmap;
1618 ext4_fsblk_t inode_bitmap;
1619 ext4_fsblk_t inode_table;
1620 int flexbg_flag = 0;
1623 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1626 ext4_debug("Checking group descriptors");
1628 for (i = 0; i < sbi->s_groups_count; i++) {
1629 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1631 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1632 last_block = ext4_blocks_count(sbi->s_es) - 1;
1634 last_block = first_block +
1635 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1637 block_bitmap = ext4_block_bitmap(sb, gdp);
1638 if (block_bitmap < first_block || block_bitmap > last_block) {
1639 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1640 "Block bitmap for group %u not in group "
1641 "(block %llu)!\n", i, block_bitmap);
1644 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1645 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1646 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1647 "Inode bitmap for group %u not in group "
1648 "(block %llu)!\n", i, inode_bitmap);
1651 inode_table = ext4_inode_table(sb, gdp);
1652 if (inode_table < first_block ||
1653 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1654 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1655 "Inode table for group %u not in group "
1656 "(block %llu)!\n", i, inode_table);
1659 spin_lock(sb_bgl_lock(sbi, i));
1660 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1661 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1662 "Checksum for group %u failed (%u!=%u)\n",
1663 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1664 gdp)), le16_to_cpu(gdp->bg_checksum));
1665 if (!(sb->s_flags & MS_RDONLY)) {
1666 spin_unlock(sb_bgl_lock(sbi, i));
1670 spin_unlock(sb_bgl_lock(sbi, i));
1672 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1675 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1676 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1680 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1681 * the superblock) which were deleted from all directories, but held open by
1682 * a process at the time of a crash. We walk the list and try to delete these
1683 * inodes at recovery time (only with a read-write filesystem).
1685 * In order to keep the orphan inode chain consistent during traversal (in
1686 * case of crash during recovery), we link each inode into the superblock
1687 * orphan list_head and handle it the same way as an inode deletion during
1688 * normal operation (which journals the operations for us).
1690 * We only do an iget() and an iput() on each inode, which is very safe if we
1691 * accidentally point at an in-use or already deleted inode. The worst that
1692 * can happen in this case is that we get a "bit already cleared" message from
1693 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1694 * e2fsck was run on this filesystem, and it must have already done the orphan
1695 * inode cleanup for us, so we can safely abort without any further action.
1697 static void ext4_orphan_cleanup(struct super_block *sb,
1698 struct ext4_super_block *es)
1700 unsigned int s_flags = sb->s_flags;
1701 int nr_orphans = 0, nr_truncates = 0;
1705 if (!es->s_last_orphan) {
1706 jbd_debug(4, "no orphan inodes to clean up\n");
1710 if (bdev_read_only(sb->s_bdev)) {
1711 printk(KERN_ERR "EXT4-fs: write access "
1712 "unavailable, skipping orphan cleanup.\n");
1716 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1717 if (es->s_last_orphan)
1718 jbd_debug(1, "Errors on filesystem, "
1719 "clearing orphan list.\n");
1720 es->s_last_orphan = 0;
1721 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1725 if (s_flags & MS_RDONLY) {
1726 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1728 sb->s_flags &= ~MS_RDONLY;
1731 /* Needed for iput() to work correctly and not trash data */
1732 sb->s_flags |= MS_ACTIVE;
1733 /* Turn on quotas so that they are updated correctly */
1734 for (i = 0; i < MAXQUOTAS; i++) {
1735 if (EXT4_SB(sb)->s_qf_names[i]) {
1736 int ret = ext4_quota_on_mount(sb, i);
1739 "EXT4-fs: Cannot turn on journaled "
1740 "quota: error %d\n", ret);
1745 while (es->s_last_orphan) {
1746 struct inode *inode;
1748 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1749 if (IS_ERR(inode)) {
1750 es->s_last_orphan = 0;
1754 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1756 if (inode->i_nlink) {
1758 "%s: truncating inode %lu to %lld bytes\n",
1759 __func__, inode->i_ino, inode->i_size);
1760 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1761 inode->i_ino, inode->i_size);
1762 ext4_truncate(inode);
1766 "%s: deleting unreferenced inode %lu\n",
1767 __func__, inode->i_ino);
1768 jbd_debug(2, "deleting unreferenced inode %lu\n",
1772 iput(inode); /* The delete magic happens here! */
1775 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1778 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1779 sb->s_id, PLURAL(nr_orphans));
1781 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1782 sb->s_id, PLURAL(nr_truncates));
1784 /* Turn quotas off */
1785 for (i = 0; i < MAXQUOTAS; i++) {
1786 if (sb_dqopt(sb)->files[i])
1787 vfs_quota_off(sb, i, 0);
1790 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1793 * Maximal extent format file size.
1794 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1795 * extent format containers, within a sector_t, and within i_blocks
1796 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1797 * so that won't be a limiting factor.
1799 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1801 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1804 loff_t upper_limit = MAX_LFS_FILESIZE;
1806 /* small i_blocks in vfs inode? */
1807 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1809 * CONFIG_LBD is not enabled implies the inode
1810 * i_block represent total blocks in 512 bytes
1811 * 32 == size of vfs inode i_blocks * 8
1813 upper_limit = (1LL << 32) - 1;
1815 /* total blocks in file system block size */
1816 upper_limit >>= (blkbits - 9);
1817 upper_limit <<= blkbits;
1820 /* 32-bit extent-start container, ee_block */
1825 /* Sanity check against vm- & vfs- imposed limits */
1826 if (res > upper_limit)
1833 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
1834 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1835 * We need to be 1 filesystem block less than the 2^48 sector limit.
1837 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1839 loff_t res = EXT4_NDIR_BLOCKS;
1842 /* This is calculated to be the largest file size for a
1843 * dense, bitmapped file such that the total number of
1844 * sectors in the file, including data and all indirect blocks,
1845 * does not exceed 2^48 -1
1846 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1847 * total number of 512 bytes blocks of the file
1850 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1852 * !has_huge_files or CONFIG_LBD is not enabled
1853 * implies the inode i_block represent total blocks in
1854 * 512 bytes 32 == size of vfs inode i_blocks * 8
1856 upper_limit = (1LL << 32) - 1;
1858 /* total blocks in file system block size */
1859 upper_limit >>= (bits - 9);
1863 * We use 48 bit ext4_inode i_blocks
1864 * With EXT4_HUGE_FILE_FL set the i_blocks
1865 * represent total number of blocks in
1866 * file system block size
1868 upper_limit = (1LL << 48) - 1;
1872 /* indirect blocks */
1874 /* double indirect blocks */
1875 meta_blocks += 1 + (1LL << (bits-2));
1876 /* tripple indirect blocks */
1877 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1879 upper_limit -= meta_blocks;
1880 upper_limit <<= bits;
1882 res += 1LL << (bits-2);
1883 res += 1LL << (2*(bits-2));
1884 res += 1LL << (3*(bits-2));
1886 if (res > upper_limit)
1889 if (res > MAX_LFS_FILESIZE)
1890 res = MAX_LFS_FILESIZE;
1895 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1896 ext4_fsblk_t logical_sb_block, int nr)
1898 struct ext4_sb_info *sbi = EXT4_SB(sb);
1899 ext4_group_t bg, first_meta_bg;
1902 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1904 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1906 return logical_sb_block + nr + 1;
1907 bg = sbi->s_desc_per_block * nr;
1908 if (ext4_bg_has_super(sb, bg))
1910 return (has_super + ext4_group_first_block_no(sb, bg));
1914 * ext4_get_stripe_size: Get the stripe size.
1915 * @sbi: In memory super block info
1917 * If we have specified it via mount option, then
1918 * use the mount option value. If the value specified at mount time is
1919 * greater than the blocks per group use the super block value.
1920 * If the super block value is greater than blocks per group return 0.
1921 * Allocator needs it be less than blocks per group.
1924 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1926 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1927 unsigned long stripe_width =
1928 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1930 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1931 return sbi->s_stripe;
1933 if (stripe_width <= sbi->s_blocks_per_group)
1934 return stripe_width;
1936 if (stride <= sbi->s_blocks_per_group)
1942 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
1943 __releases(kernel_lock)
1944 __acquires(kernel_lock)
1947 struct buffer_head *bh;
1948 struct ext4_super_block *es = NULL;
1949 struct ext4_sb_info *sbi;
1951 ext4_fsblk_t sb_block = get_sb_block(&data);
1952 ext4_fsblk_t logical_sb_block;
1953 unsigned long offset = 0;
1954 unsigned int journal_inum = 0;
1955 unsigned long journal_devnum = 0;
1956 unsigned long def_mount_opts;
1964 int needs_recovery, has_huge_files;
1969 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1972 sb->s_fs_info = sbi;
1973 sbi->s_mount_opt = 0;
1974 sbi->s_resuid = EXT4_DEF_RESUID;
1975 sbi->s_resgid = EXT4_DEF_RESGID;
1976 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
1977 sbi->s_sb_block = sb_block;
1981 /* Cleanup superblock name */
1982 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1985 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1987 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1992 * The ext4 superblock will not be buffer aligned for other than 1kB
1993 * block sizes. We need to calculate the offset from buffer start.
1995 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1996 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1997 offset = do_div(logical_sb_block, blocksize);
1999 logical_sb_block = sb_block;
2002 if (!(bh = sb_bread(sb, logical_sb_block))) {
2003 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
2007 * Note: s_es must be initialized as soon as possible because
2008 * some ext4 macro-instructions depend on its value
2010 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2012 sb->s_magic = le16_to_cpu(es->s_magic);
2013 if (sb->s_magic != EXT4_SUPER_MAGIC)
2016 /* Set defaults before we parse the mount options */
2017 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2018 if (def_mount_opts & EXT4_DEFM_DEBUG)
2019 set_opt(sbi->s_mount_opt, DEBUG);
2020 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2021 set_opt(sbi->s_mount_opt, GRPID);
2022 if (def_mount_opts & EXT4_DEFM_UID16)
2023 set_opt(sbi->s_mount_opt, NO_UID32);
2024 #ifdef CONFIG_EXT4_FS_XATTR
2025 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2026 set_opt(sbi->s_mount_opt, XATTR_USER);
2028 #ifdef CONFIG_EXT4_FS_POSIX_ACL
2029 if (def_mount_opts & EXT4_DEFM_ACL)
2030 set_opt(sbi->s_mount_opt, POSIX_ACL);
2032 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2033 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2034 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2035 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2036 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2037 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2039 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2040 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2041 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2042 set_opt(sbi->s_mount_opt, ERRORS_CONT);
2044 set_opt(sbi->s_mount_opt, ERRORS_RO);
2046 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2047 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2048 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2049 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2050 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2052 set_opt(sbi->s_mount_opt, RESERVATION);
2053 set_opt(sbi->s_mount_opt, BARRIER);
2056 * turn on extents feature by default in ext4 filesystem
2057 * only if feature flag already set by mkfs or tune2fs.
2058 * Use -o noextents to turn it off
2060 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2061 set_opt(sbi->s_mount_opt, EXTENTS);
2063 ext4_warning(sb, __func__,
2064 "extents feature not enabled on this filesystem, "
2068 * enable delayed allocation by default
2069 * Use -o nodelalloc to turn it off
2071 set_opt(sbi->s_mount_opt, DELALLOC);
2074 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2078 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2079 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2081 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2082 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2083 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2084 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2086 "EXT4-fs warning: feature flags set on rev 0 fs, "
2087 "running e2fsck is recommended\n");
2090 * Check feature flags regardless of the revision level, since we
2091 * previously didn't change the revision level when setting the flags,
2092 * so there is a chance incompat flags are set on a rev 0 filesystem.
2094 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2096 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2097 "unsupported optional features (%x).\n", sb->s_id,
2098 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2099 ~EXT4_FEATURE_INCOMPAT_SUPP));
2102 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2103 if (!(sb->s_flags & MS_RDONLY) && features) {
2104 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2105 "unsupported optional features (%x).\n", sb->s_id,
2106 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2107 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2110 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2111 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2112 if (has_huge_files) {
2114 * Large file size enabled file system can only be
2115 * mount if kernel is build with CONFIG_LBD
2117 if (sizeof(root->i_blocks) < sizeof(u64) &&
2118 !(sb->s_flags & MS_RDONLY)) {
2119 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2120 "files cannot be mounted read-write "
2121 "without CONFIG_LBD.\n", sb->s_id);
2125 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2127 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2128 blocksize > EXT4_MAX_BLOCK_SIZE) {
2130 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2131 blocksize, sb->s_id);
2135 if (sb->s_blocksize != blocksize) {
2137 /* Validate the filesystem blocksize */
2138 if (!sb_set_blocksize(sb, blocksize)) {
2139 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2145 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2146 offset = do_div(logical_sb_block, blocksize);
2147 bh = sb_bread(sb, logical_sb_block);
2150 "EXT4-fs: Can't read superblock on 2nd try.\n");
2153 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2155 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2157 "EXT4-fs: Magic mismatch, very weird !\n");
2162 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2164 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2166 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2167 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2168 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2170 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2171 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2172 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2173 (!is_power_of_2(sbi->s_inode_size)) ||
2174 (sbi->s_inode_size > blocksize)) {
2176 "EXT4-fs: unsupported inode size: %d\n",
2180 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2181 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2183 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2184 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2185 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2186 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2187 !is_power_of_2(sbi->s_desc_size)) {
2189 "EXT4-fs: unsupported descriptor size %lu\n",
2194 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2195 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2196 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2197 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2199 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2200 if (sbi->s_inodes_per_block == 0)
2202 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2203 sbi->s_inodes_per_block;
2204 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2206 sbi->s_mount_state = le16_to_cpu(es->s_state);
2207 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2208 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2209 for (i = 0; i < 4; i++)
2210 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2211 sbi->s_def_hash_version = es->s_def_hash_version;
2212 i = le32_to_cpu(es->s_flags);
2213 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2214 sbi->s_hash_unsigned = 3;
2215 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2216 #ifdef __CHAR_UNSIGNED__
2217 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2218 sbi->s_hash_unsigned = 3;
2220 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2225 if (sbi->s_blocks_per_group > blocksize * 8) {
2227 "EXT4-fs: #blocks per group too big: %lu\n",
2228 sbi->s_blocks_per_group);
2231 if (sbi->s_inodes_per_group > blocksize * 8) {
2233 "EXT4-fs: #inodes per group too big: %lu\n",
2234 sbi->s_inodes_per_group);
2238 if (ext4_blocks_count(es) >
2239 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2240 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2241 " too large to mount safely\n", sb->s_id);
2242 if (sizeof(sector_t) < 8)
2243 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2248 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2251 /* ensure blocks_count calculation below doesn't sign-extend */
2252 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2253 le32_to_cpu(es->s_first_data_block) + 1) {
2254 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2255 "first data block %u, blocks per group %lu\n",
2256 ext4_blocks_count(es),
2257 le32_to_cpu(es->s_first_data_block),
2258 EXT4_BLOCKS_PER_GROUP(sb));
2261 blocks_count = (ext4_blocks_count(es) -
2262 le32_to_cpu(es->s_first_data_block) +
2263 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2264 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2265 sbi->s_groups_count = blocks_count;
2266 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2267 EXT4_DESC_PER_BLOCK(sb);
2268 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2270 if (sbi->s_group_desc == NULL) {
2271 printk(KERN_ERR "EXT4-fs: not enough memory\n");
2275 #ifdef CONFIG_PROC_FS
2277 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2280 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2282 &sbi->s_inode_readahead_blks);
2285 bgl_lock_init(&sbi->s_blockgroup_lock);
2287 for (i = 0; i < db_count; i++) {
2288 block = descriptor_loc(sb, logical_sb_block, i);
2289 sbi->s_group_desc[i] = sb_bread(sb, block);
2290 if (!sbi->s_group_desc[i]) {
2291 printk(KERN_ERR "EXT4-fs: "
2292 "can't read group descriptor %d\n", i);
2297 if (!ext4_check_descriptors(sb)) {
2298 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2301 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2302 if (!ext4_fill_flex_info(sb)) {
2304 "EXT4-fs: unable to initialize "
2305 "flex_bg meta info!\n");
2309 sbi->s_gdb_count = db_count;
2310 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2311 spin_lock_init(&sbi->s_next_gen_lock);
2313 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2314 ext4_count_free_blocks(sb));
2316 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2317 ext4_count_free_inodes(sb));
2320 err = percpu_counter_init(&sbi->s_dirs_counter,
2321 ext4_count_dirs(sb));
2324 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2327 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2331 sbi->s_stripe = ext4_get_stripe_size(sbi);
2334 * set up enough so that it can read an inode
2336 sb->s_op = &ext4_sops;
2337 sb->s_export_op = &ext4_export_ops;
2338 sb->s_xattr = ext4_xattr_handlers;
2340 sb->s_qcop = &ext4_qctl_operations;
2341 sb->dq_op = &ext4_quota_operations;
2343 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2347 needs_recovery = (es->s_last_orphan != 0 ||
2348 EXT4_HAS_INCOMPAT_FEATURE(sb,
2349 EXT4_FEATURE_INCOMPAT_RECOVER));
2352 * The first inode we look at is the journal inode. Don't try
2353 * root first: it may be modified in the journal!
2355 if (!test_opt(sb, NOLOAD) &&
2356 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2357 if (ext4_load_journal(sb, es, journal_devnum))
2359 if (!(sb->s_flags & MS_RDONLY) &&
2360 EXT4_SB(sb)->s_journal->j_failed_commit) {
2361 printk(KERN_CRIT "EXT4-fs error (device %s): "
2362 "ext4_fill_super: Journal transaction "
2363 "%u is corrupt\n", sb->s_id,
2364 EXT4_SB(sb)->s_journal->j_failed_commit);
2365 if (test_opt(sb, ERRORS_RO)) {
2367 "Mounting filesystem read-only\n");
2368 sb->s_flags |= MS_RDONLY;
2369 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2370 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2372 if (test_opt(sb, ERRORS_PANIC)) {
2373 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2374 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2375 ext4_commit_super(sb, es, 1);
2379 } else if (journal_inum) {
2380 if (ext4_create_journal(sb, es, journal_inum))
2382 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2383 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2384 printk(KERN_ERR "EXT4-fs: required journal recovery "
2385 "suppressed and not mounted read-only\n");
2388 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2389 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2390 sbi->s_journal = NULL;
2395 if (ext4_blocks_count(es) > 0xffffffffULL &&
2396 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2397 JBD2_FEATURE_INCOMPAT_64BIT)) {
2398 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2402 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2403 jbd2_journal_set_features(sbi->s_journal,
2404 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2405 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2406 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2407 jbd2_journal_set_features(sbi->s_journal,
2408 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2409 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2410 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2412 jbd2_journal_clear_features(sbi->s_journal,
2413 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2414 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2417 /* We have now updated the journal if required, so we can
2418 * validate the data journaling mode. */
2419 switch (test_opt(sb, DATA_FLAGS)) {
2421 /* No mode set, assume a default based on the journal
2422 * capabilities: ORDERED_DATA if the journal can
2423 * cope, else JOURNAL_DATA
2425 if (jbd2_journal_check_available_features
2426 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2427 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2429 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2432 case EXT4_MOUNT_ORDERED_DATA:
2433 case EXT4_MOUNT_WRITEBACK_DATA:
2434 if (!jbd2_journal_check_available_features
2435 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2436 printk(KERN_ERR "EXT4-fs: Journal does not support "
2437 "requested data journaling mode\n");
2446 if (test_opt(sb, NOBH)) {
2447 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2448 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2449 "its supported only with writeback mode\n");
2450 clear_opt(sbi->s_mount_opt, NOBH);
2454 * The jbd2_journal_load will have done any necessary log recovery,
2455 * so we can safely mount the rest of the filesystem now.
2458 root = ext4_iget(sb, EXT4_ROOT_INO);
2460 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2461 ret = PTR_ERR(root);
2464 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2466 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2469 sb->s_root = d_alloc_root(root);
2471 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2477 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2479 /* determine the minimum size of new large inodes, if present */
2480 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2481 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2482 EXT4_GOOD_OLD_INODE_SIZE;
2483 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2484 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2485 if (sbi->s_want_extra_isize <
2486 le16_to_cpu(es->s_want_extra_isize))
2487 sbi->s_want_extra_isize =
2488 le16_to_cpu(es->s_want_extra_isize);
2489 if (sbi->s_want_extra_isize <
2490 le16_to_cpu(es->s_min_extra_isize))
2491 sbi->s_want_extra_isize =
2492 le16_to_cpu(es->s_min_extra_isize);
2495 /* Check if enough inode space is available */
2496 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2497 sbi->s_inode_size) {
2498 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2499 EXT4_GOOD_OLD_INODE_SIZE;
2500 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2504 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2505 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2506 "requested data journaling mode\n");
2507 clear_opt(sbi->s_mount_opt, DELALLOC);
2508 } else if (test_opt(sb, DELALLOC))
2509 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2512 err = ext4_mb_init(sb, needs_recovery);
2514 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2520 * akpm: core read_super() calls in here with the superblock locked.
2521 * That deadlocks, because orphan cleanup needs to lock the superblock
2522 * in numerous places. Here we just pop the lock - it's relatively
2523 * harmless, because we are now ready to accept write_super() requests,
2524 * and aviro says that's the only reason for hanging onto the
2527 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2528 ext4_orphan_cleanup(sb, es);
2529 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2530 if (needs_recovery) {
2531 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2532 ext4_mark_recovery_complete(sb, es);
2534 if (EXT4_SB(sb)->s_journal) {
2535 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2536 descr = " journalled data mode";
2537 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2538 descr = " ordered data mode";
2540 descr = " writeback data mode";
2542 descr = "out journal";
2544 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2552 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2557 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2558 if (sbi->s_journal) {
2559 jbd2_journal_destroy(sbi->s_journal);
2560 sbi->s_journal = NULL;
2563 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2564 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2565 percpu_counter_destroy(&sbi->s_dirs_counter);
2566 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2568 for (i = 0; i < db_count; i++)
2569 brelse(sbi->s_group_desc[i]);
2570 kfree(sbi->s_group_desc);
2573 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
2574 remove_proc_entry(sb->s_id, ext4_proc_root);
2577 for (i = 0; i < MAXQUOTAS; i++)
2578 kfree(sbi->s_qf_names[i]);
2580 ext4_blkdev_remove(sbi);
2583 sb->s_fs_info = NULL;
2590 * Setup any per-fs journal parameters now. We'll do this both on
2591 * initial mount, once the journal has been initialised but before we've
2592 * done any recovery; and again on any subsequent remount.
2594 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2596 struct ext4_sb_info *sbi = EXT4_SB(sb);
2598 journal->j_commit_interval = sbi->s_commit_interval;
2599 journal->j_min_batch_time = sbi->s_min_batch_time;
2600 journal->j_max_batch_time = sbi->s_max_batch_time;
2602 spin_lock(&journal->j_state_lock);
2603 if (test_opt(sb, BARRIER))
2604 journal->j_flags |= JBD2_BARRIER;
2606 journal->j_flags &= ~JBD2_BARRIER;
2607 if (test_opt(sb, DATA_ERR_ABORT))
2608 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2610 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2611 spin_unlock(&journal->j_state_lock);
2614 static journal_t *ext4_get_journal(struct super_block *sb,
2615 unsigned int journal_inum)
2617 struct inode *journal_inode;
2620 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2622 /* First, test for the existence of a valid inode on disk. Bad
2623 * things happen if we iget() an unused inode, as the subsequent
2624 * iput() will try to delete it. */
2626 journal_inode = ext4_iget(sb, journal_inum);
2627 if (IS_ERR(journal_inode)) {
2628 printk(KERN_ERR "EXT4-fs: no journal found.\n");
2631 if (!journal_inode->i_nlink) {
2632 make_bad_inode(journal_inode);
2633 iput(journal_inode);
2634 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2638 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2639 journal_inode, journal_inode->i_size);
2640 if (!S_ISREG(journal_inode->i_mode)) {
2641 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2642 iput(journal_inode);
2646 journal = jbd2_journal_init_inode(journal_inode);
2648 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2649 iput(journal_inode);
2652 journal->j_private = sb;
2653 ext4_init_journal_params(sb, journal);
2657 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2660 struct buffer_head *bh;
2664 int hblock, blocksize;
2665 ext4_fsblk_t sb_block;
2666 unsigned long offset;
2667 struct ext4_super_block *es;
2668 struct block_device *bdev;
2670 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2672 bdev = ext4_blkdev_get(j_dev);
2676 if (bd_claim(bdev, sb)) {
2678 "EXT4: failed to claim external journal device.\n");
2679 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2683 blocksize = sb->s_blocksize;
2684 hblock = bdev_hardsect_size(bdev);
2685 if (blocksize < hblock) {
2687 "EXT4-fs: blocksize too small for journal device.\n");
2691 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2692 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2693 set_blocksize(bdev, blocksize);
2694 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2695 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2696 "external journal\n");
2700 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2701 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2702 !(le32_to_cpu(es->s_feature_incompat) &
2703 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2704 printk(KERN_ERR "EXT4-fs: external journal has "
2705 "bad superblock\n");
2710 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2711 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2716 len = ext4_blocks_count(es);
2717 start = sb_block + 1;
2718 brelse(bh); /* we're done with the superblock */
2720 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2721 start, len, blocksize);
2723 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2726 journal->j_private = sb;
2727 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2728 wait_on_buffer(journal->j_sb_buffer);
2729 if (!buffer_uptodate(journal->j_sb_buffer)) {
2730 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2733 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2734 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2735 "user (unsupported) - %d\n",
2736 be32_to_cpu(journal->j_superblock->s_nr_users));
2739 EXT4_SB(sb)->journal_bdev = bdev;
2740 ext4_init_journal_params(sb, journal);
2743 jbd2_journal_destroy(journal);
2745 ext4_blkdev_put(bdev);
2749 static int ext4_load_journal(struct super_block *sb,
2750 struct ext4_super_block *es,
2751 unsigned long journal_devnum)
2754 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2757 int really_read_only;
2759 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2761 if (journal_devnum &&
2762 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2763 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2764 "numbers have changed\n");
2765 journal_dev = new_decode_dev(journal_devnum);
2767 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2769 really_read_only = bdev_read_only(sb->s_bdev);
2772 * Are we loading a blank journal or performing recovery after a
2773 * crash? For recovery, we need to check in advance whether we
2774 * can get read-write access to the device.
2777 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2778 if (sb->s_flags & MS_RDONLY) {
2779 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2780 "required on readonly filesystem.\n");
2781 if (really_read_only) {
2782 printk(KERN_ERR "EXT4-fs: write access "
2783 "unavailable, cannot proceed.\n");
2786 printk(KERN_INFO "EXT4-fs: write access will "
2787 "be enabled during recovery.\n");
2791 if (journal_inum && journal_dev) {
2792 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2793 "and inode journals!\n");
2798 if (!(journal = ext4_get_journal(sb, journal_inum)))
2801 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2805 if (journal->j_flags & JBD2_BARRIER)
2806 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2808 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2810 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2811 err = jbd2_journal_update_format(journal);
2813 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2814 jbd2_journal_destroy(journal);
2819 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2820 err = jbd2_journal_wipe(journal, !really_read_only);
2822 err = jbd2_journal_load(journal);
2825 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2826 jbd2_journal_destroy(journal);
2830 EXT4_SB(sb)->s_journal = journal;
2831 ext4_clear_journal_err(sb, es);
2833 if (journal_devnum &&
2834 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2835 es->s_journal_dev = cpu_to_le32(journal_devnum);
2838 /* Make sure we flush the recovery flag to disk. */
2839 ext4_commit_super(sb, es, 1);
2845 static int ext4_create_journal(struct super_block *sb,
2846 struct ext4_super_block *es,
2847 unsigned int journal_inum)
2852 if (sb->s_flags & MS_RDONLY) {
2853 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2854 "create journal.\n");
2858 journal = ext4_get_journal(sb, journal_inum);
2862 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2865 err = jbd2_journal_create(journal);
2867 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2868 jbd2_journal_destroy(journal);
2872 EXT4_SB(sb)->s_journal = journal;
2874 ext4_update_dynamic_rev(sb);
2875 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2876 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2878 es->s_journal_inum = cpu_to_le32(journal_inum);
2881 /* Make sure we flush the recovery flag to disk. */
2882 ext4_commit_super(sb, es, 1);
2887 static void ext4_commit_super(struct super_block *sb,
2888 struct ext4_super_block *es, int sync)
2890 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2894 if (buffer_write_io_error(sbh)) {
2896 * Oh, dear. A previous attempt to write the
2897 * superblock failed. This could happen because the
2898 * USB device was yanked out. Or it could happen to
2899 * be a transient write error and maybe the block will
2900 * be remapped. Nothing we can do but to retry the
2901 * write and hope for the best.
2903 printk(KERN_ERR "ext4: previous I/O error to "
2904 "superblock detected for %s.\n", sb->s_id);
2905 clear_buffer_write_io_error(sbh);
2906 set_buffer_uptodate(sbh);
2908 es->s_wtime = cpu_to_le32(get_seconds());
2909 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
2910 &EXT4_SB(sb)->s_freeblocks_counter));
2911 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
2912 &EXT4_SB(sb)->s_freeinodes_counter));
2914 BUFFER_TRACE(sbh, "marking dirty");
2915 mark_buffer_dirty(sbh);
2917 sync_dirty_buffer(sbh);
2918 if (buffer_write_io_error(sbh)) {
2919 printk(KERN_ERR "ext4: I/O error while writing "
2920 "superblock for %s.\n", sb->s_id);
2921 clear_buffer_write_io_error(sbh);
2922 set_buffer_uptodate(sbh);
2929 * Have we just finished recovery? If so, and if we are mounting (or
2930 * remounting) the filesystem readonly, then we will end up with a
2931 * consistent fs on disk. Record that fact.
2933 static void ext4_mark_recovery_complete(struct super_block *sb,
2934 struct ext4_super_block *es)
2936 journal_t *journal = EXT4_SB(sb)->s_journal;
2938 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2939 BUG_ON(journal != NULL);
2942 jbd2_journal_lock_updates(journal);
2943 if (jbd2_journal_flush(journal) < 0)
2947 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2948 sb->s_flags & MS_RDONLY) {
2949 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2951 ext4_commit_super(sb, es, 1);
2956 jbd2_journal_unlock_updates(journal);
2960 * If we are mounting (or read-write remounting) a filesystem whose journal
2961 * has recorded an error from a previous lifetime, move that error to the
2962 * main filesystem now.
2964 static void ext4_clear_journal_err(struct super_block *sb,
2965 struct ext4_super_block *es)
2971 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2973 journal = EXT4_SB(sb)->s_journal;
2976 * Now check for any error status which may have been recorded in the
2977 * journal by a prior ext4_error() or ext4_abort()
2980 j_errno = jbd2_journal_errno(journal);
2984 errstr = ext4_decode_error(sb, j_errno, nbuf);
2985 ext4_warning(sb, __func__, "Filesystem error recorded "
2986 "from previous mount: %s", errstr);
2987 ext4_warning(sb, __func__, "Marking fs in need of "
2988 "filesystem check.");
2990 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2991 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2992 ext4_commit_super(sb, es, 1);
2994 jbd2_journal_clear_err(journal);
2999 * Force the running and committing transactions to commit,
3000 * and wait on the commit.
3002 int ext4_force_commit(struct super_block *sb)
3007 if (sb->s_flags & MS_RDONLY)
3010 journal = EXT4_SB(sb)->s_journal;
3013 ret = ext4_journal_force_commit(journal);
3020 * Ext4 always journals updates to the superblock itself, so we don't
3021 * have to propagate any other updates to the superblock on disk at this
3022 * point. (We can probably nuke this function altogether, and remove
3023 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
3025 static void ext4_write_super(struct super_block *sb)
3027 if (EXT4_SB(sb)->s_journal) {
3028 if (mutex_trylock(&sb->s_lock) != 0)
3032 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3036 static int ext4_sync_fs(struct super_block *sb, int wait)
3040 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
3042 if (EXT4_SB(sb)->s_journal) {
3044 ret = ext4_force_commit(sb);
3046 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3048 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3054 * LVM calls this function before a (read-only) snapshot is created. This
3055 * gives us a chance to flush the journal completely and mark the fs clean.
3057 static void ext4_write_super_lockfs(struct super_block *sb)
3061 if (!(sb->s_flags & MS_RDONLY)) {
3062 journal_t *journal = EXT4_SB(sb)->s_journal;
3065 /* Now we set up the journal barrier. */
3066 jbd2_journal_lock_updates(journal);
3069 * We don't want to clear needs_recovery flag when we
3070 * failed to flush the journal.
3072 if (jbd2_journal_flush(journal) < 0)
3076 /* Journal blocked and flushed, clear needs_recovery flag. */
3077 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3078 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3083 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3084 * flag here, even though the filesystem is not technically dirty yet.
3086 static void ext4_unlockfs(struct super_block *sb)
3088 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
3090 /* Reser the needs_recovery flag before the fs is unlocked. */
3091 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3092 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3094 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3098 static int ext4_remount(struct super_block *sb, int *flags, char *data)
3100 struct ext4_super_block *es;
3101 struct ext4_sb_info *sbi = EXT4_SB(sb);
3102 ext4_fsblk_t n_blocks_count = 0;
3103 unsigned long old_sb_flags;
3104 struct ext4_mount_options old_opts;
3111 /* Store the original options */
3112 old_sb_flags = sb->s_flags;
3113 old_opts.s_mount_opt = sbi->s_mount_opt;
3114 old_opts.s_resuid = sbi->s_resuid;
3115 old_opts.s_resgid = sbi->s_resgid;
3116 old_opts.s_commit_interval = sbi->s_commit_interval;
3117 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3118 old_opts.s_max_batch_time = sbi->s_max_batch_time;
3120 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3121 for (i = 0; i < MAXQUOTAS; i++)
3122 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3126 * Allow the "check" option to be passed as a remount option.
3128 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3133 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
3134 ext4_abort(sb, __func__, "Abort forced by user");
3136 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3137 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3142 ext4_init_journal_params(sb, sbi->s_journal);
3144 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3145 n_blocks_count > ext4_blocks_count(es)) {
3146 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3151 if (*flags & MS_RDONLY) {
3153 * First of all, the unconditional stuff we have to do
3154 * to disable replay of the journal when we next remount
3156 sb->s_flags |= MS_RDONLY;
3159 * OK, test if we are remounting a valid rw partition
3160 * readonly, and if so set the rdonly flag and then
3161 * mark the partition as valid again.
3163 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3164 (sbi->s_mount_state & EXT4_VALID_FS))
3165 es->s_state = cpu_to_le16(sbi->s_mount_state);
3168 * We have to unlock super so that we can wait for
3171 if (sbi->s_journal) {
3173 ext4_mark_recovery_complete(sb, es);
3178 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3179 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3180 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3181 "remount RDWR because of unsupported "
3182 "optional features (%x).\n", sb->s_id,
3183 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3184 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3190 * Make sure the group descriptor checksums
3191 * are sane. If they aren't, refuse to
3194 for (g = 0; g < sbi->s_groups_count; g++) {
3195 struct ext4_group_desc *gdp =
3196 ext4_get_group_desc(sb, g, NULL);
3198 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3200 "EXT4-fs: ext4_remount: "
3201 "Checksum for group %u failed (%u!=%u)\n",
3202 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3203 le16_to_cpu(gdp->bg_checksum));
3210 * If we have an unprocessed orphan list hanging
3211 * around from a previously readonly bdev mount,
3212 * require a full umount/remount for now.
3214 if (es->s_last_orphan) {
3215 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3216 "remount RDWR because of unprocessed "
3217 "orphan inode list. Please "
3218 "umount/remount instead.\n",
3225 * Mounting a RDONLY partition read-write, so reread
3226 * and store the current valid flag. (It may have
3227 * been changed by e2fsck since we originally mounted
3231 ext4_clear_journal_err(sb, es);
3232 sbi->s_mount_state = le16_to_cpu(es->s_state);
3233 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3235 if (!ext4_setup_super(sb, es, 0))
3236 sb->s_flags &= ~MS_RDONLY;
3239 if (sbi->s_journal == NULL)
3240 ext4_commit_super(sb, es, 1);
3243 /* Release old quota file names */
3244 for (i = 0; i < MAXQUOTAS; i++)
3245 if (old_opts.s_qf_names[i] &&
3246 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3247 kfree(old_opts.s_qf_names[i]);
3251 sb->s_flags = old_sb_flags;
3252 sbi->s_mount_opt = old_opts.s_mount_opt;
3253 sbi->s_resuid = old_opts.s_resuid;
3254 sbi->s_resgid = old_opts.s_resgid;
3255 sbi->s_commit_interval = old_opts.s_commit_interval;
3256 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3257 sbi->s_max_batch_time = old_opts.s_max_batch_time;
3259 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3260 for (i = 0; i < MAXQUOTAS; i++) {
3261 if (sbi->s_qf_names[i] &&
3262 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3263 kfree(sbi->s_qf_names[i]);
3264 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3270 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3272 struct super_block *sb = dentry->d_sb;
3273 struct ext4_sb_info *sbi = EXT4_SB(sb);
3274 struct ext4_super_block *es = sbi->s_es;
3277 if (test_opt(sb, MINIX_DF)) {
3278 sbi->s_overhead_last = 0;
3279 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3280 ext4_group_t ngroups = sbi->s_groups_count, i;
3281 ext4_fsblk_t overhead = 0;
3285 * Compute the overhead (FS structures). This is constant
3286 * for a given filesystem unless the number of block groups
3287 * changes so we cache the previous value until it does.
3291 * All of the blocks before first_data_block are
3294 overhead = le32_to_cpu(es->s_first_data_block);
3297 * Add the overhead attributed to the superblock and
3298 * block group descriptors. If the sparse superblocks
3299 * feature is turned on, then not all groups have this.
3301 for (i = 0; i < ngroups; i++) {
3302 overhead += ext4_bg_has_super(sb, i) +
3303 ext4_bg_num_gdb(sb, i);
3308 * Every block group has an inode bitmap, a block
3309 * bitmap, and an inode table.
3311 overhead += ngroups * (2 + sbi->s_itb_per_group);
3312 sbi->s_overhead_last = overhead;
3314 sbi->s_blocks_last = ext4_blocks_count(es);
3317 buf->f_type = EXT4_SUPER_MAGIC;
3318 buf->f_bsize = sb->s_blocksize;
3319 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3320 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3321 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3322 ext4_free_blocks_count_set(es, buf->f_bfree);
3323 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3324 if (buf->f_bfree < ext4_r_blocks_count(es))
3326 buf->f_files = le32_to_cpu(es->s_inodes_count);
3327 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3328 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3329 buf->f_namelen = EXT4_NAME_LEN;
3330 fsid = le64_to_cpup((void *)es->s_uuid) ^
3331 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3332 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3333 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3337 /* Helper function for writing quotas on sync - we need to start transaction before quota file
3338 * is locked for write. Otherwise the are possible deadlocks:
3339 * Process 1 Process 2
3340 * ext4_create() quota_sync()
3341 * jbd2_journal_start() write_dquot()
3342 * DQUOT_INIT() down(dqio_mutex)
3343 * down(dqio_mutex) jbd2_journal_start()
3349 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3351 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3354 static int ext4_dquot_initialize(struct inode *inode, int type)
3359 /* We may create quota structure so we need to reserve enough blocks */
3360 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3362 return PTR_ERR(handle);
3363 ret = dquot_initialize(inode, type);
3364 err = ext4_journal_stop(handle);
3370 static int ext4_dquot_drop(struct inode *inode)
3375 /* We may delete quota structure so we need to reserve enough blocks */
3376 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3377 if (IS_ERR(handle)) {
3379 * We call dquot_drop() anyway to at least release references
3380 * to quota structures so that umount does not hang.
3383 return PTR_ERR(handle);
3385 ret = dquot_drop(inode);
3386 err = ext4_journal_stop(handle);
3392 static int ext4_write_dquot(struct dquot *dquot)
3396 struct inode *inode;
3398 inode = dquot_to_inode(dquot);
3399 handle = ext4_journal_start(inode,
3400 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3402 return PTR_ERR(handle);
3403 ret = dquot_commit(dquot);
3404 err = ext4_journal_stop(handle);
3410 static int ext4_acquire_dquot(struct dquot *dquot)
3415 handle = ext4_journal_start(dquot_to_inode(dquot),
3416 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3418 return PTR_ERR(handle);
3419 ret = dquot_acquire(dquot);
3420 err = ext4_journal_stop(handle);
3426 static int ext4_release_dquot(struct dquot *dquot)
3431 handle = ext4_journal_start(dquot_to_inode(dquot),
3432 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3433 if (IS_ERR(handle)) {
3434 /* Release dquot anyway to avoid endless cycle in dqput() */
3435 dquot_release(dquot);
3436 return PTR_ERR(handle);
3438 ret = dquot_release(dquot);
3439 err = ext4_journal_stop(handle);
3445 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3447 /* Are we journaling quotas? */
3448 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3449 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3450 dquot_mark_dquot_dirty(dquot);
3451 return ext4_write_dquot(dquot);
3453 return dquot_mark_dquot_dirty(dquot);
3457 static int ext4_write_info(struct super_block *sb, int type)
3462 /* Data block + inode block */
3463 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3465 return PTR_ERR(handle);
3466 ret = dquot_commit_info(sb, type);
3467 err = ext4_journal_stop(handle);
3474 * Turn on quotas during mount time - we need to find
3475 * the quota file and such...
3477 static int ext4_quota_on_mount(struct super_block *sb, int type)
3479 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3480 EXT4_SB(sb)->s_jquota_fmt, type);
3484 * Standard function to be called on quota_on
3486 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3487 char *name, int remount)
3492 if (!test_opt(sb, QUOTA))
3494 /* When remounting, no checks are needed and in fact, name is NULL */
3496 return vfs_quota_on(sb, type, format_id, name, remount);
3498 err = kern_path(name, LOOKUP_FOLLOW, &path);
3502 /* Quotafile not on the same filesystem? */
3503 if (path.mnt->mnt_sb != sb) {
3507 /* Journaling quota? */
3508 if (EXT4_SB(sb)->s_qf_names[type]) {
3509 /* Quotafile not in fs root? */
3510 if (path.dentry->d_parent != sb->s_root)
3512 "EXT4-fs: Quota file not on filesystem root. "
3513 "Journaled quota will not work.\n");
3517 * When we journal data on quota file, we have to flush journal to see
3518 * all updates to the file when we bypass pagecache...
3520 if (EXT4_SB(sb)->s_journal &&
3521 ext4_should_journal_data(path.dentry->d_inode)) {
3523 * We don't need to lock updates but journal_flush() could
3524 * otherwise be livelocked...
3526 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3527 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3528 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3535 err = vfs_quota_on_path(sb, type, format_id, &path);
3540 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3541 * acquiring the locks... As quota files are never truncated and quota code
3542 * itself serializes the operations (and noone else should touch the files)
3543 * we don't have to be afraid of races */
3544 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3545 size_t len, loff_t off)
3547 struct inode *inode = sb_dqopt(sb)->files[type];
3548 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3550 int offset = off & (sb->s_blocksize - 1);
3553 struct buffer_head *bh;
3554 loff_t i_size = i_size_read(inode);
3558 if (off+len > i_size)
3561 while (toread > 0) {
3562 tocopy = sb->s_blocksize - offset < toread ?
3563 sb->s_blocksize - offset : toread;
3564 bh = ext4_bread(NULL, inode, blk, 0, &err);
3567 if (!bh) /* A hole? */
3568 memset(data, 0, tocopy);
3570 memcpy(data, bh->b_data+offset, tocopy);
3580 /* Write to quotafile (we know the transaction is already started and has
3581 * enough credits) */
3582 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3583 const char *data, size_t len, loff_t off)
3585 struct inode *inode = sb_dqopt(sb)->files[type];
3586 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3588 int offset = off & (sb->s_blocksize - 1);
3590 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3591 size_t towrite = len;
3592 struct buffer_head *bh;
3593 handle_t *handle = journal_current_handle();
3595 if (EXT4_SB(sb)->s_journal && !handle) {
3596 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
3597 " cancelled because transaction is not started.\n",
3598 (unsigned long long)off, (unsigned long long)len);
3601 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3602 while (towrite > 0) {
3603 tocopy = sb->s_blocksize - offset < towrite ?
3604 sb->s_blocksize - offset : towrite;
3605 bh = ext4_bread(handle, inode, blk, 1, &err);
3608 if (journal_quota) {
3609 err = ext4_journal_get_write_access(handle, bh);
3616 memcpy(bh->b_data+offset, data, tocopy);
3617 flush_dcache_page(bh->b_page);
3620 err = ext4_handle_dirty_metadata(handle, NULL, bh);
3622 /* Always do at least ordered writes for quotas */
3623 err = ext4_jbd2_file_inode(handle, inode);
3624 mark_buffer_dirty(bh);
3635 if (len == towrite) {
3636 mutex_unlock(&inode->i_mutex);
3639 if (inode->i_size < off+len-towrite) {
3640 i_size_write(inode, off+len-towrite);
3641 EXT4_I(inode)->i_disksize = inode->i_size;
3643 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3644 ext4_mark_inode_dirty(handle, inode);
3645 mutex_unlock(&inode->i_mutex);
3646 return len - towrite;
3651 static int ext4_get_sb(struct file_system_type *fs_type,
3652 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3654 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3657 #ifdef CONFIG_PROC_FS
3658 static int ext4_ui_proc_show(struct seq_file *m, void *v)
3660 unsigned int *p = m->private;
3662 seq_printf(m, "%u\n", *p);
3666 static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3668 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3671 static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3672 size_t cnt, loff_t *ppos)
3674 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
3677 if (cnt >= sizeof(str))
3679 if (copy_from_user(str, buf, cnt))
3682 *p = simple_strtoul(str, NULL, 0);
3686 const struct file_operations ext4_ui_proc_fops = {
3687 .owner = THIS_MODULE,
3688 .open = ext4_ui_proc_open,
3690 .llseek = seq_lseek,
3691 .release = single_release,
3692 .write = ext4_ui_proc_write,
3696 static struct file_system_type ext4_fs_type = {
3697 .owner = THIS_MODULE,
3699 .get_sb = ext4_get_sb,
3700 .kill_sb = kill_block_super,
3701 .fs_flags = FS_REQUIRES_DEV,
3704 #ifdef CONFIG_EXT4DEV_COMPAT
3705 static int ext4dev_get_sb(struct file_system_type *fs_type,
3706 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3708 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3709 "to mount using ext4\n");
3710 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3711 "will go away by 2.6.31\n");
3712 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3715 static struct file_system_type ext4dev_fs_type = {
3716 .owner = THIS_MODULE,
3718 .get_sb = ext4dev_get_sb,
3719 .kill_sb = kill_block_super,
3720 .fs_flags = FS_REQUIRES_DEV,
3722 MODULE_ALIAS("ext4dev");
3725 static int __init init_ext4_fs(void)
3729 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3730 err = init_ext4_mballoc();
3734 err = init_ext4_xattr();
3737 err = init_inodecache();
3740 err = register_filesystem(&ext4_fs_type);
3743 #ifdef CONFIG_EXT4DEV_COMPAT
3744 err = register_filesystem(&ext4dev_fs_type);
3746 unregister_filesystem(&ext4_fs_type);
3752 destroy_inodecache();
3756 exit_ext4_mballoc();
3760 static void __exit exit_ext4_fs(void)
3762 unregister_filesystem(&ext4_fs_type);
3763 #ifdef CONFIG_EXT4DEV_COMPAT
3764 unregister_filesystem(&ext4dev_fs_type);
3766 destroy_inodecache();
3768 exit_ext4_mballoc();
3769 remove_proc_entry("fs/ext4", NULL);
3772 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3773 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3774 MODULE_LICENSE("GPL");
3775 module_init(init_ext4_fs)
3776 module_exit(exit_ext4_fs)