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, Opt_tagxid,
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_tagxid, "tagxid"},
303 {Opt_ignore, "grpquota"},
304 {Opt_ignore, "noquota"},
305 {Opt_ignore, "quota"},
306 {Opt_ignore, "usrquota"},
310 static int parse_options (char * options,
311 struct ext2_sb_info *sbi)
314 substring_t args[MAX_OPT_ARGS];
315 unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
321 while ((p = strsep (&options, ",")) != NULL) {
326 token = match_token(p, tokens, args);
329 clear_opt (sbi->s_mount_opt, MINIX_DF);
332 set_opt (sbi->s_mount_opt, MINIX_DF);
335 set_opt (sbi->s_mount_opt, GRPID);
338 clear_opt (sbi->s_mount_opt, GRPID);
341 if (match_int(&args[0], &option))
343 sbi->s_resuid = option;
346 if (match_int(&args[0], &option))
348 sbi->s_resgid = option;
351 /* handled by get_sb_block() instead of here */
352 /* *sb_block = match_int(&args[0]); */
355 kind = EXT2_MOUNT_ERRORS_PANIC;
358 kind = EXT2_MOUNT_ERRORS_RO;
361 kind = EXT2_MOUNT_ERRORS_CONT;
364 set_opt (sbi->s_mount_opt, NO_UID32);
366 #ifndef CONFIG_INOXID_NONE
368 set_opt (sbi->s_mount_opt, TAG_XID);
372 #ifdef CONFIG_EXT2_CHECK
373 set_opt (sbi->s_mount_opt, CHECK);
375 printk("EXT2 Check option not supported\n");
379 clear_opt (sbi->s_mount_opt, CHECK);
382 set_opt (sbi->s_mount_opt, DEBUG);
385 set_opt (sbi->s_mount_opt, OLDALLOC);
388 clear_opt (sbi->s_mount_opt, OLDALLOC);
391 set_opt (sbi->s_mount_opt, NOBH);
393 #ifdef CONFIG_EXT2_FS_XATTR
395 set_opt (sbi->s_mount_opt, XATTR_USER);
397 case Opt_nouser_xattr:
398 clear_opt (sbi->s_mount_opt, XATTR_USER);
402 case Opt_nouser_xattr:
403 printk("EXT2 (no)user_xattr options not supported\n");
406 #ifdef CONFIG_EXT2_FS_POSIX_ACL
408 set_opt(sbi->s_mount_opt, POSIX_ACL);
411 clear_opt(sbi->s_mount_opt, POSIX_ACL);
416 printk("EXT2 (no)acl options not supported\n");
425 sbi->s_mount_opt |= kind;
429 static int ext2_setup_super (struct super_block * sb,
430 struct ext2_super_block * es,
434 struct ext2_sb_info *sbi = EXT2_SB(sb);
436 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
437 printk ("EXT2-fs warning: revision level too high, "
438 "forcing read-only mode\n");
443 if (!(sbi->s_mount_state & EXT2_VALID_FS))
444 printk ("EXT2-fs warning: mounting unchecked fs, "
445 "running e2fsck is recommended\n");
446 else if ((sbi->s_mount_state & EXT2_ERROR_FS))
447 printk ("EXT2-fs warning: mounting fs with errors, "
448 "running e2fsck is recommended\n");
449 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
450 le16_to_cpu(es->s_mnt_count) >=
451 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
452 printk ("EXT2-fs warning: maximal mount count reached, "
453 "running e2fsck is recommended\n");
454 else if (le32_to_cpu(es->s_checkinterval) &&
455 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
456 printk ("EXT2-fs warning: checktime reached, "
457 "running e2fsck is recommended\n");
458 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
459 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
460 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
461 ext2_write_super(sb);
462 if (test_opt (sb, DEBUG))
463 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
464 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
465 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
468 EXT2_BLOCKS_PER_GROUP(sb),
469 EXT2_INODES_PER_GROUP(sb),
471 #ifdef CONFIG_EXT2_CHECK
472 if (test_opt (sb, CHECK)) {
473 ext2_check_blocks_bitmap (sb);
474 ext2_check_inodes_bitmap (sb);
480 static int ext2_check_descriptors (struct super_block * sb)
484 struct ext2_sb_info *sbi = EXT2_SB(sb);
485 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
486 struct ext2_group_desc * gdp = NULL;
488 ext2_debug ("Checking group descriptors");
490 for (i = 0; i < sbi->s_groups_count; i++)
492 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
493 gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
494 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
495 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
497 ext2_error (sb, "ext2_check_descriptors",
498 "Block bitmap for group %d"
499 " not in group (block %lu)!",
500 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
503 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
504 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
506 ext2_error (sb, "ext2_check_descriptors",
507 "Inode bitmap for group %d"
508 " not in group (block %lu)!",
509 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
512 if (le32_to_cpu(gdp->bg_inode_table) < block ||
513 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
514 block + EXT2_BLOCKS_PER_GROUP(sb))
516 ext2_error (sb, "ext2_check_descriptors",
517 "Inode table for group %d"
518 " not in group (block %lu)!",
519 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
522 block += EXT2_BLOCKS_PER_GROUP(sb);
528 #define log2(n) ffz(~(n))
531 * Maximal file size. There is a direct, and {,double-,triple-}indirect
532 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
533 * We need to be 1 filesystem block less than the 2^32 sector limit.
535 static loff_t ext2_max_size(int bits)
537 loff_t res = EXT2_NDIR_BLOCKS;
538 res += 1LL << (bits-2);
539 res += 1LL << (2*(bits-2));
540 res += 1LL << (3*(bits-2));
542 if (res > (512LL << 32) - (1 << bits))
543 res = (512LL << 32) - (1 << bits);
547 static unsigned long descriptor_loc(struct super_block *sb,
548 unsigned long logic_sb_block,
551 struct ext2_sb_info *sbi = EXT2_SB(sb);
552 unsigned long bg, first_data_block, first_meta_bg;
555 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
556 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
558 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
560 return (logic_sb_block + nr + 1);
561 bg = sbi->s_desc_per_block * nr;
562 if (ext2_bg_has_super(sb, bg))
564 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
567 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
569 struct buffer_head * bh;
570 struct ext2_sb_info * sbi;
571 struct ext2_super_block * es;
574 unsigned long sb_block = get_sb_block(&data);
575 unsigned long logic_sb_block;
576 unsigned long offset = 0;
577 unsigned long def_mount_opts;
578 int blocksize = BLOCK_SIZE;
582 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
586 memset(sbi, 0, sizeof(*sbi));
589 * See what the current blocksize for the device is, and
590 * use that as the blocksize. Otherwise (or if the blocksize
591 * is smaller than the default) use the default.
592 * This is important for devices that have a hardware
593 * sectorsize that is larger than the default.
595 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
597 printk ("EXT2-fs: unable to set blocksize\n");
602 * If the superblock doesn't start on a hardware sector boundary,
603 * calculate the offset.
605 if (blocksize != BLOCK_SIZE) {
606 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
607 offset = (sb_block*BLOCK_SIZE) % blocksize;
609 logic_sb_block = sb_block;
612 if (!(bh = sb_bread(sb, logic_sb_block))) {
613 printk ("EXT2-fs: unable to read superblock\n");
617 * Note: s_es must be initialized as soon as possible because
618 * some ext2 macro-instructions depend on its value
620 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
622 sb->s_magic = le16_to_cpu(es->s_magic);
623 sb->s_flags |= MS_ONE_SECOND;
624 if (sb->s_magic != EXT2_SUPER_MAGIC) {
626 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
631 /* Set defaults before we parse the mount options */
632 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
633 if (def_mount_opts & EXT2_DEFM_DEBUG)
634 set_opt(sbi->s_mount_opt, DEBUG);
635 if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
636 set_opt(sbi->s_mount_opt, GRPID);
637 if (def_mount_opts & EXT2_DEFM_UID16)
638 set_opt(sbi->s_mount_opt, NO_UID32);
639 if (def_mount_opts & EXT2_DEFM_XATTR_USER)
640 set_opt(sbi->s_mount_opt, XATTR_USER);
641 if (def_mount_opts & EXT2_DEFM_ACL)
642 set_opt(sbi->s_mount_opt, POSIX_ACL);
644 if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
645 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
646 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
647 set_opt(sbi->s_mount_opt, ERRORS_RO);
649 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
650 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
652 if (!parse_options ((char *) data, sbi))
655 if (EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_TAG_XID)
656 sb->s_flags |= MS_TAGXID;
657 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
658 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
661 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
662 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
663 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
664 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
665 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
666 "running e2fsck is recommended\n");
668 * Check feature flags regardless of the revision level, since we
669 * previously didn't change the revision level when setting the flags,
670 * so there is a chance incompat flags are set on a rev 0 filesystem.
672 if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
673 printk("EXT2-fs: %s: couldn't mount because of "
674 "unsupported optional features (%x).\n",
678 if (!(sb->s_flags & MS_RDONLY) &&
679 (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
680 printk("EXT2-fs: %s: couldn't mount RDWR because of "
681 "unsupported optional features (%x).\n",
685 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
686 /* If the blocksize doesn't match, re-read the thing.. */
687 if (sb->s_blocksize != blocksize) {
690 if (!sb_set_blocksize(sb, blocksize)) {
691 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
695 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
696 offset = (sb_block*BLOCK_SIZE) % blocksize;
697 bh = sb_bread(sb, logic_sb_block);
699 printk("EXT2-fs: Couldn't read superblock on "
703 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
705 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
706 printk ("EXT2-fs: Magic mismatch, very weird !\n");
711 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
713 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
714 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
715 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
717 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
718 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
719 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
720 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
721 (sbi->s_inode_size > blocksize)) {
722 printk ("EXT2-fs: unsupported inode size: %d\n",
727 sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
728 le32_to_cpu(es->s_log_frag_size);
729 if (sbi->s_frag_size)
730 sbi->s_frags_per_block = sb->s_blocksize /
734 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
735 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
736 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
737 sbi->s_inodes_per_block = sb->s_blocksize /
739 sbi->s_itb_per_group = sbi->s_inodes_per_group /
740 sbi->s_inodes_per_block;
741 sbi->s_desc_per_block = sb->s_blocksize /
742 sizeof (struct ext2_group_desc);
744 sbi->s_mount_state = le16_to_cpu(es->s_state);
745 sbi->s_addr_per_block_bits =
746 log2 (EXT2_ADDR_PER_BLOCK(sb));
747 sbi->s_desc_per_block_bits =
748 log2 (EXT2_DESC_PER_BLOCK(sb));
749 if (sb->s_magic != EXT2_SUPER_MAGIC) {
751 printk ("VFS: Can't find an ext2 filesystem on dev "
756 if (sb->s_blocksize != bh->b_size) {
758 printk ("VFS: Unsupported blocksize on dev "
763 if (sb->s_blocksize != sbi->s_frag_size) {
764 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
765 sbi->s_frag_size, sb->s_blocksize);
769 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
770 printk ("EXT2-fs: #blocks per group too big: %lu\n",
771 sbi->s_blocks_per_group);
774 if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
775 printk ("EXT2-fs: #fragments per group too big: %lu\n",
776 sbi->s_frags_per_group);
779 if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
780 printk ("EXT2-fs: #inodes per group too big: %lu\n",
781 sbi->s_inodes_per_group);
785 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
786 le32_to_cpu(es->s_first_data_block) +
787 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
788 EXT2_BLOCKS_PER_GROUP(sb);
789 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
790 EXT2_DESC_PER_BLOCK(sb);
791 sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
792 if (sbi->s_group_desc == NULL) {
793 printk ("EXT2-fs: not enough memory\n");
796 percpu_counter_init(&sbi->s_freeblocks_counter);
797 percpu_counter_init(&sbi->s_freeinodes_counter);
798 percpu_counter_init(&sbi->s_dirs_counter);
799 bgl_lock_init(&sbi->s_blockgroup_lock);
800 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
803 printk ("EXT2-fs: not enough memory\n");
804 goto failed_mount_group_desc;
806 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
807 for (i = 0; i < db_count; i++) {
808 block = descriptor_loc(sb, logic_sb_block, i);
809 sbi->s_group_desc[i] = sb_bread(sb, block);
810 if (!sbi->s_group_desc[i]) {
811 for (j = 0; j < i; j++)
812 brelse (sbi->s_group_desc[j]);
813 printk ("EXT2-fs: unable to read group descriptors\n");
814 goto failed_mount_group_desc;
817 if (!ext2_check_descriptors (sb)) {
818 printk ("EXT2-fs: group descriptors corrupted!\n");
822 sbi->s_gdb_count = db_count;
823 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
824 spin_lock_init(&sbi->s_next_gen_lock);
826 * set up enough so that it can read an inode
828 sb->s_op = &ext2_sops;
829 sb->s_export_op = &ext2_export_ops;
830 root = iget(sb, EXT2_ROOT_INO);
831 sb->s_root = d_alloc_root(root);
834 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
837 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
840 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
843 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
844 ext2_warning(sb, __FUNCTION__,
845 "mounting ext3 filesystem as ext2\n");
846 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
847 percpu_counter_mod(&sbi->s_freeblocks_counter,
848 ext2_count_free_blocks(sb));
849 percpu_counter_mod(&sbi->s_freeinodes_counter,
850 ext2_count_free_inodes(sb));
851 percpu_counter_mod(&sbi->s_dirs_counter,
852 ext2_count_dirs(sb));
855 for (i = 0; i < db_count; i++)
856 brelse(sbi->s_group_desc[i]);
857 failed_mount_group_desc:
858 kfree(sbi->s_group_desc);
864 sb->s_fs_info = NULL;
869 static void ext2_commit_super (struct super_block * sb,
870 struct ext2_super_block * es)
872 es->s_wtime = cpu_to_le32(get_seconds());
873 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
877 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
879 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
880 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
881 es->s_wtime = cpu_to_le32(get_seconds());
882 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
883 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
888 * In the second extended file system, it is not necessary to
889 * write the super block since we use a mapping of the
890 * disk super block in a buffer.
892 * However, this function is still used to set the fs valid
893 * flags to 0. We need to set this flag to 0 since the fs
894 * may have been checked while mounted and e2fsck may have
895 * set s_state to EXT2_VALID_FS after some corrections.
898 void ext2_write_super (struct super_block * sb)
900 struct ext2_super_block * es;
902 if (!(sb->s_flags & MS_RDONLY)) {
903 es = EXT2_SB(sb)->s_es;
905 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
906 ext2_debug ("setting valid to 0\n");
907 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
909 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
910 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
911 es->s_mtime = cpu_to_le32(get_seconds());
912 ext2_sync_super(sb, es);
914 ext2_commit_super (sb, es);
920 static int ext2_remount (struct super_block * sb, int * flags, char * data)
922 struct ext2_sb_info * sbi = EXT2_SB(sb);
923 struct ext2_super_block * es;
926 * Allow the "check" option to be passed as a remount option.
928 if (!parse_options (data, sbi))
931 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
932 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
935 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
937 if (*flags & MS_RDONLY) {
938 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
939 !(sbi->s_mount_state & EXT2_VALID_FS))
942 * OK, we are remounting a valid rw partition rdonly, so set
943 * the rdonly flag and then mark the partition as valid again.
945 es->s_state = cpu_to_le16(sbi->s_mount_state);
946 es->s_mtime = cpu_to_le32(get_seconds());
949 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
950 ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
951 printk("EXT2-fs: %s: couldn't remount RDWR because of "
952 "unsupported optional features (%x).\n",
957 * Mounting a RDONLY partition read-write, so reread and
958 * store the current valid flag. (It may have been changed
959 * by e2fsck since we originally mounted the partition.)
961 sbi->s_mount_state = le16_to_cpu(es->s_state);
962 if (!ext2_setup_super (sb, es, 0))
963 sb->s_flags &= ~MS_RDONLY;
965 ext2_sync_super(sb, es);
969 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
971 struct ext2_sb_info *sbi = EXT2_SB(sb);
972 unsigned long overhead;
975 if (test_opt (sb, MINIX_DF))
979 * Compute the overhead (FS structures)
983 * All of the blocks before first_data_block are
986 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
989 * Add the overhead attributed to the superblock and
990 * block group descriptors. If the sparse superblocks
991 * feature is turned on, then not all groups have this.
993 for (i = 0; i < sbi->s_groups_count; i++)
994 overhead += ext2_bg_has_super(sb, i) +
995 ext2_bg_num_gdb(sb, i);
998 * Every block group has an inode bitmap, a block
999 * bitmap, and an inode table.
1001 overhead += (sbi->s_groups_count *
1002 (2 + sbi->s_itb_per_group));
1005 buf->f_type = EXT2_SUPER_MAGIC;
1006 buf->f_bsize = sb->s_blocksize;
1007 buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
1008 buf->f_bfree = ext2_count_free_blocks(sb);
1009 buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
1010 if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
1012 buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
1013 buf->f_ffree = ext2_count_free_inodes (sb);
1014 buf->f_namelen = EXT2_NAME_LEN;
1018 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
1019 int flags, const char *dev_name, void *data)
1021 return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1024 static struct file_system_type ext2_fs_type = {
1025 .owner = THIS_MODULE,
1027 .get_sb = ext2_get_sb,
1028 .kill_sb = kill_block_super,
1029 .fs_flags = FS_REQUIRES_DEV,
1032 static int __init init_ext2_fs(void)
1034 int err = init_ext2_xattr();
1037 err = init_inodecache();
1040 err = register_filesystem(&ext2_fs_type);
1045 destroy_inodecache();
1051 static void __exit exit_ext2_fs(void)
1053 unregister_filesystem(&ext2_fs_type);
1054 destroy_inodecache();
1058 module_init(init_ext2_fs)
1059 module_exit(exit_ext2_fs)