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
);
207 static ssize_t
ext2_quota_read(struct super_block
*sb
, int type
, char *data
, size_t len
, loff_t off
);
208 static ssize_t
ext2_quota_write(struct super_block
*sb
, int type
, const char *data
, size_t len
, loff_t off
);
211 static struct super_operations ext2_sops
= {
212 .alloc_inode
= ext2_alloc_inode
,
213 .destroy_inode
= ext2_destroy_inode
,
214 .read_inode
= ext2_read_inode
,
215 .write_inode
= ext2_write_inode
,
216 .delete_inode
= ext2_delete_inode
,
217 .put_super
= ext2_put_super
,
218 .write_super
= ext2_write_super
,
219 .statfs
= ext2_statfs
,
220 .remount_fs
= ext2_remount
,
221 .clear_inode
= ext2_clear_inode
,
223 .quota_read
= ext2_quota_read
,
224 .quota_write
= ext2_quota_write
,
228 /* Yes, most of these are left as NULL!!
229 * A NULL value implies the default, which works with ext2-like file
230 * systems, but can be improved upon.
231 * Currently only get_parent is required.
233 struct dentry
*ext2_get_parent(struct dentry
*child
);
234 static struct export_operations ext2_export_ops
= {
235 .get_parent
= ext2_get_parent
,
238 static unsigned long get_sb_block(void **data
)
240 unsigned long sb_block
;
241 char *options
= (char *) *data
;
243 if (!options
|| strncmp(options
, "sb=", 3) != 0)
244 return 1; /* Default location */
246 sb_block
= simple_strtoul(options
, &options
, 0);
247 if (*options
&& *options
!= ',') {
248 printk("EXT2-fs: Invalid sb specification: %s\n",
254 *data
= (void *) options
;
259 Opt_bsd_df
, Opt_minix_df
, Opt_grpid
, Opt_nogrpid
,
260 Opt_resgid
, Opt_resuid
, Opt_sb
, Opt_err_cont
, Opt_err_panic
, Opt_err_ro
,
261 Opt_nouid32
, Opt_check
, Opt_nocheck
, Opt_debug
, Opt_oldalloc
, Opt_orlov
, Opt_nobh
,
262 Opt_user_xattr
, Opt_nouser_xattr
, Opt_acl
, Opt_noacl
,
266 static match_table_t tokens
= {
267 {Opt_bsd_df
, "bsddf"},
268 {Opt_minix_df
, "minixdf"},
269 {Opt_grpid
, "grpid"},
270 {Opt_grpid
, "bsdgroups"},
271 {Opt_nogrpid
, "nogrpid"},
272 {Opt_nogrpid
, "sysvgroups"},
273 {Opt_resgid
, "resgid=%u"},
274 {Opt_resuid
, "resuid=%u"},
276 {Opt_err_cont
, "errors=continue"},
277 {Opt_err_panic
, "errors=panic"},
278 {Opt_err_ro
, "errors=remount-ro"},
279 {Opt_nouid32
, "nouid32"},
280 {Opt_nocheck
, "check=none"},
281 {Opt_nocheck
, "nocheck"},
282 {Opt_check
, "check"},
283 {Opt_debug
, "debug"},
284 {Opt_oldalloc
, "oldalloc"},
285 {Opt_orlov
, "orlov"},
287 {Opt_user_xattr
, "user_xattr"},
288 {Opt_nouser_xattr
, "nouser_xattr"},
290 {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");
408 sbi
->s_mount_opt
|= kind
;
412 static int ext2_setup_super (struct super_block
* sb
,
413 struct ext2_super_block
* es
,
417 struct ext2_sb_info
*sbi
= EXT2_SB(sb
);
419 if (le32_to_cpu(es
->s_rev_level
) > EXT2_MAX_SUPP_REV
) {
420 printk ("EXT2-fs warning: revision level too high, "
421 "forcing read-only mode\n");
426 if (!(sbi
->s_mount_state
& EXT2_VALID_FS
))
427 printk ("EXT2-fs warning: mounting unchecked fs, "
428 "running e2fsck is recommended\n");
429 else if ((sbi
->s_mount_state
& EXT2_ERROR_FS
))
430 printk ("EXT2-fs warning: mounting fs with errors, "
431 "running e2fsck is recommended\n");
432 else if ((__s16
) le16_to_cpu(es
->s_max_mnt_count
) >= 0 &&
433 le16_to_cpu(es
->s_mnt_count
) >=
434 (unsigned short) (__s16
) le16_to_cpu(es
->s_max_mnt_count
))
435 printk ("EXT2-fs warning: maximal mount count reached, "
436 "running e2fsck is recommended\n");
437 else if (le32_to_cpu(es
->s_checkinterval
) &&
438 (le32_to_cpu(es
->s_lastcheck
) + le32_to_cpu(es
->s_checkinterval
) <= get_seconds()))
439 printk ("EXT2-fs warning: checktime reached, "
440 "running e2fsck is recommended\n");
441 if (!le16_to_cpu(es
->s_max_mnt_count
))
442 es
->s_max_mnt_count
= cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT
);
443 es
->s_mnt_count
=cpu_to_le16(le16_to_cpu(es
->s_mnt_count
) + 1);
444 ext2_write_super(sb
);
445 if (test_opt (sb
, DEBUG
))
446 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
447 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
448 EXT2FS_VERSION
, EXT2FS_DATE
, sb
->s_blocksize
,
451 EXT2_BLOCKS_PER_GROUP(sb
),
452 EXT2_INODES_PER_GROUP(sb
),
454 #ifdef CONFIG_EXT2_CHECK
455 if (test_opt (sb
, CHECK
)) {
456 ext2_check_blocks_bitmap (sb
);
457 ext2_check_inodes_bitmap (sb
);
463 static int ext2_check_descriptors (struct super_block
* sb
)
467 struct ext2_sb_info
*sbi
= EXT2_SB(sb
);
468 unsigned long block
= le32_to_cpu(sbi
->s_es
->s_first_data_block
);
469 struct ext2_group_desc
* gdp
= NULL
;
471 ext2_debug ("Checking group descriptors");
473 for (i
= 0; i
< sbi
->s_groups_count
; i
++)
475 if ((i
% EXT2_DESC_PER_BLOCK(sb
)) == 0)
476 gdp
= (struct ext2_group_desc
*) sbi
->s_group_desc
[desc_block
++]->b_data
;
477 if (le32_to_cpu(gdp
->bg_block_bitmap
) < block
||
478 le32_to_cpu(gdp
->bg_block_bitmap
) >= block
+ EXT2_BLOCKS_PER_GROUP(sb
))
480 ext2_error (sb
, "ext2_check_descriptors",
481 "Block bitmap for group %d"
482 " not in group (block %lu)!",
483 i
, (unsigned long) le32_to_cpu(gdp
->bg_block_bitmap
));
486 if (le32_to_cpu(gdp
->bg_inode_bitmap
) < block
||
487 le32_to_cpu(gdp
->bg_inode_bitmap
) >= block
+ EXT2_BLOCKS_PER_GROUP(sb
))
489 ext2_error (sb
, "ext2_check_descriptors",
490 "Inode bitmap for group %d"
491 " not in group (block %lu)!",
492 i
, (unsigned long) le32_to_cpu(gdp
->bg_inode_bitmap
));
495 if (le32_to_cpu(gdp
->bg_inode_table
) < block
||
496 le32_to_cpu(gdp
->bg_inode_table
) + sbi
->s_itb_per_group
>=
497 block
+ EXT2_BLOCKS_PER_GROUP(sb
))
499 ext2_error (sb
, "ext2_check_descriptors",
500 "Inode table for group %d"
501 " not in group (block %lu)!",
502 i
, (unsigned long) le32_to_cpu(gdp
->bg_inode_table
));
505 block
+= EXT2_BLOCKS_PER_GROUP(sb
);
511 #define log2(n) ffz(~(n))
514 * Maximal file size. There is a direct, and {,double-,triple-}indirect
515 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
516 * We need to be 1 filesystem block less than the 2^32 sector limit.
518 static loff_t
ext2_max_size(int bits
)
520 loff_t res
= EXT2_NDIR_BLOCKS
;
521 /* This constant is calculated to be the largest file size for a
522 * dense, 4k-blocksize file such that the total number of
523 * sectors in the file, including data and all indirect blocks,
524 * does not exceed 2^32. */
525 const loff_t upper_limit
= 0x1ff7fffd000LL
;
527 res
+= 1LL << (bits
-2);
528 res
+= 1LL << (2*(bits
-2));
529 res
+= 1LL << (3*(bits
-2));
531 if (res
> upper_limit
)
536 static unsigned long descriptor_loc(struct super_block
*sb
,
537 unsigned long logic_sb_block
,
540 struct ext2_sb_info
*sbi
= EXT2_SB(sb
);
541 unsigned long bg
, first_data_block
, first_meta_bg
;
544 first_data_block
= le32_to_cpu(sbi
->s_es
->s_first_data_block
);
545 first_meta_bg
= le32_to_cpu(sbi
->s_es
->s_first_meta_bg
);
547 if (!EXT2_HAS_INCOMPAT_FEATURE(sb
, EXT2_FEATURE_INCOMPAT_META_BG
) ||
549 return (logic_sb_block
+ nr
+ 1);
550 bg
= sbi
->s_desc_per_block
* nr
;
551 if (ext2_bg_has_super(sb
, bg
))
553 return (first_data_block
+ has_super
+ (bg
* sbi
->s_blocks_per_group
));
556 static int ext2_fill_super(struct super_block
*sb
, void *data
, int silent
)
558 struct buffer_head
* bh
;
559 struct ext2_sb_info
* sbi
;
560 struct ext2_super_block
* es
;
563 unsigned long sb_block
= get_sb_block(&data
);
564 unsigned long logic_sb_block
;
565 unsigned long offset
= 0;
566 unsigned long def_mount_opts
;
567 int blocksize
= BLOCK_SIZE
;
572 sbi
= kmalloc(sizeof(*sbi
), GFP_KERNEL
);
576 memset(sbi
, 0, sizeof(*sbi
));
579 * See what the current blocksize for the device is, and
580 * use that as the blocksize. Otherwise (or if the blocksize
581 * is smaller than the default) use the default.
582 * This is important for devices that have a hardware
583 * sectorsize that is larger than the default.
585 blocksize
= sb_min_blocksize(sb
, BLOCK_SIZE
);
587 printk ("EXT2-fs: unable to set blocksize\n");
592 * If the superblock doesn't start on a hardware sector boundary,
593 * calculate the offset.
595 if (blocksize
!= BLOCK_SIZE
) {
596 logic_sb_block
= (sb_block
*BLOCK_SIZE
) / blocksize
;
597 offset
= (sb_block
*BLOCK_SIZE
) % blocksize
;
599 logic_sb_block
= sb_block
;
602 if (!(bh
= sb_bread(sb
, logic_sb_block
))) {
603 printk ("EXT2-fs: unable to read superblock\n");
607 * Note: s_es must be initialized as soon as possible because
608 * some ext2 macro-instructions depend on its value
610 es
= (struct ext2_super_block
*) (((char *)bh
->b_data
) + offset
);
612 sb
->s_magic
= le16_to_cpu(es
->s_magic
);
614 if (sb
->s_magic
!= EXT2_SUPER_MAGIC
)
617 /* Set defaults before we parse the mount options */
618 def_mount_opts
= le32_to_cpu(es
->s_default_mount_opts
);
619 if (def_mount_opts
& EXT2_DEFM_DEBUG
)
620 set_opt(sbi
->s_mount_opt
, DEBUG
);
621 if (def_mount_opts
& EXT2_DEFM_BSDGROUPS
)
622 set_opt(sbi
->s_mount_opt
, GRPID
);
623 if (def_mount_opts
& EXT2_DEFM_UID16
)
624 set_opt(sbi
->s_mount_opt
, NO_UID32
);
625 if (def_mount_opts
& EXT2_DEFM_XATTR_USER
)
626 set_opt(sbi
->s_mount_opt
, XATTR_USER
);
627 if (def_mount_opts
& EXT2_DEFM_ACL
)
628 set_opt(sbi
->s_mount_opt
, POSIX_ACL
);
630 if (le16_to_cpu(sbi
->s_es
->s_errors
) == EXT2_ERRORS_PANIC
)
631 set_opt(sbi
->s_mount_opt
, ERRORS_PANIC
);
632 else if (le16_to_cpu(sbi
->s_es
->s_errors
) == EXT2_ERRORS_RO
)
633 set_opt(sbi
->s_mount_opt
, ERRORS_RO
);
635 sbi
->s_resuid
= le16_to_cpu(es
->s_def_resuid
);
636 sbi
->s_resgid
= le16_to_cpu(es
->s_def_resgid
);
638 if (!parse_options ((char *) data
, sbi
))
641 sb
->s_flags
= (sb
->s_flags
& ~MS_POSIXACL
) |
642 ((EXT2_SB(sb
)->s_mount_opt
& EXT2_MOUNT_POSIX_ACL
) ?
645 if (le32_to_cpu(es
->s_rev_level
) == EXT2_GOOD_OLD_REV
&&
646 (EXT2_HAS_COMPAT_FEATURE(sb
, ~0U) ||
647 EXT2_HAS_RO_COMPAT_FEATURE(sb
, ~0U) ||
648 EXT2_HAS_INCOMPAT_FEATURE(sb
, ~0U)))
649 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
650 "running e2fsck is recommended\n");
652 * Check feature flags regardless of the revision level, since we
653 * previously didn't change the revision level when setting the flags,
654 * so there is a chance incompat flags are set on a rev 0 filesystem.
656 features
= EXT2_HAS_INCOMPAT_FEATURE(sb
, ~EXT2_FEATURE_INCOMPAT_SUPP
);
658 printk("EXT2-fs: %s: couldn't mount because of "
659 "unsupported optional features (%x).\n",
660 sb
->s_id
, le32_to_cpu(features
));
663 if (!(sb
->s_flags
& MS_RDONLY
) &&
664 (features
= EXT2_HAS_RO_COMPAT_FEATURE(sb
, ~EXT2_FEATURE_RO_COMPAT_SUPP
))){
665 printk("EXT2-fs: %s: couldn't mount RDWR because of "
666 "unsupported optional features (%x).\n",
667 sb
->s_id
, le32_to_cpu(features
));
671 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbi
->s_es
->s_log_block_size
);
673 /* If the blocksize doesn't match, re-read the thing.. */
674 if (sb
->s_blocksize
!= blocksize
) {
677 if (!sb_set_blocksize(sb
, blocksize
)) {
678 printk(KERN_ERR
"EXT2-fs: blocksize too small for device.\n");
682 logic_sb_block
= (sb_block
*BLOCK_SIZE
) / blocksize
;
683 offset
= (sb_block
*BLOCK_SIZE
) % blocksize
;
684 bh
= sb_bread(sb
, logic_sb_block
);
686 printk("EXT2-fs: Couldn't read superblock on "
690 es
= (struct ext2_super_block
*) (((char *)bh
->b_data
) + offset
);
692 if (es
->s_magic
!= cpu_to_le16(EXT2_SUPER_MAGIC
)) {
693 printk ("EXT2-fs: Magic mismatch, very weird !\n");
698 sb
->s_maxbytes
= ext2_max_size(sb
->s_blocksize_bits
);
700 if (le32_to_cpu(es
->s_rev_level
) == EXT2_GOOD_OLD_REV
) {
701 sbi
->s_inode_size
= EXT2_GOOD_OLD_INODE_SIZE
;
702 sbi
->s_first_ino
= EXT2_GOOD_OLD_FIRST_INO
;
704 sbi
->s_inode_size
= le16_to_cpu(es
->s_inode_size
);
705 sbi
->s_first_ino
= le32_to_cpu(es
->s_first_ino
);
706 if ((sbi
->s_inode_size
< EXT2_GOOD_OLD_INODE_SIZE
) ||
707 (sbi
->s_inode_size
& (sbi
->s_inode_size
- 1)) ||
708 (sbi
->s_inode_size
> blocksize
)) {
709 printk ("EXT2-fs: unsupported inode size: %d\n",
715 sbi
->s_frag_size
= EXT2_MIN_FRAG_SIZE
<<
716 le32_to_cpu(es
->s_log_frag_size
);
717 if (sbi
->s_frag_size
== 0)
719 sbi
->s_frags_per_block
= sb
->s_blocksize
/ sbi
->s_frag_size
;
721 sbi
->s_blocks_per_group
= le32_to_cpu(es
->s_blocks_per_group
);
722 sbi
->s_frags_per_group
= le32_to_cpu(es
->s_frags_per_group
);
723 sbi
->s_inodes_per_group
= le32_to_cpu(es
->s_inodes_per_group
);
725 if (EXT2_INODE_SIZE(sb
) == 0)
727 sbi
->s_inodes_per_block
= sb
->s_blocksize
/ EXT2_INODE_SIZE(sb
);
728 if (sbi
->s_inodes_per_block
== 0)
730 sbi
->s_itb_per_group
= sbi
->s_inodes_per_group
/
731 sbi
->s_inodes_per_block
;
732 sbi
->s_desc_per_block
= sb
->s_blocksize
/
733 sizeof (struct ext2_group_desc
);
735 sbi
->s_mount_state
= le16_to_cpu(es
->s_state
);
736 sbi
->s_addr_per_block_bits
=
737 log2 (EXT2_ADDR_PER_BLOCK(sb
));
738 sbi
->s_desc_per_block_bits
=
739 log2 (EXT2_DESC_PER_BLOCK(sb
));
741 if (sb
->s_magic
!= EXT2_SUPER_MAGIC
)
744 if (sb
->s_blocksize
!= bh
->b_size
) {
746 printk ("VFS: Unsupported blocksize on dev "
751 if (sb
->s_blocksize
!= sbi
->s_frag_size
) {
752 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
753 sbi
->s_frag_size
, sb
->s_blocksize
);
757 if (sbi
->s_blocks_per_group
> sb
->s_blocksize
* 8) {
758 printk ("EXT2-fs: #blocks per group too big: %lu\n",
759 sbi
->s_blocks_per_group
);
762 if (sbi
->s_frags_per_group
> sb
->s_blocksize
* 8) {
763 printk ("EXT2-fs: #fragments per group too big: %lu\n",
764 sbi
->s_frags_per_group
);
767 if (sbi
->s_inodes_per_group
> sb
->s_blocksize
* 8) {
768 printk ("EXT2-fs: #inodes per group too big: %lu\n",
769 sbi
->s_inodes_per_group
);
773 if (EXT2_BLOCKS_PER_GROUP(sb
) == 0)
775 sbi
->s_groups_count
= (le32_to_cpu(es
->s_blocks_count
) -
776 le32_to_cpu(es
->s_first_data_block
) +
777 EXT2_BLOCKS_PER_GROUP(sb
) - 1) /
778 EXT2_BLOCKS_PER_GROUP(sb
);
779 db_count
= (sbi
->s_groups_count
+ EXT2_DESC_PER_BLOCK(sb
) - 1) /
780 EXT2_DESC_PER_BLOCK(sb
);
781 sbi
->s_group_desc
= kmalloc (db_count
* sizeof (struct buffer_head
*), GFP_KERNEL
);
782 if (sbi
->s_group_desc
== NULL
) {
783 printk ("EXT2-fs: not enough memory\n");
786 percpu_counter_init(&sbi
->s_freeblocks_counter
);
787 percpu_counter_init(&sbi
->s_freeinodes_counter
);
788 percpu_counter_init(&sbi
->s_dirs_counter
);
789 bgl_lock_init(&sbi
->s_blockgroup_lock
);
790 sbi
->s_debts
= kmalloc(sbi
->s_groups_count
* sizeof(*sbi
->s_debts
),
793 printk ("EXT2-fs: not enough memory\n");
794 goto failed_mount_group_desc
;
796 memset(sbi
->s_debts
, 0, sbi
->s_groups_count
* sizeof(*sbi
->s_debts
));
797 for (i
= 0; i
< db_count
; i
++) {
798 block
= descriptor_loc(sb
, logic_sb_block
, i
);
799 sbi
->s_group_desc
[i
] = sb_bread(sb
, block
);
800 if (!sbi
->s_group_desc
[i
]) {
801 for (j
= 0; j
< i
; j
++)
802 brelse (sbi
->s_group_desc
[j
]);
803 printk ("EXT2-fs: unable to read group descriptors\n");
804 goto failed_mount_group_desc
;
807 if (!ext2_check_descriptors (sb
)) {
808 printk ("EXT2-fs: group descriptors corrupted!\n");
812 sbi
->s_gdb_count
= db_count
;
813 get_random_bytes(&sbi
->s_next_generation
, sizeof(u32
));
814 spin_lock_init(&sbi
->s_next_gen_lock
);
816 * set up enough so that it can read an inode
818 sb
->s_op
= &ext2_sops
;
819 sb
->s_export_op
= &ext2_export_ops
;
820 sb
->s_xattr
= ext2_xattr_handlers
;
821 root
= iget(sb
, EXT2_ROOT_INO
);
822 sb
->s_root
= d_alloc_root(root
);
825 printk(KERN_ERR
"EXT2-fs: get root inode failed\n");
828 if (!S_ISDIR(root
->i_mode
) || !root
->i_blocks
|| !root
->i_size
) {
831 printk(KERN_ERR
"EXT2-fs: corrupt root inode, run e2fsck\n");
834 if (EXT2_HAS_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
))
835 ext2_warning(sb
, __FUNCTION__
,
836 "mounting ext3 filesystem as ext2\n");
837 ext2_setup_super (sb
, es
, sb
->s_flags
& MS_RDONLY
);
838 percpu_counter_mod(&sbi
->s_freeblocks_counter
,
839 ext2_count_free_blocks(sb
));
840 percpu_counter_mod(&sbi
->s_freeinodes_counter
,
841 ext2_count_free_inodes(sb
));
842 percpu_counter_mod(&sbi
->s_dirs_counter
,
843 ext2_count_dirs(sb
));
848 printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
853 for (i
= 0; i
< db_count
; i
++)
854 brelse(sbi
->s_group_desc
[i
]);
855 failed_mount_group_desc
:
856 kfree(sbi
->s_group_desc
);
861 sb
->s_fs_info
= NULL
;
866 static void ext2_commit_super (struct super_block
* sb
,
867 struct ext2_super_block
* es
)
869 es
->s_wtime
= cpu_to_le32(get_seconds());
870 mark_buffer_dirty(EXT2_SB(sb
)->s_sbh
);
874 static void ext2_sync_super(struct super_block
*sb
, struct ext2_super_block
*es
)
876 es
->s_free_blocks_count
= cpu_to_le32(ext2_count_free_blocks(sb
));
877 es
->s_free_inodes_count
= cpu_to_le32(ext2_count_free_inodes(sb
));
878 es
->s_wtime
= cpu_to_le32(get_seconds());
879 mark_buffer_dirty(EXT2_SB(sb
)->s_sbh
);
880 sync_dirty_buffer(EXT2_SB(sb
)->s_sbh
);
885 * In the second extended file system, it is not necessary to
886 * write the super block since we use a mapping of the
887 * disk super block in a buffer.
889 * However, this function is still used to set the fs valid
890 * flags to 0. We need to set this flag to 0 since the fs
891 * may have been checked while mounted and e2fsck may have
892 * set s_state to EXT2_VALID_FS after some corrections.
895 void ext2_write_super (struct super_block
* sb
)
897 struct ext2_super_block
* es
;
899 if (!(sb
->s_flags
& MS_RDONLY
)) {
900 es
= EXT2_SB(sb
)->s_es
;
902 if (le16_to_cpu(es
->s_state
) & EXT2_VALID_FS
) {
903 ext2_debug ("setting valid to 0\n");
904 es
->s_state
= cpu_to_le16(le16_to_cpu(es
->s_state
) &
906 es
->s_free_blocks_count
= cpu_to_le32(ext2_count_free_blocks(sb
));
907 es
->s_free_inodes_count
= cpu_to_le32(ext2_count_free_inodes(sb
));
908 es
->s_mtime
= cpu_to_le32(get_seconds());
909 ext2_sync_super(sb
, es
);
911 ext2_commit_super (sb
, es
);
917 static int ext2_remount (struct super_block
* sb
, int * flags
, char * data
)
919 struct ext2_sb_info
* sbi
= EXT2_SB(sb
);
920 struct ext2_super_block
* es
;
923 * Allow the "check" option to be passed as a remount option.
925 if (!parse_options (data
, sbi
))
928 sb
->s_flags
= (sb
->s_flags
& ~MS_POSIXACL
) |
929 ((sbi
->s_mount_opt
& EXT2_MOUNT_POSIX_ACL
) ? MS_POSIXACL
: 0);
932 if ((*flags
& MS_RDONLY
) == (sb
->s_flags
& MS_RDONLY
))
934 if (*flags
& MS_RDONLY
) {
935 if (le16_to_cpu(es
->s_state
) & EXT2_VALID_FS
||
936 !(sbi
->s_mount_state
& EXT2_VALID_FS
))
939 * OK, we are remounting a valid rw partition rdonly, so set
940 * the rdonly flag and then mark the partition as valid again.
942 es
->s_state
= cpu_to_le16(sbi
->s_mount_state
);
943 es
->s_mtime
= cpu_to_le32(get_seconds());
945 __le32 ret
= EXT2_HAS_RO_COMPAT_FEATURE(sb
,
946 ~EXT2_FEATURE_RO_COMPAT_SUPP
);
948 printk("EXT2-fs: %s: couldn't remount RDWR because of "
949 "unsupported optional features (%x).\n",
950 sb
->s_id
, le32_to_cpu(ret
));
954 * Mounting a RDONLY partition read-write, so reread and
955 * store the current valid flag. (It may have been changed
956 * by e2fsck since we originally mounted the partition.)
958 sbi
->s_mount_state
= le16_to_cpu(es
->s_state
);
959 if (!ext2_setup_super (sb
, es
, 0))
960 sb
->s_flags
&= ~MS_RDONLY
;
962 ext2_sync_super(sb
, es
);
966 static int ext2_statfs (struct super_block
* sb
, struct kstatfs
* buf
)
968 struct ext2_sb_info
*sbi
= EXT2_SB(sb
);
969 unsigned long overhead
;
972 if (test_opt (sb
, MINIX_DF
))
976 * Compute the overhead (FS structures)
980 * All of the blocks before first_data_block are
983 overhead
= le32_to_cpu(sbi
->s_es
->s_first_data_block
);
986 * Add the overhead attributed to the superblock and
987 * block group descriptors. If the sparse superblocks
988 * feature is turned on, then not all groups have this.
990 for (i
= 0; i
< sbi
->s_groups_count
; i
++)
991 overhead
+= ext2_bg_has_super(sb
, i
) +
992 ext2_bg_num_gdb(sb
, i
);
995 * Every block group has an inode bitmap, a block
996 * bitmap, and an inode table.
998 overhead
+= (sbi
->s_groups_count
*
999 (2 + sbi
->s_itb_per_group
));
1002 buf
->f_type
= EXT2_SUPER_MAGIC
;
1003 buf
->f_bsize
= sb
->s_blocksize
;
1004 buf
->f_blocks
= le32_to_cpu(sbi
->s_es
->s_blocks_count
) - overhead
;
1005 buf
->f_bfree
= ext2_count_free_blocks(sb
);
1006 buf
->f_bavail
= buf
->f_bfree
- le32_to_cpu(sbi
->s_es
->s_r_blocks_count
);
1007 if (buf
->f_bfree
< le32_to_cpu(sbi
->s_es
->s_r_blocks_count
))
1009 buf
->f_files
= le32_to_cpu(sbi
->s_es
->s_inodes_count
);
1010 buf
->f_ffree
= ext2_count_free_inodes (sb
);
1011 buf
->f_namelen
= EXT2_NAME_LEN
;
1015 static struct super_block
*ext2_get_sb(struct file_system_type
*fs_type
,
1016 int flags
, const char *dev_name
, void *data
)
1018 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, ext2_fill_super
);
1023 /* Read data from quotafile - avoid pagecache and such because we cannot afford
1024 * acquiring the locks... As quota files are never truncated and quota code
1025 * itself serializes the operations (and noone else should touch the files)
1026 * we don't have to be afraid of races */
1027 static ssize_t
ext2_quota_read(struct super_block
*sb
, int type
, char *data
,
1028 size_t len
, loff_t off
)
1030 struct inode
*inode
= sb_dqopt(sb
)->files
[type
];
1031 sector_t blk
= off
>> EXT2_BLOCK_SIZE_BITS(sb
);
1033 int offset
= off
& (sb
->s_blocksize
- 1);
1036 struct buffer_head tmp_bh
;
1037 struct buffer_head
*bh
;
1038 loff_t i_size
= i_size_read(inode
);
1042 if (off
+len
> i_size
)
1045 while (toread
> 0) {
1046 tocopy
= sb
->s_blocksize
- offset
< toread
?
1047 sb
->s_blocksize
- offset
: toread
;
1050 err
= ext2_get_block(inode
, blk
, &tmp_bh
, 0);
1053 if (!buffer_mapped(&tmp_bh
)) /* A hole? */
1054 memset(data
, 0, tocopy
);
1056 bh
= sb_bread(sb
, tmp_bh
.b_blocknr
);
1059 memcpy(data
, bh
->b_data
+offset
, tocopy
);
1070 /* Write to quotafile */
1071 static ssize_t
ext2_quota_write(struct super_block
*sb
, int type
,
1072 const char *data
, size_t len
, loff_t off
)
1074 struct inode
*inode
= sb_dqopt(sb
)->files
[type
];
1075 sector_t blk
= off
>> EXT2_BLOCK_SIZE_BITS(sb
);
1077 int offset
= off
& (sb
->s_blocksize
- 1);
1079 size_t towrite
= len
;
1080 struct buffer_head tmp_bh
;
1081 struct buffer_head
*bh
;
1083 down(&inode
->i_sem
);
1084 while (towrite
> 0) {
1085 tocopy
= sb
->s_blocksize
- offset
< towrite
?
1086 sb
->s_blocksize
- offset
: towrite
;
1089 err
= ext2_get_block(inode
, blk
, &tmp_bh
, 1);
1092 if (offset
|| tocopy
!= EXT2_BLOCK_SIZE(sb
))
1093 bh
= sb_bread(sb
, tmp_bh
.b_blocknr
);
1095 bh
= sb_getblk(sb
, tmp_bh
.b_blocknr
);
1101 memcpy(bh
->b_data
+offset
, data
, tocopy
);
1102 flush_dcache_page(bh
->b_page
);
1103 set_buffer_uptodate(bh
);
1104 mark_buffer_dirty(bh
);
1115 if (inode
->i_size
< off
+len
-towrite
)
1116 i_size_write(inode
, off
+len
-towrite
);
1118 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1119 mark_inode_dirty(inode
);
1121 return len
- towrite
;
1126 static struct file_system_type ext2_fs_type
= {
1127 .owner
= THIS_MODULE
,
1129 .get_sb
= ext2_get_sb
,
1130 .kill_sb
= kill_block_super
,
1131 .fs_flags
= FS_REQUIRES_DEV
,
1134 static int __init
init_ext2_fs(void)
1136 int err
= init_ext2_xattr();
1139 err
= init_inodecache();
1142 err
= register_filesystem(&ext2_fs_type
);
1147 destroy_inodecache();
1153 static void __exit
exit_ext2_fs(void)
1155 unregister_filesystem(&ext2_fs_type
);
1156 destroy_inodecache();
1160 module_init(init_ext2_fs
)
1161 module_exit(exit_ext2_fs
)