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