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>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/blkdev.h>
25 #include <linux/parser.h>
26 #include <linux/random.h>
27 #include <linux/buffer_head.h>
28 #include <linux/smp_lock.h>
29 #include <linux/vfs.h>
30 #include <asm/uaccess.h>
35 static void ext2_sync_super(struct super_block *sb,
36 struct ext2_super_block *es);
37 static int ext2_remount (struct super_block * sb, int * flags, char * data);
38 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf);
40 static char error_buf[1024];
42 void ext2_error (struct super_block * sb, const char * function,
43 const char * fmt, ...)
46 struct ext2_sb_info *sbi = EXT2_SB(sb);
47 struct ext2_super_block *es = sbi->s_es;
49 if (!(sb->s_flags & MS_RDONLY)) {
50 sbi->s_mount_state |= EXT2_ERROR_FS;
52 cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
53 ext2_sync_super(sb, es);
56 vsprintf (error_buf, fmt, args);
58 if (test_opt (sb, ERRORS_PANIC))
59 panic ("EXT2-fs panic (device %s): %s: %s\n",
60 sb->s_id, function, error_buf);
61 printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
62 sb->s_id, function, error_buf);
63 if (test_opt (sb, ERRORS_RO)) {
64 printk ("Remounting filesystem read-only\n");
65 sb->s_flags |= MS_RDONLY;
69 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
70 const char * fmt, ...)
73 struct ext2_sb_info *sbi = EXT2_SB(sb);
75 if (!(sb->s_flags & MS_RDONLY)) {
76 sbi->s_mount_state |= EXT2_ERROR_FS;
78 cpu_to_le16(le16_to_cpu(sbi->s_es->s_state) | EXT2_ERROR_FS);
79 mark_buffer_dirty(sbi->s_sbh);
83 vsprintf (error_buf, fmt, args);
85 sb->s_flags |= MS_RDONLY;
86 panic ("EXT2-fs panic (device %s): %s: %s\n",
87 sb->s_id, function, error_buf);
90 void ext2_warning (struct super_block * sb, const char * function,
91 const char * fmt, ...)
96 vsprintf (error_buf, fmt, args);
98 printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
99 sb->s_id, function, error_buf);
102 void ext2_update_dynamic_rev(struct super_block *sb)
104 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
106 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
109 ext2_warning(sb, __FUNCTION__,
110 "updating to rev %d because of new feature flag, "
111 "running e2fsck is recommended",
114 es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
115 es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
116 es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
117 /* leave es->s_feature_*compat flags alone */
118 /* es->s_uuid will be set by e2fsck if empty */
121 * The rest of the superblock fields should be zero, and if not it
122 * means they are likely already in use, so leave them alone. We
123 * can leave it up to e2fsck to clean up any inconsistencies there.
127 static void ext2_put_super (struct super_block * sb)
131 struct ext2_sb_info *sbi = EXT2_SB(sb);
133 ext2_xattr_put_super(sb);
134 if (!(sb->s_flags & MS_RDONLY)) {
135 struct ext2_super_block *es = sbi->s_es;
137 es->s_state = le16_to_cpu(sbi->s_mount_state);
138 ext2_sync_super(sb, es);
140 db_count = sbi->s_gdb_count;
141 for (i = 0; i < db_count; i++)
142 if (sbi->s_group_desc[i])
143 brelse (sbi->s_group_desc[i]);
144 kfree(sbi->s_group_desc);
147 sb->s_fs_info = NULL;
153 static kmem_cache_t * ext2_inode_cachep;
155 static struct inode *ext2_alloc_inode(struct super_block *sb)
157 struct ext2_inode_info *ei;
158 ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
161 #ifdef CONFIG_EXT2_FS_POSIX_ACL
162 ei->i_acl = EXT2_ACL_NOT_CACHED;
163 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
165 ei->vfs_inode.i_version = 1;
166 return &ei->vfs_inode;
169 static void ext2_destroy_inode(struct inode *inode)
171 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
174 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
176 struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
178 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
179 SLAB_CTOR_CONSTRUCTOR) {
180 rwlock_init(&ei->i_meta_lock);
181 #ifdef CONFIG_EXT2_FS_XATTR
182 init_rwsem(&ei->xattr_sem);
184 inode_init_once(&ei->vfs_inode);
188 static int init_inodecache(void)
190 ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
191 sizeof(struct ext2_inode_info),
192 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
194 if (ext2_inode_cachep == NULL)
199 static void destroy_inodecache(void)
201 if (kmem_cache_destroy(ext2_inode_cachep))
202 printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
205 #ifdef CONFIG_EXT2_FS_POSIX_ACL
207 static void ext2_clear_inode(struct inode *inode)
209 struct ext2_inode_info *ei = EXT2_I(inode);
211 if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
212 posix_acl_release(ei->i_acl);
213 ei->i_acl = EXT2_ACL_NOT_CACHED;
215 if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
216 posix_acl_release(ei->i_default_acl);
217 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
222 # define ext2_clear_inode NULL
225 static struct super_operations ext2_sops = {
226 .alloc_inode = ext2_alloc_inode,
227 .destroy_inode = ext2_destroy_inode,
228 .read_inode = ext2_read_inode,
229 .write_inode = ext2_write_inode,
230 .put_inode = ext2_put_inode,
231 .delete_inode = ext2_delete_inode,
232 .put_super = ext2_put_super,
233 .write_super = ext2_write_super,
234 .statfs = ext2_statfs,
235 .remount_fs = ext2_remount,
236 .clear_inode = ext2_clear_inode,
239 /* Yes, most of these are left as NULL!!
240 * A NULL value implies the default, which works with ext2-like file
241 * systems, but can be improved upon.
242 * Currently only get_parent is required.
244 struct dentry *ext2_get_parent(struct dentry *child);
245 static struct export_operations ext2_export_ops = {
246 .get_parent = ext2_get_parent,
249 static unsigned long get_sb_block(void **data)
251 unsigned long sb_block;
252 char *options = (char *) *data;
254 if (!options || strncmp(options, "sb=", 3) != 0)
255 return 1; /* Default location */
257 sb_block = simple_strtoul(options, &options, 0);
258 if (*options && *options != ',') {
259 printk("EXT2-fs: Invalid sb specification: %s\n",
265 *data = (void *) options;
270 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
271 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
272 Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov, Opt_nobh,
273 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
277 static match_table_t tokens = {
278 {Opt_bsd_df, "bsddf"},
279 {Opt_minix_df, "minixdf"},
280 {Opt_grpid, "grpid"},
281 {Opt_grpid, "bsdgroups"},
282 {Opt_nogrpid, "nogrpid"},
283 {Opt_nogrpid, "sysvgroups"},
284 {Opt_resgid, "resgid=%u"},
285 {Opt_resuid, "resuid=%u"},
287 {Opt_err_cont, "errors=continue"},
288 {Opt_err_panic, "errors=panic"},
289 {Opt_err_ro, "errors=remount-ro"},
290 {Opt_nouid32, "nouid32"},
291 {Opt_nocheck, "check=none"},
292 {Opt_nocheck, "nocheck"},
293 {Opt_check, "check"},
294 {Opt_debug, "debug"},
295 {Opt_oldalloc, "oldalloc"},
296 {Opt_orlov, "orlov"},
298 {Opt_user_xattr, "user_xattr"},
299 {Opt_nouser_xattr, "nouser_xattr"},
301 {Opt_noacl, "noacl"},
302 {Opt_ignore, "grpquota"},
303 {Opt_ignore, "noquota"},
304 {Opt_ignore, "quota"},
305 {Opt_ignore, "usrquota"},
309 static int parse_options (char * options,
310 struct ext2_sb_info *sbi)
313 substring_t args[MAX_OPT_ARGS];
314 unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
320 while ((p = strsep (&options, ",")) != NULL) {
325 token = match_token(p, tokens, args);
328 clear_opt (sbi->s_mount_opt, MINIX_DF);
331 set_opt (sbi->s_mount_opt, MINIX_DF);
334 set_opt (sbi->s_mount_opt, GRPID);
337 clear_opt (sbi->s_mount_opt, GRPID);
340 if (match_int(&args[0], &option))
342 sbi->s_resuid = option;
345 if (match_int(&args[0], &option))
347 sbi->s_resgid = option;
350 /* handled by get_sb_block() instead of here */
351 /* *sb_block = match_int(&args[0]); */
354 kind = EXT2_MOUNT_ERRORS_PANIC;
357 kind = EXT2_MOUNT_ERRORS_RO;
360 kind = EXT2_MOUNT_ERRORS_CONT;
363 set_opt (sbi->s_mount_opt, NO_UID32);
366 #ifdef CONFIG_EXT2_CHECK
367 set_opt (sbi->s_mount_opt, CHECK);
369 printk("EXT2 Check option not supported\n");
373 clear_opt (sbi->s_mount_opt, CHECK);
376 set_opt (sbi->s_mount_opt, DEBUG);
379 set_opt (sbi->s_mount_opt, OLDALLOC);
382 clear_opt (sbi->s_mount_opt, OLDALLOC);
385 set_opt (sbi->s_mount_opt, NOBH);
387 #ifdef CONFIG_EXT2_FS_XATTR
389 set_opt (sbi->s_mount_opt, XATTR_USER);
391 case Opt_nouser_xattr:
392 clear_opt (sbi->s_mount_opt, XATTR_USER);
396 case Opt_nouser_xattr:
397 printk("EXT2 (no)user_xattr options not supported\n");
400 #ifdef CONFIG_EXT2_FS_POSIX_ACL
402 set_opt(sbi->s_mount_opt, POSIX_ACL);
405 clear_opt(sbi->s_mount_opt, POSIX_ACL);
410 printk("EXT2 (no)acl options not supported\n");
419 sbi->s_mount_opt |= kind;
423 static int ext2_setup_super (struct super_block * sb,
424 struct ext2_super_block * es,
428 struct ext2_sb_info *sbi = EXT2_SB(sb);
430 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
431 printk ("EXT2-fs warning: revision level too high, "
432 "forcing read-only mode\n");
437 if (!(sbi->s_mount_state & EXT2_VALID_FS))
438 printk ("EXT2-fs warning: mounting unchecked fs, "
439 "running e2fsck is recommended\n");
440 else if ((sbi->s_mount_state & EXT2_ERROR_FS))
441 printk ("EXT2-fs warning: mounting fs with errors, "
442 "running e2fsck is recommended\n");
443 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
444 le16_to_cpu(es->s_mnt_count) >=
445 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
446 printk ("EXT2-fs warning: maximal mount count reached, "
447 "running e2fsck is recommended\n");
448 else if (le32_to_cpu(es->s_checkinterval) &&
449 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
450 printk ("EXT2-fs warning: checktime reached, "
451 "running e2fsck is recommended\n");
452 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
453 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
454 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
455 ext2_write_super(sb);
456 if (test_opt (sb, DEBUG))
457 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
458 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
459 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
462 EXT2_BLOCKS_PER_GROUP(sb),
463 EXT2_INODES_PER_GROUP(sb),
465 #ifdef CONFIG_EXT2_CHECK
466 if (test_opt (sb, CHECK)) {
467 ext2_check_blocks_bitmap (sb);
468 ext2_check_inodes_bitmap (sb);
474 static int ext2_check_descriptors (struct super_block * sb)
478 struct ext2_sb_info *sbi = EXT2_SB(sb);
479 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
480 struct ext2_group_desc * gdp = NULL;
482 ext2_debug ("Checking group descriptors");
484 for (i = 0; i < sbi->s_groups_count; i++)
486 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
487 gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
488 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
489 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
491 ext2_error (sb, "ext2_check_descriptors",
492 "Block bitmap for group %d"
493 " not in group (block %lu)!",
494 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
497 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
498 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
500 ext2_error (sb, "ext2_check_descriptors",
501 "Inode bitmap for group %d"
502 " not in group (block %lu)!",
503 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
506 if (le32_to_cpu(gdp->bg_inode_table) < block ||
507 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
508 block + EXT2_BLOCKS_PER_GROUP(sb))
510 ext2_error (sb, "ext2_check_descriptors",
511 "Inode table for group %d"
512 " not in group (block %lu)!",
513 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
516 block += EXT2_BLOCKS_PER_GROUP(sb);
522 #define log2(n) ffz(~(n))
525 * Maximal file size. There is a direct, and {,double-,triple-}indirect
526 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
527 * We need to be 1 filesystem block less than the 2^32 sector limit.
529 static loff_t ext2_max_size(int bits)
531 loff_t res = EXT2_NDIR_BLOCKS;
532 res += 1LL << (bits-2);
533 res += 1LL << (2*(bits-2));
534 res += 1LL << (3*(bits-2));
536 if (res > (512LL << 32) - (1 << bits))
537 res = (512LL << 32) - (1 << bits);
541 static unsigned long descriptor_loc(struct super_block *sb,
542 unsigned long logic_sb_block,
545 struct ext2_sb_info *sbi = EXT2_SB(sb);
546 unsigned long bg, first_data_block, first_meta_bg;
549 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
550 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
552 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
554 return (logic_sb_block + nr + 1);
555 bg = sbi->s_desc_per_block * nr;
556 if (ext2_bg_has_super(sb, bg))
558 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
561 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
563 struct buffer_head * bh;
564 struct ext2_sb_info * sbi;
565 struct ext2_super_block * es;
568 unsigned long sb_block = get_sb_block(&data);
569 unsigned long logic_sb_block;
570 unsigned long offset = 0;
571 unsigned long def_mount_opts;
572 int blocksize = BLOCK_SIZE;
576 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
580 memset(sbi, 0, sizeof(*sbi));
583 * See what the current blocksize for the device is, and
584 * use that as the blocksize. Otherwise (or if the blocksize
585 * is smaller than the default) use the default.
586 * This is important for devices that have a hardware
587 * sectorsize that is larger than the default.
589 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
591 printk ("EXT2-fs: unable to set blocksize\n");
596 * If the superblock doesn't start on a hardware sector boundary,
597 * calculate the offset.
599 if (blocksize != BLOCK_SIZE) {
600 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
601 offset = (sb_block*BLOCK_SIZE) % blocksize;
603 logic_sb_block = sb_block;
606 if (!(bh = sb_bread(sb, logic_sb_block))) {
607 printk ("EXT2-fs: unable to read superblock\n");
611 * Note: s_es must be initialized as soon as possible because
612 * some ext2 macro-instructions depend on its value
614 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
616 sb->s_magic = le16_to_cpu(es->s_magic);
617 sb->s_flags |= MS_ONE_SECOND;
618 if (sb->s_magic != EXT2_SUPER_MAGIC) {
620 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
625 /* Set defaults before we parse the mount options */
626 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
627 if (def_mount_opts & EXT2_DEFM_DEBUG)
628 set_opt(sbi->s_mount_opt, DEBUG);
629 if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
630 set_opt(sbi->s_mount_opt, GRPID);
631 if (def_mount_opts & EXT2_DEFM_UID16)
632 set_opt(sbi->s_mount_opt, NO_UID32);
633 if (def_mount_opts & EXT2_DEFM_XATTR_USER)
634 set_opt(sbi->s_mount_opt, XATTR_USER);
635 if (def_mount_opts & EXT2_DEFM_ACL)
636 set_opt(sbi->s_mount_opt, POSIX_ACL);
638 if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
639 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
640 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
641 set_opt(sbi->s_mount_opt, ERRORS_RO);
643 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
644 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
646 if (!parse_options ((char *) data, sbi))
649 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
650 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
653 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
654 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
655 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
656 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
657 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
658 "running e2fsck is recommended\n");
660 * Check feature flags regardless of the revision level, since we
661 * previously didn't change the revision level when setting the flags,
662 * so there is a chance incompat flags are set on a rev 0 filesystem.
664 if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
665 printk("EXT2-fs: %s: couldn't mount because of "
666 "unsupported optional features (%x).\n",
670 if (!(sb->s_flags & MS_RDONLY) &&
671 (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
672 printk("EXT2-fs: %s: couldn't mount RDWR because of "
673 "unsupported optional features (%x).\n",
677 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
678 /* If the blocksize doesn't match, re-read the thing.. */
679 if (sb->s_blocksize != blocksize) {
682 if (!sb_set_blocksize(sb, blocksize)) {
683 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
687 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
688 offset = (sb_block*BLOCK_SIZE) % blocksize;
689 bh = sb_bread(sb, logic_sb_block);
691 printk("EXT2-fs: Couldn't read superblock on "
695 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
697 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
698 printk ("EXT2-fs: Magic mismatch, very weird !\n");
703 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
705 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
706 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
707 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
709 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
710 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
711 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
712 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
713 (sbi->s_inode_size > blocksize)) {
714 printk ("EXT2-fs: unsupported inode size: %d\n",
719 sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
720 le32_to_cpu(es->s_log_frag_size);
721 if (sbi->s_frag_size)
722 sbi->s_frags_per_block = sb->s_blocksize /
726 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
727 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
728 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
729 sbi->s_inodes_per_block = sb->s_blocksize /
731 sbi->s_itb_per_group = sbi->s_inodes_per_group /
732 sbi->s_inodes_per_block;
733 sbi->s_desc_per_block = sb->s_blocksize /
734 sizeof (struct ext2_group_desc);
736 sbi->s_mount_state = le16_to_cpu(es->s_state);
737 sbi->s_addr_per_block_bits =
738 log2 (EXT2_ADDR_PER_BLOCK(sb));
739 sbi->s_desc_per_block_bits =
740 log2 (EXT2_DESC_PER_BLOCK(sb));
741 if (sb->s_magic != EXT2_SUPER_MAGIC) {
743 printk ("VFS: Can't find an ext2 filesystem on dev "
748 if (sb->s_blocksize != bh->b_size) {
750 printk ("VFS: Unsupported blocksize on dev "
755 if (sb->s_blocksize != sbi->s_frag_size) {
756 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
757 sbi->s_frag_size, sb->s_blocksize);
761 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
762 printk ("EXT2-fs: #blocks per group too big: %lu\n",
763 sbi->s_blocks_per_group);
766 if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
767 printk ("EXT2-fs: #fragments per group too big: %lu\n",
768 sbi->s_frags_per_group);
771 if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
772 printk ("EXT2-fs: #inodes per group too big: %lu\n",
773 sbi->s_inodes_per_group);
777 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
778 le32_to_cpu(es->s_first_data_block) +
779 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
780 EXT2_BLOCKS_PER_GROUP(sb);
781 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
782 EXT2_DESC_PER_BLOCK(sb);
783 sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
784 if (sbi->s_group_desc == NULL) {
785 printk ("EXT2-fs: not enough memory\n");
788 percpu_counter_init(&sbi->s_freeblocks_counter);
789 percpu_counter_init(&sbi->s_freeinodes_counter);
790 percpu_counter_init(&sbi->s_dirs_counter);
791 bgl_lock_init(&sbi->s_blockgroup_lock);
792 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
795 printk ("EXT2-fs: not enough memory\n");
796 goto failed_mount_group_desc;
798 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
799 for (i = 0; i < db_count; i++) {
800 block = descriptor_loc(sb, logic_sb_block, i);
801 sbi->s_group_desc[i] = sb_bread(sb, block);
802 if (!sbi->s_group_desc[i]) {
803 for (j = 0; j < i; j++)
804 brelse (sbi->s_group_desc[j]);
805 printk ("EXT2-fs: unable to read group descriptors\n");
806 goto failed_mount_group_desc;
809 if (!ext2_check_descriptors (sb)) {
810 printk ("EXT2-fs: group descriptors corrupted!\n");
814 sbi->s_gdb_count = db_count;
815 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
816 spin_lock_init(&sbi->s_next_gen_lock);
818 * set up enough so that it can read an inode
820 sb->s_op = &ext2_sops;
821 sb->s_export_op = &ext2_export_ops;
822 root = iget(sb, EXT2_ROOT_INO);
823 sb->s_root = d_alloc_root(root);
826 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
829 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
832 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
835 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
836 ext2_warning(sb, __FUNCTION__,
837 "mounting ext3 filesystem as ext2\n");
838 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
839 percpu_counter_mod(&sbi->s_freeblocks_counter,
840 ext2_count_free_blocks(sb));
841 percpu_counter_mod(&sbi->s_freeinodes_counter,
842 ext2_count_free_inodes(sb));
843 percpu_counter_mod(&sbi->s_dirs_counter,
844 ext2_count_dirs(sb));
847 for (i = 0; i < db_count; i++)
848 brelse(sbi->s_group_desc[i]);
849 failed_mount_group_desc:
850 kfree(sbi->s_group_desc);
856 sb->s_fs_info = NULL;
861 static void ext2_commit_super (struct super_block * sb,
862 struct ext2_super_block * es)
864 es->s_wtime = cpu_to_le32(get_seconds());
865 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
869 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
871 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
872 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
873 es->s_wtime = cpu_to_le32(get_seconds());
874 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
875 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
880 * In the second extended file system, it is not necessary to
881 * write the super block since we use a mapping of the
882 * disk super block in a buffer.
884 * However, this function is still used to set the fs valid
885 * flags to 0. We need to set this flag to 0 since the fs
886 * may have been checked while mounted and e2fsck may have
887 * set s_state to EXT2_VALID_FS after some corrections.
890 void ext2_write_super (struct super_block * sb)
892 struct ext2_super_block * es;
894 if (!(sb->s_flags & MS_RDONLY)) {
895 es = EXT2_SB(sb)->s_es;
897 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
898 ext2_debug ("setting valid to 0\n");
899 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
901 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
902 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
903 es->s_mtime = cpu_to_le32(get_seconds());
904 ext2_sync_super(sb, es);
906 ext2_commit_super (sb, es);
912 static int ext2_remount (struct super_block * sb, int * flags, char * data)
914 struct ext2_sb_info * sbi = EXT2_SB(sb);
915 struct ext2_super_block * es;
918 * Allow the "check" option to be passed as a remount option.
920 if (!parse_options (data, sbi))
923 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
924 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
927 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
929 if (*flags & MS_RDONLY) {
930 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
931 !(sbi->s_mount_state & EXT2_VALID_FS))
934 * OK, we are remounting a valid rw partition rdonly, so set
935 * the rdonly flag and then mark the partition as valid again.
937 es->s_state = cpu_to_le16(sbi->s_mount_state);
938 es->s_mtime = cpu_to_le32(get_seconds());
941 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
942 ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
943 printk("EXT2-fs: %s: couldn't remount RDWR because of "
944 "unsupported optional features (%x).\n",
949 * Mounting a RDONLY partition read-write, so reread and
950 * store the current valid flag. (It may have been changed
951 * by e2fsck since we originally mounted the partition.)
953 sbi->s_mount_state = le16_to_cpu(es->s_state);
954 if (!ext2_setup_super (sb, es, 0))
955 sb->s_flags &= ~MS_RDONLY;
957 ext2_sync_super(sb, es);
961 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
963 struct ext2_sb_info *sbi = EXT2_SB(sb);
964 unsigned long overhead;
967 if (test_opt (sb, MINIX_DF))
971 * Compute the overhead (FS structures)
975 * All of the blocks before first_data_block are
978 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
981 * Add the overhead attributed to the superblock and
982 * block group descriptors. If the sparse superblocks
983 * feature is turned on, then not all groups have this.
985 for (i = 0; i < sbi->s_groups_count; i++)
986 overhead += ext2_bg_has_super(sb, i) +
987 ext2_bg_num_gdb(sb, i);
990 * Every block group has an inode bitmap, a block
991 * bitmap, and an inode table.
993 overhead += (sbi->s_groups_count *
994 (2 + sbi->s_itb_per_group));
997 buf->f_type = EXT2_SUPER_MAGIC;
998 buf->f_bsize = sb->s_blocksize;
999 buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
1000 buf->f_bfree = ext2_count_free_blocks(sb);
1001 buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
1002 if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
1004 buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
1005 buf->f_ffree = ext2_count_free_inodes (sb);
1006 buf->f_namelen = EXT2_NAME_LEN;
1010 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
1011 int flags, const char *dev_name, void *data)
1013 return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1016 static struct file_system_type ext2_fs_type = {
1017 .owner = THIS_MODULE,
1019 .get_sb = ext2_get_sb,
1020 .kill_sb = kill_block_super,
1021 .fs_flags = FS_REQUIRES_DEV,
1024 static int __init init_ext2_fs(void)
1026 int err = init_ext2_xattr();
1029 err = init_inodecache();
1032 err = register_filesystem(&ext2_fs_type);
1037 destroy_inodecache();
1043 static void __exit exit_ext2_fs(void)
1045 unregister_filesystem(&ext2_fs_type);
1046 destroy_inodecache();
1050 module_init(init_ext2_fs)
1051 module_exit(exit_ext2_fs)