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/sched.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/locks.h>
30 #include <linux/blkdev.h>
31 #include <linux/smp_lock.h>
32 #include <linux/random.h>
33 #include <asm/uaccess.h>
35 #ifdef CONFIG_JBD_DEBUG
36 static int ext3_ro_after; /* Make fs read-only after this many jiffies */
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);
50 static int ext3_sync_fs(struct super_block * sb);
52 #ifdef CONFIG_JBD_DEBUG
53 int journal_no_write[2];
56 * Debug code for turning filesystems "read-only" after a specified
57 * amount of time. This is for crash/recovery testing.
60 static void make_rdonly(kdev_t dev, int *no_write)
63 printk(KERN_WARNING "Turning device %s read-only\n",
65 *no_write = 0xdead0000 + dev;
69 static void turn_fs_readonly(unsigned long arg)
71 struct super_block *sb = (struct super_block *)arg;
73 make_rdonly(sb->s_dev, &journal_no_write[0]);
74 make_rdonly(EXT3_SB(sb)->s_journal->j_dev, &journal_no_write[1]);
75 wake_up(&EXT3_SB(sb)->ro_wait_queue);
78 static void setup_ro_after(struct super_block *sb)
80 struct ext3_sb_info *sbi = EXT3_SB(sb);
81 init_timer(&sbi->turn_ro_timer);
83 printk(KERN_DEBUG "fs will go read-only in %d jiffies\n",
85 init_waitqueue_head(&sbi->ro_wait_queue);
86 journal_no_write[0] = 0;
87 journal_no_write[1] = 0;
88 sbi->turn_ro_timer.function = turn_fs_readonly;
89 sbi->turn_ro_timer.data = (unsigned long)sb;
90 sbi->turn_ro_timer.expires = jiffies + ext3_ro_after;
92 add_timer(&sbi->turn_ro_timer);
96 static void clear_ro_after(struct super_block *sb)
98 del_timer_sync(&EXT3_SB(sb)->turn_ro_timer);
99 journal_no_write[0] = 0;
100 journal_no_write[1] = 0;
104 #define setup_ro_after(sb) do {} while (0)
105 #define clear_ro_after(sb) do {} while (0)
109 static char error_buf[1024];
111 /* Determine the appropriate response to ext3_error on a given filesystem */
113 static int ext3_error_behaviour(struct super_block *sb)
115 /* First check for mount-time options */
116 if (test_opt (sb, ERRORS_PANIC))
117 return EXT3_ERRORS_PANIC;
118 if (test_opt (sb, ERRORS_RO))
119 return EXT3_ERRORS_RO;
120 if (test_opt (sb, ERRORS_CONT))
121 return EXT3_ERRORS_CONTINUE;
123 /* If no overrides were specified on the mount, then fall back
124 * to the default behaviour set in the filesystem's superblock
126 switch (le16_to_cpu(sb->u.ext3_sb.s_es->s_errors)) {
127 case EXT3_ERRORS_PANIC:
128 return EXT3_ERRORS_PANIC;
130 return EXT3_ERRORS_RO;
134 return EXT3_ERRORS_CONTINUE;
137 /* Deal with the reporting of failure conditions on a filesystem such as
138 * inconsistencies detected or read IO failures.
140 * On ext2, we can store the error state of the filesystem in the
141 * superblock. That is not possible on ext3, because we may have other
142 * write ordering constraints on the superblock which prevent us from
143 * writing it out straight away; and given that the journal is about to
144 * be aborted, we can't rely on the current, or future, transactions to
145 * write out the superblock safely.
147 * We'll just use the journal_abort() error code to record an error in
148 * the journal instead. On recovery, the journal will compain about
149 * that error until we've noted it down and cleared it.
152 static void ext3_handle_error(struct super_block *sb)
154 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
156 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
157 es->s_state |= cpu_to_le32(EXT3_ERROR_FS);
159 if (sb->s_flags & MS_RDONLY)
162 if (ext3_error_behaviour(sb) != EXT3_ERRORS_CONTINUE) {
163 EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
164 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
167 if (ext3_error_behaviour(sb) == EXT3_ERRORS_PANIC)
168 panic ("EXT3-fs (device %s): panic forced after error\n",
169 bdevname(sb->s_dev));
171 if (ext3_error_behaviour(sb) == EXT3_ERRORS_RO) {
172 printk (KERN_CRIT "Remounting filesystem read-only\n");
173 sb->s_flags |= MS_RDONLY;
176 ext3_commit_super(sb, es, 1);
179 void ext3_error (struct super_block * sb, const char * function,
180 const char * fmt, ...)
184 va_start (args, fmt);
185 vsprintf (error_buf, fmt, args);
188 printk (KERN_CRIT "EXT3-fs error (device %s): %s: %s\n",
189 bdevname(sb->s_dev), function, error_buf);
191 ext3_handle_error(sb);
194 const char *ext3_decode_error(struct super_block * sb, int errno, char nbuf[16])
200 errstr = "IO failure";
203 errstr = "Out of memory";
206 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
207 errstr = "Journal has aborted";
209 errstr = "Readonly filesystem";
212 /* If the caller passed in an extra buffer for unknown
213 * errors, textualise them now. Else we just return
216 /* Check for truncated error codes... */
217 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
227 /* __ext3_std_error decodes expected errors from journaling functions
228 * automatically and invokes the appropriate error response. */
230 void __ext3_std_error (struct super_block * sb, const char * function,
234 const char *errstr = ext3_decode_error(sb, errno, nbuf);
236 printk (KERN_CRIT "EXT3-fs error (device %s) in %s: %s\n",
237 bdevname(sb->s_dev), function, errstr);
239 ext3_handle_error(sb);
243 * ext3_abort is a much stronger failure handler than ext3_error. The
244 * abort function may be used to deal with unrecoverable failures such
245 * as journal IO errors or ENOMEM at a critical moment in log management.
247 * We unconditionally force the filesystem into an ABORT|READONLY state,
248 * unless the error response on the fs has been set to panic in which
249 * case we take the easy way out and panic immediately.
252 void ext3_abort (struct super_block * sb, const char * function,
253 const char * fmt, ...)
257 printk (KERN_CRIT "ext3_abort called.\n");
259 va_start (args, fmt);
260 vsprintf (error_buf, fmt, args);
263 if (ext3_error_behaviour(sb) == EXT3_ERRORS_PANIC)
264 panic ("EXT3-fs panic (device %s): %s: %s\n",
265 bdevname(sb->s_dev), function, error_buf);
267 printk (KERN_CRIT "EXT3-fs abort (device %s): %s: %s\n",
268 bdevname(sb->s_dev), function, error_buf);
270 if (sb->s_flags & MS_RDONLY)
273 printk (KERN_CRIT "Remounting filesystem read-only\n");
274 sb->u.ext3_sb.s_mount_state |= EXT3_ERROR_FS;
275 sb->s_flags |= MS_RDONLY;
276 sb->u.ext3_sb.s_mount_opt |= EXT3_MOUNT_ABORT;
277 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
280 /* Deal with the reporting of failure conditions while running, such as
281 * inconsistencies in operation or invalid system states.
283 * Use ext3_error() for cases of invalid filesystem states, as that will
284 * record an error on disk and force a filesystem check on the next boot.
286 NORET_TYPE void ext3_panic (struct super_block * sb, const char * function,
287 const char * fmt, ...)
291 va_start (args, fmt);
292 vsprintf (error_buf, fmt, args);
295 /* this is to prevent panic from syncing this filesystem */
296 /* AKPM: is this sufficient? */
297 sb->s_flags |= MS_RDONLY;
298 panic ("EXT3-fs panic (device %s): %s: %s\n",
299 bdevname(sb->s_dev), function, error_buf);
302 void ext3_warning (struct super_block * sb, const char * function,
303 const char * fmt, ...)
307 va_start (args, fmt);
308 vsprintf (error_buf, fmt, args);
310 printk (KERN_WARNING "EXT3-fs warning (device %s): %s: %s\n",
311 bdevname(sb->s_dev), function, error_buf);
314 void ext3_update_dynamic_rev(struct super_block *sb)
316 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
318 if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
321 ext3_warning(sb, __FUNCTION__,
322 "updating to rev %d because of new feature flag, "
323 "running e2fsck is recommended",
326 es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
327 es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
328 es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
329 /* leave es->s_feature_*compat flags alone */
330 /* es->s_uuid will be set by e2fsck if empty */
333 * The rest of the superblock fields should be zero, and if not it
334 * means they are likely already in use, so leave them alone. We
335 * can leave it up to e2fsck to clean up any inconsistencies there.
340 * Open the external journal device
342 static struct block_device *ext3_blkdev_get(kdev_t dev)
344 struct block_device *bdev;
347 bdev = bdget(kdev_t_to_nr(dev));
350 err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS);
356 printk(KERN_ERR "EXT3: failed to open journal device %s: %d\n",
362 * Release the journal device
364 static int ext3_blkdev_put(struct block_device *bdev)
366 return blkdev_put(bdev, BDEV_FS);
369 static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
371 struct block_device *bdev;
374 bdev = sbi->journal_bdev;
376 ret = ext3_blkdev_put(bdev);
377 sbi->journal_bdev = 0;
382 #define orphan_list_entry(l) list_entry((l), struct inode, u.ext3_i.i_orphan)
384 static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
388 printk(KERN_ERR "sb orphan head is %d\n",
389 le32_to_cpu(sbi->s_es->s_last_orphan));
391 printk(KERN_ERR "sb_info orphan list:\n");
392 list_for_each(l, &sbi->s_orphan) {
393 struct inode *inode = orphan_list_entry(l);
395 "inode 0x%04x:%ld at %p: mode %o, nlink %d, next %d\n",
396 inode->i_dev, inode->i_ino, inode,
397 inode->i_mode, inode->i_nlink,
398 le32_to_cpu(NEXT_ORPHAN(inode)));
402 void ext3_put_super (struct super_block * sb)
404 struct ext3_sb_info *sbi = EXT3_SB(sb);
405 struct ext3_super_block *es = sbi->s_es;
406 kdev_t j_dev = sbi->s_journal->j_dev;
409 journal_destroy(sbi->s_journal);
410 if (!(sb->s_flags & MS_RDONLY)) {
411 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
412 es->s_state = le16_to_cpu(sbi->s_mount_state);
413 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
414 mark_buffer_dirty(sbi->s_sbh);
415 ext3_commit_super(sb, es, 1);
418 for (i = 0; i < sbi->s_gdb_count; i++)
419 brelse(sbi->s_group_desc[i]);
420 kfree(sbi->s_group_desc);
421 for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++)
422 brelse(sbi->s_inode_bitmap[i]);
423 for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++)
424 brelse(sbi->s_block_bitmap[i]);
427 /* Debugging code just in case the in-memory inode orphan list
428 * isn't empty. The on-disk one can be non-empty if we've
429 * detected an error and taken the fs readonly, but the
430 * in-memory list had better be clean by this point. */
431 if (!list_empty(&sbi->s_orphan))
432 dump_orphan_list(sb, sbi);
433 J_ASSERT(list_empty(&sbi->s_orphan));
435 invalidate_buffers(sb->s_dev);
436 if (j_dev != sb->s_dev) {
438 * Invalidate the journal device's buffers. We don't want them
439 * floating about in memory - the physical journal device may
440 * hotswapped, and it breaks the `ro-after' testing code.
442 fsync_no_super(j_dev);
443 invalidate_buffers(j_dev);
444 ext3_blkdev_remove(sbi);
451 static struct super_operations ext3_sops = {
452 read_inode: ext3_read_inode, /* BKL held */
453 write_inode: ext3_write_inode, /* BKL not held. Don't need */
454 dirty_inode: ext3_dirty_inode, /* BKL not held. We take it */
455 put_inode: ext3_put_inode, /* BKL not held. Don't need */
456 delete_inode: ext3_delete_inode, /* BKL not held. We take it */
457 put_super: ext3_put_super, /* BKL held */
458 write_super: ext3_write_super, /* BKL held */
459 sync_fs: ext3_sync_fs,
460 write_super_lockfs: ext3_write_super_lockfs, /* BKL not held. Take it */
461 unlockfs: ext3_unlockfs, /* BKL not held. We take it */
462 statfs: ext3_statfs, /* BKL held */
463 remount_fs: ext3_remount, /* BKL held */
466 static int want_value(char *value, char *option)
468 if (!value || !*value) {
469 printk(KERN_NOTICE "EXT3-fs: the %s option needs an argument\n",
476 static int want_null_value(char *value, char *option)
479 printk(KERN_NOTICE "EXT3-fs: Invalid %s argument: %s\n",
486 static int want_numeric(char *value, char *option, unsigned long *number)
488 if (want_value(value, option))
490 *number = simple_strtoul(value, &value, 0);
491 if (want_null_value(value, option))
497 * This function has been shamelessly adapted from the msdos fs
499 static int parse_options (char * options, unsigned long * sb_block,
500 struct ext3_sb_info *sbi,
501 unsigned long * inum,
504 unsigned long *mount_options = &sbi->s_mount_opt;
505 uid_t *resuid = &sbi->s_resuid;
506 gid_t *resgid = &sbi->s_resgid;
512 for (this_char = strtok (options, ",");
514 this_char = strtok (NULL, ",")) {
515 if ((value = strchr (this_char, '=')) != NULL)
517 if (!strcmp (this_char, "bsddf"))
518 clear_opt (*mount_options, MINIX_DF);
519 else if (!strcmp (this_char, "nouid32")) {
520 set_opt (*mount_options, NO_UID32);
522 else if (!strcmp (this_char, "abort"))
523 set_opt (*mount_options, ABORT);
524 else if (!strcmp (this_char, "check")) {
525 if (!value || !*value || !strcmp (value, "none"))
526 clear_opt (*mount_options, CHECK);
528 #ifdef CONFIG_EXT3_CHECK
529 set_opt (*mount_options, CHECK);
532 "EXT3 Check option not supported\n");
535 else if (!strcmp (this_char, "debug"))
536 set_opt (*mount_options, DEBUG);
537 else if (!strcmp (this_char, "errors")) {
538 if (want_value(value, "errors"))
540 if (!strcmp (value, "continue")) {
541 clear_opt (*mount_options, ERRORS_RO);
542 clear_opt (*mount_options, ERRORS_PANIC);
543 set_opt (*mount_options, ERRORS_CONT);
545 else if (!strcmp (value, "remount-ro")) {
546 clear_opt (*mount_options, ERRORS_CONT);
547 clear_opt (*mount_options, ERRORS_PANIC);
548 set_opt (*mount_options, ERRORS_RO);
550 else if (!strcmp (value, "panic")) {
551 clear_opt (*mount_options, ERRORS_CONT);
552 clear_opt (*mount_options, ERRORS_RO);
553 set_opt (*mount_options, ERRORS_PANIC);
557 "EXT3-fs: Invalid errors option: %s\n",
562 else if (!strcmp (this_char, "grpid") ||
563 !strcmp (this_char, "bsdgroups"))
564 set_opt (*mount_options, GRPID);
565 else if (!strcmp (this_char, "minixdf"))
566 set_opt (*mount_options, MINIX_DF);
567 else if (!strcmp (this_char, "nocheck"))
568 clear_opt (*mount_options, CHECK);
569 else if (!strcmp (this_char, "nogrpid") ||
570 !strcmp (this_char, "sysvgroups"))
571 clear_opt (*mount_options, GRPID);
572 else if (!strcmp (this_char, "resgid")) {
574 if (want_numeric(value, "resgid", &v))
578 else if (!strcmp (this_char, "resuid")) {
580 if (want_numeric(value, "resuid", &v))
584 else if (!strcmp (this_char, "sb")) {
585 if (want_numeric(value, "sb", sb_block))
588 #ifdef CONFIG_JBD_DEBUG
589 else if (!strcmp (this_char, "ro-after")) {
591 if (want_numeric(value, "ro-after", &v))
596 /* Silently ignore the quota options */
597 else if (!strcmp (this_char, "grpquota")
598 || !strcmp (this_char, "noquota")
599 || !strcmp (this_char, "quota")
600 || !strcmp (this_char, "usrquota"))
601 /* Don't do anything ;-) */ ;
602 else if (!strcmp (this_char, "journal")) {
604 /* Eventually we will want to be able to create
605 a journal file here. For now, only allow the
606 user to specify an existing inode to be the
609 printk(KERN_ERR "EXT3-fs: cannot specify "
610 "journal on remount\n");
614 if (want_value(value, "journal"))
616 if (!strcmp (value, "update"))
617 set_opt (*mount_options, UPDATE_JOURNAL);
618 else if (want_numeric(value, "journal", inum))
621 else if (!strcmp (this_char, "noload"))
622 set_opt (*mount_options, NOLOAD);
623 else if (!strcmp (this_char, "data")) {
626 if (want_value(value, "data"))
628 if (!strcmp (value, "journal"))
629 data_opt = EXT3_MOUNT_JOURNAL_DATA;
630 else if (!strcmp (value, "ordered"))
631 data_opt = EXT3_MOUNT_ORDERED_DATA;
632 else if (!strcmp (value, "writeback"))
633 data_opt = EXT3_MOUNT_WRITEBACK_DATA;
636 "EXT3-fs: Invalid data option: %s\n",
641 if ((*mount_options & EXT3_MOUNT_DATA_FLAGS) !=
644 "EXT3-fs: cannot change data "
645 "mode on remount\n");
649 *mount_options &= ~EXT3_MOUNT_DATA_FLAGS;
650 *mount_options |= data_opt;
652 } else if (!strcmp (this_char, "commit")) {
654 if (want_numeric(value, "commit", &v))
656 sbi->s_commit_interval = (HZ * v);
659 "EXT3-fs: Unrecognized mount option %s\n",
667 static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
670 struct ext3_sb_info *sbi = EXT3_SB(sb);
673 if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
674 printk (KERN_ERR "EXT3-fs warning: revision level too high, "
675 "forcing read-only mode\n");
680 if (!(sbi->s_mount_state & EXT3_VALID_FS))
681 printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
682 "running e2fsck is recommended\n");
683 else if ((sbi->s_mount_state & EXT3_ERROR_FS))
685 "EXT3-fs warning: mounting fs with errors, "
686 "running e2fsck is recommended\n");
687 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
688 le16_to_cpu(es->s_mnt_count) >=
689 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
691 "EXT3-fs warning: maximal mount count reached, "
692 "running e2fsck is recommended\n");
693 else if (le32_to_cpu(es->s_checkinterval) &&
694 (le32_to_cpu(es->s_lastcheck) +
695 le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
697 "EXT3-fs warning: checktime reached, "
698 "running e2fsck is recommended\n");
700 /* @@@ We _will_ want to clear the valid bit if we find
701 inconsistencies, to force a fsck at reboot. But for
702 a plain journaled filesystem we can keep it set as
704 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
706 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
707 es->s_max_mnt_count =
708 (__s16) cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
709 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
710 es->s_mtime = cpu_to_le32(CURRENT_TIME);
711 ext3_update_dynamic_rev(sb);
712 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
713 ext3_commit_super (sb, es, 1);
714 if (test_opt (sb, DEBUG))
716 "[EXT3 FS %s, %s, bs=%lu, gc=%lu, "
717 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
718 EXT3FS_VERSION, EXT3FS_DATE, sb->s_blocksize,
720 EXT3_BLOCKS_PER_GROUP(sb),
721 EXT3_INODES_PER_GROUP(sb),
723 printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
724 bdevname(sb->s_dev));
725 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
726 printk("external journal on %s\n",
727 bdevname(EXT3_SB(sb)->s_journal->j_dev));
729 printk("internal journal\n");
731 #ifdef CONFIG_EXT3_CHECK
732 if (test_opt (sb, CHECK)) {
733 ext3_check_blocks_bitmap (sb);
734 ext3_check_inodes_bitmap (sb);
741 static int ext3_check_descriptors (struct super_block * sb)
743 struct ext3_sb_info *sbi = EXT3_SB(sb);
744 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
745 struct ext3_group_desc * gdp = NULL;
749 ext3_debug ("Checking group descriptors");
751 for (i = 0; i < sbi->s_groups_count; i++)
753 if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
754 gdp = (struct ext3_group_desc *)
755 sbi->s_group_desc[desc_block++]->b_data;
756 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
757 le32_to_cpu(gdp->bg_block_bitmap) >=
758 block + EXT3_BLOCKS_PER_GROUP(sb))
760 ext3_error (sb, "ext3_check_descriptors",
761 "Block bitmap for group %d"
762 " not in group (block %lu)!",
764 le32_to_cpu(gdp->bg_block_bitmap));
767 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
768 le32_to_cpu(gdp->bg_inode_bitmap) >=
769 block + EXT3_BLOCKS_PER_GROUP(sb))
771 ext3_error (sb, "ext3_check_descriptors",
772 "Inode bitmap for group %d"
773 " not in group (block %lu)!",
775 le32_to_cpu(gdp->bg_inode_bitmap));
778 if (le32_to_cpu(gdp->bg_inode_table) < block ||
779 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
780 block + EXT3_BLOCKS_PER_GROUP(sb))
782 ext3_error (sb, "ext3_check_descriptors",
783 "Inode table for group %d"
784 " not in group (block %lu)!",
786 le32_to_cpu(gdp->bg_inode_table));
789 block += EXT3_BLOCKS_PER_GROUP(sb);
796 /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
797 * the superblock) which were deleted from all directories, but held open by
798 * a process at the time of a crash. We walk the list and try to delete these
799 * inodes at recovery time (only with a read-write filesystem).
801 * In order to keep the orphan inode chain consistent during traversal (in
802 * case of crash during recovery), we link each inode into the superblock
803 * orphan list_head and handle it the same way as an inode deletion during
804 * normal operation (which journals the operations for us).
806 * We only do an iget() and an iput() on each inode, which is very safe if we
807 * accidentally point at an in-use or already deleted inode. The worst that
808 * can happen in this case is that we get a "bit already cleared" message from
809 * ext3_free_inode(). The only reason we would point at a wrong inode is if
810 * e2fsck was run on this filesystem, and it must have already done the orphan
811 * inode cleanup for us, so we can safely abort without any further action.
813 static void ext3_orphan_cleanup (struct super_block * sb,
814 struct ext3_super_block * es)
816 unsigned int s_flags = sb->s_flags;
817 int nr_orphans = 0, nr_truncates = 0;
818 if (!es->s_last_orphan) {
819 jbd_debug(4, "no orphan inodes to clean up\n");
823 if (s_flags & MS_RDONLY) {
824 printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
825 bdevname(sb->s_dev));
826 sb->s_flags &= ~MS_RDONLY;
829 if (sb->u.ext3_sb.s_mount_state & EXT3_ERROR_FS) {
830 if (es->s_last_orphan)
831 jbd_debug(1, "Errors on filesystem, "
832 "clearing orphan list.\n");
833 es->s_last_orphan = 0;
834 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
838 while (es->s_last_orphan) {
842 ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
843 es->s_last_orphan = 0;
847 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
848 if (inode->i_nlink) {
849 printk(KERN_DEBUG "%s: truncating inode %ld to %Ld "
850 "bytes\n", __FUNCTION__, inode->i_ino,
852 jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
853 inode->i_ino, inode->i_size);
854 ext3_truncate(inode);
857 printk(KERN_DEBUG "%s: deleting unreferenced "
858 "inode %ld\n", __FUNCTION__, inode->i_ino);
859 jbd_debug(2, "deleting unreferenced inode %ld\n",
863 iput(inode); /* The delete magic happens here! */
866 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
869 printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
870 bdevname(sb->s_dev), PLURAL(nr_orphans));
872 printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
873 bdevname(sb->s_dev), PLURAL(nr_truncates));
874 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
877 #define log2(n) ffz(~(n))
880 * Maximal file size. There is a direct, and {,double-,triple-}indirect
881 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
882 * We need to be 1 filesystem block less than the 2^32 sector limit.
884 static loff_t ext3_max_size(int bits)
886 loff_t res = EXT3_NDIR_BLOCKS;
887 res += 1LL << (bits-2);
888 res += 1LL << (2*(bits-2));
889 res += 1LL << (3*(bits-2));
891 if (res > (512LL << 32) - (1 << bits))
892 res = (512LL << 32) - (1 << bits);
896 struct super_block * ext3_read_super (struct super_block * sb, void * data,
899 struct buffer_head * bh;
900 struct ext3_super_block *es = 0;
901 struct ext3_sb_info *sbi = EXT3_SB(sb);
902 unsigned long sb_block = 1;
903 unsigned long logic_sb_block = 1;
904 unsigned long offset = 0;
905 unsigned long journal_inum = 0;
906 kdev_t dev = sb->s_dev;
913 #ifdef CONFIG_JBD_DEBUG
917 * See what the current blocksize for the device is, and
918 * use that as the blocksize. Otherwise (or if the blocksize
919 * is smaller than the default) use the default.
920 * This is important for devices that have a hardware
921 * sectorsize that is larger than the default.
923 blocksize = EXT3_MIN_BLOCK_SIZE;
924 hblock = get_hardsect_size(dev);
925 if (blocksize < hblock)
928 sbi->s_mount_opt = 0;
929 sbi->s_resuid = EXT3_DEF_RESUID;
930 sbi->s_resgid = EXT3_DEF_RESGID;
931 if (!parse_options ((char *) data, &sb_block, sbi, &journal_inum, 0)) {
936 sb->s_blocksize = blocksize;
937 set_blocksize (dev, blocksize);
940 * The ext3 superblock will not be buffer aligned for other than 1kB
941 * block sizes. We need to calculate the offset from buffer start.
943 if (blocksize != EXT3_MIN_BLOCK_SIZE) {
944 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
945 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
948 if (!(bh = sb_bread(sb, logic_sb_block))) {
949 printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
953 * Note: s_es must be initialized as soon as possible because
954 * some ext3 macro-instructions depend on its value
956 es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
958 sb->s_magic = le16_to_cpu(es->s_magic);
959 if (sb->s_magic != EXT3_SUPER_MAGIC) {
962 "VFS: Can't find ext3 filesystem on dev %s.\n",
966 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
967 (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
968 EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
969 EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
971 "EXT3-fs warning: feature flags set on rev 0 fs, "
972 "running e2fsck is recommended\n");
974 * Check feature flags regardless of the revision level, since we
975 * previously didn't change the revision level when setting the flags,
976 * so there is a chance incompat flags are set on a rev 0 filesystem.
978 if ((i = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))) {
979 printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
980 "unsupported optional features (%x).\n",
984 if (!(sb->s_flags & MS_RDONLY) &&
985 (i = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))){
986 printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
987 "unsupported optional features (%x).\n",
991 sb->s_blocksize_bits = le32_to_cpu(es->s_log_block_size) + 10;
992 sb->s_blocksize = 1 << sb->s_blocksize_bits;
994 if (sb->s_blocksize < EXT3_MIN_BLOCK_SIZE ||
995 sb->s_blocksize > EXT3_MAX_BLOCK_SIZE) {
997 "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
998 blocksize, bdevname(dev));
1002 sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1004 if (sb->s_blocksize != blocksize) {
1005 blocksize = sb->s_blocksize;
1008 * Make sure the blocksize for the filesystem is larger
1009 * than the hardware sectorsize for the machine.
1011 if (sb->s_blocksize < hblock) {
1012 printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
1013 "device blocksize %d.\n", blocksize, hblock);
1018 set_blocksize (dev, sb->s_blocksize);
1019 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1020 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1021 bh = sb_bread(sb, logic_sb_block);
1024 "EXT3-fs: Can't read superblock on 2nd try.\n");
1027 es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
1029 if (es->s_magic != le16_to_cpu(EXT3_SUPER_MAGIC)) {
1031 "EXT3-fs: Magic mismatch, very weird !\n");
1036 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1037 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1038 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1040 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1041 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1042 if (sbi->s_inode_size != EXT3_GOOD_OLD_INODE_SIZE) {
1044 "EXT3-fs: unsupported inode size: %d\n",
1049 sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1050 le32_to_cpu(es->s_log_frag_size);
1051 if (blocksize != sbi->s_frag_size) {
1053 "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
1054 sbi->s_frag_size, blocksize);
1057 sbi->s_frags_per_block = 1;
1058 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1059 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1060 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1061 sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1062 sbi->s_itb_per_group = sbi->s_inodes_per_group /sbi->s_inodes_per_block;
1063 sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1065 if (sbi->s_resuid == EXT3_DEF_RESUID)
1066 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1067 if (sbi->s_resgid == EXT3_DEF_RESGID)
1068 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1069 sbi->s_mount_state = le16_to_cpu(es->s_state);
1070 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
1071 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
1073 if (sbi->s_blocks_per_group > blocksize * 8) {
1075 "EXT3-fs: #blocks per group too big: %lu\n",
1076 sbi->s_blocks_per_group);
1079 if (sbi->s_frags_per_group > blocksize * 8) {
1081 "EXT3-fs: #fragments per group too big: %lu\n",
1082 sbi->s_frags_per_group);
1085 if (sbi->s_inodes_per_group > blocksize * 8) {
1087 "EXT3-fs: #inodes per group too big: %lu\n",
1088 sbi->s_inodes_per_group);
1092 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
1093 le32_to_cpu(es->s_first_data_block) +
1094 EXT3_BLOCKS_PER_GROUP(sb) - 1) /
1095 EXT3_BLOCKS_PER_GROUP(sb);
1096 db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
1097 EXT3_DESC_PER_BLOCK(sb);
1098 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1100 if (sbi->s_group_desc == NULL) {
1101 printk (KERN_ERR "EXT3-fs: not enough memory\n");
1104 for (i = 0; i < db_count; i++) {
1105 sbi->s_group_desc[i] = sb_bread(sb, logic_sb_block + i + 1);
1106 if (!sbi->s_group_desc[i]) {
1107 printk (KERN_ERR "EXT3-fs: "
1108 "can't read group descriptor %d\n", i);
1113 if (!ext3_check_descriptors (sb)) {
1114 printk (KERN_ERR "EXT3-fs: group descriptors corrupted !\n");
1117 for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++) {
1118 sbi->s_inode_bitmap_number[i] = 0;
1119 sbi->s_inode_bitmap[i] = NULL;
1120 sbi->s_block_bitmap_number[i] = 0;
1121 sbi->s_block_bitmap[i] = NULL;
1123 sbi->s_loaded_inode_bitmaps = 0;
1124 sbi->s_loaded_block_bitmaps = 0;
1125 sbi->s_gdb_count = db_count;
1126 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1128 * set up enough so that it can read an inode
1130 sb->s_op = &ext3_sops;
1131 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1135 needs_recovery = (es->s_last_orphan != 0 ||
1136 EXT3_HAS_INCOMPAT_FEATURE(sb,
1137 EXT3_FEATURE_INCOMPAT_RECOVER));
1140 * The first inode we look at is the journal inode. Don't try
1141 * root first: it may be modified in the journal!
1143 if (!test_opt(sb, NOLOAD) &&
1144 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1145 if (ext3_load_journal(sb, es))
1147 } else if (journal_inum) {
1148 if (ext3_create_journal(sb, es, journal_inum))
1153 "ext3: No journal on filesystem on %s\n",
1158 /* We have now updated the journal if required, so we can
1159 * validate the data journaling mode. */
1160 switch (test_opt(sb, DATA_FLAGS)) {
1162 /* No mode set, assume a default based on the journal
1163 capabilities: ORDERED_DATA if the journal can
1164 cope, else JOURNAL_DATA */
1165 if (journal_check_available_features
1166 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
1167 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1169 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1172 case EXT3_MOUNT_ORDERED_DATA:
1173 case EXT3_MOUNT_WRITEBACK_DATA:
1174 if (!journal_check_available_features
1175 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
1176 printk(KERN_ERR "EXT3-fs: Journal does not support "
1177 "requested data journaling mode\n");
1185 * The journal_load will have done any necessary log recovery,
1186 * so we can safely mount the rest of the filesystem now.
1189 sb->s_root = d_alloc_root(iget(sb, EXT3_ROOT_INO));
1190 if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
1191 !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
1196 "EXT3-fs: corrupt root inode, run e2fsck\n");
1198 printk(KERN_ERR "EXT3-fs: get root inode failed\n");
1202 ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1204 * akpm: core read_super() calls in here with the superblock locked.
1205 * That deadlocks, because orphan cleanup needs to lock the superblock
1206 * in numerous places. Here we just pop the lock - it's relatively
1207 * harmless, because we are now ready to accept write_super() requests,
1208 * and aviro says that's the only reason for hanging onto the
1211 EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
1212 unlock_super(sb); /* akpm: sigh */
1213 ext3_orphan_cleanup(sb, es);
1215 EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
1217 printk (KERN_INFO "EXT3-fs: recovery complete.\n");
1218 ext3_mark_recovery_complete(sb, es);
1219 printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
1220 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
1221 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
1227 journal_destroy(sbi->s_journal);
1229 for (i = 0; i < db_count; i++)
1230 brelse(sbi->s_group_desc[i]);
1231 kfree(sbi->s_group_desc);
1233 ext3_blkdev_remove(sbi);
1240 * Setup any per-fs journal parameters now. We'll do this both on
1241 * initial mount, once the journal has been initialised but before we've
1242 * done any recovery; and again on any subsequent remount.
1244 static void ext3_init_journal_params(struct ext3_sb_info *sbi,
1247 if (sbi->s_commit_interval)
1248 journal->j_commit_interval = sbi->s_commit_interval;
1249 /* We could also set up an ext3-specific default for the commit
1250 * interval here, but for now we'll just fall back to the jbd
1255 static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
1257 struct inode *journal_inode;
1260 /* First, test for the existence of a valid inode on disk. Bad
1261 * things happen if we iget() an unused inode, as the subsequent
1262 * iput() will try to delete it. */
1264 journal_inode = iget(sb, journal_inum);
1265 if (!journal_inode) {
1266 printk(KERN_ERR "EXT3-fs: no journal found.\n");
1269 if (!journal_inode->i_nlink) {
1270 make_bad_inode(journal_inode);
1271 iput(journal_inode);
1272 printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
1276 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1277 journal_inode, journal_inode->i_size);
1278 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
1279 printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
1280 iput(journal_inode);
1284 journal = journal_init_inode(journal_inode);
1286 printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
1287 iput(journal_inode);
1289 ext3_init_journal_params(EXT3_SB(sb), journal);
1293 static journal_t *ext3_get_dev_journal(struct super_block *sb,
1296 struct buffer_head * bh;
1300 int hblock, blocksize;
1301 unsigned long sb_block;
1302 unsigned long offset;
1303 kdev_t journal_dev = to_kdev_t(dev);
1304 struct ext3_super_block * es;
1305 struct block_device *bdev;
1307 bdev = ext3_blkdev_get(journal_dev);
1311 blocksize = sb->s_blocksize;
1312 hblock = get_hardsect_size(journal_dev);
1313 if (blocksize < hblock) {
1315 "EXT3-fs: blocksize too small for journal device.\n");
1319 sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
1320 offset = EXT3_MIN_BLOCK_SIZE % blocksize;
1321 set_blocksize(dev, blocksize);
1322 if (!(bh = bread(dev, sb_block, blocksize))) {
1323 printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
1324 "external journal\n");
1328 es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1329 if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
1330 !(le32_to_cpu(es->s_feature_incompat) &
1331 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
1332 printk(KERN_ERR "EXT3-fs: external journal has "
1333 "bad superblock\n");
1338 if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
1339 printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
1344 len = le32_to_cpu(es->s_blocks_count);
1345 start = sb_block + 1;
1346 brelse(bh); /* we're done with the superblock */
1348 journal = journal_init_dev(journal_dev, sb->s_dev,
1349 start, len, blocksize);
1351 printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
1354 ll_rw_block(READ, 1, &journal->j_sb_buffer);
1355 wait_on_buffer(journal->j_sb_buffer);
1356 if (!buffer_uptodate(journal->j_sb_buffer)) {
1357 printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
1360 if (ntohl(journal->j_superblock->s_nr_users) != 1) {
1361 printk(KERN_ERR "EXT3-fs: External journal has more than one "
1362 "user (unsupported) - %d\n",
1363 ntohl(journal->j_superblock->s_nr_users));
1366 EXT3_SB(sb)->journal_bdev = bdev;
1367 ext3_init_journal_params(EXT3_SB(sb), journal);
1370 journal_destroy(journal);
1372 ext3_blkdev_put(bdev);
1376 static int ext3_load_journal(struct super_block * sb,
1377 struct ext3_super_block * es)
1380 int journal_inum = le32_to_cpu(es->s_journal_inum);
1381 int journal_dev = le32_to_cpu(es->s_journal_dev);
1383 int really_read_only;
1385 really_read_only = is_read_only(sb->s_dev);
1388 * Are we loading a blank journal or performing recovery after a
1389 * crash? For recovery, we need to check in advance whether we
1390 * can get read-write access to the device.
1393 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
1394 if (sb->s_flags & MS_RDONLY) {
1395 printk(KERN_INFO "EXT3-fs: INFO: recovery "
1396 "required on readonly filesystem.\n");
1397 if (really_read_only) {
1398 printk(KERN_ERR "EXT3-fs: write access "
1399 "unavailable, cannot proceed.\n");
1402 printk (KERN_INFO "EXT3-fs: write access will "
1403 "be enabled during recovery.\n");
1407 if (journal_inum && journal_dev) {
1408 printk(KERN_ERR "EXT3-fs: filesystem has both journal "
1409 "and inode journals!\n");
1414 if (!(journal = ext3_get_journal(sb, journal_inum)))
1417 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
1422 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
1423 err = journal_update_format(journal);
1425 printk(KERN_ERR "EXT3-fs: error updating journal.\n");
1426 journal_destroy(journal);
1431 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
1432 err = journal_wipe(journal, !really_read_only);
1434 err = journal_load(journal);
1437 printk(KERN_ERR "EXT3-fs: error loading journal.\n");
1438 journal_destroy(journal);
1442 EXT3_SB(sb)->s_journal = journal;
1443 ext3_clear_journal_err(sb, es);
1447 static int ext3_create_journal(struct super_block * sb,
1448 struct ext3_super_block * es,
1453 if (sb->s_flags & MS_RDONLY) {
1454 printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
1455 "create journal.\n");
1459 if (!(journal = ext3_get_journal(sb, journal_inum)))
1462 printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
1465 if (journal_create(journal)) {
1466 printk(KERN_ERR "EXT3-fs: error creating journal.\n");
1467 journal_destroy(journal);
1471 EXT3_SB(sb)->s_journal = journal;
1473 ext3_update_dynamic_rev(sb);
1474 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1475 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
1477 es->s_journal_inum = cpu_to_le32(journal_inum);
1480 /* Make sure we flush the recovery flag to disk. */
1481 ext3_commit_super(sb, es, 1);
1486 static void ext3_commit_super (struct super_block * sb,
1487 struct ext3_super_block * es,
1490 es->s_wtime = cpu_to_le32(CURRENT_TIME);
1491 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "marking dirty");
1492 mark_buffer_dirty(sb->u.ext3_sb.s_sbh);
1494 ll_rw_block(WRITE, 1, &sb->u.ext3_sb.s_sbh);
1495 wait_on_buffer(sb->u.ext3_sb.s_sbh);
1501 * Have we just finished recovery? If so, and if we are mounting (or
1502 * remounting) the filesystem readonly, then we will end up with a
1503 * consistent fs on disk. Record that fact.
1505 static void ext3_mark_recovery_complete(struct super_block * sb,
1506 struct ext3_super_block * es)
1508 journal_flush(EXT3_SB(sb)->s_journal);
1509 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
1510 sb->s_flags & MS_RDONLY) {
1511 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1513 ext3_commit_super(sb, es, 1);
1518 * If we are mounting (or read-write remounting) a filesystem whose journal
1519 * has recorded an error from a previous lifetime, move that error to the
1520 * main filesystem now.
1522 static void ext3_clear_journal_err(struct super_block * sb,
1523 struct ext3_super_block * es)
1529 journal = EXT3_SB(sb)->s_journal;
1532 * Now check for any error status which may have been recorded in the
1533 * journal by a prior ext3_error() or ext3_abort()
1536 j_errno = journal_errno(journal);
1540 errstr = ext3_decode_error(sb, j_errno, nbuf);
1541 ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
1542 "from previous mount: %s", errstr);
1543 ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
1544 "filesystem check.");
1546 sb->u.ext3_sb.s_mount_state |= EXT3_ERROR_FS;
1547 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
1548 ext3_commit_super (sb, es, 1);
1550 journal_clear_err(journal);
1555 * Force the running and committing transactions to commit,
1556 * and wait on the commit.
1558 int ext3_force_commit(struct super_block *sb)
1563 if (sb->s_flags & MS_RDONLY)
1566 journal = EXT3_SB(sb)->s_journal;
1568 lock_kernel(); /* important: lock down j_running_transaction */
1569 ret = ext3_journal_force_commit(journal);
1575 * Ext3 always journals updates to the superblock itself, so we don't
1576 * have to propagate any other updates to the superblock on disk at this
1577 * point. Just start an async writeback to get the buffers on their way
1580 * This implicitly triggers the writebehind on sync().
1583 void ext3_write_super (struct super_block * sb)
1585 if (down_trylock(&sb->s_lock) == 0)
1588 log_start_commit(EXT3_SB(sb)->s_journal, NULL);
1591 static int ext3_sync_fs(struct super_block *sb)
1596 target = log_start_commit(EXT3_SB(sb)->s_journal, NULL);
1597 log_wait_commit(EXT3_SB(sb)->s_journal, target);
1602 * LVM calls this function before a (read-only) snapshot is created. This
1603 * gives us a chance to flush the journal completely and mark the fs clean.
1605 void ext3_write_super_lockfs(struct super_block *sb)
1609 lock_kernel(); /* 2.4.5 forgot to do this for us */
1610 if (!(sb->s_flags & MS_RDONLY)) {
1611 journal_t *journal = EXT3_SB(sb)->s_journal;
1613 /* Now we set up the journal barrier. */
1615 journal_lock_updates(journal);
1616 journal_flush(journal);
1619 /* Journal blocked and flushed, clear needs_recovery flag. */
1620 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1621 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1627 * Called by LVM after the snapshot is done. We need to reset the RECOVER
1628 * flag here, even though the filesystem is not technically dirty yet.
1630 void ext3_unlockfs(struct super_block *sb)
1632 if (!(sb->s_flags & MS_RDONLY)) {
1635 /* Reser the needs_recovery flag before the fs is unlocked. */
1636 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1637 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1639 journal_unlock_updates(EXT3_SB(sb)->s_journal);
1644 int ext3_remount (struct super_block * sb, int * flags, char * data)
1646 struct ext3_super_block * es;
1647 struct ext3_sb_info *sbi = EXT3_SB(sb);
1653 * Allow the "check" option to be passed as a remount option.
1655 if (!parse_options(data, &tmp, sbi, &tmp, 1))
1658 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
1659 ext3_abort(sb, __FUNCTION__, "Abort forced by user");
1663 ext3_init_journal_params(sbi, sbi->s_journal);
1665 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1666 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
1669 if (*flags & MS_RDONLY) {
1671 * First of all, the unconditional stuff we have to do
1672 * to disable replay of the journal when we next remount
1674 sb->s_flags |= MS_RDONLY;
1677 * OK, test if we are remounting a valid rw partition
1678 * readonly, and if so set the rdonly flag and then
1679 * mark the partition as valid again.
1681 if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
1682 (sbi->s_mount_state & EXT3_VALID_FS))
1683 es->s_state = cpu_to_le16(sbi->s_mount_state);
1685 ext3_mark_recovery_complete(sb, es);
1688 if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
1689 ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
1690 printk(KERN_WARNING "EXT3-fs: %s: couldn't "
1691 "remount RDWR because of unsupported "
1692 "optional features (%x).\n",
1693 bdevname(sb->s_dev), ret);
1697 * Mounting a RDONLY partition read-write, so reread
1698 * and store the current valid flag. (It may have
1699 * been changed by e2fsck since we originally mounted
1702 ext3_clear_journal_err(sb, es);
1703 sbi->s_mount_state = le16_to_cpu(es->s_state);
1704 if (!ext3_setup_super (sb, es, 0))
1705 sb->s_flags &= ~MS_RDONLY;
1712 int ext3_statfs (struct super_block * sb, struct statfs * buf)
1714 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
1715 unsigned long overhead;
1718 if (test_opt (sb, MINIX_DF))
1722 * Compute the overhead (FS structures)
1726 * All of the blocks before first_data_block are
1729 overhead = le32_to_cpu(es->s_first_data_block);
1732 * Add the overhead attributed to the superblock and
1733 * block group descriptors. If the sparse superblocks
1734 * feature is turned on, then not all groups have this.
1736 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1737 overhead += ext3_bg_has_super(sb, i) +
1738 ext3_bg_num_gdb(sb, i);
1741 * Every block group has an inode bitmap, a block
1742 * bitmap, and an inode table.
1744 overhead += (EXT3_SB(sb)->s_groups_count *
1745 (2 + EXT3_SB(sb)->s_itb_per_group));
1748 buf->f_type = EXT3_SUPER_MAGIC;
1749 buf->f_bsize = sb->s_blocksize;
1750 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
1751 buf->f_bfree = ext3_count_free_blocks (sb);
1752 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
1753 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
1755 buf->f_files = le32_to_cpu(es->s_inodes_count);
1756 buf->f_ffree = ext3_count_free_inodes (sb);
1757 buf->f_namelen = EXT3_NAME_LEN;
1761 static DECLARE_FSTYPE_DEV(ext3_fs_type, "ext3", ext3_read_super);
1763 static int __init init_ext3_fs(void)
1765 return register_filesystem(&ext3_fs_type);
1768 static void __exit exit_ext3_fs(void)
1770 unregister_filesystem(&ext3_fs_type);
1775 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
1776 MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
1777 MODULE_LICENSE("GPL");
1778 module_init(init_ext3_fs)
1779 module_exit(exit_ext3_fs)