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/module.h>
23 #include <asm/bitops.h>
24 #include <asm/uaccess.h>
25 #include <asm/system.h>
27 #include <linux/errno.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
, ...)
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);
53 vsprintf (error_buf
, fmt
, 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
, ...)
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);
83 vsprintf (error_buf
, fmt
, args
);
85 /* this is to prevent panic from syncing this filesystem */
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
, ...)
99 vsprintf (error_buf
, fmt
, 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
)
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
);
132 static struct super_operations ext2_sops
= {
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
)
156 for (this_char
= strtok (options
, ",");
158 this_char
= strtok (NULL
, ",")) {
159 if ((value
= strchr (this_char
, '=')) != NULL
)
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
);
177 printk ("EXT2-fs: Invalid check option: %s\n",
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 "
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
);
206 printk ("EXT2-fs: Invalid errors option: %s\n",
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 "
229 *resgid
= simple_strtoul (value
, &value
, 0);
231 printk ("EXT2-fs: Invalid resgid option: %s\n",
236 else if (!strcmp (this_char
, "resuid")) {
237 if (!value
|| !*value
) {
238 printk ("EXT2-fs: the resuid option requires "
242 *resuid
= simple_strtoul (value
, &value
, 0);
244 printk ("EXT2-fs: Invalid resuid option: %s\n",
249 else if (!strcmp (this_char
, "sb")) {
250 if (!value
|| !*value
) {
251 printk ("EXT2-fs: the sb option requires "
255 *sb_block
= simple_strtoul (value
, &value
, 0);
257 printk ("EXT2-fs: Invalid sb option: %s\n",
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 ;-) */ ;
269 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char
);
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);
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;
330 static int ext2_check_descriptors (struct super_block
* sb
)
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
));
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
));
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
));
371 block
+= EXT2_BLOCKS_PER_GROUP(sb
);
377 #define log2(n) ffz(~(n))
379 struct super_block
* ext2_read_super (struct super_block
* sb
, void * data
,
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
;
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
)) {
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
))) {
433 printk ("EXT2-fs: unable to read superblock\n");
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
) {
446 printk ("VFS: Can't find an ext2 filesystem on dev "
447 "%s.\n", bdevname(dev
));
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",
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",
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
);
482 && (sb
->s_blocksize
< hblock
) )
484 printk("EXT2-fs: blocksize too small for device.\n");
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
);
494 printk("EXT2-fs: Couldn't read superblock on "
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");
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
;
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
);
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
;
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
/
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
;
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
;
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
) {
552 printk ("VFS: Can't find an ext2 filesystem on dev "
557 if (sb
->s_blocksize
!= bh
->b_size
) {
559 printk ("VFS: Unsupported blocksize on dev "
560 "%s.\n", bdevname(dev
));
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
);
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
);
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
);
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
);
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");
597 for (i
= 0; i
< db_count
; i
++) {
598 sb
->u
.ext2_sb
.s_group_desc
[i
] = bread (dev
, logic_sb_block
+ i
+ 1,
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");
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");
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
;
628 * set up enough so that it can read an inode
631 sb
->s_op
= &ext2_sops
;
632 sb
->s_root
= d_alloc_root(iget(sb
, EXT2_ROOT_INO
), NULL
);
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
*));
641 printk ("EXT2-fs: get root inode failed\n");
645 ext2_setup_super (sb
, es
);
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);
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
);
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
;
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
,
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
))
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
))
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);
720 ext2_commit_super (sb
, es
);
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
);
735 static struct file_system_type ext2_fs_type
= {
737 FS_REQUIRES_DEV
/* | FS_IBASKET */, /* ibaskets have unresolved bugs */
742 __initfunc(int init_ext2_fs(void))
744 return register_filesystem(&ext2_fs_type
);
750 int init_module(void)
752 return init_ext2_fs();
755 void cleanup_module(void)
757 unregister_filesystem(&ext2_fs_type
);
762 int ext2_statfs (struct super_block
* sb
, struct statfs
* buf
, int bufsiz
)
764 unsigned long overhead
;
768 if (test_opt (sb
, MINIX_DF
))
772 * Compute the overhead (FS structures)
776 * All of the blocks before first_data_block are
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
787 if (sb
->u
.ext2_sb
.s_feature_ro_compat
&
788 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) {
790 for (i
=0 ; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++)
791 if (ext2_group_sparse(i
))
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
))
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;