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