[PATCH] knfsd: Make ext3 reject filehandles referring to invalid inode number
[linux-2.6.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/module.h>
20 #include <linux/string.h>
21 #include <linux/fs.h>
22 #include <linux/time.h>
23 #include <linux/jbd.h>
24 #include <linux/ext3_fs.h>
25 #include <linux/ext3_jbd.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/blkdev.h>
29 #include <linux/parser.h>
30 #include <linux/smp_lock.h>
31 #include <linux/buffer_head.h>
32 #include <linux/vfs.h>
33 #include <linux/random.h>
34 #include <linux/mount.h>
35 #include <linux/namei.h>
36 #include <linux/quotaops.h>
37 #include <linux/seq_file.h>
38
39 #include <asm/uaccess.h>
40
41 #include "xattr.h"
42 #include "acl.h"
43 #include "namei.h"
44
45 static int ext3_load_journal(struct super_block *, struct ext3_super_block *,
46                              unsigned long journal_devnum);
47 static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
48                                int);
49 static void ext3_commit_super (struct super_block * sb,
50                                struct ext3_super_block * es,
51                                int sync);
52 static void ext3_mark_recovery_complete(struct super_block * sb,
53                                         struct ext3_super_block * es);
54 static void ext3_clear_journal_err(struct super_block * sb,
55                                    struct ext3_super_block * es);
56 static int ext3_sync_fs(struct super_block *sb, int wait);
57 static const char *ext3_decode_error(struct super_block * sb, int errno,
58                                      char nbuf[16]);
59 static int ext3_remount (struct super_block * sb, int * flags, char * data);
60 static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf);
61 static void ext3_unlockfs(struct super_block *sb);
62 static void ext3_write_super (struct super_block * sb);
63 static void ext3_write_super_lockfs(struct super_block *sb);
64
65 /* 
66  * Wrappers for journal_start/end.
67  *
68  * The only special thing we need to do here is to make sure that all
69  * journal_end calls result in the superblock being marked dirty, so
70  * that sync() will call the filesystem's write_super callback if
71  * appropriate. 
72  */
73 handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
74 {
75         journal_t *journal;
76
77         if (sb->s_flags & MS_RDONLY)
78                 return ERR_PTR(-EROFS);
79
80         /* Special case here: if the journal has aborted behind our
81          * backs (eg. EIO in the commit thread), then we still need to
82          * take the FS itself readonly cleanly. */
83         journal = EXT3_SB(sb)->s_journal;
84         if (is_journal_aborted(journal)) {
85                 ext3_abort(sb, __FUNCTION__,
86                            "Detected aborted journal");
87                 return ERR_PTR(-EROFS);
88         }
89
90         return journal_start(journal, nblocks);
91 }
92
93 /* 
94  * The only special thing we need to do here is to make sure that all
95  * journal_stop calls result in the superblock being marked dirty, so
96  * that sync() will call the filesystem's write_super callback if
97  * appropriate. 
98  */
99 int __ext3_journal_stop(const char *where, handle_t *handle)
100 {
101         struct super_block *sb;
102         int err;
103         int rc;
104
105         sb = handle->h_transaction->t_journal->j_private;
106         err = handle->h_err;
107         rc = journal_stop(handle);
108
109         if (!err)
110                 err = rc;
111         if (err)
112                 __ext3_std_error(sb, where, err);
113         return err;
114 }
115
116 void ext3_journal_abort_handle(const char *caller, const char *err_fn,
117                 struct buffer_head *bh, handle_t *handle, int err)
118 {
119         char nbuf[16];
120         const char *errstr = ext3_decode_error(NULL, err, nbuf);
121
122         if (bh)
123                 BUFFER_TRACE(bh, "abort");
124
125         if (!handle->h_err)
126                 handle->h_err = err;
127
128         if (is_handle_aborted(handle))
129                 return;
130
131         printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
132                caller, errstr, err_fn);
133
134         journal_abort_handle(handle);
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_le16(EXT3_ERROR_FS);
158
159         if (sb->s_flags & MS_RDONLY)
160                 return;
161
162         if (test_opt (sb, ERRORS_RO)) {
163                 printk (KERN_CRIT "Remounting filesystem read-only\n");
164                 sb->s_flags |= MS_RDONLY;
165         } else {
166                 journal_t *journal = EXT3_SB(sb)->s_journal;
167
168                 EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
169                 if (journal)
170                         journal_abort(journal, -EIO);
171         }
172         if (test_opt(sb, ERRORS_PANIC))
173                 panic("EXT3-fs (device %s): panic forced after error\n",
174                         sb->s_id);
175         ext3_commit_super(sb, es, 1);
176 }
177
178 void ext3_error (struct super_block * sb, const char * function,
179                  const char * fmt, ...)
180 {
181         va_list args;
182
183         va_start(args, fmt);
184         printk(KERN_CRIT "EXT3-fs error (device %s): %s: ",sb->s_id, function);
185         vprintk(fmt, args);
186         printk("\n");
187         va_end(args);
188
189         ext3_handle_error(sb);
190 }
191
192 static const char *ext3_decode_error(struct super_block * sb, int errno,
193                                      char nbuf[16])
194 {
195         char *errstr = NULL;
196
197         switch (errno) {
198         case -EIO:
199                 errstr = "IO failure";
200                 break;
201         case -ENOMEM:
202                 errstr = "Out of memory";
203                 break;
204         case -EROFS:
205                 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
206                         errstr = "Journal has aborted";
207                 else
208                         errstr = "Readonly filesystem";
209                 break;
210         default:
211                 /* If the caller passed in an extra buffer for unknown
212                  * errors, textualise them now.  Else we just return
213                  * NULL. */
214                 if (nbuf) {
215                         /* Check for truncated error codes... */
216                         if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
217                                 errstr = nbuf;
218                 }
219                 break;
220         }
221
222         return errstr;
223 }
224
225 /* __ext3_std_error decodes expected errors from journaling functions
226  * automatically and invokes the appropriate error response.  */
227
228 void __ext3_std_error (struct super_block * sb, const char * function,
229                        int errno)
230 {
231         char nbuf[16];
232         const char *errstr;
233
234         /* Special case: if the error is EROFS, and we're not already
235          * inside a transaction, then there's really no point in logging
236          * an error. */
237         if (errno == -EROFS && journal_current_handle() == NULL &&
238             (sb->s_flags & MS_RDONLY))
239                 return;
240
241         errstr = ext3_decode_error(sb, errno, nbuf);
242         printk (KERN_CRIT "EXT3-fs error (device %s) in %s: %s\n",
243                 sb->s_id, function, errstr);
244
245         ext3_handle_error(sb);
246 }
247
248 /*
249  * ext3_abort is a much stronger failure handler than ext3_error.  The
250  * abort function may be used to deal with unrecoverable failures such
251  * as journal IO errors or ENOMEM at a critical moment in log management.
252  *
253  * We unconditionally force the filesystem into an ABORT|READONLY state,
254  * unless the error response on the fs has been set to panic in which
255  * case we take the easy way out and panic immediately.
256  */
257
258 void ext3_abort (struct super_block * sb, const char * function,
259                  const char * fmt, ...)
260 {
261         va_list args;
262
263         printk (KERN_CRIT "ext3_abort called.\n");
264
265         va_start(args, fmt);
266         printk(KERN_CRIT "EXT3-fs error (device %s): %s: ",sb->s_id, function);
267         vprintk(fmt, args);
268         printk("\n");
269         va_end(args);
270
271         if (test_opt(sb, ERRORS_PANIC))
272                 panic("EXT3-fs panic from previous error\n");
273
274         if (sb->s_flags & MS_RDONLY)
275                 return;
276
277         printk(KERN_CRIT "Remounting filesystem read-only\n");
278         EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
279         sb->s_flags |= MS_RDONLY;
280         EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
281         journal_abort(EXT3_SB(sb)->s_journal, -EIO);
282 }
283
284 void ext3_warning (struct super_block * sb, const char * function,
285                    const char * fmt, ...)
286 {
287         va_list args;
288
289         va_start(args, fmt);
290         printk(KERN_WARNING "EXT3-fs warning (device %s): %s: ",
291                sb->s_id, function);
292         vprintk(fmt, args);
293         printk("\n");
294         va_end(args);
295 }
296
297 void ext3_update_dynamic_rev(struct super_block *sb)
298 {
299         struct ext3_super_block *es = EXT3_SB(sb)->s_es;
300
301         if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
302                 return;
303
304         ext3_warning(sb, __FUNCTION__,
305                      "updating to rev %d because of new feature flag, "
306                      "running e2fsck is recommended",
307                      EXT3_DYNAMIC_REV);
308
309         es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
310         es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
311         es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
312         /* leave es->s_feature_*compat flags alone */
313         /* es->s_uuid will be set by e2fsck if empty */
314
315         /*
316          * The rest of the superblock fields should be zero, and if not it
317          * means they are likely already in use, so leave them alone.  We
318          * can leave it up to e2fsck to clean up any inconsistencies there.
319          */
320 }
321
322 /*
323  * Open the external journal device
324  */
325 static struct block_device *ext3_blkdev_get(dev_t dev)
326 {
327         struct block_device *bdev;
328         char b[BDEVNAME_SIZE];
329
330         bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
331         if (IS_ERR(bdev))
332                 goto fail;
333         return bdev;
334
335 fail:
336         printk(KERN_ERR "EXT3: failed to open journal device %s: %ld\n",
337                         __bdevname(dev, b), PTR_ERR(bdev));
338         return NULL;
339 }
340
341 /*
342  * Release the journal device
343  */
344 static int ext3_blkdev_put(struct block_device *bdev)
345 {
346         bd_release(bdev);
347         return blkdev_put(bdev);
348 }
349
350 static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
351 {
352         struct block_device *bdev;
353         int ret = -ENODEV;
354
355         bdev = sbi->journal_bdev;
356         if (bdev) {
357                 ret = ext3_blkdev_put(bdev);
358                 sbi->journal_bdev = NULL;
359         }
360         return ret;
361 }
362
363 static inline struct inode *orphan_list_entry(struct list_head *l)
364 {
365         return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
366 }
367
368 static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
369 {
370         struct list_head *l;
371
372         printk(KERN_ERR "sb orphan head is %d\n", 
373                le32_to_cpu(sbi->s_es->s_last_orphan));
374
375         printk(KERN_ERR "sb_info orphan list:\n");
376         list_for_each(l, &sbi->s_orphan) {
377                 struct inode *inode = orphan_list_entry(l);
378                 printk(KERN_ERR "  "
379                        "inode %s:%ld at %p: mode %o, nlink %d, next %d\n",
380                        inode->i_sb->s_id, inode->i_ino, inode,
381                        inode->i_mode, inode->i_nlink, 
382                        NEXT_ORPHAN(inode));
383         }
384 }
385
386 static void ext3_put_super (struct super_block * sb)
387 {
388         struct ext3_sb_info *sbi = EXT3_SB(sb);
389         struct ext3_super_block *es = sbi->s_es;
390         int i;
391
392         ext3_xattr_put_super(sb);
393         journal_destroy(sbi->s_journal);
394         if (!(sb->s_flags & MS_RDONLY)) {
395                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
396                 es->s_state = cpu_to_le16(sbi->s_mount_state);
397                 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
398                 mark_buffer_dirty(sbi->s_sbh);
399                 ext3_commit_super(sb, es, 1);
400         }
401
402         for (i = 0; i < sbi->s_gdb_count; i++)
403                 brelse(sbi->s_group_desc[i]);
404         kfree(sbi->s_group_desc);
405         percpu_counter_destroy(&sbi->s_freeblocks_counter);
406         percpu_counter_destroy(&sbi->s_freeinodes_counter);
407         percpu_counter_destroy(&sbi->s_dirs_counter);
408         brelse(sbi->s_sbh);
409 #ifdef CONFIG_QUOTA
410         for (i = 0; i < MAXQUOTAS; i++)
411                 kfree(sbi->s_qf_names[i]);
412 #endif
413
414         /* Debugging code just in case the in-memory inode orphan list
415          * isn't empty.  The on-disk one can be non-empty if we've
416          * detected an error and taken the fs readonly, but the
417          * in-memory list had better be clean by this point. */
418         if (!list_empty(&sbi->s_orphan))
419                 dump_orphan_list(sb, sbi);
420         J_ASSERT(list_empty(&sbi->s_orphan));
421
422         invalidate_bdev(sb->s_bdev, 0);
423         if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
424                 /*
425                  * Invalidate the journal device's buffers.  We don't want them
426                  * floating about in memory - the physical journal device may
427                  * hotswapped, and it breaks the `ro-after' testing code.
428                  */
429                 sync_blockdev(sbi->journal_bdev);
430                 invalidate_bdev(sbi->journal_bdev, 0);
431                 ext3_blkdev_remove(sbi);
432         }
433         sb->s_fs_info = NULL;
434         kfree(sbi);
435         return;
436 }
437
438 static kmem_cache_t *ext3_inode_cachep;
439
440 /*
441  * Called inside transaction, so use GFP_NOFS
442  */
443 static struct inode *ext3_alloc_inode(struct super_block *sb)
444 {
445         struct ext3_inode_info *ei;
446
447         ei = kmem_cache_alloc(ext3_inode_cachep, SLAB_NOFS);
448         if (!ei)
449                 return NULL;
450 #ifdef CONFIG_EXT3_FS_POSIX_ACL
451         ei->i_acl = EXT3_ACL_NOT_CACHED;
452         ei->i_default_acl = EXT3_ACL_NOT_CACHED;
453 #endif
454         ei->i_block_alloc_info = NULL;
455         ei->vfs_inode.i_version = 1;
456         return &ei->vfs_inode;
457 }
458
459 static void ext3_destroy_inode(struct inode *inode)
460 {
461         kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
462 }
463
464 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
465 {
466         struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
467
468         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
469             SLAB_CTOR_CONSTRUCTOR) {
470                 INIT_LIST_HEAD(&ei->i_orphan);
471 #ifdef CONFIG_EXT3_FS_XATTR
472                 init_rwsem(&ei->xattr_sem);
473 #endif
474                 mutex_init(&ei->truncate_mutex);
475                 inode_init_once(&ei->vfs_inode);
476         }
477 }
478  
479 static int init_inodecache(void)
480 {
481         ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
482                                              sizeof(struct ext3_inode_info),
483                                              0, (SLAB_RECLAIM_ACCOUNT|
484                                                 SLAB_MEM_SPREAD),
485                                              init_once, NULL);
486         if (ext3_inode_cachep == NULL)
487                 return -ENOMEM;
488         return 0;
489 }
490
491 static void destroy_inodecache(void)
492 {
493         if (kmem_cache_destroy(ext3_inode_cachep))
494                 printk(KERN_INFO "ext3_inode_cache: not all structures were freed\n");
495 }
496
497 static void ext3_clear_inode(struct inode *inode)
498 {
499         struct ext3_block_alloc_info *rsv = EXT3_I(inode)->i_block_alloc_info;
500 #ifdef CONFIG_EXT3_FS_POSIX_ACL
501         if (EXT3_I(inode)->i_acl &&
502                         EXT3_I(inode)->i_acl != EXT3_ACL_NOT_CACHED) {
503                 posix_acl_release(EXT3_I(inode)->i_acl);
504                 EXT3_I(inode)->i_acl = EXT3_ACL_NOT_CACHED;
505         }
506         if (EXT3_I(inode)->i_default_acl &&
507                         EXT3_I(inode)->i_default_acl != EXT3_ACL_NOT_CACHED) {
508                 posix_acl_release(EXT3_I(inode)->i_default_acl);
509                 EXT3_I(inode)->i_default_acl = EXT3_ACL_NOT_CACHED;
510         }
511 #endif
512         ext3_discard_reservation(inode);
513         EXT3_I(inode)->i_block_alloc_info = NULL;
514         if (unlikely(rsv))
515                 kfree(rsv);
516 }
517
518 static inline void ext3_show_quota_options(struct seq_file *seq, struct super_block *sb)
519 {
520 #if defined(CONFIG_QUOTA)
521         struct ext3_sb_info *sbi = EXT3_SB(sb);
522
523         if (sbi->s_jquota_fmt)
524                 seq_printf(seq, ",jqfmt=%s",
525                 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
526
527         if (sbi->s_qf_names[USRQUOTA])
528                 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
529
530         if (sbi->s_qf_names[GRPQUOTA])
531                 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
532
533         if (sbi->s_mount_opt & EXT3_MOUNT_USRQUOTA)
534                 seq_puts(seq, ",usrquota");
535
536         if (sbi->s_mount_opt & EXT3_MOUNT_GRPQUOTA)
537                 seq_puts(seq, ",grpquota");
538 #endif
539 }
540
541 static int ext3_show_options(struct seq_file *seq, struct vfsmount *vfs)
542 {
543         struct super_block *sb = vfs->mnt_sb;
544
545         if (test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA)
546                 seq_puts(seq, ",data=journal");
547         else if (test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA)
548                 seq_puts(seq, ",data=ordered");
549         else if (test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_WRITEBACK_DATA)
550                 seq_puts(seq, ",data=writeback");
551
552         ext3_show_quota_options(seq, sb);
553
554         return 0;
555 }
556
557
558 static struct dentry *ext3_get_dentry(struct super_block *sb, void *vobjp)
559 {
560         __u32 *objp = vobjp;
561         unsigned long ino = objp[0];
562         __u32 generation = objp[1];
563         struct inode *inode;
564         struct dentry *result;
565
566         if (ino < EXT3_FIRST_INO(sb) && ino != EXT3_ROOT_INO)
567                 return ERR_PTR(-ESTALE);
568         if (ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count))
569                 return ERR_PTR(-ESTALE);
570
571         /* iget isn't really right if the inode is currently unallocated!!
572          *
573          * ext3_read_inode will return a bad_inode if the inode had been
574          * deleted, so we should be safe.
575          *
576          * Currently we don't know the generation for parent directory, so
577          * a generation of 0 means "accept any"
578          */
579         inode = iget(sb, ino);
580         if (inode == NULL)
581                 return ERR_PTR(-ENOMEM);
582         if (is_bad_inode(inode) ||
583             (generation && inode->i_generation != generation)) {
584                 iput(inode);
585                 return ERR_PTR(-ESTALE);
586         }
587         /* now to find a dentry.
588          * If possible, get a well-connected one
589          */
590         result = d_alloc_anon(inode);
591         if (!result) {
592                 iput(inode);
593                 return ERR_PTR(-ENOMEM);
594         }
595         return result;
596 }
597
598 #ifdef CONFIG_QUOTA
599 #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
600 #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
601
602 static int ext3_dquot_initialize(struct inode *inode, int type);
603 static int ext3_dquot_drop(struct inode *inode);
604 static int ext3_write_dquot(struct dquot *dquot);
605 static int ext3_acquire_dquot(struct dquot *dquot);
606 static int ext3_release_dquot(struct dquot *dquot);
607 static int ext3_mark_dquot_dirty(struct dquot *dquot);
608 static int ext3_write_info(struct super_block *sb, int type);
609 static int ext3_quota_on(struct super_block *sb, int type, int format_id, char *path);
610 static int ext3_quota_on_mount(struct super_block *sb, int type);
611 static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
612                                size_t len, loff_t off);
613 static ssize_t ext3_quota_write(struct super_block *sb, int type,
614                                 const char *data, size_t len, loff_t off);
615
616 static struct dquot_operations ext3_quota_operations = {
617         .initialize     = ext3_dquot_initialize,
618         .drop           = ext3_dquot_drop,
619         .alloc_space    = dquot_alloc_space,
620         .alloc_inode    = dquot_alloc_inode,
621         .free_space     = dquot_free_space,
622         .free_inode     = dquot_free_inode,
623         .transfer       = dquot_transfer,
624         .write_dquot    = ext3_write_dquot,
625         .acquire_dquot  = ext3_acquire_dquot,
626         .release_dquot  = ext3_release_dquot,
627         .mark_dirty     = ext3_mark_dquot_dirty,
628         .write_info     = ext3_write_info
629 };
630
631 static struct quotactl_ops ext3_qctl_operations = {
632         .quota_on       = ext3_quota_on,
633         .quota_off      = vfs_quota_off,
634         .quota_sync     = vfs_quota_sync,
635         .get_info       = vfs_get_dqinfo,
636         .set_info       = vfs_set_dqinfo,
637         .get_dqblk      = vfs_get_dqblk,
638         .set_dqblk      = vfs_set_dqblk
639 };
640 #endif
641
642 static struct super_operations ext3_sops = {
643         .alloc_inode    = ext3_alloc_inode,
644         .destroy_inode  = ext3_destroy_inode,
645         .read_inode     = ext3_read_inode,
646         .write_inode    = ext3_write_inode,
647         .dirty_inode    = ext3_dirty_inode,
648         .delete_inode   = ext3_delete_inode,
649         .put_super      = ext3_put_super,
650         .write_super    = ext3_write_super,
651         .sync_fs        = ext3_sync_fs,
652         .write_super_lockfs = ext3_write_super_lockfs,
653         .unlockfs       = ext3_unlockfs,
654         .statfs         = ext3_statfs,
655         .remount_fs     = ext3_remount,
656         .clear_inode    = ext3_clear_inode,
657         .show_options   = ext3_show_options,
658 #ifdef CONFIG_QUOTA
659         .quota_read     = ext3_quota_read,
660         .quota_write    = ext3_quota_write,
661 #endif
662 };
663
664 static struct export_operations ext3_export_ops = {
665         .get_parent = ext3_get_parent,
666         .get_dentry = ext3_get_dentry,
667 };
668
669 enum {
670         Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
671         Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
672         Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
673         Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
674         Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
675         Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
676         Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
677         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
678         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
679         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
680         Opt_grpquota
681 };
682
683 static match_table_t tokens = {
684         {Opt_bsd_df, "bsddf"},
685         {Opt_minix_df, "minixdf"},
686         {Opt_grpid, "grpid"},
687         {Opt_grpid, "bsdgroups"},
688         {Opt_nogrpid, "nogrpid"},
689         {Opt_nogrpid, "sysvgroups"},
690         {Opt_resgid, "resgid=%u"},
691         {Opt_resuid, "resuid=%u"},
692         {Opt_sb, "sb=%u"},
693         {Opt_err_cont, "errors=continue"},
694         {Opt_err_panic, "errors=panic"},
695         {Opt_err_ro, "errors=remount-ro"},
696         {Opt_nouid32, "nouid32"},
697         {Opt_nocheck, "nocheck"},
698         {Opt_nocheck, "check=none"},
699         {Opt_debug, "debug"},
700         {Opt_oldalloc, "oldalloc"},
701         {Opt_orlov, "orlov"},
702         {Opt_user_xattr, "user_xattr"},
703         {Opt_nouser_xattr, "nouser_xattr"},
704         {Opt_acl, "acl"},
705         {Opt_noacl, "noacl"},
706         {Opt_reservation, "reservation"},
707         {Opt_noreservation, "noreservation"},
708         {Opt_noload, "noload"},
709         {Opt_nobh, "nobh"},
710         {Opt_bh, "bh"},
711         {Opt_commit, "commit=%u"},
712         {Opt_journal_update, "journal=update"},
713         {Opt_journal_inum, "journal=%u"},
714         {Opt_journal_dev, "journal_dev=%u"},
715         {Opt_abort, "abort"},
716         {Opt_data_journal, "data=journal"},
717         {Opt_data_ordered, "data=ordered"},
718         {Opt_data_writeback, "data=writeback"},
719         {Opt_offusrjquota, "usrjquota="},
720         {Opt_usrjquota, "usrjquota=%s"},
721         {Opt_offgrpjquota, "grpjquota="},
722         {Opt_grpjquota, "grpjquota=%s"},
723         {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
724         {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
725         {Opt_grpquota, "grpquota"},
726         {Opt_noquota, "noquota"},
727         {Opt_quota, "quota"},
728         {Opt_usrquota, "usrquota"},
729         {Opt_barrier, "barrier=%u"},
730         {Opt_err, NULL},
731         {Opt_resize, "resize"},
732 };
733
734 static ext3_fsblk_t get_sb_block(void **data)
735 {
736         ext3_fsblk_t    sb_block;
737         char            *options = (char *) *data;
738
739         if (!options || strncmp(options, "sb=", 3) != 0)
740                 return 1;       /* Default location */
741         options += 3;
742         /*todo: use simple_strtoll with >32bit ext3 */
743         sb_block = simple_strtoul(options, &options, 0);
744         if (*options && *options != ',') {
745                 printk("EXT3-fs: Invalid sb specification: %s\n",
746                        (char *) *data);
747                 return 1;
748         }
749         if (*options == ',')
750                 options++;
751         *data = (void *) options;
752         return sb_block;
753 }
754
755 static int parse_options (char *options, struct super_block *sb,
756                           unsigned long *inum, unsigned long *journal_devnum,
757                           ext3_fsblk_t *n_blocks_count, int is_remount)
758 {
759         struct ext3_sb_info *sbi = EXT3_SB(sb);
760         char * p;
761         substring_t args[MAX_OPT_ARGS];
762         int data_opt = 0;
763         int option;
764 #ifdef CONFIG_QUOTA
765         int qtype;
766         char *qname;
767 #endif
768
769         if (!options)
770                 return 1;
771
772         while ((p = strsep (&options, ",")) != NULL) {
773                 int token;
774                 if (!*p)
775                         continue;
776
777                 token = match_token(p, tokens, args);
778                 switch (token) {
779                 case Opt_bsd_df:
780                         clear_opt (sbi->s_mount_opt, MINIX_DF);
781                         break;
782                 case Opt_minix_df:
783                         set_opt (sbi->s_mount_opt, MINIX_DF);
784                         break;
785                 case Opt_grpid:
786                         set_opt (sbi->s_mount_opt, GRPID);
787                         break;
788                 case Opt_nogrpid:
789                         clear_opt (sbi->s_mount_opt, GRPID);
790                         break;
791                 case Opt_resuid:
792                         if (match_int(&args[0], &option))
793                                 return 0;
794                         sbi->s_resuid = option;
795                         break;
796                 case Opt_resgid:
797                         if (match_int(&args[0], &option))
798                                 return 0;
799                         sbi->s_resgid = option;
800                         break;
801                 case Opt_sb:
802                         /* handled by get_sb_block() instead of here */
803                         /* *sb_block = match_int(&args[0]); */
804                         break;
805                 case Opt_err_panic:
806                         clear_opt (sbi->s_mount_opt, ERRORS_CONT);
807                         clear_opt (sbi->s_mount_opt, ERRORS_RO);
808                         set_opt (sbi->s_mount_opt, ERRORS_PANIC);
809                         break;
810                 case Opt_err_ro:
811                         clear_opt (sbi->s_mount_opt, ERRORS_CONT);
812                         clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
813                         set_opt (sbi->s_mount_opt, ERRORS_RO);
814                         break;
815                 case Opt_err_cont:
816                         clear_opt (sbi->s_mount_opt, ERRORS_RO);
817                         clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
818                         set_opt (sbi->s_mount_opt, ERRORS_CONT);
819                         break;
820                 case Opt_nouid32:
821                         set_opt (sbi->s_mount_opt, NO_UID32);
822                         break;
823                 case Opt_nocheck:
824                         clear_opt (sbi->s_mount_opt, CHECK);
825                         break;
826                 case Opt_debug:
827                         set_opt (sbi->s_mount_opt, DEBUG);
828                         break;
829                 case Opt_oldalloc:
830                         set_opt (sbi->s_mount_opt, OLDALLOC);
831                         break;
832                 case Opt_orlov:
833                         clear_opt (sbi->s_mount_opt, OLDALLOC);
834                         break;
835 #ifdef CONFIG_EXT3_FS_XATTR
836                 case Opt_user_xattr:
837                         set_opt (sbi->s_mount_opt, XATTR_USER);
838                         break;
839                 case Opt_nouser_xattr:
840                         clear_opt (sbi->s_mount_opt, XATTR_USER);
841                         break;
842 #else
843                 case Opt_user_xattr:
844                 case Opt_nouser_xattr:
845                         printk("EXT3 (no)user_xattr options not supported\n");
846                         break;
847 #endif
848 #ifdef CONFIG_EXT3_FS_POSIX_ACL
849                 case Opt_acl:
850                         set_opt(sbi->s_mount_opt, POSIX_ACL);
851                         break;
852                 case Opt_noacl:
853                         clear_opt(sbi->s_mount_opt, POSIX_ACL);
854                         break;
855 #else
856                 case Opt_acl:
857                 case Opt_noacl:
858                         printk("EXT3 (no)acl options not supported\n");
859                         break;
860 #endif
861                 case Opt_reservation:
862                         set_opt(sbi->s_mount_opt, RESERVATION);
863                         break;
864                 case Opt_noreservation:
865                         clear_opt(sbi->s_mount_opt, RESERVATION);
866                         break;
867                 case Opt_journal_update:
868                         /* @@@ FIXME */
869                         /* Eventually we will want to be able to create
870                            a journal file here.  For now, only allow the
871                            user to specify an existing inode to be the
872                            journal file. */
873                         if (is_remount) {
874                                 printk(KERN_ERR "EXT3-fs: cannot specify "
875                                        "journal on remount\n");
876                                 return 0;
877                         }
878                         set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
879                         break;
880                 case Opt_journal_inum:
881                         if (is_remount) {
882                                 printk(KERN_ERR "EXT3-fs: cannot specify "
883                                        "journal on remount\n");
884                                 return 0;
885                         }
886                         if (match_int(&args[0], &option))
887                                 return 0;
888                         *inum = option;
889                         break;
890                 case Opt_journal_dev:
891                         if (is_remount) {
892                                 printk(KERN_ERR "EXT3-fs: cannot specify "
893                                        "journal on remount\n");
894                                 return 0;
895                         }
896                         if (match_int(&args[0], &option))
897                                 return 0;
898                         *journal_devnum = option;
899                         break;
900                 case Opt_noload:
901                         set_opt (sbi->s_mount_opt, NOLOAD);
902                         break;
903                 case Opt_commit:
904                         if (match_int(&args[0], &option))
905                                 return 0;
906                         if (option < 0)
907                                 return 0;
908                         if (option == 0)
909                                 option = JBD_DEFAULT_MAX_COMMIT_AGE;
910                         sbi->s_commit_interval = HZ * option;
911                         break;
912                 case Opt_data_journal:
913                         data_opt = EXT3_MOUNT_JOURNAL_DATA;
914                         goto datacheck;
915                 case Opt_data_ordered:
916                         data_opt = EXT3_MOUNT_ORDERED_DATA;
917                         goto datacheck;
918                 case Opt_data_writeback:
919                         data_opt = EXT3_MOUNT_WRITEBACK_DATA;
920                 datacheck:
921                         if (is_remount) {
922                                 if ((sbi->s_mount_opt & EXT3_MOUNT_DATA_FLAGS)
923                                                 != data_opt) {
924                                         printk(KERN_ERR
925                                                 "EXT3-fs: cannot change data "
926                                                 "mode on remount\n");
927                                         return 0;
928                                 }
929                         } else {
930                                 sbi->s_mount_opt &= ~EXT3_MOUNT_DATA_FLAGS;
931                                 sbi->s_mount_opt |= data_opt;
932                         }
933                         break;
934 #ifdef CONFIG_QUOTA
935                 case Opt_usrjquota:
936                         qtype = USRQUOTA;
937                         goto set_qf_name;
938                 case Opt_grpjquota:
939                         qtype = GRPQUOTA;
940 set_qf_name:
941                         if (sb_any_quota_enabled(sb)) {
942                                 printk(KERN_ERR
943                                         "EXT3-fs: Cannot change journalled "
944                                         "quota options when quota turned on.\n");
945                                 return 0;
946                         }
947                         qname = match_strdup(&args[0]);
948                         if (!qname) {
949                                 printk(KERN_ERR
950                                         "EXT3-fs: not enough memory for "
951                                         "storing quotafile name.\n");
952                                 return 0;
953                         }
954                         if (sbi->s_qf_names[qtype] &&
955                             strcmp(sbi->s_qf_names[qtype], qname)) {
956                                 printk(KERN_ERR
957                                         "EXT3-fs: %s quota file already "
958                                         "specified.\n", QTYPE2NAME(qtype));
959                                 kfree(qname);
960                                 return 0;
961                         }
962                         sbi->s_qf_names[qtype] = qname;
963                         if (strchr(sbi->s_qf_names[qtype], '/')) {
964                                 printk(KERN_ERR
965                                         "EXT3-fs: quotafile must be on "
966                                         "filesystem root.\n");
967                                 kfree(sbi->s_qf_names[qtype]);
968                                 sbi->s_qf_names[qtype] = NULL;
969                                 return 0;
970                         }
971                         set_opt(sbi->s_mount_opt, QUOTA);
972                         break;
973                 case Opt_offusrjquota:
974                         qtype = USRQUOTA;
975                         goto clear_qf_name;
976                 case Opt_offgrpjquota:
977                         qtype = GRPQUOTA;
978 clear_qf_name:
979                         if (sb_any_quota_enabled(sb)) {
980                                 printk(KERN_ERR "EXT3-fs: Cannot change "
981                                         "journalled quota options when "
982                                         "quota turned on.\n");
983                                 return 0;
984                         }
985                         /*
986                          * The space will be released later when all options
987                          * are confirmed to be correct
988                          */
989                         sbi->s_qf_names[qtype] = NULL;
990                         break;
991                 case Opt_jqfmt_vfsold:
992                         sbi->s_jquota_fmt = QFMT_VFS_OLD;
993                         break;
994                 case Opt_jqfmt_vfsv0:
995                         sbi->s_jquota_fmt = QFMT_VFS_V0;
996                         break;
997                 case Opt_quota:
998                 case Opt_usrquota:
999                         set_opt(sbi->s_mount_opt, QUOTA);
1000                         set_opt(sbi->s_mount_opt, USRQUOTA);
1001                         break;
1002                 case Opt_grpquota:
1003                         set_opt(sbi->s_mount_opt, QUOTA);
1004                         set_opt(sbi->s_mount_opt, GRPQUOTA);
1005                         break;
1006                 case Opt_noquota:
1007                         if (sb_any_quota_enabled(sb)) {
1008                                 printk(KERN_ERR "EXT3-fs: Cannot change quota "
1009                                         "options when quota turned on.\n");
1010                                 return 0;
1011                         }
1012                         clear_opt(sbi->s_mount_opt, QUOTA);
1013                         clear_opt(sbi->s_mount_opt, USRQUOTA);
1014                         clear_opt(sbi->s_mount_opt, GRPQUOTA);
1015                         break;
1016 #else
1017                 case Opt_quota:
1018                 case Opt_usrquota:
1019                 case Opt_grpquota:
1020                 case Opt_usrjquota:
1021                 case Opt_grpjquota:
1022                 case Opt_offusrjquota:
1023                 case Opt_offgrpjquota:
1024                 case Opt_jqfmt_vfsold:
1025                 case Opt_jqfmt_vfsv0:
1026                         printk(KERN_ERR
1027                                 "EXT3-fs: journalled quota options not "
1028                                 "supported.\n");
1029                         break;
1030                 case Opt_noquota:
1031                         break;
1032 #endif
1033                 case Opt_abort:
1034                         set_opt(sbi->s_mount_opt, ABORT);
1035                         break;
1036                 case Opt_barrier:
1037                         if (match_int(&args[0], &option))
1038                                 return 0;
1039                         if (option)
1040                                 set_opt(sbi->s_mount_opt, BARRIER);
1041                         else
1042                                 clear_opt(sbi->s_mount_opt, BARRIER);
1043                         break;
1044                 case Opt_ignore:
1045                         break;
1046                 case Opt_resize:
1047                         if (!is_remount) {
1048                                 printk("EXT3-fs: resize option only available "
1049                                         "for remount\n");
1050                                 return 0;
1051                         }
1052                         if (match_int(&args[0], &option) != 0)
1053                                 return 0;
1054                         *n_blocks_count = option;
1055                         break;
1056                 case Opt_nobh:
1057                         set_opt(sbi->s_mount_opt, NOBH);
1058                         break;
1059                 case Opt_bh:
1060                         clear_opt(sbi->s_mount_opt, NOBH);
1061                         break;
1062                 default:
1063                         printk (KERN_ERR
1064                                 "EXT3-fs: Unrecognized mount option \"%s\" "
1065                                 "or missing value\n", p);
1066                         return 0;
1067                 }
1068         }
1069 #ifdef CONFIG_QUOTA
1070         if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1071                 if ((sbi->s_mount_opt & EXT3_MOUNT_USRQUOTA) &&
1072                      sbi->s_qf_names[USRQUOTA])
1073                         clear_opt(sbi->s_mount_opt, USRQUOTA);
1074
1075                 if ((sbi->s_mount_opt & EXT3_MOUNT_GRPQUOTA) &&
1076                      sbi->s_qf_names[GRPQUOTA])
1077                         clear_opt(sbi->s_mount_opt, GRPQUOTA);
1078
1079                 if ((sbi->s_qf_names[USRQUOTA] &&
1080                                 (sbi->s_mount_opt & EXT3_MOUNT_GRPQUOTA)) ||
1081                     (sbi->s_qf_names[GRPQUOTA] &&
1082                                 (sbi->s_mount_opt & EXT3_MOUNT_USRQUOTA))) {
1083                         printk(KERN_ERR "EXT3-fs: old and new quota "
1084                                         "format mixing.\n");
1085                         return 0;
1086                 }
1087
1088                 if (!sbi->s_jquota_fmt) {
1089                         printk(KERN_ERR "EXT3-fs: journalled quota format "
1090                                         "not specified.\n");
1091                         return 0;
1092                 }
1093         } else {
1094                 if (sbi->s_jquota_fmt) {
1095                         printk(KERN_ERR "EXT3-fs: journalled quota format "
1096                                         "specified with no journalling "
1097                                         "enabled.\n");
1098                         return 0;
1099                 }
1100         }
1101 #endif
1102         return 1;
1103 }
1104
1105 static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
1106                             int read_only)
1107 {
1108         struct ext3_sb_info *sbi = EXT3_SB(sb);
1109         int res = 0;
1110
1111         if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
1112                 printk (KERN_ERR "EXT3-fs warning: revision level too high, "
1113                         "forcing read-only mode\n");
1114                 res = MS_RDONLY;
1115         }
1116         if (read_only)
1117                 return res;
1118         if (!(sbi->s_mount_state & EXT3_VALID_FS))
1119                 printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
1120                         "running e2fsck is recommended\n");
1121         else if ((sbi->s_mount_state & EXT3_ERROR_FS))
1122                 printk (KERN_WARNING
1123                         "EXT3-fs warning: mounting fs with errors, "
1124                         "running e2fsck is recommended\n");
1125         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1126                  le16_to_cpu(es->s_mnt_count) >=
1127                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1128                 printk (KERN_WARNING
1129                         "EXT3-fs warning: maximal mount count reached, "
1130                         "running e2fsck is recommended\n");
1131         else if (le32_to_cpu(es->s_checkinterval) &&
1132                 (le32_to_cpu(es->s_lastcheck) +
1133                         le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1134                 printk (KERN_WARNING
1135                         "EXT3-fs warning: checktime reached, "
1136                         "running e2fsck is recommended\n");
1137 #if 0
1138                 /* @@@ We _will_ want to clear the valid bit if we find
1139                    inconsistencies, to force a fsck at reboot.  But for
1140                    a plain journaled filesystem we can keep it set as
1141                    valid forever! :) */
1142         es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
1143 #endif
1144         if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1145                 es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
1146         es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1147         es->s_mtime = cpu_to_le32(get_seconds());
1148         ext3_update_dynamic_rev(sb);
1149         EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1150
1151         ext3_commit_super(sb, es, 1);
1152         if (test_opt(sb, DEBUG))
1153                 printk(KERN_INFO "[EXT3 FS bs=%lu, gc=%lu, "
1154                                 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1155                         sb->s_blocksize,
1156                         sbi->s_groups_count,
1157                         EXT3_BLOCKS_PER_GROUP(sb),
1158                         EXT3_INODES_PER_GROUP(sb),
1159                         sbi->s_mount_opt);
1160
1161         printk(KERN_INFO "EXT3 FS on %s, ", sb->s_id);
1162         if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
1163                 char b[BDEVNAME_SIZE];
1164
1165                 printk("external journal on %s\n",
1166                         bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
1167         } else {
1168                 printk("internal journal\n");
1169         }
1170         return res;
1171 }
1172
1173 /* Called at mount-time, super-block is locked */
1174 static int ext3_check_descriptors (struct super_block * sb)
1175 {
1176         struct ext3_sb_info *sbi = EXT3_SB(sb);
1177         ext3_fsblk_t block = le32_to_cpu(sbi->s_es->s_first_data_block);
1178         struct ext3_group_desc * gdp = NULL;
1179         int desc_block = 0;
1180         int i;
1181
1182         ext3_debug ("Checking group descriptors");
1183
1184         for (i = 0; i < sbi->s_groups_count; i++)
1185         {
1186                 if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
1187                         gdp = (struct ext3_group_desc *)
1188                                         sbi->s_group_desc[desc_block++]->b_data;
1189                 if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
1190                     le32_to_cpu(gdp->bg_block_bitmap) >=
1191                                 block + EXT3_BLOCKS_PER_GROUP(sb))
1192                 {
1193                         ext3_error (sb, "ext3_check_descriptors",
1194                                     "Block bitmap for group %d"
1195                                     " not in group (block %lu)!",
1196                                     i, (unsigned long)
1197                                         le32_to_cpu(gdp->bg_block_bitmap));
1198                         return 0;
1199                 }
1200                 if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
1201                     le32_to_cpu(gdp->bg_inode_bitmap) >=
1202                                 block + EXT3_BLOCKS_PER_GROUP(sb))
1203                 {
1204                         ext3_error (sb, "ext3_check_descriptors",
1205                                     "Inode bitmap for group %d"
1206                                     " not in group (block %lu)!",
1207                                     i, (unsigned long)
1208                                         le32_to_cpu(gdp->bg_inode_bitmap));
1209                         return 0;
1210                 }
1211                 if (le32_to_cpu(gdp->bg_inode_table) < block ||
1212                     le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
1213                     block + EXT3_BLOCKS_PER_GROUP(sb))
1214                 {
1215                         ext3_error (sb, "ext3_check_descriptors",
1216                                     "Inode table for group %d"
1217                                     " not in group (block %lu)!",
1218                                     i, (unsigned long)
1219                                         le32_to_cpu(gdp->bg_inode_table));
1220                         return 0;
1221                 }
1222                 block += EXT3_BLOCKS_PER_GROUP(sb);
1223                 gdp++;
1224         }
1225
1226         sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
1227         sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
1228         return 1;
1229 }
1230
1231
1232 /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
1233  * the superblock) which were deleted from all directories, but held open by
1234  * a process at the time of a crash.  We walk the list and try to delete these
1235  * inodes at recovery time (only with a read-write filesystem).
1236  *
1237  * In order to keep the orphan inode chain consistent during traversal (in
1238  * case of crash during recovery), we link each inode into the superblock
1239  * orphan list_head and handle it the same way as an inode deletion during
1240  * normal operation (which journals the operations for us).
1241  *
1242  * We only do an iget() and an iput() on each inode, which is very safe if we
1243  * accidentally point at an in-use or already deleted inode.  The worst that
1244  * can happen in this case is that we get a "bit already cleared" message from
1245  * ext3_free_inode().  The only reason we would point at a wrong inode is if
1246  * e2fsck was run on this filesystem, and it must have already done the orphan
1247  * inode cleanup for us, so we can safely abort without any further action.
1248  */
1249 static void ext3_orphan_cleanup (struct super_block * sb,
1250                                  struct ext3_super_block * es)
1251 {
1252         unsigned int s_flags = sb->s_flags;
1253         int nr_orphans = 0, nr_truncates = 0;
1254 #ifdef CONFIG_QUOTA
1255         int i;
1256 #endif
1257         if (!es->s_last_orphan) {
1258                 jbd_debug(4, "no orphan inodes to clean up\n");
1259                 return;
1260         }
1261
1262         if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
1263                 if (es->s_last_orphan)
1264                         jbd_debug(1, "Errors on filesystem, "
1265                                   "clearing orphan list.\n");
1266                 es->s_last_orphan = 0;
1267                 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1268                 return;
1269         }
1270
1271         if (s_flags & MS_RDONLY) {
1272                 printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
1273                        sb->s_id);
1274                 sb->s_flags &= ~MS_RDONLY;
1275         }
1276 #ifdef CONFIG_QUOTA
1277         /* Needed for iput() to work correctly and not trash data */
1278         sb->s_flags |= MS_ACTIVE;
1279         /* Turn on quotas so that they are updated correctly */
1280         for (i = 0; i < MAXQUOTAS; i++) {
1281                 if (EXT3_SB(sb)->s_qf_names[i]) {
1282                         int ret = ext3_quota_on_mount(sb, i);
1283                         if (ret < 0)
1284                                 printk(KERN_ERR
1285                                         "EXT3-fs: Cannot turn on journalled "
1286                                         "quota: error %d\n", ret);
1287                 }
1288         }
1289 #endif
1290
1291         while (es->s_last_orphan) {
1292                 struct inode *inode;
1293
1294                 if (!(inode =
1295                       ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
1296                         es->s_last_orphan = 0;
1297                         break;
1298                 }
1299
1300                 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
1301                 DQUOT_INIT(inode);
1302                 if (inode->i_nlink) {
1303                         printk(KERN_DEBUG
1304                                 "%s: truncating inode %ld to %Ld bytes\n",
1305                                 __FUNCTION__, inode->i_ino, inode->i_size);
1306                         jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
1307                                   inode->i_ino, inode->i_size);
1308                         ext3_truncate(inode);
1309                         nr_truncates++;
1310                 } else {
1311                         printk(KERN_DEBUG
1312                                 "%s: deleting unreferenced inode %ld\n",
1313                                 __FUNCTION__, inode->i_ino);
1314                         jbd_debug(2, "deleting unreferenced inode %ld\n",
1315                                   inode->i_ino);
1316                         nr_orphans++;
1317                 }
1318                 iput(inode);  /* The delete magic happens here! */
1319         }
1320
1321 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1322
1323         if (nr_orphans)
1324                 printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
1325                        sb->s_id, PLURAL(nr_orphans));
1326         if (nr_truncates)
1327                 printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
1328                        sb->s_id, PLURAL(nr_truncates));
1329 #ifdef CONFIG_QUOTA
1330         /* Turn quotas off */
1331         for (i = 0; i < MAXQUOTAS; i++) {
1332                 if (sb_dqopt(sb)->files[i])
1333                         vfs_quota_off(sb, i);
1334         }
1335 #endif
1336         sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1337 }
1338
1339 #define log2(n) ffz(~(n))
1340
1341 /*
1342  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
1343  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1344  * We need to be 1 filesystem block less than the 2^32 sector limit.
1345  */
1346 static loff_t ext3_max_size(int bits)
1347 {
1348         loff_t res = EXT3_NDIR_BLOCKS;
1349         /* This constant is calculated to be the largest file size for a
1350          * dense, 4k-blocksize file such that the total number of
1351          * sectors in the file, including data and all indirect blocks,
1352          * does not exceed 2^32. */
1353         const loff_t upper_limit = 0x1ff7fffd000LL;
1354
1355         res += 1LL << (bits-2);
1356         res += 1LL << (2*(bits-2));
1357         res += 1LL << (3*(bits-2));
1358         res <<= bits;
1359         if (res > upper_limit)
1360                 res = upper_limit;
1361         return res;
1362 }
1363
1364 static ext3_fsblk_t descriptor_loc(struct super_block *sb,
1365                                     ext3_fsblk_t logic_sb_block,
1366                                     int nr)
1367 {
1368         struct ext3_sb_info *sbi = EXT3_SB(sb);
1369         unsigned long bg, first_meta_bg;
1370         int has_super = 0;
1371
1372         first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1373
1374         if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
1375             nr < first_meta_bg)
1376                 return (logic_sb_block + nr + 1);
1377         bg = sbi->s_desc_per_block * nr;
1378         if (ext3_bg_has_super(sb, bg))
1379                 has_super = 1;
1380         return (has_super + ext3_group_first_block_no(sb, bg));
1381 }
1382
1383
1384 static int ext3_fill_super (struct super_block *sb, void *data, int silent)
1385 {
1386         struct buffer_head * bh;
1387         struct ext3_super_block *es = NULL;
1388         struct ext3_sb_info *sbi;
1389         ext3_fsblk_t block;
1390         ext3_fsblk_t sb_block = get_sb_block(&data);
1391         ext3_fsblk_t logic_sb_block;
1392         unsigned long offset = 0;
1393         unsigned long journal_inum = 0;
1394         unsigned long journal_devnum = 0;
1395         unsigned long def_mount_opts;
1396         struct inode *root;
1397         int blocksize;
1398         int hblock;
1399         int db_count;
1400         int i;
1401         int needs_recovery;
1402         __le32 features;
1403
1404         sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
1405         if (!sbi)
1406                 return -ENOMEM;
1407         sb->s_fs_info = sbi;
1408         memset(sbi, 0, sizeof(*sbi));
1409         sbi->s_mount_opt = 0;
1410         sbi->s_resuid = EXT3_DEF_RESUID;
1411         sbi->s_resgid = EXT3_DEF_RESGID;
1412
1413         unlock_kernel();
1414
1415         blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
1416         if (!blocksize) {
1417                 printk(KERN_ERR "EXT3-fs: unable to set blocksize\n");
1418                 goto out_fail;
1419         }
1420
1421         /*
1422          * The ext3 superblock will not be buffer aligned for other than 1kB
1423          * block sizes.  We need to calculate the offset from buffer start.
1424          */
1425         if (blocksize != EXT3_MIN_BLOCK_SIZE) {
1426                 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1427                 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1428         } else {
1429                 logic_sb_block = sb_block;
1430         }
1431
1432         if (!(bh = sb_bread(sb, logic_sb_block))) {
1433                 printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
1434                 goto out_fail;
1435         }
1436         /*
1437          * Note: s_es must be initialized as soon as possible because
1438          *       some ext3 macro-instructions depend on its value
1439          */
1440         es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1441         sbi->s_es = es;
1442         sb->s_magic = le16_to_cpu(es->s_magic);
1443         if (sb->s_magic != EXT3_SUPER_MAGIC)
1444                 goto cantfind_ext3;
1445
1446         /* Set defaults before we parse the mount options */
1447         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1448         if (def_mount_opts & EXT3_DEFM_DEBUG)
1449                 set_opt(sbi->s_mount_opt, DEBUG);
1450         if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
1451                 set_opt(sbi->s_mount_opt, GRPID);
1452         if (def_mount_opts & EXT3_DEFM_UID16)
1453                 set_opt(sbi->s_mount_opt, NO_UID32);
1454         if (def_mount_opts & EXT3_DEFM_XATTR_USER)
1455                 set_opt(sbi->s_mount_opt, XATTR_USER);
1456         if (def_mount_opts & EXT3_DEFM_ACL)
1457                 set_opt(sbi->s_mount_opt, POSIX_ACL);
1458         if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
1459                 sbi->s_mount_opt |= EXT3_MOUNT_JOURNAL_DATA;
1460         else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
1461                 sbi->s_mount_opt |= EXT3_MOUNT_ORDERED_DATA;
1462         else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
1463                 sbi->s_mount_opt |= EXT3_MOUNT_WRITEBACK_DATA;
1464
1465         if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
1466                 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1467         else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_RO)
1468                 set_opt(sbi->s_mount_opt, ERRORS_RO);
1469
1470         sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1471         sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1472
1473         set_opt(sbi->s_mount_opt, RESERVATION);
1474
1475         if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1476                             NULL, 0))
1477                 goto failed_mount;
1478
1479         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1480                 ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1481
1482         if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
1483             (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
1484              EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1485              EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1486                 printk(KERN_WARNING 
1487                        "EXT3-fs warning: feature flags set on rev 0 fs, "
1488                        "running e2fsck is recommended\n");
1489         /*
1490          * Check feature flags regardless of the revision level, since we
1491          * previously didn't change the revision level when setting the flags,
1492          * so there is a chance incompat flags are set on a rev 0 filesystem.
1493          */
1494         features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
1495         if (features) {
1496                 printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
1497                        "unsupported optional features (%x).\n",
1498                        sb->s_id, le32_to_cpu(features));
1499                 goto failed_mount;
1500         }
1501         features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
1502         if (!(sb->s_flags & MS_RDONLY) && features) {
1503                 printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
1504                        "unsupported optional features (%x).\n",
1505                        sb->s_id, le32_to_cpu(features));
1506                 goto failed_mount;
1507         }
1508         blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1509
1510         if (blocksize < EXT3_MIN_BLOCK_SIZE ||
1511             blocksize > EXT3_MAX_BLOCK_SIZE) {
1512                 printk(KERN_ERR 
1513                        "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
1514                        blocksize, sb->s_id);
1515                 goto failed_mount;
1516         }
1517
1518         hblock = bdev_hardsect_size(sb->s_bdev);
1519         if (sb->s_blocksize != blocksize) {
1520                 /*
1521                  * Make sure the blocksize for the filesystem is larger
1522                  * than the hardware sectorsize for the machine.
1523                  */
1524                 if (blocksize < hblock) {
1525                         printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
1526                                "device blocksize %d.\n", blocksize, hblock);
1527                         goto failed_mount;
1528                 }
1529
1530                 brelse (bh);
1531                 sb_set_blocksize(sb, blocksize);
1532                 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1533                 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1534                 bh = sb_bread(sb, logic_sb_block);
1535                 if (!bh) {
1536                         printk(KERN_ERR 
1537                                "EXT3-fs: Can't read superblock on 2nd try.\n");
1538                         goto failed_mount;
1539                 }
1540                 es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
1541                 sbi->s_es = es;
1542                 if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
1543                         printk (KERN_ERR 
1544                                 "EXT3-fs: Magic mismatch, very weird !\n");
1545                         goto failed_mount;
1546                 }
1547         }
1548
1549         sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1550
1551         if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1552                 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1553                 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1554         } else {
1555                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1556                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1557                 if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
1558                     (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1559                     (sbi->s_inode_size > blocksize)) {
1560                         printk (KERN_ERR
1561                                 "EXT3-fs: unsupported inode size: %d\n",
1562                                 sbi->s_inode_size);
1563                         goto failed_mount;
1564                 }
1565         }
1566         sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1567                                    le32_to_cpu(es->s_log_frag_size);
1568         if (blocksize != sbi->s_frag_size) {
1569                 printk(KERN_ERR
1570                        "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
1571                        sbi->s_frag_size, blocksize);
1572                 goto failed_mount;
1573         }
1574         sbi->s_frags_per_block = 1;
1575         sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1576         sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1577         sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1578         if (EXT3_INODE_SIZE(sb) == 0)
1579                 goto cantfind_ext3;
1580         sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1581         if (sbi->s_inodes_per_block == 0)
1582                 goto cantfind_ext3;
1583         sbi->s_itb_per_group = sbi->s_inodes_per_group /
1584                                         sbi->s_inodes_per_block;
1585         sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1586         sbi->s_sbh = bh;
1587         sbi->s_mount_state = le16_to_cpu(es->s_state);
1588         sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
1589         sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
1590         for (i=0; i < 4; i++)
1591                 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1592         sbi->s_def_hash_version = es->s_def_hash_version;
1593
1594         if (sbi->s_blocks_per_group > blocksize * 8) {
1595                 printk (KERN_ERR
1596                         "EXT3-fs: #blocks per group too big: %lu\n",
1597                         sbi->s_blocks_per_group);
1598                 goto failed_mount;
1599         }
1600         if (sbi->s_frags_per_group > blocksize * 8) {
1601                 printk (KERN_ERR
1602                         "EXT3-fs: #fragments per group too big: %lu\n",
1603                         sbi->s_frags_per_group);
1604                 goto failed_mount;
1605         }
1606         if (sbi->s_inodes_per_group > blocksize * 8) {
1607                 printk (KERN_ERR
1608                         "EXT3-fs: #inodes per group too big: %lu\n",
1609                         sbi->s_inodes_per_group);
1610                 goto failed_mount;
1611         }
1612
1613         if (le32_to_cpu(es->s_blocks_count) >
1614                     (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1615                 printk(KERN_ERR "EXT3-fs: filesystem on %s:"
1616                         " too large to mount safely\n", sb->s_id);
1617                 if (sizeof(sector_t) < 8)
1618                         printk(KERN_WARNING "EXT3-fs: CONFIG_LBD not "
1619                                         "enabled\n");
1620                 goto failed_mount;
1621         }
1622
1623         if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
1624                 goto cantfind_ext3;
1625         sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
1626                                le32_to_cpu(es->s_first_data_block) +
1627                                EXT3_BLOCKS_PER_GROUP(sb) - 1) /
1628                               EXT3_BLOCKS_PER_GROUP(sb);
1629         db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
1630                    EXT3_DESC_PER_BLOCK(sb);
1631         sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1632                                     GFP_KERNEL);
1633         if (sbi->s_group_desc == NULL) {
1634                 printk (KERN_ERR "EXT3-fs: not enough memory\n");
1635                 goto failed_mount;
1636         }
1637
1638         bgl_lock_init(&sbi->s_blockgroup_lock);
1639
1640         for (i = 0; i < db_count; i++) {
1641                 block = descriptor_loc(sb, logic_sb_block, i);
1642                 sbi->s_group_desc[i] = sb_bread(sb, block);
1643                 if (!sbi->s_group_desc[i]) {
1644                         printk (KERN_ERR "EXT3-fs: "
1645                                 "can't read group descriptor %d\n", i);
1646                         db_count = i;
1647                         goto failed_mount2;
1648                 }
1649         }
1650         if (!ext3_check_descriptors (sb)) {
1651                 printk(KERN_ERR "EXT3-fs: group descriptors corrupted!\n");
1652                 goto failed_mount2;
1653         }
1654         sbi->s_gdb_count = db_count;
1655         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1656         spin_lock_init(&sbi->s_next_gen_lock);
1657
1658         percpu_counter_init(&sbi->s_freeblocks_counter,
1659                 ext3_count_free_blocks(sb));
1660         percpu_counter_init(&sbi->s_freeinodes_counter,
1661                 ext3_count_free_inodes(sb));
1662         percpu_counter_init(&sbi->s_dirs_counter,
1663                 ext3_count_dirs(sb));
1664
1665         /* per fileystem reservation list head & lock */
1666         spin_lock_init(&sbi->s_rsv_window_lock);
1667         sbi->s_rsv_window_root = RB_ROOT;
1668         /* Add a single, static dummy reservation to the start of the
1669          * reservation window list --- it gives us a placeholder for
1670          * append-at-start-of-list which makes the allocation logic
1671          * _much_ simpler. */
1672         sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1673         sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1674         sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1675         sbi->s_rsv_window_head.rsv_goal_size = 0;
1676         ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
1677
1678         /*
1679          * set up enough so that it can read an inode
1680          */
1681         sb->s_op = &ext3_sops;
1682         sb->s_export_op = &ext3_export_ops;
1683         sb->s_xattr = ext3_xattr_handlers;
1684 #ifdef CONFIG_QUOTA
1685         sb->s_qcop = &ext3_qctl_operations;
1686         sb->dq_op = &ext3_quota_operations;
1687 #endif
1688         INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1689
1690         sb->s_root = NULL;
1691
1692         needs_recovery = (es->s_last_orphan != 0 ||
1693                           EXT3_HAS_INCOMPAT_FEATURE(sb,
1694                                     EXT3_FEATURE_INCOMPAT_RECOVER));
1695
1696         /*
1697          * The first inode we look at is the journal inode.  Don't try
1698          * root first: it may be modified in the journal!
1699          */
1700         if (!test_opt(sb, NOLOAD) &&
1701             EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1702                 if (ext3_load_journal(sb, es, journal_devnum))
1703                         goto failed_mount3;
1704         } else if (journal_inum) {
1705                 if (ext3_create_journal(sb, es, journal_inum))
1706                         goto failed_mount3;
1707         } else {
1708                 if (!silent)
1709                         printk (KERN_ERR
1710                                 "ext3: No journal on filesystem on %s\n",
1711                                 sb->s_id);
1712                 goto failed_mount3;
1713         }
1714
1715         /* We have now updated the journal if required, so we can
1716          * validate the data journaling mode. */
1717         switch (test_opt(sb, DATA_FLAGS)) {
1718         case 0:
1719                 /* No mode set, assume a default based on the journal
1720                    capabilities: ORDERED_DATA if the journal can
1721                    cope, else JOURNAL_DATA */
1722                 if (journal_check_available_features
1723                     (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
1724                         set_opt(sbi->s_mount_opt, ORDERED_DATA);
1725                 else
1726                         set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1727                 break;
1728
1729         case EXT3_MOUNT_ORDERED_DATA:
1730         case EXT3_MOUNT_WRITEBACK_DATA:
1731                 if (!journal_check_available_features
1732                     (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
1733                         printk(KERN_ERR "EXT3-fs: Journal does not support "
1734                                "requested data journaling mode\n");
1735                         goto failed_mount4;
1736                 }
1737         default:
1738                 break;
1739         }
1740
1741         if (test_opt(sb, NOBH)) {
1742                 if (!(test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_WRITEBACK_DATA)) {
1743                         printk(KERN_WARNING "EXT3-fs: Ignoring nobh option - "
1744                                 "its supported only with writeback mode\n");
1745                         clear_opt(sbi->s_mount_opt, NOBH);
1746                 }
1747         }
1748         /*
1749          * The journal_load will have done any necessary log recovery,
1750          * so we can safely mount the rest of the filesystem now.
1751          */
1752
1753         root = iget(sb, EXT3_ROOT_INO);
1754         sb->s_root = d_alloc_root(root);
1755         if (!sb->s_root) {
1756                 printk(KERN_ERR "EXT3-fs: get root inode failed\n");
1757                 iput(root);
1758                 goto failed_mount4;
1759         }
1760         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1761                 dput(sb->s_root);
1762                 sb->s_root = NULL;
1763                 printk(KERN_ERR "EXT3-fs: corrupt root inode, run e2fsck\n");
1764                 goto failed_mount4;
1765         }
1766
1767         ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
1768         /*
1769          * akpm: core read_super() calls in here with the superblock locked.
1770          * That deadlocks, because orphan cleanup needs to lock the superblock
1771          * in numerous places.  Here we just pop the lock - it's relatively
1772          * harmless, because we are now ready to accept write_super() requests,
1773          * and aviro says that's the only reason for hanging onto the
1774          * superblock lock.
1775          */
1776         EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
1777         ext3_orphan_cleanup(sb, es);
1778         EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
1779         if (needs_recovery)
1780                 printk (KERN_INFO "EXT3-fs: recovery complete.\n");
1781         ext3_mark_recovery_complete(sb, es);
1782         printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
1783                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
1784                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
1785                 "writeback");
1786
1787         lock_kernel();
1788         return 0;
1789
1790 cantfind_ext3:
1791         if (!silent)
1792                 printk(KERN_ERR "VFS: Can't find ext3 filesystem on dev %s.\n",
1793                        sb->s_id);
1794         goto failed_mount;
1795
1796 failed_mount4:
1797         journal_destroy(sbi->s_journal);
1798 failed_mount3:
1799         percpu_counter_destroy(&sbi->s_freeblocks_counter);
1800         percpu_counter_destroy(&sbi->s_freeinodes_counter);
1801         percpu_counter_destroy(&sbi->s_dirs_counter);
1802 failed_mount2:
1803         for (i = 0; i < db_count; i++)
1804                 brelse(sbi->s_group_desc[i]);
1805         kfree(sbi->s_group_desc);
1806 failed_mount:
1807 #ifdef CONFIG_QUOTA
1808         for (i = 0; i < MAXQUOTAS; i++)
1809                 kfree(sbi->s_qf_names[i]);
1810 #endif
1811         ext3_blkdev_remove(sbi);
1812         brelse(bh);
1813 out_fail:
1814         sb->s_fs_info = NULL;
1815         kfree(sbi);
1816         lock_kernel();
1817         return -EINVAL;
1818 }
1819
1820 /*
1821  * Setup any per-fs journal parameters now.  We'll do this both on
1822  * initial mount, once the journal has been initialised but before we've
1823  * done any recovery; and again on any subsequent remount. 
1824  */
1825 static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
1826 {
1827         struct ext3_sb_info *sbi = EXT3_SB(sb);
1828
1829         if (sbi->s_commit_interval)
1830                 journal->j_commit_interval = sbi->s_commit_interval;
1831         /* We could also set up an ext3-specific default for the commit
1832          * interval here, but for now we'll just fall back to the jbd
1833          * default. */
1834
1835         spin_lock(&journal->j_state_lock);
1836         if (test_opt(sb, BARRIER))
1837                 journal->j_flags |= JFS_BARRIER;
1838         else
1839                 journal->j_flags &= ~JFS_BARRIER;
1840         spin_unlock(&journal->j_state_lock);
1841 }
1842
1843 static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
1844 {
1845         struct inode *journal_inode;
1846         journal_t *journal;
1847
1848         /* First, test for the existence of a valid inode on disk.  Bad
1849          * things happen if we iget() an unused inode, as the subsequent
1850          * iput() will try to delete it. */
1851
1852         journal_inode = iget(sb, journal_inum);
1853         if (!journal_inode) {
1854                 printk(KERN_ERR "EXT3-fs: no journal found.\n");
1855                 return NULL;
1856         }
1857         if (!journal_inode->i_nlink) {
1858                 make_bad_inode(journal_inode);
1859                 iput(journal_inode);
1860                 printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
1861                 return NULL;
1862         }
1863
1864         jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1865                   journal_inode, journal_inode->i_size);
1866         if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
1867                 printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
1868                 iput(journal_inode);
1869                 return NULL;
1870         }
1871
1872         journal = journal_init_inode(journal_inode);
1873         if (!journal) {
1874                 printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
1875                 iput(journal_inode);
1876                 return NULL;
1877         }
1878         journal->j_private = sb;
1879         ext3_init_journal_params(sb, journal);
1880         return journal;
1881 }
1882
1883 static journal_t *ext3_get_dev_journal(struct super_block *sb,
1884                                        dev_t j_dev)
1885 {
1886         struct buffer_head * bh;
1887         journal_t *journal;
1888         ext3_fsblk_t start;
1889         ext3_fsblk_t len;
1890         int hblock, blocksize;
1891         ext3_fsblk_t sb_block;
1892         unsigned long offset;
1893         struct ext3_super_block * es;
1894         struct block_device *bdev;
1895
1896         bdev = ext3_blkdev_get(j_dev);
1897         if (bdev == NULL)
1898                 return NULL;
1899
1900         if (bd_claim(bdev, sb)) {
1901                 printk(KERN_ERR
1902                         "EXT3: failed to claim external journal device.\n");
1903                 blkdev_put(bdev);
1904                 return NULL;
1905         }
1906
1907         blocksize = sb->s_blocksize;
1908         hblock = bdev_hardsect_size(bdev);
1909         if (blocksize < hblock) {
1910                 printk(KERN_ERR
1911                         "EXT3-fs: blocksize too small for journal device.\n");
1912                 goto out_bdev;
1913         }
1914
1915         sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
1916         offset = EXT3_MIN_BLOCK_SIZE % blocksize;
1917         set_blocksize(bdev, blocksize);
1918         if (!(bh = __bread(bdev, sb_block, blocksize))) {
1919                 printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
1920                        "external journal\n");
1921                 goto out_bdev;
1922         }
1923
1924         es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1925         if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
1926             !(le32_to_cpu(es->s_feature_incompat) &
1927               EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
1928                 printk(KERN_ERR "EXT3-fs: external journal has "
1929                                         "bad superblock\n");
1930                 brelse(bh);
1931                 goto out_bdev;
1932         }
1933
1934         if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
1935                 printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
1936                 brelse(bh);
1937                 goto out_bdev;
1938         }
1939
1940         len = le32_to_cpu(es->s_blocks_count);
1941         start = sb_block + 1;
1942         brelse(bh);     /* we're done with the superblock */
1943
1944         journal = journal_init_dev(bdev, sb->s_bdev,
1945                                         start, len, blocksize);
1946         if (!journal) {
1947                 printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
1948                 goto out_bdev;
1949         }
1950         journal->j_private = sb;
1951         ll_rw_block(READ, 1, &journal->j_sb_buffer);
1952         wait_on_buffer(journal->j_sb_buffer);
1953         if (!buffer_uptodate(journal->j_sb_buffer)) {
1954                 printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
1955                 goto out_journal;
1956         }
1957         if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
1958                 printk(KERN_ERR "EXT3-fs: External journal has more than one "
1959                                         "user (unsupported) - %d\n",
1960                         be32_to_cpu(journal->j_superblock->s_nr_users));
1961                 goto out_journal;
1962         }
1963         EXT3_SB(sb)->journal_bdev = bdev;
1964         ext3_init_journal_params(sb, journal);
1965         return journal;
1966 out_journal:
1967         journal_destroy(journal);
1968 out_bdev:
1969         ext3_blkdev_put(bdev);
1970         return NULL;
1971 }
1972
1973 static int ext3_load_journal(struct super_block *sb,
1974                              struct ext3_super_block *es,
1975                              unsigned long journal_devnum)
1976 {
1977         journal_t *journal;
1978         int journal_inum = le32_to_cpu(es->s_journal_inum);
1979         dev_t journal_dev;
1980         int err = 0;
1981         int really_read_only;
1982
1983         if (journal_devnum &&
1984             journal_devnum != le32_to_cpu(es->s_journal_dev)) {
1985                 printk(KERN_INFO "EXT3-fs: external journal device major/minor "
1986                         "numbers have changed\n");
1987                 journal_dev = new_decode_dev(journal_devnum);
1988         } else
1989                 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
1990
1991         really_read_only = bdev_read_only(sb->s_bdev);
1992
1993         /*
1994          * Are we loading a blank journal or performing recovery after a
1995          * crash?  For recovery, we need to check in advance whether we
1996          * can get read-write access to the device.
1997          */
1998
1999         if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
2000                 if (sb->s_flags & MS_RDONLY) {
2001                         printk(KERN_INFO "EXT3-fs: INFO: recovery "
2002                                         "required on readonly filesystem.\n");
2003                         if (really_read_only) {
2004                                 printk(KERN_ERR "EXT3-fs: write access "
2005                                         "unavailable, cannot proceed.\n");
2006                                 return -EROFS;
2007                         }
2008                         printk (KERN_INFO "EXT3-fs: write access will "
2009                                         "be enabled during recovery.\n");
2010                 }
2011         }
2012
2013         if (journal_inum && journal_dev) {
2014                 printk(KERN_ERR "EXT3-fs: filesystem has both journal "
2015                        "and inode journals!\n");
2016                 return -EINVAL;
2017         }
2018
2019         if (journal_inum) {
2020                 if (!(journal = ext3_get_journal(sb, journal_inum)))
2021                         return -EINVAL;
2022         } else {
2023                 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
2024                         return -EINVAL;
2025         }
2026
2027         if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2028                 err = journal_update_format(journal);
2029                 if (err)  {
2030                         printk(KERN_ERR "EXT3-fs: error updating journal.\n");
2031                         journal_destroy(journal);
2032                         return err;
2033                 }
2034         }
2035
2036         if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
2037                 err = journal_wipe(journal, !really_read_only);
2038         if (!err)
2039                 err = journal_load(journal);
2040
2041         if (err) {
2042                 printk(KERN_ERR "EXT3-fs: error loading journal.\n");
2043                 journal_destroy(journal);
2044                 return err;
2045         }
2046
2047         EXT3_SB(sb)->s_journal = journal;
2048         ext3_clear_journal_err(sb, es);
2049
2050         if (journal_devnum &&
2051             journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2052                 es->s_journal_dev = cpu_to_le32(journal_devnum);
2053                 sb->s_dirt = 1;
2054
2055                 /* Make sure we flush the recovery flag to disk. */
2056                 ext3_commit_super(sb, es, 1);
2057         }
2058
2059         return 0;
2060 }
2061
2062 static int ext3_create_journal(struct super_block * sb,
2063                                struct ext3_super_block * es,
2064                                int journal_inum)
2065 {
2066         journal_t *journal;
2067
2068         if (sb->s_flags & MS_RDONLY) {
2069                 printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
2070                                 "create journal.\n");
2071                 return -EROFS;
2072         }
2073
2074         if (!(journal = ext3_get_journal(sb, journal_inum)))
2075                 return -EINVAL;
2076
2077         printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
2078                journal_inum);
2079
2080         if (journal_create(journal)) {
2081                 printk(KERN_ERR "EXT3-fs: error creating journal.\n");
2082                 journal_destroy(journal);
2083                 return -EIO;
2084         }
2085
2086         EXT3_SB(sb)->s_journal = journal;
2087
2088         ext3_update_dynamic_rev(sb);
2089         EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2090         EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
2091
2092         es->s_journal_inum = cpu_to_le32(journal_inum);
2093         sb->s_dirt = 1;
2094
2095         /* Make sure we flush the recovery flag to disk. */
2096         ext3_commit_super(sb, es, 1);
2097
2098         return 0;
2099 }
2100
2101 static void ext3_commit_super (struct super_block * sb,
2102                                struct ext3_super_block * es,
2103                                int sync)
2104 {
2105         struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
2106
2107         if (!sbh)
2108                 return;
2109         es->s_wtime = cpu_to_le32(get_seconds());
2110         es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
2111         es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
2112         BUFFER_TRACE(sbh, "marking dirty");
2113         mark_buffer_dirty(sbh);
2114         if (sync)
2115                 sync_dirty_buffer(sbh);
2116 }
2117
2118
2119 /*
2120  * Have we just finished recovery?  If so, and if we are mounting (or
2121  * remounting) the filesystem readonly, then we will end up with a
2122  * consistent fs on disk.  Record that fact.
2123  */
2124 static void ext3_mark_recovery_complete(struct super_block * sb,
2125                                         struct ext3_super_block * es)
2126 {
2127         journal_t *journal = EXT3_SB(sb)->s_journal;
2128
2129         journal_lock_updates(journal);
2130         journal_flush(journal);
2131         if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
2132             sb->s_flags & MS_RDONLY) {
2133                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2134                 sb->s_dirt = 0;
2135                 ext3_commit_super(sb, es, 1);
2136         }
2137         journal_unlock_updates(journal);
2138 }
2139
2140 /*
2141  * If we are mounting (or read-write remounting) a filesystem whose journal
2142  * has recorded an error from a previous lifetime, move that error to the
2143  * main filesystem now.
2144  */
2145 static void ext3_clear_journal_err(struct super_block * sb,
2146                                    struct ext3_super_block * es)
2147 {
2148         journal_t *journal;
2149         int j_errno;
2150         const char *errstr;
2151
2152         journal = EXT3_SB(sb)->s_journal;
2153
2154         /*
2155          * Now check for any error status which may have been recorded in the
2156          * journal by a prior ext3_error() or ext3_abort()
2157          */
2158
2159         j_errno = journal_errno(journal);
2160         if (j_errno) {
2161                 char nbuf[16];
2162
2163                 errstr = ext3_decode_error(sb, j_errno, nbuf);
2164                 ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
2165                              "from previous mount: %s", errstr);
2166                 ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
2167                              "filesystem check.");
2168
2169                 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
2170                 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
2171                 ext3_commit_super (sb, es, 1);
2172
2173                 journal_clear_err(journal);
2174         }
2175 }
2176
2177 /*
2178  * Force the running and committing transactions to commit,
2179  * and wait on the commit.
2180  */
2181 int ext3_force_commit(struct super_block *sb)
2182 {
2183         journal_t *journal;
2184         int ret;
2185
2186         if (sb->s_flags & MS_RDONLY)
2187                 return 0;
2188
2189         journal = EXT3_SB(sb)->s_journal;
2190         sb->s_dirt = 0;
2191         ret = ext3_journal_force_commit(journal);
2192         return ret;
2193 }
2194
2195 /*
2196  * Ext3 always journals updates to the superblock itself, so we don't
2197  * have to propagate any other updates to the superblock on disk at this
2198  * point.  Just start an async writeback to get the buffers on their way
2199  * to the disk.
2200  *
2201  * This implicitly triggers the writebehind on sync().
2202  */
2203
2204 static void ext3_write_super (struct super_block * sb)
2205 {
2206         if (mutex_trylock(&sb->s_lock) != 0)
2207                 BUG();
2208         sb->s_dirt = 0;
2209 }
2210
2211 static int ext3_sync_fs(struct super_block *sb, int wait)
2212 {
2213         tid_t target;
2214
2215         sb->s_dirt = 0;
2216         if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
2217                 if (wait)
2218                         log_wait_commit(EXT3_SB(sb)->s_journal, target);
2219         }
2220         return 0;
2221 }
2222
2223 /*
2224  * LVM calls this function before a (read-only) snapshot is created.  This
2225  * gives us a chance to flush the journal completely and mark the fs clean.
2226  */
2227 static void ext3_write_super_lockfs(struct super_block *sb)
2228 {
2229         sb->s_dirt = 0;
2230
2231         if (!(sb->s_flags & MS_RDONLY)) {
2232                 journal_t *journal = EXT3_SB(sb)->s_journal;
2233
2234                 /* Now we set up the journal barrier. */
2235                 journal_lock_updates(journal);
2236                 journal_flush(journal);
2237
2238                 /* Journal blocked and flushed, clear needs_recovery flag. */
2239                 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2240                 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
2241         }
2242 }
2243
2244 /*
2245  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
2246  * flag here, even though the filesystem is not technically dirty yet.
2247  */
2248 static void ext3_unlockfs(struct super_block *sb)
2249 {
2250         if (!(sb->s_flags & MS_RDONLY)) {
2251                 lock_super(sb);
2252                 /* Reser the needs_recovery flag before the fs is unlocked. */
2253                 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2254                 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
2255                 unlock_super(sb);
2256                 journal_unlock_updates(EXT3_SB(sb)->s_journal);
2257         }
2258 }
2259
2260 static int ext3_remount (struct super_block * sb, int * flags, char * data)
2261 {
2262         struct ext3_super_block * es;
2263         struct ext3_sb_info *sbi = EXT3_SB(sb);
2264         ext3_fsblk_t n_blocks_count = 0;
2265         unsigned long old_sb_flags;
2266         struct ext3_mount_options old_opts;
2267         int err;
2268 #ifdef CONFIG_QUOTA
2269         int i;
2270 #endif
2271
2272         /* Store the original options */
2273         old_sb_flags = sb->s_flags;
2274         old_opts.s_mount_opt = sbi->s_mount_opt;
2275         old_opts.s_resuid = sbi->s_resuid;
2276         old_opts.s_resgid = sbi->s_resgid;
2277         old_opts.s_commit_interval = sbi->s_commit_interval;
2278 #ifdef CONFIG_QUOTA
2279         old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2280         for (i = 0; i < MAXQUOTAS; i++)
2281                 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2282 #endif
2283
2284         /*
2285          * Allow the "check" option to be passed as a remount option.
2286          */
2287         if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2288                 err = -EINVAL;
2289                 goto restore_opts;
2290         }
2291
2292         if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
2293                 ext3_abort(sb, __FUNCTION__, "Abort forced by user");
2294
2295         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2296                 ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2297
2298         es = sbi->s_es;
2299
2300         ext3_init_journal_params(sb, sbi->s_journal);
2301
2302         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2303                 n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
2304                 if (sbi->s_mount_opt & EXT3_MOUNT_ABORT) {
2305                         err = -EROFS;
2306                         goto restore_opts;
2307                 }
2308
2309                 if (*flags & MS_RDONLY) {
2310                         /*
2311                          * First of all, the unconditional stuff we have to do
2312                          * to disable replay of the journal when we next remount
2313                          */
2314                         sb->s_flags |= MS_RDONLY;
2315
2316                         /*
2317                          * OK, test if we are remounting a valid rw partition
2318                          * readonly, and if so set the rdonly flag and then
2319                          * mark the partition as valid again.
2320                          */
2321                         if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
2322                             (sbi->s_mount_state & EXT3_VALID_FS))
2323                                 es->s_state = cpu_to_le16(sbi->s_mount_state);
2324
2325                         ext3_mark_recovery_complete(sb, es);
2326                 } else {
2327                         __le32 ret;
2328                         if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
2329                                         ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
2330                                 printk(KERN_WARNING "EXT3-fs: %s: couldn't "
2331                                        "remount RDWR because of unsupported "
2332                                        "optional features (%x).\n",
2333                                        sb->s_id, le32_to_cpu(ret));
2334                                 err = -EROFS;
2335                                 goto restore_opts;
2336                         }
2337                         /*
2338                          * Mounting a RDONLY partition read-write, so reread
2339                          * and store the current valid flag.  (It may have
2340                          * been changed by e2fsck since we originally mounted
2341                          * the partition.)
2342                          */
2343                         ext3_clear_journal_err(sb, es);
2344                         sbi->s_mount_state = le16_to_cpu(es->s_state);
2345                         if ((ret = ext3_group_extend(sb, es, n_blocks_count))) {
2346                                 err = ret;
2347                                 goto restore_opts;
2348                         }
2349                         if (!ext3_setup_super (sb, es, 0))
2350                                 sb->s_flags &= ~MS_RDONLY;
2351                 }
2352         }
2353 #ifdef CONFIG_QUOTA
2354         /* Release old quota file names */
2355         for (i = 0; i < MAXQUOTAS; i++)
2356                 if (old_opts.s_qf_names[i] &&
2357                     old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2358                         kfree(old_opts.s_qf_names[i]);
2359 #endif
2360         return 0;
2361 restore_opts:
2362         sb->s_flags = old_sb_flags;
2363         sbi->s_mount_opt = old_opts.s_mount_opt;
2364         sbi->s_resuid = old_opts.s_resuid;
2365         sbi->s_resgid = old_opts.s_resgid;
2366         sbi->s_commit_interval = old_opts.s_commit_interval;
2367 #ifdef CONFIG_QUOTA
2368         sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2369         for (i = 0; i < MAXQUOTAS; i++) {
2370                 if (sbi->s_qf_names[i] &&
2371                     old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2372                         kfree(sbi->s_qf_names[i]);
2373                 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2374         }
2375 #endif
2376         return err;
2377 }
2378
2379 static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
2380 {
2381         struct super_block *sb = dentry->d_sb;
2382         struct ext3_sb_info *sbi = EXT3_SB(sb);
2383         struct ext3_super_block *es = sbi->s_es;
2384         ext3_fsblk_t overhead;
2385         int i;
2386
2387         if (test_opt (sb, MINIX_DF))
2388                 overhead = 0;
2389         else {
2390                 unsigned long ngroups;
2391                 ngroups = EXT3_SB(sb)->s_groups_count;
2392                 smp_rmb();
2393
2394                 /*
2395                  * Compute the overhead (FS structures)
2396                  */
2397
2398                 /*
2399                  * All of the blocks before first_data_block are
2400                  * overhead
2401                  */
2402                 overhead = le32_to_cpu(es->s_first_data_block);
2403
2404                 /*
2405                  * Add the overhead attributed to the superblock and
2406                  * block group descriptors.  If the sparse superblocks
2407                  * feature is turned on, then not all groups have this.
2408                  */
2409                 for (i = 0; i < ngroups; i++) {
2410                         overhead += ext3_bg_has_super(sb, i) +
2411                                 ext3_bg_num_gdb(sb, i);
2412                         cond_resched();
2413                 }
2414
2415                 /*
2416                  * Every block group has an inode bitmap, a block
2417                  * bitmap, and an inode table.
2418                  */
2419                 overhead += (ngroups * (2 + EXT3_SB(sb)->s_itb_per_group));
2420         }
2421
2422         buf->f_type = EXT3_SUPER_MAGIC;
2423         buf->f_bsize = sb->s_blocksize;
2424         buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
2425         buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
2426         buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
2427         if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
2428                 buf->f_bavail = 0;
2429         buf->f_files = le32_to_cpu(es->s_inodes_count);
2430         buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
2431         buf->f_namelen = EXT3_NAME_LEN;
2432         return 0;
2433 }
2434
2435 /* Helper function for writing quotas on sync - we need to start transaction before quota file
2436  * is locked for write. Otherwise the are possible deadlocks:
2437  * Process 1                         Process 2
2438  * ext3_create()                     quota_sync()
2439  *   journal_start()                   write_dquot()
2440  *   DQUOT_INIT()                        down(dqio_mutex)
2441  *     down(dqio_mutex)                    journal_start()
2442  *
2443  */
2444
2445 #ifdef CONFIG_QUOTA
2446
2447 static inline struct inode *dquot_to_inode(struct dquot *dquot)
2448 {
2449         return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2450 }
2451
2452 static int ext3_dquot_initialize(struct inode *inode, int type)
2453 {
2454         handle_t *handle;
2455         int ret, err;
2456
2457         /* We may create quota structure so we need to reserve enough blocks */
2458         handle = ext3_journal_start(inode, 2*EXT3_QUOTA_INIT_BLOCKS(inode->i_sb));
2459         if (IS_ERR(handle))
2460                 return PTR_ERR(handle);
2461         ret = dquot_initialize(inode, type);
2462         err = ext3_journal_stop(handle);
2463         if (!ret)
2464                 ret = err;
2465         return ret;
2466 }
2467
2468 static int ext3_dquot_drop(struct inode *inode)
2469 {
2470         handle_t *handle;
2471         int ret, err;
2472
2473         /* We may delete quota structure so we need to reserve enough blocks */
2474         handle = ext3_journal_start(inode, 2*EXT3_QUOTA_DEL_BLOCKS(inode->i_sb));
2475         if (IS_ERR(handle))
2476                 return PTR_ERR(handle);
2477         ret = dquot_drop(inode);
2478         err = ext3_journal_stop(handle);
2479         if (!ret)
2480                 ret = err;
2481         return ret;
2482 }
2483
2484 static int ext3_write_dquot(struct dquot *dquot)
2485 {
2486         int ret, err;
2487         handle_t *handle;
2488         struct inode *inode;
2489
2490         inode = dquot_to_inode(dquot);
2491         handle = ext3_journal_start(inode,
2492                                         EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2493         if (IS_ERR(handle))
2494                 return PTR_ERR(handle);
2495         ret = dquot_commit(dquot);
2496         err = ext3_journal_stop(handle);
2497         if (!ret)
2498                 ret = err;
2499         return ret;
2500 }
2501
2502 static int ext3_acquire_dquot(struct dquot *dquot)
2503 {
2504         int ret, err;
2505         handle_t *handle;
2506
2507         handle = ext3_journal_start(dquot_to_inode(dquot),
2508                                         EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2509         if (IS_ERR(handle))
2510                 return PTR_ERR(handle);
2511         ret = dquot_acquire(dquot);
2512         err = ext3_journal_stop(handle);
2513         if (!ret)
2514                 ret = err;
2515         return ret;
2516 }
2517
2518 static int ext3_release_dquot(struct dquot *dquot)
2519 {
2520         int ret, err;
2521         handle_t *handle;
2522
2523         handle = ext3_journal_start(dquot_to_inode(dquot),
2524                                         EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2525         if (IS_ERR(handle))
2526                 return PTR_ERR(handle);
2527         ret = dquot_release(dquot);
2528         err = ext3_journal_stop(handle);
2529         if (!ret)
2530                 ret = err;
2531         return ret;
2532 }
2533
2534 static int ext3_mark_dquot_dirty(struct dquot *dquot)
2535 {
2536         /* Are we journalling quotas? */
2537         if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2538             EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2539                 dquot_mark_dquot_dirty(dquot);
2540                 return ext3_write_dquot(dquot);
2541         } else {
2542                 return dquot_mark_dquot_dirty(dquot);
2543         }
2544 }
2545
2546 static int ext3_write_info(struct super_block *sb, int type)
2547 {
2548         int ret, err;
2549         handle_t *handle;
2550
2551         /* Data block + inode block */
2552         handle = ext3_journal_start(sb->s_root->d_inode, 2);
2553         if (IS_ERR(handle))
2554                 return PTR_ERR(handle);
2555         ret = dquot_commit_info(sb, type);
2556         err = ext3_journal_stop(handle);
2557         if (!ret)
2558                 ret = err;
2559         return ret;
2560 }
2561
2562 /*
2563  * Turn on quotas during mount time - we need to find
2564  * the quota file and such...
2565  */
2566 static int ext3_quota_on_mount(struct super_block *sb, int type)
2567 {
2568         return vfs_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
2569                         EXT3_SB(sb)->s_jquota_fmt, type);
2570 }
2571
2572 /*
2573  * Standard function to be called on quota_on
2574  */
2575 static int ext3_quota_on(struct super_block *sb, int type, int format_id,
2576                          char *path)
2577 {
2578         int err;
2579         struct nameidata nd;
2580
2581         if (!test_opt(sb, QUOTA))
2582                 return -EINVAL;
2583         /* Not journalling quota? */
2584         if (!EXT3_SB(sb)->s_qf_names[USRQUOTA] &&
2585             !EXT3_SB(sb)->s_qf_names[GRPQUOTA])
2586                 return vfs_quota_on(sb, type, format_id, path);
2587         err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2588         if (err)
2589                 return err;
2590         /* Quotafile not on the same filesystem? */
2591         if (nd.mnt->mnt_sb != sb) {
2592                 path_release(&nd);
2593                 return -EXDEV;
2594         }
2595         /* Quotafile not of fs root? */
2596         if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2597                 printk(KERN_WARNING
2598                         "EXT3-fs: Quota file not on filesystem root. "
2599                         "Journalled quota will not work.\n");
2600         path_release(&nd);
2601         return vfs_quota_on(sb, type, format_id, path);
2602 }
2603
2604 /* Read data from quotafile - avoid pagecache and such because we cannot afford
2605  * acquiring the locks... As quota files are never truncated and quota code
2606  * itself serializes the operations (and noone else should touch the files)
2607  * we don't have to be afraid of races */
2608 static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
2609                                size_t len, loff_t off)
2610 {
2611         struct inode *inode = sb_dqopt(sb)->files[type];
2612         sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
2613         int err = 0;
2614         int offset = off & (sb->s_blocksize - 1);
2615         int tocopy;
2616         size_t toread;
2617         struct buffer_head *bh;
2618         loff_t i_size = i_size_read(inode);
2619
2620         if (off > i_size)
2621                 return 0;
2622         if (off+len > i_size)
2623                 len = i_size-off;
2624         toread = len;
2625         while (toread > 0) {
2626                 tocopy = sb->s_blocksize - offset < toread ?
2627                                 sb->s_blocksize - offset : toread;
2628                 bh = ext3_bread(NULL, inode, blk, 0, &err);
2629                 if (err)
2630                         return err;
2631                 if (!bh)        /* A hole? */
2632                         memset(data, 0, tocopy);
2633                 else
2634                         memcpy(data, bh->b_data+offset, tocopy);
2635                 brelse(bh);
2636                 offset = 0;
2637                 toread -= tocopy;
2638                 data += tocopy;
2639                 blk++;
2640         }
2641         return len;
2642 }
2643
2644 /* Write to quotafile (we know the transaction is already started and has
2645  * enough credits) */
2646 static ssize_t ext3_quota_write(struct super_block *sb, int type,
2647                                 const char *data, size_t len, loff_t off)
2648 {
2649         struct inode *inode = sb_dqopt(sb)->files[type];
2650         sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
2651         int err = 0;
2652         int offset = off & (sb->s_blocksize - 1);
2653         int tocopy;
2654         int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
2655         size_t towrite = len;
2656         struct buffer_head *bh;
2657         handle_t *handle = journal_current_handle();
2658
2659         mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2660         while (towrite > 0) {
2661                 tocopy = sb->s_blocksize - offset < towrite ?
2662                                 sb->s_blocksize - offset : towrite;
2663                 bh = ext3_bread(handle, inode, blk, 1, &err);
2664                 if (!bh)
2665                         goto out;
2666                 if (journal_quota) {
2667                         err = ext3_journal_get_write_access(handle, bh);
2668                         if (err) {
2669                                 brelse(bh);
2670                                 goto out;
2671                         }
2672                 }
2673                 lock_buffer(bh);
2674                 memcpy(bh->b_data+offset, data, tocopy);
2675                 flush_dcache_page(bh->b_page);
2676                 unlock_buffer(bh);
2677                 if (journal_quota)
2678                         err = ext3_journal_dirty_metadata(handle, bh);
2679                 else {
2680                         /* Always do at least ordered writes for quotas */
2681                         err = ext3_journal_dirty_data(handle, bh);
2682                         mark_buffer_dirty(bh);
2683                 }
2684                 brelse(bh);
2685                 if (err)
2686                         goto out;
2687                 offset = 0;
2688                 towrite -= tocopy;
2689                 data += tocopy;
2690                 blk++;
2691         }
2692 out:
2693         if (len == towrite)
2694                 return err;
2695         if (inode->i_size < off+len-towrite) {
2696                 i_size_write(inode, off+len-towrite);
2697                 EXT3_I(inode)->i_disksize = inode->i_size;
2698         }
2699         inode->i_version++;
2700         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2701         ext3_mark_inode_dirty(handle, inode);
2702         mutex_unlock(&inode->i_mutex);
2703         return len - towrite;
2704 }
2705
2706 #endif
2707
2708 static int ext3_get_sb(struct file_system_type *fs_type,
2709         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2710 {
2711         return get_sb_bdev(fs_type, flags, dev_name, data, ext3_fill_super, mnt);
2712 }
2713
2714 static struct file_system_type ext3_fs_type = {
2715         .owner          = THIS_MODULE,
2716         .name           = "ext3",
2717         .get_sb         = ext3_get_sb,
2718         .kill_sb        = kill_block_super,
2719         .fs_flags       = FS_REQUIRES_DEV,
2720 };
2721
2722 static int __init init_ext3_fs(void)
2723 {
2724         int err = init_ext3_xattr();
2725         if (err)
2726                 return err;
2727         err = init_inodecache();
2728         if (err)
2729                 goto out1;
2730         err = register_filesystem(&ext3_fs_type);
2731         if (err)
2732                 goto out;
2733         return 0;
2734 out:
2735         destroy_inodecache();
2736 out1:
2737         exit_ext3_xattr();
2738         return err;
2739 }
2740
2741 static void __exit exit_ext3_fs(void)
2742 {
2743         unregister_filesystem(&ext3_fs_type);
2744         destroy_inodecache();
2745         exit_ext3_xattr();
2746 }
2747
2748 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
2749 MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
2750 MODULE_LICENSE("GPL");
2751 module_init(init_ext3_fs)
2752 module_exit(exit_ext3_fs)