File: /usr/src/linux/fs/ext2/super.c
1 /*
2 * linux/fs/ext2/super.c
3 *
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)
8 *
9 * from
10 *
11 * linux/fs/minix/inode.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
17 */
18
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
22 #include <linux/fs.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>
29
30
31
32 static char error_buf[1024];
33
34 void ext2_error (struct super_block * sb, const char * function,
35 const char * fmt, ...)
36 {
37 va_list args;
38
39 if (!(sb->s_flags & MS_RDONLY)) {
40 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
41 sb->u.ext2_sb.s_es->s_state =
42 cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
43 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
44 sb->s_dirt = 1;
45 }
46 va_start (args, fmt);
47 vsprintf (error_buf, fmt, args);
48 va_end (args);
49 if (test_opt (sb, ERRORS_PANIC) ||
50 (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_PANIC &&
51 !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
52 panic ("EXT2-fs panic (device %s): %s: %s\n",
53 bdevname(sb->s_dev), function, error_buf);
54 printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
55 bdevname(sb->s_dev), function, error_buf);
56 if (test_opt (sb, ERRORS_RO) ||
57 (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_RO &&
58 !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
59 printk ("Remounting filesystem read-only\n");
60 sb->s_flags |= MS_RDONLY;
61 }
62 }
63
64 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
65 const char * fmt, ...)
66 {
67 va_list args;
68
69 if (!(sb->s_flags & MS_RDONLY)) {
70 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
71 sb->u.ext2_sb.s_es->s_state =
72 cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
73 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
74 sb->s_dirt = 1;
75 }
76 va_start (args, fmt);
77 vsprintf (error_buf, fmt, args);
78 va_end (args);
79 sb->s_flags |= MS_RDONLY;
80 panic ("EXT2-fs panic (device %s): %s: %s\n",
81 bdevname(sb->s_dev), function, error_buf);
82 }
83
84 void ext2_warning (struct super_block * sb, const char * function,
85 const char * fmt, ...)
86 {
87 va_list args;
88
89 va_start (args, fmt);
90 vsprintf (error_buf, fmt, args);
91 va_end (args);
92 printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
93 bdevname(sb->s_dev), function, error_buf);
94 }
95
96 void ext2_update_dynamic_rev(struct super_block *sb)
97 {
98 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
99
100 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
101 return;
102
103 ext2_warning(sb, __FUNCTION__,
104 "updating to rev %d because of new feature flag, "
105 "running e2fsck is recommended",
106 EXT2_DYNAMIC_REV);
107
108 es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
109 es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
110 es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
111 /* leave es->s_feature_*compat flags alone */
112 /* es->s_uuid will be set by e2fsck if empty */
113
114 /*
115 * The rest of the superblock fields should be zero, and if not it
116 * means they are likely already in use, so leave them alone. We
117 * can leave it up to e2fsck to clean up any inconsistencies there.
118 */
119 }
120
121 void ext2_put_super (struct super_block * sb)
122 {
123 int db_count;
124 int i;
125
126 if (!(sb->s_flags & MS_RDONLY)) {
127 sb->u.ext2_sb.s_es->s_state = le16_to_cpu(sb->u.ext2_sb.s_mount_state);
128 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
129 }
130 db_count = EXT2_SB(sb)->s_gdb_count;
131 for (i = 0; i < db_count; i++)
132 if (sb->u.ext2_sb.s_group_desc[i])
133 brelse (sb->u.ext2_sb.s_group_desc[i]);
134 kfree(sb->u.ext2_sb.s_group_desc);
135 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
136 if (sb->u.ext2_sb.s_inode_bitmap[i])
137 brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
138 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
139 if (sb->u.ext2_sb.s_block_bitmap[i])
140 brelse (sb->u.ext2_sb.s_block_bitmap[i]);
141 brelse (sb->u.ext2_sb.s_sbh);
142
143 return;
144 }
145
146 static struct super_operations ext2_sops = {
147 read_inode: ext2_read_inode,
148 write_inode: ext2_write_inode,
149 put_inode: ext2_put_inode,
150 delete_inode: ext2_delete_inode,
151 put_super: ext2_put_super,
152 write_super: ext2_write_super,
153 statfs: ext2_statfs,
154 remount_fs: ext2_remount,
155 };
156
157 /*
158 * This function has been shamelessly adapted from the msdos fs
159 */
160 static int parse_options (char * options, unsigned long * sb_block,
161 unsigned short *resuid, unsigned short * resgid,
162 unsigned long * mount_options)
163 {
164 char * this_char;
165 char * value;
166
167 if (!options)
168 return 1;
169 for (this_char = strtok (options, ",");
170 this_char != NULL;
171 this_char = strtok (NULL, ",")) {
172 if ((value = strchr (this_char, '=')) != NULL)
173 *value++ = 0;
174 if (!strcmp (this_char, "bsddf"))
175 clear_opt (*mount_options, MINIX_DF);
176 else if (!strcmp (this_char, "nouid32")) {
177 set_opt (*mount_options, NO_UID32);
178 }
179 else if (!strcmp (this_char, "check")) {
180 if (!value || !*value || !strcmp (value, "none"))
181 clear_opt (*mount_options, CHECK);
182 else
183 #ifdef CONFIG_EXT2_CHECK
184 set_opt (*mount_options, CHECK);
185 #else
186 printk("EXT2 Check option not supported\n");
187 #endif
188 }
189 else if (!strcmp (this_char, "debug"))
190 set_opt (*mount_options, DEBUG);
191 else if (!strcmp (this_char, "errors")) {
192 if (!value || !*value) {
193 printk ("EXT2-fs: the errors option requires "
194 "an argument\n");
195 return 0;
196 }
197 if (!strcmp (value, "continue")) {
198 clear_opt (*mount_options, ERRORS_RO);
199 clear_opt (*mount_options, ERRORS_PANIC);
200 set_opt (*mount_options, ERRORS_CONT);
201 }
202 else if (!strcmp (value, "remount-ro")) {
203 clear_opt (*mount_options, ERRORS_CONT);
204 clear_opt (*mount_options, ERRORS_PANIC);
205 set_opt (*mount_options, ERRORS_RO);
206 }
207 else if (!strcmp (value, "panic")) {
208 clear_opt (*mount_options, ERRORS_CONT);
209 clear_opt (*mount_options, ERRORS_RO);
210 set_opt (*mount_options, ERRORS_PANIC);
211 }
212 else {
213 printk ("EXT2-fs: Invalid errors option: %s\n",
214 value);
215 return 0;
216 }
217 }
218 else if (!strcmp (this_char, "grpid") ||
219 !strcmp (this_char, "bsdgroups"))
220 set_opt (*mount_options, GRPID);
221 else if (!strcmp (this_char, "minixdf"))
222 set_opt (*mount_options, MINIX_DF);
223 else if (!strcmp (this_char, "nocheck"))
224 clear_opt (*mount_options, CHECK);
225 else if (!strcmp (this_char, "nogrpid") ||
226 !strcmp (this_char, "sysvgroups"))
227 clear_opt (*mount_options, GRPID);
228 else if (!strcmp (this_char, "resgid")) {
229 if (!value || !*value) {
230 printk ("EXT2-fs: the resgid option requires "
231 "an argument\n");
232 return 0;
233 }
234 *resgid = simple_strtoul (value, &value, 0);
235 if (*value) {
236 printk ("EXT2-fs: Invalid resgid option: %s\n",
237 value);
238 return 0;
239 }
240 }
241 else if (!strcmp (this_char, "resuid")) {
242 if (!value || !*value) {
243 printk ("EXT2-fs: the resuid option requires "
244 "an argument");
245 return 0;
246 }
247 *resuid = simple_strtoul (value, &value, 0);
248 if (*value) {
249 printk ("EXT2-fs: Invalid resuid option: %s\n",
250 value);
251 return 0;
252 }
253 }
254 else if (!strcmp (this_char, "sb")) {
255 if (!value || !*value) {
256 printk ("EXT2-fs: the sb option requires "
257 "an argument");
258 return 0;
259 }
260 *sb_block = simple_strtoul (value, &value, 0);
261 if (*value) {
262 printk ("EXT2-fs: Invalid sb option: %s\n",
263 value);
264 return 0;
265 }
266 }
267 /* Silently ignore the quota options */
268 else if (!strcmp (this_char, "grpquota")
269 || !strcmp (this_char, "noquota")
270 || !strcmp (this_char, "quota")
271 || !strcmp (this_char, "usrquota"))
272 /* Don't do anything ;-) */ ;
273 else {
274 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
275 return 0;
276 }
277 }
278 return 1;
279 }
280
281 static int ext2_setup_super (struct super_block * sb,
282 struct ext2_super_block * es,
283 int read_only)
284 {
285 int res = 0;
286 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
287 printk ("EXT2-fs warning: revision level too high, "
288 "forcing read-only mode\n");
289 res = MS_RDONLY;
290 }
291 if (read_only)
292 return res;
293 if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
294 printk ("EXT2-fs warning: mounting unchecked fs, "
295 "running e2fsck is recommended\n");
296 else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
297 printk ("EXT2-fs warning: mounting fs with errors, "
298 "running e2fsck is recommended\n");
299 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
300 le16_to_cpu(es->s_mnt_count) >=
301 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
302 printk ("EXT2-fs warning: maximal mount count reached, "
303 "running e2fsck is recommended\n");
304 else if (le32_to_cpu(es->s_checkinterval) &&
305 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
306 printk ("EXT2-fs warning: checktime reached, "
307 "running e2fsck is recommended\n");
308 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT2_VALID_FS);
309 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
310 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
311 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
312 es->s_mtime = cpu_to_le32(CURRENT_TIME);
313 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
314 sb->s_dirt = 1;
315 if (test_opt (sb, DEBUG))
316 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
317 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
318 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
319 sb->u.ext2_sb.s_frag_size,
320 sb->u.ext2_sb.s_groups_count,
321 EXT2_BLOCKS_PER_GROUP(sb),
322 EXT2_INODES_PER_GROUP(sb),
323 sb->u.ext2_sb.s_mount_opt);
324 #ifdef CONFIG_EXT2_CHECK
325 if (test_opt (sb, CHECK)) {
326 ext2_check_blocks_bitmap (sb);
327 ext2_check_inodes_bitmap (sb);
328 }
329 #endif
330 return res;
331 }
332
333 static int ext2_check_descriptors (struct super_block * sb)
334 {
335 int i;
336 int desc_block = 0;
337 unsigned long block = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
338 struct ext2_group_desc * gdp = NULL;
339
340 ext2_debug ("Checking group descriptors");
341
342 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
343 {
344 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
345 gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
346 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
347 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
348 {
349 ext2_error (sb, "ext2_check_descriptors",
350 "Block bitmap for group %d"
351 " not in group (block %lu)!",
352 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
353 return 0;
354 }
355 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
356 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
357 {
358 ext2_error (sb, "ext2_check_descriptors",
359 "Inode bitmap for group %d"
360 " not in group (block %lu)!",
361 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
362 return 0;
363 }
364 if (le32_to_cpu(gdp->bg_inode_table) < block ||
365 le32_to_cpu(gdp->bg_inode_table) + sb->u.ext2_sb.s_itb_per_group >=
366 block + EXT2_BLOCKS_PER_GROUP(sb))
367 {
368 ext2_error (sb, "ext2_check_descriptors",
369 "Inode table for group %d"
370 " not in group (block %lu)!",
371 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
372 return 0;
373 }
374 block += EXT2_BLOCKS_PER_GROUP(sb);
375 gdp++;
376 }
377 return 1;
378 }
379
380 #define log2(n) ffz(~(n))
381
382 /*
383 * Maximal file size. There is a direct, and {,double-,triple-}indirect
384 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
385 * We need to be 1 filesystem block less than the 2^32 sector limit.
386 */
387 static loff_t ext2_max_size(int bits)
388 {
389 loff_t res = EXT2_NDIR_BLOCKS;
390 res += 1LL << (bits-2);
391 res += 1LL << (2*(bits-2));
392 res += 1LL << (3*(bits-2));
393 res <<= bits;
394 if (res > (512LL << 32) - (1 << bits))
395 res = (512LL << 32) - (1 << bits);
396 return res;
397 }
398
399 struct super_block * ext2_read_super (struct super_block * sb, void * data,
400 int silent)
401 {
402 struct buffer_head * bh;
403 struct ext2_super_block * es;
404 unsigned long sb_block = 1;
405 unsigned short resuid = EXT2_DEF_RESUID;
406 unsigned short resgid = EXT2_DEF_RESGID;
407 unsigned long logic_sb_block = 1;
408 unsigned long offset = 0;
409 kdev_t dev = sb->s_dev;
410 int blocksize = BLOCK_SIZE;
411 int hblock;
412 int db_count;
413 int i, j;
414
415 /*
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.
421 */
422 blocksize = get_hardsect_size(dev);
423 if(blocksize < BLOCK_SIZE )
424 blocksize = BLOCK_SIZE;
425
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)) {
429 return NULL;
430 }
431
432 set_blocksize (dev, blocksize);
433
434 /*
435 * If the superblock doesn't start on a sector boundary,
436 * calculate the offset. FIXME(eric) this doesn't make sense
437 * that we would have to do this.
438 */
439 if (blocksize != BLOCK_SIZE) {
440 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
441 offset = (sb_block*BLOCK_SIZE) % blocksize;
442 }
443
444 if (!(bh = bread (dev, logic_sb_block, blocksize))) {
445 printk ("EXT2-fs: unable to read superblock\n");
446 return NULL;
447 }
448 /*
449 * Note: s_es must be initialized as soon as possible because
450 * some ext2 macro-instructions depend on its value
451 */
452 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
453 sb->u.ext2_sb.s_es = es;
454 sb->s_magic = le16_to_cpu(es->s_magic);
455 if (sb->s_magic != EXT2_SUPER_MAGIC) {
456 if (!silent)
457 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
458 bdevname(dev));
459 goto failed_mount;
460 }
461 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
462 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
463 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
464 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
465 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
466 "running e2fsck is recommended\n");
467 /*
468 * Check feature flags regardless of the revision level, since we
469 * previously didn't change the revision level when setting the flags,
470 * so there is a chance incompat flags are set on a rev 0 filesystem.
471 */
472 if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
473 printk("EXT2-fs: %s: couldn't mount because of "
474 "unsupported optional features (%x).\n",
475 bdevname(dev), i);
476 goto failed_mount;
477 }
478 if (!(sb->s_flags & MS_RDONLY) &&
479 (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
480 printk("EXT2-fs: %s: couldn't mount RDWR because of "
481 "unsupported optional features (%x).\n",
482 bdevname(dev), i);
483 goto failed_mount;
484 }
485 sb->s_blocksize_bits =
486 le32_to_cpu(EXT2_SB(sb)->s_es->s_log_block_size) + 10;
487 sb->s_blocksize = 1 << sb->s_blocksize_bits;
488
489 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
490
491 if (sb->s_blocksize != BLOCK_SIZE &&
492 (sb->s_blocksize == 1024 || sb->s_blocksize == 2048 ||
493 sb->s_blocksize == 4096)) {
494 /*
495 * Make sure the blocksize for the filesystem is larger
496 * than the hardware sectorsize for the machine.
497 */
498 hblock = get_hardsect_size(dev);
499 if (sb->s_blocksize < hblock) {
500 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
501 goto failed_mount;
502 }
503
504 brelse (bh);
505 set_blocksize (dev, sb->s_blocksize);
506 logic_sb_block = (sb_block*BLOCK_SIZE) / sb->s_blocksize;
507 offset = (sb_block*BLOCK_SIZE) % sb->s_blocksize;
508 bh = bread (dev, logic_sb_block, sb->s_blocksize);
509 if(!bh) {
510 printk("EXT2-fs: Couldn't read superblock on "
511 "2nd try.\n");
512 goto failed_mount;
513 }
514 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
515 sb->u.ext2_sb.s_es = es;
516 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
517 printk ("EXT2-fs: Magic mismatch, very weird !\n");
518 goto failed_mount;
519 }
520 }
521 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
522 sb->u.ext2_sb.s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
523 sb->u.ext2_sb.s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
524 } else {
525 sb->u.ext2_sb.s_inode_size = le16_to_cpu(es->s_inode_size);
526 sb->u.ext2_sb.s_first_ino = le32_to_cpu(es->s_first_ino);
527 if (sb->u.ext2_sb.s_inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
528 printk ("EXT2-fs: unsupported inode size: %d\n",
529 sb->u.ext2_sb.s_inode_size);
530 goto failed_mount;
531 }
532 }
533 sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
534 le32_to_cpu(es->s_log_frag_size);
535 if (sb->u.ext2_sb.s_frag_size)
536 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
537 sb->u.ext2_sb.s_frag_size;
538 else
539 sb->s_magic = 0;
540 sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
541 sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
542 sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
543 sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
544 EXT2_INODE_SIZE(sb);
545 sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
546 sb->u.ext2_sb.s_inodes_per_block;
547 sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
548 sizeof (struct ext2_group_desc);
549 sb->u.ext2_sb.s_sbh = bh;
550 if (resuid != EXT2_DEF_RESUID)
551 sb->u.ext2_sb.s_resuid = resuid;
552 else
553 sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
554 if (resgid != EXT2_DEF_RESGID)
555 sb->u.ext2_sb.s_resgid = resgid;
556 else
557 sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
558 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
559 sb->u.ext2_sb.s_addr_per_block_bits =
560 log2 (EXT2_ADDR_PER_BLOCK(sb));
561 sb->u.ext2_sb.s_desc_per_block_bits =
562 log2 (EXT2_DESC_PER_BLOCK(sb));
563 if (sb->s_magic != EXT2_SUPER_MAGIC) {
564 if (!silent)
565 printk ("VFS: Can't find an ext2 filesystem on dev "
566 "%s.\n",
567 bdevname(dev));
568 goto failed_mount;
569 }
570 if (sb->s_blocksize != bh->b_size) {
571 if (!silent)
572 printk ("VFS: Unsupported blocksize on dev "
573 "%s.\n", bdevname(dev));
574 goto failed_mount;
575 }
576
577 if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
578 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
579 sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
580 goto failed_mount;
581 }
582
583 if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
584 printk ("EXT2-fs: #blocks per group too big: %lu\n",
585 sb->u.ext2_sb.s_blocks_per_group);
586 goto failed_mount;
587 }
588 if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
589 printk ("EXT2-fs: #fragments per group too big: %lu\n",
590 sb->u.ext2_sb.s_frags_per_group);
591 goto failed_mount;
592 }
593 if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
594 printk ("EXT2-fs: #inodes per group too big: %lu\n",
595 sb->u.ext2_sb.s_inodes_per_group);
596 goto failed_mount;
597 }
598
599 sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
600 le32_to_cpu(es->s_first_data_block) +
601 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
602 EXT2_BLOCKS_PER_GROUP(sb);
603 db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
604 EXT2_DESC_PER_BLOCK(sb);
605 sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
606 if (sb->u.ext2_sb.s_group_desc == NULL) {
607 printk ("EXT2-fs: not enough memory\n");
608 goto failed_mount;
609 }
610 for (i = 0; i < db_count; i++) {
611 sb->u.ext2_sb.s_group_desc[i] = bread (dev, logic_sb_block + i + 1,
612 sb->s_blocksize);
613 if (!sb->u.ext2_sb.s_group_desc[i]) {
614 for (j = 0; j < i; j++)
615 brelse (sb->u.ext2_sb.s_group_desc[j]);
616 kfree(sb->u.ext2_sb.s_group_desc);
617 printk ("EXT2-fs: unable to read group descriptors\n");
618 goto failed_mount;
619 }
620 }
621 if (!ext2_check_descriptors (sb)) {
622 printk ("EXT2-fs: group descriptors corrupted!\n");
623 db_count = i;
624 goto failed_mount2;
625 }
626 for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
627 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
628 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
629 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
630 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
631 }
632 sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
633 sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
634 sb->u.ext2_sb.s_gdb_count = db_count;
635 /*
636 * set up enough so that it can read an inode
637 */
638 sb->s_op = &ext2_sops;
639 sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
640 if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
641 !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
642 if (sb->s_root) {
643 dput(sb->s_root);
644 sb->s_root = NULL;
645 printk ("EXT2-fs: corrupt root inode, run e2fsck\n");
646 } else
647 printk ("EXT2-fs: get root inode failed\n");
648 goto failed_mount2;
649 }
650 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
651 return sb;
652 failed_mount2:
653 for (i = 0; i < db_count; i++)
654 brelse(sb->u.ext2_sb.s_group_desc[i]);
655 kfree(sb->u.ext2_sb.s_group_desc);
656 failed_mount:
657 brelse(bh);
658 return NULL;
659 }
660
661 static void ext2_commit_super (struct super_block * sb,
662 struct ext2_super_block * es)
663 {
664 es->s_wtime = cpu_to_le32(CURRENT_TIME);
665 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
666 sb->s_dirt = 0;
667 }
668
669 /*
670 * In the second extended file system, it is not necessary to
671 * write the super block since we use a mapping of the
672 * disk super block in a buffer.
673 *
674 * However, this function is still used to set the fs valid
675 * flags to 0. We need to set this flag to 0 since the fs
676 * may have been checked while mounted and e2fsck may have
677 * set s_state to EXT2_VALID_FS after some corrections.
678 */
679
680 void ext2_write_super (struct super_block * sb)
681 {
682 struct ext2_super_block * es;
683
684 if (!(sb->s_flags & MS_RDONLY)) {
685 es = sb->u.ext2_sb.s_es;
686
687 ext2_debug ("setting valid to 0\n");
688
689 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
690 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT2_VALID_FS);
691 es->s_mtime = cpu_to_le32(CURRENT_TIME);
692 }
693 ext2_commit_super (sb, es);
694 }
695 sb->s_dirt = 0;
696 }
697
698 int ext2_remount (struct super_block * sb, int * flags, char * data)
699 {
700 struct ext2_super_block * es;
701 unsigned short resuid = sb->u.ext2_sb.s_resuid;
702 unsigned short resgid = sb->u.ext2_sb.s_resgid;
703 unsigned long new_mount_opt;
704 unsigned long tmp;
705
706 /*
707 * Allow the "check" option to be passed as a remount option.
708 */
709 new_mount_opt = sb->u.ext2_sb.s_mount_opt;
710 if (!parse_options (data, &tmp, &resuid, &resgid,
711 &new_mount_opt))
712 return -EINVAL;
713
714 sb->u.ext2_sb.s_mount_opt = new_mount_opt;
715 sb->u.ext2_sb.s_resuid = resuid;
716 sb->u.ext2_sb.s_resgid = resgid;
717 es = sb->u.ext2_sb.s_es;
718 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
719 return 0;
720 if (*flags & MS_RDONLY) {
721 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
722 !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
723 return 0;
724 /*
725 * OK, we are remounting a valid rw partition rdonly, so set
726 * the rdonly flag and then mark the partition as valid again.
727 */
728 es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
729 es->s_mtime = cpu_to_le32(CURRENT_TIME);
730 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
731 sb->s_dirt = 1;
732 ext2_commit_super (sb, es);
733 }
734 else {
735 int ret;
736 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
737 ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
738 printk("EXT2-fs: %s: couldn't remount RDWR because of "
739 "unsupported optional features (%x).\n",
740 bdevname(sb->s_dev), ret);
741 return -EROFS;
742 }
743 /*
744 * Mounting a RDONLY partition read-write, so reread and
745 * store the current valid flag. (It may have been changed
746 * by e2fsck since we originally mounted the partition.)
747 */
748 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
749 if (!ext2_setup_super (sb, es, 0))
750 sb->s_flags &= ~MS_RDONLY;
751 }
752 return 0;
753 }
754
755 int ext2_statfs (struct super_block * sb, struct statfs * buf)
756 {
757 unsigned long overhead;
758 int i;
759
760 if (test_opt (sb, MINIX_DF))
761 overhead = 0;
762 else {
763 /*
764 * Compute the overhead (FS structures)
765 */
766
767 /*
768 * All of the blocks before first_data_block are
769 * overhead
770 */
771 overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
772
773 /*
774 * Add the overhead attributed to the superblock and
775 * block group descriptors. If the sparse superblocks
776 * feature is turned on, then not all groups have this.
777 */
778 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++)
779 overhead += ext2_bg_has_super(sb, i) +
780 ext2_bg_num_gdb(sb, i);
781
782 /*
783 * Every block group has an inode bitmap, a block
784 * bitmap, and an inode table.
785 */
786 overhead += (sb->u.ext2_sb.s_groups_count *
787 (2 + sb->u.ext2_sb.s_itb_per_group));
788 }
789
790 buf->f_type = EXT2_SUPER_MAGIC;
791 buf->f_bsize = sb->s_blocksize;
792 buf->f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
793 buf->f_bfree = ext2_count_free_blocks (sb);
794 buf->f_bavail = buf->f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
795 if (buf->f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
796 buf->f_bavail = 0;
797 buf->f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
798 buf->f_ffree = ext2_count_free_inodes (sb);
799 buf->f_namelen = EXT2_NAME_LEN;
800 return 0;
801 }
802
803 static DECLARE_FSTYPE_DEV(ext2_fs_type, "ext2", ext2_read_super);
804
805 static int __init init_ext2_fs(void)
806 {
807 return register_filesystem(&ext2_fs_type);
808 }
809
810 static void __exit exit_ext2_fs(void)
811 {
812 unregister_filesystem(&ext2_fs_type);
813 }
814
815 EXPORT_NO_SYMBOLS;
816
817 module_init(init_ext2_fs)
818 module_exit(exit_ext2_fs)
819