MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / ext2 / super.c
blobad3face04fe42ebfcd6b7b0ffa27137d1b5205f8
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/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>
31 #include "ext2.h"
32 #include "xattr.h"
33 #include "acl.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, ...)
43 va_list args;
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;
49 es->s_state =
50 cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
51 ext2_sync_super(sb, es);
54 va_start(args, fmt);
55 printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
56 vprintk(fmt, args);
57 printk("\n");
58 va_end(args);
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, ...)
71 va_list args;
73 va_start(args, fmt);
74 printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
75 sb->s_id, function);
76 vprintk(fmt, args);
77 printk("\n");
78 va_end(args);
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)
86 return;
88 ext2_warning(sb, __FUNCTION__,
89 "updating to rev %d because of new feature flag, "
90 "running e2fsck is recommended",
91 EXT2_DYNAMIC_REV);
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)
108 int db_count;
109 int i;
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);
124 kfree(sbi->s_debts);
125 brelse (sbi->s_sbh);
126 sb->s_fs_info = NULL;
127 kfree(sbi);
129 return;
132 static kmem_cache_t * ext2_inode_cachep;
134 static struct inode *ext2_alloc_inode(struct super_block *sb)
136 struct ext2_inode_info *ei;
137 ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
138 if (!ei)
139 return NULL;
140 #ifdef CONFIG_EXT2_FS_POSIX_ACL
141 ei->i_acl = EXT2_ACL_NOT_CACHED;
142 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
143 #endif
144 ei->vfs_inode.i_version = 1;
145 return &ei->vfs_inode;
148 static void ext2_destroy_inode(struct inode *inode)
150 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
153 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
155 struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
157 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
158 SLAB_CTOR_CONSTRUCTOR) {
159 rwlock_init(&ei->i_meta_lock);
160 #ifdef CONFIG_EXT2_FS_XATTR
161 init_rwsem(&ei->xattr_sem);
162 #endif
163 inode_init_once(&ei->vfs_inode);
167 static int init_inodecache(void)
169 ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
170 sizeof(struct ext2_inode_info),
171 0, SLAB_RECLAIM_ACCOUNT,
172 init_once, NULL);
173 if (ext2_inode_cachep == NULL)
174 return -ENOMEM;
175 return 0;
178 static void destroy_inodecache(void)
180 if (kmem_cache_destroy(ext2_inode_cachep))
181 printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
184 #ifdef CONFIG_EXT2_FS_POSIX_ACL
186 static void ext2_clear_inode(struct inode *inode)
188 struct ext2_inode_info *ei = EXT2_I(inode);
190 if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
191 posix_acl_release(ei->i_acl);
192 ei->i_acl = EXT2_ACL_NOT_CACHED;
194 if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
195 posix_acl_release(ei->i_default_acl);
196 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
200 #else
201 # define ext2_clear_inode NULL
202 #endif
204 static struct super_operations ext2_sops = {
205 .alloc_inode = ext2_alloc_inode,
206 .destroy_inode = ext2_destroy_inode,
207 .read_inode = ext2_read_inode,
208 .write_inode = ext2_write_inode,
209 .put_inode = ext2_put_inode,
210 .delete_inode = ext2_delete_inode,
211 .put_super = ext2_put_super,
212 .write_super = ext2_write_super,
213 .statfs = ext2_statfs,
214 .remount_fs = ext2_remount,
215 .clear_inode = ext2_clear_inode,
218 /* Yes, most of these are left as NULL!!
219 * A NULL value implies the default, which works with ext2-like file
220 * systems, but can be improved upon.
221 * Currently only get_parent is required.
223 struct dentry *ext2_get_parent(struct dentry *child);
224 static struct export_operations ext2_export_ops = {
225 .get_parent = ext2_get_parent,
228 static unsigned long get_sb_block(void **data)
230 unsigned long sb_block;
231 char *options = (char *) *data;
233 if (!options || strncmp(options, "sb=", 3) != 0)
234 return 1; /* Default location */
235 options += 3;
236 sb_block = simple_strtoul(options, &options, 0);
237 if (*options && *options != ',') {
238 printk("EXT2-fs: Invalid sb specification: %s\n",
239 (char *) *data);
240 return 1;
242 if (*options == ',')
243 options++;
244 *data = (void *) options;
245 return sb_block;
248 enum {
249 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
250 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
251 Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov, Opt_nobh,
252 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
253 Opt_ignore, Opt_err,
256 static match_table_t tokens = {
257 {Opt_bsd_df, "bsddf"},
258 {Opt_minix_df, "minixdf"},
259 {Opt_grpid, "grpid"},
260 {Opt_grpid, "bsdgroups"},
261 {Opt_nogrpid, "nogrpid"},
262 {Opt_nogrpid, "sysvgroups"},
263 {Opt_resgid, "resgid=%u"},
264 {Opt_resuid, "resuid=%u"},
265 {Opt_sb, "sb=%u"},
266 {Opt_err_cont, "errors=continue"},
267 {Opt_err_panic, "errors=panic"},
268 {Opt_err_ro, "errors=remount-ro"},
269 {Opt_nouid32, "nouid32"},
270 {Opt_nocheck, "check=none"},
271 {Opt_nocheck, "nocheck"},
272 {Opt_check, "check"},
273 {Opt_debug, "debug"},
274 {Opt_oldalloc, "oldalloc"},
275 {Opt_orlov, "orlov"},
276 {Opt_nobh, "nobh"},
277 {Opt_user_xattr, "user_xattr"},
278 {Opt_nouser_xattr, "nouser_xattr"},
279 {Opt_acl, "acl"},
280 {Opt_noacl, "noacl"},
281 {Opt_ignore, "grpquota"},
282 {Opt_ignore, "noquota"},
283 {Opt_ignore, "quota"},
284 {Opt_ignore, "usrquota"},
285 {Opt_err, NULL}
288 static int parse_options (char * options,
289 struct ext2_sb_info *sbi)
291 char * p;
292 substring_t args[MAX_OPT_ARGS];
293 unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
294 int option;
296 if (!options)
297 return 1;
299 while ((p = strsep (&options, ",")) != NULL) {
300 int token;
301 if (!*p)
302 continue;
304 token = match_token(p, tokens, args);
305 switch (token) {
306 case Opt_bsd_df:
307 clear_opt (sbi->s_mount_opt, MINIX_DF);
308 break;
309 case Opt_minix_df:
310 set_opt (sbi->s_mount_opt, MINIX_DF);
311 break;
312 case Opt_grpid:
313 set_opt (sbi->s_mount_opt, GRPID);
314 break;
315 case Opt_nogrpid:
316 clear_opt (sbi->s_mount_opt, GRPID);
317 break;
318 case Opt_resuid:
319 if (match_int(&args[0], &option))
320 return 0;
321 sbi->s_resuid = option;
322 break;
323 case Opt_resgid:
324 if (match_int(&args[0], &option))
325 return 0;
326 sbi->s_resgid = option;
327 break;
328 case Opt_sb:
329 /* handled by get_sb_block() instead of here */
330 /* *sb_block = match_int(&args[0]); */
331 break;
332 case Opt_err_panic:
333 kind = EXT2_MOUNT_ERRORS_PANIC;
334 break;
335 case Opt_err_ro:
336 kind = EXT2_MOUNT_ERRORS_RO;
337 break;
338 case Opt_err_cont:
339 kind = EXT2_MOUNT_ERRORS_CONT;
340 break;
341 case Opt_nouid32:
342 set_opt (sbi->s_mount_opt, NO_UID32);
343 break;
344 case Opt_check:
345 #ifdef CONFIG_EXT2_CHECK
346 set_opt (sbi->s_mount_opt, CHECK);
347 #else
348 printk("EXT2 Check option not supported\n");
349 #endif
350 break;
351 case Opt_nocheck:
352 clear_opt (sbi->s_mount_opt, CHECK);
353 break;
354 case Opt_debug:
355 set_opt (sbi->s_mount_opt, DEBUG);
356 break;
357 case Opt_oldalloc:
358 set_opt (sbi->s_mount_opt, OLDALLOC);
359 break;
360 case Opt_orlov:
361 clear_opt (sbi->s_mount_opt, OLDALLOC);
362 break;
363 case Opt_nobh:
364 set_opt (sbi->s_mount_opt, NOBH);
365 break;
366 #ifdef CONFIG_EXT2_FS_XATTR
367 case Opt_user_xattr:
368 set_opt (sbi->s_mount_opt, XATTR_USER);
369 break;
370 case Opt_nouser_xattr:
371 clear_opt (sbi->s_mount_opt, XATTR_USER);
372 break;
373 #else
374 case Opt_user_xattr:
375 case Opt_nouser_xattr:
376 printk("EXT2 (no)user_xattr options not supported\n");
377 break;
378 #endif
379 #ifdef CONFIG_EXT2_FS_POSIX_ACL
380 case Opt_acl:
381 set_opt(sbi->s_mount_opt, POSIX_ACL);
382 break;
383 case Opt_noacl:
384 clear_opt(sbi->s_mount_opt, POSIX_ACL);
385 break;
386 #else
387 case Opt_acl:
388 case Opt_noacl:
389 printk("EXT2 (no)acl options not supported\n");
390 break;
391 #endif
392 case Opt_ignore:
393 break;
394 default:
395 return 0;
398 sbi->s_mount_opt |= kind;
399 return 1;
402 static int ext2_setup_super (struct super_block * sb,
403 struct ext2_super_block * es,
404 int read_only)
406 int res = 0;
407 struct ext2_sb_info *sbi = EXT2_SB(sb);
409 if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
410 printk ("EXT2-fs warning: revision level too high, "
411 "forcing read-only mode\n");
412 res = MS_RDONLY;
414 if (read_only)
415 return res;
416 if (!(sbi->s_mount_state & EXT2_VALID_FS))
417 printk ("EXT2-fs warning: mounting unchecked fs, "
418 "running e2fsck is recommended\n");
419 else if ((sbi->s_mount_state & EXT2_ERROR_FS))
420 printk ("EXT2-fs warning: mounting fs with errors, "
421 "running e2fsck is recommended\n");
422 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
423 le16_to_cpu(es->s_mnt_count) >=
424 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
425 printk ("EXT2-fs warning: maximal mount count reached, "
426 "running e2fsck is recommended\n");
427 else if (le32_to_cpu(es->s_checkinterval) &&
428 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
429 printk ("EXT2-fs warning: checktime reached, "
430 "running e2fsck is recommended\n");
431 if (!le16_to_cpu(es->s_max_mnt_count))
432 es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
433 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
434 ext2_write_super(sb);
435 if (test_opt (sb, DEBUG))
436 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
437 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
438 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
439 sbi->s_frag_size,
440 sbi->s_groups_count,
441 EXT2_BLOCKS_PER_GROUP(sb),
442 EXT2_INODES_PER_GROUP(sb),
443 sbi->s_mount_opt);
444 #ifdef CONFIG_EXT2_CHECK
445 if (test_opt (sb, CHECK)) {
446 ext2_check_blocks_bitmap (sb);
447 ext2_check_inodes_bitmap (sb);
449 #endif
450 return res;
453 static int ext2_check_descriptors (struct super_block * sb)
455 int i;
456 int desc_block = 0;
457 struct ext2_sb_info *sbi = EXT2_SB(sb);
458 unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
459 struct ext2_group_desc * gdp = NULL;
461 ext2_debug ("Checking group descriptors");
463 for (i = 0; i < sbi->s_groups_count; i++)
465 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
466 gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
467 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
468 le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
470 ext2_error (sb, "ext2_check_descriptors",
471 "Block bitmap for group %d"
472 " not in group (block %lu)!",
473 i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
474 return 0;
476 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
477 le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
479 ext2_error (sb, "ext2_check_descriptors",
480 "Inode bitmap for group %d"
481 " not in group (block %lu)!",
482 i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
483 return 0;
485 if (le32_to_cpu(gdp->bg_inode_table) < block ||
486 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
487 block + EXT2_BLOCKS_PER_GROUP(sb))
489 ext2_error (sb, "ext2_check_descriptors",
490 "Inode table for group %d"
491 " not in group (block %lu)!",
492 i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
493 return 0;
495 block += EXT2_BLOCKS_PER_GROUP(sb);
496 gdp++;
498 return 1;
501 #define log2(n) ffz(~(n))
504 * Maximal file size. There is a direct, and {,double-,triple-}indirect
505 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
506 * We need to be 1 filesystem block less than the 2^32 sector limit.
508 static loff_t ext2_max_size(int bits)
510 loff_t res = EXT2_NDIR_BLOCKS;
511 res += 1LL << (bits-2);
512 res += 1LL << (2*(bits-2));
513 res += 1LL << (3*(bits-2));
514 res <<= bits;
515 if (res > (512LL << 32) - (1 << bits))
516 res = (512LL << 32) - (1 << bits);
517 return res;
520 static unsigned long descriptor_loc(struct super_block *sb,
521 unsigned long logic_sb_block,
522 int nr)
524 struct ext2_sb_info *sbi = EXT2_SB(sb);
525 unsigned long bg, first_data_block, first_meta_bg;
526 int has_super = 0;
528 first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
529 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
531 if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
532 nr < first_meta_bg)
533 return (logic_sb_block + nr + 1);
534 bg = sbi->s_desc_per_block * nr;
535 if (ext2_bg_has_super(sb, bg))
536 has_super = 1;
537 return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
540 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
542 struct buffer_head * bh;
543 struct ext2_sb_info * sbi;
544 struct ext2_super_block * es;
545 struct inode *root;
546 unsigned long block;
547 unsigned long sb_block = get_sb_block(&data);
548 unsigned long logic_sb_block;
549 unsigned long offset = 0;
550 unsigned long def_mount_opts;
551 int blocksize = BLOCK_SIZE;
552 int db_count;
553 int i, j;
554 __le32 features;
556 sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
557 if (!sbi)
558 return -ENOMEM;
559 sb->s_fs_info = sbi;
560 memset(sbi, 0, sizeof(*sbi));
563 * See what the current blocksize for the device is, and
564 * use that as the blocksize. Otherwise (or if the blocksize
565 * is smaller than the default) use the default.
566 * This is important for devices that have a hardware
567 * sectorsize that is larger than the default.
569 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
570 if (!blocksize) {
571 printk ("EXT2-fs: unable to set blocksize\n");
572 goto failed_sbi;
576 * If the superblock doesn't start on a hardware sector boundary,
577 * calculate the offset.
579 if (blocksize != BLOCK_SIZE) {
580 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
581 offset = (sb_block*BLOCK_SIZE) % blocksize;
582 } else {
583 logic_sb_block = sb_block;
586 if (!(bh = sb_bread(sb, logic_sb_block))) {
587 printk ("EXT2-fs: unable to read superblock\n");
588 goto failed_sbi;
591 * Note: s_es must be initialized as soon as possible because
592 * some ext2 macro-instructions depend on its value
594 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
595 sbi->s_es = es;
596 sb->s_magic = le16_to_cpu(es->s_magic);
597 sb->s_flags |= MS_ONE_SECOND;
598 if (sb->s_magic != EXT2_SUPER_MAGIC) {
599 if (!silent)
600 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
601 sb->s_id);
602 goto failed_mount;
605 /* Set defaults before we parse the mount options */
606 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
607 if (def_mount_opts & EXT2_DEFM_DEBUG)
608 set_opt(sbi->s_mount_opt, DEBUG);
609 if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
610 set_opt(sbi->s_mount_opt, GRPID);
611 if (def_mount_opts & EXT2_DEFM_UID16)
612 set_opt(sbi->s_mount_opt, NO_UID32);
613 if (def_mount_opts & EXT2_DEFM_XATTR_USER)
614 set_opt(sbi->s_mount_opt, XATTR_USER);
615 if (def_mount_opts & EXT2_DEFM_ACL)
616 set_opt(sbi->s_mount_opt, POSIX_ACL);
618 if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
619 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
620 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
621 set_opt(sbi->s_mount_opt, ERRORS_RO);
623 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
624 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
626 if (!parse_options ((char *) data, sbi))
627 goto failed_mount;
629 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
630 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
631 MS_POSIXACL : 0);
633 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
634 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
635 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
636 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
637 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
638 "running e2fsck is recommended\n");
640 * Check feature flags regardless of the revision level, since we
641 * previously didn't change the revision level when setting the flags,
642 * so there is a chance incompat flags are set on a rev 0 filesystem.
644 features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
645 if (features) {
646 printk("EXT2-fs: %s: couldn't mount because of "
647 "unsupported optional features (%x).\n",
648 sb->s_id, le32_to_cpu(features));
649 goto failed_mount;
651 if (!(sb->s_flags & MS_RDONLY) &&
652 (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
653 printk("EXT2-fs: %s: couldn't mount RDWR because of "
654 "unsupported optional features (%x).\n",
655 sb->s_id, le32_to_cpu(features));
656 goto failed_mount;
658 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
659 /* If the blocksize doesn't match, re-read the thing.. */
660 if (sb->s_blocksize != blocksize) {
661 brelse(bh);
663 if (!sb_set_blocksize(sb, blocksize)) {
664 printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
665 goto failed_sbi;
668 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
669 offset = (sb_block*BLOCK_SIZE) % blocksize;
670 bh = sb_bread(sb, logic_sb_block);
671 if(!bh) {
672 printk("EXT2-fs: Couldn't read superblock on "
673 "2nd try.\n");
674 goto failed_sbi;
676 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
677 sbi->s_es = es;
678 if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
679 printk ("EXT2-fs: Magic mismatch, very weird !\n");
680 goto failed_mount;
684 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
686 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
687 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
688 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
689 } else {
690 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
691 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
692 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
693 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
694 (sbi->s_inode_size > blocksize)) {
695 printk ("EXT2-fs: unsupported inode size: %d\n",
696 sbi->s_inode_size);
697 goto failed_mount;
700 sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
701 le32_to_cpu(es->s_log_frag_size);
702 if (sbi->s_frag_size)
703 sbi->s_frags_per_block = sb->s_blocksize /
704 sbi->s_frag_size;
705 else
706 sb->s_magic = 0;
707 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
708 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
709 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
710 sbi->s_inodes_per_block = sb->s_blocksize /
711 EXT2_INODE_SIZE(sb);
712 sbi->s_itb_per_group = sbi->s_inodes_per_group /
713 sbi->s_inodes_per_block;
714 sbi->s_desc_per_block = sb->s_blocksize /
715 sizeof (struct ext2_group_desc);
716 sbi->s_sbh = bh;
717 sbi->s_mount_state = le16_to_cpu(es->s_state);
718 sbi->s_addr_per_block_bits =
719 log2 (EXT2_ADDR_PER_BLOCK(sb));
720 sbi->s_desc_per_block_bits =
721 log2 (EXT2_DESC_PER_BLOCK(sb));
722 if (sb->s_magic != EXT2_SUPER_MAGIC) {
723 if (!silent)
724 printk ("VFS: Can't find an ext2 filesystem on dev "
725 "%s.\n",
726 sb->s_id);
727 goto failed_mount;
729 if (sb->s_blocksize != bh->b_size) {
730 if (!silent)
731 printk ("VFS: Unsupported blocksize on dev "
732 "%s.\n", sb->s_id);
733 goto failed_mount;
736 if (sb->s_blocksize != sbi->s_frag_size) {
737 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
738 sbi->s_frag_size, sb->s_blocksize);
739 goto failed_mount;
742 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
743 printk ("EXT2-fs: #blocks per group too big: %lu\n",
744 sbi->s_blocks_per_group);
745 goto failed_mount;
747 if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
748 printk ("EXT2-fs: #fragments per group too big: %lu\n",
749 sbi->s_frags_per_group);
750 goto failed_mount;
752 if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
753 printk ("EXT2-fs: #inodes per group too big: %lu\n",
754 sbi->s_inodes_per_group);
755 goto failed_mount;
758 sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
759 le32_to_cpu(es->s_first_data_block) +
760 EXT2_BLOCKS_PER_GROUP(sb) - 1) /
761 EXT2_BLOCKS_PER_GROUP(sb);
762 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
763 EXT2_DESC_PER_BLOCK(sb);
764 sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
765 if (sbi->s_group_desc == NULL) {
766 printk ("EXT2-fs: not enough memory\n");
767 goto failed_mount;
769 percpu_counter_init(&sbi->s_freeblocks_counter);
770 percpu_counter_init(&sbi->s_freeinodes_counter);
771 percpu_counter_init(&sbi->s_dirs_counter);
772 bgl_lock_init(&sbi->s_blockgroup_lock);
773 sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
774 GFP_KERNEL);
775 if (!sbi->s_debts) {
776 printk ("EXT2-fs: not enough memory\n");
777 goto failed_mount_group_desc;
779 memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
780 for (i = 0; i < db_count; i++) {
781 block = descriptor_loc(sb, logic_sb_block, i);
782 sbi->s_group_desc[i] = sb_bread(sb, block);
783 if (!sbi->s_group_desc[i]) {
784 for (j = 0; j < i; j++)
785 brelse (sbi->s_group_desc[j]);
786 printk ("EXT2-fs: unable to read group descriptors\n");
787 goto failed_mount_group_desc;
790 if (!ext2_check_descriptors (sb)) {
791 printk ("EXT2-fs: group descriptors corrupted!\n");
792 db_count = i;
793 goto failed_mount2;
795 sbi->s_gdb_count = db_count;
796 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
797 spin_lock_init(&sbi->s_next_gen_lock);
799 * set up enough so that it can read an inode
801 sb->s_op = &ext2_sops;
802 sb->s_export_op = &ext2_export_ops;
803 root = iget(sb, EXT2_ROOT_INO);
804 sb->s_root = d_alloc_root(root);
805 if (!sb->s_root) {
806 iput(root);
807 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
808 goto failed_mount2;
810 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
811 dput(sb->s_root);
812 sb->s_root = NULL;
813 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
814 goto failed_mount2;
816 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
817 ext2_warning(sb, __FUNCTION__,
818 "mounting ext3 filesystem as ext2\n");
819 ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
820 percpu_counter_mod(&sbi->s_freeblocks_counter,
821 ext2_count_free_blocks(sb));
822 percpu_counter_mod(&sbi->s_freeinodes_counter,
823 ext2_count_free_inodes(sb));
824 percpu_counter_mod(&sbi->s_dirs_counter,
825 ext2_count_dirs(sb));
826 return 0;
827 failed_mount2:
828 for (i = 0; i < db_count; i++)
829 brelse(sbi->s_group_desc[i]);
830 failed_mount_group_desc:
831 kfree(sbi->s_group_desc);
832 if (sbi->s_debts)
833 kfree(sbi->s_debts);
834 failed_mount:
835 brelse(bh);
836 failed_sbi:
837 sb->s_fs_info = NULL;
838 kfree(sbi);
839 return -EINVAL;
842 static void ext2_commit_super (struct super_block * sb,
843 struct ext2_super_block * es)
845 es->s_wtime = cpu_to_le32(get_seconds());
846 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
847 sb->s_dirt = 0;
850 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
852 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
853 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
854 es->s_wtime = cpu_to_le32(get_seconds());
855 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
856 sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
857 sb->s_dirt = 0;
861 * In the second extended file system, it is not necessary to
862 * write the super block since we use a mapping of the
863 * disk super block in a buffer.
865 * However, this function is still used to set the fs valid
866 * flags to 0. We need to set this flag to 0 since the fs
867 * may have been checked while mounted and e2fsck may have
868 * set s_state to EXT2_VALID_FS after some corrections.
871 void ext2_write_super (struct super_block * sb)
873 struct ext2_super_block * es;
874 lock_kernel();
875 if (!(sb->s_flags & MS_RDONLY)) {
876 es = EXT2_SB(sb)->s_es;
878 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
879 ext2_debug ("setting valid to 0\n");
880 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
881 ~EXT2_VALID_FS);
882 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
883 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
884 es->s_mtime = cpu_to_le32(get_seconds());
885 ext2_sync_super(sb, es);
886 } else
887 ext2_commit_super (sb, es);
889 sb->s_dirt = 0;
890 unlock_kernel();
893 static int ext2_remount (struct super_block * sb, int * flags, char * data)
895 struct ext2_sb_info * sbi = EXT2_SB(sb);
896 struct ext2_super_block * es;
899 * Allow the "check" option to be passed as a remount option.
901 if (!parse_options (data, sbi))
902 return -EINVAL;
904 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
905 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
907 es = sbi->s_es;
908 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
909 return 0;
910 if (*flags & MS_RDONLY) {
911 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
912 !(sbi->s_mount_state & EXT2_VALID_FS))
913 return 0;
915 * OK, we are remounting a valid rw partition rdonly, so set
916 * the rdonly flag and then mark the partition as valid again.
918 es->s_state = cpu_to_le16(sbi->s_mount_state);
919 es->s_mtime = cpu_to_le32(get_seconds());
920 } else {
921 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
922 ~EXT2_FEATURE_RO_COMPAT_SUPP);
923 if (ret) {
924 printk("EXT2-fs: %s: couldn't remount RDWR because of "
925 "unsupported optional features (%x).\n",
926 sb->s_id, le32_to_cpu(ret));
927 return -EROFS;
930 * Mounting a RDONLY partition read-write, so reread and
931 * store the current valid flag. (It may have been changed
932 * by e2fsck since we originally mounted the partition.)
934 sbi->s_mount_state = le16_to_cpu(es->s_state);
935 if (!ext2_setup_super (sb, es, 0))
936 sb->s_flags &= ~MS_RDONLY;
938 ext2_sync_super(sb, es);
939 return 0;
942 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
944 struct ext2_sb_info *sbi = EXT2_SB(sb);
945 unsigned long overhead;
946 int i;
948 if (test_opt (sb, MINIX_DF))
949 overhead = 0;
950 else {
952 * Compute the overhead (FS structures)
956 * All of the blocks before first_data_block are
957 * overhead
959 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
962 * Add the overhead attributed to the superblock and
963 * block group descriptors. If the sparse superblocks
964 * feature is turned on, then not all groups have this.
966 for (i = 0; i < sbi->s_groups_count; i++)
967 overhead += ext2_bg_has_super(sb, i) +
968 ext2_bg_num_gdb(sb, i);
971 * Every block group has an inode bitmap, a block
972 * bitmap, and an inode table.
974 overhead += (sbi->s_groups_count *
975 (2 + sbi->s_itb_per_group));
978 buf->f_type = EXT2_SUPER_MAGIC;
979 buf->f_bsize = sb->s_blocksize;
980 buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
981 buf->f_bfree = ext2_count_free_blocks(sb);
982 buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
983 if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
984 buf->f_bavail = 0;
985 buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
986 buf->f_ffree = ext2_count_free_inodes (sb);
987 buf->f_namelen = EXT2_NAME_LEN;
988 return 0;
991 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
992 int flags, const char *dev_name, void *data)
994 return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
997 static struct file_system_type ext2_fs_type = {
998 .owner = THIS_MODULE,
999 .name = "ext2",
1000 .get_sb = ext2_get_sb,
1001 .kill_sb = kill_block_super,
1002 .fs_flags = FS_REQUIRES_DEV,
1005 static int __init init_ext2_fs(void)
1007 int err = init_ext2_xattr();
1008 if (err)
1009 return err;
1010 err = init_inodecache();
1011 if (err)
1012 goto out1;
1013 err = register_filesystem(&ext2_fs_type);
1014 if (err)
1015 goto out;
1016 return 0;
1017 out:
1018 destroy_inodecache();
1019 out1:
1020 exit_ext2_xattr();
1021 return err;
1024 static void __exit exit_ext2_fs(void)
1026 unregister_filesystem(&ext2_fs_type);
1027 destroy_inodecache();
1028 exit_ext2_xattr();
1031 module_init(init_ext2_fs)
1032 module_exit(exit_ext2_fs)