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