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_update_dynamic_rev(struct super_block *sb)
371 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
373 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
376 ext4_warning(sb, __func__,
377 "updating to rev %d because of new feature flag, "
378 "running e2fsck is recommended",
381 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
382 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
383 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
384 /* leave es->s_feature_*compat flags alone */
385 /* es->s_uuid will be set by e2fsck if empty */
388 * The rest of the superblock fields should be zero, and if not it
389 * means they are likely already in use, so leave them alone. We
390 * can leave it up to e2fsck to clean up any inconsistencies there.
395 * Open the external journal device
397 static struct block_device *ext4_blkdev_get(dev_t dev)
399 struct block_device *bdev;
400 char b[BDEVNAME_SIZE];
402 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
408 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
409 __bdevname(dev, b), PTR_ERR(bdev));
414 * Release the journal device
416 static int ext4_blkdev_put(struct block_device *bdev)
419 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
422 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
424 struct block_device *bdev;
427 bdev = sbi->journal_bdev;
429 ret = ext4_blkdev_put(bdev);
430 sbi->journal_bdev = NULL;
435 static inline struct inode *orphan_list_entry(struct list_head *l)
437 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
440 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
444 printk(KERN_ERR "sb orphan head is %d\n",
445 le32_to_cpu(sbi->s_es->s_last_orphan));
447 printk(KERN_ERR "sb_info orphan list:\n");
448 list_for_each(l, &sbi->s_orphan) {
449 struct inode *inode = orphan_list_entry(l);
451 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
452 inode->i_sb->s_id, inode->i_ino, inode,
453 inode->i_mode, inode->i_nlink,
458 static void ext4_put_super(struct super_block *sb)
460 struct ext4_sb_info *sbi = EXT4_SB(sb);
461 struct ext4_super_block *es = sbi->s_es;
465 ext4_ext_release(sb);
466 ext4_xattr_put_super(sb);
467 if (sbi->s_journal) {
468 err = jbd2_journal_destroy(sbi->s_journal);
469 sbi->s_journal = NULL;
471 ext4_abort(sb, __func__,
472 "Couldn't clean up the journal");
474 if (!(sb->s_flags & MS_RDONLY)) {
475 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
476 es->s_state = cpu_to_le16(sbi->s_mount_state);
477 ext4_commit_super(sb, es, 1);
480 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
481 remove_proc_entry(sb->s_id, ext4_proc_root);
484 for (i = 0; i < sbi->s_gdb_count; i++)
485 brelse(sbi->s_group_desc[i]);
486 kfree(sbi->s_group_desc);
487 kfree(sbi->s_flex_groups);
488 percpu_counter_destroy(&sbi->s_freeblocks_counter);
489 percpu_counter_destroy(&sbi->s_freeinodes_counter);
490 percpu_counter_destroy(&sbi->s_dirs_counter);
491 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
494 for (i = 0; i < MAXQUOTAS; i++)
495 kfree(sbi->s_qf_names[i]);
498 /* Debugging code just in case the in-memory inode orphan list
499 * isn't empty. The on-disk one can be non-empty if we've
500 * detected an error and taken the fs readonly, but the
501 * in-memory list had better be clean by this point. */
502 if (!list_empty(&sbi->s_orphan))
503 dump_orphan_list(sb, sbi);
504 J_ASSERT(list_empty(&sbi->s_orphan));
506 invalidate_bdev(sb->s_bdev);
507 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
509 * Invalidate the journal device's buffers. We don't want them
510 * floating about in memory - the physical journal device may
511 * hotswapped, and it breaks the `ro-after' testing code.
513 sync_blockdev(sbi->journal_bdev);
514 invalidate_bdev(sbi->journal_bdev);
515 ext4_blkdev_remove(sbi);
517 sb->s_fs_info = NULL;
522 static struct kmem_cache *ext4_inode_cachep;
525 * Called inside transaction, so use GFP_NOFS
527 static struct inode *ext4_alloc_inode(struct super_block *sb)
529 struct ext4_inode_info *ei;
531 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
534 #ifdef CONFIG_EXT4_FS_POSIX_ACL
535 ei->i_acl = EXT4_ACL_NOT_CACHED;
536 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
538 ei->vfs_inode.i_version = 1;
539 ei->vfs_inode.i_data.writeback_index = 0;
540 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
541 INIT_LIST_HEAD(&ei->i_prealloc_list);
542 spin_lock_init(&ei->i_prealloc_lock);
544 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
545 * therefore it can be null here. Don't check it, just initialize
548 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
549 ei->i_reserved_data_blocks = 0;
550 ei->i_reserved_meta_blocks = 0;
551 ei->i_allocated_meta_blocks = 0;
552 ei->i_delalloc_reserved_flag = 0;
553 spin_lock_init(&(ei->i_block_reservation_lock));
554 return &ei->vfs_inode;
557 static void ext4_destroy_inode(struct inode *inode)
559 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
560 printk("EXT4 Inode %p: orphan list check failed!\n",
562 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
563 EXT4_I(inode), sizeof(struct ext4_inode_info),
567 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
570 static void init_once(void *foo)
572 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
574 INIT_LIST_HEAD(&ei->i_orphan);
575 #ifdef CONFIG_EXT4_FS_XATTR
576 init_rwsem(&ei->xattr_sem);
578 init_rwsem(&ei->i_data_sem);
579 inode_init_once(&ei->vfs_inode);
582 static int init_inodecache(void)
584 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
585 sizeof(struct ext4_inode_info),
586 0, (SLAB_RECLAIM_ACCOUNT|
589 if (ext4_inode_cachep == NULL)
594 static void destroy_inodecache(void)
596 kmem_cache_destroy(ext4_inode_cachep);
599 static void ext4_clear_inode(struct inode *inode)
601 #ifdef CONFIG_EXT4_FS_POSIX_ACL
602 if (EXT4_I(inode)->i_acl &&
603 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
604 posix_acl_release(EXT4_I(inode)->i_acl);
605 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
607 if (EXT4_I(inode)->i_default_acl &&
608 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
609 posix_acl_release(EXT4_I(inode)->i_default_acl);
610 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
613 ext4_discard_preallocations(inode);
614 if (EXT4_JOURNAL(inode))
615 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
616 &EXT4_I(inode)->jinode);
619 static inline void ext4_show_quota_options(struct seq_file *seq,
620 struct super_block *sb)
622 #if defined(CONFIG_QUOTA)
623 struct ext4_sb_info *sbi = EXT4_SB(sb);
625 if (sbi->s_jquota_fmt)
626 seq_printf(seq, ",jqfmt=%s",
627 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
629 if (sbi->s_qf_names[USRQUOTA])
630 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
632 if (sbi->s_qf_names[GRPQUOTA])
633 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
635 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
636 seq_puts(seq, ",usrquota");
638 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
639 seq_puts(seq, ",grpquota");
645 * - it's set to a non-default value OR
646 * - if the per-sb default is different from the global default
648 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
651 unsigned long def_mount_opts;
652 struct super_block *sb = vfs->mnt_sb;
653 struct ext4_sb_info *sbi = EXT4_SB(sb);
654 struct ext4_super_block *es = sbi->s_es;
656 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
657 def_errors = le16_to_cpu(es->s_errors);
659 if (sbi->s_sb_block != 1)
660 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
661 if (test_opt(sb, MINIX_DF))
662 seq_puts(seq, ",minixdf");
663 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
664 seq_puts(seq, ",grpid");
665 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
666 seq_puts(seq, ",nogrpid");
667 if (sbi->s_resuid != EXT4_DEF_RESUID ||
668 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
669 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
671 if (sbi->s_resgid != EXT4_DEF_RESGID ||
672 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
673 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
675 if (test_opt(sb, ERRORS_RO)) {
676 if (def_errors == EXT4_ERRORS_PANIC ||
677 def_errors == EXT4_ERRORS_CONTINUE) {
678 seq_puts(seq, ",errors=remount-ro");
681 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
682 seq_puts(seq, ",errors=continue");
683 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
684 seq_puts(seq, ",errors=panic");
685 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
686 seq_puts(seq, ",nouid32");
687 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
688 seq_puts(seq, ",debug");
689 if (test_opt(sb, OLDALLOC))
690 seq_puts(seq, ",oldalloc");
691 #ifdef CONFIG_EXT4_FS_XATTR
692 if (test_opt(sb, XATTR_USER) &&
693 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
694 seq_puts(seq, ",user_xattr");
695 if (!test_opt(sb, XATTR_USER) &&
696 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
697 seq_puts(seq, ",nouser_xattr");
700 #ifdef CONFIG_EXT4_FS_POSIX_ACL
701 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
702 seq_puts(seq, ",acl");
703 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
704 seq_puts(seq, ",noacl");
706 if (!test_opt(sb, RESERVATION))
707 seq_puts(seq, ",noreservation");
708 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
709 seq_printf(seq, ",commit=%u",
710 (unsigned) (sbi->s_commit_interval / HZ));
712 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
713 seq_printf(seq, ",min_batch_time=%u",
714 (unsigned) sbi->s_min_batch_time);
716 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
717 seq_printf(seq, ",max_batch_time=%u",
718 (unsigned) sbi->s_min_batch_time);
722 * We're changing the default of barrier mount option, so
723 * let's always display its mount state so it's clear what its
726 seq_puts(seq, ",barrier=");
727 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
728 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
729 seq_puts(seq, ",journal_async_commit");
730 if (test_opt(sb, NOBH))
731 seq_puts(seq, ",nobh");
732 if (!test_opt(sb, EXTENTS))
733 seq_puts(seq, ",noextents");
734 if (test_opt(sb, I_VERSION))
735 seq_puts(seq, ",i_version");
736 if (!test_opt(sb, DELALLOC))
737 seq_puts(seq, ",nodelalloc");
741 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
743 * journal mode get enabled in different ways
744 * So just print the value even if we didn't specify it
746 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
747 seq_puts(seq, ",data=journal");
748 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
749 seq_puts(seq, ",data=ordered");
750 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
751 seq_puts(seq, ",data=writeback");
753 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
754 seq_printf(seq, ",inode_readahead_blks=%u",
755 sbi->s_inode_readahead_blks);
757 if (test_opt(sb, DATA_ERR_ABORT))
758 seq_puts(seq, ",data_err=abort");
760 ext4_show_quota_options(seq, sb);
765 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
766 u64 ino, u32 generation)
770 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
771 return ERR_PTR(-ESTALE);
772 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
773 return ERR_PTR(-ESTALE);
775 /* iget isn't really right if the inode is currently unallocated!!
777 * ext4_read_inode will return a bad_inode if the inode had been
778 * deleted, so we should be safe.
780 * Currently we don't know the generation for parent directory, so
781 * a generation of 0 means "accept any"
783 inode = ext4_iget(sb, ino);
785 return ERR_CAST(inode);
786 if (generation && inode->i_generation != generation) {
788 return ERR_PTR(-ESTALE);
794 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
795 int fh_len, int fh_type)
797 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
801 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
802 int fh_len, int fh_type)
804 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
809 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
810 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
812 static int ext4_dquot_initialize(struct inode *inode, int type);
813 static int ext4_dquot_drop(struct inode *inode);
814 static int ext4_write_dquot(struct dquot *dquot);
815 static int ext4_acquire_dquot(struct dquot *dquot);
816 static int ext4_release_dquot(struct dquot *dquot);
817 static int ext4_mark_dquot_dirty(struct dquot *dquot);
818 static int ext4_write_info(struct super_block *sb, int type);
819 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
820 char *path, int remount);
821 static int ext4_quota_on_mount(struct super_block *sb, int type);
822 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
823 size_t len, loff_t off);
824 static ssize_t ext4_quota_write(struct super_block *sb, int type,
825 const char *data, size_t len, loff_t off);
827 static struct dquot_operations ext4_quota_operations = {
828 .initialize = ext4_dquot_initialize,
829 .drop = ext4_dquot_drop,
830 .alloc_space = dquot_alloc_space,
831 .alloc_inode = dquot_alloc_inode,
832 .free_space = dquot_free_space,
833 .free_inode = dquot_free_inode,
834 .transfer = dquot_transfer,
835 .write_dquot = ext4_write_dquot,
836 .acquire_dquot = ext4_acquire_dquot,
837 .release_dquot = ext4_release_dquot,
838 .mark_dirty = ext4_mark_dquot_dirty,
839 .write_info = ext4_write_info
842 static struct quotactl_ops ext4_qctl_operations = {
843 .quota_on = ext4_quota_on,
844 .quota_off = vfs_quota_off,
845 .quota_sync = vfs_quota_sync,
846 .get_info = vfs_get_dqinfo,
847 .set_info = vfs_set_dqinfo,
848 .get_dqblk = vfs_get_dqblk,
849 .set_dqblk = vfs_set_dqblk
853 static const struct super_operations ext4_sops = {
854 .alloc_inode = ext4_alloc_inode,
855 .destroy_inode = ext4_destroy_inode,
856 .write_inode = ext4_write_inode,
857 .dirty_inode = ext4_dirty_inode,
858 .delete_inode = ext4_delete_inode,
859 .put_super = ext4_put_super,
860 .write_super = ext4_write_super,
861 .sync_fs = ext4_sync_fs,
862 .write_super_lockfs = ext4_write_super_lockfs,
863 .unlockfs = ext4_unlockfs,
864 .statfs = ext4_statfs,
865 .remount_fs = ext4_remount,
866 .clear_inode = ext4_clear_inode,
867 .show_options = ext4_show_options,
869 .quota_read = ext4_quota_read,
870 .quota_write = ext4_quota_write,
874 static const struct export_operations ext4_export_ops = {
875 .fh_to_dentry = ext4_fh_to_dentry,
876 .fh_to_parent = ext4_fh_to_parent,
877 .get_parent = ext4_get_parent,
881 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
882 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
883 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
884 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
885 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
886 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
887 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
888 Opt_journal_checksum, Opt_journal_async_commit,
889 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
890 Opt_data_err_abort, Opt_data_err_ignore,
891 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
892 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
893 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
894 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
895 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
896 Opt_inode_readahead_blks
899 static const match_table_t tokens = {
900 {Opt_bsd_df, "bsddf"},
901 {Opt_minix_df, "minixdf"},
902 {Opt_grpid, "grpid"},
903 {Opt_grpid, "bsdgroups"},
904 {Opt_nogrpid, "nogrpid"},
905 {Opt_nogrpid, "sysvgroups"},
906 {Opt_resgid, "resgid=%u"},
907 {Opt_resuid, "resuid=%u"},
909 {Opt_err_cont, "errors=continue"},
910 {Opt_err_panic, "errors=panic"},
911 {Opt_err_ro, "errors=remount-ro"},
912 {Opt_nouid32, "nouid32"},
913 {Opt_debug, "debug"},
914 {Opt_oldalloc, "oldalloc"},
915 {Opt_orlov, "orlov"},
916 {Opt_user_xattr, "user_xattr"},
917 {Opt_nouser_xattr, "nouser_xattr"},
919 {Opt_noacl, "noacl"},
920 {Opt_reservation, "reservation"},
921 {Opt_noreservation, "noreservation"},
922 {Opt_noload, "noload"},
925 {Opt_commit, "commit=%u"},
926 {Opt_min_batch_time, "min_batch_time=%u"},
927 {Opt_max_batch_time, "max_batch_time=%u"},
928 {Opt_journal_update, "journal=update"},
929 {Opt_journal_inum, "journal=%u"},
930 {Opt_journal_dev, "journal_dev=%u"},
931 {Opt_journal_checksum, "journal_checksum"},
932 {Opt_journal_async_commit, "journal_async_commit"},
933 {Opt_abort, "abort"},
934 {Opt_data_journal, "data=journal"},
935 {Opt_data_ordered, "data=ordered"},
936 {Opt_data_writeback, "data=writeback"},
937 {Opt_data_err_abort, "data_err=abort"},
938 {Opt_data_err_ignore, "data_err=ignore"},
939 {Opt_offusrjquota, "usrjquota="},
940 {Opt_usrjquota, "usrjquota=%s"},
941 {Opt_offgrpjquota, "grpjquota="},
942 {Opt_grpjquota, "grpjquota=%s"},
943 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
944 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
945 {Opt_grpquota, "grpquota"},
946 {Opt_noquota, "noquota"},
947 {Opt_quota, "quota"},
948 {Opt_usrquota, "usrquota"},
949 {Opt_barrier, "barrier=%u"},
950 {Opt_extents, "extents"},
951 {Opt_noextents, "noextents"},
952 {Opt_i_version, "i_version"},
953 {Opt_stripe, "stripe=%u"},
954 {Opt_resize, "resize"},
955 {Opt_delalloc, "delalloc"},
956 {Opt_nodelalloc, "nodelalloc"},
957 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
961 static ext4_fsblk_t get_sb_block(void **data)
963 ext4_fsblk_t sb_block;
964 char *options = (char *) *data;
966 if (!options || strncmp(options, "sb=", 3) != 0)
967 return 1; /* Default location */
969 /*todo: use simple_strtoll with >32bit ext4 */
970 sb_block = simple_strtoul(options, &options, 0);
971 if (*options && *options != ',') {
972 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
978 *data = (void *) options;
982 static int parse_options(char *options, struct super_block *sb,
983 unsigned int *inum, unsigned long *journal_devnum,
984 ext4_fsblk_t *n_blocks_count, int is_remount)
986 struct ext4_sb_info *sbi = EXT4_SB(sb);
988 substring_t args[MAX_OPT_ARGS];
995 ext4_fsblk_t last_block;
1000 while ((p = strsep(&options, ",")) != NULL) {
1005 token = match_token(p, tokens, args);
1008 clear_opt(sbi->s_mount_opt, MINIX_DF);
1011 set_opt(sbi->s_mount_opt, MINIX_DF);
1014 set_opt(sbi->s_mount_opt, GRPID);
1017 clear_opt(sbi->s_mount_opt, GRPID);
1020 if (match_int(&args[0], &option))
1022 sbi->s_resuid = option;
1025 if (match_int(&args[0], &option))
1027 sbi->s_resgid = option;
1030 /* handled by get_sb_block() instead of here */
1031 /* *sb_block = match_int(&args[0]); */
1034 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1035 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1036 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1039 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1040 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1041 set_opt(sbi->s_mount_opt, ERRORS_RO);
1044 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1045 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1046 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1049 set_opt(sbi->s_mount_opt, NO_UID32);
1052 set_opt(sbi->s_mount_opt, DEBUG);
1055 set_opt(sbi->s_mount_opt, OLDALLOC);
1058 clear_opt(sbi->s_mount_opt, OLDALLOC);
1060 #ifdef CONFIG_EXT4_FS_XATTR
1061 case Opt_user_xattr:
1062 set_opt(sbi->s_mount_opt, XATTR_USER);
1064 case Opt_nouser_xattr:
1065 clear_opt(sbi->s_mount_opt, XATTR_USER);
1068 case Opt_user_xattr:
1069 case Opt_nouser_xattr:
1070 printk(KERN_ERR "EXT4 (no)user_xattr options "
1074 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1076 set_opt(sbi->s_mount_opt, POSIX_ACL);
1079 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1084 printk(KERN_ERR "EXT4 (no)acl options "
1088 case Opt_reservation:
1089 set_opt(sbi->s_mount_opt, RESERVATION);
1091 case Opt_noreservation:
1092 clear_opt(sbi->s_mount_opt, RESERVATION);
1094 case Opt_journal_update:
1096 /* Eventually we will want to be able to create
1097 a journal file here. For now, only allow the
1098 user to specify an existing inode to be the
1101 printk(KERN_ERR "EXT4-fs: cannot specify "
1102 "journal on remount\n");
1105 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1107 case Opt_journal_inum:
1109 printk(KERN_ERR "EXT4-fs: cannot specify "
1110 "journal on remount\n");
1113 if (match_int(&args[0], &option))
1117 case Opt_journal_dev:
1119 printk(KERN_ERR "EXT4-fs: cannot specify "
1120 "journal on remount\n");
1123 if (match_int(&args[0], &option))
1125 *journal_devnum = option;
1127 case Opt_journal_checksum:
1128 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1130 case Opt_journal_async_commit:
1131 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1132 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1135 set_opt(sbi->s_mount_opt, NOLOAD);
1138 if (match_int(&args[0], &option))
1143 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1144 sbi->s_commit_interval = HZ * option;
1146 case Opt_max_batch_time:
1147 if (match_int(&args[0], &option))
1152 option = EXT4_DEF_MAX_BATCH_TIME;
1153 sbi->s_max_batch_time = option;
1155 case Opt_min_batch_time:
1156 if (match_int(&args[0], &option))
1160 sbi->s_min_batch_time = option;
1162 case Opt_data_journal:
1163 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1165 case Opt_data_ordered:
1166 data_opt = EXT4_MOUNT_ORDERED_DATA;
1168 case Opt_data_writeback:
1169 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1172 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1175 "EXT4-fs: cannot change data "
1176 "mode on remount\n");
1180 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1181 sbi->s_mount_opt |= data_opt;
1184 case Opt_data_err_abort:
1185 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1187 case Opt_data_err_ignore:
1188 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1197 if ((sb_any_quota_enabled(sb) ||
1198 sb_any_quota_suspended(sb)) &&
1199 !sbi->s_qf_names[qtype]) {
1201 "EXT4-fs: Cannot change journaled "
1202 "quota options when quota turned on.\n");
1205 qname = match_strdup(&args[0]);
1208 "EXT4-fs: not enough memory for "
1209 "storing quotafile name.\n");
1212 if (sbi->s_qf_names[qtype] &&
1213 strcmp(sbi->s_qf_names[qtype], qname)) {
1215 "EXT4-fs: %s quota file already "
1216 "specified.\n", QTYPE2NAME(qtype));
1220 sbi->s_qf_names[qtype] = qname;
1221 if (strchr(sbi->s_qf_names[qtype], '/')) {
1223 "EXT4-fs: quotafile must be on "
1224 "filesystem root.\n");
1225 kfree(sbi->s_qf_names[qtype]);
1226 sbi->s_qf_names[qtype] = NULL;
1229 set_opt(sbi->s_mount_opt, QUOTA);
1231 case Opt_offusrjquota:
1234 case Opt_offgrpjquota:
1237 if ((sb_any_quota_enabled(sb) ||
1238 sb_any_quota_suspended(sb)) &&
1239 sbi->s_qf_names[qtype]) {
1240 printk(KERN_ERR "EXT4-fs: Cannot change "
1241 "journaled quota options when "
1242 "quota turned on.\n");
1246 * The space will be released later when all options
1247 * are confirmed to be correct
1249 sbi->s_qf_names[qtype] = NULL;
1251 case Opt_jqfmt_vfsold:
1252 qfmt = QFMT_VFS_OLD;
1254 case Opt_jqfmt_vfsv0:
1257 if ((sb_any_quota_enabled(sb) ||
1258 sb_any_quota_suspended(sb)) &&
1259 sbi->s_jquota_fmt != qfmt) {
1260 printk(KERN_ERR "EXT4-fs: Cannot change "
1261 "journaled quota options when "
1262 "quota turned on.\n");
1265 sbi->s_jquota_fmt = qfmt;
1269 set_opt(sbi->s_mount_opt, QUOTA);
1270 set_opt(sbi->s_mount_opt, USRQUOTA);
1273 set_opt(sbi->s_mount_opt, QUOTA);
1274 set_opt(sbi->s_mount_opt, GRPQUOTA);
1277 if (sb_any_quota_enabled(sb)) {
1278 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1279 "options when quota turned on.\n");
1282 clear_opt(sbi->s_mount_opt, QUOTA);
1283 clear_opt(sbi->s_mount_opt, USRQUOTA);
1284 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1291 "EXT4-fs: quota options not supported.\n");
1295 case Opt_offusrjquota:
1296 case Opt_offgrpjquota:
1297 case Opt_jqfmt_vfsold:
1298 case Opt_jqfmt_vfsv0:
1300 "EXT4-fs: journaled quota options not "
1307 set_opt(sbi->s_mount_opt, ABORT);
1310 if (match_int(&args[0], &option))
1313 set_opt(sbi->s_mount_opt, BARRIER);
1315 clear_opt(sbi->s_mount_opt, BARRIER);
1321 printk("EXT4-fs: resize option only available "
1325 if (match_int(&args[0], &option) != 0)
1327 *n_blocks_count = option;
1330 set_opt(sbi->s_mount_opt, NOBH);
1333 clear_opt(sbi->s_mount_opt, NOBH);
1336 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1337 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1338 ext4_warning(sb, __func__,
1339 "extents feature not enabled "
1340 "on this filesystem, use tune2fs");
1343 set_opt(sbi->s_mount_opt, EXTENTS);
1347 * When e2fsprogs support resizing an already existing
1348 * ext3 file system to greater than 2**32 we need to
1349 * add support to block allocator to handle growing
1350 * already existing block mapped inode so that blocks
1351 * allocated for them fall within 2**32
1353 last_block = ext4_blocks_count(sbi->s_es) - 1;
1354 if (last_block > 0xffffffffULL) {
1355 printk(KERN_ERR "EXT4-fs: Filesystem too "
1356 "large to mount with "
1357 "-o noextents options\n");
1360 clear_opt(sbi->s_mount_opt, EXTENTS);
1363 set_opt(sbi->s_mount_opt, I_VERSION);
1364 sb->s_flags |= MS_I_VERSION;
1366 case Opt_nodelalloc:
1367 clear_opt(sbi->s_mount_opt, DELALLOC);
1370 if (match_int(&args[0], &option))
1374 sbi->s_stripe = option;
1377 set_opt(sbi->s_mount_opt, DELALLOC);
1379 case Opt_inode_readahead_blks:
1380 if (match_int(&args[0], &option))
1382 if (option < 0 || option > (1 << 30))
1384 sbi->s_inode_readahead_blks = option;
1388 "EXT4-fs: Unrecognized mount option \"%s\" "
1389 "or missing value\n", p);
1394 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1395 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1396 sbi->s_qf_names[USRQUOTA])
1397 clear_opt(sbi->s_mount_opt, USRQUOTA);
1399 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1400 sbi->s_qf_names[GRPQUOTA])
1401 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1403 if ((sbi->s_qf_names[USRQUOTA] &&
1404 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1405 (sbi->s_qf_names[GRPQUOTA] &&
1406 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1407 printk(KERN_ERR "EXT4-fs: old and new quota "
1408 "format mixing.\n");
1412 if (!sbi->s_jquota_fmt) {
1413 printk(KERN_ERR "EXT4-fs: journaled quota format "
1414 "not specified.\n");
1418 if (sbi->s_jquota_fmt) {
1419 printk(KERN_ERR "EXT4-fs: journaled quota format "
1420 "specified with no journaling "
1429 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1432 struct ext4_sb_info *sbi = EXT4_SB(sb);
1435 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1436 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1437 "forcing read-only mode\n");
1442 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1443 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1444 "running e2fsck is recommended\n");
1445 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1447 "EXT4-fs warning: mounting fs with errors, "
1448 "running e2fsck is recommended\n");
1449 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1450 le16_to_cpu(es->s_mnt_count) >=
1451 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1453 "EXT4-fs warning: maximal mount count reached, "
1454 "running e2fsck is recommended\n");
1455 else if (le32_to_cpu(es->s_checkinterval) &&
1456 (le32_to_cpu(es->s_lastcheck) +
1457 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1459 "EXT4-fs warning: checktime reached, "
1460 "running e2fsck is recommended\n");
1461 if (!sbi->s_journal)
1462 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1463 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1464 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1465 le16_add_cpu(&es->s_mnt_count, 1);
1466 es->s_mtime = cpu_to_le32(get_seconds());
1467 ext4_update_dynamic_rev(sb);
1469 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1471 ext4_commit_super(sb, es, 1);
1472 if (test_opt(sb, DEBUG))
1473 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1474 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1476 sbi->s_groups_count,
1477 EXT4_BLOCKS_PER_GROUP(sb),
1478 EXT4_INODES_PER_GROUP(sb),
1481 if (EXT4_SB(sb)->s_journal) {
1482 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1483 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1484 "external", EXT4_SB(sb)->s_journal->j_devname);
1486 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1491 static int ext4_fill_flex_info(struct super_block *sb)
1493 struct ext4_sb_info *sbi = EXT4_SB(sb);
1494 struct ext4_group_desc *gdp = NULL;
1495 struct buffer_head *bh;
1496 ext4_group_t flex_group_count;
1497 ext4_group_t flex_group;
1498 int groups_per_flex = 0;
1501 if (!sbi->s_es->s_log_groups_per_flex) {
1502 sbi->s_log_groups_per_flex = 0;
1506 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1507 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1509 /* We allocate both existing and potentially added groups */
1510 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1511 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1512 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1513 sbi->s_flex_groups = kzalloc(flex_group_count *
1514 sizeof(struct flex_groups), GFP_KERNEL);
1515 if (sbi->s_flex_groups == NULL) {
1516 printk(KERN_ERR "EXT4-fs: not enough memory for "
1517 "%u flex groups\n", flex_group_count);
1521 for (i = 0; i < sbi->s_groups_count; i++) {
1522 gdp = ext4_get_group_desc(sb, i, &bh);
1524 flex_group = ext4_flex_group(sbi, i);
1525 sbi->s_flex_groups[flex_group].free_inodes +=
1526 le16_to_cpu(gdp->bg_free_inodes_count);
1527 sbi->s_flex_groups[flex_group].free_blocks +=
1528 le16_to_cpu(gdp->bg_free_blocks_count);
1536 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1537 struct ext4_group_desc *gdp)
1541 if (sbi->s_es->s_feature_ro_compat &
1542 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1543 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1544 __le32 le_group = cpu_to_le32(block_group);
1546 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1547 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1548 crc = crc16(crc, (__u8 *)gdp, offset);
1549 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1550 /* for checksum of struct ext4_group_desc do the rest...*/
1551 if ((sbi->s_es->s_feature_incompat &
1552 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1553 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1554 crc = crc16(crc, (__u8 *)gdp + offset,
1555 le16_to_cpu(sbi->s_es->s_desc_size) -
1559 return cpu_to_le16(crc);
1562 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1563 struct ext4_group_desc *gdp)
1565 if ((sbi->s_es->s_feature_ro_compat &
1566 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1567 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1573 /* Called at mount-time, super-block is locked */
1574 static int ext4_check_descriptors(struct super_block *sb)
1576 struct ext4_sb_info *sbi = EXT4_SB(sb);
1577 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1578 ext4_fsblk_t last_block;
1579 ext4_fsblk_t block_bitmap;
1580 ext4_fsblk_t inode_bitmap;
1581 ext4_fsblk_t inode_table;
1582 int flexbg_flag = 0;
1585 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1588 ext4_debug("Checking group descriptors");
1590 for (i = 0; i < sbi->s_groups_count; i++) {
1591 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1593 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1594 last_block = ext4_blocks_count(sbi->s_es) - 1;
1596 last_block = first_block +
1597 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1599 block_bitmap = ext4_block_bitmap(sb, gdp);
1600 if (block_bitmap < first_block || block_bitmap > last_block) {
1601 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1602 "Block bitmap for group %u not in group "
1603 "(block %llu)!\n", i, block_bitmap);
1606 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1607 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1608 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1609 "Inode bitmap for group %u not in group "
1610 "(block %llu)!\n", i, inode_bitmap);
1613 inode_table = ext4_inode_table(sb, gdp);
1614 if (inode_table < first_block ||
1615 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1616 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1617 "Inode table for group %u not in group "
1618 "(block %llu)!\n", i, inode_table);
1621 spin_lock(sb_bgl_lock(sbi, i));
1622 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1623 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1624 "Checksum for group %u failed (%u!=%u)\n",
1625 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1626 gdp)), le16_to_cpu(gdp->bg_checksum));
1627 if (!(sb->s_flags & MS_RDONLY)) {
1628 spin_unlock(sb_bgl_lock(sbi, i));
1632 spin_unlock(sb_bgl_lock(sbi, i));
1634 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1637 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1638 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1642 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1643 * the superblock) which were deleted from all directories, but held open by
1644 * a process at the time of a crash. We walk the list and try to delete these
1645 * inodes at recovery time (only with a read-write filesystem).
1647 * In order to keep the orphan inode chain consistent during traversal (in
1648 * case of crash during recovery), we link each inode into the superblock
1649 * orphan list_head and handle it the same way as an inode deletion during
1650 * normal operation (which journals the operations for us).
1652 * We only do an iget() and an iput() on each inode, which is very safe if we
1653 * accidentally point at an in-use or already deleted inode. The worst that
1654 * can happen in this case is that we get a "bit already cleared" message from
1655 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1656 * e2fsck was run on this filesystem, and it must have already done the orphan
1657 * inode cleanup for us, so we can safely abort without any further action.
1659 static void ext4_orphan_cleanup(struct super_block *sb,
1660 struct ext4_super_block *es)
1662 unsigned int s_flags = sb->s_flags;
1663 int nr_orphans = 0, nr_truncates = 0;
1667 if (!es->s_last_orphan) {
1668 jbd_debug(4, "no orphan inodes to clean up\n");
1672 if (bdev_read_only(sb->s_bdev)) {
1673 printk(KERN_ERR "EXT4-fs: write access "
1674 "unavailable, skipping orphan cleanup.\n");
1678 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1679 if (es->s_last_orphan)
1680 jbd_debug(1, "Errors on filesystem, "
1681 "clearing orphan list.\n");
1682 es->s_last_orphan = 0;
1683 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1687 if (s_flags & MS_RDONLY) {
1688 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1690 sb->s_flags &= ~MS_RDONLY;
1693 /* Needed for iput() to work correctly and not trash data */
1694 sb->s_flags |= MS_ACTIVE;
1695 /* Turn on quotas so that they are updated correctly */
1696 for (i = 0; i < MAXQUOTAS; i++) {
1697 if (EXT4_SB(sb)->s_qf_names[i]) {
1698 int ret = ext4_quota_on_mount(sb, i);
1701 "EXT4-fs: Cannot turn on journaled "
1702 "quota: error %d\n", ret);
1707 while (es->s_last_orphan) {
1708 struct inode *inode;
1710 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1711 if (IS_ERR(inode)) {
1712 es->s_last_orphan = 0;
1716 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1718 if (inode->i_nlink) {
1720 "%s: truncating inode %lu to %lld bytes\n",
1721 __func__, inode->i_ino, inode->i_size);
1722 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1723 inode->i_ino, inode->i_size);
1724 ext4_truncate(inode);
1728 "%s: deleting unreferenced inode %lu\n",
1729 __func__, inode->i_ino);
1730 jbd_debug(2, "deleting unreferenced inode %lu\n",
1734 iput(inode); /* The delete magic happens here! */
1737 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1740 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1741 sb->s_id, PLURAL(nr_orphans));
1743 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1744 sb->s_id, PLURAL(nr_truncates));
1746 /* Turn quotas off */
1747 for (i = 0; i < MAXQUOTAS; i++) {
1748 if (sb_dqopt(sb)->files[i])
1749 vfs_quota_off(sb, i, 0);
1752 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1755 * Maximal extent format file size.
1756 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1757 * extent format containers, within a sector_t, and within i_blocks
1758 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1759 * so that won't be a limiting factor.
1761 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1763 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1766 loff_t upper_limit = MAX_LFS_FILESIZE;
1768 /* small i_blocks in vfs inode? */
1769 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1771 * CONFIG_LBD is not enabled implies the inode
1772 * i_block represent total blocks in 512 bytes
1773 * 32 == size of vfs inode i_blocks * 8
1775 upper_limit = (1LL << 32) - 1;
1777 /* total blocks in file system block size */
1778 upper_limit >>= (blkbits - 9);
1779 upper_limit <<= blkbits;
1782 /* 32-bit extent-start container, ee_block */
1787 /* Sanity check against vm- & vfs- imposed limits */
1788 if (res > upper_limit)
1795 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
1796 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1797 * We need to be 1 filesystem block less than the 2^48 sector limit.
1799 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1801 loff_t res = EXT4_NDIR_BLOCKS;
1804 /* This is calculated to be the largest file size for a
1805 * dense, bitmapped file such that the total number of
1806 * sectors in the file, including data and all indirect blocks,
1807 * does not exceed 2^48 -1
1808 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1809 * total number of 512 bytes blocks of the file
1812 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1814 * !has_huge_files or CONFIG_LBD is not enabled
1815 * implies the inode i_block represent total blocks in
1816 * 512 bytes 32 == size of vfs inode i_blocks * 8
1818 upper_limit = (1LL << 32) - 1;
1820 /* total blocks in file system block size */
1821 upper_limit >>= (bits - 9);
1825 * We use 48 bit ext4_inode i_blocks
1826 * With EXT4_HUGE_FILE_FL set the i_blocks
1827 * represent total number of blocks in
1828 * file system block size
1830 upper_limit = (1LL << 48) - 1;
1834 /* indirect blocks */
1836 /* double indirect blocks */
1837 meta_blocks += 1 + (1LL << (bits-2));
1838 /* tripple indirect blocks */
1839 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1841 upper_limit -= meta_blocks;
1842 upper_limit <<= bits;
1844 res += 1LL << (bits-2);
1845 res += 1LL << (2*(bits-2));
1846 res += 1LL << (3*(bits-2));
1848 if (res > upper_limit)
1851 if (res > MAX_LFS_FILESIZE)
1852 res = MAX_LFS_FILESIZE;
1857 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1858 ext4_fsblk_t logical_sb_block, int nr)
1860 struct ext4_sb_info *sbi = EXT4_SB(sb);
1861 ext4_group_t bg, first_meta_bg;
1864 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1866 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1868 return logical_sb_block + nr + 1;
1869 bg = sbi->s_desc_per_block * nr;
1870 if (ext4_bg_has_super(sb, bg))
1872 return (has_super + ext4_group_first_block_no(sb, bg));
1876 * ext4_get_stripe_size: Get the stripe size.
1877 * @sbi: In memory super block info
1879 * If we have specified it via mount option, then
1880 * use the mount option value. If the value specified at mount time is
1881 * greater than the blocks per group use the super block value.
1882 * If the super block value is greater than blocks per group return 0.
1883 * Allocator needs it be less than blocks per group.
1886 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1888 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1889 unsigned long stripe_width =
1890 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1892 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1893 return sbi->s_stripe;
1895 if (stripe_width <= sbi->s_blocks_per_group)
1896 return stripe_width;
1898 if (stride <= sbi->s_blocks_per_group)
1904 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
1905 __releases(kernel_lock)
1906 __acquires(kernel_lock)
1909 struct buffer_head *bh;
1910 struct ext4_super_block *es = NULL;
1911 struct ext4_sb_info *sbi;
1913 ext4_fsblk_t sb_block = get_sb_block(&data);
1914 ext4_fsblk_t logical_sb_block;
1915 unsigned long offset = 0;
1916 unsigned int journal_inum = 0;
1917 unsigned long journal_devnum = 0;
1918 unsigned long def_mount_opts;
1926 int needs_recovery, has_huge_files;
1931 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1934 sb->s_fs_info = sbi;
1935 sbi->s_mount_opt = 0;
1936 sbi->s_resuid = EXT4_DEF_RESUID;
1937 sbi->s_resgid = EXT4_DEF_RESGID;
1938 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
1939 sbi->s_sb_block = sb_block;
1943 /* Cleanup superblock name */
1944 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1947 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1949 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1954 * The ext4 superblock will not be buffer aligned for other than 1kB
1955 * block sizes. We need to calculate the offset from buffer start.
1957 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1958 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1959 offset = do_div(logical_sb_block, blocksize);
1961 logical_sb_block = sb_block;
1964 if (!(bh = sb_bread(sb, logical_sb_block))) {
1965 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
1969 * Note: s_es must be initialized as soon as possible because
1970 * some ext4 macro-instructions depend on its value
1972 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1974 sb->s_magic = le16_to_cpu(es->s_magic);
1975 if (sb->s_magic != EXT4_SUPER_MAGIC)
1978 /* Set defaults before we parse the mount options */
1979 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1980 if (def_mount_opts & EXT4_DEFM_DEBUG)
1981 set_opt(sbi->s_mount_opt, DEBUG);
1982 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1983 set_opt(sbi->s_mount_opt, GRPID);
1984 if (def_mount_opts & EXT4_DEFM_UID16)
1985 set_opt(sbi->s_mount_opt, NO_UID32);
1986 #ifdef CONFIG_EXT4_FS_XATTR
1987 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1988 set_opt(sbi->s_mount_opt, XATTR_USER);
1990 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1991 if (def_mount_opts & EXT4_DEFM_ACL)
1992 set_opt(sbi->s_mount_opt, POSIX_ACL);
1994 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1995 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1996 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1997 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1998 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1999 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2001 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2002 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2003 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2004 set_opt(sbi->s_mount_opt, ERRORS_CONT);
2006 set_opt(sbi->s_mount_opt, ERRORS_RO);
2008 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2009 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2010 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2011 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2012 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2014 set_opt(sbi->s_mount_opt, RESERVATION);
2015 set_opt(sbi->s_mount_opt, BARRIER);
2018 * turn on extents feature by default in ext4 filesystem
2019 * only if feature flag already set by mkfs or tune2fs.
2020 * Use -o noextents to turn it off
2022 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2023 set_opt(sbi->s_mount_opt, EXTENTS);
2025 ext4_warning(sb, __func__,
2026 "extents feature not enabled on this filesystem, "
2030 * enable delayed allocation by default
2031 * Use -o nodelalloc to turn it off
2033 set_opt(sbi->s_mount_opt, DELALLOC);
2036 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2040 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2041 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2043 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2044 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2045 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2046 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2048 "EXT4-fs warning: feature flags set on rev 0 fs, "
2049 "running e2fsck is recommended\n");
2052 * Check feature flags regardless of the revision level, since we
2053 * previously didn't change the revision level when setting the flags,
2054 * so there is a chance incompat flags are set on a rev 0 filesystem.
2056 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2058 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2059 "unsupported optional features (%x).\n", sb->s_id,
2060 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2061 ~EXT4_FEATURE_INCOMPAT_SUPP));
2064 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2065 if (!(sb->s_flags & MS_RDONLY) && features) {
2066 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2067 "unsupported optional features (%x).\n", sb->s_id,
2068 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2069 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2072 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2073 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2074 if (has_huge_files) {
2076 * Large file size enabled file system can only be
2077 * mount if kernel is build with CONFIG_LBD
2079 if (sizeof(root->i_blocks) < sizeof(u64) &&
2080 !(sb->s_flags & MS_RDONLY)) {
2081 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2082 "files cannot be mounted read-write "
2083 "without CONFIG_LBD.\n", sb->s_id);
2087 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2089 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2090 blocksize > EXT4_MAX_BLOCK_SIZE) {
2092 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2093 blocksize, sb->s_id);
2097 if (sb->s_blocksize != blocksize) {
2099 /* Validate the filesystem blocksize */
2100 if (!sb_set_blocksize(sb, blocksize)) {
2101 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2107 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2108 offset = do_div(logical_sb_block, blocksize);
2109 bh = sb_bread(sb, logical_sb_block);
2112 "EXT4-fs: Can't read superblock on 2nd try.\n");
2115 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2117 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2119 "EXT4-fs: Magic mismatch, very weird !\n");
2124 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2126 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2128 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2129 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2130 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2132 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2133 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2134 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2135 (!is_power_of_2(sbi->s_inode_size)) ||
2136 (sbi->s_inode_size > blocksize)) {
2138 "EXT4-fs: unsupported inode size: %d\n",
2142 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2143 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2145 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2146 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2147 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2148 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2149 !is_power_of_2(sbi->s_desc_size)) {
2151 "EXT4-fs: unsupported descriptor size %lu\n",
2156 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2157 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2158 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2159 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2161 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2162 if (sbi->s_inodes_per_block == 0)
2164 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2165 sbi->s_inodes_per_block;
2166 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2168 sbi->s_mount_state = le16_to_cpu(es->s_state);
2169 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2170 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2171 for (i = 0; i < 4; i++)
2172 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2173 sbi->s_def_hash_version = es->s_def_hash_version;
2174 i = le32_to_cpu(es->s_flags);
2175 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2176 sbi->s_hash_unsigned = 3;
2177 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2178 #ifdef __CHAR_UNSIGNED__
2179 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2180 sbi->s_hash_unsigned = 3;
2182 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2187 if (sbi->s_blocks_per_group > blocksize * 8) {
2189 "EXT4-fs: #blocks per group too big: %lu\n",
2190 sbi->s_blocks_per_group);
2193 if (sbi->s_inodes_per_group > blocksize * 8) {
2195 "EXT4-fs: #inodes per group too big: %lu\n",
2196 sbi->s_inodes_per_group);
2200 if (ext4_blocks_count(es) >
2201 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2202 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2203 " too large to mount safely\n", sb->s_id);
2204 if (sizeof(sector_t) < 8)
2205 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2210 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2213 /* ensure blocks_count calculation below doesn't sign-extend */
2214 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2215 le32_to_cpu(es->s_first_data_block) + 1) {
2216 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2217 "first data block %u, blocks per group %lu\n",
2218 ext4_blocks_count(es),
2219 le32_to_cpu(es->s_first_data_block),
2220 EXT4_BLOCKS_PER_GROUP(sb));
2223 blocks_count = (ext4_blocks_count(es) -
2224 le32_to_cpu(es->s_first_data_block) +
2225 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2226 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2227 sbi->s_groups_count = blocks_count;
2228 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2229 EXT4_DESC_PER_BLOCK(sb);
2230 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2232 if (sbi->s_group_desc == NULL) {
2233 printk(KERN_ERR "EXT4-fs: not enough memory\n");
2237 #ifdef CONFIG_PROC_FS
2239 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2242 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2244 &sbi->s_inode_readahead_blks);
2247 bgl_lock_init(&sbi->s_blockgroup_lock);
2249 for (i = 0; i < db_count; i++) {
2250 block = descriptor_loc(sb, logical_sb_block, i);
2251 sbi->s_group_desc[i] = sb_bread(sb, block);
2252 if (!sbi->s_group_desc[i]) {
2253 printk(KERN_ERR "EXT4-fs: "
2254 "can't read group descriptor %d\n", i);
2259 if (!ext4_check_descriptors(sb)) {
2260 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2263 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2264 if (!ext4_fill_flex_info(sb)) {
2266 "EXT4-fs: unable to initialize "
2267 "flex_bg meta info!\n");
2271 sbi->s_gdb_count = db_count;
2272 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2273 spin_lock_init(&sbi->s_next_gen_lock);
2275 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2276 ext4_count_free_blocks(sb));
2278 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2279 ext4_count_free_inodes(sb));
2282 err = percpu_counter_init(&sbi->s_dirs_counter,
2283 ext4_count_dirs(sb));
2286 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2289 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2293 sbi->s_stripe = ext4_get_stripe_size(sbi);
2296 * set up enough so that it can read an inode
2298 sb->s_op = &ext4_sops;
2299 sb->s_export_op = &ext4_export_ops;
2300 sb->s_xattr = ext4_xattr_handlers;
2302 sb->s_qcop = &ext4_qctl_operations;
2303 sb->dq_op = &ext4_quota_operations;
2305 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2309 needs_recovery = (es->s_last_orphan != 0 ||
2310 EXT4_HAS_INCOMPAT_FEATURE(sb,
2311 EXT4_FEATURE_INCOMPAT_RECOVER));
2314 * The first inode we look at is the journal inode. Don't try
2315 * root first: it may be modified in the journal!
2317 if (!test_opt(sb, NOLOAD) &&
2318 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2319 if (ext4_load_journal(sb, es, journal_devnum))
2321 if (!(sb->s_flags & MS_RDONLY) &&
2322 EXT4_SB(sb)->s_journal->j_failed_commit) {
2323 printk(KERN_CRIT "EXT4-fs error (device %s): "
2324 "ext4_fill_super: Journal transaction "
2325 "%u is corrupt\n", sb->s_id,
2326 EXT4_SB(sb)->s_journal->j_failed_commit);
2327 if (test_opt(sb, ERRORS_RO)) {
2329 "Mounting filesystem read-only\n");
2330 sb->s_flags |= MS_RDONLY;
2331 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2332 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2334 if (test_opt(sb, ERRORS_PANIC)) {
2335 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2336 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2337 ext4_commit_super(sb, es, 1);
2341 } else if (journal_inum) {
2342 if (ext4_create_journal(sb, es, journal_inum))
2344 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2345 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2346 printk(KERN_ERR "EXT4-fs: required journal recovery "
2347 "suppressed and not mounted read-only\n");
2350 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2351 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2352 sbi->s_journal = NULL;
2357 if (ext4_blocks_count(es) > 0xffffffffULL &&
2358 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2359 JBD2_FEATURE_INCOMPAT_64BIT)) {
2360 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2364 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2365 jbd2_journal_set_features(sbi->s_journal,
2366 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2367 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2368 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2369 jbd2_journal_set_features(sbi->s_journal,
2370 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2371 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2372 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2374 jbd2_journal_clear_features(sbi->s_journal,
2375 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2376 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2379 /* We have now updated the journal if required, so we can
2380 * validate the data journaling mode. */
2381 switch (test_opt(sb, DATA_FLAGS)) {
2383 /* No mode set, assume a default based on the journal
2384 * capabilities: ORDERED_DATA if the journal can
2385 * cope, else JOURNAL_DATA
2387 if (jbd2_journal_check_available_features
2388 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2389 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2391 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2394 case EXT4_MOUNT_ORDERED_DATA:
2395 case EXT4_MOUNT_WRITEBACK_DATA:
2396 if (!jbd2_journal_check_available_features
2397 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2398 printk(KERN_ERR "EXT4-fs: Journal does not support "
2399 "requested data journaling mode\n");
2408 if (test_opt(sb, NOBH)) {
2409 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2410 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2411 "its supported only with writeback mode\n");
2412 clear_opt(sbi->s_mount_opt, NOBH);
2416 * The jbd2_journal_load will have done any necessary log recovery,
2417 * so we can safely mount the rest of the filesystem now.
2420 root = ext4_iget(sb, EXT4_ROOT_INO);
2422 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2423 ret = PTR_ERR(root);
2426 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2428 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2431 sb->s_root = d_alloc_root(root);
2433 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2439 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2441 /* determine the minimum size of new large inodes, if present */
2442 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2443 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2444 EXT4_GOOD_OLD_INODE_SIZE;
2445 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2446 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2447 if (sbi->s_want_extra_isize <
2448 le16_to_cpu(es->s_want_extra_isize))
2449 sbi->s_want_extra_isize =
2450 le16_to_cpu(es->s_want_extra_isize);
2451 if (sbi->s_want_extra_isize <
2452 le16_to_cpu(es->s_min_extra_isize))
2453 sbi->s_want_extra_isize =
2454 le16_to_cpu(es->s_min_extra_isize);
2457 /* Check if enough inode space is available */
2458 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2459 sbi->s_inode_size) {
2460 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2461 EXT4_GOOD_OLD_INODE_SIZE;
2462 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2466 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2467 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2468 "requested data journaling mode\n");
2469 clear_opt(sbi->s_mount_opt, DELALLOC);
2470 } else if (test_opt(sb, DELALLOC))
2471 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2474 err = ext4_mb_init(sb, needs_recovery);
2476 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2482 * akpm: core read_super() calls in here with the superblock locked.
2483 * That deadlocks, because orphan cleanup needs to lock the superblock
2484 * in numerous places. Here we just pop the lock - it's relatively
2485 * harmless, because we are now ready to accept write_super() requests,
2486 * and aviro says that's the only reason for hanging onto the
2489 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2490 ext4_orphan_cleanup(sb, es);
2491 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2492 if (needs_recovery) {
2493 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2494 ext4_mark_recovery_complete(sb, es);
2496 if (EXT4_SB(sb)->s_journal) {
2497 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2498 descr = " journalled data mode";
2499 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2500 descr = " ordered data mode";
2502 descr = " writeback data mode";
2504 descr = "out journal";
2506 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2514 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2519 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2520 if (sbi->s_journal) {
2521 jbd2_journal_destroy(sbi->s_journal);
2522 sbi->s_journal = NULL;
2525 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2526 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2527 percpu_counter_destroy(&sbi->s_dirs_counter);
2528 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2530 for (i = 0; i < db_count; i++)
2531 brelse(sbi->s_group_desc[i]);
2532 kfree(sbi->s_group_desc);
2535 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
2536 remove_proc_entry(sb->s_id, ext4_proc_root);
2539 for (i = 0; i < MAXQUOTAS; i++)
2540 kfree(sbi->s_qf_names[i]);
2542 ext4_blkdev_remove(sbi);
2545 sb->s_fs_info = NULL;
2552 * Setup any per-fs journal parameters now. We'll do this both on
2553 * initial mount, once the journal has been initialised but before we've
2554 * done any recovery; and again on any subsequent remount.
2556 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2558 struct ext4_sb_info *sbi = EXT4_SB(sb);
2560 journal->j_commit_interval = sbi->s_commit_interval;
2561 journal->j_min_batch_time = sbi->s_min_batch_time;
2562 journal->j_max_batch_time = sbi->s_max_batch_time;
2564 spin_lock(&journal->j_state_lock);
2565 if (test_opt(sb, BARRIER))
2566 journal->j_flags |= JBD2_BARRIER;
2568 journal->j_flags &= ~JBD2_BARRIER;
2569 if (test_opt(sb, DATA_ERR_ABORT))
2570 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2572 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2573 spin_unlock(&journal->j_state_lock);
2576 static journal_t *ext4_get_journal(struct super_block *sb,
2577 unsigned int journal_inum)
2579 struct inode *journal_inode;
2582 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2584 /* First, test for the existence of a valid inode on disk. Bad
2585 * things happen if we iget() an unused inode, as the subsequent
2586 * iput() will try to delete it. */
2588 journal_inode = ext4_iget(sb, journal_inum);
2589 if (IS_ERR(journal_inode)) {
2590 printk(KERN_ERR "EXT4-fs: no journal found.\n");
2593 if (!journal_inode->i_nlink) {
2594 make_bad_inode(journal_inode);
2595 iput(journal_inode);
2596 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2600 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2601 journal_inode, journal_inode->i_size);
2602 if (!S_ISREG(journal_inode->i_mode)) {
2603 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2604 iput(journal_inode);
2608 journal = jbd2_journal_init_inode(journal_inode);
2610 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2611 iput(journal_inode);
2614 journal->j_private = sb;
2615 ext4_init_journal_params(sb, journal);
2619 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2622 struct buffer_head *bh;
2626 int hblock, blocksize;
2627 ext4_fsblk_t sb_block;
2628 unsigned long offset;
2629 struct ext4_super_block *es;
2630 struct block_device *bdev;
2632 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2634 bdev = ext4_blkdev_get(j_dev);
2638 if (bd_claim(bdev, sb)) {
2640 "EXT4: failed to claim external journal device.\n");
2641 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2645 blocksize = sb->s_blocksize;
2646 hblock = bdev_hardsect_size(bdev);
2647 if (blocksize < hblock) {
2649 "EXT4-fs: blocksize too small for journal device.\n");
2653 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2654 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2655 set_blocksize(bdev, blocksize);
2656 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2657 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2658 "external journal\n");
2662 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2663 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2664 !(le32_to_cpu(es->s_feature_incompat) &
2665 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2666 printk(KERN_ERR "EXT4-fs: external journal has "
2667 "bad superblock\n");
2672 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2673 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2678 len = ext4_blocks_count(es);
2679 start = sb_block + 1;
2680 brelse(bh); /* we're done with the superblock */
2682 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2683 start, len, blocksize);
2685 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2688 journal->j_private = sb;
2689 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2690 wait_on_buffer(journal->j_sb_buffer);
2691 if (!buffer_uptodate(journal->j_sb_buffer)) {
2692 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2695 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2696 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2697 "user (unsupported) - %d\n",
2698 be32_to_cpu(journal->j_superblock->s_nr_users));
2701 EXT4_SB(sb)->journal_bdev = bdev;
2702 ext4_init_journal_params(sb, journal);
2705 jbd2_journal_destroy(journal);
2707 ext4_blkdev_put(bdev);
2711 static int ext4_load_journal(struct super_block *sb,
2712 struct ext4_super_block *es,
2713 unsigned long journal_devnum)
2716 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2719 int really_read_only;
2721 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2723 if (journal_devnum &&
2724 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2725 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2726 "numbers have changed\n");
2727 journal_dev = new_decode_dev(journal_devnum);
2729 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2731 really_read_only = bdev_read_only(sb->s_bdev);
2734 * Are we loading a blank journal or performing recovery after a
2735 * crash? For recovery, we need to check in advance whether we
2736 * can get read-write access to the device.
2739 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2740 if (sb->s_flags & MS_RDONLY) {
2741 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2742 "required on readonly filesystem.\n");
2743 if (really_read_only) {
2744 printk(KERN_ERR "EXT4-fs: write access "
2745 "unavailable, cannot proceed.\n");
2748 printk(KERN_INFO "EXT4-fs: write access will "
2749 "be enabled during recovery.\n");
2753 if (journal_inum && journal_dev) {
2754 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2755 "and inode journals!\n");
2760 if (!(journal = ext4_get_journal(sb, journal_inum)))
2763 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2767 if (journal->j_flags & JBD2_BARRIER)
2768 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2770 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2772 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2773 err = jbd2_journal_update_format(journal);
2775 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2776 jbd2_journal_destroy(journal);
2781 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2782 err = jbd2_journal_wipe(journal, !really_read_only);
2784 err = jbd2_journal_load(journal);
2787 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2788 jbd2_journal_destroy(journal);
2792 EXT4_SB(sb)->s_journal = journal;
2793 ext4_clear_journal_err(sb, es);
2795 if (journal_devnum &&
2796 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2797 es->s_journal_dev = cpu_to_le32(journal_devnum);
2800 /* Make sure we flush the recovery flag to disk. */
2801 ext4_commit_super(sb, es, 1);
2807 static int ext4_create_journal(struct super_block *sb,
2808 struct ext4_super_block *es,
2809 unsigned int journal_inum)
2814 if (sb->s_flags & MS_RDONLY) {
2815 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2816 "create journal.\n");
2820 journal = ext4_get_journal(sb, journal_inum);
2824 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2827 err = jbd2_journal_create(journal);
2829 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2830 jbd2_journal_destroy(journal);
2834 EXT4_SB(sb)->s_journal = journal;
2836 ext4_update_dynamic_rev(sb);
2837 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2838 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2840 es->s_journal_inum = cpu_to_le32(journal_inum);
2843 /* Make sure we flush the recovery flag to disk. */
2844 ext4_commit_super(sb, es, 1);
2849 static void ext4_commit_super(struct super_block *sb,
2850 struct ext4_super_block *es, int sync)
2852 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2856 if (buffer_write_io_error(sbh)) {
2858 * Oh, dear. A previous attempt to write the
2859 * superblock failed. This could happen because the
2860 * USB device was yanked out. Or it could happen to
2861 * be a transient write error and maybe the block will
2862 * be remapped. Nothing we can do but to retry the
2863 * write and hope for the best.
2865 printk(KERN_ERR "ext4: previous I/O error to "
2866 "superblock detected for %s.\n", sb->s_id);
2867 clear_buffer_write_io_error(sbh);
2868 set_buffer_uptodate(sbh);
2870 es->s_wtime = cpu_to_le32(get_seconds());
2871 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2872 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2873 BUFFER_TRACE(sbh, "marking dirty");
2874 mark_buffer_dirty(sbh);
2876 sync_dirty_buffer(sbh);
2877 if (buffer_write_io_error(sbh)) {
2878 printk(KERN_ERR "ext4: I/O error while writing "
2879 "superblock for %s.\n", sb->s_id);
2880 clear_buffer_write_io_error(sbh);
2881 set_buffer_uptodate(sbh);
2888 * Have we just finished recovery? If so, and if we are mounting (or
2889 * remounting) the filesystem readonly, then we will end up with a
2890 * consistent fs on disk. Record that fact.
2892 static void ext4_mark_recovery_complete(struct super_block *sb,
2893 struct ext4_super_block *es)
2895 journal_t *journal = EXT4_SB(sb)->s_journal;
2897 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2898 BUG_ON(journal != NULL);
2901 jbd2_journal_lock_updates(journal);
2902 if (jbd2_journal_flush(journal) < 0)
2906 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2907 sb->s_flags & MS_RDONLY) {
2908 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2910 ext4_commit_super(sb, es, 1);
2915 jbd2_journal_unlock_updates(journal);
2919 * If we are mounting (or read-write remounting) a filesystem whose journal
2920 * has recorded an error from a previous lifetime, move that error to the
2921 * main filesystem now.
2923 static void ext4_clear_journal_err(struct super_block *sb,
2924 struct ext4_super_block *es)
2930 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2932 journal = EXT4_SB(sb)->s_journal;
2935 * Now check for any error status which may have been recorded in the
2936 * journal by a prior ext4_error() or ext4_abort()
2939 j_errno = jbd2_journal_errno(journal);
2943 errstr = ext4_decode_error(sb, j_errno, nbuf);
2944 ext4_warning(sb, __func__, "Filesystem error recorded "
2945 "from previous mount: %s", errstr);
2946 ext4_warning(sb, __func__, "Marking fs in need of "
2947 "filesystem check.");
2949 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2950 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2951 ext4_commit_super(sb, es, 1);
2953 jbd2_journal_clear_err(journal);
2958 * Force the running and committing transactions to commit,
2959 * and wait on the commit.
2961 int ext4_force_commit(struct super_block *sb)
2966 if (sb->s_flags & MS_RDONLY)
2969 journal = EXT4_SB(sb)->s_journal;
2972 ret = ext4_journal_force_commit(journal);
2979 * Ext4 always journals updates to the superblock itself, so we don't
2980 * have to propagate any other updates to the superblock on disk at this
2981 * point. (We can probably nuke this function altogether, and remove
2982 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
2984 static void ext4_write_super(struct super_block *sb)
2986 if (EXT4_SB(sb)->s_journal) {
2987 if (mutex_trylock(&sb->s_lock) != 0)
2991 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2995 static int ext4_sync_fs(struct super_block *sb, int wait)
2999 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
3001 if (EXT4_SB(sb)->s_journal) {
3003 ret = ext4_force_commit(sb);
3005 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3007 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3013 * LVM calls this function before a (read-only) snapshot is created. This
3014 * gives us a chance to flush the journal completely and mark the fs clean.
3016 static void ext4_write_super_lockfs(struct super_block *sb)
3020 if (!(sb->s_flags & MS_RDONLY)) {
3021 journal_t *journal = EXT4_SB(sb)->s_journal;
3024 /* Now we set up the journal barrier. */
3025 jbd2_journal_lock_updates(journal);
3028 * We don't want to clear needs_recovery flag when we
3029 * failed to flush the journal.
3031 if (jbd2_journal_flush(journal) < 0)
3035 /* Journal blocked and flushed, clear needs_recovery flag. */
3036 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3037 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3042 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3043 * flag here, even though the filesystem is not technically dirty yet.
3045 static void ext4_unlockfs(struct super_block *sb)
3047 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
3049 /* Reser the needs_recovery flag before the fs is unlocked. */
3050 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3051 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3053 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3057 static int ext4_remount(struct super_block *sb, int *flags, char *data)
3059 struct ext4_super_block *es;
3060 struct ext4_sb_info *sbi = EXT4_SB(sb);
3061 ext4_fsblk_t n_blocks_count = 0;
3062 unsigned long old_sb_flags;
3063 struct ext4_mount_options old_opts;
3070 /* Store the original options */
3071 old_sb_flags = sb->s_flags;
3072 old_opts.s_mount_opt = sbi->s_mount_opt;
3073 old_opts.s_resuid = sbi->s_resuid;
3074 old_opts.s_resgid = sbi->s_resgid;
3075 old_opts.s_commit_interval = sbi->s_commit_interval;
3076 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3077 old_opts.s_max_batch_time = sbi->s_max_batch_time;
3079 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3080 for (i = 0; i < MAXQUOTAS; i++)
3081 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3085 * Allow the "check" option to be passed as a remount option.
3087 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3092 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
3093 ext4_abort(sb, __func__, "Abort forced by user");
3095 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3096 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3101 ext4_init_journal_params(sb, sbi->s_journal);
3103 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3104 n_blocks_count > ext4_blocks_count(es)) {
3105 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3110 if (*flags & MS_RDONLY) {
3112 * First of all, the unconditional stuff we have to do
3113 * to disable replay of the journal when we next remount
3115 sb->s_flags |= MS_RDONLY;
3118 * OK, test if we are remounting a valid rw partition
3119 * readonly, and if so set the rdonly flag and then
3120 * mark the partition as valid again.
3122 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3123 (sbi->s_mount_state & EXT4_VALID_FS))
3124 es->s_state = cpu_to_le16(sbi->s_mount_state);
3127 * We have to unlock super so that we can wait for
3130 if (sbi->s_journal) {
3132 ext4_mark_recovery_complete(sb, es);
3137 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3138 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3139 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3140 "remount RDWR because of unsupported "
3141 "optional features (%x).\n", sb->s_id,
3142 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3143 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3149 * Make sure the group descriptor checksums
3150 * are sane. If they aren't, refuse to
3153 for (g = 0; g < sbi->s_groups_count; g++) {
3154 struct ext4_group_desc *gdp =
3155 ext4_get_group_desc(sb, g, NULL);
3157 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3159 "EXT4-fs: ext4_remount: "
3160 "Checksum for group %u failed (%u!=%u)\n",
3161 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3162 le16_to_cpu(gdp->bg_checksum));
3169 * If we have an unprocessed orphan list hanging
3170 * around from a previously readonly bdev mount,
3171 * require a full umount/remount for now.
3173 if (es->s_last_orphan) {
3174 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3175 "remount RDWR because of unprocessed "
3176 "orphan inode list. Please "
3177 "umount/remount instead.\n",
3184 * Mounting a RDONLY partition read-write, so reread
3185 * and store the current valid flag. (It may have
3186 * been changed by e2fsck since we originally mounted
3190 ext4_clear_journal_err(sb, es);
3191 sbi->s_mount_state = le16_to_cpu(es->s_state);
3192 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3194 if (!ext4_setup_super(sb, es, 0))
3195 sb->s_flags &= ~MS_RDONLY;
3198 if (sbi->s_journal == NULL)
3199 ext4_commit_super(sb, es, 1);
3202 /* Release old quota file names */
3203 for (i = 0; i < MAXQUOTAS; i++)
3204 if (old_opts.s_qf_names[i] &&
3205 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3206 kfree(old_opts.s_qf_names[i]);
3210 sb->s_flags = old_sb_flags;
3211 sbi->s_mount_opt = old_opts.s_mount_opt;
3212 sbi->s_resuid = old_opts.s_resuid;
3213 sbi->s_resgid = old_opts.s_resgid;
3214 sbi->s_commit_interval = old_opts.s_commit_interval;
3215 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3216 sbi->s_max_batch_time = old_opts.s_max_batch_time;
3218 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3219 for (i = 0; i < MAXQUOTAS; i++) {
3220 if (sbi->s_qf_names[i] &&
3221 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3222 kfree(sbi->s_qf_names[i]);
3223 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3229 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3231 struct super_block *sb = dentry->d_sb;
3232 struct ext4_sb_info *sbi = EXT4_SB(sb);
3233 struct ext4_super_block *es = sbi->s_es;
3236 if (test_opt(sb, MINIX_DF)) {
3237 sbi->s_overhead_last = 0;
3238 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3239 ext4_group_t ngroups = sbi->s_groups_count, i;
3240 ext4_fsblk_t overhead = 0;
3244 * Compute the overhead (FS structures). This is constant
3245 * for a given filesystem unless the number of block groups
3246 * changes so we cache the previous value until it does.
3250 * All of the blocks before first_data_block are
3253 overhead = le32_to_cpu(es->s_first_data_block);
3256 * Add the overhead attributed to the superblock and
3257 * block group descriptors. If the sparse superblocks
3258 * feature is turned on, then not all groups have this.
3260 for (i = 0; i < ngroups; i++) {
3261 overhead += ext4_bg_has_super(sb, i) +
3262 ext4_bg_num_gdb(sb, i);
3267 * Every block group has an inode bitmap, a block
3268 * bitmap, and an inode table.
3270 overhead += ngroups * (2 + sbi->s_itb_per_group);
3271 sbi->s_overhead_last = overhead;
3273 sbi->s_blocks_last = ext4_blocks_count(es);
3276 buf->f_type = EXT4_SUPER_MAGIC;
3277 buf->f_bsize = sb->s_blocksize;
3278 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3279 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3280 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3281 ext4_free_blocks_count_set(es, buf->f_bfree);
3282 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3283 if (buf->f_bfree < ext4_r_blocks_count(es))
3285 buf->f_files = le32_to_cpu(es->s_inodes_count);
3286 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3287 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3288 buf->f_namelen = EXT4_NAME_LEN;
3289 fsid = le64_to_cpup((void *)es->s_uuid) ^
3290 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3291 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3292 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3296 /* Helper function for writing quotas on sync - we need to start transaction before quota file
3297 * is locked for write. Otherwise the are possible deadlocks:
3298 * Process 1 Process 2
3299 * ext4_create() quota_sync()
3300 * jbd2_journal_start() write_dquot()
3301 * DQUOT_INIT() down(dqio_mutex)
3302 * down(dqio_mutex) jbd2_journal_start()
3308 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3310 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3313 static int ext4_dquot_initialize(struct inode *inode, int type)
3318 /* We may create quota structure so we need to reserve enough blocks */
3319 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3321 return PTR_ERR(handle);
3322 ret = dquot_initialize(inode, type);
3323 err = ext4_journal_stop(handle);
3329 static int ext4_dquot_drop(struct inode *inode)
3334 /* We may delete quota structure so we need to reserve enough blocks */
3335 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3336 if (IS_ERR(handle)) {
3338 * We call dquot_drop() anyway to at least release references
3339 * to quota structures so that umount does not hang.
3342 return PTR_ERR(handle);
3344 ret = dquot_drop(inode);
3345 err = ext4_journal_stop(handle);
3351 static int ext4_write_dquot(struct dquot *dquot)
3355 struct inode *inode;
3357 inode = dquot_to_inode(dquot);
3358 handle = ext4_journal_start(inode,
3359 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3361 return PTR_ERR(handle);
3362 ret = dquot_commit(dquot);
3363 err = ext4_journal_stop(handle);
3369 static int ext4_acquire_dquot(struct dquot *dquot)
3374 handle = ext4_journal_start(dquot_to_inode(dquot),
3375 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3377 return PTR_ERR(handle);
3378 ret = dquot_acquire(dquot);
3379 err = ext4_journal_stop(handle);
3385 static int ext4_release_dquot(struct dquot *dquot)
3390 handle = ext4_journal_start(dquot_to_inode(dquot),
3391 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3392 if (IS_ERR(handle)) {
3393 /* Release dquot anyway to avoid endless cycle in dqput() */
3394 dquot_release(dquot);
3395 return PTR_ERR(handle);
3397 ret = dquot_release(dquot);
3398 err = ext4_journal_stop(handle);
3404 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3406 /* Are we journaling quotas? */
3407 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3408 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3409 dquot_mark_dquot_dirty(dquot);
3410 return ext4_write_dquot(dquot);
3412 return dquot_mark_dquot_dirty(dquot);
3416 static int ext4_write_info(struct super_block *sb, int type)
3421 /* Data block + inode block */
3422 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3424 return PTR_ERR(handle);
3425 ret = dquot_commit_info(sb, type);
3426 err = ext4_journal_stop(handle);
3433 * Turn on quotas during mount time - we need to find
3434 * the quota file and such...
3436 static int ext4_quota_on_mount(struct super_block *sb, int type)
3438 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3439 EXT4_SB(sb)->s_jquota_fmt, type);
3443 * Standard function to be called on quota_on
3445 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3446 char *name, int remount)
3451 if (!test_opt(sb, QUOTA))
3453 /* When remounting, no checks are needed and in fact, name is NULL */
3455 return vfs_quota_on(sb, type, format_id, name, remount);
3457 err = kern_path(name, LOOKUP_FOLLOW, &path);
3461 /* Quotafile not on the same filesystem? */
3462 if (path.mnt->mnt_sb != sb) {
3466 /* Journaling quota? */
3467 if (EXT4_SB(sb)->s_qf_names[type]) {
3468 /* Quotafile not in fs root? */
3469 if (path.dentry->d_parent != sb->s_root)
3471 "EXT4-fs: Quota file not on filesystem root. "
3472 "Journaled quota will not work.\n");
3476 * When we journal data on quota file, we have to flush journal to see
3477 * all updates to the file when we bypass pagecache...
3479 if (EXT4_SB(sb)->s_journal &&
3480 ext4_should_journal_data(path.dentry->d_inode)) {
3482 * We don't need to lock updates but journal_flush() could
3483 * otherwise be livelocked...
3485 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3486 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3487 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3494 err = vfs_quota_on_path(sb, type, format_id, &path);
3499 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3500 * acquiring the locks... As quota files are never truncated and quota code
3501 * itself serializes the operations (and noone else should touch the files)
3502 * we don't have to be afraid of races */
3503 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3504 size_t len, loff_t off)
3506 struct inode *inode = sb_dqopt(sb)->files[type];
3507 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3509 int offset = off & (sb->s_blocksize - 1);
3512 struct buffer_head *bh;
3513 loff_t i_size = i_size_read(inode);
3517 if (off+len > i_size)
3520 while (toread > 0) {
3521 tocopy = sb->s_blocksize - offset < toread ?
3522 sb->s_blocksize - offset : toread;
3523 bh = ext4_bread(NULL, inode, blk, 0, &err);
3526 if (!bh) /* A hole? */
3527 memset(data, 0, tocopy);
3529 memcpy(data, bh->b_data+offset, tocopy);
3539 /* Write to quotafile (we know the transaction is already started and has
3540 * enough credits) */
3541 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3542 const char *data, size_t len, loff_t off)
3544 struct inode *inode = sb_dqopt(sb)->files[type];
3545 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3547 int offset = off & (sb->s_blocksize - 1);
3549 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3550 size_t towrite = len;
3551 struct buffer_head *bh;
3552 handle_t *handle = journal_current_handle();
3554 if (EXT4_SB(sb)->s_journal && !handle) {
3555 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
3556 " cancelled because transaction is not started.\n",
3557 (unsigned long long)off, (unsigned long long)len);
3560 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3561 while (towrite > 0) {
3562 tocopy = sb->s_blocksize - offset < towrite ?
3563 sb->s_blocksize - offset : towrite;
3564 bh = ext4_bread(handle, inode, blk, 1, &err);
3567 if (journal_quota) {
3568 err = ext4_journal_get_write_access(handle, bh);
3575 memcpy(bh->b_data+offset, data, tocopy);
3576 flush_dcache_page(bh->b_page);
3579 err = ext4_handle_dirty_metadata(handle, NULL, bh);
3581 /* Always do at least ordered writes for quotas */
3582 err = ext4_jbd2_file_inode(handle, inode);
3583 mark_buffer_dirty(bh);
3594 if (len == towrite) {
3595 mutex_unlock(&inode->i_mutex);
3598 if (inode->i_size < off+len-towrite) {
3599 i_size_write(inode, off+len-towrite);
3600 EXT4_I(inode)->i_disksize = inode->i_size;
3602 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3603 ext4_mark_inode_dirty(handle, inode);
3604 mutex_unlock(&inode->i_mutex);
3605 return len - towrite;
3610 static int ext4_get_sb(struct file_system_type *fs_type,
3611 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3613 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3616 #ifdef CONFIG_PROC_FS
3617 static int ext4_ui_proc_show(struct seq_file *m, void *v)
3619 unsigned int *p = m->private;
3621 seq_printf(m, "%u\n", *p);
3625 static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3627 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3630 static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3631 size_t cnt, loff_t *ppos)
3633 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
3636 if (cnt >= sizeof(str))
3638 if (copy_from_user(str, buf, cnt))
3641 *p = simple_strtoul(str, NULL, 0);
3645 const struct file_operations ext4_ui_proc_fops = {
3646 .owner = THIS_MODULE,
3647 .open = ext4_ui_proc_open,
3649 .llseek = seq_lseek,
3650 .release = single_release,
3651 .write = ext4_ui_proc_write,
3655 static struct file_system_type ext4_fs_type = {
3656 .owner = THIS_MODULE,
3658 .get_sb = ext4_get_sb,
3659 .kill_sb = kill_block_super,
3660 .fs_flags = FS_REQUIRES_DEV,
3663 #ifdef CONFIG_EXT4DEV_COMPAT
3664 static int ext4dev_get_sb(struct file_system_type *fs_type,
3665 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3667 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3668 "to mount using ext4\n");
3669 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3670 "will go away by 2.6.31\n");
3671 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3674 static struct file_system_type ext4dev_fs_type = {
3675 .owner = THIS_MODULE,
3677 .get_sb = ext4dev_get_sb,
3678 .kill_sb = kill_block_super,
3679 .fs_flags = FS_REQUIRES_DEV,
3681 MODULE_ALIAS("ext4dev");
3684 static int __init init_ext4_fs(void)
3688 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3689 err = init_ext4_mballoc();
3693 err = init_ext4_xattr();
3696 err = init_inodecache();
3699 err = register_filesystem(&ext4_fs_type);
3702 #ifdef CONFIG_EXT4DEV_COMPAT
3703 err = register_filesystem(&ext4dev_fs_type);
3705 unregister_filesystem(&ext4_fs_type);
3711 destroy_inodecache();
3715 exit_ext4_mballoc();
3719 static void __exit exit_ext4_fs(void)
3721 unregister_filesystem(&ext4_fs_type);
3722 #ifdef CONFIG_EXT4DEV_COMPAT
3723 unregister_filesystem(&ext4dev_fs_type);
3725 destroy_inodecache();
3727 exit_ext4_mballoc();
3728 remove_proc_entry("fs/ext4", NULL);
3731 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3732 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3733 MODULE_LICENSE("GPL");
3734 module_init(init_ext4_fs)
3735 module_exit(exit_ext4_fs)