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 struct super_block * ext2_read_super (struct super_block * sb, void * data,
403 struct buffer_head * bh;
404 struct ext2_super_block * es;
405 unsigned long sb_block = 1;
406 unsigned short resuid = EXT2_DEF_RESUID;
407 unsigned short resgid = EXT2_DEF_RESGID;
408 unsigned long logic_sb_block = 1;
409 unsigned long offset = 0;
410 kdev_t dev = sb->s_dev;
411 int blocksize = BLOCK_SIZE;
416 * See what the current blocksize for the device is, and
417 * use that as the blocksize. Otherwise (or if the blocksize
418 * is smaller than the default) use the default.
419 * This is important for devices that have a hardware
420 * sectorsize that is larger than the default.
422 blocksize = get_hardsect_size(dev);
423 if(blocksize < BLOCK_SIZE )
424 blocksize = BLOCK_SIZE;
426 sb->u.ext2_sb.s_mount_opt = 0;
427 if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
428 &sb->u.ext2_sb.s_mount_opt)) {
432 if (set_blocksize(dev, blocksize) < 0) {
433 printk ("EXT2-fs: unable to set blocksize %d\n", blocksize);
436 sb->s_blocksize = blocksize;
439 * If the superblock doesn't start on a sector boundary,
440 * calculate the offset. FIXME(eric) this doesn't make sense
441 * that we would have to do this.
443 if (blocksize != BLOCK_SIZE) {
444 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
445 offset = (sb_block*BLOCK_SIZE) % blocksize;
448 if (!(bh = sb_bread(sb, logic_sb_block))) {
449 printk ("EXT2-fs: unable to read superblock\n");
453 * Note: s_es must be initialized as soon as possible because
454 * some ext2 macro-instructions depend on its value
456 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
457 sb->u.ext2_sb.s_es = es;
458 sb->s_magic = le16_to_cpu(es->s_magic);
459 if (sb->s_magic != EXT2_SUPER_MAGIC) {
461 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
465 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
466 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
467 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
468 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
469 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
470 "running e2fsck is recommended\n");
472 * Check feature flags regardless of the revision level, since we
473 * previously didn't change the revision level when setting the flags,
474 * so there is a chance incompat flags are set on a rev 0 filesystem.
476 if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
477 printk("EXT2-fs: %s: couldn't mount because of "
478 "unsupported optional features (%x).\n",
482 if (!(sb->s_flags & MS_RDONLY) &&
483 (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
484 printk("EXT2-fs: %s: couldn't mount RDWR because of "
485 "unsupported optional features (%x).\n",
489 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
490 ext2_warning(sb, __FUNCTION__,
491 "mounting ext3 filesystem as ext2\n");
492 sb->s_blocksize_bits =
493 le32_to_cpu(EXT2_SB(sb)->s_es->s_log_block_size) + 10;
494 sb->s_blocksize = 1 << sb->s_blocksize_bits;
496 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
498 /* If the blocksize doesn't match, re-read the thing.. */
499 if (sb->s_blocksize != blocksize) {
500 blocksize = sb->s_blocksize;
503 if (set_blocksize(dev, blocksize) < 0) {
504 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
508 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
509 offset = (sb_block*BLOCK_SIZE) % blocksize;
510 bh = sb_bread(sb, logic_sb_block);
512 printk("EXT2-fs: Couldn't read superblock on "
516 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
517 sb->u.ext2_sb.s_es = es;
518 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
519 printk ("EXT2-fs: Magic mismatch, very weird !\n");
524 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
525 sb->u.ext2_sb.s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
526 sb->u.ext2_sb.s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
528 sb->u.ext2_sb.s_inode_size = le16_to_cpu(es->s_inode_size);
529 sb->u.ext2_sb.s_first_ino = le32_to_cpu(es->s_first_ino);
530 if (sb->u.ext2_sb.s_inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
531 printk ("EXT2-fs: unsupported inode size: %d\n",
532 sb->u.ext2_sb.s_inode_size);
536 sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
537 le32_to_cpu(es->s_log_frag_size);
538 if (sb->u.ext2_sb.s_frag_size)
539 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
540 sb->u.ext2_sb.s_frag_size;
543 sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
544 sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
545 sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
546 sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
548 sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
549 sb->u.ext2_sb.s_inodes_per_block;
550 sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
551 sizeof (struct ext2_group_desc);
552 sb->u.ext2_sb.s_sbh = bh;
553 if (resuid != EXT2_DEF_RESUID)
554 sb->u.ext2_sb.s_resuid = resuid;
556 sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
557 if (resgid != EXT2_DEF_RESGID)
558 sb->u.ext2_sb.s_resgid = resgid;
560 sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
561 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
562 sb->u.ext2_sb.s_addr_per_block_bits =
563 log2 (EXT2_ADDR_PER_BLOCK(sb));
564 sb->u.ext2_sb.s_desc_per_block_bits =
565 log2 (EXT2_DESC_PER_BLOCK(sb));
566 if (sb->s_magic != EXT2_SUPER_MAGIC) {
568 printk ("VFS: Can't find an ext2 filesystem on dev "
573 if (sb->s_blocksize != bh->b_size) {
575 printk ("VFS: Unsupported blocksize on dev "
576 "%s.\n", bdevname(dev));
580 if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
581 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
582 sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
586 if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
587 printk ("EXT2-fs: #blocks per group too big: %lu\n",
588 sb->u.ext2_sb.s_blocks_per_group);
591 if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
592 printk ("EXT2-fs: #fragments per group too big: %lu\n",
593 sb->u.ext2_sb.s_frags_per_group);
596 if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
597 printk ("EXT2-fs: #inodes per group too big: %lu\n",
598 sb->u.ext2_sb.s_inodes_per_group);
602 sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
603 le32_to_cpu(es->s_first_data_block) +
604 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
605 EXT2_BLOCKS_PER_GROUP(sb);
606 db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
607 EXT2_DESC_PER_BLOCK(sb);
608 sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
609 if (sb->u.ext2_sb.s_group_desc == NULL) {
610 printk ("EXT2-fs: not enough memory\n");
613 for (i = 0; i < db_count; i++) {
614 sb->u.ext2_sb.s_group_desc[i] = sb_bread(sb, logic_sb_block + i + 1);
615 if (!sb->u.ext2_sb.s_group_desc[i]) {
616 for (j = 0; j < i; j++)
617 brelse (sb->u.ext2_sb.s_group_desc[j]);
618 kfree(sb->u.ext2_sb.s_group_desc);
619 printk ("EXT2-fs: unable to read group descriptors\n");
623 if (!ext2_check_descriptors (sb)) {
624 printk ("EXT2-fs: group descriptors corrupted!\n");
628 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
629 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
630 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
631 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
632 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
634 sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
635 sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
636 sb->u.ext2_sb.s_gdb_count = db_count;
638 * set up enough so that it can read an inode
640 sb->s_op = &ext2_sops;
641 sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
642 if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
643 !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
647 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
649 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
652 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
655 for (i = 0; i < db_count; i++)
656 brelse(sb->u.ext2_sb.s_group_desc[i]);
657 kfree(sb->u.ext2_sb.s_group_desc);
663 static void ext2_commit_super (struct super_block * sb,
664 struct ext2_super_block * es)
666 es->s_wtime = cpu_to_le32(CURRENT_TIME);
667 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
671 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
673 es->s_wtime = cpu_to_le32(CURRENT_TIME);
674 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
675 ll_rw_block(WRITE, 1, &EXT2_SB(sb)->s_sbh);
676 wait_on_buffer(EXT2_SB(sb)->s_sbh);
681 * In the second extended file system, it is not necessary to
682 * write the super block since we use a mapping of the
683 * disk super block in a buffer.
685 * However, this function is still used to set the fs valid
686 * flags to 0. We need to set this flag to 0 since the fs
687 * may have been checked while mounted and e2fsck may have
688 * set s_state to EXT2_VALID_FS after some corrections.
691 void ext2_write_super (struct super_block * sb)
693 struct ext2_super_block * es;
695 if (!(sb->s_flags & MS_RDONLY)) {
696 es = sb->u.ext2_sb.s_es;
698 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
699 ext2_debug ("setting valid to 0\n");
700 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
702 es->s_mtime = cpu_to_le32(CURRENT_TIME);
703 ext2_sync_super(sb, es);
705 ext2_commit_super (sb, es);
710 int ext2_remount (struct super_block * sb, int * flags, char * data)
712 struct ext2_super_block * es;
713 unsigned short resuid = sb->u.ext2_sb.s_resuid;
714 unsigned short resgid = sb->u.ext2_sb.s_resgid;
715 unsigned long new_mount_opt;
719 * Allow the "check" option to be passed as a remount option.
721 new_mount_opt = sb->u.ext2_sb.s_mount_opt;
722 if (!parse_options (data, &tmp, &resuid, &resgid,
726 sb->u.ext2_sb.s_mount_opt = new_mount_opt;
727 sb->u.ext2_sb.s_resuid = resuid;
728 sb->u.ext2_sb.s_resgid = resgid;
729 es = sb->u.ext2_sb.s_es;
730 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
732 if (*flags & MS_RDONLY) {
733 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
734 !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
737 * OK, we are remounting a valid rw partition rdonly, so set
738 * the rdonly flag and then mark the partition as valid again.
740 es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
741 es->s_mtime = cpu_to_le32(CURRENT_TIME);
744 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
745 ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
746 printk("EXT2-fs: %s: couldn't remount RDWR because of "
747 "unsupported optional features (%x).\n",
748 bdevname(sb->s_dev), ret);
752 * Mounting a RDONLY partition read-write, so reread and
753 * store the current valid flag. (It may have been changed
754 * by e2fsck since we originally mounted the partition.)
756 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
757 if (!ext2_setup_super (sb, es, 0))
758 sb->s_flags &= ~MS_RDONLY;
760 ext2_sync_super(sb, es);
764 int ext2_statfs (struct super_block * sb, struct statfs * buf)
766 unsigned long overhead;
769 if (test_opt (sb, MINIX_DF))
773 * Compute the overhead (FS structures)
777 * All of the blocks before first_data_block are
780 overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
783 * Add the overhead attributed to the superblock and
784 * block group descriptors. If the sparse superblocks
785 * feature is turned on, then not all groups have this.
787 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++)
788 overhead += ext2_bg_has_super(sb, i) +
789 ext2_bg_num_gdb(sb, i);
792 * Every block group has an inode bitmap, a block
793 * bitmap, and an inode table.
795 overhead += (sb->u.ext2_sb.s_groups_count *
796 (2 + sb->u.ext2_sb.s_itb_per_group));
799 buf->f_type = EXT2_SUPER_MAGIC;
800 buf->f_bsize = sb->s_blocksize;
801 buf->f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
802 buf->f_bfree = ext2_count_free_blocks (sb);
803 buf->f_bavail = buf->f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
804 if (buf->f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
806 buf->f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
807 buf->f_ffree = ext2_count_free_inodes (sb);
808 buf->f_namelen = EXT2_NAME_LEN;
812 static DECLARE_FSTYPE_DEV(ext2_fs_type, "ext2", ext2_read_super);
814 static int __init init_ext2_fs(void)
816 return register_filesystem(&ext2_fs_type);
819 static void __exit exit_ext2_fs(void)
821 unregister_filesystem(&ext2_fs_type);
826 module_init(init_ext2_fs)
827 module_exit(exit_ext2_fs)