2 * linux/fs/ext3/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/config.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
23 #include <linux/time.h>
24 #include <linux/jbd.h>
25 #include <linux/ext3_fs.h>
26 #include <linux/ext3_jbd.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/blkdev.h>
30 #include <linux/parser.h>
31 #include <linux/smp_lock.h>
32 #include <linux/buffer_head.h>
33 #include <linux/vfs.h>
34 #include <linux/random.h>
35 #include <asm/uaccess.h>
39 static int ext3_load_journal(struct super_block *, struct ext3_super_block *);
40 static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
42 static void ext3_commit_super (struct super_block * sb,
43 struct ext3_super_block * es,
45 static void ext3_mark_recovery_complete(struct super_block * sb,
46 struct ext3_super_block * es);
47 static void ext3_clear_journal_err(struct super_block * sb,
48 struct ext3_super_block * es);
49 static int ext3_sync_fs(struct super_block *sb, int wait);
52 * Wrappers for journal_start/end.
54 * The only special thing we need to do here is to make sure that all
55 * journal_end calls result in the superblock being marked dirty, so
56 * that sync() will call the filesystem's write_super callback if
59 handle_t *ext3_journal_start(struct inode *inode, int nblocks)
63 if (inode->i_sb->s_flags & MS_RDONLY)
64 return ERR_PTR(-EROFS);
66 /* Special case here: if the journal has aborted behind our
67 * backs (eg. EIO in the commit thread), then we still need to
68 * take the FS itself readonly cleanly. */
69 journal = EXT3_JOURNAL(inode);
70 if (is_journal_aborted(journal)) {
71 ext3_abort(inode->i_sb, __FUNCTION__,
72 "Detected aborted journal");
73 return ERR_PTR(-EROFS);
76 return journal_start(journal, nblocks);
80 * The only special thing we need to do here is to make sure that all
81 * journal_stop calls result in the superblock being marked dirty, so
82 * that sync() will call the filesystem's write_super callback if
85 int __ext3_journal_stop(const char *where, handle_t *handle)
87 struct super_block *sb;
91 sb = handle->h_transaction->t_journal->j_private;
93 rc = journal_stop(handle);
98 __ext3_std_error(sb, where, err);
102 void ext3_journal_abort_handle(const char *caller, const char *err_fn,
103 struct buffer_head *bh, handle_t *handle, int err)
106 const char *errstr = ext3_decode_error(NULL, err, nbuf);
108 printk(KERN_ERR "%s: aborting transaction: %s in %s",
109 caller, errstr, err_fn);
112 BUFFER_TRACE(bh, "abort");
113 journal_abort_handle(handle);
118 static char error_buf[1024];
120 /* Deal with the reporting of failure conditions on a filesystem such as
121 * inconsistencies detected or read IO failures.
123 * On ext2, we can store the error state of the filesystem in the
124 * superblock. That is not possible on ext3, because we may have other
125 * write ordering constraints on the superblock which prevent us from
126 * writing it out straight away; and given that the journal is about to
127 * be aborted, we can't rely on the current, or future, transactions to
128 * write out the superblock safely.
130 * We'll just use the journal_abort() error code to record an error in
131 * the journal instead. On recovery, the journal will compain about
132 * that error until we've noted it down and cleared it.
135 static void ext3_handle_error(struct super_block *sb)
137 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
139 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
140 es->s_state |= cpu_to_le32(EXT3_ERROR_FS);
142 if (sb->s_flags & MS_RDONLY)
145 if (test_opt (sb, ERRORS_PANIC))
146 panic ("EXT3-fs (device %s): panic forced after error\n",
148 if (test_opt (sb, ERRORS_RO)) {
149 printk (KERN_CRIT "Remounting filesystem read-only\n");
150 sb->s_flags |= MS_RDONLY;
152 journal_t *journal = EXT3_SB(sb)->s_journal;
154 EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
156 journal_abort(journal, -EIO);
158 ext3_commit_super(sb, es, 1);
161 void ext3_error (struct super_block * sb, const char * function,
162 const char * fmt, ...)
166 va_start (args, fmt);
167 vsprintf (error_buf, fmt, args);
170 printk (KERN_CRIT "EXT3-fs error (device %s): %s: %s\n",
171 sb->s_id, function, error_buf);
173 ext3_handle_error(sb);
176 const char *ext3_decode_error(struct super_block * sb, int errno, char nbuf[16])
182 errstr = "IO failure";
185 errstr = "Out of memory";
188 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
189 errstr = "Journal has aborted";
191 errstr = "Readonly filesystem";
194 /* If the caller passed in an extra buffer for unknown
195 * errors, textualise them now. Else we just return
198 /* Check for truncated error codes... */
199 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
208 /* __ext3_std_error decodes expected errors from journaling functions
209 * automatically and invokes the appropriate error response. */
211 void __ext3_std_error (struct super_block * sb, const char * function,
215 const char *errstr = ext3_decode_error(sb, errno, nbuf);
217 printk (KERN_CRIT "EXT3-fs error (device %s) in %s: %s\n",
218 sb->s_id, function, errstr);
220 ext3_handle_error(sb);
224 * ext3_abort is a much stronger failure handler than ext3_error. The
225 * abort function may be used to deal with unrecoverable failures such
226 * as journal IO errors or ENOMEM at a critical moment in log management.
228 * We unconditionally force the filesystem into an ABORT|READONLY state,
229 * unless the error response on the fs has been set to panic in which
230 * case we take the easy way out and panic immediately.
233 void ext3_abort (struct super_block * sb, const char * function,
234 const char * fmt, ...)
238 printk (KERN_CRIT "ext3_abort called.\n");
240 va_start (args, fmt);
241 vsprintf (error_buf, fmt, args);
244 if (test_opt (sb, ERRORS_PANIC))
245 panic ("EXT3-fs panic (device %s): %s: %s\n",
246 sb->s_id, function, error_buf);
248 printk (KERN_CRIT "EXT3-fs abort (device %s): %s: %s\n",
249 sb->s_id, function, error_buf);
251 if (sb->s_flags & MS_RDONLY)
254 printk (KERN_CRIT "Remounting filesystem read-only\n");
255 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
256 sb->s_flags |= MS_RDONLY;
257 EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
258 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
261 /* Deal with the reporting of failure conditions while running, such as
262 * inconsistencies in operation or invalid system states.
264 * Use ext3_error() for cases of invalid filesystem states, as that will
265 * record an error on disk and force a filesystem check on the next boot.
267 NORET_TYPE void ext3_panic (struct super_block * sb, const char * function,
268 const char * fmt, ...)
272 va_start (args, fmt);
273 vsprintf (error_buf, fmt, args);
276 /* this is to prevent panic from syncing this filesystem */
277 /* AKPM: is this sufficient? */
278 sb->s_flags |= MS_RDONLY;
279 panic ("EXT3-fs panic (device %s): %s: %s\n",
280 sb->s_id, function, error_buf);
283 void ext3_warning (struct super_block * sb, const char * function,
284 const char * fmt, ...)
288 va_start (args, fmt);
289 vsprintf (error_buf, fmt, args);
291 printk (KERN_WARNING "EXT3-fs warning (device %s): %s: %s\n",
292 sb->s_id, function, error_buf);
295 void ext3_update_dynamic_rev(struct super_block *sb)
297 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
299 if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
302 ext3_warning(sb, __FUNCTION__,
303 "updating to rev %d because of new feature flag, "
304 "running e2fsck is recommended",
307 es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
308 es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
309 es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
310 /* leave es->s_feature_*compat flags alone */
311 /* es->s_uuid will be set by e2fsck if empty */
314 * The rest of the superblock fields should be zero, and if not it
315 * means they are likely already in use, so leave them alone. We
316 * can leave it up to e2fsck to clean up any inconsistencies there.
321 * Open the external journal device
323 static struct block_device *ext3_blkdev_get(dev_t dev)
325 struct block_device *bdev;
326 char b[BDEVNAME_SIZE];
328 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE, BDEV_FS);
334 printk(KERN_ERR "EXT3: failed to open journal device %s: %ld\n",
335 __bdevname(dev, b), PTR_ERR(bdev));
340 * Release the journal device
342 static int ext3_blkdev_put(struct block_device *bdev)
345 return blkdev_put(bdev, BDEV_FS);
348 static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
350 struct block_device *bdev;
353 bdev = sbi->journal_bdev;
355 ret = ext3_blkdev_put(bdev);
356 sbi->journal_bdev = 0;
361 static inline struct inode *orphan_list_entry(struct list_head *l)
363 return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
366 static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
370 printk(KERN_ERR "sb orphan head is %d\n",
371 le32_to_cpu(sbi->s_es->s_last_orphan));
373 printk(KERN_ERR "sb_info orphan list:\n");
374 list_for_each(l, &sbi->s_orphan) {
375 struct inode *inode = orphan_list_entry(l);
377 "inode %s:%ld at %p: mode %o, nlink %d, next %d\n",
378 inode->i_sb->s_id, inode->i_ino, inode,
379 inode->i_mode, inode->i_nlink,
380 le32_to_cpu(NEXT_ORPHAN(inode)));
384 void ext3_put_super (struct super_block * sb)
386 struct ext3_sb_info *sbi = EXT3_SB(sb);
387 struct ext3_super_block *es = sbi->s_es;
390 ext3_xattr_put_super(sb);
391 journal_destroy(sbi->s_journal);
392 if (!(sb->s_flags & MS_RDONLY)) {
393 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
394 es->s_state = le16_to_cpu(sbi->s_mount_state);
395 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
396 mark_buffer_dirty(sbi->s_sbh);
397 ext3_commit_super(sb, es, 1);
400 for (i = 0; i < sbi->s_gdb_count; i++)
401 brelse(sbi->s_group_desc[i]);
402 kfree(sbi->s_group_desc);
406 /* Debugging code just in case the in-memory inode orphan list
407 * isn't empty. The on-disk one can be non-empty if we've
408 * detected an error and taken the fs readonly, but the
409 * in-memory list had better be clean by this point. */
410 if (!list_empty(&sbi->s_orphan))
411 dump_orphan_list(sb, sbi);
412 J_ASSERT(list_empty(&sbi->s_orphan));
414 invalidate_bdev(sb->s_bdev, 0);
415 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
417 * Invalidate the journal device's buffers. We don't want them
418 * floating about in memory - the physical journal device may
419 * hotswapped, and it breaks the `ro-after' testing code.
421 sync_blockdev(sbi->journal_bdev);
422 invalidate_bdev(sbi->journal_bdev, 0);
423 ext3_blkdev_remove(sbi);
425 sb->s_fs_info = NULL;
430 static kmem_cache_t *ext3_inode_cachep;
433 * Called inside transaction, so use GFP_NOFS
435 static struct inode *ext3_alloc_inode(struct super_block *sb)
437 struct ext3_inode_info *ei;
439 ei = kmem_cache_alloc(ext3_inode_cachep, SLAB_NOFS);
442 #ifdef CONFIG_EXT3_FS_POSIX_ACL
443 ei->i_acl = EXT3_ACL_NOT_CACHED;
444 ei->i_default_acl = EXT3_ACL_NOT_CACHED;
446 ei->vfs_inode.i_version = 1;
447 return &ei->vfs_inode;
450 static void ext3_destroy_inode(struct inode *inode)
452 kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
455 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
457 struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
459 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
460 SLAB_CTOR_CONSTRUCTOR) {
461 INIT_LIST_HEAD(&ei->i_orphan);
462 #ifdef CONFIG_EXT3_FS_XATTR
463 init_rwsem(&ei->xattr_sem);
465 init_MUTEX(&ei->truncate_sem);
466 inode_init_once(&ei->vfs_inode);
470 static int init_inodecache(void)
472 ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
473 sizeof(struct ext3_inode_info),
474 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
476 if (ext3_inode_cachep == NULL)
481 static void destroy_inodecache(void)
483 if (kmem_cache_destroy(ext3_inode_cachep))
484 printk(KERN_INFO "ext3_inode_cache: not all structures were freed\n");
487 #ifdef CONFIG_EXT3_FS_POSIX_ACL
489 static void ext3_clear_inode(struct inode *inode)
491 if (EXT3_I(inode)->i_acl &&
492 EXT3_I(inode)->i_acl != EXT3_ACL_NOT_CACHED) {
493 posix_acl_release(EXT3_I(inode)->i_acl);
494 EXT3_I(inode)->i_acl = EXT3_ACL_NOT_CACHED;
496 if (EXT3_I(inode)->i_default_acl &&
497 EXT3_I(inode)->i_default_acl != EXT3_ACL_NOT_CACHED) {
498 posix_acl_release(EXT3_I(inode)->i_default_acl);
499 EXT3_I(inode)->i_default_acl = EXT3_ACL_NOT_CACHED;
504 # define ext3_clear_inode NULL
507 static struct dquot_operations ext3_qops;
509 static struct super_operations ext3_sops = {
510 .alloc_inode = ext3_alloc_inode,
511 .destroy_inode = ext3_destroy_inode,
512 .read_inode = ext3_read_inode,
513 .write_inode = ext3_write_inode,
514 .dirty_inode = ext3_dirty_inode,
515 .put_inode = ext3_put_inode,
516 .delete_inode = ext3_delete_inode,
517 .put_super = ext3_put_super,
518 .write_super = ext3_write_super,
519 .sync_fs = ext3_sync_fs,
520 .write_super_lockfs = ext3_write_super_lockfs,
521 .unlockfs = ext3_unlockfs,
522 .statfs = ext3_statfs,
523 .remount_fs = ext3_remount,
524 .clear_inode = ext3_clear_inode,
527 struct dentry *ext3_get_parent(struct dentry *child);
528 static struct export_operations ext3_export_ops = {
529 .get_parent = ext3_get_parent,
533 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
534 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
535 Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
536 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, Opt_noload,
537 Opt_commit, Opt_journal_update, Opt_journal_inum,
538 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
542 static match_table_t tokens = {
543 {Opt_bsd_df, "bsddf"},
544 {Opt_minix_df, "minixdf"},
545 {Opt_grpid, "grpid"},
546 {Opt_grpid, "bsdgroups"},
547 {Opt_nogrpid, "nogrpid"},
548 {Opt_nogrpid, "sysvgroups"},
549 {Opt_resgid, "resgid=%u"},
550 {Opt_resuid, "resuid=%u"},
552 {Opt_err_cont, "errors=continue"},
553 {Opt_err_panic, "errors=panic"},
554 {Opt_err_ro, "errors=remount-ro"},
555 {Opt_nouid32, "nouid32"},
556 {Opt_nocheck, "nocheck"},
557 {Opt_nocheck, "check=none"},
558 {Opt_check, "check"},
559 {Opt_debug, "debug"},
560 {Opt_oldalloc, "oldalloc"},
561 {Opt_orlov, "orlov"},
562 {Opt_user_xattr, "user_xattr"},
563 {Opt_nouser_xattr, "nouser_xattr"},
565 {Opt_noacl, "noacl"},
566 {Opt_noload, "noload"},
567 {Opt_commit, "commit=%u"},
568 {Opt_journal_update, "journal=update"},
569 {Opt_journal_inum, "journal=%u"},
570 {Opt_abort, "abort"},
571 {Opt_data_journal, "data=journal"},
572 {Opt_data_ordered, "data=ordered"},
573 {Opt_data_writeback, "data=writeback"},
574 {Opt_ignore, "grpquota"},
575 {Opt_ignore, "noquota"},
576 {Opt_ignore, "quota"},
577 {Opt_ignore, "usrquota"},
581 static unsigned long get_sb_block(void **data)
583 unsigned long sb_block;
584 char *options = (char *) *data;
586 if (!options || strncmp(options, "sb=", 3) != 0)
587 return 1; /* Default location */
589 sb_block = simple_strtoul(options, &options, 0);
590 if (*options && *options != ',') {
591 printk("EXT3-fs: Invalid sb specification: %s\n",
597 *data = (void *) options;
601 static int parse_options (char * options, struct ext3_sb_info *sbi,
602 unsigned long * inum, int is_remount)
605 substring_t args[MAX_OPT_ARGS];
612 while ((p = strsep (&options, ",")) != NULL) {
617 token = match_token(p, tokens, args);
620 clear_opt (sbi->s_mount_opt, MINIX_DF);
623 set_opt (sbi->s_mount_opt, MINIX_DF);
626 set_opt (sbi->s_mount_opt, GRPID);
629 clear_opt (sbi->s_mount_opt, GRPID);
632 if (match_int(&args[0], &option))
634 sbi->s_resuid = option;
637 if (match_int(&args[0], &option))
639 sbi->s_resgid = option;
642 /* handled by get_sb_block() instead of here */
643 /* *sb_block = match_int(&args[0]); */
646 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
647 clear_opt (sbi->s_mount_opt, ERRORS_RO);
648 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
651 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
652 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
653 set_opt (sbi->s_mount_opt, ERRORS_RO);
656 clear_opt (sbi->s_mount_opt, ERRORS_RO);
657 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
658 set_opt (sbi->s_mount_opt, ERRORS_CONT);
661 set_opt (sbi->s_mount_opt, NO_UID32);
664 #ifdef CONFIG_EXT3_CHECK
665 set_opt (sbi->s_mount_opt, CHECK);
668 "EXT3 Check option not supported\n");
672 clear_opt (sbi->s_mount_opt, CHECK);
675 set_opt (sbi->s_mount_opt, DEBUG);
678 set_opt (sbi->s_mount_opt, OLDALLOC);
681 clear_opt (sbi->s_mount_opt, OLDALLOC);
683 #ifdef CONFIG_EXT3_FS_XATTR
685 set_opt (sbi->s_mount_opt, XATTR_USER);
687 case Opt_nouser_xattr:
688 clear_opt (sbi->s_mount_opt, XATTR_USER);
692 case Opt_nouser_xattr:
693 printk("EXT3 (no)user_xattr options not supported\n");
696 #ifdef CONFIG_EXT3_FS_POSIX_ACL
698 set_opt(sbi->s_mount_opt, POSIX_ACL);
701 clear_opt(sbi->s_mount_opt, POSIX_ACL);
706 printk("EXT3 (no)acl options not supported\n");
709 case Opt_journal_update:
711 /* Eventually we will want to be able to create
712 a journal file here. For now, only allow the
713 user to specify an existing inode to be the
716 printk(KERN_ERR "EXT3-fs: cannot specify "
717 "journal on remount\n");
720 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
722 case Opt_journal_inum:
724 printk(KERN_ERR "EXT3-fs: cannot specify "
725 "journal on remount\n");
728 if (match_int(&args[0], &option))
733 set_opt (sbi->s_mount_opt, NOLOAD);
736 if (match_int(&args[0], &option))
738 sbi->s_commit_interval = HZ * option;
740 case Opt_data_journal:
741 data_opt = EXT3_MOUNT_JOURNAL_DATA;
743 case Opt_data_ordered:
744 data_opt = EXT3_MOUNT_ORDERED_DATA;
746 case Opt_data_writeback:
747 data_opt = EXT3_MOUNT_WRITEBACK_DATA;
750 if ((sbi->s_mount_opt & EXT3_MOUNT_DATA_FLAGS)
753 "EXT3-fs: cannot change data "
754 "mode on remount\n");
758 sbi->s_mount_opt &= ~EXT3_MOUNT_DATA_FLAGS;
759 sbi->s_mount_opt |= data_opt;
763 set_opt(sbi->s_mount_opt, ABORT);
769 "EXT3-fs: Unrecognized mount option \"%s\" "
770 "or missing value\n", p);
778 static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
781 struct ext3_sb_info *sbi = EXT3_SB(sb);
784 if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
785 printk (KERN_ERR "EXT3-fs warning: revision level too high, "
786 "forcing read-only mode\n");
791 if (!(sbi->s_mount_state & EXT3_VALID_FS))
792 printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
793 "running e2fsck is recommended\n");
794 else if ((sbi->s_mount_state & EXT3_ERROR_FS))
796 "EXT3-fs warning: mounting fs with errors, "
797 "running e2fsck is recommended\n");
798 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
799 le16_to_cpu(es->s_mnt_count) >=
800 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
802 "EXT3-fs warning: maximal mount count reached, "
803 "running e2fsck is recommended\n");
804 else if (le32_to_cpu(es->s_checkinterval) &&
805 (le32_to_cpu(es->s_lastcheck) +
806 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
808 "EXT3-fs warning: checktime reached, "
809 "running e2fsck is recommended\n");
811 /* @@@ We _will_ want to clear the valid bit if we find
812 inconsistencies, to force a fsck at reboot. But for
813 a plain journaled filesystem we can keep it set as
815 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
817 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
818 es->s_max_mnt_count =
819 (__s16) cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
820 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
821 es->s_mtime = cpu_to_le32(get_seconds());
822 ext3_update_dynamic_rev(sb);
823 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
825 ext3_commit_super(sb, es, 1);
826 if (test_opt(sb, DEBUG))
827 printk(KERN_INFO "[EXT3 FS bs=%lu, gc=%lu, "
828 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
831 EXT3_BLOCKS_PER_GROUP(sb),
832 EXT3_INODES_PER_GROUP(sb),
835 printk(KERN_INFO "EXT3 FS on %s, ", sb->s_id);
836 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
837 char b[BDEVNAME_SIZE];
839 printk("external journal on %s\n",
840 bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
842 printk("internal journal\n");
844 #ifdef CONFIG_EXT3_CHECK
845 if (test_opt (sb, CHECK)) {
846 ext3_check_blocks_bitmap (sb);
847 ext3_check_inodes_bitmap (sb);
853 static int ext3_check_descriptors (struct super_block * sb)
855 struct ext3_sb_info *sbi = EXT3_SB(sb);
856 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
857 struct ext3_group_desc * gdp = NULL;
861 ext3_debug ("Checking group descriptors");
863 for (i = 0; i < sbi->s_groups_count; i++)
865 if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
866 gdp = (struct ext3_group_desc *)
867 sbi->s_group_desc[desc_block++]->b_data;
868 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
869 le32_to_cpu(gdp->bg_block_bitmap) >=
870 block + EXT3_BLOCKS_PER_GROUP(sb))
872 ext3_error (sb, "ext3_check_descriptors",
873 "Block bitmap for group %d"
874 " not in group (block %lu)!",
876 le32_to_cpu(gdp->bg_block_bitmap));
879 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
880 le32_to_cpu(gdp->bg_inode_bitmap) >=
881 block + EXT3_BLOCKS_PER_GROUP(sb))
883 ext3_error (sb, "ext3_check_descriptors",
884 "Inode bitmap for group %d"
885 " not in group (block %lu)!",
887 le32_to_cpu(gdp->bg_inode_bitmap));
890 if (le32_to_cpu(gdp->bg_inode_table) < block ||
891 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
892 block + EXT3_BLOCKS_PER_GROUP(sb))
894 ext3_error (sb, "ext3_check_descriptors",
895 "Inode table for group %d"
896 " not in group (block %lu)!",
898 le32_to_cpu(gdp->bg_inode_table));
901 block += EXT3_BLOCKS_PER_GROUP(sb);
905 sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
906 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
911 /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
912 * the superblock) which were deleted from all directories, but held open by
913 * a process at the time of a crash. We walk the list and try to delete these
914 * inodes at recovery time (only with a read-write filesystem).
916 * In order to keep the orphan inode chain consistent during traversal (in
917 * case of crash during recovery), we link each inode into the superblock
918 * orphan list_head and handle it the same way as an inode deletion during
919 * normal operation (which journals the operations for us).
921 * We only do an iget() and an iput() on each inode, which is very safe if we
922 * accidentally point at an in-use or already deleted inode. The worst that
923 * can happen in this case is that we get a "bit already cleared" message from
924 * ext3_free_inode(). The only reason we would point at a wrong inode is if
925 * e2fsck was run on this filesystem, and it must have already done the orphan
926 * inode cleanup for us, so we can safely abort without any further action.
928 static void ext3_orphan_cleanup (struct super_block * sb,
929 struct ext3_super_block * es)
931 unsigned int s_flags = sb->s_flags;
932 int nr_orphans = 0, nr_truncates = 0;
933 if (!es->s_last_orphan) {
934 jbd_debug(4, "no orphan inodes to clean up\n");
938 if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
939 if (es->s_last_orphan)
940 jbd_debug(1, "Errors on filesystem, "
941 "clearing orphan list.\n");
942 es->s_last_orphan = 0;
943 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
947 if (s_flags & MS_RDONLY) {
948 printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
950 sb->s_flags &= ~MS_RDONLY;
953 while (es->s_last_orphan) {
957 ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
958 es->s_last_orphan = 0;
962 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
963 if (inode->i_nlink) {
965 "%s: truncating inode %ld to %Ld bytes\n",
966 __FUNCTION__, inode->i_ino, inode->i_size);
967 jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
968 inode->i_ino, inode->i_size);
969 ext3_truncate(inode);
973 "%s: deleting unreferenced inode %ld\n",
974 __FUNCTION__, inode->i_ino);
975 jbd_debug(2, "deleting unreferenced inode %ld\n",
979 iput(inode); /* The delete magic happens here! */
982 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
985 printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
986 sb->s_id, PLURAL(nr_orphans));
988 printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
989 sb->s_id, PLURAL(nr_truncates));
990 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
993 #define log2(n) ffz(~(n))
996 * Maximal file size. There is a direct, and {,double-,triple-}indirect
997 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
998 * We need to be 1 filesystem block less than the 2^32 sector limit.
1000 static loff_t ext3_max_size(int bits)
1002 loff_t res = EXT3_NDIR_BLOCKS;
1003 res += 1LL << (bits-2);
1004 res += 1LL << (2*(bits-2));
1005 res += 1LL << (3*(bits-2));
1007 if (res > (512LL << 32) - (1 << bits))
1008 res = (512LL << 32) - (1 << bits);
1012 static unsigned long descriptor_loc(struct super_block *sb,
1013 unsigned long logic_sb_block,
1016 struct ext3_sb_info *sbi = EXT3_SB(sb);
1017 unsigned long bg, first_data_block, first_meta_bg;
1020 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1021 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1023 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
1025 return (logic_sb_block + nr + 1);
1026 bg = sbi->s_desc_per_block * nr;
1027 if (ext3_bg_has_super(sb, bg))
1029 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
1033 static int ext3_fill_super (struct super_block *sb, void *data, int silent)
1035 struct buffer_head * bh;
1036 struct ext3_super_block *es = 0;
1037 struct ext3_sb_info *sbi;
1038 unsigned long sb_block = get_sb_block(&data);
1039 unsigned long block, logic_sb_block = 1;
1040 unsigned long offset = 0;
1041 unsigned long journal_inum = 0;
1042 unsigned long def_mount_opts;
1049 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
1052 sb->s_fs_info = sbi;
1053 memset(sbi, 0, sizeof(*sbi));
1054 sbi->s_mount_opt = 0;
1055 sbi->s_resuid = EXT3_DEF_RESUID;
1056 sbi->s_resgid = EXT3_DEF_RESGID;
1058 blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
1060 printk(KERN_ERR "EXT3-fs: unable to set blocksize\n");
1065 * The ext3 superblock will not be buffer aligned for other than 1kB
1066 * block sizes. We need to calculate the offset from buffer start.
1068 if (blocksize != EXT3_MIN_BLOCK_SIZE) {
1069 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1070 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1073 if (!(bh = sb_bread(sb, logic_sb_block))) {
1074 printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
1078 * Note: s_es must be initialized as soon as possible because
1079 * some ext3 macro-instructions depend on its value
1081 es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1083 sb->s_magic = le16_to_cpu(es->s_magic);
1084 if (sb->s_magic != EXT3_SUPER_MAGIC) {
1087 "VFS: Can't find ext3 filesystem on dev %s.\n",
1092 /* Set defaults before we parse the mount options */
1093 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1094 if (def_mount_opts & EXT3_DEFM_DEBUG)
1095 set_opt(sbi->s_mount_opt, DEBUG);
1096 if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
1097 set_opt(sbi->s_mount_opt, GRPID);
1098 if (def_mount_opts & EXT3_DEFM_UID16)
1099 set_opt(sbi->s_mount_opt, NO_UID32);
1100 if (def_mount_opts & EXT3_DEFM_XATTR_USER)
1101 set_opt(sbi->s_mount_opt, XATTR_USER);
1102 if (def_mount_opts & EXT3_DEFM_ACL)
1103 set_opt(sbi->s_mount_opt, POSIX_ACL);
1104 if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
1105 sbi->s_mount_opt |= EXT3_MOUNT_JOURNAL_DATA;
1106 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
1107 sbi->s_mount_opt |= EXT3_MOUNT_ORDERED_DATA;
1108 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
1109 sbi->s_mount_opt |= EXT3_MOUNT_WRITEBACK_DATA;
1111 if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
1112 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1113 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_RO)
1114 set_opt(sbi->s_mount_opt, ERRORS_RO);
1116 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1117 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1119 if (!parse_options ((char *) data, sbi, &journal_inum, 0))
1122 sb->s_flags |= MS_ONE_SECOND;
1123 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1124 ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1126 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
1127 (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
1128 EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1129 EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1131 "EXT3-fs warning: feature flags set on rev 0 fs, "
1132 "running e2fsck is recommended\n");
1134 * Check feature flags regardless of the revision level, since we
1135 * previously didn't change the revision level when setting the flags,
1136 * so there is a chance incompat flags are set on a rev 0 filesystem.
1138 if ((i = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))) {
1139 printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
1140 "unsupported optional features (%x).\n",
1144 if (!(sb->s_flags & MS_RDONLY) &&
1145 (i = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))){
1146 printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
1147 "unsupported optional features (%x).\n",
1151 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1153 if (blocksize < EXT3_MIN_BLOCK_SIZE ||
1154 blocksize > EXT3_MAX_BLOCK_SIZE) {
1156 "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
1157 blocksize, sb->s_id);
1161 hblock = bdev_hardsect_size(sb->s_bdev);
1162 if (sb->s_blocksize != blocksize) {
1164 * Make sure the blocksize for the filesystem is larger
1165 * than the hardware sectorsize for the machine.
1167 if (blocksize < hblock) {
1168 printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
1169 "device blocksize %d.\n", blocksize, hblock);
1174 sb_set_blocksize(sb, blocksize);
1175 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1176 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1177 bh = sb_bread(sb, logic_sb_block);
1180 "EXT3-fs: Can't read superblock on 2nd try.\n");
1183 es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
1185 if (es->s_magic != le16_to_cpu(EXT3_SUPER_MAGIC)) {
1187 "EXT3-fs: Magic mismatch, very weird !\n");
1192 sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1194 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1195 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1196 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1198 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1199 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1200 if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
1201 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1202 (sbi->s_inode_size > blocksize)) {
1204 "EXT3-fs: unsupported inode size: %d\n",
1209 sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1210 le32_to_cpu(es->s_log_frag_size);
1211 if (blocksize != sbi->s_frag_size) {
1213 "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
1214 sbi->s_frag_size, blocksize);
1217 sbi->s_frags_per_block = 1;
1218 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1219 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1220 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1221 sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1222 sbi->s_itb_per_group = sbi->s_inodes_per_group /sbi->s_inodes_per_block;
1223 sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1225 sbi->s_mount_state = le16_to_cpu(es->s_state);
1226 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
1227 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
1228 for (i=0; i < 4; i++)
1229 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1230 sbi->s_def_hash_version = es->s_def_hash_version;
1232 if (sbi->s_blocks_per_group > blocksize * 8) {
1234 "EXT3-fs: #blocks per group too big: %lu\n",
1235 sbi->s_blocks_per_group);
1238 if (sbi->s_frags_per_group > blocksize * 8) {
1240 "EXT3-fs: #fragments per group too big: %lu\n",
1241 sbi->s_frags_per_group);
1244 if (sbi->s_inodes_per_group > blocksize * 8) {
1246 "EXT3-fs: #inodes per group too big: %lu\n",
1247 sbi->s_inodes_per_group);
1251 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
1252 le32_to_cpu(es->s_first_data_block) +
1253 EXT3_BLOCKS_PER_GROUP(sb) - 1) /
1254 EXT3_BLOCKS_PER_GROUP(sb);
1255 db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
1256 EXT3_DESC_PER_BLOCK(sb);
1257 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1259 if (sbi->s_group_desc == NULL) {
1260 printk (KERN_ERR "EXT3-fs: not enough memory\n");
1263 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(u8),
1265 if (!sbi->s_debts) {
1266 printk("EXT3-fs: not enough memory to allocate s_bgi\n");
1269 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(u8));
1271 percpu_counter_init(&sbi->s_freeblocks_counter);
1272 percpu_counter_init(&sbi->s_freeinodes_counter);
1273 percpu_counter_init(&sbi->s_dirs_counter);
1274 bgl_lock_init(&sbi->s_blockgroup_lock);
1276 for (i = 0; i < db_count; i++) {
1277 block = descriptor_loc(sb, logic_sb_block, i);
1278 sbi->s_group_desc[i] = sb_bread(sb, block);
1279 if (!sbi->s_group_desc[i]) {
1280 printk (KERN_ERR "EXT3-fs: "
1281 "can't read group descriptor %d\n", i);
1286 if (!ext3_check_descriptors (sb)) {
1287 printk (KERN_ERR "EXT3-fs: group descriptors corrupted !\n");
1290 sbi->s_gdb_count = db_count;
1291 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1292 spin_lock_init(&sbi->s_next_gen_lock);
1294 * set up enough so that it can read an inode
1296 sb->s_op = &ext3_sops;
1297 sb->s_export_op = &ext3_export_ops;
1298 sb->dq_op = &ext3_qops;
1299 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1303 needs_recovery = (es->s_last_orphan != 0 ||
1304 EXT3_HAS_INCOMPAT_FEATURE(sb,
1305 EXT3_FEATURE_INCOMPAT_RECOVER));
1308 * The first inode we look at is the journal inode. Don't try
1309 * root first: it may be modified in the journal!
1311 if (!test_opt(sb, NOLOAD) &&
1312 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1313 if (ext3_load_journal(sb, es))
1315 } else if (journal_inum) {
1316 if (ext3_create_journal(sb, es, journal_inum))
1321 "ext3: No journal on filesystem on %s\n",
1326 /* We have now updated the journal if required, so we can
1327 * validate the data journaling mode. */
1328 switch (test_opt(sb, DATA_FLAGS)) {
1330 /* No mode set, assume a default based on the journal
1331 capabilities: ORDERED_DATA if the journal can
1332 cope, else JOURNAL_DATA */
1333 if (journal_check_available_features
1334 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
1335 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1337 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1340 case EXT3_MOUNT_ORDERED_DATA:
1341 case EXT3_MOUNT_WRITEBACK_DATA:
1342 if (!journal_check_available_features
1343 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
1344 printk(KERN_ERR "EXT3-fs: Journal does not support "
1345 "requested data journaling mode\n");
1353 * The journal_load will have done any necessary log recovery,
1354 * so we can safely mount the rest of the filesystem now.
1357 sb->s_root = d_alloc_root(iget(sb, EXT3_ROOT_INO));
1358 if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
1359 !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
1364 "EXT3-fs: corrupt root inode, run e2fsck\n");
1366 printk(KERN_ERR "EXT3-fs: get root inode failed\n");
1370 ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1372 * akpm: core read_super() calls in here with the superblock locked.
1373 * That deadlocks, because orphan cleanup needs to lock the superblock
1374 * in numerous places. Here we just pop the lock - it's relatively
1375 * harmless, because we are now ready to accept write_super() requests,
1376 * and aviro says that's the only reason for hanging onto the
1379 EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
1380 ext3_orphan_cleanup(sb, es);
1381 EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
1383 printk (KERN_INFO "EXT3-fs: recovery complete.\n");
1384 ext3_mark_recovery_complete(sb, es);
1385 printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
1386 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
1387 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
1390 percpu_counter_mod(&sbi->s_freeblocks_counter,
1391 ext3_count_free_blocks(sb));
1392 percpu_counter_mod(&sbi->s_freeinodes_counter,
1393 ext3_count_free_inodes(sb));
1394 percpu_counter_mod(&sbi->s_dirs_counter,
1395 ext3_count_dirs(sb));
1400 journal_destroy(sbi->s_journal);
1402 kfree(sbi->s_debts);
1403 for (i = 0; i < db_count; i++)
1404 brelse(sbi->s_group_desc[i]);
1405 kfree(sbi->s_group_desc);
1407 ext3_blkdev_remove(sbi);
1410 sb->s_fs_info = NULL;
1416 * Setup any per-fs journal parameters now. We'll do this both on
1417 * initial mount, once the journal has been initialised but before we've
1418 * done any recovery; and again on any subsequent remount.
1420 static void ext3_init_journal_params(struct ext3_sb_info *sbi,
1423 if (sbi->s_commit_interval)
1424 journal->j_commit_interval = sbi->s_commit_interval;
1425 /* We could also set up an ext3-specific default for the commit
1426 * interval here, but for now we'll just fall back to the jbd
1431 static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
1433 struct inode *journal_inode;
1436 /* First, test for the existence of a valid inode on disk. Bad
1437 * things happen if we iget() an unused inode, as the subsequent
1438 * iput() will try to delete it. */
1440 journal_inode = iget(sb, journal_inum);
1441 if (!journal_inode) {
1442 printk(KERN_ERR "EXT3-fs: no journal found.\n");
1445 if (!journal_inode->i_nlink) {
1446 make_bad_inode(journal_inode);
1447 iput(journal_inode);
1448 printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
1452 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1453 journal_inode, journal_inode->i_size);
1454 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
1455 printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
1456 iput(journal_inode);
1460 journal = journal_init_inode(journal_inode);
1462 printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
1463 iput(journal_inode);
1465 journal->j_private = sb;
1466 ext3_init_journal_params(EXT3_SB(sb), journal);
1470 static journal_t *ext3_get_dev_journal(struct super_block *sb,
1473 struct buffer_head * bh;
1477 int hblock, blocksize;
1478 unsigned long sb_block;
1479 unsigned long offset;
1480 struct ext3_super_block * es;
1481 struct block_device *bdev;
1483 bdev = ext3_blkdev_get(j_dev);
1487 if (bd_claim(bdev, sb)) {
1489 "EXT3: failed to claim external journal device.\n");
1490 blkdev_put(bdev, BDEV_FS);
1494 blocksize = sb->s_blocksize;
1495 hblock = bdev_hardsect_size(bdev);
1496 if (blocksize < hblock) {
1498 "EXT3-fs: blocksize too small for journal device.\n");
1502 sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
1503 offset = EXT3_MIN_BLOCK_SIZE % blocksize;
1504 set_blocksize(bdev, blocksize);
1505 if (!(bh = __bread(bdev, sb_block, blocksize))) {
1506 printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
1507 "external journal\n");
1511 es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1512 if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
1513 !(le32_to_cpu(es->s_feature_incompat) &
1514 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
1515 printk(KERN_ERR "EXT3-fs: external journal has "
1516 "bad superblock\n");
1521 if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
1522 printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
1527 len = le32_to_cpu(es->s_blocks_count);
1528 start = sb_block + 1;
1529 brelse(bh); /* we're done with the superblock */
1531 journal = journal_init_dev(bdev, sb->s_bdev,
1532 start, len, blocksize);
1534 printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
1537 journal->j_private = sb;
1538 ll_rw_block(READ, 1, &journal->j_sb_buffer);
1539 wait_on_buffer(journal->j_sb_buffer);
1540 if (!buffer_uptodate(journal->j_sb_buffer)) {
1541 printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
1544 if (ntohl(journal->j_superblock->s_nr_users) != 1) {
1545 printk(KERN_ERR "EXT3-fs: External journal has more than one "
1546 "user (unsupported) - %d\n",
1547 ntohl(journal->j_superblock->s_nr_users));
1550 EXT3_SB(sb)->journal_bdev = bdev;
1551 ext3_init_journal_params(EXT3_SB(sb), journal);
1554 journal_destroy(journal);
1556 ext3_blkdev_put(bdev);
1560 static int ext3_load_journal(struct super_block * sb,
1561 struct ext3_super_block * es)
1564 int journal_inum = le32_to_cpu(es->s_journal_inum);
1565 dev_t journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
1567 int really_read_only;
1569 really_read_only = bdev_read_only(sb->s_bdev);
1572 * Are we loading a blank journal or performing recovery after a
1573 * crash? For recovery, we need to check in advance whether we
1574 * can get read-write access to the device.
1577 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
1578 if (sb->s_flags & MS_RDONLY) {
1579 printk(KERN_INFO "EXT3-fs: INFO: recovery "
1580 "required on readonly filesystem.\n");
1581 if (really_read_only) {
1582 printk(KERN_ERR "EXT3-fs: write access "
1583 "unavailable, cannot proceed.\n");
1586 printk (KERN_INFO "EXT3-fs: write access will "
1587 "be enabled during recovery.\n");
1591 if (journal_inum && journal_dev) {
1592 printk(KERN_ERR "EXT3-fs: filesystem has both journal "
1593 "and inode journals!\n");
1598 if (!(journal = ext3_get_journal(sb, journal_inum)))
1601 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
1605 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
1606 err = journal_update_format(journal);
1608 printk(KERN_ERR "EXT3-fs: error updating journal.\n");
1609 journal_destroy(journal);
1614 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
1615 err = journal_wipe(journal, !really_read_only);
1617 err = journal_load(journal);
1620 printk(KERN_ERR "EXT3-fs: error loading journal.\n");
1621 journal_destroy(journal);
1625 EXT3_SB(sb)->s_journal = journal;
1626 ext3_clear_journal_err(sb, es);
1630 static int ext3_create_journal(struct super_block * sb,
1631 struct ext3_super_block * es,
1636 if (sb->s_flags & MS_RDONLY) {
1637 printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
1638 "create journal.\n");
1642 if (!(journal = ext3_get_journal(sb, journal_inum)))
1645 printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
1648 if (journal_create(journal)) {
1649 printk(KERN_ERR "EXT3-fs: error creating journal.\n");
1650 journal_destroy(journal);
1654 EXT3_SB(sb)->s_journal = journal;
1656 ext3_update_dynamic_rev(sb);
1657 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1658 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
1660 es->s_journal_inum = cpu_to_le32(journal_inum);
1663 /* Make sure we flush the recovery flag to disk. */
1664 ext3_commit_super(sb, es, 1);
1669 static void ext3_commit_super (struct super_block * sb,
1670 struct ext3_super_block * es,
1673 struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
1677 es->s_wtime = cpu_to_le32(get_seconds());
1678 es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
1679 es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
1680 BUFFER_TRACE(sbh, "marking dirty");
1681 mark_buffer_dirty(sbh);
1683 sync_dirty_buffer(sbh);
1688 * Have we just finished recovery? If so, and if we are mounting (or
1689 * remounting) the filesystem readonly, then we will end up with a
1690 * consistent fs on disk. Record that fact.
1692 static void ext3_mark_recovery_complete(struct super_block * sb,
1693 struct ext3_super_block * es)
1695 journal_flush(EXT3_SB(sb)->s_journal);
1696 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
1697 sb->s_flags & MS_RDONLY) {
1698 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1700 ext3_commit_super(sb, es, 1);
1705 * If we are mounting (or read-write remounting) a filesystem whose journal
1706 * has recorded an error from a previous lifetime, move that error to the
1707 * main filesystem now.
1709 static void ext3_clear_journal_err(struct super_block * sb,
1710 struct ext3_super_block * es)
1716 journal = EXT3_SB(sb)->s_journal;
1719 * Now check for any error status which may have been recorded in the
1720 * journal by a prior ext3_error() or ext3_abort()
1723 j_errno = journal_errno(journal);
1727 errstr = ext3_decode_error(sb, j_errno, nbuf);
1728 ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
1729 "from previous mount: %s", errstr);
1730 ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
1731 "filesystem check.");
1733 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
1734 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
1735 ext3_commit_super (sb, es, 1);
1737 journal_clear_err(journal);
1742 * Force the running and committing transactions to commit,
1743 * and wait on the commit.
1745 int ext3_force_commit(struct super_block *sb)
1750 if (sb->s_flags & MS_RDONLY)
1753 journal = EXT3_SB(sb)->s_journal;
1755 ret = ext3_journal_force_commit(journal);
1760 * Ext3 always journals updates to the superblock itself, so we don't
1761 * have to propagate any other updates to the superblock on disk at this
1762 * point. Just start an async writeback to get the buffers on their way
1765 * This implicitly triggers the writebehind on sync().
1768 void ext3_write_super (struct super_block * sb)
1770 if (down_trylock(&sb->s_lock) == 0)
1775 static int ext3_sync_fs(struct super_block *sb, int wait)
1780 if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
1782 log_wait_commit(EXT3_SB(sb)->s_journal, target);
1788 * LVM calls this function before a (read-only) snapshot is created. This
1789 * gives us a chance to flush the journal completely and mark the fs clean.
1791 void ext3_write_super_lockfs(struct super_block *sb)
1795 if (!(sb->s_flags & MS_RDONLY)) {
1796 journal_t *journal = EXT3_SB(sb)->s_journal;
1798 /* Now we set up the journal barrier. */
1799 journal_lock_updates(journal);
1800 journal_flush(journal);
1802 /* Journal blocked and flushed, clear needs_recovery flag. */
1803 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1804 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1809 * Called by LVM after the snapshot is done. We need to reset the RECOVER
1810 * flag here, even though the filesystem is not technically dirty yet.
1812 void ext3_unlockfs(struct super_block *sb)
1814 if (!(sb->s_flags & MS_RDONLY)) {
1816 /* Reser the needs_recovery flag before the fs is unlocked. */
1817 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1818 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1820 journal_unlock_updates(EXT3_SB(sb)->s_journal);
1824 int ext3_remount (struct super_block * sb, int * flags, char * data)
1826 struct ext3_super_block * es;
1827 struct ext3_sb_info *sbi = EXT3_SB(sb);
1831 * Allow the "check" option to be passed as a remount option.
1833 if (!parse_options(data, sbi, &tmp, 1))
1836 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
1837 ext3_abort(sb, __FUNCTION__, "Abort forced by user");
1839 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1840 ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1844 ext3_init_journal_params(sbi, sbi->s_journal);
1846 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1847 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
1850 if (*flags & MS_RDONLY) {
1852 * First of all, the unconditional stuff we have to do
1853 * to disable replay of the journal when we next remount
1855 sb->s_flags |= MS_RDONLY;
1858 * OK, test if we are remounting a valid rw partition
1859 * readonly, and if so set the rdonly flag and then
1860 * mark the partition as valid again.
1862 if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
1863 (sbi->s_mount_state & EXT3_VALID_FS))
1864 es->s_state = cpu_to_le16(sbi->s_mount_state);
1866 ext3_mark_recovery_complete(sb, es);
1869 if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
1870 ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
1871 printk(KERN_WARNING "EXT3-fs: %s: couldn't "
1872 "remount RDWR because of unsupported "
1873 "optional features (%x).\n",
1878 * Mounting a RDONLY partition read-write, so reread
1879 * and store the current valid flag. (It may have
1880 * been changed by e2fsck since we originally mounted
1883 ext3_clear_journal_err(sb, es);
1884 sbi->s_mount_state = le16_to_cpu(es->s_state);
1885 if (!ext3_setup_super (sb, es, 0))
1886 sb->s_flags &= ~MS_RDONLY;
1892 int ext3_statfs (struct super_block * sb, struct kstatfs * buf)
1894 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
1895 unsigned long overhead;
1898 if (test_opt (sb, MINIX_DF))
1902 * Compute the overhead (FS structures)
1906 * All of the blocks before first_data_block are
1909 overhead = le32_to_cpu(es->s_first_data_block);
1912 * Add the overhead attributed to the superblock and
1913 * block group descriptors. If the sparse superblocks
1914 * feature is turned on, then not all groups have this.
1916 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1917 overhead += ext3_bg_has_super(sb, i) +
1918 ext3_bg_num_gdb(sb, i);
1921 * Every block group has an inode bitmap, a block
1922 * bitmap, and an inode table.
1924 overhead += (EXT3_SB(sb)->s_groups_count *
1925 (2 + EXT3_SB(sb)->s_itb_per_group));
1928 buf->f_type = EXT3_SUPER_MAGIC;
1929 buf->f_bsize = sb->s_blocksize;
1930 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
1931 buf->f_bfree = ext3_count_free_blocks (sb);
1932 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
1933 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
1935 buf->f_files = le32_to_cpu(es->s_inodes_count);
1936 buf->f_ffree = ext3_count_free_inodes (sb);
1937 buf->f_namelen = EXT3_NAME_LEN;
1941 /* Helper function for writing quotas on sync - we need to start transaction before quota file
1942 * is locked for write. Otherwise the are possible deadlocks:
1943 * Process 1 Process 2
1944 * ext3_create() quota_sync()
1945 * journal_start() write_dquot()
1946 * DQUOT_INIT() down(dqio_sem)
1947 * down(dqio_sem) journal_start()
1953 /* Blocks: (2 data blocks) * (3 indirect + 1 descriptor + 1 bitmap) + superblock */
1954 #define EXT3_OLD_QFMT_BLOCKS 11
1955 /* Blocks: quota info + (4 pointer blocks + 1 entry block) * (3 indirect + 1 descriptor + 1 bitmap) + superblock */
1956 #define EXT3_V0_QFMT_BLOCKS 27
1958 static int (*old_write_dquot)(struct dquot *dquot);
1960 static int ext3_write_dquot(struct dquot *dquot)
1966 struct quota_info *dqops = sb_dqopt(dquot->dq_sb);
1967 struct inode *qinode;
1969 switch (dqops->info[dquot->dq_type].dqi_format->qf_fmt_id) {
1971 nblocks = EXT3_OLD_QFMT_BLOCKS;
1974 nblocks = EXT3_V0_QFMT_BLOCKS;
1977 nblocks = EXT3_MAX_TRANS_DATA;
1979 qinode = dqops->files[dquot->dq_type]->f_dentry->d_inode;
1980 handle = ext3_journal_start(qinode, nblocks);
1981 if (IS_ERR(handle)) {
1982 ret = PTR_ERR(handle);
1985 ret = old_write_dquot(dquot);
1986 err = ext3_journal_stop(handle);
1994 static struct super_block *ext3_get_sb(struct file_system_type *fs_type,
1995 int flags, const char *dev_name, void *data)
1997 return get_sb_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
2000 static struct file_system_type ext3_fs_type = {
2001 .owner = THIS_MODULE,
2003 .get_sb = ext3_get_sb,
2004 .kill_sb = kill_block_super,
2005 .fs_flags = FS_REQUIRES_DEV,
2008 static int __init init_ext3_fs(void)
2010 int err = init_ext3_xattr();
2013 err = init_inodecache();
2017 init_dquot_operations(&ext3_qops);
2018 old_write_dquot = ext3_qops.write_dquot;
2019 ext3_qops.write_dquot = ext3_write_dquot;
2021 err = register_filesystem_lifo(&ext3_fs_type);
2026 destroy_inodecache();
2032 static void __exit exit_ext3_fs(void)
2034 unregister_filesystem(&ext3_fs_type);
2035 destroy_inodecache();
2039 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2040 MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
2041 MODULE_LICENSE("GPL");
2042 module_init(init_ext3_fs)
2043 module_exit(exit_ext3_fs)