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>
36 static void ext2_sync_super(struct super_block *sb,
37 struct ext2_super_block *es);
38 static int ext2_remount (struct super_block * sb, int * flags, char * data);
39 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf);
41 void ext2_error (struct super_block * sb, const char * function,
42 const char * fmt, ...)
45 struct ext2_sb_info *sbi = EXT2_SB(sb);
46 struct ext2_super_block *es = sbi->s_es;
48 if (!(sb->s_flags & MS_RDONLY)) {
49 sbi->s_mount_state |= EXT2_ERROR_FS;
51 cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
52 ext2_sync_super(sb, es);
56 printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
61 if (test_opt(sb, ERRORS_PANIC))
62 panic("EXT2-fs panic from previous error\n");
63 if (test_opt(sb, ERRORS_RO)) {
64 printk("Remounting filesystem read-only\n");
65 sb->s_flags |= MS_RDONLY;
69 void ext2_warning (struct super_block * sb, const char * function,
70 const char * fmt, ...)
75 printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
82 void ext2_update_dynamic_rev(struct super_block *sb)
84 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
86 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
89 ext2_warning(sb, __FUNCTION__,
90 "updating to rev %d because of new feature flag, "
91 "running e2fsck is recommended",
94 es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
95 es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
96 es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
97 /* leave es->s_feature_*compat flags alone */
98 /* es->s_uuid will be set by e2fsck if empty */
101 * The rest of the superblock fields should be zero, and if not it
102 * means they are likely already in use, so leave them alone. We
103 * can leave it up to e2fsck to clean up any inconsistencies there.
107 static void ext2_put_super (struct super_block * sb)
111 struct ext2_sb_info *sbi = EXT2_SB(sb);
113 ext2_xattr_put_super(sb);
114 if (!(sb->s_flags & MS_RDONLY)) {
115 struct ext2_super_block *es = sbi->s_es;
117 es->s_state = cpu_to_le16(sbi->s_mount_state);
118 ext2_sync_super(sb, es);
120 db_count = sbi->s_gdb_count;
121 for (i = 0; i < db_count; i++)
122 if (sbi->s_group_desc[i])
123 brelse (sbi->s_group_desc[i]);
124 kfree(sbi->s_group_desc);
126 percpu_counter_destroy(&sbi->s_freeblocks_counter);
127 percpu_counter_destroy(&sbi->s_freeinodes_counter);
128 percpu_counter_destroy(&sbi->s_dirs_counter);
130 sb->s_fs_info = NULL;
136 static kmem_cache_t * ext2_inode_cachep;
138 static struct inode *ext2_alloc_inode(struct super_block *sb)
140 struct ext2_inode_info *ei;
141 ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
144 #ifdef CONFIG_EXT2_FS_POSIX_ACL
145 ei->i_acl = EXT2_ACL_NOT_CACHED;
146 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
148 ei->vfs_inode.i_version = 1;
149 return &ei->vfs_inode;
152 static void ext2_destroy_inode(struct inode *inode)
154 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
157 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
159 struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
161 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
162 SLAB_CTOR_CONSTRUCTOR) {
163 rwlock_init(&ei->i_meta_lock);
164 #ifdef CONFIG_EXT2_FS_XATTR
165 init_rwsem(&ei->xattr_sem);
167 inode_init_once(&ei->vfs_inode);
171 static int init_inodecache(void)
173 ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
174 sizeof(struct ext2_inode_info),
175 0, SLAB_RECLAIM_ACCOUNT,
177 if (ext2_inode_cachep == NULL)
182 static void destroy_inodecache(void)
184 if (kmem_cache_destroy(ext2_inode_cachep))
185 printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
188 static void ext2_clear_inode(struct inode *inode)
190 #ifdef CONFIG_EXT2_FS_POSIX_ACL
191 struct ext2_inode_info *ei = EXT2_I(inode);
193 if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
194 posix_acl_release(ei->i_acl);
195 ei->i_acl = EXT2_ACL_NOT_CACHED;
197 if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
198 posix_acl_release(ei->i_default_acl);
199 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
205 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
206 static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
209 static struct super_operations ext2_sops = {
210 .alloc_inode = ext2_alloc_inode,
211 .destroy_inode = ext2_destroy_inode,
212 .read_inode = ext2_read_inode,
213 .write_inode = ext2_write_inode,
214 .put_inode = ext2_put_inode,
215 .delete_inode = ext2_delete_inode,
216 .put_super = ext2_put_super,
217 .write_super = ext2_write_super,
218 .statfs = ext2_statfs,
219 .remount_fs = ext2_remount,
220 .clear_inode = ext2_clear_inode,
222 .quota_read = ext2_quota_read,
223 .quota_write = ext2_quota_write,
227 /* Yes, most of these are left as NULL!!
228 * A NULL value implies the default, which works with ext2-like file
229 * systems, but can be improved upon.
230 * Currently only get_parent is required.
232 struct dentry *ext2_get_parent(struct dentry *child);
233 static struct export_operations ext2_export_ops = {
234 .get_parent = ext2_get_parent,
237 static unsigned long get_sb_block(void **data)
239 unsigned long sb_block;
240 char *options = (char *) *data;
242 if (!options || strncmp(options, "sb=", 3) != 0)
243 return 1; /* Default location */
245 sb_block = simple_strtoul(options, &options, 0);
246 if (*options && *options != ',') {
247 printk("EXT2-fs: Invalid sb specification: %s\n",
253 *data = (void *) options;
258 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
259 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
260 Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov, Opt_nobh,
261 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, Opt_xip,
265 static match_table_t tokens = {
266 {Opt_bsd_df, "bsddf"},
267 {Opt_minix_df, "minixdf"},
268 {Opt_grpid, "grpid"},
269 {Opt_grpid, "bsdgroups"},
270 {Opt_nogrpid, "nogrpid"},
271 {Opt_nogrpid, "sysvgroups"},
272 {Opt_resgid, "resgid=%u"},
273 {Opt_resuid, "resuid=%u"},
275 {Opt_err_cont, "errors=continue"},
276 {Opt_err_panic, "errors=panic"},
277 {Opt_err_ro, "errors=remount-ro"},
278 {Opt_nouid32, "nouid32"},
279 {Opt_nocheck, "check=none"},
280 {Opt_nocheck, "nocheck"},
281 {Opt_check, "check"},
282 {Opt_debug, "debug"},
283 {Opt_oldalloc, "oldalloc"},
284 {Opt_orlov, "orlov"},
286 {Opt_user_xattr, "user_xattr"},
287 {Opt_nouser_xattr, "nouser_xattr"},
289 {Opt_noacl, "noacl"},
291 {Opt_ignore, "grpquota"},
292 {Opt_ignore, "noquota"},
293 {Opt_ignore, "quota"},
294 {Opt_ignore, "usrquota"},
298 static int parse_options (char * options,
299 struct ext2_sb_info *sbi)
302 substring_t args[MAX_OPT_ARGS];
303 unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
309 while ((p = strsep (&options, ",")) != NULL) {
314 token = match_token(p, tokens, args);
317 clear_opt (sbi->s_mount_opt, MINIX_DF);
320 set_opt (sbi->s_mount_opt, MINIX_DF);
323 set_opt (sbi->s_mount_opt, GRPID);
326 clear_opt (sbi->s_mount_opt, GRPID);
329 if (match_int(&args[0], &option))
331 sbi->s_resuid = option;
334 if (match_int(&args[0], &option))
336 sbi->s_resgid = option;
339 /* handled by get_sb_block() instead of here */
340 /* *sb_block = match_int(&args[0]); */
343 kind = EXT2_MOUNT_ERRORS_PANIC;
346 kind = EXT2_MOUNT_ERRORS_RO;
349 kind = EXT2_MOUNT_ERRORS_CONT;
352 set_opt (sbi->s_mount_opt, NO_UID32);
355 #ifdef CONFIG_EXT2_CHECK
356 set_opt (sbi->s_mount_opt, CHECK);
358 printk("EXT2 Check option not supported\n");
362 clear_opt (sbi->s_mount_opt, CHECK);
365 set_opt (sbi->s_mount_opt, DEBUG);
368 set_opt (sbi->s_mount_opt, OLDALLOC);
371 clear_opt (sbi->s_mount_opt, OLDALLOC);
374 set_opt (sbi->s_mount_opt, NOBH);
376 #ifdef CONFIG_EXT2_FS_XATTR
378 set_opt (sbi->s_mount_opt, XATTR_USER);
380 case Opt_nouser_xattr:
381 clear_opt (sbi->s_mount_opt, XATTR_USER);
385 case Opt_nouser_xattr:
386 printk("EXT2 (no)user_xattr options not supported\n");
389 #ifdef CONFIG_EXT2_FS_POSIX_ACL
391 set_opt(sbi->s_mount_opt, POSIX_ACL);
394 clear_opt(sbi->s_mount_opt, POSIX_ACL);
399 printk("EXT2 (no)acl options not supported\n");
403 #ifdef CONFIG_EXT2_FS_XIP
404 set_opt (sbi->s_mount_opt, XIP);
406 printk("EXT2 xip option not supported\n");
415 sbi->s_mount_opt |= kind;
419 static int ext2_setup_super (struct super_block * sb,
420 struct ext2_super_block * es,
424 struct ext2_sb_info *sbi = EXT2_SB(sb);
426 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
427 printk ("EXT2-fs warning: revision level too high, "
428 "forcing read-only mode\n");
433 if (!(sbi->s_mount_state & EXT2_VALID_FS))
434 printk ("EXT2-fs warning: mounting unchecked fs, "
435 "running e2fsck is recommended\n");
436 else if ((sbi->s_mount_state & EXT2_ERROR_FS))
437 printk ("EXT2-fs warning: mounting fs with errors, "
438 "running e2fsck is recommended\n");
439 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
440 le16_to_cpu(es->s_mnt_count) >=
441 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
442 printk ("EXT2-fs warning: maximal mount count reached, "
443 "running e2fsck is recommended\n");
444 else if (le32_to_cpu(es->s_checkinterval) &&
445 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
446 printk ("EXT2-fs warning: checktime reached, "
447 "running e2fsck is recommended\n");
448 if (!le16_to_cpu(es->s_max_mnt_count))
449 es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
450 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
451 ext2_write_super(sb);
452 if (test_opt (sb, DEBUG))
453 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
454 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
455 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
458 EXT2_BLOCKS_PER_GROUP(sb),
459 EXT2_INODES_PER_GROUP(sb),
461 #ifdef CONFIG_EXT2_CHECK
462 if (test_opt (sb, CHECK)) {
463 ext2_check_blocks_bitmap (sb);
464 ext2_check_inodes_bitmap (sb);
470 static int ext2_check_descriptors (struct super_block * sb)
474 struct ext2_sb_info *sbi = EXT2_SB(sb);
475 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
476 struct ext2_group_desc * gdp = NULL;
478 ext2_debug ("Checking group descriptors");
480 for (i = 0; i < sbi->s_groups_count; i++)
482 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
483 gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
484 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
485 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
487 ext2_error (sb, "ext2_check_descriptors",
488 "Block bitmap for group %d"
489 " not in group (block %lu)!",
490 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
493 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
494 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
496 ext2_error (sb, "ext2_check_descriptors",
497 "Inode bitmap for group %d"
498 " not in group (block %lu)!",
499 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
502 if (le32_to_cpu(gdp->bg_inode_table) < block ||
503 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
504 block + EXT2_BLOCKS_PER_GROUP(sb))
506 ext2_error (sb, "ext2_check_descriptors",
507 "Inode table for group %d"
508 " not in group (block %lu)!",
509 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
512 block += EXT2_BLOCKS_PER_GROUP(sb);
518 #define log2(n) ffz(~(n))
521 * Maximal file size. There is a direct, and {,double-,triple-}indirect
522 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
523 * We need to be 1 filesystem block less than the 2^32 sector limit.
525 static loff_t ext2_max_size(int bits)
527 loff_t res = EXT2_NDIR_BLOCKS;
528 /* This constant is calculated to be the largest file size for a
529 * dense, 4k-blocksize file such that the total number of
530 * sectors in the file, including data and all indirect blocks,
531 * does not exceed 2^32. */
532 const loff_t upper_limit = 0x1ff7fffd000LL;
534 res += 1LL << (bits-2);
535 res += 1LL << (2*(bits-2));
536 res += 1LL << (3*(bits-2));
538 if (res > upper_limit)
543 static unsigned long descriptor_loc(struct super_block *sb,
544 unsigned long logic_sb_block,
547 struct ext2_sb_info *sbi = EXT2_SB(sb);
548 unsigned long bg, first_data_block, first_meta_bg;
551 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
552 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
554 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
556 return (logic_sb_block + nr + 1);
557 bg = sbi->s_desc_per_block * nr;
558 if (ext2_bg_has_super(sb, bg))
560 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
563 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
565 struct buffer_head * bh;
566 struct ext2_sb_info * sbi;
567 struct ext2_super_block * es;
570 unsigned long sb_block = get_sb_block(&data);
571 unsigned long logic_sb_block;
572 unsigned long offset = 0;
573 unsigned long def_mount_opts;
574 int blocksize = BLOCK_SIZE;
579 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
583 memset(sbi, 0, sizeof(*sbi));
586 * See what the current blocksize for the device is, and
587 * use that as the blocksize. Otherwise (or if the blocksize
588 * is smaller than the default) use the default.
589 * This is important for devices that have a hardware
590 * sectorsize that is larger than the default.
592 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
594 printk ("EXT2-fs: unable to set blocksize\n");
599 * If the superblock doesn't start on a hardware sector boundary,
600 * calculate the offset.
602 if (blocksize != BLOCK_SIZE) {
603 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
604 offset = (sb_block*BLOCK_SIZE) % blocksize;
606 logic_sb_block = sb_block;
609 if (!(bh = sb_bread(sb, logic_sb_block))) {
610 printk ("EXT2-fs: unable to read superblock\n");
614 * Note: s_es must be initialized as soon as possible because
615 * some ext2 macro-instructions depend on its value
617 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
619 sb->s_magic = le16_to_cpu(es->s_magic);
621 if (sb->s_magic != EXT2_SUPER_MAGIC)
624 /* Set defaults before we parse the mount options */
625 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
626 if (def_mount_opts & EXT2_DEFM_DEBUG)
627 set_opt(sbi->s_mount_opt, DEBUG);
628 if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
629 set_opt(sbi->s_mount_opt, GRPID);
630 if (def_mount_opts & EXT2_DEFM_UID16)
631 set_opt(sbi->s_mount_opt, NO_UID32);
632 if (def_mount_opts & EXT2_DEFM_XATTR_USER)
633 set_opt(sbi->s_mount_opt, XATTR_USER);
634 if (def_mount_opts & EXT2_DEFM_ACL)
635 set_opt(sbi->s_mount_opt, POSIX_ACL);
637 if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
638 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
639 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
640 set_opt(sbi->s_mount_opt, ERRORS_RO);
642 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
643 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
645 if (!parse_options ((char *) data, sbi))
648 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
649 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
652 ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
653 EXT2_MOUNT_XIP if not */
655 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
656 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
657 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
658 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
659 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
660 "running e2fsck is recommended\n");
662 * Check feature flags regardless of the revision level, since we
663 * previously didn't change the revision level when setting the flags,
664 * so there is a chance incompat flags are set on a rev 0 filesystem.
666 features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
668 printk("EXT2-fs: %s: couldn't mount because of "
669 "unsupported optional features (%x).\n",
670 sb->s_id, le32_to_cpu(features));
673 if (!(sb->s_flags & MS_RDONLY) &&
674 (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
675 printk("EXT2-fs: %s: couldn't mount RDWR because of "
676 "unsupported optional features (%x).\n",
677 sb->s_id, le32_to_cpu(features));
681 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
683 if ((ext2_use_xip(sb)) && ((blocksize != PAGE_SIZE) ||
684 (sb->s_blocksize != blocksize))) {
686 printk("XIP: Unsupported blocksize\n");
690 /* If the blocksize doesn't match, re-read the thing.. */
691 if (sb->s_blocksize != blocksize) {
694 if (!sb_set_blocksize(sb, blocksize)) {
695 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
699 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
700 offset = (sb_block*BLOCK_SIZE) % blocksize;
701 bh = sb_bread(sb, logic_sb_block);
703 printk("EXT2-fs: Couldn't read superblock on "
707 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
709 if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
710 printk ("EXT2-fs: Magic mismatch, very weird !\n");
715 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
717 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
718 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
719 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
721 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
722 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
723 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
724 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
725 (sbi->s_inode_size > blocksize)) {
726 printk ("EXT2-fs: unsupported inode size: %d\n",
732 sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
733 le32_to_cpu(es->s_log_frag_size);
734 if (sbi->s_frag_size == 0)
736 sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
738 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
739 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
740 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
742 if (EXT2_INODE_SIZE(sb) == 0)
744 sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
745 if (sbi->s_inodes_per_block == 0)
747 sbi->s_itb_per_group = sbi->s_inodes_per_group /
748 sbi->s_inodes_per_block;
749 sbi->s_desc_per_block = sb->s_blocksize /
750 sizeof (struct ext2_group_desc);
752 sbi->s_mount_state = le16_to_cpu(es->s_state);
753 sbi->s_addr_per_block_bits =
754 log2 (EXT2_ADDR_PER_BLOCK(sb));
755 sbi->s_desc_per_block_bits =
756 log2 (EXT2_DESC_PER_BLOCK(sb));
758 if (sb->s_magic != EXT2_SUPER_MAGIC)
761 if (sb->s_blocksize != bh->b_size) {
763 printk ("VFS: Unsupported blocksize on dev "
768 if (sb->s_blocksize != sbi->s_frag_size) {
769 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
770 sbi->s_frag_size, sb->s_blocksize);
774 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
775 printk ("EXT2-fs: #blocks per group too big: %lu\n",
776 sbi->s_blocks_per_group);
779 if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
780 printk ("EXT2-fs: #fragments per group too big: %lu\n",
781 sbi->s_frags_per_group);
784 if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
785 printk ("EXT2-fs: #inodes per group too big: %lu\n",
786 sbi->s_inodes_per_group);
790 if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
792 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
793 le32_to_cpu(es->s_first_data_block) +
794 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
795 EXT2_BLOCKS_PER_GROUP(sb);
796 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
797 EXT2_DESC_PER_BLOCK(sb);
798 sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
799 if (sbi->s_group_desc == NULL) {
800 printk ("EXT2-fs: not enough memory\n");
803 percpu_counter_init(&sbi->s_freeblocks_counter);
804 percpu_counter_init(&sbi->s_freeinodes_counter);
805 percpu_counter_init(&sbi->s_dirs_counter);
806 bgl_lock_init(&sbi->s_blockgroup_lock);
807 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
810 printk ("EXT2-fs: not enough memory\n");
811 goto failed_mount_group_desc;
813 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
814 for (i = 0; i < db_count; i++) {
815 block = descriptor_loc(sb, logic_sb_block, i);
816 sbi->s_group_desc[i] = sb_bread(sb, block);
817 if (!sbi->s_group_desc[i]) {
818 for (j = 0; j < i; j++)
819 brelse (sbi->s_group_desc[j]);
820 printk ("EXT2-fs: unable to read group descriptors\n");
821 goto failed_mount_group_desc;
824 if (!ext2_check_descriptors (sb)) {
825 printk ("EXT2-fs: group descriptors corrupted!\n");
829 sbi->s_gdb_count = db_count;
830 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
831 spin_lock_init(&sbi->s_next_gen_lock);
833 * set up enough so that it can read an inode
835 sb->s_op = &ext2_sops;
836 sb->s_export_op = &ext2_export_ops;
837 sb->s_xattr = ext2_xattr_handlers;
838 root = iget(sb, EXT2_ROOT_INO);
839 sb->s_root = d_alloc_root(root);
842 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
845 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
848 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
851 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
852 ext2_warning(sb, __FUNCTION__,
853 "mounting ext3 filesystem as ext2\n");
854 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
855 percpu_counter_mod(&sbi->s_freeblocks_counter,
856 ext2_count_free_blocks(sb));
857 percpu_counter_mod(&sbi->s_freeinodes_counter,
858 ext2_count_free_inodes(sb));
859 percpu_counter_mod(&sbi->s_dirs_counter,
860 ext2_count_dirs(sb));
865 printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
870 for (i = 0; i < db_count; i++)
871 brelse(sbi->s_group_desc[i]);
872 failed_mount_group_desc:
873 kfree(sbi->s_group_desc);
878 sb->s_fs_info = NULL;
883 static void ext2_commit_super (struct super_block * sb,
884 struct ext2_super_block * es)
886 es->s_wtime = cpu_to_le32(get_seconds());
887 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
891 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
893 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
894 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
895 es->s_wtime = cpu_to_le32(get_seconds());
896 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
897 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
902 * In the second extended file system, it is not necessary to
903 * write the super block since we use a mapping of the
904 * disk super block in a buffer.
906 * However, this function is still used to set the fs valid
907 * flags to 0. We need to set this flag to 0 since the fs
908 * may have been checked while mounted and e2fsck may have
909 * set s_state to EXT2_VALID_FS after some corrections.
912 void ext2_write_super (struct super_block * sb)
914 struct ext2_super_block * es;
916 if (!(sb->s_flags & MS_RDONLY)) {
917 es = EXT2_SB(sb)->s_es;
919 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
920 ext2_debug ("setting valid to 0\n");
921 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
923 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
924 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
925 es->s_mtime = cpu_to_le32(get_seconds());
926 ext2_sync_super(sb, es);
928 ext2_commit_super (sb, es);
934 static int ext2_remount (struct super_block * sb, int * flags, char * data)
936 struct ext2_sb_info * sbi = EXT2_SB(sb);
937 struct ext2_super_block * es;
938 unsigned long old_mount_opt = sbi->s_mount_opt;
939 struct ext2_mount_options old_opts;
940 unsigned long old_sb_flags;
943 /* Store the old options */
944 old_sb_flags = sb->s_flags;
945 old_opts.s_mount_opt = sbi->s_mount_opt;
946 old_opts.s_resuid = sbi->s_resuid;
947 old_opts.s_resgid = sbi->s_resgid;
950 * Allow the "check" option to be passed as a remount option.
952 if (!parse_options (data, sbi)) {
957 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
958 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
961 if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
962 (old_mount_opt & EXT2_MOUNT_XIP)) &&
963 invalidate_inodes(sb))
964 ext2_warning(sb, __FUNCTION__, "busy inodes while remounting "\
965 "xip remain in cache (no functional problem)");
966 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
968 if (*flags & MS_RDONLY) {
969 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
970 !(sbi->s_mount_state & EXT2_VALID_FS))
973 * OK, we are remounting a valid rw partition rdonly, so set
974 * the rdonly flag and then mark the partition as valid again.
976 es->s_state = cpu_to_le16(sbi->s_mount_state);
977 es->s_mtime = cpu_to_le32(get_seconds());
979 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
980 ~EXT2_FEATURE_RO_COMPAT_SUPP);
982 printk("EXT2-fs: %s: couldn't remount RDWR because of "
983 "unsupported optional features (%x).\n",
984 sb->s_id, le32_to_cpu(ret));
989 * Mounting a RDONLY partition read-write, so reread and
990 * store the current valid flag. (It may have been changed
991 * by e2fsck since we originally mounted the partition.)
993 sbi->s_mount_state = le16_to_cpu(es->s_state);
994 if (!ext2_setup_super (sb, es, 0))
995 sb->s_flags &= ~MS_RDONLY;
997 ext2_sync_super(sb, es);
1000 sbi->s_mount_opt = old_opts.s_mount_opt;
1001 sbi->s_resuid = old_opts.s_resuid;
1002 sbi->s_resgid = old_opts.s_resgid;
1003 sb->s_flags = old_sb_flags;
1007 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
1009 struct ext2_sb_info *sbi = EXT2_SB(sb);
1010 unsigned long overhead;
1013 if (test_opt (sb, MINIX_DF))
1017 * Compute the overhead (FS structures)
1021 * All of the blocks before first_data_block are
1024 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
1027 * Add the overhead attributed to the superblock and
1028 * block group descriptors. If the sparse superblocks
1029 * feature is turned on, then not all groups have this.
1031 for (i = 0; i < sbi->s_groups_count; i++)
1032 overhead += ext2_bg_has_super(sb, i) +
1033 ext2_bg_num_gdb(sb, i);
1036 * Every block group has an inode bitmap, a block
1037 * bitmap, and an inode table.
1039 overhead += (sbi->s_groups_count *
1040 (2 + sbi->s_itb_per_group));
1043 buf->f_type = EXT2_SUPER_MAGIC;
1044 buf->f_bsize = sb->s_blocksize;
1045 buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
1046 buf->f_bfree = ext2_count_free_blocks(sb);
1047 buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
1048 if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
1050 buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
1051 buf->f_ffree = ext2_count_free_inodes (sb);
1052 buf->f_namelen = EXT2_NAME_LEN;
1056 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
1057 int flags, const char *dev_name, void *data)
1059 return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1064 /* Read data from quotafile - avoid pagecache and such because we cannot afford
1065 * acquiring the locks... As quota files are never truncated and quota code
1066 * itself serializes the operations (and noone else should touch the files)
1067 * we don't have to be afraid of races */
1068 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
1069 size_t len, loff_t off)
1071 struct inode *inode = sb_dqopt(sb)->files[type];
1072 sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1074 int offset = off & (sb->s_blocksize - 1);
1077 struct buffer_head tmp_bh;
1078 struct buffer_head *bh;
1079 loff_t i_size = i_size_read(inode);
1083 if (off+len > i_size)
1086 while (toread > 0) {
1087 tocopy = sb->s_blocksize - offset < toread ?
1088 sb->s_blocksize - offset : toread;
1091 err = ext2_get_block(inode, blk, &tmp_bh, 0);
1094 if (!buffer_mapped(&tmp_bh)) /* A hole? */
1095 memset(data, 0, tocopy);
1097 bh = sb_bread(sb, tmp_bh.b_blocknr);
1100 memcpy(data, bh->b_data+offset, tocopy);
1111 /* Write to quotafile */
1112 static ssize_t ext2_quota_write(struct super_block *sb, int type,
1113 const char *data, size_t len, loff_t off)
1115 struct inode *inode = sb_dqopt(sb)->files[type];
1116 sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1118 int offset = off & (sb->s_blocksize - 1);
1120 size_t towrite = len;
1121 struct buffer_head tmp_bh;
1122 struct buffer_head *bh;
1124 down(&inode->i_sem);
1125 while (towrite > 0) {
1126 tocopy = sb->s_blocksize - offset < towrite ?
1127 sb->s_blocksize - offset : towrite;
1130 err = ext2_get_block(inode, blk, &tmp_bh, 1);
1133 if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
1134 bh = sb_bread(sb, tmp_bh.b_blocknr);
1136 bh = sb_getblk(sb, tmp_bh.b_blocknr);
1142 memcpy(bh->b_data+offset, data, tocopy);
1143 flush_dcache_page(bh->b_page);
1144 set_buffer_uptodate(bh);
1145 mark_buffer_dirty(bh);
1156 if (inode->i_size < off+len-towrite)
1157 i_size_write(inode, off+len-towrite);
1159 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1160 mark_inode_dirty(inode);
1162 return len - towrite;
1167 static struct file_system_type ext2_fs_type = {
1168 .owner = THIS_MODULE,
1170 .get_sb = ext2_get_sb,
1171 .kill_sb = kill_block_super,
1172 .fs_flags = FS_REQUIRES_DEV,
1175 static int __init init_ext2_fs(void)
1177 int err = init_ext2_xattr();
1180 err = init_inodecache();
1183 err = register_filesystem(&ext2_fs_type);
1188 destroy_inodecache();
1194 static void __exit exit_ext2_fs(void)
1196 unregister_filesystem(&ext2_fs_type);
1197 destroy_inodecache();
1201 module_init(init_ext2_fs)
1202 module_exit(exit_ext2_fs)