2 * linux/fs/ext2/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/ext2_fs.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/locks.h>
27 #include <linux/blkdev.h>
28 #include <asm/uaccess.h>
31 static void ext2_sync_super(struct super_block *sb,
32 struct ext2_super_block *es);
34 static char error_buf[1024];
36 void ext2_error (struct super_block * sb, const char * function,
37 const char * fmt, ...)
40 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
42 if (!(sb->s_flags & MS_RDONLY)) {
43 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
45 cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
46 ext2_sync_super(sb, es);
49 vsprintf (error_buf, fmt, args);
51 if (test_opt (sb, ERRORS_PANIC) ||
52 (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_PANIC &&
53 !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
54 panic ("EXT2-fs panic (device %s): %s: %s\n",
55 bdevname(sb->s_dev), function, error_buf);
56 printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
57 bdevname(sb->s_dev), function, error_buf);
58 if (test_opt (sb, ERRORS_RO) ||
59 (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_RO &&
60 !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
61 printk ("Remounting filesystem read-only\n");
62 sb->s_flags |= MS_RDONLY;
66 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
67 const char * fmt, ...)
71 if (!(sb->s_flags & MS_RDONLY)) {
72 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
73 sb->u.ext2_sb.s_es->s_state =
74 cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
75 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
79 vsprintf (error_buf, fmt, args);
81 sb->s_flags |= MS_RDONLY;
82 panic ("EXT2-fs panic (device %s): %s: %s\n",
83 bdevname(sb->s_dev), function, error_buf);
86 void ext2_warning (struct super_block * sb, const char * function,
87 const char * fmt, ...)
92 vsprintf (error_buf, fmt, args);
94 printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
95 bdevname(sb->s_dev), function, error_buf);
98 void ext2_update_dynamic_rev(struct super_block *sb)
100 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
102 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
105 ext2_warning(sb, __FUNCTION__,
106 "updating to rev %d because of new feature flag, "
107 "running e2fsck is recommended",
110 es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
111 es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
112 es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
113 /* leave es->s_feature_*compat flags alone */
114 /* es->s_uuid will be set by e2fsck if empty */
117 * The rest of the superblock fields should be zero, and if not it
118 * means they are likely already in use, so leave them alone. We
119 * can leave it up to e2fsck to clean up any inconsistencies there.
123 void ext2_put_super (struct super_block * sb)
128 if (!(sb->s_flags & MS_RDONLY)) {
129 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
131 es->s_state = le16_to_cpu(EXT2_SB(sb)->s_mount_state);
132 ext2_sync_super(sb, es);
134 db_count = EXT2_SB(sb)->s_gdb_count;
135 for (i = 0; i < db_count; i++)
136 if (sb->u.ext2_sb.s_group_desc[i])
137 brelse (sb->u.ext2_sb.s_group_desc[i]);
138 kfree(sb->u.ext2_sb.s_group_desc);
139 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
140 if (sb->u.ext2_sb.s_inode_bitmap[i])
141 brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
142 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
143 if (sb->u.ext2_sb.s_block_bitmap[i])
144 brelse (sb->u.ext2_sb.s_block_bitmap[i]);
145 brelse (sb->u.ext2_sb.s_sbh);
150 static struct super_operations ext2_sops = {
151 read_inode: ext2_read_inode,
152 write_inode: ext2_write_inode,
153 put_inode: ext2_put_inode,
154 delete_inode: ext2_delete_inode,
155 put_super: ext2_put_super,
156 write_super: ext2_write_super,
158 remount_fs: ext2_remount,
162 * This function has been shamelessly adapted from the msdos fs
164 static int parse_options (char * options, unsigned long * sb_block,
165 unsigned short *resuid, unsigned short * resgid,
166 unsigned long * mount_options)
173 for (this_char = strtok (options, ",");
175 this_char = strtok (NULL, ",")) {
176 if ((value = strchr (this_char, '=')) != NULL)
178 if (!strcmp (this_char, "bsddf"))
179 clear_opt (*mount_options, MINIX_DF);
180 else if (!strcmp (this_char, "nouid32")) {
181 set_opt (*mount_options, NO_UID32);
183 else if (!strcmp (this_char, "check")) {
184 if (!value || !*value || !strcmp (value, "none"))
185 clear_opt (*mount_options, CHECK);
187 #ifdef CONFIG_EXT2_CHECK
188 set_opt (*mount_options, CHECK);
190 printk("EXT2 Check option not supported\n");
193 else if (!strcmp (this_char, "debug"))
194 set_opt (*mount_options, DEBUG);
195 else if (!strcmp (this_char, "errors")) {
196 if (!value || !*value) {
197 printk ("EXT2-fs: the errors option requires "
201 if (!strcmp (value, "continue")) {
202 clear_opt (*mount_options, ERRORS_RO);
203 clear_opt (*mount_options, ERRORS_PANIC);
204 set_opt (*mount_options, ERRORS_CONT);
206 else if (!strcmp (value, "remount-ro")) {
207 clear_opt (*mount_options, ERRORS_CONT);
208 clear_opt (*mount_options, ERRORS_PANIC);
209 set_opt (*mount_options, ERRORS_RO);
211 else if (!strcmp (value, "panic")) {
212 clear_opt (*mount_options, ERRORS_CONT);
213 clear_opt (*mount_options, ERRORS_RO);
214 set_opt (*mount_options, ERRORS_PANIC);
217 printk ("EXT2-fs: Invalid errors option: %s\n",
222 else if (!strcmp (this_char, "grpid") ||
223 !strcmp (this_char, "bsdgroups"))
224 set_opt (*mount_options, GRPID);
225 else if (!strcmp (this_char, "minixdf"))
226 set_opt (*mount_options, MINIX_DF);
227 else if (!strcmp (this_char, "nocheck"))
228 clear_opt (*mount_options, CHECK);
229 else if (!strcmp (this_char, "nogrpid") ||
230 !strcmp (this_char, "sysvgroups"))
231 clear_opt (*mount_options, GRPID);
232 else if (!strcmp (this_char, "resgid")) {
233 if (!value || !*value) {
234 printk ("EXT2-fs: the resgid option requires "
238 *resgid = simple_strtoul (value, &value, 0);
240 printk ("EXT2-fs: Invalid resgid option: %s\n",
245 else if (!strcmp (this_char, "resuid")) {
246 if (!value || !*value) {
247 printk ("EXT2-fs: the resuid option requires "
251 *resuid = simple_strtoul (value, &value, 0);
253 printk ("EXT2-fs: Invalid resuid option: %s\n",
258 else if (!strcmp (this_char, "sb")) {
259 if (!value || !*value) {
260 printk ("EXT2-fs: the sb option requires "
264 *sb_block = simple_strtoul (value, &value, 0);
266 printk ("EXT2-fs: Invalid sb option: %s\n",
271 /* Silently ignore the quota options */
272 else if (!strcmp (this_char, "grpquota")
273 || !strcmp (this_char, "noquota")
274 || !strcmp (this_char, "quota")
275 || !strcmp (this_char, "usrquota"))
276 /* Don't do anything ;-) */ ;
278 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
285 static int ext2_setup_super (struct super_block * sb,
286 struct ext2_super_block * es,
290 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
291 printk ("EXT2-fs warning: revision level too high, "
292 "forcing read-only mode\n");
297 if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
298 printk ("EXT2-fs warning: mounting unchecked fs, "
299 "running e2fsck is recommended\n");
300 else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
301 printk ("EXT2-fs warning: mounting fs with errors, "
302 "running e2fsck is recommended\n");
303 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
304 le16_to_cpu(es->s_mnt_count) >=
305 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
306 printk ("EXT2-fs warning: maximal mount count reached, "
307 "running e2fsck is recommended\n");
308 else if (le32_to_cpu(es->s_checkinterval) &&
309 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
310 printk ("EXT2-fs warning: checktime reached, "
311 "running e2fsck is recommended\n");
312 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
313 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
314 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
315 ext2_write_super(sb);
316 if (test_opt (sb, DEBUG))
317 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
318 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
319 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
320 sb->u.ext2_sb.s_frag_size,
321 sb->u.ext2_sb.s_groups_count,
322 EXT2_BLOCKS_PER_GROUP(sb),
323 EXT2_INODES_PER_GROUP(sb),
324 sb->u.ext2_sb.s_mount_opt);
325 #ifdef CONFIG_EXT2_CHECK
326 if (test_opt (sb, CHECK)) {
327 ext2_check_blocks_bitmap (sb);
328 ext2_check_inodes_bitmap (sb);
334 static int ext2_check_descriptors (struct super_block * sb)
338 unsigned long block = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
339 struct ext2_group_desc * gdp = NULL;
341 ext2_debug ("Checking group descriptors");
343 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
345 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
346 gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
347 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
348 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
350 ext2_error (sb, "ext2_check_descriptors",
351 "Block bitmap for group %d"
352 " not in group (block %lu)!",
353 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
356 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
357 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
359 ext2_error (sb, "ext2_check_descriptors",
360 "Inode bitmap for group %d"
361 " not in group (block %lu)!",
362 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
365 if (le32_to_cpu(gdp->bg_inode_table) < block ||
366 le32_to_cpu(gdp->bg_inode_table) + sb->u.ext2_sb.s_itb_per_group >=
367 block + EXT2_BLOCKS_PER_GROUP(sb))
369 ext2_error (sb, "ext2_check_descriptors",
370 "Inode table for group %d"
371 " not in group (block %lu)!",
372 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
375 block += EXT2_BLOCKS_PER_GROUP(sb);
381 #define log2(n) ffz(~(n))
384 * Maximal file size. There is a direct, and {,double-,triple-}indirect
385 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
386 * We need to be 1 filesystem block less than the 2^32 sector limit.
388 static loff_t ext2_max_size(int bits)
390 loff_t res = EXT2_NDIR_BLOCKS;
391 res += 1LL << (bits-2);
392 res += 1LL << (2*(bits-2));
393 res += 1LL << (3*(bits-2));
395 if (res > (512LL << 32) - (1 << bits))
396 res = (512LL << 32) - (1 << bits);
400 static unsigned long descriptor_loc(struct super_block *sb,
401 unsigned long logic_sb_block,
404 struct ext2_sb_info *sbi = EXT2_SB(sb);
405 unsigned long bg, first_data_block, first_meta_bg;
408 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
409 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
411 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
413 return (logic_sb_block + nr + 1);
414 bg = sbi->s_desc_per_block * nr;
415 if (ext2_bg_has_super(sb, bg))
417 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
420 struct super_block * ext2_read_super (struct super_block * sb, void * data,
423 struct buffer_head * bh;
424 struct ext2_sb_info * sbi = EXT2_SB(sb);
425 struct ext2_super_block * es;
426 unsigned long sb_block = 1;
427 unsigned short resuid = EXT2_DEF_RESUID;
428 unsigned short resgid = EXT2_DEF_RESGID;
430 unsigned long logic_sb_block;
431 unsigned long offset = 0;
432 kdev_t dev = sb->s_dev;
433 int blocksize = BLOCK_SIZE;
438 * See what the current blocksize for the device is, and
439 * use that as the blocksize. Otherwise (or if the blocksize
440 * is smaller than the default) use the default.
441 * This is important for devices that have a hardware
442 * sectorsize that is larger than the default.
444 blocksize = get_hardsect_size(dev);
445 if(blocksize < BLOCK_SIZE )
446 blocksize = BLOCK_SIZE;
448 sb->u.ext2_sb.s_mount_opt = 0;
449 if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
450 &sb->u.ext2_sb.s_mount_opt)) {
454 if (set_blocksize(dev, blocksize) < 0) {
455 printk ("EXT2-fs: unable to set blocksize %d\n", blocksize);
458 sb->s_blocksize = blocksize;
461 * If the superblock doesn't start on a sector boundary,
462 * calculate the offset. FIXME(eric) this doesn't make sense
463 * that we would have to do this.
465 if (blocksize != BLOCK_SIZE) {
466 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
467 offset = (sb_block*BLOCK_SIZE) % blocksize;
469 logic_sb_block = sb_block;
472 if (!(bh = sb_bread(sb, logic_sb_block))) {
473 printk ("EXT2-fs: unable to read superblock\n");
477 * Note: s_es must be initialized as soon as possible because
478 * some ext2 macro-instructions depend on its value
480 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
481 sb->u.ext2_sb.s_es = es;
482 sb->s_magic = le16_to_cpu(es->s_magic);
483 if (sb->s_magic != EXT2_SUPER_MAGIC)
485 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
486 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
487 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
488 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
489 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
490 "running e2fsck is recommended\n");
492 * Check feature flags regardless of the revision level, since we
493 * previously didn't change the revision level when setting the flags,
494 * so there is a chance incompat flags are set on a rev 0 filesystem.
496 if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
497 printk("EXT2-fs: %s: couldn't mount because of "
498 "unsupported optional features (%x).\n",
502 if (!(sb->s_flags & MS_RDONLY) &&
503 (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
504 printk("EXT2-fs: %s: couldn't mount RDWR because of "
505 "unsupported optional features (%x).\n",
509 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
510 ext2_warning(sb, __FUNCTION__,
511 "mounting ext3 filesystem as ext2\n");
512 sb->s_blocksize_bits =
513 le32_to_cpu(EXT2_SB(sb)->s_es->s_log_block_size) + 10;
514 sb->s_blocksize = 1 << sb->s_blocksize_bits;
516 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
518 /* If the blocksize doesn't match, re-read the thing.. */
519 if (sb->s_blocksize != blocksize) {
520 blocksize = sb->s_blocksize;
523 if (set_blocksize(dev, blocksize) < 0) {
524 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
528 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
529 offset = (sb_block*BLOCK_SIZE) % blocksize;
530 bh = sb_bread(sb, logic_sb_block);
532 printk("EXT2-fs: Couldn't read superblock on "
536 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
537 sb->u.ext2_sb.s_es = es;
538 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
539 printk ("EXT2-fs: Magic mismatch, very weird !\n");
544 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
545 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
546 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
548 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
549 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
550 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
551 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
552 (sbi->s_inode_size > blocksize)) {
553 printk ("EXT2-fs: unsupported inode size: %d\n",
558 sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
559 le32_to_cpu(es->s_log_frag_size);
560 if (sb->u.ext2_sb.s_frag_size == 0)
562 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
563 sb->u.ext2_sb.s_frag_size;
564 sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
565 sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
566 sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
567 if (EXT2_INODE_SIZE(sb) == 0)
569 sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
571 if (sb->u.ext2_sb.s_inodes_per_block == 0)
573 sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
574 sb->u.ext2_sb.s_inodes_per_block;
575 sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
576 sizeof (struct ext2_group_desc);
577 sb->u.ext2_sb.s_sbh = bh;
578 if (resuid != EXT2_DEF_RESUID)
579 sb->u.ext2_sb.s_resuid = resuid;
581 sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
582 if (resgid != EXT2_DEF_RESGID)
583 sb->u.ext2_sb.s_resgid = resgid;
585 sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
586 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
587 sb->u.ext2_sb.s_addr_per_block_bits =
588 log2 (EXT2_ADDR_PER_BLOCK(sb));
589 sb->u.ext2_sb.s_desc_per_block_bits =
590 log2 (EXT2_DESC_PER_BLOCK(sb));
592 if (sb->s_magic != EXT2_SUPER_MAGIC)
595 if (sb->s_blocksize != bh->b_size) {
597 printk ("VFS: Unsupported blocksize on dev "
598 "%s.\n", bdevname(dev));
602 if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
603 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
604 sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
608 if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
609 printk ("EXT2-fs: #blocks per group too big: %lu\n",
610 sb->u.ext2_sb.s_blocks_per_group);
613 if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
614 printk ("EXT2-fs: #fragments per group too big: %lu\n",
615 sb->u.ext2_sb.s_frags_per_group);
618 if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
619 printk ("EXT2-fs: #inodes per group too big: %lu\n",
620 sb->u.ext2_sb.s_inodes_per_group);
624 if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
626 sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
627 le32_to_cpu(es->s_first_data_block) +
628 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
629 EXT2_BLOCKS_PER_GROUP(sb);
630 db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
631 EXT2_DESC_PER_BLOCK(sb);
632 sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
633 if (sb->u.ext2_sb.s_group_desc == NULL) {
634 printk ("EXT2-fs: not enough memory\n");
637 for (i = 0; i < db_count; i++) {
638 block = descriptor_loc(sb, logic_sb_block, i);
639 sbi->s_group_desc[i] = sb_bread(sb, block);
640 if (!sbi->s_group_desc[i]) {
641 for (j = 0; j < i; j++)
642 brelse (sbi->s_group_desc[j]);
643 kfree(sbi->s_group_desc);
644 printk ("EXT2-fs: unable to read group descriptors\n");
648 if (!ext2_check_descriptors (sb)) {
649 printk ("EXT2-fs: group descriptors corrupted!\n");
653 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
654 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
655 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
656 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
657 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
659 sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
660 sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
661 sb->u.ext2_sb.s_gdb_count = db_count;
663 * set up enough so that it can read an inode
665 sb->s_op = &ext2_sops;
666 sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
667 if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
668 !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
672 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
674 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
677 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
681 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
685 for (i = 0; i < db_count; i++)
686 brelse(sb->u.ext2_sb.s_group_desc[i]);
687 kfree(sb->u.ext2_sb.s_group_desc);
693 static void ext2_commit_super (struct super_block * sb,
694 struct ext2_super_block * es)
696 es->s_wtime = cpu_to_le32(CURRENT_TIME);
697 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
701 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
703 es->s_wtime = cpu_to_le32(CURRENT_TIME);
704 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
705 ll_rw_block(WRITE, 1, &EXT2_SB(sb)->s_sbh);
706 wait_on_buffer(EXT2_SB(sb)->s_sbh);
711 * In the second extended file system, it is not necessary to
712 * write the super block since we use a mapping of the
713 * disk super block in a buffer.
715 * However, this function is still used to set the fs valid
716 * flags to 0. We need to set this flag to 0 since the fs
717 * may have been checked while mounted and e2fsck may have
718 * set s_state to EXT2_VALID_FS after some corrections.
721 void ext2_write_super (struct super_block * sb)
723 struct ext2_super_block * es;
725 if (!(sb->s_flags & MS_RDONLY)) {
726 es = sb->u.ext2_sb.s_es;
728 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
729 ext2_debug ("setting valid to 0\n");
730 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
732 es->s_mtime = cpu_to_le32(CURRENT_TIME);
733 ext2_sync_super(sb, es);
735 ext2_commit_super (sb, es);
740 int ext2_remount (struct super_block * sb, int * flags, char * data)
742 struct ext2_super_block * es;
743 unsigned short resuid = sb->u.ext2_sb.s_resuid;
744 unsigned short resgid = sb->u.ext2_sb.s_resgid;
745 unsigned long new_mount_opt;
749 * Allow the "check" option to be passed as a remount option.
751 new_mount_opt = sb->u.ext2_sb.s_mount_opt;
752 if (!parse_options (data, &tmp, &resuid, &resgid,
756 sb->u.ext2_sb.s_mount_opt = new_mount_opt;
757 sb->u.ext2_sb.s_resuid = resuid;
758 sb->u.ext2_sb.s_resgid = resgid;
759 es = sb->u.ext2_sb.s_es;
760 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
762 if (*flags & MS_RDONLY) {
763 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
764 !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
767 * OK, we are remounting a valid rw partition rdonly, so set
768 * the rdonly flag and then mark the partition as valid again.
770 es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
771 es->s_mtime = cpu_to_le32(CURRENT_TIME);
774 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
775 ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
776 printk("EXT2-fs: %s: couldn't remount RDWR because of "
777 "unsupported optional features (%x).\n",
778 bdevname(sb->s_dev), ret);
782 * Mounting a RDONLY partition read-write, so reread and
783 * store the current valid flag. (It may have been changed
784 * by e2fsck since we originally mounted the partition.)
786 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
787 if (!ext2_setup_super (sb, es, 0))
788 sb->s_flags &= ~MS_RDONLY;
790 ext2_sync_super(sb, es);
794 int ext2_statfs (struct super_block * sb, struct statfs * buf)
796 unsigned long overhead;
799 if (test_opt (sb, MINIX_DF))
803 * Compute the overhead (FS structures)
807 * All of the blocks before first_data_block are
810 overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
813 * Add the overhead attributed to the superblock and
814 * block group descriptors. If the sparse superblocks
815 * feature is turned on, then not all groups have this.
817 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++)
818 overhead += ext2_bg_has_super(sb, i) +
819 ext2_bg_num_gdb(sb, i);
822 * Every block group has an inode bitmap, a block
823 * bitmap, and an inode table.
825 overhead += (sb->u.ext2_sb.s_groups_count *
826 (2 + sb->u.ext2_sb.s_itb_per_group));
829 buf->f_type = EXT2_SUPER_MAGIC;
830 buf->f_bsize = sb->s_blocksize;
831 buf->f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
832 buf->f_bfree = ext2_count_free_blocks (sb);
833 buf->f_bavail = buf->f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
834 if (buf->f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
836 buf->f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
837 buf->f_ffree = ext2_count_free_inodes (sb);
838 buf->f_namelen = EXT2_NAME_LEN;
842 static DECLARE_FSTYPE_DEV(ext2_fs_type, "ext2", ext2_read_super);
844 static int __init init_ext2_fs(void)
846 return register_filesystem(&ext2_fs_type);
849 static void __exit exit_ext2_fs(void)
851 unregister_filesystem(&ext2_fs_type);
856 module_init(init_ext2_fs)
857 module_exit(exit_ext2_fs)