import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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/fs.h>
23 #include <linux/ext2_fs.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/locks.h>
27 #include <linux/blkdev.h>
28 #include <asm/uaccess.h>
29
30
31 static void ext2_sync_super(struct super_block *sb,
32                             struct ext2_super_block *es);
33
34 static char error_buf[1024];
35
36 void ext2_error (struct super_block * sb, const char * function,
37                  const char * fmt, ...)
38 {
39         va_list args;
40         struct ext2_super_block *es = EXT2_SB(sb)->s_es;
41
42         if (!(sb->s_flags & MS_RDONLY)) {
43                 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
44                 es->s_state =
45                         cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
46                 ext2_sync_super(sb, es);
47         }
48         va_start (args, fmt);
49         vsprintf (error_buf, fmt, args);
50         va_end (args);
51         if (test_opt (sb, ERRORS_PANIC) ||
52             (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_PANIC &&
53              !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
54                 panic ("EXT2-fs panic (device %s): %s: %s\n",
55                        bdevname(sb->s_dev), function, error_buf);
56         printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
57                 bdevname(sb->s_dev), function, error_buf);
58         if (test_opt (sb, ERRORS_RO) ||
59             (le16_to_cpu(sb->u.ext2_sb.s_es->s_errors) == EXT2_ERRORS_RO &&
60              !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
61                 printk ("Remounting filesystem read-only\n");
62                 sb->s_flags |= MS_RDONLY;
63         }
64 }
65
66 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
67                             const char * fmt, ...)
68 {
69         va_list args;
70
71         if (!(sb->s_flags & MS_RDONLY)) {
72                 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
73                 sb->u.ext2_sb.s_es->s_state =
74                         cpu_to_le16(le16_to_cpu(sb->u.ext2_sb.s_es->s_state) | EXT2_ERROR_FS);
75                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
76                 sb->s_dirt = 1;
77         }
78         va_start (args, fmt);
79         vsprintf (error_buf, fmt, args);
80         va_end (args);
81         sb->s_flags |= MS_RDONLY;
82         panic ("EXT2-fs panic (device %s): %s: %s\n",
83                bdevname(sb->s_dev), function, error_buf);
84 }
85
86 void ext2_warning (struct super_block * sb, const char * function,
87                    const char * fmt, ...)
88 {
89         va_list args;
90
91         va_start (args, fmt);
92         vsprintf (error_buf, fmt, args);
93         va_end (args);
94         printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
95                 bdevname(sb->s_dev), function, error_buf);
96 }
97
98 void ext2_update_dynamic_rev(struct super_block *sb)
99 {
100         struct ext2_super_block *es = EXT2_SB(sb)->s_es;
101
102         if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
103                 return;
104
105         ext2_warning(sb, __FUNCTION__,
106                      "updating to rev %d because of new feature flag, "
107                      "running e2fsck is recommended",
108                      EXT2_DYNAMIC_REV);
109
110         es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
111         es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
112         es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
113         /* leave es->s_feature_*compat flags alone */
114         /* es->s_uuid will be set by e2fsck if empty */
115
116         /*
117          * The rest of the superblock fields should be zero, and if not it
118          * means they are likely already in use, so leave them alone.  We
119          * can leave it up to e2fsck to clean up any inconsistencies there.
120          */
121 }
122
123 void ext2_put_super (struct super_block * sb)
124 {
125         int db_count;
126         int i;
127
128         if (!(sb->s_flags & MS_RDONLY)) {
129                 struct ext2_super_block *es = EXT2_SB(sb)->s_es;
130
131                 es->s_state = le16_to_cpu(EXT2_SB(sb)->s_mount_state);
132                 ext2_sync_super(sb, es);
133         }
134         db_count = EXT2_SB(sb)->s_gdb_count;
135         for (i = 0; i < db_count; i++)
136                 if (sb->u.ext2_sb.s_group_desc[i])
137                         brelse (sb->u.ext2_sb.s_group_desc[i]);
138         kfree(sb->u.ext2_sb.s_group_desc);
139         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
140                 if (sb->u.ext2_sb.s_inode_bitmap[i])
141                         brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
142         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
143                 if (sb->u.ext2_sb.s_block_bitmap[i])
144                         brelse (sb->u.ext2_sb.s_block_bitmap[i]);
145         brelse (sb->u.ext2_sb.s_sbh);
146
147         return;
148 }
149
150 static struct super_operations ext2_sops = {
151         read_inode:     ext2_read_inode,
152         write_inode:    ext2_write_inode,
153         put_inode:      ext2_put_inode,
154         delete_inode:   ext2_delete_inode,
155         put_super:      ext2_put_super,
156         write_super:    ext2_write_super,
157         statfs:         ext2_statfs,
158         remount_fs:     ext2_remount,
159 };
160
161 /*
162  * This function has been shamelessly adapted from the msdos fs
163  */
164 static int parse_options (char * options, unsigned long * sb_block,
165                           unsigned short *resuid, unsigned short * resgid,
166                           unsigned long * mount_options)
167 {
168         char * this_char;
169         char * value;
170
171         if (!options)
172                 return 1;
173         for (this_char = strtok (options, ",");
174              this_char != NULL;
175              this_char = strtok (NULL, ",")) {
176                 if ((value = strchr (this_char, '=')) != NULL)
177                         *value++ = 0;
178                 if (!strcmp (this_char, "bsddf"))
179                         clear_opt (*mount_options, MINIX_DF);
180                 else if (!strcmp (this_char, "nouid32")) {
181                         set_opt (*mount_options, NO_UID32);
182                 }
183                 else if (!strcmp (this_char, "check")) {
184                         if (!value || !*value || !strcmp (value, "none"))
185                                 clear_opt (*mount_options, CHECK);
186                         else
187 #ifdef CONFIG_EXT2_CHECK
188                                 set_opt (*mount_options, CHECK);
189 #else
190                                 printk("EXT2 Check option not supported\n");
191 #endif
192                 }
193                 else if (!strcmp (this_char, "debug"))
194                         set_opt (*mount_options, DEBUG);
195                 else if (!strcmp (this_char, "errors")) {
196                         if (!value || !*value) {
197                                 printk ("EXT2-fs: the errors option requires "
198                                         "an argument\n");
199                                 return 0;
200                         }
201                         if (!strcmp (value, "continue")) {
202                                 clear_opt (*mount_options, ERRORS_RO);
203                                 clear_opt (*mount_options, ERRORS_PANIC);
204                                 set_opt (*mount_options, ERRORS_CONT);
205                         }
206                         else if (!strcmp (value, "remount-ro")) {
207                                 clear_opt (*mount_options, ERRORS_CONT);
208                                 clear_opt (*mount_options, ERRORS_PANIC);
209                                 set_opt (*mount_options, ERRORS_RO);
210                         }
211                         else if (!strcmp (value, "panic")) {
212                                 clear_opt (*mount_options, ERRORS_CONT);
213                                 clear_opt (*mount_options, ERRORS_RO);
214                                 set_opt (*mount_options, ERRORS_PANIC);
215                         }
216                         else {
217                                 printk ("EXT2-fs: Invalid errors option: %s\n",
218                                         value);
219                                 return 0;
220                         }
221                 }
222                 else if (!strcmp (this_char, "grpid") ||
223                          !strcmp (this_char, "bsdgroups"))
224                         set_opt (*mount_options, GRPID);
225                 else if (!strcmp (this_char, "minixdf"))
226                         set_opt (*mount_options, MINIX_DF);
227                 else if (!strcmp (this_char, "nocheck"))
228                         clear_opt (*mount_options, CHECK);
229                 else if (!strcmp (this_char, "nogrpid") ||
230                          !strcmp (this_char, "sysvgroups"))
231                         clear_opt (*mount_options, GRPID);
232                 else if (!strcmp (this_char, "resgid")) {
233                         if (!value || !*value) {
234                                 printk ("EXT2-fs: the resgid option requires "
235                                         "an argument\n");
236                                 return 0;
237                         }
238                         *resgid = simple_strtoul (value, &value, 0);
239                         if (*value) {
240                                 printk ("EXT2-fs: Invalid resgid option: %s\n",
241                                         value);
242                                 return 0;
243                         }
244                 }
245                 else if (!strcmp (this_char, "resuid")) {
246                         if (!value || !*value) {
247                                 printk ("EXT2-fs: the resuid option requires "
248                                         "an argument");
249                                 return 0;
250                         }
251                         *resuid = simple_strtoul (value, &value, 0);
252                         if (*value) {
253                                 printk ("EXT2-fs: Invalid resuid option: %s\n",
254                                         value);
255                                 return 0;
256                         }
257                 }
258                 else if (!strcmp (this_char, "sb")) {
259                         if (!value || !*value) {
260                                 printk ("EXT2-fs: the sb option requires "
261                                         "an argument");
262                                 return 0;
263                         }
264                         *sb_block = simple_strtoul (value, &value, 0);
265                         if (*value) {
266                                 printk ("EXT2-fs: Invalid sb option: %s\n",
267                                         value);
268                                 return 0;
269                         }
270                 }
271                 /* Silently ignore the quota options */
272                 else if (!strcmp (this_char, "grpquota")
273                          || !strcmp (this_char, "noquota")
274                          || !strcmp (this_char, "quota")
275                          || !strcmp (this_char, "usrquota"))
276                         /* Don't do anything ;-) */ ;
277                 else {
278                         printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
279                         return 0;
280                 }
281         }
282         return 1;
283 }
284
285 static int ext2_setup_super (struct super_block * sb,
286                               struct ext2_super_block * es,
287                               int read_only)
288 {
289         int res = 0;
290         if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
291                 printk ("EXT2-fs warning: revision level too high, "
292                         "forcing read-only mode\n");
293                 res = MS_RDONLY;
294         }
295         if (read_only)
296                 return res;
297         if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
298                 printk ("EXT2-fs warning: mounting unchecked fs, "
299                         "running e2fsck is recommended\n");
300         else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
301                 printk ("EXT2-fs warning: mounting fs with errors, "
302                         "running e2fsck is recommended\n");
303         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
304                  le16_to_cpu(es->s_mnt_count) >=
305                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
306                 printk ("EXT2-fs warning: maximal mount count reached, "
307                         "running e2fsck is recommended\n");
308         else if (le32_to_cpu(es->s_checkinterval) &&
309                 (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
310                 printk ("EXT2-fs warning: checktime reached, "
311                         "running e2fsck is recommended\n");
312         if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
313                 es->s_max_mnt_count = (__s16) cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
314         es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
315         ext2_write_super(sb);
316         if (test_opt (sb, DEBUG))
317                 printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
318                         "bpg=%lu, ipg=%lu, mo=%04lx]\n",
319                         EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
320                         sb->u.ext2_sb.s_frag_size,
321                         sb->u.ext2_sb.s_groups_count,
322                         EXT2_BLOCKS_PER_GROUP(sb),
323                         EXT2_INODES_PER_GROUP(sb),
324                         sb->u.ext2_sb.s_mount_opt);
325 #ifdef CONFIG_EXT2_CHECK
326         if (test_opt (sb, CHECK)) {
327                 ext2_check_blocks_bitmap (sb);
328                 ext2_check_inodes_bitmap (sb);
329         }
330 #endif
331         return res;
332 }
333
334 static int ext2_check_descriptors (struct super_block * sb)
335 {
336         int i;
337         int desc_block = 0;
338         unsigned long block = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
339         struct ext2_group_desc * gdp = NULL;
340
341         ext2_debug ("Checking group descriptors");
342
343         for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
344         {
345                 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
346                         gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
347                 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
348                     le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
349                 {
350                         ext2_error (sb, "ext2_check_descriptors",
351                                     "Block bitmap for group %d"
352                                     " not in group (block %lu)!",
353                                     i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
354                         return 0;
355                 }
356                 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
357                     le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
358                 {
359                         ext2_error (sb, "ext2_check_descriptors",
360                                     "Inode bitmap for group %d"
361                                     " not in group (block %lu)!",
362                                     i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
363                         return 0;
364                 }
365                 if (le32_to_cpu(gdp->bg_inode_table) < block ||
366                     le32_to_cpu(gdp->bg_inode_table) + sb->u.ext2_sb.s_itb_per_group >=
367                     block + EXT2_BLOCKS_PER_GROUP(sb))
368                 {
369                         ext2_error (sb, "ext2_check_descriptors",
370                                     "Inode table for group %d"
371                                     " not in group (block %lu)!",
372                                     i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
373                         return 0;
374                 }
375                 block += EXT2_BLOCKS_PER_GROUP(sb);
376                 gdp++;
377         }
378         return 1;
379 }
380
381 #define log2(n) ffz(~(n))
382  
383 /*
384  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
385  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
386  * We need to be 1 filesystem block less than the 2^32 sector limit.
387  */
388 static loff_t ext2_max_size(int bits)
389 {
390         loff_t res = EXT2_NDIR_BLOCKS;
391         res += 1LL << (bits-2);
392         res += 1LL << (2*(bits-2));
393         res += 1LL << (3*(bits-2));
394         res <<= bits;
395         if (res > (512LL << 32) - (1 << bits))
396                 res = (512LL << 32) - (1 << bits);
397         return res;
398 }
399
400 static unsigned long descriptor_loc(struct super_block *sb,
401                                     unsigned long logic_sb_block,
402                                     int nr)
403 {
404         struct ext2_sb_info *sbi = EXT2_SB(sb);
405         unsigned long bg, first_data_block, first_meta_bg;
406         int has_super = 0;
407         
408         first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
409         first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
410
411         if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
412             nr < first_meta_bg)
413                 return (logic_sb_block + nr + 1);
414         bg = sbi->s_desc_per_block * nr;
415         if (ext2_bg_has_super(sb, bg))
416                 has_super = 1;
417         return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
418 }
419
420 struct super_block * ext2_read_super (struct super_block * sb, void * data,
421                                       int silent)
422 {
423         struct buffer_head * bh;
424         struct ext2_sb_info * sbi = EXT2_SB(sb);
425         struct ext2_super_block * es;
426         unsigned long sb_block = 1;
427         unsigned short resuid = EXT2_DEF_RESUID;
428         unsigned short resgid = EXT2_DEF_RESGID;
429         unsigned long block;
430         unsigned long logic_sb_block;
431         unsigned long offset = 0;
432         kdev_t dev = sb->s_dev;
433         int blocksize = BLOCK_SIZE;
434         int db_count;
435         int i, j;
436
437         /*
438          * See what the current blocksize for the device is, and
439          * use that as the blocksize.  Otherwise (or if the blocksize
440          * is smaller than the default) use the default.
441          * This is important for devices that have a hardware
442          * sectorsize that is larger than the default.
443          */
444         blocksize = get_hardsect_size(dev);
445         if(blocksize < BLOCK_SIZE )
446             blocksize = BLOCK_SIZE;
447
448         sb->u.ext2_sb.s_mount_opt = 0;
449         if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
450             &sb->u.ext2_sb.s_mount_opt)) {
451                 return NULL;
452         }
453
454         if (set_blocksize(dev, blocksize) < 0) {
455                 printk ("EXT2-fs: unable to set blocksize %d\n", blocksize);
456                 return NULL;
457         }
458         sb->s_blocksize = blocksize;
459
460         /*
461          * If the superblock doesn't start on a sector boundary,
462          * calculate the offset.  FIXME(eric) this doesn't make sense
463          * that we would have to do this.
464          */
465         if (blocksize != BLOCK_SIZE) {
466                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
467                 offset = (sb_block*BLOCK_SIZE) % blocksize;
468         } else {
469                 logic_sb_block = sb_block;
470         }
471
472         if (!(bh = sb_bread(sb, logic_sb_block))) {
473                 printk ("EXT2-fs: unable to read superblock\n");
474                 return NULL;
475         }
476         /*
477          * Note: s_es must be initialized as soon as possible because
478          *       some ext2 macro-instructions depend on its value
479          */
480         es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
481         sb->u.ext2_sb.s_es = es;
482         sb->s_magic = le16_to_cpu(es->s_magic);
483         if (sb->s_magic != EXT2_SUPER_MAGIC)
484                 goto cantfind_ext2;
485         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
486             (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
487              EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
488              EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
489                 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
490                        "running e2fsck is recommended\n");
491         /*
492          * Check feature flags regardless of the revision level, since we
493          * previously didn't change the revision level when setting the flags,
494          * so there is a chance incompat flags are set on a rev 0 filesystem.
495          */
496         if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
497                 printk("EXT2-fs: %s: couldn't mount because of "
498                        "unsupported optional features (%x).\n",
499                        bdevname(dev), i);
500                 goto failed_mount;
501         }
502         if (!(sb->s_flags & MS_RDONLY) &&
503             (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
504                 printk("EXT2-fs: %s: couldn't mount RDWR because of "
505                        "unsupported optional features (%x).\n",
506                        bdevname(dev), i);
507                 goto failed_mount;
508         }
509         if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
510                 ext2_warning(sb, __FUNCTION__,
511                         "mounting ext3 filesystem as ext2\n");
512         sb->s_blocksize_bits =
513                 le32_to_cpu(EXT2_SB(sb)->s_es->s_log_block_size) + 10;
514         sb->s_blocksize = 1 << sb->s_blocksize_bits;
515
516         sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
517
518         /* If the blocksize doesn't match, re-read the thing.. */
519         if (sb->s_blocksize != blocksize) {
520                 blocksize = sb->s_blocksize;
521                 brelse(bh);
522
523                 if (set_blocksize(dev, blocksize) < 0) {
524                         printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
525                         return NULL;
526                 }
527
528                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
529                 offset = (sb_block*BLOCK_SIZE) % blocksize;
530                 bh = sb_bread(sb, logic_sb_block);
531                 if(!bh) {
532                         printk("EXT2-fs: Couldn't read superblock on "
533                                "2nd try.\n");
534                         goto failed_mount;
535                 }
536                 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
537                 sb->u.ext2_sb.s_es = es;
538                 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
539                         printk ("EXT2-fs: Magic mismatch, very weird !\n");
540                         goto failed_mount;
541                 }
542         }
543
544         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
545                 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
546                 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
547         } else {
548                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
549                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
550                 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
551                     (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
552                     (sbi->s_inode_size > blocksize)) {
553                         printk ("EXT2-fs: unsupported inode size: %d\n",
554                                 sbi->s_inode_size);
555                         goto failed_mount;
556                 }
557         }
558         sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
559                                    le32_to_cpu(es->s_log_frag_size);
560         if (sb->u.ext2_sb.s_frag_size == 0)
561                 goto cantfind_ext2;
562         sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
563                                           sb->u.ext2_sb.s_frag_size;
564         sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
565         sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
566         sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
567         if (EXT2_INODE_SIZE(sb) == 0)
568                 goto cantfind_ext2;
569         sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
570                                            EXT2_INODE_SIZE(sb);
571         if (sb->u.ext2_sb.s_inodes_per_block == 0)
572                 goto cantfind_ext2;
573         sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
574                                         sb->u.ext2_sb.s_inodes_per_block;
575         sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
576                                          sizeof (struct ext2_group_desc);
577         sb->u.ext2_sb.s_sbh = bh;
578         if (resuid != EXT2_DEF_RESUID)
579                 sb->u.ext2_sb.s_resuid = resuid;
580         else
581                 sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
582         if (resgid != EXT2_DEF_RESGID)
583                 sb->u.ext2_sb.s_resgid = resgid;
584         else
585                 sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
586         sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
587         sb->u.ext2_sb.s_addr_per_block_bits =
588                 log2 (EXT2_ADDR_PER_BLOCK(sb));
589         sb->u.ext2_sb.s_desc_per_block_bits =
590                 log2 (EXT2_DESC_PER_BLOCK(sb));
591
592         if (sb->s_magic != EXT2_SUPER_MAGIC)
593                 goto cantfind_ext2;
594
595         if (sb->s_blocksize != bh->b_size) {
596                 if (!silent)
597                         printk ("VFS: Unsupported blocksize on dev "
598                                 "%s.\n", bdevname(dev));
599                 goto failed_mount;
600         }
601
602         if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
603                 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
604                         sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
605                 goto failed_mount;
606         }
607
608         if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
609                 printk ("EXT2-fs: #blocks per group too big: %lu\n",
610                         sb->u.ext2_sb.s_blocks_per_group);
611                 goto failed_mount;
612         }
613         if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
614                 printk ("EXT2-fs: #fragments per group too big: %lu\n",
615                         sb->u.ext2_sb.s_frags_per_group);
616                 goto failed_mount;
617         }
618         if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
619                 printk ("EXT2-fs: #inodes per group too big: %lu\n",
620                         sb->u.ext2_sb.s_inodes_per_group);
621                 goto failed_mount;
622         }
623
624         if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
625                 goto cantfind_ext2;
626         sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
627                                         le32_to_cpu(es->s_first_data_block) +
628                                        EXT2_BLOCKS_PER_GROUP(sb) - 1) /
629                                        EXT2_BLOCKS_PER_GROUP(sb);
630         db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
631                    EXT2_DESC_PER_BLOCK(sb);
632         sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
633         if (sb->u.ext2_sb.s_group_desc == NULL) {
634                 printk ("EXT2-fs: not enough memory\n");
635                 goto failed_mount;
636         }
637         for (i = 0; i < db_count; i++) {
638                 block = descriptor_loc(sb, logic_sb_block, i);
639                 sbi->s_group_desc[i] = sb_bread(sb, block);
640                 if (!sbi->s_group_desc[i]) {
641                         for (j = 0; j < i; j++)
642                                 brelse (sbi->s_group_desc[j]);
643                         kfree(sbi->s_group_desc);
644                         printk ("EXT2-fs: unable to read group descriptors\n");
645                         goto failed_mount;
646                 }
647         }
648         if (!ext2_check_descriptors (sb)) {
649                 printk ("EXT2-fs: group descriptors corrupted!\n");
650                 db_count = i;
651                 goto failed_mount2;
652         }
653         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
654                 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
655                 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
656                 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
657                 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
658         }
659         sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
660         sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
661         sb->u.ext2_sb.s_gdb_count = db_count;
662         /*
663          * set up enough so that it can read an inode
664          */
665         sb->s_op = &ext2_sops;
666         sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
667         if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
668             !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
669                 if (sb->s_root) {
670                         dput(sb->s_root);
671                         sb->s_root = NULL;
672                         printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
673                 } else
674                         printk(KERN_ERR "EXT2-fs: get root inode failed\n");
675                 goto failed_mount2;
676         }
677         ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
678         return sb;
679 cantfind_ext2:
680         if (!silent)
681                 printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
682                         bdevname(dev));
683         goto failed_mount;
684 failed_mount2:
685         for (i = 0; i < db_count; i++)
686                 brelse(sb->u.ext2_sb.s_group_desc[i]);
687         kfree(sb->u.ext2_sb.s_group_desc);
688 failed_mount:
689         brelse(bh);
690         return NULL;
691 }
692
693 static void ext2_commit_super (struct super_block * sb,
694                                struct ext2_super_block * es)
695 {
696         es->s_wtime = cpu_to_le32(CURRENT_TIME);
697         mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
698         sb->s_dirt = 0;
699 }
700
701 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
702 {
703         es->s_wtime = cpu_to_le32(CURRENT_TIME);
704         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
705         ll_rw_block(WRITE, 1, &EXT2_SB(sb)->s_sbh);
706         wait_on_buffer(EXT2_SB(sb)->s_sbh);
707         sb->s_dirt = 0;
708 }
709
710 /*
711  * In the second extended file system, it is not necessary to
712  * write the super block since we use a mapping of the
713  * disk super block in a buffer.
714  *
715  * However, this function is still used to set the fs valid
716  * flags to 0.  We need to set this flag to 0 since the fs
717  * may have been checked while mounted and e2fsck may have
718  * set s_state to EXT2_VALID_FS after some corrections.
719  */
720
721 void ext2_write_super (struct super_block * sb)
722 {
723         struct ext2_super_block * es;
724
725         if (!(sb->s_flags & MS_RDONLY)) {
726                 es = sb->u.ext2_sb.s_es;
727
728                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
729                         ext2_debug ("setting valid to 0\n");
730                         es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
731                                                   ~EXT2_VALID_FS);
732                         es->s_mtime = cpu_to_le32(CURRENT_TIME);
733                         ext2_sync_super(sb, es);
734                 } else
735                         ext2_commit_super (sb, es);
736         }
737         sb->s_dirt = 0;
738 }
739
740 int ext2_remount (struct super_block * sb, int * flags, char * data)
741 {
742         struct ext2_super_block * es;
743         unsigned short resuid = sb->u.ext2_sb.s_resuid;
744         unsigned short resgid = sb->u.ext2_sb.s_resgid;
745         unsigned long new_mount_opt;
746         unsigned long tmp;
747
748         /*
749          * Allow the "check" option to be passed as a remount option.
750          */
751         new_mount_opt = sb->u.ext2_sb.s_mount_opt;
752         if (!parse_options (data, &tmp, &resuid, &resgid,
753                             &new_mount_opt))
754                 return -EINVAL;
755
756         sb->u.ext2_sb.s_mount_opt = new_mount_opt;
757         sb->u.ext2_sb.s_resuid = resuid;
758         sb->u.ext2_sb.s_resgid = resgid;
759         es = sb->u.ext2_sb.s_es;
760         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
761                 return 0;
762         if (*flags & MS_RDONLY) {
763                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
764                     !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
765                         return 0;
766                 /*
767                  * OK, we are remounting a valid rw partition rdonly, so set
768                  * the rdonly flag and then mark the partition as valid again.
769                  */
770                 es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
771                 es->s_mtime = cpu_to_le32(CURRENT_TIME);
772         } else {
773                 int ret;
774                 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
775                                                ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
776                         printk("EXT2-fs: %s: couldn't remount RDWR because of "
777                                "unsupported optional features (%x).\n",
778                                bdevname(sb->s_dev), ret);
779                         return -EROFS;
780                 }
781                 /*
782                  * Mounting a RDONLY partition read-write, so reread and
783                  * store the current valid flag.  (It may have been changed
784                  * by e2fsck since we originally mounted the partition.)
785                  */
786                 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
787                 if (!ext2_setup_super (sb, es, 0))
788                         sb->s_flags &= ~MS_RDONLY;
789         }
790         ext2_sync_super(sb, es);
791         return 0;
792 }
793
794 int ext2_statfs (struct super_block * sb, struct statfs * buf)
795 {
796         unsigned long overhead;
797         int i;
798
799         if (test_opt (sb, MINIX_DF))
800                 overhead = 0;
801         else {
802                 /*
803                  * Compute the overhead (FS structures)
804                  */
805
806                 /*
807                  * All of the blocks before first_data_block are
808                  * overhead
809                  */
810                 overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
811
812                 /*
813                  * Add the overhead attributed to the superblock and
814                  * block group descriptors.  If the sparse superblocks
815                  * feature is turned on, then not all groups have this.
816                  */
817                 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++)
818                         overhead += ext2_bg_has_super(sb, i) +
819                                 ext2_bg_num_gdb(sb, i);
820
821                 /*
822                  * Every block group has an inode bitmap, a block
823                  * bitmap, and an inode table.
824                  */
825                 overhead += (sb->u.ext2_sb.s_groups_count *
826                              (2 + sb->u.ext2_sb.s_itb_per_group));
827         }
828
829         buf->f_type = EXT2_SUPER_MAGIC;
830         buf->f_bsize = sb->s_blocksize;
831         buf->f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
832         buf->f_bfree = ext2_count_free_blocks (sb);
833         buf->f_bavail = buf->f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
834         if (buf->f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
835                 buf->f_bavail = 0;
836         buf->f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
837         buf->f_ffree = ext2_count_free_inodes (sb);
838         buf->f_namelen = EXT2_NAME_LEN;
839         return 0;
840 }
841
842 static DECLARE_FSTYPE_DEV(ext2_fs_type, "ext2", ext2_read_super);
843
844 static int __init init_ext2_fs(void)
845 {
846         return register_filesystem(&ext2_fs_type);
847 }
848
849 static void __exit exit_ext2_fs(void)
850 {
851         unregister_filesystem(&ext2_fs_type);
852 }
853
854 EXPORT_NO_SYMBOLS;
855
856 module_init(init_ext2_fs)
857 module_exit(exit_ext2_fs)