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