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 void ext2_error (struct super_block * sb, const char * function,
41 const char * fmt, ...)
44 struct ext2_sb_info *sbi = EXT2_SB(sb);
45 struct ext2_super_block *es = sbi->s_es;
47 if (!(sb->s_flags & MS_RDONLY)) {
48 sbi->s_mount_state |= EXT2_ERROR_FS;
50 cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
51 ext2_sync_super(sb, es);
55 printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
60 if (test_opt(sb, ERRORS_PANIC))
61 panic("EXT2-fs panic from previous error\n");
62 if (test_opt(sb, ERRORS_RO)) {
63 printk("Remounting filesystem read-only\n");
64 sb->s_flags |= MS_RDONLY;
68 void ext2_warning (struct super_block * sb, const char * function,
69 const char * fmt, ...)
74 printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
81 void ext2_update_dynamic_rev(struct super_block *sb)
83 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
85 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
88 ext2_warning(sb, __FUNCTION__,
89 "updating to rev %d because of new feature flag, "
90 "running e2fsck is recommended",
93 es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
94 es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
95 es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
96 /* leave es->s_feature_*compat flags alone */
97 /* es->s_uuid will be set by e2fsck if empty */
100 * The rest of the superblock fields should be zero, and if not it
101 * means they are likely already in use, so leave them alone. We
102 * can leave it up to e2fsck to clean up any inconsistencies there.
106 static void ext2_put_super (struct super_block * sb)
110 struct ext2_sb_info *sbi = EXT2_SB(sb);
112 ext2_xattr_put_super(sb);
113 if (!(sb->s_flags & MS_RDONLY)) {
114 struct ext2_super_block *es = sbi->s_es;
116 es->s_state = cpu_to_le16(sbi->s_mount_state);
117 ext2_sync_super(sb, es);
119 db_count = sbi->s_gdb_count;
120 for (i = 0; i < db_count; i++)
121 if (sbi->s_group_desc[i])
122 brelse (sbi->s_group_desc[i]);
123 kfree(sbi->s_group_desc);
125 percpu_counter_destroy(&sbi->s_freeblocks_counter);
126 percpu_counter_destroy(&sbi->s_freeinodes_counter);
127 percpu_counter_destroy(&sbi->s_dirs_counter);
129 sb->s_fs_info = NULL;
135 static kmem_cache_t * ext2_inode_cachep;
137 static struct inode *ext2_alloc_inode(struct super_block *sb)
139 struct ext2_inode_info *ei;
140 ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
143 #ifdef CONFIG_EXT2_FS_POSIX_ACL
144 ei->i_acl = EXT2_ACL_NOT_CACHED;
145 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
147 ei->vfs_inode.i_version = 1;
148 return &ei->vfs_inode;
151 static void ext2_destroy_inode(struct inode *inode)
153 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
156 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
158 struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
160 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
161 SLAB_CTOR_CONSTRUCTOR) {
162 rwlock_init(&ei->i_meta_lock);
163 #ifdef CONFIG_EXT2_FS_XATTR
164 init_rwsem(&ei->xattr_sem);
166 inode_init_once(&ei->vfs_inode);
170 static int init_inodecache(void)
172 ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
173 sizeof(struct ext2_inode_info),
174 0, SLAB_RECLAIM_ACCOUNT,
176 if (ext2_inode_cachep == NULL)
181 static void destroy_inodecache(void)
183 if (kmem_cache_destroy(ext2_inode_cachep))
184 printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
187 static void ext2_clear_inode(struct inode *inode)
189 #ifdef CONFIG_EXT2_FS_POSIX_ACL
190 struct ext2_inode_info *ei = EXT2_I(inode);
192 if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
193 posix_acl_release(ei->i_acl);
194 ei->i_acl = EXT2_ACL_NOT_CACHED;
196 if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
197 posix_acl_release(ei->i_default_acl);
198 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
201 if (!is_bad_inode(inode))
202 ext2_discard_prealloc(inode);
206 static struct super_operations ext2_sops = {
207 .alloc_inode = ext2_alloc_inode,
208 .destroy_inode = ext2_destroy_inode,
209 .read_inode = ext2_read_inode,
210 .write_inode = ext2_write_inode,
211 .delete_inode = ext2_delete_inode,
212 .put_super = ext2_put_super,
213 .write_super = ext2_write_super,
214 .statfs = ext2_statfs,
215 .remount_fs = ext2_remount,
216 .clear_inode = ext2_clear_inode,
219 /* Yes, most of these are left as NULL!!
220 * A NULL value implies the default, which works with ext2-like file
221 * systems, but can be improved upon.
222 * Currently only get_parent is required.
224 struct dentry *ext2_get_parent(struct dentry *child);
225 static struct export_operations ext2_export_ops = {
226 .get_parent = ext2_get_parent,
229 static unsigned long get_sb_block(void **data)
231 unsigned long sb_block;
232 char *options = (char *) *data;
234 if (!options || strncmp(options, "sb=", 3) != 0)
235 return 1; /* Default location */
237 sb_block = simple_strtoul(options, &options, 0);
238 if (*options && *options != ',') {
239 printk("EXT2-fs: Invalid sb specification: %s\n",
245 *data = (void *) options;
250 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
251 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
252 Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov, Opt_nobh,
253 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, Opt_tagxid,
257 static match_table_t tokens = {
258 {Opt_bsd_df, "bsddf"},
259 {Opt_minix_df, "minixdf"},
260 {Opt_grpid, "grpid"},
261 {Opt_grpid, "bsdgroups"},
262 {Opt_nogrpid, "nogrpid"},
263 {Opt_nogrpid, "sysvgroups"},
264 {Opt_resgid, "resgid=%u"},
265 {Opt_resuid, "resuid=%u"},
267 {Opt_err_cont, "errors=continue"},
268 {Opt_err_panic, "errors=panic"},
269 {Opt_err_ro, "errors=remount-ro"},
270 {Opt_nouid32, "nouid32"},
271 {Opt_nocheck, "check=none"},
272 {Opt_nocheck, "nocheck"},
273 {Opt_check, "check"},
274 {Opt_debug, "debug"},
275 {Opt_oldalloc, "oldalloc"},
276 {Opt_orlov, "orlov"},
278 {Opt_user_xattr, "user_xattr"},
279 {Opt_nouser_xattr, "nouser_xattr"},
281 {Opt_noacl, "noacl"},
282 {Opt_tagxid, "tagxid"},
283 {Opt_ignore, "grpquota"},
284 {Opt_ignore, "noquota"},
285 {Opt_ignore, "quota"},
286 {Opt_ignore, "usrquota"},
290 static int parse_options (char * options,
291 struct ext2_sb_info *sbi)
294 substring_t args[MAX_OPT_ARGS];
295 unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
301 while ((p = strsep (&options, ",")) != NULL) {
306 token = match_token(p, tokens, args);
309 clear_opt (sbi->s_mount_opt, MINIX_DF);
312 set_opt (sbi->s_mount_opt, MINIX_DF);
315 set_opt (sbi->s_mount_opt, GRPID);
318 clear_opt (sbi->s_mount_opt, GRPID);
321 if (match_int(&args[0], &option))
323 sbi->s_resuid = option;
326 if (match_int(&args[0], &option))
328 sbi->s_resgid = option;
331 /* handled by get_sb_block() instead of here */
332 /* *sb_block = match_int(&args[0]); */
335 kind = EXT2_MOUNT_ERRORS_PANIC;
338 kind = EXT2_MOUNT_ERRORS_RO;
341 kind = EXT2_MOUNT_ERRORS_CONT;
344 set_opt (sbi->s_mount_opt, NO_UID32);
346 #ifndef CONFIG_INOXID_NONE
348 set_opt (sbi->s_mount_opt, TAG_XID);
352 #ifdef CONFIG_EXT2_CHECK
353 set_opt (sbi->s_mount_opt, CHECK);
355 printk("EXT2 Check option not supported\n");
359 clear_opt (sbi->s_mount_opt, CHECK);
362 set_opt (sbi->s_mount_opt, DEBUG);
365 set_opt (sbi->s_mount_opt, OLDALLOC);
368 clear_opt (sbi->s_mount_opt, OLDALLOC);
371 set_opt (sbi->s_mount_opt, NOBH);
373 #ifdef CONFIG_EXT2_FS_XATTR
375 set_opt (sbi->s_mount_opt, XATTR_USER);
377 case Opt_nouser_xattr:
378 clear_opt (sbi->s_mount_opt, XATTR_USER);
382 case Opt_nouser_xattr:
383 printk("EXT2 (no)user_xattr options not supported\n");
386 #ifdef CONFIG_EXT2_FS_POSIX_ACL
388 set_opt(sbi->s_mount_opt, POSIX_ACL);
391 clear_opt(sbi->s_mount_opt, POSIX_ACL);
396 printk("EXT2 (no)acl options not supported\n");
405 sbi->s_mount_opt |= kind;
409 static int ext2_setup_super (struct super_block * sb,
410 struct ext2_super_block * es,
414 struct ext2_sb_info *sbi = EXT2_SB(sb);
416 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
417 printk ("EXT2-fs warning: revision level too high, "
418 "forcing read-only mode\n");
423 if (!(sbi->s_mount_state & EXT2_VALID_FS))
424 printk ("EXT2-fs warning: mounting unchecked fs, "
425 "running e2fsck is recommended\n");
426 else if ((sbi->s_mount_state & EXT2_ERROR_FS))
427 printk ("EXT2-fs warning: mounting fs with errors, "
428 "running e2fsck is recommended\n");
429 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
430 le16_to_cpu(es->s_mnt_count) >=
431 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
432 printk ("EXT2-fs warning: maximal mount count reached, "
433 "running e2fsck is recommended\n");
434 else if (le32_to_cpu(es->s_checkinterval) &&
435 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
436 printk ("EXT2-fs warning: checktime reached, "
437 "running e2fsck is recommended\n");
438 if (!le16_to_cpu(es->s_max_mnt_count))
439 es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
440 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
441 ext2_write_super(sb);
442 if (test_opt (sb, DEBUG))
443 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
444 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
445 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
448 EXT2_BLOCKS_PER_GROUP(sb),
449 EXT2_INODES_PER_GROUP(sb),
451 #ifdef CONFIG_EXT2_CHECK
452 if (test_opt (sb, CHECK)) {
453 ext2_check_blocks_bitmap (sb);
454 ext2_check_inodes_bitmap (sb);
460 static int ext2_check_descriptors (struct super_block * sb)
464 struct ext2_sb_info *sbi = EXT2_SB(sb);
465 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
466 struct ext2_group_desc * gdp = NULL;
468 ext2_debug ("Checking group descriptors");
470 for (i = 0; i < sbi->s_groups_count; i++)
472 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
473 gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
474 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
475 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
477 ext2_error (sb, "ext2_check_descriptors",
478 "Block bitmap for group %d"
479 " not in group (block %lu)!",
480 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
483 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
484 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
486 ext2_error (sb, "ext2_check_descriptors",
487 "Inode bitmap for group %d"
488 " not in group (block %lu)!",
489 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
492 if (le32_to_cpu(gdp->bg_inode_table) < block ||
493 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
494 block + EXT2_BLOCKS_PER_GROUP(sb))
496 ext2_error (sb, "ext2_check_descriptors",
497 "Inode table for group %d"
498 " not in group (block %lu)!",
499 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
502 block += EXT2_BLOCKS_PER_GROUP(sb);
508 #define log2(n) ffz(~(n))
511 * Maximal file size. There is a direct, and {,double-,triple-}indirect
512 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
513 * We need to be 1 filesystem block less than the 2^32 sector limit.
515 static loff_t ext2_max_size(int bits)
517 loff_t res = EXT2_NDIR_BLOCKS;
518 res += 1LL << (bits-2);
519 res += 1LL << (2*(bits-2));
520 res += 1LL << (3*(bits-2));
522 if (res > (512LL << 32) - (1 << bits))
523 res = (512LL << 32) - (1 << bits);
527 static unsigned long descriptor_loc(struct super_block *sb,
528 unsigned long logic_sb_block,
531 struct ext2_sb_info *sbi = EXT2_SB(sb);
532 unsigned long bg, first_data_block, first_meta_bg;
535 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
536 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
538 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
540 return (logic_sb_block + nr + 1);
541 bg = sbi->s_desc_per_block * nr;
542 if (ext2_bg_has_super(sb, bg))
544 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
547 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
549 struct buffer_head * bh;
550 struct ext2_sb_info * sbi;
551 struct ext2_super_block * es;
554 unsigned long sb_block = get_sb_block(&data);
555 unsigned long logic_sb_block;
556 unsigned long offset = 0;
557 unsigned long def_mount_opts;
558 int blocksize = BLOCK_SIZE;
563 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
567 memset(sbi, 0, sizeof(*sbi));
570 * See what the current blocksize for the device is, and
571 * use that as the blocksize. Otherwise (or if the blocksize
572 * is smaller than the default) use the default.
573 * This is important for devices that have a hardware
574 * sectorsize that is larger than the default.
576 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
578 printk ("EXT2-fs: unable to set blocksize\n");
583 * If the superblock doesn't start on a hardware sector boundary,
584 * calculate the offset.
586 if (blocksize != BLOCK_SIZE) {
587 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
588 offset = (sb_block*BLOCK_SIZE) % blocksize;
590 logic_sb_block = sb_block;
593 if (!(bh = sb_bread(sb, logic_sb_block))) {
594 printk ("EXT2-fs: unable to read superblock\n");
598 * Note: s_es must be initialized as soon as possible because
599 * some ext2 macro-instructions depend on its value
601 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
603 sb->s_magic = le16_to_cpu(es->s_magic);
604 sb->s_flags |= MS_ONE_SECOND;
606 if (sb->s_magic != EXT2_SUPER_MAGIC)
609 /* Set defaults before we parse the mount options */
610 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
611 if (def_mount_opts & EXT2_DEFM_DEBUG)
612 set_opt(sbi->s_mount_opt, DEBUG);
613 if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
614 set_opt(sbi->s_mount_opt, GRPID);
615 if (def_mount_opts & EXT2_DEFM_UID16)
616 set_opt(sbi->s_mount_opt, NO_UID32);
617 if (def_mount_opts & EXT2_DEFM_XATTR_USER)
618 set_opt(sbi->s_mount_opt, XATTR_USER);
619 if (def_mount_opts & EXT2_DEFM_ACL)
620 set_opt(sbi->s_mount_opt, POSIX_ACL);
622 if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
623 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
624 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
625 set_opt(sbi->s_mount_opt, ERRORS_RO);
627 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
628 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
630 if (!parse_options ((char *) data, sbi))
633 if (EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_TAG_XID)
634 sb->s_flags |= MS_TAGXID;
635 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
636 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
639 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
640 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
641 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
642 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
643 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
644 "running e2fsck is recommended\n");
646 * Check feature flags regardless of the revision level, since we
647 * previously didn't change the revision level when setting the flags,
648 * so there is a chance incompat flags are set on a rev 0 filesystem.
650 features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
652 printk("EXT2-fs: %s: couldn't mount because of "
653 "unsupported optional features (%x).\n",
654 sb->s_id, le32_to_cpu(features));
657 if (!(sb->s_flags & MS_RDONLY) &&
658 (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
659 printk("EXT2-fs: %s: couldn't mount RDWR because of "
660 "unsupported optional features (%x).\n",
661 sb->s_id, le32_to_cpu(features));
665 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
667 /* If the blocksize doesn't match, re-read the thing.. */
668 if (sb->s_blocksize != blocksize) {
671 if (!sb_set_blocksize(sb, blocksize)) {
672 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
676 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
677 offset = (sb_block*BLOCK_SIZE) % blocksize;
678 bh = sb_bread(sb, logic_sb_block);
680 printk("EXT2-fs: Couldn't read superblock on "
684 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
686 if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
687 printk ("EXT2-fs: Magic mismatch, very weird !\n");
692 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
694 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
695 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
696 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
698 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
699 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
700 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
701 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
702 (sbi->s_inode_size > blocksize)) {
703 printk ("EXT2-fs: unsupported inode size: %d\n",
709 sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
710 le32_to_cpu(es->s_log_frag_size);
711 if (sbi->s_frag_size == 0)
713 sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
715 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
716 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
717 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
719 if (EXT2_INODE_SIZE(sb) == 0)
721 sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
722 if (sbi->s_inodes_per_block == 0)
724 sbi->s_itb_per_group = sbi->s_inodes_per_group /
725 sbi->s_inodes_per_block;
726 sbi->s_desc_per_block = sb->s_blocksize /
727 sizeof (struct ext2_group_desc);
729 sbi->s_mount_state = le16_to_cpu(es->s_state);
730 sbi->s_addr_per_block_bits =
731 log2 (EXT2_ADDR_PER_BLOCK(sb));
732 sbi->s_desc_per_block_bits =
733 log2 (EXT2_DESC_PER_BLOCK(sb));
735 if (sb->s_magic != EXT2_SUPER_MAGIC)
738 if (sb->s_blocksize != bh->b_size) {
740 printk ("VFS: Unsupported blocksize on dev "
745 if (sb->s_blocksize != sbi->s_frag_size) {
746 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
747 sbi->s_frag_size, sb->s_blocksize);
751 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
752 printk ("EXT2-fs: #blocks per group too big: %lu\n",
753 sbi->s_blocks_per_group);
756 if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
757 printk ("EXT2-fs: #fragments per group too big: %lu\n",
758 sbi->s_frags_per_group);
761 if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
762 printk ("EXT2-fs: #inodes per group too big: %lu\n",
763 sbi->s_inodes_per_group);
767 if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
769 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
770 le32_to_cpu(es->s_first_data_block) +
771 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
772 EXT2_BLOCKS_PER_GROUP(sb);
773 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
774 EXT2_DESC_PER_BLOCK(sb);
775 sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
776 if (sbi->s_group_desc == NULL) {
777 printk ("EXT2-fs: not enough memory\n");
780 percpu_counter_init(&sbi->s_freeblocks_counter);
781 percpu_counter_init(&sbi->s_freeinodes_counter);
782 percpu_counter_init(&sbi->s_dirs_counter);
783 bgl_lock_init(&sbi->s_blockgroup_lock);
784 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
787 printk ("EXT2-fs: not enough memory\n");
788 goto failed_mount_group_desc;
790 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
791 for (i = 0; i < db_count; i++) {
792 block = descriptor_loc(sb, logic_sb_block, i);
793 sbi->s_group_desc[i] = sb_bread(sb, block);
794 if (!sbi->s_group_desc[i]) {
795 for (j = 0; j < i; j++)
796 brelse (sbi->s_group_desc[j]);
797 printk ("EXT2-fs: unable to read group descriptors\n");
798 goto failed_mount_group_desc;
801 if (!ext2_check_descriptors (sb)) {
802 printk ("EXT2-fs: group descriptors corrupted!\n");
806 sbi->s_gdb_count = db_count;
807 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
808 spin_lock_init(&sbi->s_next_gen_lock);
810 * set up enough so that it can read an inode
812 sb->s_op = &ext2_sops;
813 sb->s_export_op = &ext2_export_ops;
814 sb->s_xattr = ext2_xattr_handlers;
815 root = iget(sb, EXT2_ROOT_INO);
816 sb->s_root = d_alloc_root(root);
819 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
822 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
825 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
828 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
829 ext2_warning(sb, __FUNCTION__,
830 "mounting ext3 filesystem as ext2\n");
831 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
832 percpu_counter_mod(&sbi->s_freeblocks_counter,
833 ext2_count_free_blocks(sb));
834 percpu_counter_mod(&sbi->s_freeinodes_counter,
835 ext2_count_free_inodes(sb));
836 percpu_counter_mod(&sbi->s_dirs_counter,
837 ext2_count_dirs(sb));
842 printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
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);
855 sb->s_fs_info = NULL;
860 static void ext2_commit_super (struct super_block * sb,
861 struct ext2_super_block * es)
863 es->s_wtime = cpu_to_le32(get_seconds());
864 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
868 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
870 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
871 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
872 es->s_wtime = cpu_to_le32(get_seconds());
873 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
874 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
879 * In the second extended file system, it is not necessary to
880 * write the super block since we use a mapping of the
881 * disk super block in a buffer.
883 * However, this function is still used to set the fs valid
884 * flags to 0. We need to set this flag to 0 since the fs
885 * may have been checked while mounted and e2fsck may have
886 * set s_state to EXT2_VALID_FS after some corrections.
889 void ext2_write_super (struct super_block * sb)
891 struct ext2_super_block * es;
893 if (!(sb->s_flags & MS_RDONLY)) {
894 es = EXT2_SB(sb)->s_es;
896 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
897 ext2_debug ("setting valid to 0\n");
898 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
900 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
901 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
902 es->s_mtime = cpu_to_le32(get_seconds());
903 ext2_sync_super(sb, es);
905 ext2_commit_super (sb, es);
911 static int ext2_remount (struct super_block * sb, int * flags, char * data)
913 struct ext2_sb_info * sbi = EXT2_SB(sb);
914 struct ext2_super_block * es;
917 * Allow the "check" option to be passed as a remount option.
919 if (!parse_options (data, sbi))
922 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
923 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
926 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
928 if (*flags & MS_RDONLY) {
929 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
930 !(sbi->s_mount_state & EXT2_VALID_FS))
933 * OK, we are remounting a valid rw partition rdonly, so set
934 * the rdonly flag and then mark the partition as valid again.
936 es->s_state = cpu_to_le16(sbi->s_mount_state);
937 es->s_mtime = cpu_to_le32(get_seconds());
939 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
940 ~EXT2_FEATURE_RO_COMPAT_SUPP);
942 printk("EXT2-fs: %s: couldn't remount RDWR because of "
943 "unsupported optional features (%x).\n",
944 sb->s_id, le32_to_cpu(ret));
948 * Mounting a RDONLY partition read-write, so reread and
949 * store the current valid flag. (It may have been changed
950 * by e2fsck since we originally mounted the partition.)
952 sbi->s_mount_state = le16_to_cpu(es->s_state);
953 if (!ext2_setup_super (sb, es, 0))
954 sb->s_flags &= ~MS_RDONLY;
956 ext2_sync_super(sb, es);
960 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
962 struct ext2_sb_info *sbi = EXT2_SB(sb);
963 unsigned long overhead;
966 if (test_opt (sb, MINIX_DF))
970 * Compute the overhead (FS structures)
974 * All of the blocks before first_data_block are
977 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
980 * Add the overhead attributed to the superblock and
981 * block group descriptors. If the sparse superblocks
982 * feature is turned on, then not all groups have this.
984 for (i = 0; i < sbi->s_groups_count; i++)
985 overhead += ext2_bg_has_super(sb, i) +
986 ext2_bg_num_gdb(sb, i);
989 * Every block group has an inode bitmap, a block
990 * bitmap, and an inode table.
992 overhead += (sbi->s_groups_count *
993 (2 + sbi->s_itb_per_group));
996 buf->f_type = EXT2_SUPER_MAGIC;
997 buf->f_bsize = sb->s_blocksize;
998 buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
999 buf->f_bfree = ext2_count_free_blocks(sb);
1000 buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
1001 if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
1003 buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
1004 buf->f_ffree = ext2_count_free_inodes (sb);
1005 buf->f_namelen = EXT2_NAME_LEN;
1009 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
1010 int flags, const char *dev_name, void *data)
1012 return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1015 static struct file_system_type ext2_fs_type = {
1016 .owner = THIS_MODULE,
1018 .get_sb = ext2_get_sb,
1019 .kill_sb = kill_block_super,
1020 .fs_flags = FS_REQUIRES_DEV,
1023 static int __init init_ext2_fs(void)
1025 int err = init_ext2_xattr();
1028 err = init_inodecache();
1031 err = register_filesystem(&ext2_fs_type);
1036 destroy_inodecache();
1042 static void __exit exit_ext2_fs(void)
1044 unregister_filesystem(&ext2_fs_type);
1045 destroy_inodecache();
1049 module_init(init_ext2_fs)
1050 module_exit(exit_ext2_fs)