vserver 1.9.3
[linux-2.6.git] / fs / ext2 / super.c
1 /*
2  *  linux/fs/ext2/super.c
3  *
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)
8  *
9  *  from
10  *
11  *  linux/fs/minix/inode.c
12  *
13  *  Copyright (C) 1991, 1992  Linus Torvalds
14  *
15  *  Big-endian to little-endian byte-swapping/bitmaps by
16  *        David S. Miller (davem@caip.rutgers.edu), 1995
17  */
18
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"
34
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);
39
40 void ext2_error (struct super_block * sb, const char * function,
41                  const char * fmt, ...)
42 {
43         va_list args;
44         struct ext2_sb_info *sbi = EXT2_SB(sb);
45         struct ext2_super_block *es = sbi->s_es;
46
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);
52         }
53
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);
59
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;
65         }
66 }
67
68 void ext2_warning (struct super_block * sb, const char * function,
69                    const char * fmt, ...)
70 {
71         va_list args;
72
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);
79 }
80
81 void ext2_update_dynamic_rev(struct super_block *sb)
82 {
83         struct ext2_super_block *es = EXT2_SB(sb)->s_es;
84
85         if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
86                 return;
87
88         ext2_warning(sb, __FUNCTION__,
89                      "updating to rev %d because of new feature flag, "
90                      "running e2fsck is recommended",
91                      EXT2_DYNAMIC_REV);
92
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 */
98
99         /*
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.
103          */
104 }
105
106 static void ext2_put_super (struct super_block * sb)
107 {
108         int db_count;
109         int i;
110         struct ext2_sb_info *sbi = EXT2_SB(sb);
111
112         ext2_xattr_put_super(sb);
113         if (!(sb->s_flags & MS_RDONLY)) {
114                 struct ext2_super_block *es = sbi->s_es;
115
116                 es->s_state = cpu_to_le16(sbi->s_mount_state);
117                 ext2_sync_super(sb, es);
118         }
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);
128
129         return;
130 }
131
132 static kmem_cache_t * ext2_inode_cachep;
133
134 static struct inode *ext2_alloc_inode(struct super_block *sb)
135 {
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;
146 }
147
148 static void ext2_destroy_inode(struct inode *inode)
149 {
150         kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
151 }
152
153 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
154 {
155         struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
156
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);
164         }
165 }
166  
167 static int init_inodecache(void)
168 {
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;
176 }
177
178 static void destroy_inodecache(void)
179 {
180         if (kmem_cache_destroy(ext2_inode_cachep))
181                 printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
182 }
183
184 #ifdef CONFIG_EXT2_FS_POSIX_ACL
185
186 static void ext2_clear_inode(struct inode *inode)
187 {
188         struct ext2_inode_info *ei = EXT2_I(inode);
189
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;
193         }
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;
197         }
198 }
199
200 #else
201 # define ext2_clear_inode NULL
202 #endif
203
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,
216 };
217
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.
222  */
223 struct dentry *ext2_get_parent(struct dentry *child);
224 static struct export_operations ext2_export_ops = {
225         .get_parent = ext2_get_parent,
226 };
227
228 static unsigned long get_sb_block(void **data)
229 {
230         unsigned long   sb_block;
231         char            *options = (char *) *data;
232
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;
241         }
242         if (*options == ',')
243                 options++;
244         *data = (void *) options;
245         return sb_block;
246 }
247
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, Opt_tagxid,
253         Opt_ignore, Opt_err,
254 };
255
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_tagxid, "tagxid"},
282         {Opt_ignore, "grpquota"},
283         {Opt_ignore, "noquota"},
284         {Opt_ignore, "quota"},
285         {Opt_ignore, "usrquota"},
286         {Opt_err, NULL}
287 };
288
289 static int parse_options (char * options,
290                           struct ext2_sb_info *sbi)
291 {
292         char * p;
293         substring_t args[MAX_OPT_ARGS];
294         unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
295         int option;
296
297         if (!options)
298                 return 1;
299
300         while ((p = strsep (&options, ",")) != NULL) {
301                 int token;
302                 if (!*p)
303                         continue;
304
305                 token = match_token(p, tokens, args);
306                 switch (token) {
307                 case Opt_bsd_df:
308                         clear_opt (sbi->s_mount_opt, MINIX_DF);
309                         break;
310                 case Opt_minix_df:
311                         set_opt (sbi->s_mount_opt, MINIX_DF);
312                         break;
313                 case Opt_grpid:
314                         set_opt (sbi->s_mount_opt, GRPID);
315                         break;
316                 case Opt_nogrpid:
317                         clear_opt (sbi->s_mount_opt, GRPID);
318                         break;
319                 case Opt_resuid:
320                         if (match_int(&args[0], &option))
321                                 return 0;
322                         sbi->s_resuid = option;
323                         break;
324                 case Opt_resgid:
325                         if (match_int(&args[0], &option))
326                                 return 0;
327                         sbi->s_resgid = option;
328                         break;
329                 case Opt_sb:
330                         /* handled by get_sb_block() instead of here */
331                         /* *sb_block = match_int(&args[0]); */
332                         break;
333                 case Opt_err_panic:
334                         kind = EXT2_MOUNT_ERRORS_PANIC;
335                         break;
336                 case Opt_err_ro:
337                         kind = EXT2_MOUNT_ERRORS_RO;
338                         break;
339                 case Opt_err_cont:
340                         kind = EXT2_MOUNT_ERRORS_CONT;
341                         break;
342                 case Opt_nouid32:
343                         set_opt (sbi->s_mount_opt, NO_UID32);
344                         break;
345 #ifndef CONFIG_INOXID_NONE
346                 case Opt_tagxid:
347                         set_opt (sbi->s_mount_opt, TAG_XID);
348                         break;
349 #endif
350                 case Opt_check:
351 #ifdef CONFIG_EXT2_CHECK
352                         set_opt (sbi->s_mount_opt, CHECK);
353 #else
354                         printk("EXT2 Check option not supported\n");
355 #endif
356                         break;
357                 case Opt_nocheck:
358                         clear_opt (sbi->s_mount_opt, CHECK);
359                         break;
360                 case Opt_debug:
361                         set_opt (sbi->s_mount_opt, DEBUG);
362                         break;
363                 case Opt_oldalloc:
364                         set_opt (sbi->s_mount_opt, OLDALLOC);
365                         break;
366                 case Opt_orlov:
367                         clear_opt (sbi->s_mount_opt, OLDALLOC);
368                         break;
369                 case Opt_nobh:
370                         set_opt (sbi->s_mount_opt, NOBH);
371                         break;
372 #ifdef CONFIG_EXT2_FS_XATTR
373                 case Opt_user_xattr:
374                         set_opt (sbi->s_mount_opt, XATTR_USER);
375                         break;
376                 case Opt_nouser_xattr:
377                         clear_opt (sbi->s_mount_opt, XATTR_USER);
378                         break;
379 #else
380                 case Opt_user_xattr:
381                 case Opt_nouser_xattr:
382                         printk("EXT2 (no)user_xattr options not supported\n");
383                         break;
384 #endif
385 #ifdef CONFIG_EXT2_FS_POSIX_ACL
386                 case Opt_acl:
387                         set_opt(sbi->s_mount_opt, POSIX_ACL);
388                         break;
389                 case Opt_noacl:
390                         clear_opt(sbi->s_mount_opt, POSIX_ACL);
391                         break;
392 #else
393                 case Opt_acl:
394                 case Opt_noacl:
395                         printk("EXT2 (no)acl options not supported\n");
396                         break;
397 #endif
398                 case Opt_ignore:
399                         break;
400                 default:
401                         return 0;
402                 }
403         }
404         sbi->s_mount_opt |= kind;
405         return 1;
406 }
407
408 static int ext2_setup_super (struct super_block * sb,
409                               struct ext2_super_block * es,
410                               int read_only)
411 {
412         int res = 0;
413         struct ext2_sb_info *sbi = EXT2_SB(sb);
414
415         if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
416                 printk ("EXT2-fs warning: revision level too high, "
417                         "forcing read-only mode\n");
418                 res = MS_RDONLY;
419         }
420         if (read_only)
421                 return res;
422         if (!(sbi->s_mount_state & EXT2_VALID_FS))
423                 printk ("EXT2-fs warning: mounting unchecked fs, "
424                         "running e2fsck is recommended\n");
425         else if ((sbi->s_mount_state & EXT2_ERROR_FS))
426                 printk ("EXT2-fs warning: mounting fs with errors, "
427                         "running e2fsck is recommended\n");
428         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
429                  le16_to_cpu(es->s_mnt_count) >=
430                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
431                 printk ("EXT2-fs warning: maximal mount count reached, "
432                         "running e2fsck is recommended\n");
433         else if (le32_to_cpu(es->s_checkinterval) &&
434                 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
435                 printk ("EXT2-fs warning: checktime reached, "
436                         "running e2fsck is recommended\n");
437         if (!le16_to_cpu(es->s_max_mnt_count))
438                 es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
439         es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
440         ext2_write_super(sb);
441         if (test_opt (sb, DEBUG))
442                 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
443                         "bpg=%lu, ipg=%lu, mo=%04lx]\n",
444                         EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
445                         sbi->s_frag_size,
446                         sbi->s_groups_count,
447                         EXT2_BLOCKS_PER_GROUP(sb),
448                         EXT2_INODES_PER_GROUP(sb),
449                         sbi->s_mount_opt);
450 #ifdef CONFIG_EXT2_CHECK
451         if (test_opt (sb, CHECK)) {
452                 ext2_check_blocks_bitmap (sb);
453                 ext2_check_inodes_bitmap (sb);
454         }
455 #endif
456         return res;
457 }
458
459 static int ext2_check_descriptors (struct super_block * sb)
460 {
461         int i;
462         int desc_block = 0;
463         struct ext2_sb_info *sbi = EXT2_SB(sb);
464         unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
465         struct ext2_group_desc * gdp = NULL;
466
467         ext2_debug ("Checking group descriptors");
468
469         for (i = 0; i < sbi->s_groups_count; i++)
470         {
471                 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
472                         gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
473                 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
474                     le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
475                 {
476                         ext2_error (sb, "ext2_check_descriptors",
477                                     "Block bitmap for group %d"
478                                     " not in group (block %lu)!",
479                                     i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
480                         return 0;
481                 }
482                 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
483                     le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
484                 {
485                         ext2_error (sb, "ext2_check_descriptors",
486                                     "Inode bitmap for group %d"
487                                     " not in group (block %lu)!",
488                                     i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
489                         return 0;
490                 }
491                 if (le32_to_cpu(gdp->bg_inode_table) < block ||
492                     le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
493                     block + EXT2_BLOCKS_PER_GROUP(sb))
494                 {
495                         ext2_error (sb, "ext2_check_descriptors",
496                                     "Inode table for group %d"
497                                     " not in group (block %lu)!",
498                                     i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
499                         return 0;
500                 }
501                 block += EXT2_BLOCKS_PER_GROUP(sb);
502                 gdp++;
503         }
504         return 1;
505 }
506
507 #define log2(n) ffz(~(n))
508  
509 /*
510  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
511  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
512  * We need to be 1 filesystem block less than the 2^32 sector limit.
513  */
514 static loff_t ext2_max_size(int bits)
515 {
516         loff_t res = EXT2_NDIR_BLOCKS;
517         res += 1LL << (bits-2);
518         res += 1LL << (2*(bits-2));
519         res += 1LL << (3*(bits-2));
520         res <<= bits;
521         if (res > (512LL << 32) - (1 << bits))
522                 res = (512LL << 32) - (1 << bits);
523         return res;
524 }
525
526 static unsigned long descriptor_loc(struct super_block *sb,
527                                     unsigned long logic_sb_block,
528                                     int nr)
529 {
530         struct ext2_sb_info *sbi = EXT2_SB(sb);
531         unsigned long bg, first_data_block, first_meta_bg;
532         int has_super = 0;
533         
534         first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
535         first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
536
537         if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
538             nr < first_meta_bg)
539                 return (logic_sb_block + nr + 1);
540         bg = sbi->s_desc_per_block * nr;
541         if (ext2_bg_has_super(sb, bg))
542                 has_super = 1;
543         return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
544 }
545
546 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
547 {
548         struct buffer_head * bh;
549         struct ext2_sb_info * sbi;
550         struct ext2_super_block * es;
551         struct inode *root;
552         unsigned long block;
553         unsigned long sb_block = get_sb_block(&data);
554         unsigned long logic_sb_block;
555         unsigned long offset = 0;
556         unsigned long def_mount_opts;
557         int blocksize = BLOCK_SIZE;
558         int db_count;
559         int i, j;
560         __le32 features;
561
562         sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
563         if (!sbi)
564                 return -ENOMEM;
565         sb->s_fs_info = sbi;
566         memset(sbi, 0, sizeof(*sbi));
567
568         /*
569          * See what the current blocksize for the device is, and
570          * use that as the blocksize.  Otherwise (or if the blocksize
571          * is smaller than the default) use the default.
572          * This is important for devices that have a hardware
573          * sectorsize that is larger than the default.
574          */
575         blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
576         if (!blocksize) {
577                 printk ("EXT2-fs: unable to set blocksize\n");
578                 goto failed_sbi;
579         }
580
581         /*
582          * If the superblock doesn't start on a hardware sector boundary,
583          * calculate the offset.  
584          */
585         if (blocksize != BLOCK_SIZE) {
586                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
587                 offset = (sb_block*BLOCK_SIZE) % blocksize;
588         } else {
589                 logic_sb_block = sb_block;
590         }
591
592         if (!(bh = sb_bread(sb, logic_sb_block))) {
593                 printk ("EXT2-fs: unable to read superblock\n");
594                 goto failed_sbi;
595         }
596         /*
597          * Note: s_es must be initialized as soon as possible because
598          *       some ext2 macro-instructions depend on its value
599          */
600         es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
601         sbi->s_es = es;
602         sb->s_magic = le16_to_cpu(es->s_magic);
603         sb->s_flags |= MS_ONE_SECOND;
604         if (sb->s_magic != EXT2_SUPER_MAGIC) {
605                 if (!silent)
606                         printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
607                                 sb->s_id);
608                 goto failed_mount;
609         }
610
611         /* Set defaults before we parse the mount options */
612         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
613         if (def_mount_opts & EXT2_DEFM_DEBUG)
614                 set_opt(sbi->s_mount_opt, DEBUG);
615         if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
616                 set_opt(sbi->s_mount_opt, GRPID);
617         if (def_mount_opts & EXT2_DEFM_UID16)
618                 set_opt(sbi->s_mount_opt, NO_UID32);
619         if (def_mount_opts & EXT2_DEFM_XATTR_USER)
620                 set_opt(sbi->s_mount_opt, XATTR_USER);
621         if (def_mount_opts & EXT2_DEFM_ACL)
622                 set_opt(sbi->s_mount_opt, POSIX_ACL);
623         
624         if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
625                 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
626         else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
627                 set_opt(sbi->s_mount_opt, ERRORS_RO);
628
629         sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
630         sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
631         
632         if (!parse_options ((char *) data, sbi))
633                 goto failed_mount;
634
635         if (EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_TAG_XID)
636                 sb->s_flags |= MS_TAGXID;
637         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
638                 ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
639                  MS_POSIXACL : 0);
640
641         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
642             (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
643              EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
644              EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
645                 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
646                        "running e2fsck is recommended\n");
647         /*
648          * Check feature flags regardless of the revision level, since we
649          * previously didn't change the revision level when setting the flags,
650          * so there is a chance incompat flags are set on a rev 0 filesystem.
651          */
652         features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
653         if (features) {
654                 printk("EXT2-fs: %s: couldn't mount because of "
655                        "unsupported optional features (%x).\n",
656                        sb->s_id, le32_to_cpu(features));
657                 goto failed_mount;
658         }
659         if (!(sb->s_flags & MS_RDONLY) &&
660             (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
661                 printk("EXT2-fs: %s: couldn't mount RDWR because of "
662                        "unsupported optional features (%x).\n",
663                        sb->s_id, le32_to_cpu(features));
664                 goto failed_mount;
665         }
666         blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
667         /* If the blocksize doesn't match, re-read the thing.. */
668         if (sb->s_blocksize != blocksize) {
669                 brelse(bh);
670
671                 if (!sb_set_blocksize(sb, blocksize)) {
672                         printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
673                         goto failed_sbi;
674                 }
675
676                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
677                 offset = (sb_block*BLOCK_SIZE) % blocksize;
678                 bh = sb_bread(sb, logic_sb_block);
679                 if(!bh) {
680                         printk("EXT2-fs: Couldn't read superblock on "
681                                "2nd try.\n");
682                         goto failed_sbi;
683                 }
684                 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
685                 sbi->s_es = es;
686                 if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
687                         printk ("EXT2-fs: Magic mismatch, very weird !\n");
688                         goto failed_mount;
689                 }
690         }
691
692         sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
693
694         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
695                 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
696                 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
697         } else {
698                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
699                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
700                 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
701                     (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
702                     (sbi->s_inode_size > blocksize)) {
703                         printk ("EXT2-fs: unsupported inode size: %d\n",
704                                 sbi->s_inode_size);
705                         goto failed_mount;
706                 }
707         }
708         sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
709                                    le32_to_cpu(es->s_log_frag_size);
710         if (sbi->s_frag_size)
711                 sbi->s_frags_per_block = sb->s_blocksize /
712                                                   sbi->s_frag_size;
713         else
714                 sb->s_magic = 0;
715         sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
716         sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
717         sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
718         sbi->s_inodes_per_block = sb->s_blocksize /
719                                            EXT2_INODE_SIZE(sb);
720         sbi->s_itb_per_group = sbi->s_inodes_per_group /
721                                         sbi->s_inodes_per_block;
722         sbi->s_desc_per_block = sb->s_blocksize /
723                                          sizeof (struct ext2_group_desc);
724         sbi->s_sbh = bh;
725         sbi->s_mount_state = le16_to_cpu(es->s_state);
726         sbi->s_addr_per_block_bits =
727                 log2 (EXT2_ADDR_PER_BLOCK(sb));
728         sbi->s_desc_per_block_bits =
729                 log2 (EXT2_DESC_PER_BLOCK(sb));
730         if (sb->s_magic != EXT2_SUPER_MAGIC) {
731                 if (!silent)
732                         printk ("VFS: Can't find an ext2 filesystem on dev "
733                                 "%s.\n",
734                                 sb->s_id);
735                 goto failed_mount;
736         }
737         if (sb->s_blocksize != bh->b_size) {
738                 if (!silent)
739                         printk ("VFS: Unsupported blocksize on dev "
740                                 "%s.\n", sb->s_id);
741                 goto failed_mount;
742         }
743
744         if (sb->s_blocksize != sbi->s_frag_size) {
745                 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
746                         sbi->s_frag_size, sb->s_blocksize);
747                 goto failed_mount;
748         }
749
750         if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
751                 printk ("EXT2-fs: #blocks per group too big: %lu\n",
752                         sbi->s_blocks_per_group);
753                 goto failed_mount;
754         }
755         if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
756                 printk ("EXT2-fs: #fragments per group too big: %lu\n",
757                         sbi->s_frags_per_group);
758                 goto failed_mount;
759         }
760         if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
761                 printk ("EXT2-fs: #inodes per group too big: %lu\n",
762                         sbi->s_inodes_per_group);
763                 goto failed_mount;
764         }
765
766         sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
767                                         le32_to_cpu(es->s_first_data_block) +
768                                        EXT2_BLOCKS_PER_GROUP(sb) - 1) /
769                                        EXT2_BLOCKS_PER_GROUP(sb);
770         db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
771                    EXT2_DESC_PER_BLOCK(sb);
772         sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
773         if (sbi->s_group_desc == NULL) {
774                 printk ("EXT2-fs: not enough memory\n");
775                 goto failed_mount;
776         }
777         percpu_counter_init(&sbi->s_freeblocks_counter);
778         percpu_counter_init(&sbi->s_freeinodes_counter);
779         percpu_counter_init(&sbi->s_dirs_counter);
780         bgl_lock_init(&sbi->s_blockgroup_lock);
781         sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
782                                GFP_KERNEL);
783         if (!sbi->s_debts) {
784                 printk ("EXT2-fs: not enough memory\n");
785                 goto failed_mount_group_desc;
786         }
787         memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
788         for (i = 0; i < db_count; i++) {
789                 block = descriptor_loc(sb, logic_sb_block, i);
790                 sbi->s_group_desc[i] = sb_bread(sb, block);
791                 if (!sbi->s_group_desc[i]) {
792                         for (j = 0; j < i; j++)
793                                 brelse (sbi->s_group_desc[j]);
794                         printk ("EXT2-fs: unable to read group descriptors\n");
795                         goto failed_mount_group_desc;
796                 }
797         }
798         if (!ext2_check_descriptors (sb)) {
799                 printk ("EXT2-fs: group descriptors corrupted!\n");
800                 db_count = i;
801                 goto failed_mount2;
802         }
803         sbi->s_gdb_count = db_count;
804         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
805         spin_lock_init(&sbi->s_next_gen_lock);
806         /*
807          * set up enough so that it can read an inode
808          */
809         sb->s_op = &ext2_sops;
810         sb->s_export_op = &ext2_export_ops;
811         root = iget(sb, EXT2_ROOT_INO);
812         sb->s_root = d_alloc_root(root);
813         if (!sb->s_root) {
814                 iput(root);
815                 printk(KERN_ERR "EXT2-fs: get root inode failed\n");
816                 goto failed_mount2;
817         }
818         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
819                 dput(sb->s_root);
820                 sb->s_root = NULL;
821                 printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
822                 goto failed_mount2;
823         }
824         if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
825                 ext2_warning(sb, __FUNCTION__,
826                         "mounting ext3 filesystem as ext2\n");
827         ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
828         percpu_counter_mod(&sbi->s_freeblocks_counter,
829                                 ext2_count_free_blocks(sb));
830         percpu_counter_mod(&sbi->s_freeinodes_counter,
831                                 ext2_count_free_inodes(sb));
832         percpu_counter_mod(&sbi->s_dirs_counter,
833                                 ext2_count_dirs(sb));
834         return 0;
835 failed_mount2:
836         for (i = 0; i < db_count; i++)
837                 brelse(sbi->s_group_desc[i]);
838 failed_mount_group_desc:
839         kfree(sbi->s_group_desc);
840         if (sbi->s_debts)
841                 kfree(sbi->s_debts);
842 failed_mount:
843         brelse(bh);
844 failed_sbi:
845         sb->s_fs_info = NULL;
846         kfree(sbi);
847         return -EINVAL;
848 }
849
850 static void ext2_commit_super (struct super_block * sb,
851                                struct ext2_super_block * es)
852 {
853         es->s_wtime = cpu_to_le32(get_seconds());
854         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
855         sb->s_dirt = 0;
856 }
857
858 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
859 {
860         es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
861         es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
862         es->s_wtime = cpu_to_le32(get_seconds());
863         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
864         sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
865         sb->s_dirt = 0;
866 }
867
868 /*
869  * In the second extended file system, it is not necessary to
870  * write the super block since we use a mapping of the
871  * disk super block in a buffer.
872  *
873  * However, this function is still used to set the fs valid
874  * flags to 0.  We need to set this flag to 0 since the fs
875  * may have been checked while mounted and e2fsck may have
876  * set s_state to EXT2_VALID_FS after some corrections.
877  */
878
879 void ext2_write_super (struct super_block * sb)
880 {
881         struct ext2_super_block * es;
882         lock_kernel();
883         if (!(sb->s_flags & MS_RDONLY)) {
884                 es = EXT2_SB(sb)->s_es;
885
886                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
887                         ext2_debug ("setting valid to 0\n");
888                         es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
889                                                   ~EXT2_VALID_FS);
890                         es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
891                         es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
892                         es->s_mtime = cpu_to_le32(get_seconds());
893                         ext2_sync_super(sb, es);
894                 } else
895                         ext2_commit_super (sb, es);
896         }
897         sb->s_dirt = 0;
898         unlock_kernel();
899 }
900
901 static int ext2_remount (struct super_block * sb, int * flags, char * data)
902 {
903         struct ext2_sb_info * sbi = EXT2_SB(sb);
904         struct ext2_super_block * es;
905
906         /*
907          * Allow the "check" option to be passed as a remount option.
908          */
909         if (!parse_options (data, sbi))
910                 return -EINVAL;
911
912         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
913                 ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
914
915         es = sbi->s_es;
916         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
917                 return 0;
918         if (*flags & MS_RDONLY) {
919                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
920                     !(sbi->s_mount_state & EXT2_VALID_FS))
921                         return 0;
922                 /*
923                  * OK, we are remounting a valid rw partition rdonly, so set
924                  * the rdonly flag and then mark the partition as valid again.
925                  */
926                 es->s_state = cpu_to_le16(sbi->s_mount_state);
927                 es->s_mtime = cpu_to_le32(get_seconds());
928         } else {
929                 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
930                                                ~EXT2_FEATURE_RO_COMPAT_SUPP);
931                 if (ret) {
932                         printk("EXT2-fs: %s: couldn't remount RDWR because of "
933                                "unsupported optional features (%x).\n",
934                                sb->s_id, le32_to_cpu(ret));
935                         return -EROFS;
936                 }
937                 /*
938                  * Mounting a RDONLY partition read-write, so reread and
939                  * store the current valid flag.  (It may have been changed
940                  * by e2fsck since we originally mounted the partition.)
941                  */
942                 sbi->s_mount_state = le16_to_cpu(es->s_state);
943                 if (!ext2_setup_super (sb, es, 0))
944                         sb->s_flags &= ~MS_RDONLY;
945         }
946         ext2_sync_super(sb, es);
947         return 0;
948 }
949
950 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
951 {
952         struct ext2_sb_info *sbi = EXT2_SB(sb);
953         unsigned long overhead;
954         int i;
955
956         if (test_opt (sb, MINIX_DF))
957                 overhead = 0;
958         else {
959                 /*
960                  * Compute the overhead (FS structures)
961                  */
962
963                 /*
964                  * All of the blocks before first_data_block are
965                  * overhead
966                  */
967                 overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
968
969                 /*
970                  * Add the overhead attributed to the superblock and
971                  * block group descriptors.  If the sparse superblocks
972                  * feature is turned on, then not all groups have this.
973                  */
974                 for (i = 0; i < sbi->s_groups_count; i++)
975                         overhead += ext2_bg_has_super(sb, i) +
976                                 ext2_bg_num_gdb(sb, i);
977
978                 /*
979                  * Every block group has an inode bitmap, a block
980                  * bitmap, and an inode table.
981                  */
982                 overhead += (sbi->s_groups_count *
983                              (2 + sbi->s_itb_per_group));
984         }
985
986         buf->f_type = EXT2_SUPER_MAGIC;
987         buf->f_bsize = sb->s_blocksize;
988         buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
989         buf->f_bfree = ext2_count_free_blocks(sb);
990         buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
991         if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
992                 buf->f_bavail = 0;
993         buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
994         buf->f_ffree = ext2_count_free_inodes (sb);
995         buf->f_namelen = EXT2_NAME_LEN;
996         return 0;
997 }
998
999 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
1000         int flags, const char *dev_name, void *data)
1001 {
1002         return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1003 }
1004
1005 static struct file_system_type ext2_fs_type = {
1006         .owner          = THIS_MODULE,
1007         .name           = "ext2",
1008         .get_sb         = ext2_get_sb,
1009         .kill_sb        = kill_block_super,
1010         .fs_flags       = FS_REQUIRES_DEV,
1011 };
1012
1013 static int __init init_ext2_fs(void)
1014 {
1015         int err = init_ext2_xattr();
1016         if (err)
1017                 return err;
1018         err = init_inodecache();
1019         if (err)
1020                 goto out1;
1021         err = register_filesystem(&ext2_fs_type);
1022         if (err)
1023                 goto out;
1024         return 0;
1025 out:
1026         destroy_inodecache();
1027 out1:
1028         exit_ext2_xattr();
1029         return err;
1030 }
1031
1032 static void __exit exit_ext2_fs(void)
1033 {
1034         unregister_filesystem(&ext2_fs_type);
1035         destroy_inodecache();
1036         exit_ext2_xattr();
1037 }
1038
1039 module_init(init_ext2_fs)
1040 module_exit(exit_ext2_fs)