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