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