added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.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 struct super_block * ext2_read_super (struct super_block * sb, void * data,
401                                       int silent)
402 {
403         struct buffer_head * bh;
404         struct ext2_super_block * es;
405         unsigned long sb_block = 1;
406         unsigned short resuid = EXT2_DEF_RESUID;
407         unsigned short resgid = EXT2_DEF_RESGID;
408         unsigned long logic_sb_block = 1;
409         unsigned long offset = 0;
410         kdev_t dev = sb->s_dev;
411         int blocksize = BLOCK_SIZE;
412         int db_count;
413         int i, j;
414
415         /*
416          * See what the current blocksize for the device is, and
417          * use that as the blocksize.  Otherwise (or if the blocksize
418          * is smaller than the default) use the default.
419          * This is important for devices that have a hardware
420          * sectorsize that is larger than the default.
421          */
422         blocksize = get_hardsect_size(dev);
423         if(blocksize < BLOCK_SIZE )
424             blocksize = BLOCK_SIZE;
425
426         sb->u.ext2_sb.s_mount_opt = 0;
427         if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
428             &sb->u.ext2_sb.s_mount_opt)) {
429                 return NULL;
430         }
431
432         if (set_blocksize(dev, blocksize) < 0) {
433                 printk ("EXT2-fs: unable to set blocksize %d\n", blocksize);
434                 return NULL;
435         }
436         sb->s_blocksize = blocksize;
437
438         /*
439          * If the superblock doesn't start on a sector boundary,
440          * calculate the offset.  FIXME(eric) this doesn't make sense
441          * that we would have to do this.
442          */
443         if (blocksize != BLOCK_SIZE) {
444                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
445                 offset = (sb_block*BLOCK_SIZE) % blocksize;
446         }
447
448         if (!(bh = sb_bread(sb, logic_sb_block))) {
449                 printk ("EXT2-fs: unable to read superblock\n");
450                 return NULL;
451         }
452         /*
453          * Note: s_es must be initialized as soon as possible because
454          *       some ext2 macro-instructions depend on its value
455          */
456         es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
457         sb->u.ext2_sb.s_es = es;
458         sb->s_magic = le16_to_cpu(es->s_magic);
459         if (sb->s_magic != EXT2_SUPER_MAGIC) {
460                 if (!silent)
461                         printk ("VFS: Can't find ext2 filesystem on dev %s.\n",
462                                 bdevname(dev));
463                 goto failed_mount;
464         }
465         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
466             (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
467              EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
468              EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
469                 printk("EXT2-fs warning: feature flags set on rev 0 fs, "
470                        "running e2fsck is recommended\n");
471         /*
472          * Check feature flags regardless of the revision level, since we
473          * previously didn't change the revision level when setting the flags,
474          * so there is a chance incompat flags are set on a rev 0 filesystem.
475          */
476         if ((i = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))) {
477                 printk("EXT2-fs: %s: couldn't mount because of "
478                        "unsupported optional features (%x).\n",
479                        bdevname(dev), i);
480                 goto failed_mount;
481         }
482         if (!(sb->s_flags & MS_RDONLY) &&
483             (i = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
484                 printk("EXT2-fs: %s: couldn't mount RDWR because of "
485                        "unsupported optional features (%x).\n",
486                        bdevname(dev), i);
487                 goto failed_mount;
488         }
489         if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
490                 ext2_warning(sb, __FUNCTION__,
491                         "mounting ext3 filesystem as ext2\n");
492         sb->s_blocksize_bits =
493                 le32_to_cpu(EXT2_SB(sb)->s_es->s_log_block_size) + 10;
494         sb->s_blocksize = 1 << sb->s_blocksize_bits;
495
496         sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
497
498         /* If the blocksize doesn't match, re-read the thing.. */
499         if (sb->s_blocksize != blocksize) {
500                 blocksize = sb->s_blocksize;
501                 brelse(bh);
502
503                 if (set_blocksize(dev, blocksize) < 0) {
504                         printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
505                         return NULL;
506                 }
507
508                 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
509                 offset = (sb_block*BLOCK_SIZE) % blocksize;
510                 bh = sb_bread(sb, logic_sb_block);
511                 if(!bh) {
512                         printk("EXT2-fs: Couldn't read superblock on "
513                                "2nd try.\n");
514                         goto failed_mount;
515                 }
516                 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
517                 sb->u.ext2_sb.s_es = es;
518                 if (es->s_magic != le16_to_cpu(EXT2_SUPER_MAGIC)) {
519                         printk ("EXT2-fs: Magic mismatch, very weird !\n");
520                         goto failed_mount;
521                 }
522         }
523
524         if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
525                 sb->u.ext2_sb.s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
526                 sb->u.ext2_sb.s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
527         } else {
528                 sb->u.ext2_sb.s_inode_size = le16_to_cpu(es->s_inode_size);
529                 sb->u.ext2_sb.s_first_ino = le32_to_cpu(es->s_first_ino);
530                 if (sb->u.ext2_sb.s_inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
531                         printk ("EXT2-fs: unsupported inode size: %d\n",
532                                 sb->u.ext2_sb.s_inode_size);
533                         goto failed_mount;
534                 }
535         }
536         sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
537                                    le32_to_cpu(es->s_log_frag_size);
538         if (sb->u.ext2_sb.s_frag_size)
539                 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
540                                                   sb->u.ext2_sb.s_frag_size;
541         else
542                 sb->s_magic = 0;
543         sb->u.ext2_sb.s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
544         sb->u.ext2_sb.s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
545         sb->u.ext2_sb.s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
546         sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
547                                            EXT2_INODE_SIZE(sb);
548         sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
549                                         sb->u.ext2_sb.s_inodes_per_block;
550         sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
551                                          sizeof (struct ext2_group_desc);
552         sb->u.ext2_sb.s_sbh = bh;
553         if (resuid != EXT2_DEF_RESUID)
554                 sb->u.ext2_sb.s_resuid = resuid;
555         else
556                 sb->u.ext2_sb.s_resuid = le16_to_cpu(es->s_def_resuid);
557         if (resgid != EXT2_DEF_RESGID)
558                 sb->u.ext2_sb.s_resgid = resgid;
559         else
560                 sb->u.ext2_sb.s_resgid = le16_to_cpu(es->s_def_resgid);
561         sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
562         sb->u.ext2_sb.s_addr_per_block_bits =
563                 log2 (EXT2_ADDR_PER_BLOCK(sb));
564         sb->u.ext2_sb.s_desc_per_block_bits =
565                 log2 (EXT2_DESC_PER_BLOCK(sb));
566         if (sb->s_magic != EXT2_SUPER_MAGIC) {
567                 if (!silent)
568                         printk ("VFS: Can't find an ext2 filesystem on dev "
569                                 "%s.\n",
570                                 bdevname(dev));
571                 goto failed_mount;
572         }
573         if (sb->s_blocksize != bh->b_size) {
574                 if (!silent)
575                         printk ("VFS: Unsupported blocksize on dev "
576                                 "%s.\n", bdevname(dev));
577                 goto failed_mount;
578         }
579
580         if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
581                 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
582                         sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
583                 goto failed_mount;
584         }
585
586         if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
587                 printk ("EXT2-fs: #blocks per group too big: %lu\n",
588                         sb->u.ext2_sb.s_blocks_per_group);
589                 goto failed_mount;
590         }
591         if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
592                 printk ("EXT2-fs: #fragments per group too big: %lu\n",
593                         sb->u.ext2_sb.s_frags_per_group);
594                 goto failed_mount;
595         }
596         if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
597                 printk ("EXT2-fs: #inodes per group too big: %lu\n",
598                         sb->u.ext2_sb.s_inodes_per_group);
599                 goto failed_mount;
600         }
601
602         sb->u.ext2_sb.s_groups_count = (le32_to_cpu(es->s_blocks_count) -
603                                         le32_to_cpu(es->s_first_data_block) +
604                                        EXT2_BLOCKS_PER_GROUP(sb) - 1) /
605                                        EXT2_BLOCKS_PER_GROUP(sb);
606         db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
607                    EXT2_DESC_PER_BLOCK(sb);
608         sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
609         if (sb->u.ext2_sb.s_group_desc == NULL) {
610                 printk ("EXT2-fs: not enough memory\n");
611                 goto failed_mount;
612         }
613         for (i = 0; i < db_count; i++) {
614                 sb->u.ext2_sb.s_group_desc[i] = sb_bread(sb, logic_sb_block + i + 1);
615                 if (!sb->u.ext2_sb.s_group_desc[i]) {
616                         for (j = 0; j < i; j++)
617                                 brelse (sb->u.ext2_sb.s_group_desc[j]);
618                         kfree(sb->u.ext2_sb.s_group_desc);
619                         printk ("EXT2-fs: unable to read group descriptors\n");
620                         goto failed_mount;
621                 }
622         }
623         if (!ext2_check_descriptors (sb)) {
624                 printk ("EXT2-fs: group descriptors corrupted!\n");
625                 db_count = i;
626                 goto failed_mount2;
627         }
628         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
629                 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
630                 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
631                 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
632                 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
633         }
634         sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
635         sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
636         sb->u.ext2_sb.s_gdb_count = db_count;
637         /*
638          * set up enough so that it can read an inode
639          */
640         sb->s_op = &ext2_sops;
641         sb->s_root = d_alloc_root(iget(sb, EXT2_ROOT_INO));
642         if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
643             !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
644                 if (sb->s_root) {
645                         dput(sb->s_root);
646                         sb->s_root = NULL;
647                         printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
648                 } else
649                         printk(KERN_ERR "EXT2-fs: get root inode failed\n");
650                 goto failed_mount2;
651         }
652         ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
653         return sb;
654 failed_mount2:
655         for (i = 0; i < db_count; i++)
656                 brelse(sb->u.ext2_sb.s_group_desc[i]);
657         kfree(sb->u.ext2_sb.s_group_desc);
658 failed_mount:
659         brelse(bh);
660         return NULL;
661 }
662
663 static void ext2_commit_super (struct super_block * sb,
664                                struct ext2_super_block * es)
665 {
666         es->s_wtime = cpu_to_le32(CURRENT_TIME);
667         mark_buffer_dirty(sb->u.ext2_sb.s_sbh);
668         sb->s_dirt = 0;
669 }
670
671 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
672 {
673         es->s_wtime = cpu_to_le32(CURRENT_TIME);
674         mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
675         ll_rw_block(WRITE, 1, &EXT2_SB(sb)->s_sbh);
676         wait_on_buffer(EXT2_SB(sb)->s_sbh);
677         sb->s_dirt = 0;
678 }
679
680 /*
681  * In the second extended file system, it is not necessary to
682  * write the super block since we use a mapping of the
683  * disk super block in a buffer.
684  *
685  * However, this function is still used to set the fs valid
686  * flags to 0.  We need to set this flag to 0 since the fs
687  * may have been checked while mounted and e2fsck may have
688  * set s_state to EXT2_VALID_FS after some corrections.
689  */
690
691 void ext2_write_super (struct super_block * sb)
692 {
693         struct ext2_super_block * es;
694
695         if (!(sb->s_flags & MS_RDONLY)) {
696                 es = sb->u.ext2_sb.s_es;
697
698                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
699                         ext2_debug ("setting valid to 0\n");
700                         es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
701                                                   ~EXT2_VALID_FS);
702                         es->s_mtime = cpu_to_le32(CURRENT_TIME);
703                         ext2_sync_super(sb, es);
704                 } else
705                         ext2_commit_super (sb, es);
706         }
707         sb->s_dirt = 0;
708 }
709
710 int ext2_remount (struct super_block * sb, int * flags, char * data)
711 {
712         struct ext2_super_block * es;
713         unsigned short resuid = sb->u.ext2_sb.s_resuid;
714         unsigned short resgid = sb->u.ext2_sb.s_resgid;
715         unsigned long new_mount_opt;
716         unsigned long tmp;
717
718         /*
719          * Allow the "check" option to be passed as a remount option.
720          */
721         new_mount_opt = sb->u.ext2_sb.s_mount_opt;
722         if (!parse_options (data, &tmp, &resuid, &resgid,
723                             &new_mount_opt))
724                 return -EINVAL;
725
726         sb->u.ext2_sb.s_mount_opt = new_mount_opt;
727         sb->u.ext2_sb.s_resuid = resuid;
728         sb->u.ext2_sb.s_resgid = resgid;
729         es = sb->u.ext2_sb.s_es;
730         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
731                 return 0;
732         if (*flags & MS_RDONLY) {
733                 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
734                     !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
735                         return 0;
736                 /*
737                  * OK, we are remounting a valid rw partition rdonly, so set
738                  * the rdonly flag and then mark the partition as valid again.
739                  */
740                 es->s_state = cpu_to_le16(sb->u.ext2_sb.s_mount_state);
741                 es->s_mtime = cpu_to_le32(CURRENT_TIME);
742         } else {
743                 int ret;
744                 if ((ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
745                                                ~EXT2_FEATURE_RO_COMPAT_SUPP))) {
746                         printk("EXT2-fs: %s: couldn't remount RDWR because of "
747                                "unsupported optional features (%x).\n",
748                                bdevname(sb->s_dev), ret);
749                         return -EROFS;
750                 }
751                 /*
752                  * Mounting a RDONLY partition read-write, so reread and
753                  * store the current valid flag.  (It may have been changed
754                  * by e2fsck since we originally mounted the partition.)
755                  */
756                 sb->u.ext2_sb.s_mount_state = le16_to_cpu(es->s_state);
757                 if (!ext2_setup_super (sb, es, 0))
758                         sb->s_flags &= ~MS_RDONLY;
759         }
760         ext2_sync_super(sb, es);
761         return 0;
762 }
763
764 int ext2_statfs (struct super_block * sb, struct statfs * buf)
765 {
766         unsigned long overhead;
767         int i;
768
769         if (test_opt (sb, MINIX_DF))
770                 overhead = 0;
771         else {
772                 /*
773                  * Compute the overhead (FS structures)
774                  */
775
776                 /*
777                  * All of the blocks before first_data_block are
778                  * overhead
779                  */
780                 overhead = le32_to_cpu(sb->u.ext2_sb.s_es->s_first_data_block);
781
782                 /*
783                  * Add the overhead attributed to the superblock and
784                  * block group descriptors.  If the sparse superblocks
785                  * feature is turned on, then not all groups have this.
786                  */
787                 for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++)
788                         overhead += ext2_bg_has_super(sb, i) +
789                                 ext2_bg_num_gdb(sb, i);
790
791                 /*
792                  * Every block group has an inode bitmap, a block
793                  * bitmap, and an inode table.
794                  */
795                 overhead += (sb->u.ext2_sb.s_groups_count *
796                              (2 + sb->u.ext2_sb.s_itb_per_group));
797         }
798
799         buf->f_type = EXT2_SUPER_MAGIC;
800         buf->f_bsize = sb->s_blocksize;
801         buf->f_blocks = le32_to_cpu(sb->u.ext2_sb.s_es->s_blocks_count) - overhead;
802         buf->f_bfree = ext2_count_free_blocks (sb);
803         buf->f_bavail = buf->f_bfree - le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count);
804         if (buf->f_bfree < le32_to_cpu(sb->u.ext2_sb.s_es->s_r_blocks_count))
805                 buf->f_bavail = 0;
806         buf->f_files = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
807         buf->f_ffree = ext2_count_free_inodes (sb);
808         buf->f_namelen = EXT2_NAME_LEN;
809         return 0;
810 }
811
812 static DECLARE_FSTYPE_DEV(ext2_fs_type, "ext2", ext2_read_super);
813
814 static int __init init_ext2_fs(void)
815 {
816         return register_filesystem(&ext2_fs_type);
817 }
818
819 static void __exit exit_ext2_fs(void)
820 {
821         unregister_filesystem(&ext2_fs_type);
822 }
823
824 EXPORT_NO_SYMBOLS;
825
826 module_init(init_ext2_fs)
827 module_exit(exit_ext2_fs)