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