Merge with 2.5.75.
[linux-2.6/linux-mips.git] / fs / ext2 / super.c
blob14b8cca47277ce51e98da8f237f7449013621e19
1 /*
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)
9 * from
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/random.h>
26 #include <linux/buffer_head.h>
27 #include <linux/smp_lock.h>
28 #include <linux/vfs.h>
29 #include <asm/uaccess.h>
30 #include "ext2.h"
31 #include "xattr.h"
32 #include "acl.h"
34 static void ext2_sync_super(struct super_block *sb,
35 struct ext2_super_block *es);
36 static int ext2_remount (struct super_block * sb, int * flags, char * data);
37 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf);
39 static char error_buf[1024];
41 void ext2_error (struct super_block * sb, const char * function,
42 const char * fmt, ...)
44 va_list args;
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;
50 es->s_state =
51 cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
52 ext2_sync_super(sb, es);
54 va_start (args, fmt);
55 vsprintf (error_buf, fmt, args);
56 va_end (args);
57 if (test_opt (sb, ERRORS_PANIC))
58 panic ("EXT2-fs panic (device %s): %s: %s\n",
59 sb->s_id, function, error_buf);
60 printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
61 sb->s_id, function, error_buf);
62 if (test_opt (sb, ERRORS_RO)) {
63 printk ("Remounting filesystem read-only\n");
64 sb->s_flags |= MS_RDONLY;
68 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
69 const char * fmt, ...)
71 va_list args;
72 struct ext2_sb_info *sbi = EXT2_SB(sb);
74 if (!(sb->s_flags & MS_RDONLY)) {
75 sbi->s_mount_state |= EXT2_ERROR_FS;
76 sbi->s_es->s_state =
77 cpu_to_le16(le16_to_cpu(sbi->s_es->s_state) | EXT2_ERROR_FS);
78 mark_buffer_dirty(sbi->s_sbh);
79 sb->s_dirt = 1;
81 va_start (args, fmt);
82 vsprintf (error_buf, fmt, args);
83 va_end (args);
84 sb->s_flags |= MS_RDONLY;
85 panic ("EXT2-fs panic (device %s): %s: %s\n",
86 sb->s_id, function, error_buf);
89 void ext2_warning (struct super_block * sb, const char * function,
90 const char * fmt, ...)
92 va_list args;
94 va_start (args, fmt);
95 vsprintf (error_buf, fmt, args);
96 va_end (args);
97 printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
98 sb->s_id, function, error_buf);
101 void ext2_update_dynamic_rev(struct super_block *sb)
103 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
105 if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
106 return;
108 ext2_warning(sb, __FUNCTION__,
109 "updating to rev %d because of new feature flag, "
110 "running e2fsck is recommended",
111 EXT2_DYNAMIC_REV);
113 es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
114 es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
115 es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
116 /* leave es->s_feature_*compat flags alone */
117 /* es->s_uuid will be set by e2fsck if empty */
120 * The rest of the superblock fields should be zero, and if not it
121 * means they are likely already in use, so leave them alone. We
122 * can leave it up to e2fsck to clean up any inconsistencies there.
126 static void ext2_put_super (struct super_block * sb)
128 int db_count;
129 int i;
130 struct ext2_sb_info *sbi = EXT2_SB(sb);
132 ext2_xattr_put_super(sb);
133 if (!(sb->s_flags & MS_RDONLY)) {
134 struct ext2_super_block *es = sbi->s_es;
136 es->s_state = le16_to_cpu(sbi->s_mount_state);
137 ext2_sync_super(sb, es);
139 db_count = sbi->s_gdb_count;
140 for (i = 0; i < db_count; i++)
141 if (sbi->s_group_desc[i])
142 brelse (sbi->s_group_desc[i]);
143 kfree(sbi->s_group_desc);
144 kfree(sbi->s_debts);
145 brelse (sbi->s_sbh);
146 sb->s_fs_info = NULL;
147 kfree(sbi);
149 return;
152 static kmem_cache_t * ext2_inode_cachep;
154 static struct inode *ext2_alloc_inode(struct super_block *sb)
156 struct ext2_inode_info *ei;
157 ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
158 if (!ei)
159 return NULL;
160 #ifdef CONFIG_EXT2_FS_POSIX_ACL
161 ei->i_acl = EXT2_ACL_NOT_CACHED;
162 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
163 #endif
164 ei->vfs_inode.i_version = 1;
165 return &ei->vfs_inode;
168 static void ext2_destroy_inode(struct inode *inode)
170 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
173 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
175 struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
177 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
178 SLAB_CTOR_CONSTRUCTOR) {
179 rwlock_init(&ei->i_meta_lock);
180 #ifdef CONFIG_EXT2_FS_XATTR
181 init_rwsem(&ei->xattr_sem);
182 #endif
183 inode_init_once(&ei->vfs_inode);
187 static int init_inodecache(void)
189 ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
190 sizeof(struct ext2_inode_info),
191 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
192 init_once, NULL);
193 if (ext2_inode_cachep == NULL)
194 return -ENOMEM;
195 return 0;
198 static void destroy_inodecache(void)
200 if (kmem_cache_destroy(ext2_inode_cachep))
201 printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
204 #ifdef CONFIG_EXT2_FS_POSIX_ACL
206 static void ext2_clear_inode(struct inode *inode)
208 struct ext2_inode_info *ei = EXT2_I(inode);
210 if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
211 posix_acl_release(ei->i_acl);
212 ei->i_acl = EXT2_ACL_NOT_CACHED;
214 if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
215 posix_acl_release(ei->i_default_acl);
216 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
220 #else
221 # define ext2_clear_inode NULL
222 #endif
224 static struct super_operations ext2_sops = {
225 .alloc_inode = ext2_alloc_inode,
226 .destroy_inode = ext2_destroy_inode,
227 .read_inode = ext2_read_inode,
228 .write_inode = ext2_write_inode,
229 .put_inode = ext2_put_inode,
230 .delete_inode = ext2_delete_inode,
231 .put_super = ext2_put_super,
232 .write_super = ext2_write_super,
233 .statfs = ext2_statfs,
234 .remount_fs = ext2_remount,
235 .clear_inode = ext2_clear_inode,
238 /* Yes, most of these are left as NULL!!
239 * A NULL value implies the default, which works with ext2-like file
240 * systems, but can be improved upon.
241 * Currently only get_parent is required.
243 struct dentry *ext2_get_parent(struct dentry *child);
244 static struct export_operations ext2_export_ops = {
245 .get_parent = ext2_get_parent,
248 static unsigned long get_sb_block(void **data)
250 unsigned long sb_block;
251 char *options = (char *) *data;
253 if (!options || strncmp(options, "sb=", 3) != 0)
254 return 1; /* Default location */
255 options += 3;
256 sb_block = simple_strtoul(options, &options, 0);
257 if (*options && *options != ',') {
258 printk("EXT2-fs: Invalid sb specification: %s\n",
259 (char *) *data);
260 return 1;
262 if (*options == ',')
263 options++;
264 *data = (void *) options;
265 return sb_block;
268 static int want_value(char *value, char *option)
270 if (!value || !*value) {
271 printk(KERN_NOTICE "EXT2-fs: the %s option needs an argument\n",
272 option);
273 return -1;
275 return 0;
278 static int want_null_value(char *value, char *option)
280 if (*value) {
281 printk(KERN_NOTICE "EXT2-fs: Invalid %s argument: %s\n",
282 option, value);
283 return -1;
285 return 0;
288 static int want_numeric(char *value, char *option, unsigned long *number)
290 if (want_value(value, option))
291 return -1;
292 *number = simple_strtoul(value, &value, 0);
293 if (want_null_value(value, option))
294 return -1;
295 return 0;
299 * This function has been shamelessly adapted from the msdos fs
301 static int parse_options (char * options,
302 struct ext2_sb_info *sbi)
304 char * this_char;
305 char * value;
307 if (!options)
308 return 1;
309 while ((this_char = strsep (&options, ",")) != NULL) {
310 if (!*this_char)
311 continue;
312 if ((value = strchr (this_char, '=')) != NULL)
313 *value++ = 0;
314 #ifdef CONFIG_EXT2_FS_XATTR
315 if (!strcmp (this_char, "user_xattr"))
316 set_opt (sbi->s_mount_opt, XATTR_USER);
317 else if (!strcmp (this_char, "nouser_xattr"))
318 clear_opt (sbi->s_mount_opt, XATTR_USER);
319 else
320 #endif
321 #ifdef CONFIG_EXT2_FS_POSIX_ACL
322 if (!strcmp(this_char, "acl"))
323 set_opt(sbi->s_mount_opt, POSIX_ACL);
324 else if (!strcmp(this_char, "noacl"))
325 clear_opt(sbi->s_mount_opt, POSIX_ACL);
326 else
327 #endif
328 if (!strcmp (this_char, "bsddf"))
329 clear_opt (sbi->s_mount_opt, MINIX_DF);
330 else if (!strcmp (this_char, "nouid32")) {
331 set_opt (sbi->s_mount_opt, NO_UID32);
333 else if (!strcmp (this_char, "check")) {
334 if (!value || !*value || !strcmp (value, "none"))
335 clear_opt (sbi->s_mount_opt, CHECK);
336 else
337 #ifdef CONFIG_EXT2_CHECK
338 set_opt (sbi->s_mount_opt, CHECK);
339 #else
340 printk("EXT2 Check option not supported\n");
341 #endif
343 else if (!strcmp (this_char, "debug"))
344 set_opt (sbi->s_mount_opt, DEBUG);
345 else if (!strcmp (this_char, "errors")) {
346 if (!value || !*value) {
347 printk ("EXT2-fs: the errors option requires "
348 "an argument\n");
349 return 0;
351 if (!strcmp (value, "continue")) {
352 clear_opt (sbi->s_mount_opt, ERRORS_RO);
353 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
354 set_opt (sbi->s_mount_opt, ERRORS_CONT);
356 else if (!strcmp (value, "remount-ro")) {
357 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
358 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
359 set_opt (sbi->s_mount_opt, ERRORS_RO);
361 else if (!strcmp (value, "panic")) {
362 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
363 clear_opt (sbi->s_mount_opt, ERRORS_RO);
364 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
366 else {
367 printk ("EXT2-fs: Invalid errors option: %s\n",
368 value);
369 return 0;
372 else if (!strcmp (this_char, "grpid") ||
373 !strcmp (this_char, "bsdgroups"))
374 set_opt (sbi->s_mount_opt, GRPID);
375 else if (!strcmp (this_char, "minixdf"))
376 set_opt (sbi->s_mount_opt, MINIX_DF);
377 else if (!strcmp (this_char, "nocheck"))
378 clear_opt (sbi->s_mount_opt, CHECK);
379 else if (!strcmp (this_char, "nogrpid") ||
380 !strcmp (this_char, "sysvgroups"))
381 clear_opt (sbi->s_mount_opt, GRPID);
382 else if (!strcmp (this_char, "resgid")) {
383 unsigned long v;
384 if (want_numeric(value, "resgid", &v))
385 return 0;
386 sbi->s_resgid = v;
388 else if (!strcmp (this_char, "resuid")) {
389 unsigned long v;
390 if (want_numeric(value, "resuid", &v))
391 return 0;
392 sbi->s_resuid = v;
394 else if (!strcmp (this_char, "oldalloc"))
395 set_opt (sbi->s_mount_opt, OLDALLOC);
396 else if (!strcmp (this_char, "orlov"))
397 clear_opt (sbi->s_mount_opt, OLDALLOC);
398 else if (!strcmp (this_char, "nobh"))
399 set_opt(sbi->s_mount_opt, NOBH);
400 /* Silently ignore the quota options */
401 else if (!strcmp (this_char, "grpquota")
402 || !strcmp (this_char, "noquota")
403 || !strcmp (this_char, "quota")
404 || !strcmp (this_char, "usrquota"))
405 /* Don't do anything ;-) */ ;
406 else {
407 printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
408 return 0;
411 return 1;
414 static int ext2_setup_super (struct super_block * sb,
415 struct ext2_super_block * es,
416 int read_only)
418 int res = 0;
419 struct ext2_sb_info *sbi = EXT2_SB(sb);
421 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
422 printk ("EXT2-fs warning: revision level too high, "
423 "forcing read-only mode\n");
424 res = MS_RDONLY;
426 if (read_only)
427 return res;
428 if (!(sbi->s_mount_state & EXT2_VALID_FS))
429 printk ("EXT2-fs warning: mounting unchecked fs, "
430 "running e2fsck is recommended\n");
431 else if ((sbi->s_mount_state & EXT2_ERROR_FS))
432 printk ("EXT2-fs warning: mounting fs with errors, "
433 "running e2fsck is recommended\n");
434 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
435 le16_to_cpu(es->s_mnt_count) >=
436 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
437 printk ("EXT2-fs warning: maximal mount count reached, "
438 "running e2fsck is recommended\n");
439 else if (le32_to_cpu(es->s_checkinterval) &&
440 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
441 printk ("EXT2-fs warning: checktime reached, "
442 "running e2fsck is recommended\n");
443 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
444 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
445 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
446 ext2_write_super(sb);
447 if (test_opt (sb, DEBUG))
448 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
449 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
450 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
451 sbi->s_frag_size,
452 sbi->s_groups_count,
453 EXT2_BLOCKS_PER_GROUP(sb),
454 EXT2_INODES_PER_GROUP(sb),
455 sbi->s_mount_opt);
456 #ifdef CONFIG_EXT2_CHECK
457 if (test_opt (sb, CHECK)) {
458 ext2_check_blocks_bitmap (sb);
459 ext2_check_inodes_bitmap (sb);
461 #endif
462 return res;
465 static int ext2_check_descriptors (struct super_block * sb)
467 int i;
468 int desc_block = 0;
469 struct ext2_sb_info *sbi = EXT2_SB(sb);
470 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
471 struct ext2_group_desc * gdp = NULL;
473 ext2_debug ("Checking group descriptors");
475 for (i = 0; i < sbi->s_groups_count; i++)
477 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
478 gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
479 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
480 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
482 ext2_error (sb, "ext2_check_descriptors",
483 "Block bitmap for group %d"
484 " not in group (block %lu)!",
485 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
486 return 0;
488 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
489 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
491 ext2_error (sb, "ext2_check_descriptors",
492 "Inode bitmap for group %d"
493 " not in group (block %lu)!",
494 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
495 return 0;
497 if (le32_to_cpu(gdp->bg_inode_table) < block ||
498 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
499 block + EXT2_BLOCKS_PER_GROUP(sb))
501 ext2_error (sb, "ext2_check_descriptors",
502 "Inode table for group %d"
503 " not in group (block %lu)!",
504 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
505 return 0;
507 block += EXT2_BLOCKS_PER_GROUP(sb);
508 gdp++;
510 return 1;
513 #define log2(n) ffz(~(n))
516 * Maximal file size. There is a direct, and {,double-,triple-}indirect
517 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
518 * We need to be 1 filesystem block less than the 2^32 sector limit.
520 static loff_t ext2_max_size(int bits)
522 loff_t res = EXT2_NDIR_BLOCKS;
523 res += 1LL << (bits-2);
524 res += 1LL << (2*(bits-2));
525 res += 1LL << (3*(bits-2));
526 res <<= bits;
527 if (res > (512LL << 32) - (1 << bits))
528 res = (512LL << 32) - (1 << bits);
529 return res;
532 static unsigned long descriptor_loc(struct super_block *sb,
533 unsigned long logic_sb_block,
534 int nr)
536 struct ext2_sb_info *sbi = EXT2_SB(sb);
537 unsigned long bg, first_data_block, first_meta_bg;
538 int has_super = 0;
540 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
541 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
543 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
544 nr < first_meta_bg)
545 return (logic_sb_block + nr + 1);
546 bg = sbi->s_desc_per_block * nr;
547 if (ext2_bg_has_super(sb, bg))
548 has_super = 1;
549 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
552 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
554 struct buffer_head * bh;
555 struct ext2_sb_info * sbi;
556 struct ext2_super_block * es;
557 unsigned long block, sb_block = 1;
558 unsigned long logic_sb_block = get_sb_block(&data);
559 unsigned long offset = 0;
560 unsigned long def_mount_opts;
561 int blocksize = BLOCK_SIZE;
562 int db_count;
563 int i, j;
565 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
566 if (!sbi)
567 return -ENOMEM;
568 sb->s_fs_info = sbi;
569 memset(sbi, 0, sizeof(*sbi));
572 * See what the current blocksize for the device is, and
573 * use that as the blocksize. Otherwise (or if the blocksize
574 * is smaller than the default) use the default.
575 * This is important for devices that have a hardware
576 * sectorsize that is larger than the default.
578 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
579 if (!blocksize) {
580 printk ("EXT2-fs: unable to set blocksize\n");
581 goto failed_sbi;
585 * If the superblock doesn't start on a hardware sector boundary,
586 * calculate the offset.
588 if (blocksize != BLOCK_SIZE) {
589 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
590 offset = (sb_block*BLOCK_SIZE) % blocksize;
593 if (!(bh = sb_bread(sb, logic_sb_block))) {
594 printk ("EXT2-fs: unable to read superblock\n");
595 goto failed_sbi;
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);
602 sbi->s_es = es;
603 sb->s_magic = le16_to_cpu(es->s_magic);
604 sb->s_flags |= MS_ONE_SECOND;
605 if (sb->s_magic != EXT2_SUPER_MAGIC) {
606 if (!silent)
607 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
608 sb->s_id);
609 goto failed_mount;
612 /* Set defaults before we parse the mount options */
613 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
614 if (def_mount_opts & EXT2_DEFM_DEBUG)
615 set_opt(sbi->s_mount_opt, DEBUG);
616 if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
617 set_opt(sbi->s_mount_opt, GRPID);
618 if (def_mount_opts & EXT2_DEFM_UID16)
619 set_opt(sbi->s_mount_opt, NO_UID32);
620 if (def_mount_opts & EXT2_DEFM_XATTR_USER)
621 set_opt(sbi->s_mount_opt, XATTR_USER);
622 if (def_mount_opts & EXT2_DEFM_ACL)
623 set_opt(sbi->s_mount_opt, POSIX_ACL);
625 if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
626 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
627 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
628 set_opt(sbi->s_mount_opt, ERRORS_RO);
630 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
631 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
633 if (!parse_options ((char *) data, sbi))
634 goto failed_mount;
636 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
637 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
638 MS_POSIXACL : 0);
640 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
641 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
642 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
643 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
644 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
645 "running e2fsck is recommended\n");
647 * Check feature flags regardless of the revision level, since we
648 * previously didn't change the revision level when setting the flags,
649 * so there is a chance incompat flags are set on a rev 0 filesystem.
651 if ((i = 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, i);
655 goto failed_mount;
657 if (!(sb->s_flags & MS_RDONLY) &&
658 (i = 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, i);
662 goto failed_mount;
664 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
665 /* If the blocksize doesn't match, re-read the thing.. */
666 if (sb->s_blocksize != blocksize) {
667 brelse(bh);
669 if (!sb_set_blocksize(sb, blocksize)) {
670 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
671 goto failed_sbi;
674 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
675 offset = (sb_block*BLOCK_SIZE) % blocksize;
676 bh = sb_bread(sb, logic_sb_block);
677 if(!bh) {
678 printk("EXT2-fs: Couldn't read superblock on "
679 "2nd try.\n");
680 goto failed_sbi;
682 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
683 sbi->s_es = es;
684 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
685 printk ("EXT2-fs: Magic mismatch, very weird !\n");
686 goto failed_mount;
690 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
692 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
693 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
694 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
695 } else {
696 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
697 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
698 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
699 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
700 (sbi->s_inode_size > blocksize)) {
701 printk ("EXT2-fs: unsupported inode size: %d\n",
702 sbi->s_inode_size);
703 goto failed_mount;
706 sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
707 le32_to_cpu(es->s_log_frag_size);
708 if (sbi->s_frag_size)
709 sbi->s_frags_per_block = sb->s_blocksize /
710 sbi->s_frag_size;
711 else
712 sb->s_magic = 0;
713 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
714 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
715 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
716 sbi->s_inodes_per_block = sb->s_blocksize /
717 EXT2_INODE_SIZE(sb);
718 sbi->s_itb_per_group = sbi->s_inodes_per_group /
719 sbi->s_inodes_per_block;
720 sbi->s_desc_per_block = sb->s_blocksize /
721 sizeof (struct ext2_group_desc);
722 sbi->s_sbh = bh;
723 sbi->s_mount_state = le16_to_cpu(es->s_state);
724 sbi->s_addr_per_block_bits =
725 log2 (EXT2_ADDR_PER_BLOCK(sb));
726 sbi->s_desc_per_block_bits =
727 log2 (EXT2_DESC_PER_BLOCK(sb));
728 if (sb->s_magic != EXT2_SUPER_MAGIC) {
729 if (!silent)
730 printk ("VFS: Can't find an ext2 filesystem on dev "
731 "%s.\n",
732 sb->s_id);
733 goto failed_mount;
735 if (sb->s_blocksize != bh->b_size) {
736 if (!silent)
737 printk ("VFS: Unsupported blocksize on dev "
738 "%s.\n", sb->s_id);
739 goto failed_mount;
742 if (sb->s_blocksize != sbi->s_frag_size) {
743 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
744 sbi->s_frag_size, sb->s_blocksize);
745 goto failed_mount;
748 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
749 printk ("EXT2-fs: #blocks per group too big: %lu\n",
750 sbi->s_blocks_per_group);
751 goto failed_mount;
753 if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
754 printk ("EXT2-fs: #fragments per group too big: %lu\n",
755 sbi->s_frags_per_group);
756 goto failed_mount;
758 if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
759 printk ("EXT2-fs: #inodes per group too big: %lu\n",
760 sbi->s_inodes_per_group);
761 goto failed_mount;
764 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
765 le32_to_cpu(es->s_first_data_block) +
766 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
767 EXT2_BLOCKS_PER_GROUP(sb);
768 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
769 EXT2_DESC_PER_BLOCK(sb);
770 sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
771 if (sbi->s_group_desc == NULL) {
772 printk ("EXT2-fs: not enough memory\n");
773 goto failed_mount;
775 percpu_counter_init(&sbi->s_freeblocks_counter);
776 percpu_counter_init(&sbi->s_freeinodes_counter);
777 percpu_counter_init(&sbi->s_dirs_counter);
778 bgl_lock_init(&sbi->s_blockgroup_lock);
779 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
780 GFP_KERNEL);
781 if (!sbi->s_debts) {
782 printk ("EXT2-fs: not enough memory\n");
783 goto failed_mount_group_desc;
785 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
786 for (i = 0; i < db_count; i++) {
787 block = descriptor_loc(sb, logic_sb_block, i);
788 sbi->s_group_desc[i] = sb_bread(sb, block);
789 if (!sbi->s_group_desc[i]) {
790 for (j = 0; j < i; j++)
791 brelse (sbi->s_group_desc[j]);
792 printk ("EXT2-fs: unable to read group descriptors\n");
793 goto failed_mount_group_desc;
796 if (!ext2_check_descriptors (sb)) {
797 printk ("EXT2-fs: group descriptors corrupted!\n");
798 db_count = i;
799 goto failed_mount2;
801 sbi->s_gdb_count = db_count;
802 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
804 * set up enough so that it can read an inode
806 sb->s_op = &ext2_sops;
807 sb->s_export_op = &ext2_export_ops;
808 sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
809 if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
810 !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
811 if (sb->s_root) {
812 dput(sb->s_root);
813 sb->s_root = NULL;
814 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
815 } else
816 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
817 goto failed_mount2;
819 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
820 ext2_warning(sb, __FUNCTION__,
821 "mounting ext3 filesystem as ext2\n");
822 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
823 percpu_counter_mod(&sbi->s_freeblocks_counter,
824 ext2_count_free_blocks(sb));
825 percpu_counter_mod(&sbi->s_freeinodes_counter,
826 ext2_count_free_inodes(sb));
827 percpu_counter_mod(&sbi->s_dirs_counter,
828 ext2_count_dirs(sb));
829 return 0;
830 failed_mount2:
831 for (i = 0; i < db_count; i++)
832 brelse(sbi->s_group_desc[i]);
833 failed_mount_group_desc:
834 kfree(sbi->s_group_desc);
835 if (sbi->s_debts)
836 kfree(sbi->s_debts);
837 failed_mount:
838 brelse(bh);
839 failed_sbi:
840 sb->s_fs_info = NULL;
841 kfree(sbi);
842 return -EINVAL;
845 static void ext2_commit_super (struct super_block * sb,
846 struct ext2_super_block * es)
848 es->s_wtime = cpu_to_le32(get_seconds());
849 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
850 sb->s_dirt = 0;
853 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
855 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
856 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
857 es->s_wtime = cpu_to_le32(get_seconds());
858 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
859 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
860 sb->s_dirt = 0;
864 * In the second extended file system, it is not necessary to
865 * write the super block since we use a mapping of the
866 * disk super block in a buffer.
868 * However, this function is still used to set the fs valid
869 * flags to 0. We need to set this flag to 0 since the fs
870 * may have been checked while mounted and e2fsck may have
871 * set s_state to EXT2_VALID_FS after some corrections.
874 void ext2_write_super (struct super_block * sb)
876 struct ext2_super_block * es;
877 lock_kernel();
878 if (!(sb->s_flags & MS_RDONLY)) {
879 es = EXT2_SB(sb)->s_es;
881 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
882 ext2_debug ("setting valid to 0\n");
883 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
884 ~EXT2_VALID_FS);
885 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
886 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
887 es->s_mtime = cpu_to_le32(get_seconds());
888 ext2_sync_super(sb, es);
889 } else
890 ext2_commit_super (sb, es);
892 sb->s_dirt = 0;
893 unlock_kernel();
896 static int ext2_remount (struct super_block * sb, int * flags, char * data)
898 struct ext2_sb_info * sbi = EXT2_SB(sb);
899 struct ext2_super_block * es;
902 * Allow the "check" option to be passed as a remount option.
904 if (!parse_options (data, sbi))
905 return -EINVAL;
907 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
908 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
910 es = sbi->s_es;
911 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
912 return 0;
913 if (*flags & MS_RDONLY) {
914 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
915 !(sbi->s_mount_state & EXT2_VALID_FS))
916 return 0;
918 * OK, we are remounting a valid rw partition rdonly, so set
919 * the rdonly flag and then mark the partition as valid again.
921 es->s_state = cpu_to_le16(sbi->s_mount_state);
922 es->s_mtime = cpu_to_le32(get_seconds());
923 } else {
924 int ret;
925 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
926 ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
927 printk("EXT2-fs: %s: couldn't remount RDWR because of "
928 "unsupported optional features (%x).\n",
929 sb->s_id, ret);
930 return -EROFS;
933 * Mounting a RDONLY partition read-write, so reread and
934 * store the current valid flag. (It may have been changed
935 * by e2fsck since we originally mounted the partition.)
937 sbi->s_mount_state = le16_to_cpu(es->s_state);
938 if (!ext2_setup_super (sb, es, 0))
939 sb->s_flags &= ~MS_RDONLY;
941 ext2_sync_super(sb, es);
942 return 0;
945 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
947 struct ext2_sb_info *sbi = EXT2_SB(sb);
948 unsigned long overhead;
949 int i;
951 if (test_opt (sb, MINIX_DF))
952 overhead = 0;
953 else {
955 * Compute the overhead (FS structures)
959 * All of the blocks before first_data_block are
960 * overhead
962 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
965 * Add the overhead attributed to the superblock and
966 * block group descriptors. If the sparse superblocks
967 * feature is turned on, then not all groups have this.
969 for (i = 0; i < sbi->s_groups_count; i++)
970 overhead += ext2_bg_has_super(sb, i) +
971 ext2_bg_num_gdb(sb, i);
974 * Every block group has an inode bitmap, a block
975 * bitmap, and an inode table.
977 overhead += (sbi->s_groups_count *
978 (2 + sbi->s_itb_per_group));
981 buf->f_type = EXT2_SUPER_MAGIC;
982 buf->f_bsize = sb->s_blocksize;
983 buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
984 buf->f_bfree = ext2_count_free_blocks(sb);
985 buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
986 if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
987 buf->f_bavail = 0;
988 buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
989 buf->f_ffree = ext2_count_free_inodes (sb);
990 buf->f_namelen = EXT2_NAME_LEN;
991 return 0;
994 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
995 int flags, const char *dev_name, void *data)
997 return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1000 static struct file_system_type ext2_fs_type = {
1001 .owner = THIS_MODULE,
1002 .name = "ext2",
1003 .get_sb = ext2_get_sb,
1004 .kill_sb = kill_block_super,
1005 .fs_flags = FS_REQUIRES_DEV,
1008 static int __init init_ext2_fs(void)
1010 int err = init_ext2_xattr();
1011 if (err)
1012 return err;
1013 err = init_inodecache();
1014 if (err)
1015 goto out1;
1016 err = register_filesystem(&ext2_fs_type);
1017 if (err)
1018 goto out;
1019 return 0;
1020 out:
1021 destroy_inodecache();
1022 out1:
1023 exit_ext2_xattr();
1024 return err;
1027 static void __exit exit_ext2_fs(void)
1029 unregister_filesystem(&ext2_fs_type);
1030 destroy_inodecache();
1031 exit_ext2_xattr();
1034 module_init(init_ext2_fs)
1035 module_exit(exit_ext2_fs)