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