added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / fs / ext3 / super.c
1 /*
2  *  linux/fs/ext3/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/sched.h>
24 #include <linux/jbd.h>
25 #include <linux/ext3_fs.h>
26 #include <linux/ext3_jbd.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/locks.h>
30 #include <linux/blkdev.h>
31 #include <linux/smp_lock.h>
32 #include <linux/random.h>
33 #include <asm/uaccess.h>
34
35 #ifdef CONFIG_JBD_DEBUG
36 static int ext3_ro_after; /* Make fs read-only after this many jiffies */
37 #endif
38
39 static int ext3_load_journal(struct super_block *, struct ext3_super_block *);
40 static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
41                                int);
42 static void ext3_commit_super (struct super_block * sb,
43                                struct ext3_super_block * es,
44                                int sync);
45 static void ext3_mark_recovery_complete(struct super_block * sb,
46                                         struct ext3_super_block * es);
47 static void ext3_clear_journal_err(struct super_block * sb,
48                                    struct ext3_super_block * es);
49
50 static int ext3_sync_fs(struct super_block * sb);
51
52 #ifdef CONFIG_JBD_DEBUG
53 int journal_no_write[2];
54
55 /*
56  * Debug code for turning filesystems "read-only" after a specified
57  * amount of time.  This is for crash/recovery testing.
58  */
59
60 static void make_rdonly(kdev_t dev, int *no_write)
61 {
62         if (dev) {
63                 printk(KERN_WARNING "Turning device %s read-only\n", 
64                        bdevname(dev));
65                 *no_write = 0xdead0000 + dev;
66         }
67 }
68
69 static void turn_fs_readonly(unsigned long arg)
70 {
71         struct super_block *sb = (struct super_block *)arg;
72
73         make_rdonly(sb->s_dev, &journal_no_write[0]);
74         make_rdonly(EXT3_SB(sb)->s_journal->j_dev, &journal_no_write[1]);
75         wake_up(&EXT3_SB(sb)->ro_wait_queue);
76 }
77
78 static void setup_ro_after(struct super_block *sb)
79 {
80         struct ext3_sb_info *sbi = EXT3_SB(sb);
81         init_timer(&sbi->turn_ro_timer);
82         if (ext3_ro_after) {
83                 printk(KERN_DEBUG "fs will go read-only in %d jiffies\n",
84                        ext3_ro_after);
85                 init_waitqueue_head(&sbi->ro_wait_queue);
86                 journal_no_write[0] = 0;
87                 journal_no_write[1] = 0;
88                 sbi->turn_ro_timer.function = turn_fs_readonly;
89                 sbi->turn_ro_timer.data = (unsigned long)sb;
90                 sbi->turn_ro_timer.expires = jiffies + ext3_ro_after;
91                 ext3_ro_after = 0;
92                 add_timer(&sbi->turn_ro_timer);
93         }
94 }
95
96 static void clear_ro_after(struct super_block *sb)
97 {
98         del_timer_sync(&EXT3_SB(sb)->turn_ro_timer);
99         journal_no_write[0] = 0;
100         journal_no_write[1] = 0;
101         ext3_ro_after = 0;
102 }
103 #else
104 #define setup_ro_after(sb)      do {} while (0)
105 #define clear_ro_after(sb)      do {} while (0)
106 #endif
107
108
109 static char error_buf[1024];
110
111 /* Determine the appropriate response to ext3_error on a given filesystem */
112
113 static int ext3_error_behaviour(struct super_block *sb)
114 {
115         /* First check for mount-time options */
116         if (test_opt (sb, ERRORS_PANIC))
117                 return EXT3_ERRORS_PANIC;
118         if (test_opt (sb, ERRORS_RO))
119                 return EXT3_ERRORS_RO;
120         if (test_opt (sb, ERRORS_CONT))
121                 return EXT3_ERRORS_CONTINUE;
122         
123         /* If no overrides were specified on the mount, then fall back
124          * to the default behaviour set in the filesystem's superblock
125          * on disk. */
126         switch (le16_to_cpu(sb->u.ext3_sb.s_es->s_errors)) {
127         case EXT3_ERRORS_PANIC:
128                 return EXT3_ERRORS_PANIC;
129         case EXT3_ERRORS_RO:
130                 return EXT3_ERRORS_RO;
131         default:
132                 break;
133         }
134         return EXT3_ERRORS_CONTINUE;
135 }
136
137 /* Deal with the reporting of failure conditions on a filesystem such as
138  * inconsistencies detected or read IO failures.
139  *
140  * On ext2, we can store the error state of the filesystem in the
141  * superblock.  That is not possible on ext3, because we may have other
142  * write ordering constraints on the superblock which prevent us from
143  * writing it out straight away; and given that the journal is about to
144  * be aborted, we can't rely on the current, or future, transactions to
145  * write out the superblock safely.
146  *
147  * We'll just use the journal_abort() error code to record an error in
148  * the journal instead.  On recovery, the journal will compain about
149  * that error until we've noted it down and cleared it.
150  */
151
152 static void ext3_handle_error(struct super_block *sb)
153 {
154         struct ext3_super_block *es = EXT3_SB(sb)->s_es;
155
156         EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
157         es->s_state |= cpu_to_le32(EXT3_ERROR_FS);
158
159         if (sb->s_flags & MS_RDONLY)
160                 return;
161
162         if (ext3_error_behaviour(sb) != EXT3_ERRORS_CONTINUE) {
163                 EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
164                 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
165         }
166
167         if (ext3_error_behaviour(sb) == EXT3_ERRORS_PANIC) 
168                 panic ("EXT3-fs (device %s): panic forced after error\n",
169                        bdevname(sb->s_dev));
170
171         if (ext3_error_behaviour(sb) == EXT3_ERRORS_RO) {
172                 printk (KERN_CRIT "Remounting filesystem read-only\n");
173                 sb->s_flags |= MS_RDONLY;
174         }
175
176         ext3_commit_super(sb, es, 1);
177 }
178
179 void ext3_error (struct super_block * sb, const char * function,
180                  const char * fmt, ...)
181 {
182         va_list args;
183
184         va_start (args, fmt);
185         vsprintf (error_buf, fmt, args);
186         va_end (args);
187
188         printk (KERN_CRIT "EXT3-fs error (device %s): %s: %s\n",
189                 bdevname(sb->s_dev), function, error_buf);
190
191         ext3_handle_error(sb);
192 }
193
194 const char *ext3_decode_error(struct super_block * sb, int errno, char nbuf[16])
195 {
196         char *errstr = NULL;
197         
198         switch (errno) {
199         case -EIO:
200                 errstr = "IO failure";
201                 break;
202         case -ENOMEM:
203                 errstr = "Out of memory";
204                 break;
205         case -EROFS:
206                 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
207                         errstr = "Journal has aborted";
208                 else
209                         errstr = "Readonly filesystem";
210                 break;
211         default:
212                 /* If the caller passed in an extra buffer for unknown
213                  * errors, textualise them now.  Else we just return
214                  * NULL. */
215                 if (nbuf) {
216                         /* Check for truncated error codes... */
217                         if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
218                                 errstr = nbuf;
219                 }
220                 
221                 break;
222         }
223
224         return errstr;
225 }
226
227 /* __ext3_std_error decodes expected errors from journaling functions
228  * automatically and invokes the appropriate error response.  */
229
230 void __ext3_std_error (struct super_block * sb, const char * function,
231                        int errno)
232 {
233         char nbuf[16];
234         const char *errstr = ext3_decode_error(sb, errno, nbuf);
235
236         printk (KERN_CRIT "EXT3-fs error (device %s) in %s: %s\n",
237                 bdevname(sb->s_dev), function, errstr);
238         
239         ext3_handle_error(sb);
240 }
241
242 /*
243  * ext3_abort is a much stronger failure handler than ext3_error.  The
244  * abort function may be used to deal with unrecoverable failures such
245  * as journal IO errors or ENOMEM at a critical moment in log management.
246  *
247  * We unconditionally force the filesystem into an ABORT|READONLY state,
248  * unless the error response on the fs has been set to panic in which
249  * case we take the easy way out and panic immediately.
250  */
251
252 void ext3_abort (struct super_block * sb, const char * function,
253                  const char * fmt, ...)
254 {
255         va_list args;
256
257         printk (KERN_CRIT "ext3_abort called.\n");
258
259         va_start (args, fmt);
260         vsprintf (error_buf, fmt, args);
261         va_end (args);
262
263         if (ext3_error_behaviour(sb) == EXT3_ERRORS_PANIC)
264                 panic ("EXT3-fs panic (device %s): %s: %s\n",
265                        bdevname(sb->s_dev), function, error_buf);
266
267         printk (KERN_CRIT "EXT3-fs abort (device %s): %s: %s\n",
268                 bdevname(sb->s_dev), function, error_buf);
269
270         if (sb->s_flags & MS_RDONLY)
271                 return;
272         
273         printk (KERN_CRIT "Remounting filesystem read-only\n");
274         sb->u.ext3_sb.s_mount_state |= EXT3_ERROR_FS;
275         sb->s_flags |= MS_RDONLY;
276         sb->u.ext3_sb.s_mount_opt |= EXT3_MOUNT_ABORT;
277         journal_abort(EXT3_SB(sb)->s_journal, -EIO);
278 }
279
280 /* Deal with the reporting of failure conditions while running, such as
281  * inconsistencies in operation or invalid system states.
282  *
283  * Use ext3_error() for cases of invalid filesystem states, as that will
284  * record an error on disk and force a filesystem check on the next boot.
285  */
286 NORET_TYPE void ext3_panic (struct super_block * sb, const char * function,
287                             const char * fmt, ...)
288 {
289         va_list args;
290
291         va_start (args, fmt);
292         vsprintf (error_buf, fmt, args);
293         va_end (args);
294
295         /* this is to prevent panic from syncing this filesystem */
296         /* AKPM: is this sufficient? */
297         sb->s_flags |= MS_RDONLY;
298         panic ("EXT3-fs panic (device %s): %s: %s\n",
299                bdevname(sb->s_dev), function, error_buf);
300 }
301
302 void ext3_warning (struct super_block * sb, const char * function,
303                    const char * fmt, ...)
304 {
305         va_list args;
306
307         va_start (args, fmt);
308         vsprintf (error_buf, fmt, args);
309         va_end (args);
310         printk (KERN_WARNING "EXT3-fs warning (device %s): %s: %s\n",
311                 bdevname(sb->s_dev), function, error_buf);
312 }
313
314 void ext3_update_dynamic_rev(struct super_block *sb)
315 {
316         struct ext3_super_block *es = EXT3_SB(sb)->s_es;
317
318         if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
319                 return;
320
321         ext3_warning(sb, __FUNCTION__,
322                      "updating to rev %d because of new feature flag, "
323                      "running e2fsck is recommended",
324                      EXT3_DYNAMIC_REV);
325
326         es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
327         es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
328         es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
329         /* leave es->s_feature_*compat flags alone */
330         /* es->s_uuid will be set by e2fsck if empty */
331
332         /*
333          * The rest of the superblock fields should be zero, and if not it
334          * means they are likely already in use, so leave them alone.  We
335          * can leave it up to e2fsck to clean up any inconsistencies there.
336          */
337 }
338
339 /*
340  * Open the external journal device
341  */
342 static struct block_device *ext3_blkdev_get(kdev_t dev)
343 {
344         struct block_device *bdev;
345         int err = -ENODEV;
346
347         bdev = bdget(kdev_t_to_nr(dev));
348         if (bdev == NULL)
349                 goto fail;
350         err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FS);
351         if (err < 0)
352                 goto fail;
353         return bdev;
354
355 fail:
356         printk(KERN_ERR "EXT3: failed to open journal device %s: %d\n",
357                         bdevname(dev), err);
358         return NULL;
359 }
360
361 /*
362  * Release the journal device
363  */
364 static int ext3_blkdev_put(struct block_device *bdev)
365 {
366         return blkdev_put(bdev, BDEV_FS);
367 }
368
369 static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
370 {
371         struct block_device *bdev;
372         int ret = -ENODEV;
373
374         bdev = sbi->journal_bdev;
375         if (bdev) {
376                 ret = ext3_blkdev_put(bdev);
377                 sbi->journal_bdev = 0;
378         }
379         return ret;
380 }
381
382 #define orphan_list_entry(l) list_entry((l), struct inode, u.ext3_i.i_orphan)
383
384 static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
385 {
386         struct list_head *l;
387         
388         printk(KERN_ERR "sb orphan head is %d\n", 
389                le32_to_cpu(sbi->s_es->s_last_orphan));
390         
391         printk(KERN_ERR "sb_info orphan list:\n");
392         list_for_each(l, &sbi->s_orphan) {
393                 struct inode *inode = orphan_list_entry(l);
394                 printk(KERN_ERR "  "
395                        "inode 0x%04x:%ld at %p: mode %o, nlink %d, next %d\n",
396                        inode->i_dev, inode->i_ino, inode,
397                        inode->i_mode, inode->i_nlink, 
398                        le32_to_cpu(NEXT_ORPHAN(inode)));
399         }
400 }
401
402 void ext3_put_super (struct super_block * sb)
403 {
404         struct ext3_sb_info *sbi = EXT3_SB(sb);
405         struct ext3_super_block *es = sbi->s_es;
406         kdev_t j_dev = sbi->s_journal->j_dev;
407         int i;
408
409         journal_destroy(sbi->s_journal);
410         if (!(sb->s_flags & MS_RDONLY)) {
411                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
412                 es->s_state = le16_to_cpu(sbi->s_mount_state);
413                 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
414                 mark_buffer_dirty(sbi->s_sbh);
415                 ext3_commit_super(sb, es, 1);
416         }
417
418         for (i = 0; i < sbi->s_gdb_count; i++)
419                 brelse(sbi->s_group_desc[i]);
420         kfree(sbi->s_group_desc);
421         for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++)
422                 brelse(sbi->s_inode_bitmap[i]);
423         for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++)
424                 brelse(sbi->s_block_bitmap[i]);
425         brelse(sbi->s_sbh);
426
427         /* Debugging code just in case the in-memory inode orphan list
428          * isn't empty.  The on-disk one can be non-empty if we've
429          * detected an error and taken the fs readonly, but the
430          * in-memory list had better be clean by this point. */
431         if (!list_empty(&sbi->s_orphan))
432                 dump_orphan_list(sb, sbi);
433         J_ASSERT(list_empty(&sbi->s_orphan));
434
435         invalidate_buffers(sb->s_dev);
436         if (j_dev != sb->s_dev) {
437                 /*
438                  * Invalidate the journal device's buffers.  We don't want them
439                  * floating about in memory - the physical journal device may
440                  * hotswapped, and it breaks the `ro-after' testing code.
441                  */
442                 fsync_no_super(j_dev);
443                 invalidate_buffers(j_dev);
444                 ext3_blkdev_remove(sbi);
445         }
446         clear_ro_after(sb);
447
448         return;
449 }
450
451 static struct super_operations ext3_sops = {
452         read_inode:     ext3_read_inode,        /* BKL held */
453         write_inode:    ext3_write_inode,       /* BKL not held.  Don't need */
454         dirty_inode:    ext3_dirty_inode,       /* BKL not held.  We take it */
455         put_inode:      ext3_put_inode,         /* BKL not held.  Don't need */
456         delete_inode:   ext3_delete_inode,      /* BKL not held.  We take it */
457         put_super:      ext3_put_super,         /* BKL held */
458         write_super:    ext3_write_super,       /* BKL held */
459         sync_fs:        ext3_sync_fs,
460         write_super_lockfs: ext3_write_super_lockfs, /* BKL not held. Take it */
461         unlockfs:       ext3_unlockfs,          /* BKL not held.  We take it */
462         statfs:         ext3_statfs,            /* BKL held */
463         remount_fs:     ext3_remount,           /* BKL held */
464 };
465
466 static int want_value(char *value, char *option)
467 {
468         if (!value || !*value) {
469                 printk(KERN_NOTICE "EXT3-fs: the %s option needs an argument\n",
470                        option);
471                 return -1;
472         }
473         return 0;
474 }
475
476 static int want_null_value(char *value, char *option)
477 {
478         if (*value) {
479                 printk(KERN_NOTICE "EXT3-fs: Invalid %s argument: %s\n",
480                        option, value);
481                 return -1;
482         }
483         return 0;
484 }
485
486 static int want_numeric(char *value, char *option, unsigned long *number)
487 {
488         if (want_value(value, option))
489                 return -1;
490         *number = simple_strtoul(value, &value, 0);
491         if (want_null_value(value, option))
492                 return -1;
493         return 0;
494 }
495
496 /*
497  * This function has been shamelessly adapted from the msdos fs
498  */
499 static int parse_options (char * options, unsigned long * sb_block,
500                           struct ext3_sb_info *sbi,
501                           unsigned long * inum,
502                           int is_remount)
503 {
504         unsigned long *mount_options = &sbi->s_mount_opt;
505         uid_t *resuid = &sbi->s_resuid;
506         gid_t *resgid = &sbi->s_resgid;
507         char * this_char;
508         char * value;
509
510         if (!options)
511                 return 1;
512         for (this_char = strtok (options, ",");
513              this_char != NULL;
514              this_char = strtok (NULL, ",")) {
515                 if ((value = strchr (this_char, '=')) != NULL)
516                         *value++ = 0;
517                 if (!strcmp (this_char, "bsddf"))
518                         clear_opt (*mount_options, MINIX_DF);
519                 else if (!strcmp (this_char, "nouid32")) {
520                         set_opt (*mount_options, NO_UID32);
521                 }
522                 else if (!strcmp (this_char, "abort"))
523                         set_opt (*mount_options, ABORT);
524                 else if (!strcmp (this_char, "check")) {
525                         if (!value || !*value || !strcmp (value, "none"))
526                                 clear_opt (*mount_options, CHECK);
527                         else
528 #ifdef CONFIG_EXT3_CHECK
529                                 set_opt (*mount_options, CHECK);
530 #else
531                                 printk(KERN_ERR 
532                                        "EXT3 Check option not supported\n");
533 #endif
534                 }
535                 else if (!strcmp (this_char, "debug"))
536                         set_opt (*mount_options, DEBUG);
537                 else if (!strcmp (this_char, "errors")) {
538                         if (want_value(value, "errors"))
539                                 return 0;
540                         if (!strcmp (value, "continue")) {
541                                 clear_opt (*mount_options, ERRORS_RO);
542                                 clear_opt (*mount_options, ERRORS_PANIC);
543                                 set_opt (*mount_options, ERRORS_CONT);
544                         }
545                         else if (!strcmp (value, "remount-ro")) {
546                                 clear_opt (*mount_options, ERRORS_CONT);
547                                 clear_opt (*mount_options, ERRORS_PANIC);
548                                 set_opt (*mount_options, ERRORS_RO);
549                         }
550                         else if (!strcmp (value, "panic")) {
551                                 clear_opt (*mount_options, ERRORS_CONT);
552                                 clear_opt (*mount_options, ERRORS_RO);
553                                 set_opt (*mount_options, ERRORS_PANIC);
554                         }
555                         else {
556                                 printk (KERN_ERR
557                                         "EXT3-fs: Invalid errors option: %s\n",
558                                         value);
559                                 return 0;
560                         }
561                 }
562                 else if (!strcmp (this_char, "grpid") ||
563                          !strcmp (this_char, "bsdgroups"))
564                         set_opt (*mount_options, GRPID);
565                 else if (!strcmp (this_char, "minixdf"))
566                         set_opt (*mount_options, MINIX_DF);
567                 else if (!strcmp (this_char, "nocheck"))
568                         clear_opt (*mount_options, CHECK);
569                 else if (!strcmp (this_char, "nogrpid") ||
570                          !strcmp (this_char, "sysvgroups"))
571                         clear_opt (*mount_options, GRPID);
572                 else if (!strcmp (this_char, "resgid")) {
573                         unsigned long v;
574                         if (want_numeric(value, "resgid", &v))
575                                 return 0;
576                         *resgid = v;
577                 }
578                 else if (!strcmp (this_char, "resuid")) {
579                         unsigned long v;
580                         if (want_numeric(value, "resuid", &v))
581                                 return 0;
582                         *resuid = v;
583                 }
584                 else if (!strcmp (this_char, "sb")) {
585                         if (want_numeric(value, "sb", sb_block))
586                                 return 0;
587                 }
588 #ifdef CONFIG_JBD_DEBUG
589                 else if (!strcmp (this_char, "ro-after")) {
590                         unsigned long v;
591                         if (want_numeric(value, "ro-after", &v))
592                                 return 0;
593                         ext3_ro_after = v;
594                 }
595 #endif
596                 /* Silently ignore the quota options */
597                 else if (!strcmp (this_char, "grpquota")
598                          || !strcmp (this_char, "noquota")
599                          || !strcmp (this_char, "quota")
600                          || !strcmp (this_char, "usrquota"))
601                         /* Don't do anything ;-) */ ;
602                 else if (!strcmp (this_char, "journal")) {
603                         /* @@@ FIXME */
604                         /* Eventually we will want to be able to create
605                            a journal file here.  For now, only allow the
606                            user to specify an existing inode to be the
607                            journal file. */
608                         if (is_remount) {
609                                 printk(KERN_ERR "EXT3-fs: cannot specify "
610                                        "journal on remount\n");
611                                 return 0;
612                         }
613
614                         if (want_value(value, "journal"))
615                                 return 0;
616                         if (!strcmp (value, "update"))
617                                 set_opt (*mount_options, UPDATE_JOURNAL);
618                         else if (want_numeric(value, "journal", inum))
619                                 return 0;
620                 }
621                 else if (!strcmp (this_char, "noload"))
622                         set_opt (*mount_options, NOLOAD);
623                 else if (!strcmp (this_char, "data")) {
624                         int data_opt = 0;
625
626                         if (want_value(value, "data"))
627                                 return 0;
628                         if (!strcmp (value, "journal"))
629                                 data_opt = EXT3_MOUNT_JOURNAL_DATA;
630                         else if (!strcmp (value, "ordered"))
631                                 data_opt = EXT3_MOUNT_ORDERED_DATA;
632                         else if (!strcmp (value, "writeback"))
633                                 data_opt = EXT3_MOUNT_WRITEBACK_DATA;
634                         else {
635                                 printk (KERN_ERR 
636                                         "EXT3-fs: Invalid data option: %s\n",
637                                         value);
638                                 return 0;
639                         }
640                         if (is_remount) {
641                                 if ((*mount_options & EXT3_MOUNT_DATA_FLAGS) !=
642                                                         data_opt) {
643                                         printk(KERN_ERR
644                                                "EXT3-fs: cannot change data "
645                                                "mode on remount\n");
646                                         return 0;
647                                 }
648                         } else {
649                                 *mount_options &= ~EXT3_MOUNT_DATA_FLAGS;
650                                 *mount_options |= data_opt;
651                         }
652                 } else if (!strcmp (this_char, "commit")) {
653                         unsigned long v;
654                         if (want_numeric(value, "commit", &v))
655                                 return 0;
656                         sbi->s_commit_interval = (HZ * v);
657                 } else {
658                         printk (KERN_ERR 
659                                 "EXT3-fs: Unrecognized mount option %s\n",
660                                 this_char);
661                         return 0;
662                 }
663         }
664         return 1;
665 }
666
667 static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
668                             int read_only)
669 {
670         struct ext3_sb_info *sbi = EXT3_SB(sb);
671         int res = 0;
672
673         if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
674                 printk (KERN_ERR "EXT3-fs warning: revision level too high, "
675                         "forcing read-only mode\n");
676                 res = MS_RDONLY;
677         }
678         if (read_only)
679                 return res;
680         if (!(sbi->s_mount_state & EXT3_VALID_FS))
681                 printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
682                         "running e2fsck is recommended\n");
683         else if ((sbi->s_mount_state & EXT3_ERROR_FS))
684                 printk (KERN_WARNING
685                         "EXT3-fs warning: mounting fs with errors, "
686                         "running e2fsck is recommended\n");
687         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
688                  le16_to_cpu(es->s_mnt_count) >=
689                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
690                 printk (KERN_WARNING
691                         "EXT3-fs warning: maximal mount count reached, "
692                         "running e2fsck is recommended\n");
693         else if (le32_to_cpu(es->s_checkinterval) &&
694                 (le32_to_cpu(es->s_lastcheck) +
695                         le32_to_cpu(es->s_checkinterval) <= CURRENT_TIME))
696                 printk (KERN_WARNING
697                         "EXT3-fs warning: checktime reached, "
698                         "running e2fsck is recommended\n");
699 #if 0
700                 /* @@@ We _will_ want to clear the valid bit if we find
701                    inconsistencies, to force a fsck at reboot.  But for
702                    a plain journaled filesystem we can keep it set as
703                    valid forever! :) */
704         es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
705 #endif
706         if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
707                 es->s_max_mnt_count =
708                         (__s16) cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
709         es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
710         es->s_mtime = cpu_to_le32(CURRENT_TIME);
711         ext3_update_dynamic_rev(sb);
712         EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
713         ext3_commit_super (sb, es, 1);
714         if (test_opt (sb, DEBUG))
715                 printk (KERN_INFO
716                         "[EXT3 FS %s, %s, bs=%lu, gc=%lu, "
717                         "bpg=%lu, ipg=%lu, mo=%04lx]\n",
718                         EXT3FS_VERSION, EXT3FS_DATE, sb->s_blocksize,
719                         sbi->s_groups_count,
720                         EXT3_BLOCKS_PER_GROUP(sb),
721                         EXT3_INODES_PER_GROUP(sb),
722                         sbi->s_mount_opt);
723         printk(KERN_INFO "EXT3 FS " EXT3FS_VERSION ", " EXT3FS_DATE " on %s, ",
724                                 bdevname(sb->s_dev));
725         if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
726                 printk("external journal on %s\n",
727                                 bdevname(EXT3_SB(sb)->s_journal->j_dev));
728         } else {
729                 printk("internal journal\n");
730         }
731 #ifdef CONFIG_EXT3_CHECK
732         if (test_opt (sb, CHECK)) {
733                 ext3_check_blocks_bitmap (sb);
734                 ext3_check_inodes_bitmap (sb);
735         }
736 #endif
737         setup_ro_after(sb);
738         return res;
739 }
740
741 static int ext3_check_descriptors (struct super_block * sb)
742 {
743         struct ext3_sb_info *sbi = EXT3_SB(sb);
744         unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
745         struct ext3_group_desc * gdp = NULL;
746         int desc_block = 0;
747         int i;
748
749         ext3_debug ("Checking group descriptors");
750
751         for (i = 0; i < sbi->s_groups_count; i++)
752         {
753                 if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
754                         gdp = (struct ext3_group_desc *)
755                                         sbi->s_group_desc[desc_block++]->b_data;
756                 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
757                     le32_to_cpu(gdp->bg_block_bitmap) >=
758                                 block + EXT3_BLOCKS_PER_GROUP(sb))
759                 {
760                         ext3_error (sb, "ext3_check_descriptors",
761                                     "Block bitmap for group %d"
762                                     " not in group (block %lu)!",
763                                     i, (unsigned long)
764                                         le32_to_cpu(gdp->bg_block_bitmap));
765                         return 0;
766                 }
767                 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
768                     le32_to_cpu(gdp->bg_inode_bitmap) >=
769                                 block + EXT3_BLOCKS_PER_GROUP(sb))
770                 {
771                         ext3_error (sb, "ext3_check_descriptors",
772                                     "Inode bitmap for group %d"
773                                     " not in group (block %lu)!",
774                                     i, (unsigned long)
775                                         le32_to_cpu(gdp->bg_inode_bitmap));
776                         return 0;
777                 }
778                 if (le32_to_cpu(gdp->bg_inode_table) < block ||
779                     le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
780                     block + EXT3_BLOCKS_PER_GROUP(sb))
781                 {
782                         ext3_error (sb, "ext3_check_descriptors",
783                                     "Inode table for group %d"
784                                     " not in group (block %lu)!",
785                                     i, (unsigned long)
786                                         le32_to_cpu(gdp->bg_inode_table));
787                         return 0;
788                 }
789                 block += EXT3_BLOCKS_PER_GROUP(sb);
790                 gdp++;
791         }
792         return 1;
793 }
794
795
796 /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
797  * the superblock) which were deleted from all directories, but held open by
798  * a process at the time of a crash.  We walk the list and try to delete these
799  * inodes at recovery time (only with a read-write filesystem).
800  *
801  * In order to keep the orphan inode chain consistent during traversal (in
802  * case of crash during recovery), we link each inode into the superblock
803  * orphan list_head and handle it the same way as an inode deletion during
804  * normal operation (which journals the operations for us).
805  *
806  * We only do an iget() and an iput() on each inode, which is very safe if we
807  * accidentally point at an in-use or already deleted inode.  The worst that
808  * can happen in this case is that we get a "bit already cleared" message from
809  * ext3_free_inode().  The only reason we would point at a wrong inode is if
810  * e2fsck was run on this filesystem, and it must have already done the orphan
811  * inode cleanup for us, so we can safely abort without any further action.
812  */
813 static void ext3_orphan_cleanup (struct super_block * sb,
814                                  struct ext3_super_block * es)
815 {
816         unsigned int s_flags = sb->s_flags;
817         int nr_orphans = 0, nr_truncates = 0;
818         if (!es->s_last_orphan) {
819                 jbd_debug(4, "no orphan inodes to clean up\n");
820                 return;
821         }
822
823         if (s_flags & MS_RDONLY) {
824                 printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
825                        bdevname(sb->s_dev));
826                 sb->s_flags &= ~MS_RDONLY;
827         }
828
829         if (sb->u.ext3_sb.s_mount_state & EXT3_ERROR_FS) {
830                 if (es->s_last_orphan)
831                         jbd_debug(1, "Errors on filesystem, "
832                                   "clearing orphan list.\n");
833                 es->s_last_orphan = 0;
834                 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
835                 return;
836         }
837
838         while (es->s_last_orphan) {
839                 struct inode *inode;
840
841                 if (!(inode =
842                       ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
843                         es->s_last_orphan = 0;
844                         break;
845                 }
846
847                 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
848                 if (inode->i_nlink) {
849                         printk(KERN_DEBUG "%s: truncating inode %ld to %Ld "
850                                 "bytes\n", __FUNCTION__, inode->i_ino,
851                                 inode->i_size);
852                         jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
853                                   inode->i_ino, inode->i_size);
854                         ext3_truncate(inode);
855                         nr_truncates++;
856                 } else {
857                         printk(KERN_DEBUG "%s: deleting unreferenced "
858                                 "inode %ld\n", __FUNCTION__, inode->i_ino);
859                         jbd_debug(2, "deleting unreferenced inode %ld\n",
860                                   inode->i_ino);
861                         nr_orphans++;
862                 }
863                 iput(inode);  /* The delete magic happens here! */
864         }
865
866 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
867
868         if (nr_orphans)
869                 printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
870                        bdevname(sb->s_dev), PLURAL(nr_orphans));
871         if (nr_truncates)
872                 printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
873                        bdevname(sb->s_dev), PLURAL(nr_truncates));
874         sb->s_flags = s_flags; /* Restore MS_RDONLY status */
875 }
876
877 #define log2(n) ffz(~(n))
878
879 /*
880  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
881  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
882  * We need to be 1 filesystem block less than the 2^32 sector limit.
883  */
884 static loff_t ext3_max_size(int bits)
885 {
886         loff_t res = EXT3_NDIR_BLOCKS;
887         res += 1LL << (bits-2);
888         res += 1LL << (2*(bits-2));
889         res += 1LL << (3*(bits-2));
890         res <<= bits;
891         if (res > (512LL << 32) - (1 << bits))
892                 res = (512LL << 32) - (1 << bits);
893         return res;
894 }
895
896 struct super_block * ext3_read_super (struct super_block * sb, void * data,
897                                       int silent)
898 {
899         struct buffer_head * bh;
900         struct ext3_super_block *es = 0;
901         struct ext3_sb_info *sbi = EXT3_SB(sb);
902         unsigned long sb_block = 1;
903         unsigned long logic_sb_block = 1;
904         unsigned long offset = 0;
905         unsigned long journal_inum = 0;
906         kdev_t dev = sb->s_dev;
907         int blocksize;
908         int hblock;
909         int db_count;
910         int i;
911         int needs_recovery;
912
913 #ifdef CONFIG_JBD_DEBUG
914         ext3_ro_after = 0;
915 #endif
916         /*
917          * See what the current blocksize for the device is, and
918          * use that as the blocksize.  Otherwise (or if the blocksize
919          * is smaller than the default) use the default.
920          * This is important for devices that have a hardware
921          * sectorsize that is larger than the default.
922          */
923         blocksize = EXT3_MIN_BLOCK_SIZE;
924         hblock = get_hardsect_size(dev);
925         if (blocksize < hblock)
926                 blocksize = hblock;
927
928         sbi->s_mount_opt = 0;
929         sbi->s_resuid = EXT3_DEF_RESUID;
930         sbi->s_resgid = EXT3_DEF_RESGID;
931         if (!parse_options ((char *) data, &sb_block, sbi, &journal_inum, 0)) {
932                 sb->s_dev = 0;
933                 goto out_fail;
934         }
935
936         sb->s_blocksize = blocksize;
937         set_blocksize (dev, blocksize);
938
939         /*
940          * The ext3 superblock will not be buffer aligned for other than 1kB
941          * block sizes.  We need to calculate the offset from buffer start.
942          */
943         if (blocksize != EXT3_MIN_BLOCK_SIZE) {
944                 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
945                 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
946         }
947
948         if (!(bh = sb_bread(sb, logic_sb_block))) {
949                 printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
950                 goto out_fail;
951         }
952         /*
953          * Note: s_es must be initialized as soon as possible because
954          *       some ext3 macro-instructions depend on its value
955          */
956         es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
957         sbi->s_es = es;
958         sb->s_magic = le16_to_cpu(es->s_magic);
959         if (sb->s_magic != EXT3_SUPER_MAGIC) {
960                 if (!silent)
961                         printk(KERN_ERR 
962                                "VFS: Can't find ext3 filesystem on dev %s.\n",
963                                bdevname(dev));
964                 goto failed_mount;
965         }
966         if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
967             (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
968              EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
969              EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
970                 printk(KERN_WARNING 
971                        "EXT3-fs warning: feature flags set on rev 0 fs, "
972                        "running e2fsck is recommended\n");
973         /*
974          * Check feature flags regardless of the revision level, since we
975          * previously didn't change the revision level when setting the flags,
976          * so there is a chance incompat flags are set on a rev 0 filesystem.
977          */
978         if ((i = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))) {
979                 printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
980                        "unsupported optional features (%x).\n",
981                        bdevname(dev), i);
982                 goto failed_mount;
983         }
984         if (!(sb->s_flags & MS_RDONLY) &&
985             (i = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))){
986                 printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
987                        "unsupported optional features (%x).\n",
988                        bdevname(dev), i);
989                 goto failed_mount;
990         }
991         sb->s_blocksize_bits = le32_to_cpu(es->s_log_block_size) + 10;
992         sb->s_blocksize = 1 << sb->s_blocksize_bits;
993
994         if (sb->s_blocksize < EXT3_MIN_BLOCK_SIZE ||
995             sb->s_blocksize > EXT3_MAX_BLOCK_SIZE) {
996                 printk(KERN_ERR 
997                        "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
998                        blocksize, bdevname(dev));
999                 goto failed_mount;
1000         }
1001
1002         sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1003
1004         if (sb->s_blocksize != blocksize) {
1005                 blocksize = sb->s_blocksize;
1006
1007                 /*
1008                  * Make sure the blocksize for the filesystem is larger
1009                  * than the hardware sectorsize for the machine.
1010                  */
1011                 if (sb->s_blocksize < hblock) {
1012                         printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
1013                                "device blocksize %d.\n", blocksize, hblock);
1014                         goto failed_mount;
1015                 }
1016
1017                 brelse (bh);
1018                 set_blocksize (dev, sb->s_blocksize);
1019                 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1020                 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1021                 bh = sb_bread(sb, logic_sb_block);
1022                 if (!bh) {
1023                         printk(KERN_ERR 
1024                                "EXT3-fs: Can't read superblock on 2nd try.\n");
1025                         return NULL;
1026                 }
1027                 es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
1028                 sbi->s_es = es;
1029                 if (es->s_magic != le16_to_cpu(EXT3_SUPER_MAGIC)) {
1030                         printk (KERN_ERR 
1031                                 "EXT3-fs: Magic mismatch, very weird !\n");
1032                         goto failed_mount;
1033                 }
1034         }
1035
1036         if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1037                 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1038                 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1039         } else {
1040                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1041                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1042                 if (sbi->s_inode_size != EXT3_GOOD_OLD_INODE_SIZE) {
1043                         printk (KERN_ERR
1044                                 "EXT3-fs: unsupported inode size: %d\n",
1045                                 sbi->s_inode_size);
1046                         goto failed_mount;
1047                 }
1048         }
1049         sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1050                                    le32_to_cpu(es->s_log_frag_size);
1051         if (blocksize != sbi->s_frag_size) {
1052                 printk(KERN_ERR
1053                        "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
1054                        sbi->s_frag_size, blocksize);
1055                 goto failed_mount;
1056         }
1057         sbi->s_frags_per_block = 1;
1058         sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1059         sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1060         sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1061         sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1062         sbi->s_itb_per_group = sbi->s_inodes_per_group /sbi->s_inodes_per_block;
1063         sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1064         sbi->s_sbh = bh;
1065         if (sbi->s_resuid == EXT3_DEF_RESUID)
1066                 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1067         if (sbi->s_resgid == EXT3_DEF_RESGID)
1068                 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1069         sbi->s_mount_state = le16_to_cpu(es->s_state);
1070         sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
1071         sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
1072
1073         if (sbi->s_blocks_per_group > blocksize * 8) {
1074                 printk (KERN_ERR
1075                         "EXT3-fs: #blocks per group too big: %lu\n",
1076                         sbi->s_blocks_per_group);
1077                 goto failed_mount;
1078         }
1079         if (sbi->s_frags_per_group > blocksize * 8) {
1080                 printk (KERN_ERR
1081                         "EXT3-fs: #fragments per group too big: %lu\n",
1082                         sbi->s_frags_per_group);
1083                 goto failed_mount;
1084         }
1085         if (sbi->s_inodes_per_group > blocksize * 8) {
1086                 printk (KERN_ERR
1087                         "EXT3-fs: #inodes per group too big: %lu\n",
1088                         sbi->s_inodes_per_group);
1089                 goto failed_mount;
1090         }
1091
1092         sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
1093                                le32_to_cpu(es->s_first_data_block) +
1094                                EXT3_BLOCKS_PER_GROUP(sb) - 1) /
1095                               EXT3_BLOCKS_PER_GROUP(sb);
1096         db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
1097                    EXT3_DESC_PER_BLOCK(sb);
1098         sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1099                                     GFP_KERNEL);
1100         if (sbi->s_group_desc == NULL) {
1101                 printk (KERN_ERR "EXT3-fs: not enough memory\n");
1102                 goto failed_mount;
1103         }
1104         for (i = 0; i < db_count; i++) {
1105                 sbi->s_group_desc[i] = sb_bread(sb, logic_sb_block + i + 1);
1106                 if (!sbi->s_group_desc[i]) {
1107                         printk (KERN_ERR "EXT3-fs: "
1108                                 "can't read group descriptor %d\n", i);
1109                         db_count = i;
1110                         goto failed_mount2;
1111                 }
1112         }
1113         if (!ext3_check_descriptors (sb)) {
1114                 printk (KERN_ERR "EXT3-fs: group descriptors corrupted !\n");
1115                 goto failed_mount2;
1116         }
1117         for (i = 0; i < EXT3_MAX_GROUP_LOADED; i++) {
1118                 sbi->s_inode_bitmap_number[i] = 0;
1119                 sbi->s_inode_bitmap[i] = NULL;
1120                 sbi->s_block_bitmap_number[i] = 0;
1121                 sbi->s_block_bitmap[i] = NULL;
1122         }
1123         sbi->s_loaded_inode_bitmaps = 0;
1124         sbi->s_loaded_block_bitmaps = 0;
1125         sbi->s_gdb_count = db_count;
1126         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1127         /*
1128          * set up enough so that it can read an inode
1129          */
1130         sb->s_op = &ext3_sops;
1131         INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1132
1133         sb->s_root = 0;
1134
1135         needs_recovery = (es->s_last_orphan != 0 ||
1136                           EXT3_HAS_INCOMPAT_FEATURE(sb,
1137                                     EXT3_FEATURE_INCOMPAT_RECOVER));
1138
1139         /*
1140          * The first inode we look at is the journal inode.  Don't try
1141          * root first: it may be modified in the journal!
1142          */
1143         if (!test_opt(sb, NOLOAD) &&
1144             EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1145                 if (ext3_load_journal(sb, es))
1146                         goto failed_mount2;
1147         } else if (journal_inum) {
1148                 if (ext3_create_journal(sb, es, journal_inum))
1149                         goto failed_mount2;
1150         } else {
1151                 if (!silent)
1152                         printk (KERN_ERR
1153                                 "ext3: No journal on filesystem on %s\n",
1154                                 bdevname(dev));
1155                 goto failed_mount2;
1156         }
1157
1158         /* We have now updated the journal if required, so we can
1159          * validate the data journaling mode. */
1160         switch (test_opt(sb, DATA_FLAGS)) {
1161         case 0:
1162                 /* No mode set, assume a default based on the journal
1163                    capabilities: ORDERED_DATA if the journal can
1164                    cope, else JOURNAL_DATA */
1165                 if (journal_check_available_features
1166                     (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
1167                         set_opt(sbi->s_mount_opt, ORDERED_DATA);
1168                 else
1169                         set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1170                 break;
1171
1172         case EXT3_MOUNT_ORDERED_DATA:
1173         case EXT3_MOUNT_WRITEBACK_DATA:
1174                 if (!journal_check_available_features
1175                     (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
1176                         printk(KERN_ERR "EXT3-fs: Journal does not support "
1177                                "requested data journaling mode\n");
1178                         goto failed_mount3;
1179                 }
1180         default:
1181                 break;
1182         }
1183
1184         /*
1185          * The journal_load will have done any necessary log recovery,
1186          * so we can safely mount the rest of the filesystem now.
1187          */
1188
1189         sb->s_root = d_alloc_root(iget(sb, EXT3_ROOT_INO));
1190         if (!sb->s_root || !S_ISDIR(sb->s_root->d_inode->i_mode) ||
1191             !sb->s_root->d_inode->i_blocks || !sb->s_root->d_inode->i_size) {
1192                 if (sb->s_root) {
1193                         dput(sb->s_root);
1194                         sb->s_root = NULL;
1195                         printk(KERN_ERR
1196                                "EXT3-fs: corrupt root inode, run e2fsck\n");
1197                 } else
1198                         printk(KERN_ERR "EXT3-fs: get root inode failed\n");
1199                 goto failed_mount3;
1200         }
1201
1202         ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1203         /*
1204          * akpm: core read_super() calls in here with the superblock locked.
1205          * That deadlocks, because orphan cleanup needs to lock the superblock
1206          * in numerous places.  Here we just pop the lock - it's relatively
1207          * harmless, because we are now ready to accept write_super() requests,
1208          * and aviro says that's the only reason for hanging onto the
1209          * superblock lock.
1210          */
1211         EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
1212         unlock_super(sb);       /* akpm: sigh */
1213         ext3_orphan_cleanup(sb, es);
1214         lock_super(sb);
1215         EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
1216         if (needs_recovery)
1217                 printk (KERN_INFO "EXT3-fs: recovery complete.\n");
1218         ext3_mark_recovery_complete(sb, es);
1219         printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
1220                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
1221                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
1222                 "writeback");
1223
1224         return sb;
1225
1226 failed_mount3:
1227         journal_destroy(sbi->s_journal);
1228 failed_mount2:
1229         for (i = 0; i < db_count; i++)
1230                 brelse(sbi->s_group_desc[i]);
1231         kfree(sbi->s_group_desc);
1232 failed_mount:
1233         ext3_blkdev_remove(sbi);
1234         brelse(bh);
1235 out_fail:
1236         return NULL;
1237 }
1238
1239 /*
1240  * Setup any per-fs journal parameters now.  We'll do this both on
1241  * initial mount, once the journal has been initialised but before we've
1242  * done any recovery; and again on any subsequent remount. 
1243  */
1244 static void ext3_init_journal_params(struct ext3_sb_info *sbi, 
1245                                      journal_t *journal)
1246 {
1247         if (sbi->s_commit_interval)
1248                 journal->j_commit_interval = sbi->s_commit_interval;
1249         /* We could also set up an ext3-specific default for the commit
1250          * interval here, but for now we'll just fall back to the jbd
1251          * default. */
1252 }
1253
1254
1255 static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
1256 {
1257         struct inode *journal_inode;
1258         journal_t *journal;
1259
1260         /* First, test for the existence of a valid inode on disk.  Bad
1261          * things happen if we iget() an unused inode, as the subsequent
1262          * iput() will try to delete it. */
1263
1264         journal_inode = iget(sb, journal_inum);
1265         if (!journal_inode) {
1266                 printk(KERN_ERR "EXT3-fs: no journal found.\n");
1267                 return NULL;
1268         }
1269         if (!journal_inode->i_nlink) {
1270                 make_bad_inode(journal_inode);
1271                 iput(journal_inode);
1272                 printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
1273                 return NULL;
1274         }
1275
1276         jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1277                   journal_inode, journal_inode->i_size);
1278         if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
1279                 printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
1280                 iput(journal_inode);
1281                 return NULL;
1282         }
1283
1284         journal = journal_init_inode(journal_inode);
1285         if (!journal) {
1286                 printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
1287                 iput(journal_inode);
1288         }
1289         ext3_init_journal_params(EXT3_SB(sb), journal);
1290         return journal;
1291 }
1292
1293 static journal_t *ext3_get_dev_journal(struct super_block *sb,
1294                                        int dev)
1295 {
1296         struct buffer_head * bh;
1297         journal_t *journal;
1298         int start;
1299         int len;
1300         int hblock, blocksize;
1301         unsigned long sb_block;
1302         unsigned long offset;
1303         kdev_t journal_dev = to_kdev_t(dev);
1304         struct ext3_super_block * es;
1305         struct block_device *bdev;
1306
1307         bdev = ext3_blkdev_get(journal_dev);
1308         if (bdev == NULL)
1309                 return NULL;
1310
1311         blocksize = sb->s_blocksize;
1312         hblock = get_hardsect_size(journal_dev);
1313         if (blocksize < hblock) {
1314                 printk(KERN_ERR
1315                         "EXT3-fs: blocksize too small for journal device.\n");
1316                 goto out_bdev;
1317         }
1318         
1319         sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
1320         offset = EXT3_MIN_BLOCK_SIZE % blocksize;
1321         set_blocksize(dev, blocksize);
1322         if (!(bh = bread(dev, sb_block, blocksize))) {
1323                 printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
1324                        "external journal\n");
1325                 goto out_bdev;
1326         }
1327
1328         es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1329         if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
1330             !(le32_to_cpu(es->s_feature_incompat) &
1331               EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
1332                 printk(KERN_ERR "EXT3-fs: external journal has "
1333                                         "bad superblock\n");
1334                 brelse(bh);
1335                 goto out_bdev;
1336         }
1337
1338         if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
1339                 printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
1340                 brelse(bh);
1341                 goto out_bdev;
1342         }
1343
1344         len = le32_to_cpu(es->s_blocks_count);
1345         start = sb_block + 1;
1346         brelse(bh);     /* we're done with the superblock */
1347
1348         journal = journal_init_dev(journal_dev, sb->s_dev, 
1349                                         start, len, blocksize);
1350         if (!journal) {
1351                 printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
1352                 goto out_bdev;
1353         }
1354         ll_rw_block(READ, 1, &journal->j_sb_buffer);
1355         wait_on_buffer(journal->j_sb_buffer);
1356         if (!buffer_uptodate(journal->j_sb_buffer)) {
1357                 printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
1358                 goto out_journal;
1359         }
1360         if (ntohl(journal->j_superblock->s_nr_users) != 1) {
1361                 printk(KERN_ERR "EXT3-fs: External journal has more than one "
1362                                         "user (unsupported) - %d\n",
1363                         ntohl(journal->j_superblock->s_nr_users));
1364                 goto out_journal;
1365         }
1366         EXT3_SB(sb)->journal_bdev = bdev;
1367         ext3_init_journal_params(EXT3_SB(sb), journal);
1368         return journal;
1369 out_journal:
1370         journal_destroy(journal);
1371 out_bdev:
1372         ext3_blkdev_put(bdev);
1373         return NULL;
1374 }
1375
1376 static int ext3_load_journal(struct super_block * sb,
1377                              struct ext3_super_block * es)
1378 {
1379         journal_t *journal;
1380         int journal_inum = le32_to_cpu(es->s_journal_inum);
1381         int journal_dev = le32_to_cpu(es->s_journal_dev);
1382         int err = 0;
1383         int really_read_only;
1384
1385         really_read_only = is_read_only(sb->s_dev);
1386
1387         /*
1388          * Are we loading a blank journal or performing recovery after a
1389          * crash?  For recovery, we need to check in advance whether we
1390          * can get read-write access to the device.
1391          */
1392
1393         if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
1394                 if (sb->s_flags & MS_RDONLY) {
1395                         printk(KERN_INFO "EXT3-fs: INFO: recovery "
1396                                         "required on readonly filesystem.\n");
1397                         if (really_read_only) {
1398                                 printk(KERN_ERR "EXT3-fs: write access "
1399                                         "unavailable, cannot proceed.\n");
1400                                 return -EROFS;
1401                         }
1402                         printk (KERN_INFO "EXT3-fs: write access will "
1403                                         "be enabled during recovery.\n");
1404                 }
1405         }
1406
1407         if (journal_inum && journal_dev) {
1408                 printk(KERN_ERR "EXT3-fs: filesystem has both journal "
1409                        "and inode journals!\n");
1410                 return -EINVAL;
1411         }
1412
1413         if (journal_inum) {
1414                 if (!(journal = ext3_get_journal(sb, journal_inum)))
1415                         return -EINVAL;
1416         } else {
1417                 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
1418                         return -EINVAL;
1419         }
1420         
1421
1422         if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
1423                 err = journal_update_format(journal);
1424                 if (err)  {
1425                         printk(KERN_ERR "EXT3-fs: error updating journal.\n");
1426                         journal_destroy(journal);
1427                         return err;
1428                 }
1429         }
1430
1431         if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
1432                 err = journal_wipe(journal, !really_read_only);
1433         if (!err)
1434                 err = journal_load(journal);
1435
1436         if (err) {
1437                 printk(KERN_ERR "EXT3-fs: error loading journal.\n");
1438                 journal_destroy(journal);
1439                 return err;
1440         }
1441
1442         EXT3_SB(sb)->s_journal = journal;
1443         ext3_clear_journal_err(sb, es);
1444         return 0;
1445 }
1446
1447 static int ext3_create_journal(struct super_block * sb,
1448                                struct ext3_super_block * es,
1449                                int journal_inum)
1450 {
1451         journal_t *journal;
1452
1453         if (sb->s_flags & MS_RDONLY) {
1454                 printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
1455                                 "create journal.\n");
1456                 return -EROFS;
1457         }
1458
1459         if (!(journal = ext3_get_journal(sb, journal_inum)))
1460                 return -EINVAL;
1461
1462         printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
1463                journal_inum);
1464
1465         if (journal_create(journal)) {
1466                 printk(KERN_ERR "EXT3-fs: error creating journal.\n");
1467                 journal_destroy(journal);
1468                 return -EIO;
1469         }
1470
1471         EXT3_SB(sb)->s_journal = journal;
1472
1473         ext3_update_dynamic_rev(sb);
1474         EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1475         EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
1476
1477         es->s_journal_inum = cpu_to_le32(journal_inum);
1478         sb->s_dirt = 1;
1479
1480         /* Make sure we flush the recovery flag to disk. */
1481         ext3_commit_super(sb, es, 1);
1482
1483         return 0;
1484 }
1485
1486 static void ext3_commit_super (struct super_block * sb,
1487                                struct ext3_super_block * es,
1488                                int sync)
1489 {
1490         es->s_wtime = cpu_to_le32(CURRENT_TIME);
1491         BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "marking dirty");
1492         mark_buffer_dirty(sb->u.ext3_sb.s_sbh);
1493         if (sync) {
1494                 ll_rw_block(WRITE, 1, &sb->u.ext3_sb.s_sbh);
1495                 wait_on_buffer(sb->u.ext3_sb.s_sbh);
1496         }
1497 }
1498
1499
1500 /*
1501  * Have we just finished recovery?  If so, and if we are mounting (or
1502  * remounting) the filesystem readonly, then we will end up with a
1503  * consistent fs on disk.  Record that fact.
1504  */
1505 static void ext3_mark_recovery_complete(struct super_block * sb,
1506                                         struct ext3_super_block * es)
1507 {
1508         journal_flush(EXT3_SB(sb)->s_journal);
1509         if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
1510             sb->s_flags & MS_RDONLY) {
1511                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1512                 sb->s_dirt = 0;
1513                 ext3_commit_super(sb, es, 1);
1514         }
1515 }
1516
1517 /*
1518  * If we are mounting (or read-write remounting) a filesystem whose journal
1519  * has recorded an error from a previous lifetime, move that error to the
1520  * main filesystem now.
1521  */
1522 static void ext3_clear_journal_err(struct super_block * sb,
1523                                    struct ext3_super_block * es)
1524 {
1525         journal_t *journal;
1526         int j_errno;
1527         const char *errstr;
1528         
1529         journal = EXT3_SB(sb)->s_journal;
1530
1531         /*
1532          * Now check for any error status which may have been recorded in the
1533          * journal by a prior ext3_error() or ext3_abort()
1534          */
1535
1536         j_errno = journal_errno(journal);
1537         if (j_errno) {
1538                 char nbuf[16];
1539                 
1540                 errstr = ext3_decode_error(sb, j_errno, nbuf);
1541                 ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
1542                              "from previous mount: %s", errstr);
1543                 ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
1544                              "filesystem check.");
1545                 
1546                 sb->u.ext3_sb.s_mount_state |= EXT3_ERROR_FS;
1547                 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
1548                 ext3_commit_super (sb, es, 1);
1549
1550                 journal_clear_err(journal);
1551         }
1552 }
1553
1554 /*
1555  * Force the running and committing transactions to commit,
1556  * and wait on the commit.
1557  */
1558 int ext3_force_commit(struct super_block *sb)
1559 {
1560         journal_t *journal;
1561         int ret;
1562
1563         if (sb->s_flags & MS_RDONLY)
1564                 return 0;
1565
1566         journal = EXT3_SB(sb)->s_journal;
1567         sb->s_dirt = 0;
1568         lock_kernel();  /* important: lock down j_running_transaction */
1569         ret = ext3_journal_force_commit(journal);
1570         unlock_kernel();
1571         return ret;
1572 }
1573
1574 /*
1575  * Ext3 always journals updates to the superblock itself, so we don't
1576  * have to propagate any other updates to the superblock on disk at this
1577  * point.  Just start an async writeback to get the buffers on their way
1578  * to the disk.
1579  *
1580  * This implicitly triggers the writebehind on sync().
1581  */
1582
1583 void ext3_write_super (struct super_block * sb)
1584 {
1585         if (down_trylock(&sb->s_lock) == 0)
1586                 BUG();
1587         sb->s_dirt = 0;
1588         log_start_commit(EXT3_SB(sb)->s_journal, NULL);
1589 }
1590
1591 static int ext3_sync_fs(struct super_block *sb)
1592 {
1593         tid_t target;
1594         
1595         sb->s_dirt = 0;
1596         target = log_start_commit(EXT3_SB(sb)->s_journal, NULL);
1597         log_wait_commit(EXT3_SB(sb)->s_journal, target);
1598         return 0;
1599 }
1600
1601 /*
1602  * LVM calls this function before a (read-only) snapshot is created.  This
1603  * gives us a chance to flush the journal completely and mark the fs clean.
1604  */
1605 void ext3_write_super_lockfs(struct super_block *sb)
1606 {
1607         sb->s_dirt = 0;
1608
1609         lock_kernel();          /* 2.4.5 forgot to do this for us */
1610         if (!(sb->s_flags & MS_RDONLY)) {
1611                 journal_t *journal = EXT3_SB(sb)->s_journal;
1612
1613                 /* Now we set up the journal barrier. */
1614                 unlock_super(sb);
1615                 journal_lock_updates(journal);
1616                 journal_flush(journal);
1617                 lock_super(sb);
1618
1619                 /* Journal blocked and flushed, clear needs_recovery flag. */
1620                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1621                 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1622         }
1623         unlock_kernel();
1624 }
1625
1626 /*
1627  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
1628  * flag here, even though the filesystem is not technically dirty yet.
1629  */
1630 void ext3_unlockfs(struct super_block *sb)
1631 {
1632         if (!(sb->s_flags & MS_RDONLY)) {
1633                 lock_kernel();
1634                 lock_super(sb);
1635                 /* Reser the needs_recovery flag before the fs is unlocked. */
1636                 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1637                 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
1638                 unlock_super(sb);
1639                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
1640                 unlock_kernel();
1641         }
1642 }
1643
1644 int ext3_remount (struct super_block * sb, int * flags, char * data)
1645 {
1646         struct ext3_super_block * es;
1647         struct ext3_sb_info *sbi = EXT3_SB(sb);
1648         unsigned long tmp;
1649
1650         clear_ro_after(sb);
1651
1652         /*
1653          * Allow the "check" option to be passed as a remount option.
1654          */
1655         if (!parse_options(data, &tmp, sbi, &tmp, 1))
1656                 return -EINVAL;
1657
1658         if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
1659                 ext3_abort(sb, __FUNCTION__, "Abort forced by user");
1660
1661         es = sbi->s_es;
1662
1663         ext3_init_journal_params(sbi, sbi->s_journal);
1664         
1665         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1666                 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
1667                         return -EROFS;
1668
1669                 if (*flags & MS_RDONLY) {
1670                         /*
1671                          * First of all, the unconditional stuff we have to do
1672                          * to disable replay of the journal when we next remount
1673                          */
1674                         sb->s_flags |= MS_RDONLY;
1675
1676                         /*
1677                          * OK, test if we are remounting a valid rw partition
1678                          * readonly, and if so set the rdonly flag and then
1679                          * mark the partition as valid again.
1680                          */
1681                         if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
1682                             (sbi->s_mount_state & EXT3_VALID_FS))
1683                                 es->s_state = cpu_to_le16(sbi->s_mount_state);
1684
1685                         ext3_mark_recovery_complete(sb, es);
1686                 } else {
1687                         int ret;
1688                         if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
1689                                         ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
1690                                 printk(KERN_WARNING "EXT3-fs: %s: couldn't "
1691                                        "remount RDWR because of unsupported "
1692                                        "optional features (%x).\n",
1693                                        bdevname(sb->s_dev), ret);
1694                                 return -EROFS;
1695                         }
1696                         /*
1697                          * Mounting a RDONLY partition read-write, so reread
1698                          * and store the current valid flag.  (It may have
1699                          * been changed by e2fsck since we originally mounted
1700                          * the partition.)
1701                          */
1702                         ext3_clear_journal_err(sb, es);
1703                         sbi->s_mount_state = le16_to_cpu(es->s_state);
1704                         if (!ext3_setup_super (sb, es, 0))
1705                                 sb->s_flags &= ~MS_RDONLY;
1706                 }
1707         }
1708         setup_ro_after(sb);
1709         return 0;
1710 }
1711
1712 int ext3_statfs (struct super_block * sb, struct statfs * buf)
1713 {
1714         struct ext3_super_block *es = EXT3_SB(sb)->s_es;
1715         unsigned long overhead;
1716         int i;
1717
1718         if (test_opt (sb, MINIX_DF))
1719                 overhead = 0;
1720         else {
1721                 /*
1722                  * Compute the overhead (FS structures)
1723                  */
1724
1725                 /*
1726                  * All of the blocks before first_data_block are
1727                  * overhead
1728                  */
1729                 overhead = le32_to_cpu(es->s_first_data_block);
1730
1731                 /*
1732                  * Add the overhead attributed to the superblock and
1733                  * block group descriptors.  If the sparse superblocks
1734                  * feature is turned on, then not all groups have this.
1735                  */
1736                 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1737                         overhead += ext3_bg_has_super(sb, i) +
1738                                 ext3_bg_num_gdb(sb, i);
1739
1740                 /*
1741                  * Every block group has an inode bitmap, a block
1742                  * bitmap, and an inode table.
1743                  */
1744                 overhead += (EXT3_SB(sb)->s_groups_count *
1745                              (2 + EXT3_SB(sb)->s_itb_per_group));
1746         }
1747
1748         buf->f_type = EXT3_SUPER_MAGIC;
1749         buf->f_bsize = sb->s_blocksize;
1750         buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
1751         buf->f_bfree = ext3_count_free_blocks (sb);
1752         buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
1753         if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
1754                 buf->f_bavail = 0;
1755         buf->f_files = le32_to_cpu(es->s_inodes_count);
1756         buf->f_ffree = ext3_count_free_inodes (sb);
1757         buf->f_namelen = EXT3_NAME_LEN;
1758         return 0;
1759 }
1760
1761 static DECLARE_FSTYPE_DEV(ext3_fs_type, "ext3", ext3_read_super);
1762
1763 static int __init init_ext3_fs(void)
1764 {
1765         return register_filesystem(&ext3_fs_type);
1766 }
1767
1768 static void __exit exit_ext3_fs(void)
1769 {
1770         unregister_filesystem(&ext3_fs_type);
1771 }
1772
1773 EXPORT_NO_SYMBOLS;
1774
1775 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
1776 MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
1777 MODULE_LICENSE("GPL");
1778 module_init(init_ext3_fs)
1779 module_exit(exit_ext3_fs)