Linux 2.3.0
[davej-history.git] / fs / ext2 / super.c
blobb7c08a0094b81f387aa77f5d669723c20a14e742
1 /*
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)
9 * from
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
19 #include <linux/module.h>
21 #include <stdarg.h>
23 #include <asm/bitops.h>
24 #include <asm/uaccess.h>
25 #include <asm/system.h>
27 #include <linux/errno.h>
28 #include <linux/fs.h>
29 #include <linux/ext2_fs.h>
30 #include <linux/malloc.h>
31 #include <linux/sched.h>
32 #include <linux/stat.h>
33 #include <linux/string.h>
34 #include <linux/locks.h>
35 #include <linux/blkdev.h>
36 #include <linux/init.h>
38 static char error_buf[1024];
40 void ext2_error (struct super_block * sb, const char * function,
41 const char * fmt, ...)
43 va_list args;
45 if (!(sb->s_flags & MS_RDONLY)) {
46 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
47 sb->u.ext2_sb.s_es->s_state =
48 cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
49 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
50 sb->s_dirt = 1;
52 va_start (args, fmt);
53 vsprintf (error_buf, fmt, args);
54 va_end (args);
55 if (test_opt (sb, ERRORS_PANIC) ||
56 (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_PANIC &&
57 !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
58 panic ("EXT2-fs panic (device %s): %s: %s\n",
59 bdevname(sb->s_dev), function, error_buf);
60 printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
61 bdevname(sb->s_dev), function, error_buf);
62 if (test_opt (sb, ERRORS_RO) ||
63 (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_RO &&
64 !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
65 printk ("Remounting filesystem read-only\n");
66 sb->s_flags |= MS_RDONLY;
70 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
71 const char * fmt, ...)
73 va_list args;
75 if (!(sb->s_flags & MS_RDONLY)) {
76 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
77 sb->u.ext2_sb.s_es->s_state =
78 cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
79 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
80 sb->s_dirt = 1;
82 va_start (args, fmt);
83 vsprintf (error_buf, fmt, args);
84 va_end (args);
85 /* this is to prevent panic from syncing this filesystem */
86 if (sb->s_lock)
87 sb->s_lock=0;
88 sb->s_flags |= MS_RDONLY;
89 panic ("EXT2-fs panic (device %s): %s: %s\n",
90 bdevname(sb->s_dev), function, error_buf);
93 void ext2_warning (struct super_block * sb, const char * function,
94 const char * fmt, ...)
96 va_list args;
98 va_start (args, fmt);
99 vsprintf (error_buf, fmt, args);
100 va_end (args);
101 printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
102 bdevname(sb->s_dev), function, error_buf);
105 void ext2_put_super (struct super_block * sb)
107 int db_count;
108 int i;
110 if (!(sb->s_flags & MS_RDONLY)) {
111 sb->u.ext2_sb.s_es->s_state = le16_to_cpu(sb->u.ext2_sb.s_mount_state);
112 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
114 db_count = sb->u.ext2_sb.s_db_per_group;
115 for (i = 0; i < db_count; i++)
116 if (sb->u.ext2_sb.s_group_desc[i])
117 brelse (sb->u.ext2_sb.s_group_desc[i]);
118 kfree_s (sb->u.ext2_sb.s_group_desc,
119 db_count * sizeof (struct buffer_head *));
120 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
121 if (sb->u.ext2_sb.s_inode_bitmap[i])
122 brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
123 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
124 if (sb->u.ext2_sb.s_block_bitmap[i])
125 brelse (sb->u.ext2_sb.s_block_bitmap[i]);
126 brelse (sb->u.ext2_sb.s_sbh);
128 MOD_DEC_USE_COUNT;
129 return;
132 static struct super_operations ext2_sops = {
133 ext2_read_inode,
134 ext2_write_inode,
135 ext2_put_inode,
136 ext2_delete_inode,
137 NULL,
138 ext2_put_super,
139 ext2_write_super,
140 ext2_statfs,
141 ext2_remount
145 * This function has been shamelessly adapted from the msdos fs
147 static int parse_options (char * options, unsigned long * sb_block,
148 unsigned short *resuid, unsigned short * resgid,
149 unsigned long * mount_options)
151 char * this_char;
152 char * value;
154 if (!options)
155 return 1;
156 for (this_char = strtok (options, ",");
157 this_char != NULL;
158 this_char = strtok (NULL, ",")) {
159 if ((value = strchr (this_char, '=')) != NULL)
160 *value++ = 0;
161 if (!strcmp (this_char, "bsddf"))
162 clear_opt (*mount_options, MINIX_DF);
163 else if (!strcmp (this_char, "check")) {
164 if (!value || !*value)
165 set_opt (*mount_options, CHECK_NORMAL);
166 else if (!strcmp (value, "none")) {
167 clear_opt (*mount_options, CHECK_NORMAL);
168 clear_opt (*mount_options, CHECK_STRICT);
170 else if (!strcmp (value, "normal"))
171 set_opt (*mount_options, CHECK_NORMAL);
172 else if (!strcmp (value, "strict")) {
173 set_opt (*mount_options, CHECK_NORMAL);
174 set_opt (*mount_options, CHECK_STRICT);
176 else {
177 printk ("EXT2-fs: Invalid check option: %s\n",
178 value);
179 return 0;
182 else if (!strcmp (this_char, "debug"))
183 set_opt (*mount_options, DEBUG);
184 else if (!strcmp (this_char, "errors")) {
185 if (!value || !*value) {
186 printk ("EXT2-fs: the errors option requires "
187 "an argument");
188 return 0;
190 if (!strcmp (value, "continue")) {
191 clear_opt (*mount_options, ERRORS_RO);
192 clear_opt (*mount_options, ERRORS_PANIC);
193 set_opt (*mount_options, ERRORS_CONT);
195 else if (!strcmp (value, "remount-ro")) {
196 clear_opt (*mount_options, ERRORS_CONT);
197 clear_opt (*mount_options, ERRORS_PANIC);
198 set_opt (*mount_options, ERRORS_RO);
200 else if (!strcmp (value, "panic")) {
201 clear_opt (*mount_options, ERRORS_CONT);
202 clear_opt (*mount_options, ERRORS_RO);
203 set_opt (*mount_options, ERRORS_PANIC);
205 else {
206 printk ("EXT2-fs: Invalid errors option: %s\n",
207 value);
208 return 0;
211 else if (!strcmp (this_char, "grpid") ||
212 !strcmp (this_char, "bsdgroups"))
213 set_opt (*mount_options, GRPID);
214 else if (!strcmp (this_char, "minixdf"))
215 set_opt (*mount_options, MINIX_DF);
216 else if (!strcmp (this_char, "nocheck")) {
217 clear_opt (*mount_options, CHECK_NORMAL);
218 clear_opt (*mount_options, CHECK_STRICT);
220 else if (!strcmp (this_char, "nogrpid") ||
221 !strcmp (this_char, "sysvgroups"))
222 clear_opt (*mount_options, GRPID);
223 else if (!strcmp (this_char, "resgid")) {
224 if (!value || !*value) {
225 printk ("EXT2-fs: the resgid option requires "
226 "an argument");
227 return 0;
229 *resgid = simple_strtoul (value, &value, 0);
230 if (*value) {
231 printk ("EXT2-fs: Invalid resgid option: %s\n",
232 value);
233 return 0;
236 else if (!strcmp (this_char, "resuid")) {
237 if (!value || !*value) {
238 printk ("EXT2-fs: the resuid option requires "
239 "an argument");
240 return 0;
242 *resuid = simple_strtoul (value, &value, 0);
243 if (*value) {
244 printk ("EXT2-fs: Invalid resuid option: %s\n",
245 value);
246 return 0;
249 else if (!strcmp (this_char, "sb")) {
250 if (!value || !*value) {
251 printk ("EXT2-fs: the sb option requires "
252 "an argument");
253 return 0;
255 *sb_block = simple_strtoul (value, &value, 0);
256 if (*value) {
257 printk ("EXT2-fs: Invalid sb option: %s\n",
258 value);
259 return 0;
262 /* Silently ignore the quota options */
263 else if (!strcmp (this_char, "grpquota")
264 || !strcmp (this_char, "noquota")
265 || !strcmp (this_char, "quota")
266 || !strcmp (this_char, "usrquota"))
267 /* Don't do anything ;-) */ ;
268 else {
269 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
270 return 0;
273 return 1;
276 static void ext2_setup_super (struct super_block * sb,
277 struct ext2_super_block * es)
279 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
280 printk ("EXT2-fs warning: revision level too high, "
281 "forcing read/only mode\n");
282 sb->s_flags |= MS_RDONLY;
284 if (!(sb->s_flags & MS_RDONLY)) {
285 if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
286 printk ("EXT2-fs warning: mounting unchecked fs, "
287 "running e2fsck is recommended\n");
288 else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
289 printk ("EXT2-fs warning: mounting fs with errors, "
290 "running e2fsck is recommended\n");
291 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
292 le16_to_cpu(es->s_mnt_count) >=
293 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
294 printk ("EXT2-fs warning: maximal mount count reached, "
295 "running e2fsck is recommended\n");
296 else if (le32_to_cpu(es->s_checkinterval) &&
297 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
298 printk ("EXT2-fs warning: checktime reached, "
299 "running e2fsck is recommended\n");
300 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT2_VALID_FS);
301 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
302 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
303 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
304 es->s_mtime = cpu_to_le32(CURRENT_TIME);
305 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
306 sb->s_dirt = 1;
307 if (test_opt (sb, DEBUG))
308 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
309 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
310 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
311 sb->u.ext2_sb.s_frag_size,
312 sb->u.ext2_sb.s_groups_count,
313 EXT2_BLOCKS_PER_GROUP(sb),
314 EXT2_INODES_PER_GROUP(sb),
315 sb->u.ext2_sb.s_mount_opt);
316 if (test_opt (sb, CHECK)) {
317 ext2_check_blocks_bitmap (sb);
318 ext2_check_inodes_bitmap (sb);
321 #if 0 /* ibasket's still have unresolved bugs... -DaveM */
323 /* [T. Schoebel-Theuer] This limit should be maintained on disk.
324 * This is just provisionary.
326 sb->s_ibasket_max = 100;
327 #endif
330 static int ext2_check_descriptors (struct super_block * sb)
332 int i;
333 int desc_block = 0;
334 unsigned long block = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
335 struct ext2_group_desc * gdp = NULL;
337 ext2_debug ("Checking group descriptors");
339 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
341 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
342 gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
343 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
344 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
346 ext2_error (sb, "ext2_check_descriptors",
347 "Block bitmap for group %d"
348 " not in group (block %lu)!",
349 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
350 return 0;
352 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
353 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
355 ext2_error (sb, "ext2_check_descriptors",
356 "Inode bitmap for group %d"
357 " not in group (block %lu)!",
358 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
359 return 0;
361 if (le32_to_cpu(gdp->bg_inode_table) < block ||
362 le32_to_cpu(gdp->bg_inode_table) + sb->u.ext2_sb.s_itb_per_group >=
363 block + EXT2_BLOCKS_PER_GROUP(sb))
365 ext2_error (sb, "ext2_check_descriptors",
366 "Inode table for group %d"
367 " not in group (block %lu)!",
368 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
369 return 0;
371 block += EXT2_BLOCKS_PER_GROUP(sb);
372 gdp++;
374 return 1;
377 #define log2(n) ffz(~(n))
379 struct super_block * ext2_read_super (struct super_block * sb, void * data,
380 int silent)
382 struct buffer_head * bh;
383 struct ext2_super_block * es;
384 unsigned long sb_block = 1;
385 unsigned short resuid = EXT2_DEF_RESUID;
386 unsigned short resgid = EXT2_DEF_RESGID;
387 unsigned long logic_sb_block = 1;
388 unsigned long offset = 0;
389 kdev_t dev = sb->s_dev;
390 int blocksize = BLOCK_SIZE;
391 int hblock;
392 int db_count;
393 int i, j;
396 * See what the current blocksize for the device is, and
397 * use that as the blocksize. Otherwise (or if the blocksize
398 * is smaller than the default) use the default.
399 * This is important for devices that have a hardware
400 * sectorsize that is larger than the default.
402 blocksize = get_hardblocksize(dev);
403 if( blocksize == 0 || blocksize < BLOCK_SIZE )
405 blocksize = BLOCK_SIZE;
408 sb->u.ext2_sb.s_mount_opt = 0;
409 set_opt (sb->u.ext2_sb.s_mount_opt, CHECK_NORMAL);
410 if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
411 &sb->u.ext2_sb.s_mount_opt)) {
412 sb->s_dev = 0;
413 return NULL;
416 MOD_INC_USE_COUNT;
417 lock_super (sb);
418 set_blocksize (dev, blocksize);
421 * If the superblock doesn't start on a sector boundary,
422 * calculate the offset. FIXME(eric) this doesn't make sense
423 * that we would have to do this.
425 if (blocksize != BLOCK_SIZE) {
426 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
427 offset = (sb_block*BLOCK_SIZE) % blocksize;
430 if (!(bh = bread (dev, logic_sb_block, blocksize))) {
431 sb->s_dev = 0;
432 unlock_super (sb);
433 printk ("EXT2-fs: unable to read superblock\n");
434 MOD_DEC_USE_COUNT;
435 return NULL;
438 * Note: s_es must be initialized s_es as soon as possible because
439 * some ext2 macro-instructions depend on its value
441 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
442 sb->u.ext2_sb.s_es = es;
443 sb->s_magic = le16_to_cpu(es->s_magic);
444 if (sb->s_magic != EXT2_SUPER_MAGIC) {
445 if (!silent)
446 printk ("VFS: Can't find an ext2 filesystem on dev "
447 "%s.\n", bdevname(dev));
448 failed_mount:
449 sb->s_dev = 0;
450 unlock_super (sb);
451 if (bh)
452 brelse(bh);
453 MOD_DEC_USE_COUNT;
454 return NULL;
456 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV) {
457 if (le32_to_cpu(es->s_feature_incompat) & ~EXT2_FEATURE_INCOMPAT_SUPP) {
458 printk("EXT2-fs: %s: couldn't mount because of "
459 "unsupported optional features.\n",
460 bdevname(dev));
461 goto failed_mount;
463 if (!(sb->s_flags & MS_RDONLY) &&
464 (le32_to_cpu(es->s_feature_ro_compat) & ~EXT2_FEATURE_RO_COMPAT_SUPP)) {
465 printk("EXT2-fs: %s: couldn't mount RDWR because of "
466 "unsupported optional features.\n",
467 bdevname(dev));
468 goto failed_mount;
471 sb->s_blocksize_bits = le32_to_cpu(sb->u.ext2_sb.s_es->s_log_block_size) + 10;
472 sb->s_blocksize = 1 << sb->s_blocksize_bits;
473 if (sb->s_blocksize != BLOCK_SIZE &&
474 (sb->s_blocksize == 1024 || sb->s_blocksize == 2048 ||
475 sb->s_blocksize == 4096)) {
477 * Make sure the blocksize for the filesystem is larger
478 * than the hardware sectorsize for the machine.
480 hblock = get_hardblocksize(dev);
481 if( (hblock != 0)
482 && (sb->s_blocksize < hblock) )
484 printk("EXT2-fs: blocksize too small for device.\n");
485 goto failed_mount;
488 brelse (bh);
489 set_blocksize (dev, sb->s_blocksize);
490 logic_sb_block = (sb_block*BLOCK_SIZE) / sb->s_blocksize;
491 offset = (sb_block*BLOCK_SIZE) % sb->s_blocksize;
492 bh = bread (dev, logic_sb_block, sb->s_blocksize);
493 if(!bh) {
494 printk("EXT2-fs: Couldn't read superblock on "
495 "2nd try.\n");
496 goto failed_mount;
498 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
499 sb->u.ext2_sb.s_es = es;
500 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
501 printk ("EXT2-fs: Magic mismatch, very weird !\n");
502 goto failed_mount;
505 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
506 sb->u.ext2_sb.s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
507 sb->u.ext2_sb.s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
508 } else {
509 sb->u.ext2_sb.s_inode_size = le16_to_cpu(es->s_inode_size);
510 sb->u.ext2_sb.s_first_ino = le32_to_cpu(es->s_first_ino);
511 if (sb->u.ext2_sb.s_inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
512 printk ("EXT2-fs: unsupported inode size: %d\n",
513 sb->u.ext2_sb.s_inode_size);
514 goto failed_mount;
517 sb->u.ext2_sb.s_feature_compat = le32_to_cpu(es->s_feature_compat);
518 sb->u.ext2_sb.s_feature_incompat = le32_to_cpu(es->s_feature_incompat);
519 sb->u.ext2_sb.s_feature_ro_compat = le32_to_cpu(es->s_feature_ro_compat);
520 sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
521 le32_to_cpu(es->s_log_frag_size);
522 if (sb->u.ext2_sb.s_frag_size)
523 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
524 sb->u.ext2_sb.s_frag_size;
525 else
526 sb->s_magic = 0;
527 sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
528 sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
529 sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
530 sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
531 EXT2_INODE_SIZE(sb);
532 sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
533 sb->u.ext2_sb.s_inodes_per_block;
534 sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
535 sizeof (struct ext2_group_desc);
536 sb->u.ext2_sb.s_sbh = bh;
537 if (resuid != EXT2_DEF_RESUID)
538 sb->u.ext2_sb.s_resuid = resuid;
539 else
540 sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
541 if (resgid != EXT2_DEF_RESGID)
542 sb->u.ext2_sb.s_resgid = resgid;
543 else
544 sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
545 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
546 sb->u.ext2_sb.s_addr_per_block_bits =
547 log2 (EXT2_ADDR_PER_BLOCK(sb));
548 sb->u.ext2_sb.s_desc_per_block_bits =
549 log2 (EXT2_DESC_PER_BLOCK(sb));
550 if (sb->s_magic != EXT2_SUPER_MAGIC) {
551 if (!silent)
552 printk ("VFS: Can't find an ext2 filesystem on dev "
553 "%s.\n",
554 bdevname(dev));
555 goto failed_mount;
557 if (sb->s_blocksize != bh->b_size) {
558 if (!silent)
559 printk ("VFS: Unsupported blocksize on dev "
560 "%s.\n", bdevname(dev));
561 goto failed_mount;
564 if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
565 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
566 sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
567 goto failed_mount;
570 if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
571 printk ("EXT2-fs: #blocks per group too big: %lu\n",
572 sb->u.ext2_sb.s_blocks_per_group);
573 goto failed_mount;
575 if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
576 printk ("EXT2-fs: #fragments per group too big: %lu\n",
577 sb->u.ext2_sb.s_frags_per_group);
578 goto failed_mount;
580 if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
581 printk ("EXT2-fs: #inodes per group too big: %lu\n",
582 sb->u.ext2_sb.s_inodes_per_group);
583 goto failed_mount;
586 sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
587 le32_to_cpu(es->s_first_data_block) +
588 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
589 EXT2_BLOCKS_PER_GROUP(sb);
590 db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
591 EXT2_DESC_PER_BLOCK(sb);
592 sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
593 if (sb->u.ext2_sb.s_group_desc == NULL) {
594 printk ("EXT2-fs: not enough memory\n");
595 goto failed_mount;
597 for (i = 0; i < db_count; i++) {
598 sb->u.ext2_sb.s_group_desc[i] = bread (dev, logic_sb_block + i + 1,
599 sb->s_blocksize);
600 if (!sb->u.ext2_sb.s_group_desc[i]) {
601 for (j = 0; j < i; j++)
602 brelse (sb->u.ext2_sb.s_group_desc[j]);
603 kfree_s (sb->u.ext2_sb.s_group_desc,
604 db_count * sizeof (struct buffer_head *));
605 printk ("EXT2-fs: unable to read group descriptors\n");
606 goto failed_mount;
609 if (!ext2_check_descriptors (sb)) {
610 for (j = 0; j < db_count; j++)
611 brelse (sb->u.ext2_sb.s_group_desc[j]);
612 kfree_s (sb->u.ext2_sb.s_group_desc,
613 db_count * sizeof (struct buffer_head *));
614 printk ("EXT2-fs: group descriptors corrupted !\n");
615 goto failed_mount;
617 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
618 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
619 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
620 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
621 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
623 sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
624 sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
625 sb->u.ext2_sb.s_db_per_group = db_count;
626 unlock_super (sb);
628 * set up enough so that it can read an inode
630 sb->s_dev = dev;
631 sb->s_op = &ext2_sops;
632 sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO), NULL);
633 if (!sb->s_root) {
634 sb->s_dev = 0;
635 for (i = 0; i < db_count; i++)
636 if (sb->u.ext2_sb.s_group_desc[i])
637 brelse (sb->u.ext2_sb.s_group_desc[i]);
638 kfree_s (sb->u.ext2_sb.s_group_desc,
639 db_count * sizeof (struct buffer_head *));
640 brelse (bh);
641 printk ("EXT2-fs: get root inode failed\n");
642 MOD_DEC_USE_COUNT;
643 return NULL;
645 ext2_setup_super (sb, es);
646 return sb;
649 static void ext2_commit_super (struct super_block * sb,
650 struct ext2_super_block * es)
652 es->s_wtime = cpu_to_le32(CURRENT_TIME);
653 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
654 sb->s_dirt = 0;
658 * In the second extended file system, it is not necessary to
659 * write the super block since we use a mapping of the
660 * disk super block in a buffer.
662 * However, this function is still used to set the fs valid
663 * flags to 0. We need to set this flag to 0 since the fs
664 * may have been checked while mounted and e2fsck may have
665 * set s_state to EXT2_VALID_FS after some corrections.
668 void ext2_write_super (struct super_block * sb)
670 struct ext2_super_block * es;
672 if (!(sb->s_flags & MS_RDONLY)) {
673 es = sb->u.ext2_sb.s_es;
675 ext2_debug ("setting valid to 0\n");
677 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
678 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT2_VALID_FS);
679 es->s_mtime = cpu_to_le32(CURRENT_TIME);
681 ext2_commit_super (sb, es);
683 sb->s_dirt = 0;
686 int ext2_remount (struct super_block * sb, int * flags, char * data)
688 struct ext2_super_block * es;
689 unsigned short resuid = sb->u.ext2_sb.s_resuid;
690 unsigned short resgid = sb->u.ext2_sb.s_resgid;
691 unsigned long new_mount_opt;
692 unsigned long tmp;
695 * Allow the "check" option to be passed as a remount option.
697 new_mount_opt = EXT2_MOUNT_CHECK_NORMAL;
698 if (!parse_options (data, &tmp, &resuid, &resgid,
699 &new_mount_opt))
700 return -EINVAL;
702 sb->u.ext2_sb.s_mount_opt = new_mount_opt;
703 sb->u.ext2_sb.s_resuid = resuid;
704 sb->u.ext2_sb.s_resgid = resgid;
705 es = sb->u.ext2_sb.s_es;
706 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
707 return 0;
708 if (*flags & MS_RDONLY) {
709 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
710 !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
711 return 0;
713 * OK, we are remounting a valid rw partition rdonly, so set
714 * the rdonly flag and then mark the partition as valid again.
716 es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
717 es->s_mtime = cpu_to_le32(CURRENT_TIME);
718 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
719 sb->s_dirt = 1;
720 ext2_commit_super (sb, es);
722 else {
724 * Mounting a RDONLY partition read-write, so reread and
725 * store the current valid flag. (It may have been changed
726 * by e2fsck since we originally mounted the partition.)
728 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
729 sb->s_flags &= ~MS_RDONLY;
730 ext2_setup_super (sb, es);
732 return 0;
735 static struct file_system_type ext2_fs_type = {
736 "ext2",
737 FS_REQUIRES_DEV /* | FS_IBASKET */, /* ibaskets have unresolved bugs */
738 ext2_read_super,
739 NULL
742 __initfunc(int init_ext2_fs(void))
744 return register_filesystem(&ext2_fs_type);
747 #ifdef MODULE
748 EXPORT_NO_SYMBOLS;
750 int init_module(void)
752 return init_ext2_fs();
755 void cleanup_module(void)
757 unregister_filesystem(&ext2_fs_type);
760 #endif
762 int ext2_statfs (struct super_block * sb, struct statfs * buf, int bufsiz)
764 unsigned long overhead;
765 struct statfs tmp;
766 int ngroups, i;
768 if (test_opt (sb, MINIX_DF))
769 overhead = 0;
770 else {
772 * Compute the overhead (FS structures)
776 * All of the blocks before first_data_block are
777 * overhead
779 overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
782 * Add the overhead attributed to the superblock and
783 * block group descriptors. If this is sparse
784 * superblocks is turned on, then not all groups have
785 * this.
787 if (sb->u.ext2_sb.s_feature_ro_compat &
788 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) {
789 ngroups = 0;
790 for (i=0 ; i < sb->u.ext2_sb.s_groups_count; i++)
791 if (ext2_group_sparse(i))
792 ngroups++;
793 } else
794 ngroups = sb->u.ext2_sb.s_groups_count;
795 overhead += ngroups * (1 + sb->u.ext2_sb.s_db_per_group);
798 * Every block group has an inode bitmap, a block
799 * bitmap, and an inode table.
801 overhead += (sb->u.ext2_sb.s_groups_count *
802 (2 + sb->u.ext2_sb.s_itb_per_group));
805 tmp.f_type = EXT2_SUPER_MAGIC;
806 tmp.f_bsize = sb->s_blocksize;
807 tmp.f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
808 tmp.f_bfree = ext2_count_free_blocks (sb);
809 tmp.f_bavail = tmp.f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
810 if (tmp.f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
811 tmp.f_bavail = 0;
812 tmp.f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
813 tmp.f_ffree = ext2_count_free_inodes (sb);
814 tmp.f_namelen = EXT2_NAME_LEN;
815 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;