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/slab.h>
24 #include <linux/init.h>
25 #include <linux/locks.h>
26 #include <asm/uaccess.h>
30 static char error_buf
[1024];
32 void ext2_error (struct super_block
* sb
, const char * function
,
33 const char * fmt
, ...)
37 if (!(sb
->s_flags
& MS_RDONLY
)) {
38 sb
->u
.ext2_sb
.s_mount_state
|= EXT2_ERROR_FS
;
39 sb
->u
.ext2_sb
.s_es
->s_state
=
40 cpu_to_le16(le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_state
) | EXT2_ERROR_FS
);
41 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
45 vsprintf (error_buf
, fmt
, args
);
47 if (test_opt (sb
, ERRORS_PANIC
) ||
48 (le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_errors
) == EXT2_ERRORS_PANIC
&&
49 !test_opt (sb
, ERRORS_CONT
) && !test_opt (sb
, ERRORS_RO
)))
50 panic ("EXT2-fs panic (device %s): %s: %s\n",
51 bdevname(sb
->s_dev
), function
, error_buf
);
52 printk (KERN_CRIT
"EXT2-fs error (device %s): %s: %s\n",
53 bdevname(sb
->s_dev
), function
, error_buf
);
54 if (test_opt (sb
, ERRORS_RO
) ||
55 (le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_errors
) == EXT2_ERRORS_RO
&&
56 !test_opt (sb
, ERRORS_CONT
) && !test_opt (sb
, ERRORS_PANIC
))) {
57 printk ("Remounting filesystem read-only\n");
58 sb
->s_flags
|= MS_RDONLY
;
62 NORET_TYPE
void ext2_panic (struct super_block
* sb
, const char * function
,
63 const char * fmt
, ...)
67 if (!(sb
->s_flags
& MS_RDONLY
)) {
68 sb
->u
.ext2_sb
.s_mount_state
|= EXT2_ERROR_FS
;
69 sb
->u
.ext2_sb
.s_es
->s_state
=
70 cpu_to_le16(le16_to_cpu(sb
->u
.ext2_sb
.s_es
->s_state
) | EXT2_ERROR_FS
);
71 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
75 vsprintf (error_buf
, fmt
, args
);
77 /* this is to prevent panic from syncing this filesystem */
80 sb
->s_flags
|= MS_RDONLY
;
81 panic ("EXT2-fs panic (device %s): %s: %s\n",
82 bdevname(sb
->s_dev
), function
, error_buf
);
85 void ext2_warning (struct super_block
* sb
, const char * function
,
86 const char * fmt
, ...)
91 vsprintf (error_buf
, fmt
, args
);
93 printk (KERN_WARNING
"EXT2-fs warning (device %s): %s: %s\n",
94 bdevname(sb
->s_dev
), function
, error_buf
);
97 void ext2_put_super (struct super_block
* sb
)
102 if (!(sb
->s_flags
& MS_RDONLY
)) {
103 sb
->u
.ext2_sb
.s_es
->s_state
= le16_to_cpu(sb
->u
.ext2_sb
.s_mount_state
);
104 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
106 db_count
= sb
->u
.ext2_sb
.s_db_per_group
;
107 for (i
= 0; i
< db_count
; i
++)
108 if (sb
->u
.ext2_sb
.s_group_desc
[i
])
109 brelse (sb
->u
.ext2_sb
.s_group_desc
[i
]);
110 kfree_s (sb
->u
.ext2_sb
.s_group_desc
,
111 db_count
* sizeof (struct buffer_head
*));
112 for (i
= 0; i
< EXT2_MAX_GROUP_LOADED
; i
++)
113 if (sb
->u
.ext2_sb
.s_inode_bitmap
[i
])
114 brelse (sb
->u
.ext2_sb
.s_inode_bitmap
[i
]);
115 for (i
= 0; i
< EXT2_MAX_GROUP_LOADED
; i
++)
116 if (sb
->u
.ext2_sb
.s_block_bitmap
[i
])
117 brelse (sb
->u
.ext2_sb
.s_block_bitmap
[i
]);
118 brelse (sb
->u
.ext2_sb
.s_sbh
);
123 static struct super_operations ext2_sops
= {
124 read_inode
: ext2_read_inode
,
125 write_inode
: ext2_write_inode
,
126 put_inode
: ext2_put_inode
,
127 delete_inode
: ext2_delete_inode
,
128 put_super
: ext2_put_super
,
129 write_super
: ext2_write_super
,
131 remount_fs
: ext2_remount
,
135 * This function has been shamelessly adapted from the msdos fs
137 static int parse_options (char * options
, unsigned long * sb_block
,
138 unsigned short *resuid
, unsigned short * resgid
,
139 unsigned long * mount_options
)
146 for (this_char
= strtok (options
, ",");
148 this_char
= strtok (NULL
, ",")) {
149 if ((value
= strchr (this_char
, '=')) != NULL
)
151 if (!strcmp (this_char
, "bsddf"))
152 clear_opt (*mount_options
, MINIX_DF
);
153 else if (!strcmp (this_char
, "nouid32")) {
154 set_opt (*mount_options
, NO_UID32
);
156 else if (!strcmp (this_char
, "check")) {
157 if (!value
|| !*value
|| !strcmp (value
, "none"))
158 clear_opt (*mount_options
, CHECK
);
160 #ifdef CONFIG_EXT2_CHECK
161 set_opt (*mount_options
, CHECK
);
163 printk("EXT2 Check option not supported\n");
166 else if (!strcmp (this_char
, "debug"))
167 set_opt (*mount_options
, DEBUG
);
168 else if (!strcmp (this_char
, "errors")) {
169 if (!value
|| !*value
) {
170 printk ("EXT2-fs: the errors option requires "
174 if (!strcmp (value
, "continue")) {
175 clear_opt (*mount_options
, ERRORS_RO
);
176 clear_opt (*mount_options
, ERRORS_PANIC
);
177 set_opt (*mount_options
, ERRORS_CONT
);
179 else if (!strcmp (value
, "remount-ro")) {
180 clear_opt (*mount_options
, ERRORS_CONT
);
181 clear_opt (*mount_options
, ERRORS_PANIC
);
182 set_opt (*mount_options
, ERRORS_RO
);
184 else if (!strcmp (value
, "panic")) {
185 clear_opt (*mount_options
, ERRORS_CONT
);
186 clear_opt (*mount_options
, ERRORS_RO
);
187 set_opt (*mount_options
, ERRORS_PANIC
);
190 printk ("EXT2-fs: Invalid errors option: %s\n",
195 else if (!strcmp (this_char
, "grpid") ||
196 !strcmp (this_char
, "bsdgroups"))
197 set_opt (*mount_options
, GRPID
);
198 else if (!strcmp (this_char
, "minixdf"))
199 set_opt (*mount_options
, MINIX_DF
);
200 else if (!strcmp (this_char
, "nocheck"))
201 clear_opt (*mount_options
, CHECK
);
202 else if (!strcmp (this_char
, "nogrpid") ||
203 !strcmp (this_char
, "sysvgroups"))
204 clear_opt (*mount_options
, GRPID
);
205 else if (!strcmp (this_char
, "resgid")) {
206 if (!value
|| !*value
) {
207 printk ("EXT2-fs: the resgid option requires "
211 *resgid
= simple_strtoul (value
, &value
, 0);
213 printk ("EXT2-fs: Invalid resgid option: %s\n",
218 else if (!strcmp (this_char
, "resuid")) {
219 if (!value
|| !*value
) {
220 printk ("EXT2-fs: the resuid option requires "
224 *resuid
= simple_strtoul (value
, &value
, 0);
226 printk ("EXT2-fs: Invalid resuid option: %s\n",
231 else if (!strcmp (this_char
, "sb")) {
232 if (!value
|| !*value
) {
233 printk ("EXT2-fs: the sb option requires "
237 *sb_block
= simple_strtoul (value
, &value
, 0);
239 printk ("EXT2-fs: Invalid sb option: %s\n",
244 /* Silently ignore the quota options */
245 else if (!strcmp (this_char
, "grpquota")
246 || !strcmp (this_char
, "noquota")
247 || !strcmp (this_char
, "quota")
248 || !strcmp (this_char
, "usrquota"))
249 /* Don't do anything ;-) */ ;
251 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char
);
258 static void ext2_setup_super (struct super_block
* sb
,
259 struct ext2_super_block
* es
)
261 if (le32_to_cpu(es
->s_rev_level
) > EXT2_MAX_SUPP_REV
) {
262 printk ("EXT2-fs warning: revision level too high, "
263 "forcing read/only mode\n");
264 sb
->s_flags
|= MS_RDONLY
;
266 if (!(sb
->s_flags
& MS_RDONLY
)) {
267 if (!(sb
->u
.ext2_sb
.s_mount_state
& EXT2_VALID_FS
))
268 printk ("EXT2-fs warning: mounting unchecked fs, "
269 "running e2fsck is recommended\n");
270 else if ((sb
->u
.ext2_sb
.s_mount_state
& EXT2_ERROR_FS
))
271 printk ("EXT2-fs warning: mounting fs with errors, "
272 "running e2fsck is recommended\n");
273 else if ((__s16
) le16_to_cpu(es
->s_max_mnt_count
) >= 0 &&
274 le16_to_cpu(es
->s_mnt_count
) >=
275 (unsigned short) (__s16
) le16_to_cpu(es
->s_max_mnt_count
))
276 printk ("EXT2-fs warning: maximal mount count reached, "
277 "running e2fsck is recommended\n");
278 else if (le32_to_cpu(es
->s_checkinterval
) &&
279 (le32_to_cpu(es
->s_lastcheck
) + le32_to_cpu(es
->s_checkinterval
) <= CURRENT_TIME
))
280 printk ("EXT2-fs warning: checktime reached, "
281 "running e2fsck is recommended\n");
282 es
->s_state
= cpu_to_le16(le16_to_cpu(es
->s_state
) & ~EXT2_VALID_FS
);
283 if (!(__s16
) le16_to_cpu(es
->s_max_mnt_count
))
284 es
->s_max_mnt_count
= (__s16
) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT
);
285 es
->s_mnt_count
=cpu_to_le16(le16_to_cpu(es
->s_mnt_count
) + 1);
286 es
->s_mtime
= cpu_to_le32(CURRENT_TIME
);
287 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
289 if (test_opt (sb
, DEBUG
))
290 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
291 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
292 EXT2FS_VERSION
, EXT2FS_DATE
, sb
->s_blocksize
,
293 sb
->u
.ext2_sb
.s_frag_size
,
294 sb
->u
.ext2_sb
.s_groups_count
,
295 EXT2_BLOCKS_PER_GROUP(sb
),
296 EXT2_INODES_PER_GROUP(sb
),
297 sb
->u
.ext2_sb
.s_mount_opt
);
298 #ifdef CONFIG_EXT2_CHECK
299 if (test_opt (sb
, CHECK
)) {
300 ext2_check_blocks_bitmap (sb
);
301 ext2_check_inodes_bitmap (sb
);
307 static int ext2_check_descriptors (struct super_block
* sb
)
311 unsigned long block
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_first_data_block
);
312 struct ext2_group_desc
* gdp
= NULL
;
314 ext2_debug ("Checking group descriptors");
316 for (i
= 0; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++)
318 if ((i
% EXT2_DESC_PER_BLOCK(sb
)) == 0)
319 gdp
= (struct ext2_group_desc
*) sb
->u
.ext2_sb
.s_group_desc
[desc_block
++]->b_data
;
320 if (le32_to_cpu(gdp
->bg_block_bitmap
) < block
||
321 le32_to_cpu(gdp
->bg_block_bitmap
) >= block
+ EXT2_BLOCKS_PER_GROUP(sb
))
323 ext2_error (sb
, "ext2_check_descriptors",
324 "Block bitmap for group %d"
325 " not in group (block %lu)!",
326 i
, (unsigned long) le32_to_cpu(gdp
->bg_block_bitmap
));
329 if (le32_to_cpu(gdp
->bg_inode_bitmap
) < block
||
330 le32_to_cpu(gdp
->bg_inode_bitmap
) >= block
+ EXT2_BLOCKS_PER_GROUP(sb
))
332 ext2_error (sb
, "ext2_check_descriptors",
333 "Inode bitmap for group %d"
334 " not in group (block %lu)!",
335 i
, (unsigned long) le32_to_cpu(gdp
->bg_inode_bitmap
));
338 if (le32_to_cpu(gdp
->bg_inode_table
) < block
||
339 le32_to_cpu(gdp
->bg_inode_table
) + sb
->u
.ext2_sb
.s_itb_per_group
>=
340 block
+ EXT2_BLOCKS_PER_GROUP(sb
))
342 ext2_error (sb
, "ext2_check_descriptors",
343 "Inode table for group %d"
344 " not in group (block %lu)!",
345 i
, (unsigned long) le32_to_cpu(gdp
->bg_inode_table
));
348 block
+= EXT2_BLOCKS_PER_GROUP(sb
);
354 #define log2(n) ffz(~(n))
356 struct super_block
* ext2_read_super (struct super_block
* sb
, void * data
,
359 struct buffer_head
* bh
;
360 struct ext2_super_block
* es
;
361 unsigned long sb_block
= 1;
362 unsigned short resuid
= EXT2_DEF_RESUID
;
363 unsigned short resgid
= EXT2_DEF_RESGID
;
364 unsigned long logic_sb_block
= 1;
365 unsigned long offset
= 0;
366 kdev_t dev
= sb
->s_dev
;
367 int blocksize
= BLOCK_SIZE
;
373 * See what the current blocksize for the device is, and
374 * use that as the blocksize. Otherwise (or if the blocksize
375 * is smaller than the default) use the default.
376 * This is important for devices that have a hardware
377 * sectorsize that is larger than the default.
379 blocksize
= get_hardblocksize(dev
);
380 if( blocksize
== 0 || blocksize
< BLOCK_SIZE
)
382 blocksize
= BLOCK_SIZE
;
385 sb
->u
.ext2_sb
.s_mount_opt
= 0;
386 if (!parse_options ((char *) data
, &sb_block
, &resuid
, &resgid
,
387 &sb
->u
.ext2_sb
.s_mount_opt
)) {
391 set_blocksize (dev
, blocksize
);
394 * If the superblock doesn't start on a sector boundary,
395 * calculate the offset. FIXME(eric) this doesn't make sense
396 * that we would have to do this.
398 if (blocksize
!= BLOCK_SIZE
) {
399 logic_sb_block
= (sb_block
*BLOCK_SIZE
) / blocksize
;
400 offset
= (sb_block
*BLOCK_SIZE
) % blocksize
;
403 if (!(bh
= bread (dev
, logic_sb_block
, blocksize
))) {
404 printk ("EXT2-fs: unable to read superblock\n");
408 * Note: s_es must be initialized s_es as soon as possible because
409 * some ext2 macro-instructions depend on its value
411 es
= (struct ext2_super_block
*) (((char *)bh
->b_data
) + offset
);
412 sb
->u
.ext2_sb
.s_es
= es
;
413 sb
->s_magic
= le16_to_cpu(es
->s_magic
);
414 if (sb
->s_magic
!= EXT2_SUPER_MAGIC
) {
416 printk ("VFS: Can't find an ext2 filesystem on dev "
417 "%s.\n", bdevname(dev
));
423 if (le32_to_cpu(es
->s_rev_level
) > EXT2_GOOD_OLD_REV
) {
424 if (le32_to_cpu(es
->s_feature_incompat
) & ~EXT2_FEATURE_INCOMPAT_SUPP
) {
425 printk("EXT2-fs: %s: couldn't mount because of "
426 "unsupported optional features.\n",
430 if (!(sb
->s_flags
& MS_RDONLY
) &&
431 (le32_to_cpu(es
->s_feature_ro_compat
) & ~EXT2_FEATURE_RO_COMPAT_SUPP
)) {
432 printk("EXT2-fs: %s: couldn't mount RDWR because of "
433 "unsupported optional features.\n",
438 sb
->s_blocksize_bits
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_log_block_size
) + 10;
439 sb
->s_blocksize
= 1 << sb
->s_blocksize_bits
;
440 if (sb
->s_blocksize
!= BLOCK_SIZE
&&
441 (sb
->s_blocksize
== 1024 || sb
->s_blocksize
== 2048 ||
442 sb
->s_blocksize
== 4096)) {
444 * Make sure the blocksize for the filesystem is larger
445 * than the hardware sectorsize for the machine.
447 hblock
= get_hardblocksize(dev
);
449 && (sb
->s_blocksize
< hblock
) )
451 printk("EXT2-fs: blocksize too small for device.\n");
456 set_blocksize (dev
, sb
->s_blocksize
);
457 logic_sb_block
= (sb_block
*BLOCK_SIZE
) / sb
->s_blocksize
;
458 offset
= (sb_block
*BLOCK_SIZE
) % sb
->s_blocksize
;
459 bh
= bread (dev
, logic_sb_block
, sb
->s_blocksize
);
461 printk("EXT2-fs: Couldn't read superblock on "
465 es
= (struct ext2_super_block
*) (((char *)bh
->b_data
) + offset
);
466 sb
->u
.ext2_sb
.s_es
= es
;
467 if (es
->s_magic
!= le16_to_cpu(EXT2_SUPER_MAGIC
)) {
468 printk ("EXT2-fs: Magic mismatch, very weird !\n");
472 if (le32_to_cpu(es
->s_rev_level
) == EXT2_GOOD_OLD_REV
) {
473 sb
->u
.ext2_sb
.s_inode_size
= EXT2_GOOD_OLD_INODE_SIZE
;
474 sb
->u
.ext2_sb
.s_first_ino
= EXT2_GOOD_OLD_FIRST_INO
;
476 sb
->u
.ext2_sb
.s_inode_size
= le16_to_cpu(es
->s_inode_size
);
477 sb
->u
.ext2_sb
.s_first_ino
= le32_to_cpu(es
->s_first_ino
);
478 if (sb
->u
.ext2_sb
.s_inode_size
!= EXT2_GOOD_OLD_INODE_SIZE
) {
479 printk ("EXT2-fs: unsupported inode size: %d\n",
480 sb
->u
.ext2_sb
.s_inode_size
);
484 sb
->u
.ext2_sb
.s_feature_compat
= le32_to_cpu(es
->s_feature_compat
);
485 sb
->u
.ext2_sb
.s_feature_incompat
= le32_to_cpu(es
->s_feature_incompat
);
486 sb
->u
.ext2_sb
.s_feature_ro_compat
= le32_to_cpu(es
->s_feature_ro_compat
);
487 sb
->u
.ext2_sb
.s_frag_size
= EXT2_MIN_FRAG_SIZE
<<
488 le32_to_cpu(es
->s_log_frag_size
);
489 if (sb
->u
.ext2_sb
.s_frag_size
)
490 sb
->u
.ext2_sb
.s_frags_per_block
= sb
->s_blocksize
/
491 sb
->u
.ext2_sb
.s_frag_size
;
494 sb
->u
.ext2_sb
.s_blocks_per_group
= le32_to_cpu(es
->s_blocks_per_group
);
495 sb
->u
.ext2_sb
.s_frags_per_group
= le32_to_cpu(es
->s_frags_per_group
);
496 sb
->u
.ext2_sb
.s_inodes_per_group
= le32_to_cpu(es
->s_inodes_per_group
);
497 sb
->u
.ext2_sb
.s_inodes_per_block
= sb
->s_blocksize
/
499 sb
->u
.ext2_sb
.s_itb_per_group
= sb
->u
.ext2_sb
.s_inodes_per_group
/
500 sb
->u
.ext2_sb
.s_inodes_per_block
;
501 sb
->u
.ext2_sb
.s_desc_per_block
= sb
->s_blocksize
/
502 sizeof (struct ext2_group_desc
);
503 sb
->u
.ext2_sb
.s_sbh
= bh
;
504 if (resuid
!= EXT2_DEF_RESUID
)
505 sb
->u
.ext2_sb
.s_resuid
= resuid
;
507 sb
->u
.ext2_sb
.s_resuid
= le16_to_cpu(es
->s_def_resuid
);
508 if (resgid
!= EXT2_DEF_RESGID
)
509 sb
->u
.ext2_sb
.s_resgid
= resgid
;
511 sb
->u
.ext2_sb
.s_resgid
= le16_to_cpu(es
->s_def_resgid
);
512 sb
->u
.ext2_sb
.s_mount_state
= le16_to_cpu(es
->s_state
);
513 sb
->u
.ext2_sb
.s_addr_per_block_bits
=
514 log2 (EXT2_ADDR_PER_BLOCK(sb
));
515 sb
->u
.ext2_sb
.s_desc_per_block_bits
=
516 log2 (EXT2_DESC_PER_BLOCK(sb
));
517 if (sb
->s_magic
!= EXT2_SUPER_MAGIC
) {
519 printk ("VFS: Can't find an ext2 filesystem on dev "
524 if (sb
->s_blocksize
!= bh
->b_size
) {
526 printk ("VFS: Unsupported blocksize on dev "
527 "%s.\n", bdevname(dev
));
531 if (sb
->s_blocksize
!= sb
->u
.ext2_sb
.s_frag_size
) {
532 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
533 sb
->u
.ext2_sb
.s_frag_size
, sb
->s_blocksize
);
537 if (sb
->u
.ext2_sb
.s_blocks_per_group
> sb
->s_blocksize
* 8) {
538 printk ("EXT2-fs: #blocks per group too big: %lu\n",
539 sb
->u
.ext2_sb
.s_blocks_per_group
);
542 if (sb
->u
.ext2_sb
.s_frags_per_group
> sb
->s_blocksize
* 8) {
543 printk ("EXT2-fs: #fragments per group too big: %lu\n",
544 sb
->u
.ext2_sb
.s_frags_per_group
);
547 if (sb
->u
.ext2_sb
.s_inodes_per_group
> sb
->s_blocksize
* 8) {
548 printk ("EXT2-fs: #inodes per group too big: %lu\n",
549 sb
->u
.ext2_sb
.s_inodes_per_group
);
553 sb
->u
.ext2_sb
.s_groups_count
= (le32_to_cpu(es
->s_blocks_count
) -
554 le32_to_cpu(es
->s_first_data_block
) +
555 EXT2_BLOCKS_PER_GROUP(sb
) - 1) /
556 EXT2_BLOCKS_PER_GROUP(sb
);
557 db_count
= (sb
->u
.ext2_sb
.s_groups_count
+ EXT2_DESC_PER_BLOCK(sb
) - 1) /
558 EXT2_DESC_PER_BLOCK(sb
);
559 sb
->u
.ext2_sb
.s_group_desc
= kmalloc (db_count
* sizeof (struct buffer_head
*), GFP_KERNEL
);
560 if (sb
->u
.ext2_sb
.s_group_desc
== NULL
) {
561 printk ("EXT2-fs: not enough memory\n");
564 for (i
= 0; i
< db_count
; i
++) {
565 sb
->u
.ext2_sb
.s_group_desc
[i
] = bread (dev
, logic_sb_block
+ i
+ 1,
567 if (!sb
->u
.ext2_sb
.s_group_desc
[i
]) {
568 for (j
= 0; j
< i
; j
++)
569 brelse (sb
->u
.ext2_sb
.s_group_desc
[j
]);
570 kfree_s (sb
->u
.ext2_sb
.s_group_desc
,
571 db_count
* sizeof (struct buffer_head
*));
572 printk ("EXT2-fs: unable to read group descriptors\n");
576 if (!ext2_check_descriptors (sb
)) {
577 for (j
= 0; j
< db_count
; j
++)
578 brelse (sb
->u
.ext2_sb
.s_group_desc
[j
]);
579 kfree_s (sb
->u
.ext2_sb
.s_group_desc
,
580 db_count
* sizeof (struct buffer_head
*));
581 printk ("EXT2-fs: group descriptors corrupted !\n");
584 for (i
= 0; i
< EXT2_MAX_GROUP_LOADED
; i
++) {
585 sb
->u
.ext2_sb
.s_inode_bitmap_number
[i
] = 0;
586 sb
->u
.ext2_sb
.s_inode_bitmap
[i
] = NULL
;
587 sb
->u
.ext2_sb
.s_block_bitmap_number
[i
] = 0;
588 sb
->u
.ext2_sb
.s_block_bitmap
[i
] = NULL
;
590 sb
->u
.ext2_sb
.s_loaded_inode_bitmaps
= 0;
591 sb
->u
.ext2_sb
.s_loaded_block_bitmaps
= 0;
592 sb
->u
.ext2_sb
.s_db_per_group
= db_count
;
594 * set up enough so that it can read an inode
597 sb
->s_op
= &ext2_sops
;
598 sb
->s_root
= d_alloc_root(iget(sb
, EXT2_ROOT_INO
));
600 for (i
= 0; i
< db_count
; i
++)
601 if (sb
->u
.ext2_sb
.s_group_desc
[i
])
602 brelse (sb
->u
.ext2_sb
.s_group_desc
[i
]);
603 kfree_s (sb
->u
.ext2_sb
.s_group_desc
,
604 db_count
* sizeof (struct buffer_head
*));
606 printk ("EXT2-fs: get root inode failed\n");
609 ext2_setup_super (sb
, es
);
613 static void ext2_commit_super (struct super_block
* sb
,
614 struct ext2_super_block
* es
)
616 es
->s_wtime
= cpu_to_le32(CURRENT_TIME
);
617 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
622 * In the second extended file system, it is not necessary to
623 * write the super block since we use a mapping of the
624 * disk super block in a buffer.
626 * However, this function is still used to set the fs valid
627 * flags to 0. We need to set this flag to 0 since the fs
628 * may have been checked while mounted and e2fsck may have
629 * set s_state to EXT2_VALID_FS after some corrections.
632 void ext2_write_super (struct super_block
* sb
)
634 struct ext2_super_block
* es
;
636 if (!(sb
->s_flags
& MS_RDONLY
)) {
637 es
= sb
->u
.ext2_sb
.s_es
;
639 ext2_debug ("setting valid to 0\n");
641 if (le16_to_cpu(es
->s_state
) & EXT2_VALID_FS
) {
642 es
->s_state
= cpu_to_le16(le16_to_cpu(es
->s_state
) & ~EXT2_VALID_FS
);
643 es
->s_mtime
= cpu_to_le32(CURRENT_TIME
);
645 ext2_commit_super (sb
, es
);
650 int ext2_remount (struct super_block
* sb
, int * flags
, char * data
)
652 struct ext2_super_block
* es
;
653 unsigned short resuid
= sb
->u
.ext2_sb
.s_resuid
;
654 unsigned short resgid
= sb
->u
.ext2_sb
.s_resgid
;
655 unsigned long new_mount_opt
;
659 * Allow the "check" option to be passed as a remount option.
661 if (!parse_options (data
, &tmp
, &resuid
, &resgid
,
665 sb
->u
.ext2_sb
.s_mount_opt
= new_mount_opt
;
666 sb
->u
.ext2_sb
.s_resuid
= resuid
;
667 sb
->u
.ext2_sb
.s_resgid
= resgid
;
668 es
= sb
->u
.ext2_sb
.s_es
;
669 if ((*flags
& MS_RDONLY
) == (sb
->s_flags
& MS_RDONLY
))
671 if (*flags
& MS_RDONLY
) {
672 if (le16_to_cpu(es
->s_state
) & EXT2_VALID_FS
||
673 !(sb
->u
.ext2_sb
.s_mount_state
& EXT2_VALID_FS
))
676 * OK, we are remounting a valid rw partition rdonly, so set
677 * the rdonly flag and then mark the partition as valid again.
679 es
->s_state
= cpu_to_le16(sb
->u
.ext2_sb
.s_mount_state
);
680 es
->s_mtime
= cpu_to_le32(CURRENT_TIME
);
681 mark_buffer_dirty(sb
->u
.ext2_sb
.s_sbh
, 1);
683 ext2_commit_super (sb
, es
);
687 * Mounting a RDONLY partition read-write, so reread and
688 * store the current valid flag. (It may have been changed
689 * by e2fsck since we originally mounted the partition.)
691 sb
->u
.ext2_sb
.s_mount_state
= le16_to_cpu(es
->s_state
);
692 sb
->s_flags
&= ~MS_RDONLY
;
693 ext2_setup_super (sb
, es
);
698 int ext2_statfs (struct super_block
* sb
, struct statfs
* buf
)
700 unsigned long overhead
;
703 if (test_opt (sb
, MINIX_DF
))
707 * Compute the overhead (FS structures)
711 * All of the blocks before first_data_block are
714 overhead
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_first_data_block
);
717 * Add the overhead attributed to the superblock and
718 * block group descriptors. If this is sparse
719 * superblocks is turned on, then not all groups have
722 if (sb
->u
.ext2_sb
.s_feature_ro_compat
&
723 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) {
725 for (i
=0 ; i
< sb
->u
.ext2_sb
.s_groups_count
; i
++)
726 if (ext2_group_sparse(i
))
729 ngroups
= sb
->u
.ext2_sb
.s_groups_count
;
730 overhead
+= ngroups
* (1 + sb
->u
.ext2_sb
.s_db_per_group
);
733 * Every block group has an inode bitmap, a block
734 * bitmap, and an inode table.
736 overhead
+= (sb
->u
.ext2_sb
.s_groups_count
*
737 (2 + sb
->u
.ext2_sb
.s_itb_per_group
));
740 buf
->f_type
= EXT2_SUPER_MAGIC
;
741 buf
->f_bsize
= sb
->s_blocksize
;
742 buf
->f_blocks
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_blocks_count
) - overhead
;
743 buf
->f_bfree
= ext2_count_free_blocks (sb
);
744 buf
->f_bavail
= buf
->f_bfree
- le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_r_blocks_count
);
745 if (buf
->f_bfree
< le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_r_blocks_count
))
747 buf
->f_files
= le32_to_cpu(sb
->u
.ext2_sb
.s_es
->s_inodes_count
);
748 buf
->f_ffree
= ext2_count_free_inodes (sb
);
749 buf
->f_namelen
= EXT2_NAME_LEN
;
753 static DECLARE_FSTYPE_DEV(ext2_fs_type
, "ext2", ext2_read_super
);
755 static int __init
init_ext2_fs(void)
757 return register_filesystem(&ext2_fs_type
);
760 static void __exit
exit_ext2_fs(void)
762 unregister_filesystem(&ext2_fs_type
);
767 module_init(init_ext2_fs
)
768 module_exit(exit_ext2_fs
)