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 <asm/uaccess.h>
31 static char error_buf
[1024];
33 void ext2_error (struct super_block
* sb
, const char * function
,
34 const char * fmt
, ...)
38 if (!(sb
->s_flags
& MS_RDONLY
)) {
39 sb
->u
.ext2_sb
.s_mount_state
|= EXT2_ERROR_FS
;
40 sb
->u
.ext2_sb
.s_es
->s_state
=
41 cpu_to_le16(le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_state
) | EXT2_ERROR_FS
);
42 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
46 vsprintf (error_buf
, fmt
, args
);
48 if (test_opt (sb
, ERRORS_PANIC
) ||
49 (le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_errors
) == EXT2_ERRORS_PANIC
&&
50 !test_opt (sb
, ERRORS_CONT
) && !test_opt (sb
, ERRORS_RO
)))
51 panic ("EXT2-fs panic (device %s): %s: %s\n",
52 bdevname(sb
->s_dev
), function
, error_buf
);
53 printk (KERN_CRIT
"EXT2-fs error (device %s): %s: %s\n",
54 bdevname(sb
->s_dev
), function
, error_buf
);
55 if (test_opt (sb
, ERRORS_RO
) ||
56 (le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_errors
) == EXT2_ERRORS_RO
&&
57 !test_opt (sb
, ERRORS_CONT
) && !test_opt (sb
, ERRORS_PANIC
))) {
58 printk ("Remounting filesystem read-only\n");
59 sb
->s_flags
|= MS_RDONLY
;
63 NORET_TYPE
void ext2_panic (struct super_block
* sb
, const char * function
,
64 const char * fmt
, ...)
68 if (!(sb
->s_flags
& MS_RDONLY
)) {
69 sb
->u
.ext2_sb
.s_mount_state
|= EXT2_ERROR_FS
;
70 sb
->u
.ext2_sb
.s_es
->s_state
=
71 cpu_to_le16(le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_state
) | EXT2_ERROR_FS
);
72 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
76 vsprintf (error_buf
, fmt
, args
);
78 /* this is to prevent panic from syncing this filesystem */
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 sb
->u
.ext2_sb
.s_es
->s_state
= le16_to_cpu(sb
->u
.ext2_sb
.s_mount_state
);
130 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
132 db_count
= EXT2_SB(sb
)->s_gdb_count
;
133 for (i
= 0; i
< db_count
; i
++)
134 if (sb
->u
.ext2_sb
.s_group_desc
[i
])
135 brelse (sb
->u
.ext2_sb
.s_group_desc
[i
]);
136 kfree(sb
->u
.ext2_sb
.s_group_desc
);
137 for (i
= 0; i
< EXT2_MAX_GROUP_LOADED
; i
++)
138 if (sb
->u
.ext2_sb
.s_inode_bitmap
[i
])
139 brelse (sb
->u
.ext2_sb
.s_inode_bitmap
[i
]);
140 for (i
= 0; i
< EXT2_MAX_GROUP_LOADED
; i
++)
141 if (sb
->u
.ext2_sb
.s_block_bitmap
[i
])
142 brelse (sb
->u
.ext2_sb
.s_block_bitmap
[i
]);
143 brelse (sb
->u
.ext2_sb
.s_sbh
);
148 static struct super_operations ext2_sops
= {
149 read_inode
: ext2_read_inode
,
150 write_inode
: ext2_write_inode
,
151 put_inode
: ext2_put_inode
,
152 delete_inode
: ext2_delete_inode
,
153 put_super
: ext2_put_super
,
154 write_super
: ext2_write_super
,
156 remount_fs
: ext2_remount
,
160 * This function has been shamelessly adapted from the msdos fs
162 static int parse_options (char * options
, unsigned long * sb_block
,
163 unsigned short *resuid
, unsigned short * resgid
,
164 unsigned long * mount_options
)
171 for (this_char
= strtok (options
, ",");
173 this_char
= strtok (NULL
, ",")) {
174 if ((value
= strchr (this_char
, '=')) != NULL
)
176 if (!strcmp (this_char
, "bsddf"))
177 clear_opt (*mount_options
, MINIX_DF
);
178 else if (!strcmp (this_char
, "nouid32")) {
179 set_opt (*mount_options
, NO_UID32
);
181 else if (!strcmp (this_char
, "check")) {
182 if (!value
|| !*value
|| !strcmp (value
, "none"))
183 clear_opt (*mount_options
, CHECK
);
185 #ifdef CONFIG_EXT2_CHECK
186 set_opt (*mount_options
, CHECK
);
188 printk("EXT2 Check option not supported\n");
191 else if (!strcmp (this_char
, "debug"))
192 set_opt (*mount_options
, DEBUG
);
193 else if (!strcmp (this_char
, "errors")) {
194 if (!value
|| !*value
) {
195 printk ("EXT2-fs: the errors option requires "
199 if (!strcmp (value
, "continue")) {
200 clear_opt (*mount_options
, ERRORS_RO
);
201 clear_opt (*mount_options
, ERRORS_PANIC
);
202 set_opt (*mount_options
, ERRORS_CONT
);
204 else if (!strcmp (value
, "remount-ro")) {
205 clear_opt (*mount_options
, ERRORS_CONT
);
206 clear_opt (*mount_options
, ERRORS_PANIC
);
207 set_opt (*mount_options
, ERRORS_RO
);
209 else if (!strcmp (value
, "panic")) {
210 clear_opt (*mount_options
, ERRORS_CONT
);
211 clear_opt (*mount_options
, ERRORS_RO
);
212 set_opt (*mount_options
, ERRORS_PANIC
);
215 printk ("EXT2-fs: Invalid errors option: %s\n",
220 else if (!strcmp (this_char
, "grpid") ||
221 !strcmp (this_char
, "bsdgroups"))
222 set_opt (*mount_options
, GRPID
);
223 else if (!strcmp (this_char
, "minixdf"))
224 set_opt (*mount_options
, MINIX_DF
);
225 else if (!strcmp (this_char
, "nocheck"))
226 clear_opt (*mount_options
, CHECK
);
227 else if (!strcmp (this_char
, "nogrpid") ||
228 !strcmp (this_char
, "sysvgroups"))
229 clear_opt (*mount_options
, GRPID
);
230 else if (!strcmp (this_char
, "resgid")) {
231 if (!value
|| !*value
) {
232 printk ("EXT2-fs: the resgid option requires "
236 *resgid
= simple_strtoul (value
, &value
, 0);
238 printk ("EXT2-fs: Invalid resgid option: %s\n",
243 else if (!strcmp (this_char
, "resuid")) {
244 if (!value
|| !*value
) {
245 printk ("EXT2-fs: the resuid option requires "
249 *resuid
= simple_strtoul (value
, &value
, 0);
251 printk ("EXT2-fs: Invalid resuid option: %s\n",
256 else if (!strcmp (this_char
, "sb")) {
257 if (!value
|| !*value
) {
258 printk ("EXT2-fs: the sb option requires "
262 *sb_block
= simple_strtoul (value
, &value
, 0);
264 printk ("EXT2-fs: Invalid sb option: %s\n",
269 /* Silently ignore the quota options */
270 else if (!strcmp (this_char
, "grpquota")
271 || !strcmp (this_char
, "noquota")
272 || !strcmp (this_char
, "quota")
273 || !strcmp (this_char
, "usrquota"))
274 /* Don't do anything ;-) */ ;
276 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char
);
283 static int ext2_setup_super (struct super_block
* sb
,
284 struct ext2_super_block
* es
,
288 if (le32_to_cpu(es
->s_rev_level
) > EXT2_MAX_SUPP_REV
) {
289 printk ("EXT2-fs warning: revision level too high, "
290 "forcing read-only mode\n");
295 if (!(sb
->u
.ext2_sb
.s_mount_state
& EXT2_VALID_FS
))
296 printk ("EXT2-fs warning: mounting unchecked fs, "
297 "running e2fsck is recommended\n");
298 else if ((sb
->u
.ext2_sb
.s_mount_state
& EXT2_ERROR_FS
))
299 printk ("EXT2-fs warning: mounting fs with errors, "
300 "running e2fsck is recommended\n");
301 else if ((__s16
) le16_to_cpu(es
->s_max_mnt_count
) >= 0 &&
302 le16_to_cpu(es
->s_mnt_count
) >=
303 (unsigned short) (__s16
) le16_to_cpu(es
->s_max_mnt_count
))
304 printk ("EXT2-fs warning: maximal mount count reached, "
305 "running e2fsck is recommended\n");
306 else if (le32_to_cpu(es
->s_checkinterval
) &&
307 (le32_to_cpu(es
->s_lastcheck
) + le32_to_cpu(es
->s_checkinterval
) <= CURRENT_TIME
))
308 printk ("EXT2-fs warning: checktime reached, "
309 "running e2fsck is recommended\n");
310 es
->s_state
= cpu_to_le16(le16_to_cpu(es
->s_state
) & ~EXT2_VALID_FS
);
311 if (!(__s16
) le16_to_cpu(es
->s_max_mnt_count
))
312 es
->s_max_mnt_count
= (__s16
) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT
);
313 es
->s_mnt_count
=cpu_to_le16(le16_to_cpu(es
->s_mnt_count
) + 1);
314 es
->s_mtime
= cpu_to_le32(CURRENT_TIME
);
315 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
317 if (test_opt (sb
, DEBUG
))
318 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
319 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
320 EXT2FS_VERSION
, EXT2FS_DATE
, sb
->s_blocksize
,
321 sb
->u
.ext2_sb
.s_frag_size
,
322 sb
->u
.ext2_sb
.s_groups_count
,
323 EXT2_BLOCKS_PER_GROUP(sb
),
324 EXT2_INODES_PER_GROUP(sb
),
325 sb
->u
.ext2_sb
.s_mount_opt
);
326 #ifdef CONFIG_EXT2_CHECK
327 if (test_opt (sb
, CHECK
)) {
328 ext2_check_blocks_bitmap (sb
);
329 ext2_check_inodes_bitmap (sb
);
335 static int ext2_check_descriptors (struct super_block
* sb
)
339 unsigned long block
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_first_data_block
);
340 struct ext2_group_desc
* gdp
= NULL
;
342 ext2_debug ("Checking group descriptors");
344 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++)
346 if ((i
% EXT2_DESC_PER_BLOCK(sb
)) == 0)
347 gdp
= (struct ext2_group_desc
*) sb
->u
.ext2_sb
.s_group_desc
[desc_block
++]->b_data
;
348 if (le32_to_cpu(gdp
->bg_block_bitmap
) < block
||
349 le32_to_cpu(gdp
->bg_block_bitmap
) >= block
+ EXT2_BLOCKS_PER_GROUP(sb
))
351 ext2_error (sb
, "ext2_check_descriptors",
352 "Block bitmap for group %d"
353 " not in group (block %lu)!",
354 i
, (unsigned long) le32_to_cpu(gdp
->bg_block_bitmap
));
357 if (le32_to_cpu(gdp
->bg_inode_bitmap
) < block
||
358 le32_to_cpu(gdp
->bg_inode_bitmap
) >= block
+ EXT2_BLOCKS_PER_GROUP(sb
))
360 ext2_error (sb
, "ext2_check_descriptors",
361 "Inode bitmap for group %d"
362 " not in group (block %lu)!",
363 i
, (unsigned long) le32_to_cpu(gdp
->bg_inode_bitmap
));
366 if (le32_to_cpu(gdp
->bg_inode_table
) < block
||
367 le32_to_cpu(gdp
->bg_inode_table
) + sb
->u
.ext2_sb
.s_itb_per_group
>=
368 block
+ EXT2_BLOCKS_PER_GROUP(sb
))
370 ext2_error (sb
, "ext2_check_descriptors",
371 "Inode table for group %d"
372 " not in group (block %lu)!",
373 i
, (unsigned long) le32_to_cpu(gdp
->bg_inode_table
));
376 block
+= EXT2_BLOCKS_PER_GROUP(sb
);
382 #define log2(n) ffz(~(n))
384 struct super_block
* ext2_read_super (struct super_block
* sb
, void * data
,
387 struct buffer_head
* bh
;
388 struct ext2_super_block
* es
;
389 unsigned long sb_block
= 1;
390 unsigned short resuid
= EXT2_DEF_RESUID
;
391 unsigned short resgid
= EXT2_DEF_RESGID
;
392 unsigned long logic_sb_block
= 1;
393 unsigned long offset
= 0;
394 kdev_t dev
= sb
->s_dev
;
395 int blocksize
= BLOCK_SIZE
;
401 * See what the current blocksize for the device is, and
402 * use that as the blocksize. Otherwise (or if the blocksize
403 * is smaller than the default) use the default.
404 * This is important for devices that have a hardware
405 * sectorsize that is larger than the default.
407 blocksize
= get_hardblocksize(dev
);
408 if( blocksize
== 0 || blocksize
< BLOCK_SIZE
)
410 blocksize
= BLOCK_SIZE
;
413 sb
->u
.ext2_sb
.s_mount_opt
= 0;
414 if (!parse_options ((char *) data
, &sb_block
, &resuid
, &resgid
,
415 &sb
->u
.ext2_sb
.s_mount_opt
)) {
419 set_blocksize (dev
, blocksize
);
422 * If the superblock doesn't start on a sector boundary,
423 * calculate the offset. FIXME(eric) this doesn't make sense
424 * that we would have to do this.
426 if (blocksize
!= BLOCK_SIZE
) {
427 logic_sb_block
= (sb_block
*BLOCK_SIZE
) / blocksize
;
428 offset
= (sb_block
*BLOCK_SIZE
) % blocksize
;
431 if (!(bh
= bread (dev
, logic_sb_block
, blocksize
))) {
432 printk ("EXT2-fs: unable to read superblock\n");
436 * Note: s_es must be initialized s_es as soon as possible because
437 * some ext2 macro-instructions depend on its value
439 es
= (struct ext2_super_block
*) (((char *)bh
->b_data
) + offset
);
440 sb
->u
.ext2_sb
.s_es
= es
;
441 sb
->s_magic
= le16_to_cpu(es
->s_magic
);
442 if (sb
->s_magic
!= EXT2_SUPER_MAGIC
) {
444 printk ("VFS: Can't find an ext2 filesystem on dev "
445 "%s.\n", bdevname(dev
));
451 if (le32_to_cpu(es
->s_rev_level
) == EXT2_GOOD_OLD_REV
&&
452 (EXT2_HAS_COMPAT_FEATURE(sb
, ~0U) ||
453 EXT2_HAS_RO_COMPAT_FEATURE(sb
, ~0U) ||
454 EXT2_HAS_INCOMPAT_FEATURE(sb
, ~0U)))
455 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
456 "running e2fsck is recommended\n");
458 * Check feature flags regardless of the revision level, since we
459 * previously didn't change the revision level when setting the flags,
460 * so there is a chance incompat flags are set on a rev 0 filesystem.
462 if ((i
= EXT2_HAS_INCOMPAT_FEATURE(sb
, ~EXT2_FEATURE_INCOMPAT_SUPP
))) {
463 printk("EXT2-fs: %s: couldn't mount because of "
464 "unsupported optional features (%x).\n",
468 if (!(sb
->s_flags
& MS_RDONLY
) &&
469 (i
= EXT2_HAS_RO_COMPAT_FEATURE(sb
, ~EXT2_FEATURE_RO_COMPAT_SUPP
))){
470 printk("EXT2-fs: %s: couldn't mount RDWR because of "
471 "unsupported optional features (%x).\n",
475 sb
->s_blocksize_bits
=
476 le32_to_cpu(EXT2_SB(sb
)->s_es
->s_log_block_size
) + 10;
477 sb
->s_blocksize
= 1 << sb
->s_blocksize_bits
;
478 if (sb
->s_blocksize
!= BLOCK_SIZE
&&
479 (sb
->s_blocksize
== 1024 || sb
->s_blocksize
== 2048 ||
480 sb
->s_blocksize
== 4096)) {
482 * Make sure the blocksize for the filesystem is larger
483 * than the hardware sectorsize for the machine.
485 hblock
= get_hardblocksize(dev
);
487 && (sb
->s_blocksize
< hblock
) )
489 printk("EXT2-fs: blocksize too small for device.\n");
494 set_blocksize (dev
, sb
->s_blocksize
);
495 logic_sb_block
= (sb_block
*BLOCK_SIZE
) / sb
->s_blocksize
;
496 offset
= (sb_block
*BLOCK_SIZE
) % sb
->s_blocksize
;
497 bh
= bread (dev
, logic_sb_block
, sb
->s_blocksize
);
499 printk("EXT2-fs: Couldn't read superblock on "
503 es
= (struct ext2_super_block
*) (((char *)bh
->b_data
) + offset
);
504 sb
->u
.ext2_sb
.s_es
= es
;
505 if (es
->s_magic
!= le16_to_cpu(EXT2_SUPER_MAGIC
)) {
506 printk ("EXT2-fs: Magic mismatch, very weird !\n");
510 if (le32_to_cpu(es
->s_rev_level
) == EXT2_GOOD_OLD_REV
) {
511 sb
->u
.ext2_sb
.s_inode_size
= EXT2_GOOD_OLD_INODE_SIZE
;
512 sb
->u
.ext2_sb
.s_first_ino
= EXT2_GOOD_OLD_FIRST_INO
;
514 sb
->u
.ext2_sb
.s_inode_size
= le16_to_cpu(es
->s_inode_size
);
515 sb
->u
.ext2_sb
.s_first_ino
= le32_to_cpu(es
->s_first_ino
);
516 if (sb
->u
.ext2_sb
.s_inode_size
!= EXT2_GOOD_OLD_INODE_SIZE
) {
517 printk ("EXT2-fs: unsupported inode size: %d\n",
518 sb
->u
.ext2_sb
.s_inode_size
);
522 sb
->u
.ext2_sb
.s_frag_size
= EXT2_MIN_FRAG_SIZE
<<
523 le32_to_cpu(es
->s_log_frag_size
);
524 if (sb
->u
.ext2_sb
.s_frag_size
)
525 sb
->u
.ext2_sb
.s_frags_per_block
= sb
->s_blocksize
/
526 sb
->u
.ext2_sb
.s_frag_size
;
529 sb
->u
.ext2_sb
.s_blocks_per_group
= le32_to_cpu(es
->s_blocks_per_group
);
530 sb
->u
.ext2_sb
.s_frags_per_group
= le32_to_cpu(es
->s_frags_per_group
);
531 sb
->u
.ext2_sb
.s_inodes_per_group
= le32_to_cpu(es
->s_inodes_per_group
);
532 sb
->u
.ext2_sb
.s_inodes_per_block
= sb
->s_blocksize
/
534 sb
->u
.ext2_sb
.s_itb_per_group
= sb
->u
.ext2_sb
.s_inodes_per_group
/
535 sb
->u
.ext2_sb
.s_inodes_per_block
;
536 sb
->u
.ext2_sb
.s_desc_per_block
= sb
->s_blocksize
/
537 sizeof (struct ext2_group_desc
);
538 sb
->u
.ext2_sb
.s_sbh
= bh
;
539 if (resuid
!= EXT2_DEF_RESUID
)
540 sb
->u
.ext2_sb
.s_resuid
= resuid
;
542 sb
->u
.ext2_sb
.s_resuid
= le16_to_cpu(es
->s_def_resuid
);
543 if (resgid
!= EXT2_DEF_RESGID
)
544 sb
->u
.ext2_sb
.s_resgid
= resgid
;
546 sb
->u
.ext2_sb
.s_resgid
= le16_to_cpu(es
->s_def_resgid
);
547 sb
->u
.ext2_sb
.s_mount_state
= le16_to_cpu(es
->s_state
);
548 sb
->u
.ext2_sb
.s_addr_per_block_bits
=
549 log2 (EXT2_ADDR_PER_BLOCK(sb
));
550 sb
->u
.ext2_sb
.s_desc_per_block_bits
=
551 log2 (EXT2_DESC_PER_BLOCK(sb
));
552 if (sb
->s_magic
!= EXT2_SUPER_MAGIC
) {
554 printk ("VFS: Can't find an ext2 filesystem on dev "
559 if (sb
->s_blocksize
!= bh
->b_size
) {
561 printk ("VFS: Unsupported blocksize on dev "
562 "%s.\n", bdevname(dev
));
566 if (sb
->s_blocksize
!= sb
->u
.ext2_sb
.s_frag_size
) {
567 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
568 sb
->u
.ext2_sb
.s_frag_size
, sb
->s_blocksize
);
572 if (sb
->u
.ext2_sb
.s_blocks_per_group
> sb
->s_blocksize
* 8) {
573 printk ("EXT2-fs: #blocks per group too big: %lu\n",
574 sb
->u
.ext2_sb
.s_blocks_per_group
);
577 if (sb
->u
.ext2_sb
.s_frags_per_group
> sb
->s_blocksize
* 8) {
578 printk ("EXT2-fs: #fragments per group too big: %lu\n",
579 sb
->u
.ext2_sb
.s_frags_per_group
);
582 if (sb
->u
.ext2_sb
.s_inodes_per_group
> sb
->s_blocksize
* 8) {
583 printk ("EXT2-fs: #inodes per group too big: %lu\n",
584 sb
->u
.ext2_sb
.s_inodes_per_group
);
588 sb
->u
.ext2_sb
.s_groups_count
= (le32_to_cpu(es
->s_blocks_count
) -
589 le32_to_cpu(es
->s_first_data_block
) +
590 EXT2_BLOCKS_PER_GROUP(sb
) - 1) /
591 EXT2_BLOCKS_PER_GROUP(sb
);
592 db_count
= (sb
->u
.ext2_sb
.s_groups_count
+ EXT2_DESC_PER_BLOCK(sb
) - 1) /
593 EXT2_DESC_PER_BLOCK(sb
);
594 sb
->u
.ext2_sb
.s_group_desc
= kmalloc (db_count
* sizeof (struct buffer_head
*), GFP_KERNEL
);
595 if (sb
->u
.ext2_sb
.s_group_desc
== NULL
) {
596 printk ("EXT2-fs: not enough memory\n");
599 for (i
= 0; i
< db_count
; i
++) {
600 sb
->u
.ext2_sb
.s_group_desc
[i
] = bread (dev
, logic_sb_block
+ i
+ 1,
602 if (!sb
->u
.ext2_sb
.s_group_desc
[i
]) {
603 for (j
= 0; j
< i
; j
++)
604 brelse (sb
->u
.ext2_sb
.s_group_desc
[j
]);
605 kfree(sb
->u
.ext2_sb
.s_group_desc
);
606 printk ("EXT2-fs: unable to read group descriptors\n");
610 if (!ext2_check_descriptors (sb
)) {
611 for (j
= 0; j
< db_count
; j
++)
612 brelse (sb
->u
.ext2_sb
.s_group_desc
[j
]);
613 kfree(sb
->u
.ext2_sb
.s_group_desc
);
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_gdb_count
= db_count
;
627 * set up enough so that it can read an inode
629 sb
->s_op
= &ext2_sops
;
630 sb
->s_root
= d_alloc_root(iget(sb
, EXT2_ROOT_INO
));
632 for (i
= 0; i
< db_count
; i
++)
633 if (sb
->u
.ext2_sb
.s_group_desc
[i
])
634 brelse (sb
->u
.ext2_sb
.s_group_desc
[i
]);
635 kfree(sb
->u
.ext2_sb
.s_group_desc
);
637 printk ("EXT2-fs: get root inode failed\n");
640 ext2_setup_super (sb
, es
, sb
->s_flags
& MS_RDONLY
);
644 static void ext2_commit_super (struct super_block
* sb
,
645 struct ext2_super_block
* es
)
647 es
->s_wtime
= cpu_to_le32(CURRENT_TIME
);
648 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
653 * In the second extended file system, it is not necessary to
654 * write the super block since we use a mapping of the
655 * disk super block in a buffer.
657 * However, this function is still used to set the fs valid
658 * flags to 0. We need to set this flag to 0 since the fs
659 * may have been checked while mounted and e2fsck may have
660 * set s_state to EXT2_VALID_FS after some corrections.
663 void ext2_write_super (struct super_block
* sb
)
665 struct ext2_super_block
* es
;
667 if (!(sb
->s_flags
& MS_RDONLY
)) {
668 es
= sb
->u
.ext2_sb
.s_es
;
670 ext2_debug ("setting valid to 0\n");
672 if (le16_to_cpu(es
->s_state
) & EXT2_VALID_FS
) {
673 es
->s_state
= cpu_to_le16(le16_to_cpu(es
->s_state
) & ~EXT2_VALID_FS
);
674 es
->s_mtime
= cpu_to_le32(CURRENT_TIME
);
676 ext2_commit_super (sb
, es
);
681 int ext2_remount (struct super_block
* sb
, int * flags
, char * data
)
683 struct ext2_super_block
* es
;
684 unsigned short resuid
= sb
->u
.ext2_sb
.s_resuid
;
685 unsigned short resgid
= sb
->u
.ext2_sb
.s_resgid
;
686 unsigned long new_mount_opt
;
690 * Allow the "check" option to be passed as a remount option.
692 new_mount_opt
= sb
->u
.ext2_sb
.s_mount_opt
;
693 if (!parse_options (data
, &tmp
, &resuid
, &resgid
,
697 sb
->u
.ext2_sb
.s_mount_opt
= new_mount_opt
;
698 sb
->u
.ext2_sb
.s_resuid
= resuid
;
699 sb
->u
.ext2_sb
.s_resgid
= resgid
;
700 es
= sb
->u
.ext2_sb
.s_es
;
701 if ((*flags
& MS_RDONLY
) == (sb
->s_flags
& MS_RDONLY
))
703 if (*flags
& MS_RDONLY
) {
704 if (le16_to_cpu(es
->s_state
) & EXT2_VALID_FS
||
705 !(sb
->u
.ext2_sb
.s_mount_state
& EXT2_VALID_FS
))
708 * OK, we are remounting a valid rw partition rdonly, so set
709 * the rdonly flag and then mark the partition as valid again.
711 es
->s_state
= cpu_to_le16(sb
->u
.ext2_sb
.s_mount_state
);
712 es
->s_mtime
= cpu_to_le32(CURRENT_TIME
);
713 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
);
715 ext2_commit_super (sb
, es
);
719 if ((ret
= EXT2_HAS_RO_COMPAT_FEATURE(sb
,
720 ~EXT2_FEATURE_RO_COMPAT_SUPP
))) {
721 printk("EXT2-fs: %s: couldn't remount RDWR because of "
722 "unsupported optional features (%x).\n",
723 bdevname(sb
->s_dev
), ret
);
727 * Mounting a RDONLY partition read-write, so reread and
728 * store the current valid flag. (It may have been changed
729 * by e2fsck since we originally mounted the partition.)
731 sb
->u
.ext2_sb
.s_mount_state
= le16_to_cpu(es
->s_state
);
732 if (!ext2_setup_super (sb
, es
, 0))
733 sb
->s_flags
&= ~MS_RDONLY
;
738 int ext2_statfs (struct super_block
* sb
, struct statfs
* buf
)
740 unsigned long overhead
;
743 if (test_opt (sb
, MINIX_DF
))
747 * Compute the overhead (FS structures)
751 * All of the blocks before first_data_block are
754 overhead
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_first_data_block
);
757 * Add the overhead attributed to the superblock and
758 * block group descriptors. If the sparse superblocks
759 * feature is turned on, then not all groups have this.
761 for (i
= 0; i
< EXT2_SB(sb
)->s_groups_count
; i
++)
762 overhead
+= ext2_bg_has_super(sb
, i
) +
763 ext2_bg_num_gdb(sb
, i
);
766 * Every block group has an inode bitmap, a block
767 * bitmap, and an inode table.
769 overhead
+= (sb
->u
.ext2_sb
.s_groups_count
*
770 (2 + sb
->u
.ext2_sb
.s_itb_per_group
));
773 buf
->f_type
= EXT2_SUPER_MAGIC
;
774 buf
->f_bsize
= sb
->s_blocksize
;
775 buf
->f_blocks
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_blocks_count
) - overhead
;
776 buf
->f_bfree
= ext2_count_free_blocks (sb
);
777 buf
->f_bavail
= buf
->f_bfree
- le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_r_blocks_count
);
778 if (buf
->f_bfree
< le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_r_blocks_count
))
780 buf
->f_files
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_inodes_count
);
781 buf
->f_ffree
= ext2_count_free_inodes (sb
);
782 buf
->f_namelen
= EXT2_NAME_LEN
;
786 static DECLARE_FSTYPE_DEV(ext2_fs_type
, "ext2", ext2_read_super
);
788 static int __init
init_ext2_fs(void)
790 return register_filesystem(&ext2_fs_type
);
793 static void __exit
exit_ext2_fs(void)
795 unregister_filesystem(&ext2_fs_type
);
800 module_init(init_ext2_fs
)
801 module_exit(exit_ext2_fs
)