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