]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/ext4/super.c
ext4: fix BUG when calling ext4_error with locked block group
[mv-sheeva.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/slab.h>
25 #include <linux/init.h>
26 #include <linux/blkdev.h>
27 #include <linux/parser.h>
28 #include <linux/smp_lock.h>
29 #include <linux/buffer_head.h>
30 #include <linux/exportfs.h>
31 #include <linux/vfs.h>
32 #include <linux/random.h>
33 #include <linux/mount.h>
34 #include <linux/namei.h>
35 #include <linux/quotaops.h>
36 #include <linux/seq_file.h>
37 #include <linux/proc_fs.h>
38 #include <linux/marker.h>
39 #include <linux/log2.h>
40 #include <linux/crc16.h>
41 #include <asm/uaccess.h>
42
43 #include "ext4.h"
44 #include "ext4_jbd2.h"
45 #include "xattr.h"
46 #include "acl.h"
47 #include "namei.h"
48 #include "group.h"
49
50 struct proc_dir_entry *ext4_proc_root;
51
52 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
53                              unsigned long journal_devnum);
54 static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
55                                unsigned int);
56 static void ext4_commit_super(struct super_block *sb,
57                               struct ext4_super_block *es, int sync);
58 static void ext4_mark_recovery_complete(struct super_block *sb,
59                                         struct ext4_super_block *es);
60 static void ext4_clear_journal_err(struct super_block *sb,
61                                    struct ext4_super_block *es);
62 static int ext4_sync_fs(struct super_block *sb, int wait);
63 static const char *ext4_decode_error(struct super_block *sb, int errno,
64                                      char nbuf[16]);
65 static int ext4_remount(struct super_block *sb, int *flags, char *data);
66 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
67 static void ext4_unlockfs(struct super_block *sb);
68 static void ext4_write_super(struct super_block *sb);
69 static void ext4_write_super_lockfs(struct super_block *sb);
70
71
72 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
73                                struct ext4_group_desc *bg)
74 {
75         return le32_to_cpu(bg->bg_block_bitmap_lo) |
76                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
77                 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
78 }
79
80 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
81                                struct ext4_group_desc *bg)
82 {
83         return le32_to_cpu(bg->bg_inode_bitmap_lo) |
84                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
85                 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
86 }
87
88 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
89                               struct ext4_group_desc *bg)
90 {
91         return le32_to_cpu(bg->bg_inode_table_lo) |
92                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
93                 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
94 }
95
96 void ext4_block_bitmap_set(struct super_block *sb,
97                            struct ext4_group_desc *bg, ext4_fsblk_t blk)
98 {
99         bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
100         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
101                 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
102 }
103
104 void ext4_inode_bitmap_set(struct super_block *sb,
105                            struct ext4_group_desc *bg, ext4_fsblk_t blk)
106 {
107         bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
108         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
109                 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
110 }
111
112 void ext4_inode_table_set(struct super_block *sb,
113                           struct ext4_group_desc *bg, ext4_fsblk_t blk)
114 {
115         bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
116         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
117                 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
118 }
119
120 /*
121  * Wrappers for jbd2_journal_start/end.
122  *
123  * The only special thing we need to do here is to make sure that all
124  * journal_end calls result in the superblock being marked dirty, so
125  * that sync() will call the filesystem's write_super callback if
126  * appropriate.
127  */
128 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
129 {
130         journal_t *journal;
131
132         if (sb->s_flags & MS_RDONLY)
133                 return ERR_PTR(-EROFS);
134
135         /* Special case here: if the journal has aborted behind our
136          * backs (eg. EIO in the commit thread), then we still need to
137          * take the FS itself readonly cleanly. */
138         journal = EXT4_SB(sb)->s_journal;
139         if (journal) {
140                 if (is_journal_aborted(journal)) {
141                         ext4_abort(sb, __func__,
142                                    "Detected aborted journal");
143                         return ERR_PTR(-EROFS);
144                 }
145                 return jbd2_journal_start(journal, nblocks);
146         }
147         /*
148          * We're not journaling, return the appropriate indication.
149          */
150         current->journal_info = EXT4_NOJOURNAL_HANDLE;
151         return current->journal_info;
152 }
153
154 /*
155  * The only special thing we need to do here is to make sure that all
156  * jbd2_journal_stop calls result in the superblock being marked dirty, so
157  * that sync() will call the filesystem's write_super callback if
158  * appropriate.
159  */
160 int __ext4_journal_stop(const char *where, handle_t *handle)
161 {
162         struct super_block *sb;
163         int err;
164         int rc;
165
166         if (!ext4_handle_valid(handle)) {
167                 /*
168                  * Do this here since we don't call jbd2_journal_stop() in
169                  * no-journal mode.
170                  */
171                 current->journal_info = NULL;
172                 return 0;
173         }
174         sb = handle->h_transaction->t_journal->j_private;
175         err = handle->h_err;
176         rc = jbd2_journal_stop(handle);
177
178         if (!err)
179                 err = rc;
180         if (err)
181                 __ext4_std_error(sb, where, err);
182         return err;
183 }
184
185 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
186                 struct buffer_head *bh, handle_t *handle, int err)
187 {
188         char nbuf[16];
189         const char *errstr = ext4_decode_error(NULL, err, nbuf);
190
191         BUG_ON(!ext4_handle_valid(handle));
192
193         if (bh)
194                 BUFFER_TRACE(bh, "abort");
195
196         if (!handle->h_err)
197                 handle->h_err = err;
198
199         if (is_handle_aborted(handle))
200                 return;
201
202         printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
203                caller, errstr, err_fn);
204
205         jbd2_journal_abort_handle(handle);
206 }
207
208 /* Deal with the reporting of failure conditions on a filesystem such as
209  * inconsistencies detected or read IO failures.
210  *
211  * On ext2, we can store the error state of the filesystem in the
212  * superblock.  That is not possible on ext4, because we may have other
213  * write ordering constraints on the superblock which prevent us from
214  * writing it out straight away; and given that the journal is about to
215  * be aborted, we can't rely on the current, or future, transactions to
216  * write out the superblock safely.
217  *
218  * We'll just use the jbd2_journal_abort() error code to record an error in
219  * the journal instead.  On recovery, the journal will compain about
220  * that error until we've noted it down and cleared it.
221  */
222
223 static void ext4_handle_error(struct super_block *sb)
224 {
225         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
226
227         EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
228         es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
229
230         if (sb->s_flags & MS_RDONLY)
231                 return;
232
233         if (!test_opt(sb, ERRORS_CONT)) {
234                 journal_t *journal = EXT4_SB(sb)->s_journal;
235
236                 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
237                 if (journal)
238                         jbd2_journal_abort(journal, -EIO);
239         }
240         if (test_opt(sb, ERRORS_RO)) {
241                 printk(KERN_CRIT "Remounting filesystem read-only\n");
242                 sb->s_flags |= MS_RDONLY;
243         }
244         ext4_commit_super(sb, es, 1);
245         if (test_opt(sb, ERRORS_PANIC))
246                 panic("EXT4-fs (device %s): panic forced after error\n",
247                         sb->s_id);
248 }
249
250 void ext4_error(struct super_block *sb, const char *function,
251                 const char *fmt, ...)
252 {
253         va_list args;
254
255         va_start(args, fmt);
256         printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
257         vprintk(fmt, args);
258         printk("\n");
259         va_end(args);
260
261         ext4_handle_error(sb);
262 }
263
264 static const char *ext4_decode_error(struct super_block *sb, int errno,
265                                      char nbuf[16])
266 {
267         char *errstr = NULL;
268
269         switch (errno) {
270         case -EIO:
271                 errstr = "IO failure";
272                 break;
273         case -ENOMEM:
274                 errstr = "Out of memory";
275                 break;
276         case -EROFS:
277                 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
278                         errstr = "Journal has aborted";
279                 else
280                         errstr = "Readonly filesystem";
281                 break;
282         default:
283                 /* If the caller passed in an extra buffer for unknown
284                  * errors, textualise them now.  Else we just return
285                  * NULL. */
286                 if (nbuf) {
287                         /* Check for truncated error codes... */
288                         if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
289                                 errstr = nbuf;
290                 }
291                 break;
292         }
293
294         return errstr;
295 }
296
297 /* __ext4_std_error decodes expected errors from journaling functions
298  * automatically and invokes the appropriate error response.  */
299
300 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
301 {
302         char nbuf[16];
303         const char *errstr;
304
305         /* Special case: if the error is EROFS, and we're not already
306          * inside a transaction, then there's really no point in logging
307          * an error. */
308         if (errno == -EROFS && journal_current_handle() == NULL &&
309             (sb->s_flags & MS_RDONLY))
310                 return;
311
312         errstr = ext4_decode_error(sb, errno, nbuf);
313         printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
314                sb->s_id, function, errstr);
315
316         ext4_handle_error(sb);
317 }
318
319 /*
320  * ext4_abort is a much stronger failure handler than ext4_error.  The
321  * abort function may be used to deal with unrecoverable failures such
322  * as journal IO errors or ENOMEM at a critical moment in log management.
323  *
324  * We unconditionally force the filesystem into an ABORT|READONLY state,
325  * unless the error response on the fs has been set to panic in which
326  * case we take the easy way out and panic immediately.
327  */
328
329 void ext4_abort(struct super_block *sb, const char *function,
330                 const char *fmt, ...)
331 {
332         va_list args;
333
334         printk(KERN_CRIT "ext4_abort called.\n");
335
336         va_start(args, fmt);
337         printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
338         vprintk(fmt, args);
339         printk("\n");
340         va_end(args);
341
342         if (test_opt(sb, ERRORS_PANIC))
343                 panic("EXT4-fs panic from previous error\n");
344
345         if (sb->s_flags & MS_RDONLY)
346                 return;
347
348         printk(KERN_CRIT "Remounting filesystem read-only\n");
349         EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
350         sb->s_flags |= MS_RDONLY;
351         EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
352         if (EXT4_SB(sb)->s_journal)
353                 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
354 }
355
356 void ext4_warning(struct super_block *sb, const char *function,
357                   const char *fmt, ...)
358 {
359         va_list args;
360
361         va_start(args, fmt);
362         printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
363                sb->s_id, function);
364         vprintk(fmt, args);
365         printk("\n");
366         va_end(args);
367 }
368
369 void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
370                                 const char *function, const char *fmt, ...)
371 __releases(bitlock)
372 __acquires(bitlock)
373 {
374         va_list args;
375         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
376
377         va_start(args, fmt);
378         printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
379         vprintk(fmt, args);
380         printk("\n");
381         va_end(args);
382
383         if (test_opt(sb, ERRORS_CONT)) {
384                 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
385                 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
386                 ext4_commit_super(sb, es, 0);
387                 return;
388         }
389         ext4_unlock_group(sb, grp);
390         ext4_handle_error(sb);
391         /*
392          * We only get here in the ERRORS_RO case; relocking the group
393          * may be dangerous, but nothing bad will happen since the
394          * filesystem will have already been marked read/only and the
395          * journal has been aborted.  We return 1 as a hint to callers
396          * who might what to use the return value from
397          * ext4_grp_locked_error() to distinguish beween the
398          * ERRORS_CONT and ERRORS_RO case, and perhaps return more
399          * aggressively from the ext4 function in question, with a
400          * more appropriate error code.
401          */
402         ext4_lock_group(sb, grp);
403         return;
404 }
405
406
407 void ext4_update_dynamic_rev(struct super_block *sb)
408 {
409         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
410
411         if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
412                 return;
413
414         ext4_warning(sb, __func__,
415                      "updating to rev %d because of new feature flag, "
416                      "running e2fsck is recommended",
417                      EXT4_DYNAMIC_REV);
418
419         es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
420         es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
421         es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
422         /* leave es->s_feature_*compat flags alone */
423         /* es->s_uuid will be set by e2fsck if empty */
424
425         /*
426          * The rest of the superblock fields should be zero, and if not it
427          * means they are likely already in use, so leave them alone.  We
428          * can leave it up to e2fsck to clean up any inconsistencies there.
429          */
430 }
431
432 /*
433  * Open the external journal device
434  */
435 static struct block_device *ext4_blkdev_get(dev_t dev)
436 {
437         struct block_device *bdev;
438         char b[BDEVNAME_SIZE];
439
440         bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
441         if (IS_ERR(bdev))
442                 goto fail;
443         return bdev;
444
445 fail:
446         printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
447                         __bdevname(dev, b), PTR_ERR(bdev));
448         return NULL;
449 }
450
451 /*
452  * Release the journal device
453  */
454 static int ext4_blkdev_put(struct block_device *bdev)
455 {
456         bd_release(bdev);
457         return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
458 }
459
460 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
461 {
462         struct block_device *bdev;
463         int ret = -ENODEV;
464
465         bdev = sbi->journal_bdev;
466         if (bdev) {
467                 ret = ext4_blkdev_put(bdev);
468                 sbi->journal_bdev = NULL;
469         }
470         return ret;
471 }
472
473 static inline struct inode *orphan_list_entry(struct list_head *l)
474 {
475         return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
476 }
477
478 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
479 {
480         struct list_head *l;
481
482         printk(KERN_ERR "sb orphan head is %d\n",
483                le32_to_cpu(sbi->s_es->s_last_orphan));
484
485         printk(KERN_ERR "sb_info orphan list:\n");
486         list_for_each(l, &sbi->s_orphan) {
487                 struct inode *inode = orphan_list_entry(l);
488                 printk(KERN_ERR "  "
489                        "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
490                        inode->i_sb->s_id, inode->i_ino, inode,
491                        inode->i_mode, inode->i_nlink,
492                        NEXT_ORPHAN(inode));
493         }
494 }
495
496 static void ext4_put_super(struct super_block *sb)
497 {
498         struct ext4_sb_info *sbi = EXT4_SB(sb);
499         struct ext4_super_block *es = sbi->s_es;
500         int i, err;
501
502         ext4_mb_release(sb);
503         ext4_ext_release(sb);
504         ext4_xattr_put_super(sb);
505         if (sbi->s_journal) {
506                 err = jbd2_journal_destroy(sbi->s_journal);
507                 sbi->s_journal = NULL;
508                 if (err < 0)
509                         ext4_abort(sb, __func__,
510                                    "Couldn't clean up the journal");
511         }
512         if (!(sb->s_flags & MS_RDONLY)) {
513                 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
514                 es->s_state = cpu_to_le16(sbi->s_mount_state);
515                 ext4_commit_super(sb, es, 1);
516         }
517         if (sbi->s_proc) {
518                 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
519                 remove_proc_entry(sb->s_id, ext4_proc_root);
520         }
521
522         for (i = 0; i < sbi->s_gdb_count; i++)
523                 brelse(sbi->s_group_desc[i]);
524         kfree(sbi->s_group_desc);
525         kfree(sbi->s_flex_groups);
526         percpu_counter_destroy(&sbi->s_freeblocks_counter);
527         percpu_counter_destroy(&sbi->s_freeinodes_counter);
528         percpu_counter_destroy(&sbi->s_dirs_counter);
529         percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
530         brelse(sbi->s_sbh);
531 #ifdef CONFIG_QUOTA
532         for (i = 0; i < MAXQUOTAS; i++)
533                 kfree(sbi->s_qf_names[i]);
534 #endif
535
536         /* Debugging code just in case the in-memory inode orphan list
537          * isn't empty.  The on-disk one can be non-empty if we've
538          * detected an error and taken the fs readonly, but the
539          * in-memory list had better be clean by this point. */
540         if (!list_empty(&sbi->s_orphan))
541                 dump_orphan_list(sb, sbi);
542         J_ASSERT(list_empty(&sbi->s_orphan));
543
544         invalidate_bdev(sb->s_bdev);
545         if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
546                 /*
547                  * Invalidate the journal device's buffers.  We don't want them
548                  * floating about in memory - the physical journal device may
549                  * hotswapped, and it breaks the `ro-after' testing code.
550                  */
551                 sync_blockdev(sbi->journal_bdev);
552                 invalidate_bdev(sbi->journal_bdev);
553                 ext4_blkdev_remove(sbi);
554         }
555         sb->s_fs_info = NULL;
556         kfree(sbi);
557         return;
558 }
559
560 static struct kmem_cache *ext4_inode_cachep;
561
562 /*
563  * Called inside transaction, so use GFP_NOFS
564  */
565 static struct inode *ext4_alloc_inode(struct super_block *sb)
566 {
567         struct ext4_inode_info *ei;
568
569         ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
570         if (!ei)
571                 return NULL;
572 #ifdef CONFIG_EXT4_FS_POSIX_ACL
573         ei->i_acl = EXT4_ACL_NOT_CACHED;
574         ei->i_default_acl = EXT4_ACL_NOT_CACHED;
575 #endif
576         ei->vfs_inode.i_version = 1;
577         ei->vfs_inode.i_data.writeback_index = 0;
578         memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
579         INIT_LIST_HEAD(&ei->i_prealloc_list);
580         spin_lock_init(&ei->i_prealloc_lock);
581         /*
582          * Note:  We can be called before EXT4_SB(sb)->s_journal is set,
583          * therefore it can be null here.  Don't check it, just initialize
584          * jinode.
585          */
586         jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
587         ei->i_reserved_data_blocks = 0;
588         ei->i_reserved_meta_blocks = 0;
589         ei->i_allocated_meta_blocks = 0;
590         ei->i_delalloc_reserved_flag = 0;
591         spin_lock_init(&(ei->i_block_reservation_lock));
592         return &ei->vfs_inode;
593 }
594
595 static void ext4_destroy_inode(struct inode *inode)
596 {
597         if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
598                 printk("EXT4 Inode %p: orphan list check failed!\n",
599                         EXT4_I(inode));
600                 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
601                                 EXT4_I(inode), sizeof(struct ext4_inode_info),
602                                 true);
603                 dump_stack();
604         }
605         kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
606 }
607
608 static void init_once(void *foo)
609 {
610         struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
611
612         INIT_LIST_HEAD(&ei->i_orphan);
613 #ifdef CONFIG_EXT4_FS_XATTR
614         init_rwsem(&ei->xattr_sem);
615 #endif
616         init_rwsem(&ei->i_data_sem);
617         inode_init_once(&ei->vfs_inode);
618 }
619
620 static int init_inodecache(void)
621 {
622         ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
623                                              sizeof(struct ext4_inode_info),
624                                              0, (SLAB_RECLAIM_ACCOUNT|
625                                                 SLAB_MEM_SPREAD),
626                                              init_once);
627         if (ext4_inode_cachep == NULL)
628                 return -ENOMEM;
629         return 0;
630 }
631
632 static void destroy_inodecache(void)
633 {
634         kmem_cache_destroy(ext4_inode_cachep);
635 }
636
637 static void ext4_clear_inode(struct inode *inode)
638 {
639 #ifdef CONFIG_EXT4_FS_POSIX_ACL
640         if (EXT4_I(inode)->i_acl &&
641                         EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
642                 posix_acl_release(EXT4_I(inode)->i_acl);
643                 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
644         }
645         if (EXT4_I(inode)->i_default_acl &&
646                         EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
647                 posix_acl_release(EXT4_I(inode)->i_default_acl);
648                 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
649         }
650 #endif
651         ext4_discard_preallocations(inode);
652         if (EXT4_JOURNAL(inode))
653                 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
654                                        &EXT4_I(inode)->jinode);
655 }
656
657 static inline void ext4_show_quota_options(struct seq_file *seq,
658                                            struct super_block *sb)
659 {
660 #if defined(CONFIG_QUOTA)
661         struct ext4_sb_info *sbi = EXT4_SB(sb);
662
663         if (sbi->s_jquota_fmt)
664                 seq_printf(seq, ",jqfmt=%s",
665                 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
666
667         if (sbi->s_qf_names[USRQUOTA])
668                 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
669
670         if (sbi->s_qf_names[GRPQUOTA])
671                 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
672
673         if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
674                 seq_puts(seq, ",usrquota");
675
676         if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
677                 seq_puts(seq, ",grpquota");
678 #endif
679 }
680
681 /*
682  * Show an option if
683  *  - it's set to a non-default value OR
684  *  - if the per-sb default is different from the global default
685  */
686 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
687 {
688         int def_errors;
689         unsigned long def_mount_opts;
690         struct super_block *sb = vfs->mnt_sb;
691         struct ext4_sb_info *sbi = EXT4_SB(sb);
692         struct ext4_super_block *es = sbi->s_es;
693
694         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
695         def_errors     = le16_to_cpu(es->s_errors);
696
697         if (sbi->s_sb_block != 1)
698                 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
699         if (test_opt(sb, MINIX_DF))
700                 seq_puts(seq, ",minixdf");
701         if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
702                 seq_puts(seq, ",grpid");
703         if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
704                 seq_puts(seq, ",nogrpid");
705         if (sbi->s_resuid != EXT4_DEF_RESUID ||
706             le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
707                 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
708         }
709         if (sbi->s_resgid != EXT4_DEF_RESGID ||
710             le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
711                 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
712         }
713         if (test_opt(sb, ERRORS_RO)) {
714                 if (def_errors == EXT4_ERRORS_PANIC ||
715                     def_errors == EXT4_ERRORS_CONTINUE) {
716                         seq_puts(seq, ",errors=remount-ro");
717                 }
718         }
719         if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
720                 seq_puts(seq, ",errors=continue");
721         if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
722                 seq_puts(seq, ",errors=panic");
723         if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
724                 seq_puts(seq, ",nouid32");
725         if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
726                 seq_puts(seq, ",debug");
727         if (test_opt(sb, OLDALLOC))
728                 seq_puts(seq, ",oldalloc");
729 #ifdef CONFIG_EXT4_FS_XATTR
730         if (test_opt(sb, XATTR_USER) &&
731                 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
732                 seq_puts(seq, ",user_xattr");
733         if (!test_opt(sb, XATTR_USER) &&
734             (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
735                 seq_puts(seq, ",nouser_xattr");
736         }
737 #endif
738 #ifdef CONFIG_EXT4_FS_POSIX_ACL
739         if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
740                 seq_puts(seq, ",acl");
741         if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
742                 seq_puts(seq, ",noacl");
743 #endif
744         if (!test_opt(sb, RESERVATION))
745                 seq_puts(seq, ",noreservation");
746         if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
747                 seq_printf(seq, ",commit=%u",
748                            (unsigned) (sbi->s_commit_interval / HZ));
749         }
750         if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
751                 seq_printf(seq, ",min_batch_time=%u",
752                            (unsigned) sbi->s_min_batch_time);
753         }
754         if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
755                 seq_printf(seq, ",max_batch_time=%u",
756                            (unsigned) sbi->s_min_batch_time);
757         }
758
759         /*
760          * We're changing the default of barrier mount option, so
761          * let's always display its mount state so it's clear what its
762          * status is.
763          */
764         seq_puts(seq, ",barrier=");
765         seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
766         if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
767                 seq_puts(seq, ",journal_async_commit");
768         if (test_opt(sb, NOBH))
769                 seq_puts(seq, ",nobh");
770         if (!test_opt(sb, EXTENTS))
771                 seq_puts(seq, ",noextents");
772         if (test_opt(sb, I_VERSION))
773                 seq_puts(seq, ",i_version");
774         if (!test_opt(sb, DELALLOC))
775                 seq_puts(seq, ",nodelalloc");
776
777
778         if (sbi->s_stripe)
779                 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
780         /*
781          * journal mode get enabled in different ways
782          * So just print the value even if we didn't specify it
783          */
784         if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
785                 seq_puts(seq, ",data=journal");
786         else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
787                 seq_puts(seq, ",data=ordered");
788         else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
789                 seq_puts(seq, ",data=writeback");
790
791         if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
792                 seq_printf(seq, ",inode_readahead_blks=%u",
793                            sbi->s_inode_readahead_blks);
794
795         if (test_opt(sb, DATA_ERR_ABORT))
796                 seq_puts(seq, ",data_err=abort");
797
798         ext4_show_quota_options(seq, sb);
799         return 0;
800 }
801
802
803 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
804                 u64 ino, u32 generation)
805 {
806         struct inode *inode;
807
808         if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
809                 return ERR_PTR(-ESTALE);
810         if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
811                 return ERR_PTR(-ESTALE);
812
813         /* iget isn't really right if the inode is currently unallocated!!
814          *
815          * ext4_read_inode will return a bad_inode if the inode had been
816          * deleted, so we should be safe.
817          *
818          * Currently we don't know the generation for parent directory, so
819          * a generation of 0 means "accept any"
820          */
821         inode = ext4_iget(sb, ino);
822         if (IS_ERR(inode))
823                 return ERR_CAST(inode);
824         if (generation && inode->i_generation != generation) {
825                 iput(inode);
826                 return ERR_PTR(-ESTALE);
827         }
828
829         return inode;
830 }
831
832 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
833                 int fh_len, int fh_type)
834 {
835         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
836                                     ext4_nfs_get_inode);
837 }
838
839 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
840                 int fh_len, int fh_type)
841 {
842         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
843                                     ext4_nfs_get_inode);
844 }
845
846 #ifdef CONFIG_QUOTA
847 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
848 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
849
850 static int ext4_dquot_initialize(struct inode *inode, int type);
851 static int ext4_dquot_drop(struct inode *inode);
852 static int ext4_write_dquot(struct dquot *dquot);
853 static int ext4_acquire_dquot(struct dquot *dquot);
854 static int ext4_release_dquot(struct dquot *dquot);
855 static int ext4_mark_dquot_dirty(struct dquot *dquot);
856 static int ext4_write_info(struct super_block *sb, int type);
857 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
858                                 char *path, int remount);
859 static int ext4_quota_on_mount(struct super_block *sb, int type);
860 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
861                                size_t len, loff_t off);
862 static ssize_t ext4_quota_write(struct super_block *sb, int type,
863                                 const char *data, size_t len, loff_t off);
864
865 static struct dquot_operations ext4_quota_operations = {
866         .initialize     = ext4_dquot_initialize,
867         .drop           = ext4_dquot_drop,
868         .alloc_space    = dquot_alloc_space,
869         .alloc_inode    = dquot_alloc_inode,
870         .free_space     = dquot_free_space,
871         .free_inode     = dquot_free_inode,
872         .transfer       = dquot_transfer,
873         .write_dquot    = ext4_write_dquot,
874         .acquire_dquot  = ext4_acquire_dquot,
875         .release_dquot  = ext4_release_dquot,
876         .mark_dirty     = ext4_mark_dquot_dirty,
877         .write_info     = ext4_write_info
878 };
879
880 static struct quotactl_ops ext4_qctl_operations = {
881         .quota_on       = ext4_quota_on,
882         .quota_off      = vfs_quota_off,
883         .quota_sync     = vfs_quota_sync,
884         .get_info       = vfs_get_dqinfo,
885         .set_info       = vfs_set_dqinfo,
886         .get_dqblk      = vfs_get_dqblk,
887         .set_dqblk      = vfs_set_dqblk
888 };
889 #endif
890
891 static const struct super_operations ext4_sops = {
892         .alloc_inode    = ext4_alloc_inode,
893         .destroy_inode  = ext4_destroy_inode,
894         .write_inode    = ext4_write_inode,
895         .dirty_inode    = ext4_dirty_inode,
896         .delete_inode   = ext4_delete_inode,
897         .put_super      = ext4_put_super,
898         .write_super    = ext4_write_super,
899         .sync_fs        = ext4_sync_fs,
900         .write_super_lockfs = ext4_write_super_lockfs,
901         .unlockfs       = ext4_unlockfs,
902         .statfs         = ext4_statfs,
903         .remount_fs     = ext4_remount,
904         .clear_inode    = ext4_clear_inode,
905         .show_options   = ext4_show_options,
906 #ifdef CONFIG_QUOTA
907         .quota_read     = ext4_quota_read,
908         .quota_write    = ext4_quota_write,
909 #endif
910 };
911
912 static const struct export_operations ext4_export_ops = {
913         .fh_to_dentry = ext4_fh_to_dentry,
914         .fh_to_parent = ext4_fh_to_parent,
915         .get_parent = ext4_get_parent,
916 };
917
918 enum {
919         Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
920         Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
921         Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
922         Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
923         Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
924         Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
925         Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
926         Opt_journal_checksum, Opt_journal_async_commit,
927         Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
928         Opt_data_err_abort, Opt_data_err_ignore,
929         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
930         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
931         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
932         Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
933         Opt_stripe, Opt_delalloc, Opt_nodelalloc,
934         Opt_inode_readahead_blks
935 };
936
937 static const match_table_t tokens = {
938         {Opt_bsd_df, "bsddf"},
939         {Opt_minix_df, "minixdf"},
940         {Opt_grpid, "grpid"},
941         {Opt_grpid, "bsdgroups"},
942         {Opt_nogrpid, "nogrpid"},
943         {Opt_nogrpid, "sysvgroups"},
944         {Opt_resgid, "resgid=%u"},
945         {Opt_resuid, "resuid=%u"},
946         {Opt_sb, "sb=%u"},
947         {Opt_err_cont, "errors=continue"},
948         {Opt_err_panic, "errors=panic"},
949         {Opt_err_ro, "errors=remount-ro"},
950         {Opt_nouid32, "nouid32"},
951         {Opt_debug, "debug"},
952         {Opt_oldalloc, "oldalloc"},
953         {Opt_orlov, "orlov"},
954         {Opt_user_xattr, "user_xattr"},
955         {Opt_nouser_xattr, "nouser_xattr"},
956         {Opt_acl, "acl"},
957         {Opt_noacl, "noacl"},
958         {Opt_reservation, "reservation"},
959         {Opt_noreservation, "noreservation"},
960         {Opt_noload, "noload"},
961         {Opt_nobh, "nobh"},
962         {Opt_bh, "bh"},
963         {Opt_commit, "commit=%u"},
964         {Opt_min_batch_time, "min_batch_time=%u"},
965         {Opt_max_batch_time, "max_batch_time=%u"},
966         {Opt_journal_update, "journal=update"},
967         {Opt_journal_inum, "journal=%u"},
968         {Opt_journal_dev, "journal_dev=%u"},
969         {Opt_journal_checksum, "journal_checksum"},
970         {Opt_journal_async_commit, "journal_async_commit"},
971         {Opt_abort, "abort"},
972         {Opt_data_journal, "data=journal"},
973         {Opt_data_ordered, "data=ordered"},
974         {Opt_data_writeback, "data=writeback"},
975         {Opt_data_err_abort, "data_err=abort"},
976         {Opt_data_err_ignore, "data_err=ignore"},
977         {Opt_offusrjquota, "usrjquota="},
978         {Opt_usrjquota, "usrjquota=%s"},
979         {Opt_offgrpjquota, "grpjquota="},
980         {Opt_grpjquota, "grpjquota=%s"},
981         {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
982         {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
983         {Opt_grpquota, "grpquota"},
984         {Opt_noquota, "noquota"},
985         {Opt_quota, "quota"},
986         {Opt_usrquota, "usrquota"},
987         {Opt_barrier, "barrier=%u"},
988         {Opt_extents, "extents"},
989         {Opt_noextents, "noextents"},
990         {Opt_i_version, "i_version"},
991         {Opt_stripe, "stripe=%u"},
992         {Opt_resize, "resize"},
993         {Opt_delalloc, "delalloc"},
994         {Opt_nodelalloc, "nodelalloc"},
995         {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
996         {Opt_err, NULL},
997 };
998
999 static ext4_fsblk_t get_sb_block(void **data)
1000 {
1001         ext4_fsblk_t    sb_block;
1002         char            *options = (char *) *data;
1003
1004         if (!options || strncmp(options, "sb=", 3) != 0)
1005                 return 1;       /* Default location */
1006         options += 3;
1007         /*todo: use simple_strtoll with >32bit ext4 */
1008         sb_block = simple_strtoul(options, &options, 0);
1009         if (*options && *options != ',') {
1010                 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1011                        (char *) *data);
1012                 return 1;
1013         }
1014         if (*options == ',')
1015                 options++;
1016         *data = (void *) options;
1017         return sb_block;
1018 }
1019
1020 static int parse_options(char *options, struct super_block *sb,
1021                          unsigned int *inum, unsigned long *journal_devnum,
1022                          ext4_fsblk_t *n_blocks_count, int is_remount)
1023 {
1024         struct ext4_sb_info *sbi = EXT4_SB(sb);
1025         char *p;
1026         substring_t args[MAX_OPT_ARGS];
1027         int data_opt = 0;
1028         int option;
1029 #ifdef CONFIG_QUOTA
1030         int qtype, qfmt;
1031         char *qname;
1032 #endif
1033         ext4_fsblk_t last_block;
1034
1035         if (!options)
1036                 return 1;
1037
1038         while ((p = strsep(&options, ",")) != NULL) {
1039                 int token;
1040                 if (!*p)
1041                         continue;
1042
1043                 token = match_token(p, tokens, args);
1044                 switch (token) {
1045                 case Opt_bsd_df:
1046                         clear_opt(sbi->s_mount_opt, MINIX_DF);
1047                         break;
1048                 case Opt_minix_df:
1049                         set_opt(sbi->s_mount_opt, MINIX_DF);
1050                         break;
1051                 case Opt_grpid:
1052                         set_opt(sbi->s_mount_opt, GRPID);
1053                         break;
1054                 case Opt_nogrpid:
1055                         clear_opt(sbi->s_mount_opt, GRPID);
1056                         break;
1057                 case Opt_resuid:
1058                         if (match_int(&args[0], &option))
1059                                 return 0;
1060                         sbi->s_resuid = option;
1061                         break;
1062                 case Opt_resgid:
1063                         if (match_int(&args[0], &option))
1064                                 return 0;
1065                         sbi->s_resgid = option;
1066                         break;
1067                 case Opt_sb:
1068                         /* handled by get_sb_block() instead of here */
1069                         /* *sb_block = match_int(&args[0]); */
1070                         break;
1071                 case Opt_err_panic:
1072                         clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1073                         clear_opt(sbi->s_mount_opt, ERRORS_RO);
1074                         set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1075                         break;
1076                 case Opt_err_ro:
1077                         clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1078                         clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1079                         set_opt(sbi->s_mount_opt, ERRORS_RO);
1080                         break;
1081                 case Opt_err_cont:
1082                         clear_opt(sbi->s_mount_opt, ERRORS_RO);
1083                         clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1084                         set_opt(sbi->s_mount_opt, ERRORS_CONT);
1085                         break;
1086                 case Opt_nouid32:
1087                         set_opt(sbi->s_mount_opt, NO_UID32);
1088                         break;
1089                 case Opt_debug:
1090                         set_opt(sbi->s_mount_opt, DEBUG);
1091                         break;
1092                 case Opt_oldalloc:
1093                         set_opt(sbi->s_mount_opt, OLDALLOC);
1094                         break;
1095                 case Opt_orlov:
1096                         clear_opt(sbi->s_mount_opt, OLDALLOC);
1097                         break;
1098 #ifdef CONFIG_EXT4_FS_XATTR
1099                 case Opt_user_xattr:
1100                         set_opt(sbi->s_mount_opt, XATTR_USER);
1101                         break;
1102                 case Opt_nouser_xattr:
1103                         clear_opt(sbi->s_mount_opt, XATTR_USER);
1104                         break;
1105 #else
1106                 case Opt_user_xattr:
1107                 case Opt_nouser_xattr:
1108                         printk(KERN_ERR "EXT4 (no)user_xattr options "
1109                                "not supported\n");
1110                         break;
1111 #endif
1112 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1113                 case Opt_acl:
1114                         set_opt(sbi->s_mount_opt, POSIX_ACL);
1115                         break;
1116                 case Opt_noacl:
1117                         clear_opt(sbi->s_mount_opt, POSIX_ACL);
1118                         break;
1119 #else
1120                 case Opt_acl:
1121                 case Opt_noacl:
1122                         printk(KERN_ERR "EXT4 (no)acl options "
1123                                "not supported\n");
1124                         break;
1125 #endif
1126                 case Opt_reservation:
1127                         set_opt(sbi->s_mount_opt, RESERVATION);
1128                         break;
1129                 case Opt_noreservation:
1130                         clear_opt(sbi->s_mount_opt, RESERVATION);
1131                         break;
1132                 case Opt_journal_update:
1133                         /* @@@ FIXME */
1134                         /* Eventually we will want to be able to create
1135                            a journal file here.  For now, only allow the
1136                            user to specify an existing inode to be the
1137                            journal file. */
1138                         if (is_remount) {
1139                                 printk(KERN_ERR "EXT4-fs: cannot specify "
1140                                        "journal on remount\n");
1141                                 return 0;
1142                         }
1143                         set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1144                         break;
1145                 case Opt_journal_inum:
1146                         if (is_remount) {
1147                                 printk(KERN_ERR "EXT4-fs: cannot specify "
1148                                        "journal on remount\n");
1149                                 return 0;
1150                         }
1151                         if (match_int(&args[0], &option))
1152                                 return 0;
1153                         *inum = option;
1154                         break;
1155                 case Opt_journal_dev:
1156                         if (is_remount) {
1157                                 printk(KERN_ERR "EXT4-fs: cannot specify "
1158                                        "journal on remount\n");
1159                                 return 0;
1160                         }
1161                         if (match_int(&args[0], &option))
1162                                 return 0;
1163                         *journal_devnum = option;
1164                         break;
1165                 case Opt_journal_checksum:
1166                         set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1167                         break;
1168                 case Opt_journal_async_commit:
1169                         set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1170                         set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1171                         break;
1172                 case Opt_noload:
1173                         set_opt(sbi->s_mount_opt, NOLOAD);
1174                         break;
1175                 case Opt_commit:
1176                         if (match_int(&args[0], &option))
1177                                 return 0;
1178                         if (option < 0)
1179                                 return 0;
1180                         if (option == 0)
1181                                 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1182                         sbi->s_commit_interval = HZ * option;
1183                         break;
1184                 case Opt_max_batch_time:
1185                         if (match_int(&args[0], &option))
1186                                 return 0;
1187                         if (option < 0)
1188                                 return 0;
1189                         if (option == 0)
1190                                 option = EXT4_DEF_MAX_BATCH_TIME;
1191                         sbi->s_max_batch_time = option;
1192                         break;
1193                 case Opt_min_batch_time:
1194                         if (match_int(&args[0], &option))
1195                                 return 0;
1196                         if (option < 0)
1197                                 return 0;
1198                         sbi->s_min_batch_time = option;
1199                         break;
1200                 case Opt_data_journal:
1201                         data_opt = EXT4_MOUNT_JOURNAL_DATA;
1202                         goto datacheck;
1203                 case Opt_data_ordered:
1204                         data_opt = EXT4_MOUNT_ORDERED_DATA;
1205                         goto datacheck;
1206                 case Opt_data_writeback:
1207                         data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1208                 datacheck:
1209                         if (is_remount) {
1210                                 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1211                                                 != data_opt) {
1212                                         printk(KERN_ERR
1213                                                 "EXT4-fs: cannot change data "
1214                                                 "mode on remount\n");
1215                                         return 0;
1216                                 }
1217                         } else {
1218                                 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1219                                 sbi->s_mount_opt |= data_opt;
1220                         }
1221                         break;
1222                 case Opt_data_err_abort:
1223                         set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1224                         break;
1225                 case Opt_data_err_ignore:
1226                         clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1227                         break;
1228 #ifdef CONFIG_QUOTA
1229                 case Opt_usrjquota:
1230                         qtype = USRQUOTA;
1231                         goto set_qf_name;
1232                 case Opt_grpjquota:
1233                         qtype = GRPQUOTA;
1234 set_qf_name:
1235                         if ((sb_any_quota_enabled(sb) ||
1236                              sb_any_quota_suspended(sb)) &&
1237                             !sbi->s_qf_names[qtype]) {
1238                                 printk(KERN_ERR
1239                                        "EXT4-fs: Cannot change journaled "
1240                                        "quota options when quota turned on.\n");
1241                                 return 0;
1242                         }
1243                         qname = match_strdup(&args[0]);
1244                         if (!qname) {
1245                                 printk(KERN_ERR
1246                                         "EXT4-fs: not enough memory for "
1247                                         "storing quotafile name.\n");
1248                                 return 0;
1249                         }
1250                         if (sbi->s_qf_names[qtype] &&
1251                             strcmp(sbi->s_qf_names[qtype], qname)) {
1252                                 printk(KERN_ERR
1253                                         "EXT4-fs: %s quota file already "
1254                                         "specified.\n", QTYPE2NAME(qtype));
1255                                 kfree(qname);
1256                                 return 0;
1257                         }
1258                         sbi->s_qf_names[qtype] = qname;
1259                         if (strchr(sbi->s_qf_names[qtype], '/')) {
1260                                 printk(KERN_ERR
1261                                         "EXT4-fs: quotafile must be on "
1262                                         "filesystem root.\n");
1263                                 kfree(sbi->s_qf_names[qtype]);
1264                                 sbi->s_qf_names[qtype] = NULL;
1265                                 return 0;
1266                         }
1267                         set_opt(sbi->s_mount_opt, QUOTA);
1268                         break;
1269                 case Opt_offusrjquota:
1270                         qtype = USRQUOTA;
1271                         goto clear_qf_name;
1272                 case Opt_offgrpjquota:
1273                         qtype = GRPQUOTA;
1274 clear_qf_name:
1275                         if ((sb_any_quota_enabled(sb) ||
1276                              sb_any_quota_suspended(sb)) &&
1277                             sbi->s_qf_names[qtype]) {
1278                                 printk(KERN_ERR "EXT4-fs: Cannot change "
1279                                         "journaled quota options when "
1280                                         "quota turned on.\n");
1281                                 return 0;
1282                         }
1283                         /*
1284                          * The space will be released later when all options
1285                          * are confirmed to be correct
1286                          */
1287                         sbi->s_qf_names[qtype] = NULL;
1288                         break;
1289                 case Opt_jqfmt_vfsold:
1290                         qfmt = QFMT_VFS_OLD;
1291                         goto set_qf_format;
1292                 case Opt_jqfmt_vfsv0:
1293                         qfmt = QFMT_VFS_V0;
1294 set_qf_format:
1295                         if ((sb_any_quota_enabled(sb) ||
1296                              sb_any_quota_suspended(sb)) &&
1297                             sbi->s_jquota_fmt != qfmt) {
1298                                 printk(KERN_ERR "EXT4-fs: Cannot change "
1299                                         "journaled quota options when "
1300                                         "quota turned on.\n");
1301                                 return 0;
1302                         }
1303                         sbi->s_jquota_fmt = qfmt;
1304                         break;
1305                 case Opt_quota:
1306                 case Opt_usrquota:
1307                         set_opt(sbi->s_mount_opt, QUOTA);
1308                         set_opt(sbi->s_mount_opt, USRQUOTA);
1309                         break;
1310                 case Opt_grpquota:
1311                         set_opt(sbi->s_mount_opt, QUOTA);
1312                         set_opt(sbi->s_mount_opt, GRPQUOTA);
1313                         break;
1314                 case Opt_noquota:
1315                         if (sb_any_quota_enabled(sb)) {
1316                                 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1317                                         "options when quota turned on.\n");
1318                                 return 0;
1319                         }
1320                         clear_opt(sbi->s_mount_opt, QUOTA);
1321                         clear_opt(sbi->s_mount_opt, USRQUOTA);
1322                         clear_opt(sbi->s_mount_opt, GRPQUOTA);
1323                         break;
1324 #else
1325                 case Opt_quota:
1326                 case Opt_usrquota:
1327                 case Opt_grpquota:
1328                         printk(KERN_ERR
1329                                 "EXT4-fs: quota options not supported.\n");
1330                         break;
1331                 case Opt_usrjquota:
1332                 case Opt_grpjquota:
1333                 case Opt_offusrjquota:
1334                 case Opt_offgrpjquota:
1335                 case Opt_jqfmt_vfsold:
1336                 case Opt_jqfmt_vfsv0:
1337                         printk(KERN_ERR
1338                                 "EXT4-fs: journaled quota options not "
1339                                 "supported.\n");
1340                         break;
1341                 case Opt_noquota:
1342                         break;
1343 #endif
1344                 case Opt_abort:
1345                         set_opt(sbi->s_mount_opt, ABORT);
1346                         break;
1347                 case Opt_barrier:
1348                         if (match_int(&args[0], &option))
1349                                 return 0;
1350                         if (option)
1351                                 set_opt(sbi->s_mount_opt, BARRIER);
1352                         else
1353                                 clear_opt(sbi->s_mount_opt, BARRIER);
1354                         break;
1355                 case Opt_ignore:
1356                         break;
1357                 case Opt_resize:
1358                         if (!is_remount) {
1359                                 printk("EXT4-fs: resize option only available "
1360                                         "for remount\n");
1361                                 return 0;
1362                         }
1363                         if (match_int(&args[0], &option) != 0)
1364                                 return 0;
1365                         *n_blocks_count = option;
1366                         break;
1367                 case Opt_nobh:
1368                         set_opt(sbi->s_mount_opt, NOBH);
1369                         break;
1370                 case Opt_bh:
1371                         clear_opt(sbi->s_mount_opt, NOBH);
1372                         break;
1373                 case Opt_extents:
1374                         if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1375                                         EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1376                                 ext4_warning(sb, __func__,
1377                                         "extents feature not enabled "
1378                                         "on this filesystem, use tune2fs");
1379                                 return 0;
1380                         }
1381                         set_opt(sbi->s_mount_opt, EXTENTS);
1382                         break;
1383                 case Opt_noextents:
1384                         /*
1385                          * When e2fsprogs support resizing an already existing
1386                          * ext3 file system to greater than 2**32 we need to
1387                          * add support to block allocator to handle growing
1388                          * already existing block  mapped inode so that blocks
1389                          * allocated for them fall within 2**32
1390                          */
1391                         last_block = ext4_blocks_count(sbi->s_es) - 1;
1392                         if (last_block  > 0xffffffffULL) {
1393                                 printk(KERN_ERR "EXT4-fs: Filesystem too "
1394                                                 "large to mount with "
1395                                                 "-o noextents options\n");
1396                                 return 0;
1397                         }
1398                         clear_opt(sbi->s_mount_opt, EXTENTS);
1399                         break;
1400                 case Opt_i_version:
1401                         set_opt(sbi->s_mount_opt, I_VERSION);
1402                         sb->s_flags |= MS_I_VERSION;
1403                         break;
1404                 case Opt_nodelalloc:
1405                         clear_opt(sbi->s_mount_opt, DELALLOC);
1406                         break;
1407                 case Opt_stripe:
1408                         if (match_int(&args[0], &option))
1409                                 return 0;
1410                         if (option < 0)
1411                                 return 0;
1412                         sbi->s_stripe = option;
1413                         break;
1414                 case Opt_delalloc:
1415                         set_opt(sbi->s_mount_opt, DELALLOC);
1416                         break;
1417                 case Opt_inode_readahead_blks:
1418                         if (match_int(&args[0], &option))
1419                                 return 0;
1420                         if (option < 0 || option > (1 << 30))
1421                                 return 0;
1422                         sbi->s_inode_readahead_blks = option;
1423                         break;
1424                 default:
1425                         printk(KERN_ERR
1426                                "EXT4-fs: Unrecognized mount option \"%s\" "
1427                                "or missing value\n", p);
1428                         return 0;
1429                 }
1430         }
1431 #ifdef CONFIG_QUOTA
1432         if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1433                 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1434                      sbi->s_qf_names[USRQUOTA])
1435                         clear_opt(sbi->s_mount_opt, USRQUOTA);
1436
1437                 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1438                      sbi->s_qf_names[GRPQUOTA])
1439                         clear_opt(sbi->s_mount_opt, GRPQUOTA);
1440
1441                 if ((sbi->s_qf_names[USRQUOTA] &&
1442                                 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1443                     (sbi->s_qf_names[GRPQUOTA] &&
1444                                 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1445                         printk(KERN_ERR "EXT4-fs: old and new quota "
1446                                         "format mixing.\n");
1447                         return 0;
1448                 }
1449
1450                 if (!sbi->s_jquota_fmt) {
1451                         printk(KERN_ERR "EXT4-fs: journaled quota format "
1452                                         "not specified.\n");
1453                         return 0;
1454                 }
1455         } else {
1456                 if (sbi->s_jquota_fmt) {
1457                         printk(KERN_ERR "EXT4-fs: journaled quota format "
1458                                         "specified with no journaling "
1459                                         "enabled.\n");
1460                         return 0;
1461                 }
1462         }
1463 #endif
1464         return 1;
1465 }
1466
1467 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1468                             int read_only)
1469 {
1470         struct ext4_sb_info *sbi = EXT4_SB(sb);
1471         int res = 0;
1472
1473         if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1474                 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1475                        "forcing read-only mode\n");
1476                 res = MS_RDONLY;
1477         }
1478         if (read_only)
1479                 return res;
1480         if (!(sbi->s_mount_state & EXT4_VALID_FS))
1481                 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1482                        "running e2fsck is recommended\n");
1483         else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1484                 printk(KERN_WARNING
1485                        "EXT4-fs warning: mounting fs with errors, "
1486                        "running e2fsck is recommended\n");
1487         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1488                  le16_to_cpu(es->s_mnt_count) >=
1489                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1490                 printk(KERN_WARNING
1491                        "EXT4-fs warning: maximal mount count reached, "
1492                        "running e2fsck is recommended\n");
1493         else if (le32_to_cpu(es->s_checkinterval) &&
1494                 (le32_to_cpu(es->s_lastcheck) +
1495                         le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1496                 printk(KERN_WARNING
1497                        "EXT4-fs warning: checktime reached, "
1498                        "running e2fsck is recommended\n");
1499         if (!sbi->s_journal) 
1500                 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1501         if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1502                 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1503         le16_add_cpu(&es->s_mnt_count, 1);
1504         es->s_mtime = cpu_to_le32(get_seconds());
1505         ext4_update_dynamic_rev(sb);
1506         if (sbi->s_journal)
1507                 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1508
1509         ext4_commit_super(sb, es, 1);
1510         if (test_opt(sb, DEBUG))
1511                 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1512                                 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1513                         sb->s_blocksize,
1514                         sbi->s_groups_count,
1515                         EXT4_BLOCKS_PER_GROUP(sb),
1516                         EXT4_INODES_PER_GROUP(sb),
1517                         sbi->s_mount_opt);
1518
1519         if (EXT4_SB(sb)->s_journal) {
1520                 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1521                        sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1522                        "external", EXT4_SB(sb)->s_journal->j_devname);
1523         } else {
1524                 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1525         }
1526         return res;
1527 }
1528
1529 static int ext4_fill_flex_info(struct super_block *sb)
1530 {
1531         struct ext4_sb_info *sbi = EXT4_SB(sb);
1532         struct ext4_group_desc *gdp = NULL;
1533         struct buffer_head *bh;
1534         ext4_group_t flex_group_count;
1535         ext4_group_t flex_group;
1536         int groups_per_flex = 0;
1537         int i;
1538
1539         if (!sbi->s_es->s_log_groups_per_flex) {
1540                 sbi->s_log_groups_per_flex = 0;
1541                 return 1;
1542         }
1543
1544         sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1545         groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1546
1547         /* We allocate both existing and potentially added groups */
1548         flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1549                         ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1550                               EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1551         sbi->s_flex_groups = kzalloc(flex_group_count *
1552                                      sizeof(struct flex_groups), GFP_KERNEL);
1553         if (sbi->s_flex_groups == NULL) {
1554                 printk(KERN_ERR "EXT4-fs: not enough memory for "
1555                                 "%u flex groups\n", flex_group_count);
1556                 goto failed;
1557         }
1558
1559         for (i = 0; i < sbi->s_groups_count; i++) {
1560                 gdp = ext4_get_group_desc(sb, i, &bh);
1561
1562                 flex_group = ext4_flex_group(sbi, i);
1563                 sbi->s_flex_groups[flex_group].free_inodes +=
1564                         le16_to_cpu(gdp->bg_free_inodes_count);
1565                 sbi->s_flex_groups[flex_group].free_blocks +=
1566                         le16_to_cpu(gdp->bg_free_blocks_count);
1567         }
1568
1569         return 1;
1570 failed:
1571         return 0;
1572 }
1573
1574 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1575                             struct ext4_group_desc *gdp)
1576 {
1577         __u16 crc = 0;
1578
1579         if (sbi->s_es->s_feature_ro_compat &
1580             cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1581                 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1582                 __le32 le_group = cpu_to_le32(block_group);
1583
1584                 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1585                 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1586                 crc = crc16(crc, (__u8 *)gdp, offset);
1587                 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1588                 /* for checksum of struct ext4_group_desc do the rest...*/
1589                 if ((sbi->s_es->s_feature_incompat &
1590                      cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1591                     offset < le16_to_cpu(sbi->s_es->s_desc_size))
1592                         crc = crc16(crc, (__u8 *)gdp + offset,
1593                                     le16_to_cpu(sbi->s_es->s_desc_size) -
1594                                         offset);
1595         }
1596
1597         return cpu_to_le16(crc);
1598 }
1599
1600 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1601                                 struct ext4_group_desc *gdp)
1602 {
1603         if ((sbi->s_es->s_feature_ro_compat &
1604              cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1605             (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1606                 return 0;
1607
1608         return 1;
1609 }
1610
1611 /* Called at mount-time, super-block is locked */
1612 static int ext4_check_descriptors(struct super_block *sb)
1613 {
1614         struct ext4_sb_info *sbi = EXT4_SB(sb);
1615         ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1616         ext4_fsblk_t last_block;
1617         ext4_fsblk_t block_bitmap;
1618         ext4_fsblk_t inode_bitmap;
1619         ext4_fsblk_t inode_table;
1620         int flexbg_flag = 0;
1621         ext4_group_t i;
1622
1623         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1624                 flexbg_flag = 1;
1625
1626         ext4_debug("Checking group descriptors");
1627
1628         for (i = 0; i < sbi->s_groups_count; i++) {
1629                 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1630
1631                 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1632                         last_block = ext4_blocks_count(sbi->s_es) - 1;
1633                 else
1634                         last_block = first_block +
1635                                 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1636
1637                 block_bitmap = ext4_block_bitmap(sb, gdp);
1638                 if (block_bitmap < first_block || block_bitmap > last_block) {
1639                         printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1640                                "Block bitmap for group %u not in group "
1641                                "(block %llu)!\n", i, block_bitmap);
1642                         return 0;
1643                 }
1644                 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1645                 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1646                         printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1647                                "Inode bitmap for group %u not in group "
1648                                "(block %llu)!\n", i, inode_bitmap);
1649                         return 0;
1650                 }
1651                 inode_table = ext4_inode_table(sb, gdp);
1652                 if (inode_table < first_block ||
1653                     inode_table + sbi->s_itb_per_group - 1 > last_block) {
1654                         printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1655                                "Inode table for group %u not in group "
1656                                "(block %llu)!\n", i, inode_table);
1657                         return 0;
1658                 }
1659                 spin_lock(sb_bgl_lock(sbi, i));
1660                 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1661                         printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1662                                "Checksum for group %u failed (%u!=%u)\n",
1663                                i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1664                                gdp)), le16_to_cpu(gdp->bg_checksum));
1665                         if (!(sb->s_flags & MS_RDONLY)) {
1666                                 spin_unlock(sb_bgl_lock(sbi, i));
1667                                 return 0;
1668                         }
1669                 }
1670                 spin_unlock(sb_bgl_lock(sbi, i));
1671                 if (!flexbg_flag)
1672                         first_block += EXT4_BLOCKS_PER_GROUP(sb);
1673         }
1674
1675         ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1676         sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1677         return 1;
1678 }
1679
1680 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1681  * the superblock) which were deleted from all directories, but held open by
1682  * a process at the time of a crash.  We walk the list and try to delete these
1683  * inodes at recovery time (only with a read-write filesystem).
1684  *
1685  * In order to keep the orphan inode chain consistent during traversal (in
1686  * case of crash during recovery), we link each inode into the superblock
1687  * orphan list_head and handle it the same way as an inode deletion during
1688  * normal operation (which journals the operations for us).
1689  *
1690  * We only do an iget() and an iput() on each inode, which is very safe if we
1691  * accidentally point at an in-use or already deleted inode.  The worst that
1692  * can happen in this case is that we get a "bit already cleared" message from
1693  * ext4_free_inode().  The only reason we would point at a wrong inode is if
1694  * e2fsck was run on this filesystem, and it must have already done the orphan
1695  * inode cleanup for us, so we can safely abort without any further action.
1696  */
1697 static void ext4_orphan_cleanup(struct super_block *sb,
1698                                 struct ext4_super_block *es)
1699 {
1700         unsigned int s_flags = sb->s_flags;
1701         int nr_orphans = 0, nr_truncates = 0;
1702 #ifdef CONFIG_QUOTA
1703         int i;
1704 #endif
1705         if (!es->s_last_orphan) {
1706                 jbd_debug(4, "no orphan inodes to clean up\n");
1707                 return;
1708         }
1709
1710         if (bdev_read_only(sb->s_bdev)) {
1711                 printk(KERN_ERR "EXT4-fs: write access "
1712                         "unavailable, skipping orphan cleanup.\n");
1713                 return;
1714         }
1715
1716         if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1717                 if (es->s_last_orphan)
1718                         jbd_debug(1, "Errors on filesystem, "
1719                                   "clearing orphan list.\n");
1720                 es->s_last_orphan = 0;
1721                 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1722                 return;
1723         }
1724
1725         if (s_flags & MS_RDONLY) {
1726                 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1727                        sb->s_id);
1728                 sb->s_flags &= ~MS_RDONLY;
1729         }
1730 #ifdef CONFIG_QUOTA
1731         /* Needed for iput() to work correctly and not trash data */
1732         sb->s_flags |= MS_ACTIVE;
1733         /* Turn on quotas so that they are updated correctly */
1734         for (i = 0; i < MAXQUOTAS; i++) {
1735                 if (EXT4_SB(sb)->s_qf_names[i]) {
1736                         int ret = ext4_quota_on_mount(sb, i);
1737                         if (ret < 0)
1738                                 printk(KERN_ERR
1739                                         "EXT4-fs: Cannot turn on journaled "
1740                                         "quota: error %d\n", ret);
1741                 }
1742         }
1743 #endif
1744
1745         while (es->s_last_orphan) {
1746                 struct inode *inode;
1747
1748                 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1749                 if (IS_ERR(inode)) {
1750                         es->s_last_orphan = 0;
1751                         break;
1752                 }
1753
1754                 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1755                 DQUOT_INIT(inode);
1756                 if (inode->i_nlink) {
1757                         printk(KERN_DEBUG
1758                                 "%s: truncating inode %lu to %lld bytes\n",
1759                                 __func__, inode->i_ino, inode->i_size);
1760                         jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1761                                   inode->i_ino, inode->i_size);
1762                         ext4_truncate(inode);
1763                         nr_truncates++;
1764                 } else {
1765                         printk(KERN_DEBUG
1766                                 "%s: deleting unreferenced inode %lu\n",
1767                                 __func__, inode->i_ino);
1768                         jbd_debug(2, "deleting unreferenced inode %lu\n",
1769                                   inode->i_ino);
1770                         nr_orphans++;
1771                 }
1772                 iput(inode);  /* The delete magic happens here! */
1773         }
1774
1775 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1776
1777         if (nr_orphans)
1778                 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1779                        sb->s_id, PLURAL(nr_orphans));
1780         if (nr_truncates)
1781                 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1782                        sb->s_id, PLURAL(nr_truncates));
1783 #ifdef CONFIG_QUOTA
1784         /* Turn quotas off */
1785         for (i = 0; i < MAXQUOTAS; i++) {
1786                 if (sb_dqopt(sb)->files[i])
1787                         vfs_quota_off(sb, i, 0);
1788         }
1789 #endif
1790         sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1791 }
1792 /*
1793  * Maximal extent format file size.
1794  * Resulting logical blkno at s_maxbytes must fit in our on-disk
1795  * extent format containers, within a sector_t, and within i_blocks
1796  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
1797  * so that won't be a limiting factor.
1798  *
1799  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1800  */
1801 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1802 {
1803         loff_t res;
1804         loff_t upper_limit = MAX_LFS_FILESIZE;
1805
1806         /* small i_blocks in vfs inode? */
1807         if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1808                 /*
1809                  * CONFIG_LBD is not enabled implies the inode
1810                  * i_block represent total blocks in 512 bytes
1811                  * 32 == size of vfs inode i_blocks * 8
1812                  */
1813                 upper_limit = (1LL << 32) - 1;
1814
1815                 /* total blocks in file system block size */
1816                 upper_limit >>= (blkbits - 9);
1817                 upper_limit <<= blkbits;
1818         }
1819
1820         /* 32-bit extent-start container, ee_block */
1821         res = 1LL << 32;
1822         res <<= blkbits;
1823         res -= 1;
1824
1825         /* Sanity check against vm- & vfs- imposed limits */
1826         if (res > upper_limit)
1827                 res = upper_limit;
1828
1829         return res;
1830 }
1831
1832 /*
1833  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
1834  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1835  * We need to be 1 filesystem block less than the 2^48 sector limit.
1836  */
1837 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1838 {
1839         loff_t res = EXT4_NDIR_BLOCKS;
1840         int meta_blocks;
1841         loff_t upper_limit;
1842         /* This is calculated to be the largest file size for a
1843          * dense, bitmapped file such that the total number of
1844          * sectors in the file, including data and all indirect blocks,
1845          * does not exceed 2^48 -1
1846          * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1847          * total number of  512 bytes blocks of the file
1848          */
1849
1850         if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1851                 /*
1852                  * !has_huge_files or CONFIG_LBD is not enabled
1853                  * implies the inode i_block represent total blocks in
1854                  * 512 bytes 32 == size of vfs inode i_blocks * 8
1855                  */
1856                 upper_limit = (1LL << 32) - 1;
1857
1858                 /* total blocks in file system block size */
1859                 upper_limit >>= (bits - 9);
1860
1861         } else {
1862                 /*
1863                  * We use 48 bit ext4_inode i_blocks
1864                  * With EXT4_HUGE_FILE_FL set the i_blocks
1865                  * represent total number of blocks in
1866                  * file system block size
1867                  */
1868                 upper_limit = (1LL << 48) - 1;
1869
1870         }
1871
1872         /* indirect blocks */
1873         meta_blocks = 1;
1874         /* double indirect blocks */
1875         meta_blocks += 1 + (1LL << (bits-2));
1876         /* tripple indirect blocks */
1877         meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1878
1879         upper_limit -= meta_blocks;
1880         upper_limit <<= bits;
1881
1882         res += 1LL << (bits-2);
1883         res += 1LL << (2*(bits-2));
1884         res += 1LL << (3*(bits-2));
1885         res <<= bits;
1886         if (res > upper_limit)
1887                 res = upper_limit;
1888
1889         if (res > MAX_LFS_FILESIZE)
1890                 res = MAX_LFS_FILESIZE;
1891
1892         return res;
1893 }
1894
1895 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1896                                 ext4_fsblk_t logical_sb_block, int nr)
1897 {
1898         struct ext4_sb_info *sbi = EXT4_SB(sb);
1899         ext4_group_t bg, first_meta_bg;
1900         int has_super = 0;
1901
1902         first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1903
1904         if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1905             nr < first_meta_bg)
1906                 return logical_sb_block + nr + 1;
1907         bg = sbi->s_desc_per_block * nr;
1908         if (ext4_bg_has_super(sb, bg))
1909                 has_super = 1;
1910         return (has_super + ext4_group_first_block_no(sb, bg));
1911 }
1912
1913 /**
1914  * ext4_get_stripe_size: Get the stripe size.
1915  * @sbi: In memory super block info
1916  *
1917  * If we have specified it via mount option, then
1918  * use the mount option value. If the value specified at mount time is
1919  * greater than the blocks per group use the super block value.
1920  * If the super block value is greater than blocks per group return 0.
1921  * Allocator needs it be less than blocks per group.
1922  *
1923  */
1924 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1925 {
1926         unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1927         unsigned long stripe_width =
1928                         le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1929
1930         if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1931                 return sbi->s_stripe;
1932
1933         if (stripe_width <= sbi->s_blocks_per_group)
1934                 return stripe_width;
1935
1936         if (stride <= sbi->s_blocks_per_group)
1937                 return stride;
1938
1939         return 0;
1940 }
1941
1942 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
1943                                 __releases(kernel_lock)
1944                                 __acquires(kernel_lock)
1945
1946 {
1947         struct buffer_head *bh;
1948         struct ext4_super_block *es = NULL;
1949         struct ext4_sb_info *sbi;
1950         ext4_fsblk_t block;
1951         ext4_fsblk_t sb_block = get_sb_block(&data);
1952         ext4_fsblk_t logical_sb_block;
1953         unsigned long offset = 0;
1954         unsigned int journal_inum = 0;
1955         unsigned long journal_devnum = 0;
1956         unsigned long def_mount_opts;
1957         struct inode *root;
1958         char *cp;
1959         const char *descr;
1960         int ret = -EINVAL;
1961         int blocksize;
1962         int db_count;
1963         int i;
1964         int needs_recovery, has_huge_files;
1965         int features;
1966         __u64 blocks_count;
1967         int err;
1968
1969         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1970         if (!sbi)
1971                 return -ENOMEM;
1972         sb->s_fs_info = sbi;
1973         sbi->s_mount_opt = 0;
1974         sbi->s_resuid = EXT4_DEF_RESUID;
1975         sbi->s_resgid = EXT4_DEF_RESGID;
1976         sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
1977         sbi->s_sb_block = sb_block;
1978
1979         unlock_kernel();
1980
1981         /* Cleanup superblock name */
1982         for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1983                 *cp = '!';
1984
1985         blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1986         if (!blocksize) {
1987                 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1988                 goto out_fail;
1989         }
1990
1991         /*
1992          * The ext4 superblock will not be buffer aligned for other than 1kB
1993          * block sizes.  We need to calculate the offset from buffer start.
1994          */
1995         if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1996                 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1997                 offset = do_div(logical_sb_block, blocksize);
1998         } else {
1999                 logical_sb_block = sb_block;
2000         }
2001
2002         if (!(bh = sb_bread(sb, logical_sb_block))) {
2003                 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
2004                 goto out_fail;
2005         }
2006         /*
2007          * Note: s_es must be initialized as soon as possible because
2008          *       some ext4 macro-instructions depend on its value
2009          */
2010         es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2011         sbi->s_es = es;
2012         sb->s_magic = le16_to_cpu(es->s_magic);
2013         if (sb->s_magic != EXT4_SUPER_MAGIC)
2014                 goto cantfind_ext4;
2015
2016         /* Set defaults before we parse the mount options */
2017         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2018         if (def_mount_opts & EXT4_DEFM_DEBUG)
2019                 set_opt(sbi->s_mount_opt, DEBUG);
2020         if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2021                 set_opt(sbi->s_mount_opt, GRPID);
2022         if (def_mount_opts & EXT4_DEFM_UID16)
2023                 set_opt(sbi->s_mount_opt, NO_UID32);
2024 #ifdef CONFIG_EXT4_FS_XATTR
2025         if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2026                 set_opt(sbi->s_mount_opt, XATTR_USER);
2027 #endif
2028 #ifdef CONFIG_EXT4_FS_POSIX_ACL
2029         if (def_mount_opts & EXT4_DEFM_ACL)
2030                 set_opt(sbi->s_mount_opt, POSIX_ACL);
2031 #endif
2032         if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2033                 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2034         else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2035                 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2036         else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2037                 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2038
2039         if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2040                 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2041         else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2042                 set_opt(sbi->s_mount_opt, ERRORS_CONT);
2043         else
2044                 set_opt(sbi->s_mount_opt, ERRORS_RO);
2045
2046         sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2047         sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2048         sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2049         sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2050         sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2051
2052         set_opt(sbi->s_mount_opt, RESERVATION);
2053         set_opt(sbi->s_mount_opt, BARRIER);
2054
2055         /*
2056          * turn on extents feature by default in ext4 filesystem
2057          * only if feature flag already set by mkfs or tune2fs.
2058          * Use -o noextents to turn it off
2059          */
2060         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2061                 set_opt(sbi->s_mount_opt, EXTENTS);
2062         else
2063                 ext4_warning(sb, __func__,
2064                         "extents feature not enabled on this filesystem, "
2065                         "use tune2fs.");
2066
2067         /*
2068          * enable delayed allocation by default
2069          * Use -o nodelalloc to turn it off
2070          */
2071         set_opt(sbi->s_mount_opt, DELALLOC);
2072
2073
2074         if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2075                            NULL, 0))
2076                 goto failed_mount;
2077
2078         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2079                 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2080
2081         if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2082             (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2083              EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2084              EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2085                 printk(KERN_WARNING
2086                        "EXT4-fs warning: feature flags set on rev 0 fs, "
2087                        "running e2fsck is recommended\n");
2088
2089         /*
2090          * Check feature flags regardless of the revision level, since we
2091          * previously didn't change the revision level when setting the flags,
2092          * so there is a chance incompat flags are set on a rev 0 filesystem.
2093          */
2094         features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2095         if (features) {
2096                 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2097                        "unsupported optional features (%x).\n", sb->s_id,
2098                         (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2099                         ~EXT4_FEATURE_INCOMPAT_SUPP));
2100                 goto failed_mount;
2101         }
2102         features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2103         if (!(sb->s_flags & MS_RDONLY) && features) {
2104                 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2105                        "unsupported optional features (%x).\n", sb->s_id,
2106                         (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2107                         ~EXT4_FEATURE_RO_COMPAT_SUPP));
2108                 goto failed_mount;
2109         }
2110         has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2111                                     EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2112         if (has_huge_files) {
2113                 /*
2114                  * Large file size enabled file system can only be
2115                  * mount if kernel is build with CONFIG_LBD
2116                  */
2117                 if (sizeof(root->i_blocks) < sizeof(u64) &&
2118                                 !(sb->s_flags & MS_RDONLY)) {
2119                         printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2120                                         "files cannot be mounted read-write "
2121                                         "without CONFIG_LBD.\n", sb->s_id);
2122                         goto failed_mount;
2123                 }
2124         }
2125         blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2126
2127         if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2128             blocksize > EXT4_MAX_BLOCK_SIZE) {
2129                 printk(KERN_ERR
2130                        "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2131                        blocksize, sb->s_id);
2132                 goto failed_mount;
2133         }
2134
2135         if (sb->s_blocksize != blocksize) {
2136
2137                 /* Validate the filesystem blocksize */
2138                 if (!sb_set_blocksize(sb, blocksize)) {
2139                         printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2140                                         blocksize);
2141                         goto failed_mount;
2142                 }
2143
2144                 brelse(bh);
2145                 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2146                 offset = do_div(logical_sb_block, blocksize);
2147                 bh = sb_bread(sb, logical_sb_block);
2148                 if (!bh) {
2149                         printk(KERN_ERR
2150                                "EXT4-fs: Can't read superblock on 2nd try.\n");
2151                         goto failed_mount;
2152                 }
2153                 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2154                 sbi->s_es = es;
2155                 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2156                         printk(KERN_ERR
2157                                "EXT4-fs: Magic mismatch, very weird !\n");
2158                         goto failed_mount;
2159                 }
2160         }
2161
2162         sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2163                                                       has_huge_files);
2164         sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2165
2166         if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2167                 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2168                 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2169         } else {
2170                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2171                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2172                 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2173                     (!is_power_of_2(sbi->s_inode_size)) ||
2174                     (sbi->s_inode_size > blocksize)) {
2175                         printk(KERN_ERR
2176                                "EXT4-fs: unsupported inode size: %d\n",
2177                                sbi->s_inode_size);
2178                         goto failed_mount;
2179                 }
2180                 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2181                         sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2182         }
2183         sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2184         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2185                 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2186                     sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2187                     !is_power_of_2(sbi->s_desc_size)) {
2188                         printk(KERN_ERR
2189                                "EXT4-fs: unsupported descriptor size %lu\n",
2190                                sbi->s_desc_size);
2191                         goto failed_mount;
2192                 }
2193         } else
2194                 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2195         sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2196         sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2197         if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2198                 goto cantfind_ext4;
2199         sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2200         if (sbi->s_inodes_per_block == 0)
2201                 goto cantfind_ext4;
2202         sbi->s_itb_per_group = sbi->s_inodes_per_group /
2203                                         sbi->s_inodes_per_block;
2204         sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2205         sbi->s_sbh = bh;
2206         sbi->s_mount_state = le16_to_cpu(es->s_state);
2207         sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2208         sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2209         for (i = 0; i < 4; i++)
2210                 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2211         sbi->s_def_hash_version = es->s_def_hash_version;
2212         i = le32_to_cpu(es->s_flags);
2213         if (i & EXT2_FLAGS_UNSIGNED_HASH)
2214                 sbi->s_hash_unsigned = 3;
2215         else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2216 #ifdef __CHAR_UNSIGNED__
2217                 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2218                 sbi->s_hash_unsigned = 3;
2219 #else
2220                 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2221 #endif
2222                 sb->s_dirt = 1;
2223         }
2224
2225         if (sbi->s_blocks_per_group > blocksize * 8) {
2226                 printk(KERN_ERR
2227                        "EXT4-fs: #blocks per group too big: %lu\n",
2228                        sbi->s_blocks_per_group);
2229                 goto failed_mount;
2230         }
2231         if (sbi->s_inodes_per_group > blocksize * 8) {
2232                 printk(KERN_ERR
2233                        "EXT4-fs: #inodes per group too big: %lu\n",
2234                        sbi->s_inodes_per_group);
2235                 goto failed_mount;
2236         }
2237
2238         if (ext4_blocks_count(es) >
2239                     (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2240                 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2241                         " too large to mount safely\n", sb->s_id);
2242                 if (sizeof(sector_t) < 8)
2243                         printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2244                                         "enabled\n");
2245                 goto failed_mount;
2246         }
2247
2248         if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2249                 goto cantfind_ext4;
2250
2251         /* ensure blocks_count calculation below doesn't sign-extend */
2252         if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2253             le32_to_cpu(es->s_first_data_block) + 1) {
2254                 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2255                        "first data block %u, blocks per group %lu\n",
2256                         ext4_blocks_count(es),
2257                         le32_to_cpu(es->s_first_data_block),
2258                         EXT4_BLOCKS_PER_GROUP(sb));
2259                 goto failed_mount;
2260         }
2261         blocks_count = (ext4_blocks_count(es) -
2262                         le32_to_cpu(es->s_first_data_block) +
2263                         EXT4_BLOCKS_PER_GROUP(sb) - 1);
2264         do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2265         sbi->s_groups_count = blocks_count;
2266         db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2267                    EXT4_DESC_PER_BLOCK(sb);
2268         sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2269                                     GFP_KERNEL);
2270         if (sbi->s_group_desc == NULL) {
2271                 printk(KERN_ERR "EXT4-fs: not enough memory\n");
2272                 goto failed_mount;
2273         }
2274
2275 #ifdef CONFIG_PROC_FS
2276         if (ext4_proc_root)
2277                 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2278
2279         if (sbi->s_proc)
2280                 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2281                                  &ext4_ui_proc_fops,
2282                                  &sbi->s_inode_readahead_blks);
2283 #endif
2284
2285         bgl_lock_init(&sbi->s_blockgroup_lock);
2286
2287         for (i = 0; i < db_count; i++) {
2288                 block = descriptor_loc(sb, logical_sb_block, i);
2289                 sbi->s_group_desc[i] = sb_bread(sb, block);
2290                 if (!sbi->s_group_desc[i]) {
2291                         printk(KERN_ERR "EXT4-fs: "
2292                                "can't read group descriptor %d\n", i);
2293                         db_count = i;
2294                         goto failed_mount2;
2295                 }
2296         }
2297         if (!ext4_check_descriptors(sb)) {
2298                 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2299                 goto failed_mount2;
2300         }
2301         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2302                 if (!ext4_fill_flex_info(sb)) {
2303                         printk(KERN_ERR
2304                                "EXT4-fs: unable to initialize "
2305                                "flex_bg meta info!\n");
2306                         goto failed_mount2;
2307                 }
2308
2309         sbi->s_gdb_count = db_count;
2310         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2311         spin_lock_init(&sbi->s_next_gen_lock);
2312
2313         err = percpu_counter_init(&sbi->s_freeblocks_counter,
2314                         ext4_count_free_blocks(sb));
2315         if (!err) {
2316                 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2317                                 ext4_count_free_inodes(sb));
2318         }
2319         if (!err) {
2320                 err = percpu_counter_init(&sbi->s_dirs_counter,
2321                                 ext4_count_dirs(sb));
2322         }
2323         if (!err) {
2324                 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2325         }
2326         if (err) {
2327                 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2328                 goto failed_mount3;
2329         }
2330
2331         sbi->s_stripe = ext4_get_stripe_size(sbi);
2332
2333         /*
2334          * set up enough so that it can read an inode
2335          */
2336         sb->s_op = &ext4_sops;
2337         sb->s_export_op = &ext4_export_ops;
2338         sb->s_xattr = ext4_xattr_handlers;
2339 #ifdef CONFIG_QUOTA
2340         sb->s_qcop = &ext4_qctl_operations;
2341         sb->dq_op = &ext4_quota_operations;
2342 #endif
2343         INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2344
2345         sb->s_root = NULL;
2346
2347         needs_recovery = (es->s_last_orphan != 0 ||
2348                           EXT4_HAS_INCOMPAT_FEATURE(sb,
2349                                     EXT4_FEATURE_INCOMPAT_RECOVER));
2350
2351         /*
2352          * The first inode we look at is the journal inode.  Don't try
2353          * root first: it may be modified in the journal!
2354          */
2355         if (!test_opt(sb, NOLOAD) &&
2356             EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2357                 if (ext4_load_journal(sb, es, journal_devnum))
2358                         goto failed_mount3;
2359                 if (!(sb->s_flags & MS_RDONLY) &&
2360                     EXT4_SB(sb)->s_journal->j_failed_commit) {
2361                         printk(KERN_CRIT "EXT4-fs error (device %s): "
2362                                "ext4_fill_super: Journal transaction "
2363                                "%u is corrupt\n", sb->s_id,
2364                                EXT4_SB(sb)->s_journal->j_failed_commit);
2365                         if (test_opt(sb, ERRORS_RO)) {
2366                                 printk(KERN_CRIT
2367                                        "Mounting filesystem read-only\n");
2368                                 sb->s_flags |= MS_RDONLY;
2369                                 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2370                                 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2371                         }
2372                         if (test_opt(sb, ERRORS_PANIC)) {
2373                                 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2374                                 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2375                                 ext4_commit_super(sb, es, 1);
2376                                 goto failed_mount4;
2377                         }
2378                 }
2379         } else if (journal_inum) {
2380                 if (ext4_create_journal(sb, es, journal_inum))
2381                         goto failed_mount3;
2382         } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2383               EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2384                 printk(KERN_ERR "EXT4-fs: required journal recovery "
2385                        "suppressed and not mounted read-only\n");
2386                 goto failed_mount4;
2387         } else {
2388                 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2389                 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2390                 sbi->s_journal = NULL;
2391                 needs_recovery = 0;
2392                 goto no_journal;
2393         }
2394
2395         if (ext4_blocks_count(es) > 0xffffffffULL &&
2396             !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2397                                        JBD2_FEATURE_INCOMPAT_64BIT)) {
2398                 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2399                 goto failed_mount4;
2400         }
2401
2402         if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2403                 jbd2_journal_set_features(sbi->s_journal,
2404                                 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2405                                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2406         } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2407                 jbd2_journal_set_features(sbi->s_journal,
2408                                 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2409                 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2410                                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2411         } else {
2412                 jbd2_journal_clear_features(sbi->s_journal,
2413                                 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2414                                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2415         }
2416
2417         /* We have now updated the journal if required, so we can
2418          * validate the data journaling mode. */
2419         switch (test_opt(sb, DATA_FLAGS)) {
2420         case 0:
2421                 /* No mode set, assume a default based on the journal
2422                  * capabilities: ORDERED_DATA if the journal can
2423                  * cope, else JOURNAL_DATA
2424                  */
2425                 if (jbd2_journal_check_available_features
2426                     (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2427                         set_opt(sbi->s_mount_opt, ORDERED_DATA);
2428                 else
2429                         set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2430                 break;
2431
2432         case EXT4_MOUNT_ORDERED_DATA:
2433         case EXT4_MOUNT_WRITEBACK_DATA:
2434                 if (!jbd2_journal_check_available_features
2435                     (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2436                         printk(KERN_ERR "EXT4-fs: Journal does not support "
2437                                "requested data journaling mode\n");
2438                         goto failed_mount4;
2439                 }
2440         default:
2441                 break;
2442         }
2443
2444 no_journal:
2445
2446         if (test_opt(sb, NOBH)) {
2447                 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2448                         printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2449                                 "its supported only with writeback mode\n");
2450                         clear_opt(sbi->s_mount_opt, NOBH);
2451                 }
2452         }
2453         /*
2454          * The jbd2_journal_load will have done any necessary log recovery,
2455          * so we can safely mount the rest of the filesystem now.
2456          */
2457
2458         root = ext4_iget(sb, EXT4_ROOT_INO);
2459         if (IS_ERR(root)) {
2460                 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2461                 ret = PTR_ERR(root);
2462                 goto failed_mount4;
2463         }
2464         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2465                 iput(root);
2466                 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2467                 goto failed_mount4;
2468         }
2469         sb->s_root = d_alloc_root(root);
2470         if (!sb->s_root) {
2471                 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2472                 iput(root);
2473                 ret = -ENOMEM;
2474                 goto failed_mount4;
2475         }
2476
2477         ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2478
2479         /* determine the minimum size of new large inodes, if present */
2480         if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2481                 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2482                                                      EXT4_GOOD_OLD_INODE_SIZE;
2483                 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2484                                        EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2485                         if (sbi->s_want_extra_isize <
2486                             le16_to_cpu(es->s_want_extra_isize))
2487                                 sbi->s_want_extra_isize =
2488                                         le16_to_cpu(es->s_want_extra_isize);
2489                         if (sbi->s_want_extra_isize <
2490                             le16_to_cpu(es->s_min_extra_isize))
2491                                 sbi->s_want_extra_isize =
2492                                         le16_to_cpu(es->s_min_extra_isize);
2493                 }
2494         }
2495         /* Check if enough inode space is available */
2496         if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2497                                                         sbi->s_inode_size) {
2498                 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2499                                                        EXT4_GOOD_OLD_INODE_SIZE;
2500                 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2501                         "available.\n");
2502         }
2503
2504         if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2505                 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2506                                 "requested data journaling mode\n");
2507                 clear_opt(sbi->s_mount_opt, DELALLOC);
2508         } else if (test_opt(sb, DELALLOC))
2509                 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2510
2511         ext4_ext_init(sb);
2512         err = ext4_mb_init(sb, needs_recovery);
2513         if (err) {
2514                 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2515                        err);
2516                 goto failed_mount4;
2517         }
2518
2519         /*
2520          * akpm: core read_super() calls in here with the superblock locked.
2521          * That deadlocks, because orphan cleanup needs to lock the superblock
2522          * in numerous places.  Here we just pop the lock - it's relatively
2523          * harmless, because we are now ready to accept write_super() requests,
2524          * and aviro says that's the only reason for hanging onto the
2525          * superblock lock.
2526          */
2527         EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2528         ext4_orphan_cleanup(sb, es);
2529         EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2530         if (needs_recovery) {
2531                 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2532                 ext4_mark_recovery_complete(sb, es);
2533         }
2534         if (EXT4_SB(sb)->s_journal) {
2535                 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2536                         descr = " journalled data mode";
2537                 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2538                         descr = " ordered data mode";
2539                 else
2540                         descr = " writeback data mode";
2541         } else
2542                 descr = "out journal";
2543
2544         printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2545                sb->s_id, descr);
2546
2547         lock_kernel();
2548         return 0;
2549
2550 cantfind_ext4:
2551         if (!silent)
2552                 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2553                        sb->s_id);
2554         goto failed_mount;
2555
2556 failed_mount4:
2557         printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2558         if (sbi->s_journal) {
2559                 jbd2_journal_destroy(sbi->s_journal);
2560                 sbi->s_journal = NULL;
2561         }
2562 failed_mount3:
2563         percpu_counter_destroy(&sbi->s_freeblocks_counter);
2564         percpu_counter_destroy(&sbi->s_freeinodes_counter);
2565         percpu_counter_destroy(&sbi->s_dirs_counter);
2566         percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2567 failed_mount2:
2568         for (i = 0; i < db_count; i++)
2569                 brelse(sbi->s_group_desc[i]);
2570         kfree(sbi->s_group_desc);
2571 failed_mount:
2572         if (sbi->s_proc) {
2573                 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
2574                 remove_proc_entry(sb->s_id, ext4_proc_root);
2575         }
2576 #ifdef CONFIG_QUOTA
2577         for (i = 0; i < MAXQUOTAS; i++)
2578                 kfree(sbi->s_qf_names[i]);
2579 #endif
2580         ext4_blkdev_remove(sbi);
2581         brelse(bh);
2582 out_fail:
2583         sb->s_fs_info = NULL;
2584         kfree(sbi);
2585         lock_kernel();
2586         return ret;
2587 }
2588
2589 /*
2590  * Setup any per-fs journal parameters now.  We'll do this both on
2591  * initial mount, once the journal has been initialised but before we've
2592  * done any recovery; and again on any subsequent remount.
2593  */
2594 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2595 {
2596         struct ext4_sb_info *sbi = EXT4_SB(sb);
2597
2598         journal->j_commit_interval = sbi->s_commit_interval;
2599         journal->j_min_batch_time = sbi->s_min_batch_time;
2600         journal->j_max_batch_time = sbi->s_max_batch_time;
2601
2602         spin_lock(&journal->j_state_lock);
2603         if (test_opt(sb, BARRIER))
2604                 journal->j_flags |= JBD2_BARRIER;
2605         else
2606                 journal->j_flags &= ~JBD2_BARRIER;
2607         if (test_opt(sb, DATA_ERR_ABORT))
2608                 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2609         else
2610                 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2611         spin_unlock(&journal->j_state_lock);
2612 }
2613
2614 static journal_t *ext4_get_journal(struct super_block *sb,
2615                                    unsigned int journal_inum)
2616 {
2617         struct inode *journal_inode;
2618         journal_t *journal;
2619
2620         BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2621
2622         /* First, test for the existence of a valid inode on disk.  Bad
2623          * things happen if we iget() an unused inode, as the subsequent
2624          * iput() will try to delete it. */
2625
2626         journal_inode = ext4_iget(sb, journal_inum);
2627         if (IS_ERR(journal_inode)) {
2628                 printk(KERN_ERR "EXT4-fs: no journal found.\n");
2629                 return NULL;
2630         }
2631         if (!journal_inode->i_nlink) {
2632                 make_bad_inode(journal_inode);
2633                 iput(journal_inode);
2634                 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2635                 return NULL;
2636         }
2637
2638         jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2639                   journal_inode, journal_inode->i_size);
2640         if (!S_ISREG(journal_inode->i_mode)) {
2641                 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2642                 iput(journal_inode);
2643                 return NULL;
2644         }
2645
2646         journal = jbd2_journal_init_inode(journal_inode);
2647         if (!journal) {
2648                 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2649                 iput(journal_inode);
2650                 return NULL;
2651         }
2652         journal->j_private = sb;
2653         ext4_init_journal_params(sb, journal);
2654         return journal;
2655 }
2656
2657 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2658                                        dev_t j_dev)
2659 {
2660         struct buffer_head *bh;
2661         journal_t *journal;
2662         ext4_fsblk_t start;
2663         ext4_fsblk_t len;
2664         int hblock, blocksize;
2665         ext4_fsblk_t sb_block;
2666         unsigned long offset;
2667         struct ext4_super_block *es;
2668         struct block_device *bdev;
2669
2670         BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2671
2672         bdev = ext4_blkdev_get(j_dev);
2673         if (bdev == NULL)
2674                 return NULL;
2675
2676         if (bd_claim(bdev, sb)) {
2677                 printk(KERN_ERR
2678                         "EXT4: failed to claim external journal device.\n");
2679                 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2680                 return NULL;
2681         }
2682
2683         blocksize = sb->s_blocksize;
2684         hblock = bdev_hardsect_size(bdev);
2685         if (blocksize < hblock) {
2686                 printk(KERN_ERR
2687                         "EXT4-fs: blocksize too small for journal device.\n");
2688                 goto out_bdev;
2689         }
2690
2691         sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2692         offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2693         set_blocksize(bdev, blocksize);
2694         if (!(bh = __bread(bdev, sb_block, blocksize))) {
2695                 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2696                        "external journal\n");
2697                 goto out_bdev;
2698         }
2699
2700         es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2701         if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2702             !(le32_to_cpu(es->s_feature_incompat) &
2703               EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2704                 printk(KERN_ERR "EXT4-fs: external journal has "
2705                                         "bad superblock\n");
2706                 brelse(bh);
2707                 goto out_bdev;
2708         }
2709
2710         if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2711                 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2712                 brelse(bh);
2713                 goto out_bdev;
2714         }
2715
2716         len = ext4_blocks_count(es);
2717         start = sb_block + 1;
2718         brelse(bh);     /* we're done with the superblock */
2719
2720         journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2721                                         start, len, blocksize);
2722         if (!journal) {
2723                 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2724                 goto out_bdev;
2725         }
2726         journal->j_private = sb;
2727         ll_rw_block(READ, 1, &journal->j_sb_buffer);
2728         wait_on_buffer(journal->j_sb_buffer);
2729         if (!buffer_uptodate(journal->j_sb_buffer)) {
2730                 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2731                 goto out_journal;
2732         }
2733         if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2734                 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2735                                         "user (unsupported) - %d\n",
2736                         be32_to_cpu(journal->j_superblock->s_nr_users));
2737                 goto out_journal;
2738         }
2739         EXT4_SB(sb)->journal_bdev = bdev;
2740         ext4_init_journal_params(sb, journal);
2741         return journal;
2742 out_journal:
2743         jbd2_journal_destroy(journal);
2744 out_bdev:
2745         ext4_blkdev_put(bdev);
2746         return NULL;
2747 }
2748
2749 static int ext4_load_journal(struct super_block *sb,
2750                              struct ext4_super_block *es,
2751                              unsigned long journal_devnum)
2752 {
2753         journal_t *journal;
2754         unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2755         dev_t journal_dev;
2756         int err = 0;
2757         int really_read_only;
2758
2759         BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2760
2761         if (journal_devnum &&
2762             journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2763                 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2764                         "numbers have changed\n");
2765                 journal_dev = new_decode_dev(journal_devnum);
2766         } else
2767                 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2768
2769         really_read_only = bdev_read_only(sb->s_bdev);
2770
2771         /*
2772          * Are we loading a blank journal or performing recovery after a
2773          * crash?  For recovery, we need to check in advance whether we
2774          * can get read-write access to the device.
2775          */
2776
2777         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2778                 if (sb->s_flags & MS_RDONLY) {
2779                         printk(KERN_INFO "EXT4-fs: INFO: recovery "
2780                                         "required on readonly filesystem.\n");
2781                         if (really_read_only) {
2782                                 printk(KERN_ERR "EXT4-fs: write access "
2783                                         "unavailable, cannot proceed.\n");
2784                                 return -EROFS;
2785                         }
2786                         printk(KERN_INFO "EXT4-fs: write access will "
2787                                "be enabled during recovery.\n");
2788                 }
2789         }
2790
2791         if (journal_inum && journal_dev) {
2792                 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2793                        "and inode journals!\n");
2794                 return -EINVAL;
2795         }
2796
2797         if (journal_inum) {
2798                 if (!(journal = ext4_get_journal(sb, journal_inum)))
2799                         return -EINVAL;
2800         } else {
2801                 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2802                         return -EINVAL;
2803         }
2804
2805         if (journal->j_flags & JBD2_BARRIER)
2806                 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2807         else
2808                 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2809
2810         if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2811                 err = jbd2_journal_update_format(journal);
2812                 if (err)  {
2813                         printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2814                         jbd2_journal_destroy(journal);
2815                         return err;
2816                 }
2817         }
2818
2819         if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2820                 err = jbd2_journal_wipe(journal, !really_read_only);
2821         if (!err)
2822                 err = jbd2_journal_load(journal);
2823
2824         if (err) {
2825                 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2826                 jbd2_journal_destroy(journal);
2827                 return err;
2828         }
2829
2830         EXT4_SB(sb)->s_journal = journal;
2831         ext4_clear_journal_err(sb, es);
2832
2833         if (journal_devnum &&
2834             journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2835                 es->s_journal_dev = cpu_to_le32(journal_devnum);
2836                 sb->s_dirt = 1;
2837
2838                 /* Make sure we flush the recovery flag to disk. */
2839                 ext4_commit_super(sb, es, 1);
2840         }
2841
2842         return 0;
2843 }
2844
2845 static int ext4_create_journal(struct super_block *sb,
2846                                struct ext4_super_block *es,
2847                                unsigned int journal_inum)
2848 {
2849         journal_t *journal;
2850         int err;
2851
2852         if (sb->s_flags & MS_RDONLY) {
2853                 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2854                                 "create journal.\n");
2855                 return -EROFS;
2856         }
2857
2858         journal = ext4_get_journal(sb, journal_inum);
2859         if (!journal)
2860                 return -EINVAL;
2861
2862         printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2863                journal_inum);
2864
2865         err = jbd2_journal_create(journal);
2866         if (err) {
2867                 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2868                 jbd2_journal_destroy(journal);
2869                 return -EIO;
2870         }
2871
2872         EXT4_SB(sb)->s_journal = journal;
2873
2874         ext4_update_dynamic_rev(sb);
2875         EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2876         EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2877
2878         es->s_journal_inum = cpu_to_le32(journal_inum);
2879         sb->s_dirt = 1;
2880
2881         /* Make sure we flush the recovery flag to disk. */
2882         ext4_commit_super(sb, es, 1);
2883
2884         return 0;
2885 }
2886
2887 static void ext4_commit_super(struct super_block *sb,
2888                               struct ext4_super_block *es, int sync)
2889 {
2890         struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2891
2892         if (!sbh)
2893                 return;
2894         if (buffer_write_io_error(sbh)) {
2895                 /*
2896                  * Oh, dear.  A previous attempt to write the
2897                  * superblock failed.  This could happen because the
2898                  * USB device was yanked out.  Or it could happen to
2899                  * be a transient write error and maybe the block will
2900                  * be remapped.  Nothing we can do but to retry the
2901                  * write and hope for the best.
2902                  */
2903                 printk(KERN_ERR "ext4: previous I/O error to "
2904                        "superblock detected for %s.\n", sb->s_id);
2905                 clear_buffer_write_io_error(sbh);
2906                 set_buffer_uptodate(sbh);
2907         }
2908         es->s_wtime = cpu_to_le32(get_seconds());
2909         ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
2910                                         &EXT4_SB(sb)->s_freeblocks_counter));
2911         es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
2912                                         &EXT4_SB(sb)->s_freeinodes_counter));
2913
2914         BUFFER_TRACE(sbh, "marking dirty");
2915         mark_buffer_dirty(sbh);
2916         if (sync) {
2917                 sync_dirty_buffer(sbh);
2918                 if (buffer_write_io_error(sbh)) {
2919                         printk(KERN_ERR "ext4: I/O error while writing "
2920                                "superblock for %s.\n", sb->s_id);
2921                         clear_buffer_write_io_error(sbh);
2922                         set_buffer_uptodate(sbh);
2923                 }
2924         }
2925 }
2926
2927
2928 /*
2929  * Have we just finished recovery?  If so, and if we are mounting (or
2930  * remounting) the filesystem readonly, then we will end up with a
2931  * consistent fs on disk.  Record that fact.
2932  */
2933 static void ext4_mark_recovery_complete(struct super_block *sb,
2934                                         struct ext4_super_block *es)
2935 {
2936         journal_t *journal = EXT4_SB(sb)->s_journal;
2937
2938         if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2939                 BUG_ON(journal != NULL);
2940                 return;
2941         }
2942         jbd2_journal_lock_updates(journal);
2943         if (jbd2_journal_flush(journal) < 0)
2944                 goto out;
2945
2946         lock_super(sb);
2947         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2948             sb->s_flags & MS_RDONLY) {
2949                 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2950                 sb->s_dirt = 0;
2951                 ext4_commit_super(sb, es, 1);
2952         }
2953         unlock_super(sb);
2954
2955 out:
2956         jbd2_journal_unlock_updates(journal);
2957 }
2958
2959 /*
2960  * If we are mounting (or read-write remounting) a filesystem whose journal
2961  * has recorded an error from a previous lifetime, move that error to the
2962  * main filesystem now.
2963  */
2964 static void ext4_clear_journal_err(struct super_block *sb,
2965                                    struct ext4_super_block *es)
2966 {
2967         journal_t *journal;
2968         int j_errno;
2969         const char *errstr;
2970
2971         BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2972
2973         journal = EXT4_SB(sb)->s_journal;
2974
2975         /*
2976          * Now check for any error status which may have been recorded in the
2977          * journal by a prior ext4_error() or ext4_abort()
2978          */
2979
2980         j_errno = jbd2_journal_errno(journal);
2981         if (j_errno) {
2982                 char nbuf[16];
2983
2984                 errstr = ext4_decode_error(sb, j_errno, nbuf);
2985                 ext4_warning(sb, __func__, "Filesystem error recorded "
2986                              "from previous mount: %s", errstr);
2987                 ext4_warning(sb, __func__, "Marking fs in need of "
2988                              "filesystem check.");
2989
2990                 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2991                 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2992                 ext4_commit_super(sb, es, 1);
2993
2994                 jbd2_journal_clear_err(journal);
2995         }
2996 }
2997
2998 /*
2999  * Force the running and committing transactions to commit,
3000  * and wait on the commit.
3001  */
3002 int ext4_force_commit(struct super_block *sb)
3003 {
3004         journal_t *journal;
3005         int ret = 0;
3006
3007         if (sb->s_flags & MS_RDONLY)
3008                 return 0;
3009
3010         journal = EXT4_SB(sb)->s_journal;
3011         if (journal) {
3012                 sb->s_dirt = 0;
3013                 ret = ext4_journal_force_commit(journal);
3014         }
3015
3016         return ret;
3017 }
3018
3019 /*
3020  * Ext4 always journals updates to the superblock itself, so we don't
3021  * have to propagate any other updates to the superblock on disk at this
3022  * point.  (We can probably nuke this function altogether, and remove
3023  * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
3024  */
3025 static void ext4_write_super(struct super_block *sb)
3026 {
3027         if (EXT4_SB(sb)->s_journal) {
3028                 if (mutex_trylock(&sb->s_lock) != 0)
3029                         BUG();
3030                 sb->s_dirt = 0;
3031         } else {
3032                 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3033         }
3034 }
3035
3036 static int ext4_sync_fs(struct super_block *sb, int wait)
3037 {
3038         int ret = 0;
3039
3040         trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
3041         sb->s_dirt = 0;
3042         if (EXT4_SB(sb)->s_journal) {
3043                 if (wait)
3044                         ret = ext4_force_commit(sb);
3045                 else
3046                         jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3047         } else {
3048                 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3049         }
3050         return ret;
3051 }
3052
3053 /*
3054  * LVM calls this function before a (read-only) snapshot is created.  This
3055  * gives us a chance to flush the journal completely and mark the fs clean.
3056  */
3057 static void ext4_write_super_lockfs(struct super_block *sb)
3058 {
3059         sb->s_dirt = 0;
3060
3061         if (!(sb->s_flags & MS_RDONLY)) {
3062                 journal_t *journal = EXT4_SB(sb)->s_journal;
3063
3064                 if (journal) {
3065                         /* Now we set up the journal barrier. */
3066                         jbd2_journal_lock_updates(journal);
3067
3068                         /*
3069                          * We don't want to clear needs_recovery flag when we
3070                          * failed to flush the journal.
3071                          */
3072                         if (jbd2_journal_flush(journal) < 0)
3073                                 return;
3074                 }
3075
3076                 /* Journal blocked and flushed, clear needs_recovery flag. */
3077                 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3078                 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3079         }
3080 }
3081
3082 /*
3083  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
3084  * flag here, even though the filesystem is not technically dirty yet.
3085  */
3086 static void ext4_unlockfs(struct super_block *sb)
3087 {
3088         if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
3089                 lock_super(sb);
3090                 /* Reser the needs_recovery flag before the fs is unlocked. */
3091                 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3092                 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3093                 unlock_super(sb);
3094                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3095         }
3096 }
3097
3098 static int ext4_remount(struct super_block *sb, int *flags, char *data)
3099 {
3100         struct ext4_super_block *es;
3101         struct ext4_sb_info *sbi = EXT4_SB(sb);
3102         ext4_fsblk_t n_blocks_count = 0;
3103         unsigned long old_sb_flags;
3104         struct ext4_mount_options old_opts;
3105         ext4_group_t g;
3106         int err;
3107 #ifdef CONFIG_QUOTA
3108         int i;
3109 #endif
3110
3111         /* Store the original options */
3112         old_sb_flags = sb->s_flags;
3113         old_opts.s_mount_opt = sbi->s_mount_opt;
3114         old_opts.s_resuid = sbi->s_resuid;
3115         old_opts.s_resgid = sbi->s_resgid;
3116         old_opts.s_commit_interval = sbi->s_commit_interval;
3117         old_opts.s_min_batch_time = sbi->s_min_batch_time;
3118         old_opts.s_max_batch_time = sbi->s_max_batch_time;
3119 #ifdef CONFIG_QUOTA
3120         old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3121         for (i = 0; i < MAXQUOTAS; i++)
3122                 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3123 #endif
3124
3125         /*
3126          * Allow the "check" option to be passed as a remount option.
3127          */
3128         if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3129                 err = -EINVAL;
3130                 goto restore_opts;
3131         }
3132
3133         if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
3134                 ext4_abort(sb, __func__, "Abort forced by user");
3135
3136         sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3137                 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3138
3139         es = sbi->s_es;
3140
3141         if (sbi->s_journal)
3142                 ext4_init_journal_params(sb, sbi->s_journal);
3143
3144         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3145                 n_blocks_count > ext4_blocks_count(es)) {
3146                 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3147                         err = -EROFS;
3148                         goto restore_opts;
3149                 }
3150
3151                 if (*flags & MS_RDONLY) {
3152                         /*
3153                          * First of all, the unconditional stuff we have to do
3154                          * to disable replay of the journal when we next remount
3155                          */
3156                         sb->s_flags |= MS_RDONLY;
3157
3158                         /*
3159                          * OK, test if we are remounting a valid rw partition
3160                          * readonly, and if so set the rdonly flag and then
3161                          * mark the partition as valid again.
3162                          */
3163                         if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3164                             (sbi->s_mount_state & EXT4_VALID_FS))
3165                                 es->s_state = cpu_to_le16(sbi->s_mount_state);
3166
3167                         /*
3168                          * We have to unlock super so that we can wait for
3169                          * transactions.
3170                          */
3171                         if (sbi->s_journal) {
3172                                 unlock_super(sb);
3173                                 ext4_mark_recovery_complete(sb, es);
3174                                 lock_super(sb);
3175                         }
3176                 } else {
3177                         int ret;
3178                         if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3179                                         ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3180                                 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3181                                        "remount RDWR because of unsupported "
3182                                        "optional features (%x).\n", sb->s_id,
3183                                 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3184                                         ~EXT4_FEATURE_RO_COMPAT_SUPP));
3185                                 err = -EROFS;
3186                                 goto restore_opts;
3187                         }
3188
3189                         /*
3190                          * Make sure the group descriptor checksums
3191                          * are sane.  If they aren't, refuse to
3192                          * remount r/w.
3193                          */
3194                         for (g = 0; g < sbi->s_groups_count; g++) {
3195                                 struct ext4_group_desc *gdp =
3196                                         ext4_get_group_desc(sb, g, NULL);
3197
3198                                 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3199                                         printk(KERN_ERR
3200                "EXT4-fs: ext4_remount: "
3201                 "Checksum for group %u failed (%u!=%u)\n",
3202                 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3203                                                le16_to_cpu(gdp->bg_checksum));
3204                                         err = -EINVAL;
3205                                         goto restore_opts;
3206                                 }
3207                         }
3208
3209                         /*
3210                          * If we have an unprocessed orphan list hanging
3211                          * around from a previously readonly bdev mount,
3212                          * require a full umount/remount for now.
3213                          */
3214                         if (es->s_last_orphan) {
3215                                 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3216                                        "remount RDWR because of unprocessed "
3217                                        "orphan inode list.  Please "
3218                                        "umount/remount instead.\n",
3219                                        sb->s_id);
3220                                 err = -EINVAL;
3221                                 goto restore_opts;
3222                         }
3223
3224                         /*
3225                          * Mounting a RDONLY partition read-write, so reread
3226                          * and store the current valid flag.  (It may have
3227                          * been changed by e2fsck since we originally mounted
3228                          * the partition.)
3229                          */
3230                         if (sbi->s_journal)
3231                                 ext4_clear_journal_err(sb, es);
3232                         sbi->s_mount_state = le16_to_cpu(es->s_state);
3233                         if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3234                                 goto restore_opts;
3235                         if (!ext4_setup_super(sb, es, 0))
3236                                 sb->s_flags &= ~MS_RDONLY;
3237                 }
3238         }
3239         if (sbi->s_journal == NULL)
3240                 ext4_commit_super(sb, es, 1);
3241
3242 #ifdef CONFIG_QUOTA
3243         /* Release old quota file names */
3244         for (i = 0; i < MAXQUOTAS; i++)
3245                 if (old_opts.s_qf_names[i] &&
3246                     old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3247                         kfree(old_opts.s_qf_names[i]);
3248 #endif
3249         return 0;
3250 restore_opts:
3251         sb->s_flags = old_sb_flags;
3252         sbi->s_mount_opt = old_opts.s_mount_opt;
3253         sbi->s_resuid = old_opts.s_resuid;
3254         sbi->s_resgid = old_opts.s_resgid;
3255         sbi->s_commit_interval = old_opts.s_commit_interval;
3256         sbi->s_min_batch_time = old_opts.s_min_batch_time;
3257         sbi->s_max_batch_time = old_opts.s_max_batch_time;
3258 #ifdef CONFIG_QUOTA
3259         sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3260         for (i = 0; i < MAXQUOTAS; i++) {
3261                 if (sbi->s_qf_names[i] &&
3262                     old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3263                         kfree(sbi->s_qf_names[i]);
3264                 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3265         }
3266 #endif
3267         return err;
3268 }
3269
3270 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3271 {
3272         struct super_block *sb = dentry->d_sb;
3273         struct ext4_sb_info *sbi = EXT4_SB(sb);
3274         struct ext4_super_block *es = sbi->s_es;
3275         u64 fsid;
3276
3277         if (test_opt(sb, MINIX_DF)) {
3278                 sbi->s_overhead_last = 0;
3279         } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3280                 ext4_group_t ngroups = sbi->s_groups_count, i;
3281                 ext4_fsblk_t overhead = 0;
3282                 smp_rmb();
3283
3284                 /*
3285                  * Compute the overhead (FS structures).  This is constant
3286                  * for a given filesystem unless the number of block groups
3287                  * changes so we cache the previous value until it does.
3288                  */
3289
3290                 /*
3291                  * All of the blocks before first_data_block are
3292                  * overhead
3293                  */
3294                 overhead = le32_to_cpu(es->s_first_data_block);
3295
3296                 /*
3297                  * Add the overhead attributed to the superblock and
3298                  * block group descriptors.  If the sparse superblocks
3299                  * feature is turned on, then not all groups have this.
3300                  */
3301                 for (i = 0; i < ngroups; i++) {
3302                         overhead += ext4_bg_has_super(sb, i) +
3303                                 ext4_bg_num_gdb(sb, i);
3304                         cond_resched();
3305                 }
3306
3307                 /*
3308                  * Every block group has an inode bitmap, a block
3309                  * bitmap, and an inode table.
3310                  */
3311                 overhead += ngroups * (2 + sbi->s_itb_per_group);
3312                 sbi->s_overhead_last = overhead;
3313                 smp_wmb();
3314                 sbi->s_blocks_last = ext4_blocks_count(es);
3315         }
3316
3317         buf->f_type = EXT4_SUPER_MAGIC;
3318         buf->f_bsize = sb->s_blocksize;
3319         buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3320         buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3321                        percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3322         ext4_free_blocks_count_set(es, buf->f_bfree);
3323         buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3324         if (buf->f_bfree < ext4_r_blocks_count(es))
3325                 buf->f_bavail = 0;
3326         buf->f_files = le32_to_cpu(es->s_inodes_count);
3327         buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3328         es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3329         buf->f_namelen = EXT4_NAME_LEN;
3330         fsid = le64_to_cpup((void *)es->s_uuid) ^
3331                le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3332         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3333         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3334         return 0;
3335 }
3336
3337 /* Helper function for writing quotas on sync - we need to start transaction before quota file
3338  * is locked for write. Otherwise the are possible deadlocks:
3339  * Process 1                         Process 2
3340  * ext4_create()                     quota_sync()
3341  *   jbd2_journal_start()                   write_dquot()
3342  *   DQUOT_INIT()                        down(dqio_mutex)
3343  *     down(dqio_mutex)                    jbd2_journal_start()
3344  *
3345  */
3346
3347 #ifdef CONFIG_QUOTA
3348
3349 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3350 {
3351         return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3352 }
3353
3354 static int ext4_dquot_initialize(struct inode *inode, int type)
3355 {
3356         handle_t *handle;
3357         int ret, err;
3358
3359         /* We may create quota structure so we need to reserve enough blocks */
3360         handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3361         if (IS_ERR(handle))
3362                 return PTR_ERR(handle);
3363         ret = dquot_initialize(inode, type);
3364         err = ext4_journal_stop(handle);
3365         if (!ret)
3366                 ret = err;
3367         return ret;
3368 }
3369
3370 static int ext4_dquot_drop(struct inode *inode)
3371 {
3372         handle_t *handle;
3373         int ret, err;
3374
3375         /* We may delete quota structure so we need to reserve enough blocks */
3376         handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3377         if (IS_ERR(handle)) {
3378                 /*
3379                  * We call dquot_drop() anyway to at least release references
3380                  * to quota structures so that umount does not hang.
3381                  */
3382                 dquot_drop(inode);
3383                 return PTR_ERR(handle);
3384         }
3385         ret = dquot_drop(inode);
3386         err = ext4_journal_stop(handle);
3387         if (!ret)
3388                 ret = err;
3389         return ret;
3390 }
3391
3392 static int ext4_write_dquot(struct dquot *dquot)
3393 {
3394         int ret, err;
3395         handle_t *handle;
3396         struct inode *inode;
3397
3398         inode = dquot_to_inode(dquot);
3399         handle = ext4_journal_start(inode,
3400                                         EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3401         if (IS_ERR(handle))
3402                 return PTR_ERR(handle);
3403         ret = dquot_commit(dquot);
3404         err = ext4_journal_stop(handle);
3405         if (!ret)
3406                 ret = err;
3407         return ret;
3408 }
3409
3410 static int ext4_acquire_dquot(struct dquot *dquot)
3411 {
3412         int ret, err;
3413         handle_t *handle;
3414
3415         handle = ext4_journal_start(dquot_to_inode(dquot),
3416                                         EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3417         if (IS_ERR(handle))
3418                 return PTR_ERR(handle);
3419         ret = dquot_acquire(dquot);
3420         err = ext4_journal_stop(handle);
3421         if (!ret)
3422                 ret = err;
3423         return ret;
3424 }
3425
3426 static int ext4_release_dquot(struct dquot *dquot)
3427 {
3428         int ret, err;
3429         handle_t *handle;
3430
3431         handle = ext4_journal_start(dquot_to_inode(dquot),
3432                                         EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3433         if (IS_ERR(handle)) {
3434                 /* Release dquot anyway to avoid endless cycle in dqput() */
3435                 dquot_release(dquot);
3436                 return PTR_ERR(handle);
3437         }
3438         ret = dquot_release(dquot);
3439         err = ext4_journal_stop(handle);
3440         if (!ret)
3441                 ret = err;
3442         return ret;
3443 }
3444
3445 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3446 {
3447         /* Are we journaling quotas? */
3448         if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3449             EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3450                 dquot_mark_dquot_dirty(dquot);
3451                 return ext4_write_dquot(dquot);
3452         } else {
3453                 return dquot_mark_dquot_dirty(dquot);
3454         }
3455 }
3456
3457 static int ext4_write_info(struct super_block *sb, int type)
3458 {
3459         int ret, err;
3460         handle_t *handle;
3461
3462         /* Data block + inode block */
3463         handle = ext4_journal_start(sb->s_root->d_inode, 2);
3464         if (IS_ERR(handle))
3465                 return PTR_ERR(handle);
3466         ret = dquot_commit_info(sb, type);
3467         err = ext4_journal_stop(handle);
3468         if (!ret)
3469                 ret = err;
3470         return ret;
3471 }
3472
3473 /*
3474  * Turn on quotas during mount time - we need to find
3475  * the quota file and such...
3476  */
3477 static int ext4_quota_on_mount(struct super_block *sb, int type)
3478 {
3479         return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3480                         EXT4_SB(sb)->s_jquota_fmt, type);
3481 }
3482
3483 /*
3484  * Standard function to be called on quota_on
3485  */
3486 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3487                          char *name, int remount)
3488 {
3489         int err;
3490         struct path path;
3491
3492         if (!test_opt(sb, QUOTA))
3493                 return -EINVAL;
3494         /* When remounting, no checks are needed and in fact, name is NULL */
3495         if (remount)
3496                 return vfs_quota_on(sb, type, format_id, name, remount);
3497
3498         err = kern_path(name, LOOKUP_FOLLOW, &path);
3499         if (err)
3500                 return err;
3501
3502         /* Quotafile not on the same filesystem? */
3503         if (path.mnt->mnt_sb != sb) {
3504                 path_put(&path);
3505                 return -EXDEV;
3506         }
3507         /* Journaling quota? */
3508         if (EXT4_SB(sb)->s_qf_names[type]) {
3509                 /* Quotafile not in fs root? */
3510                 if (path.dentry->d_parent != sb->s_root)
3511                         printk(KERN_WARNING
3512                                 "EXT4-fs: Quota file not on filesystem root. "
3513                                 "Journaled quota will not work.\n");
3514         }
3515
3516         /*
3517          * When we journal data on quota file, we have to flush journal to see
3518          * all updates to the file when we bypass pagecache...
3519          */
3520         if (EXT4_SB(sb)->s_journal &&
3521             ext4_should_journal_data(path.dentry->d_inode)) {
3522                 /*
3523                  * We don't need to lock updates but journal_flush() could
3524                  * otherwise be livelocked...
3525                  */
3526                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3527                 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3528                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3529                 if (err) {
3530                         path_put(&path);
3531                         return err;
3532                 }
3533         }
3534
3535         err = vfs_quota_on_path(sb, type, format_id, &path);
3536         path_put(&path);
3537         return err;
3538 }
3539
3540 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3541  * acquiring the locks... As quota files are never truncated and quota code
3542  * itself serializes the operations (and noone else should touch the files)
3543  * we don't have to be afraid of races */
3544 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3545                                size_t len, loff_t off)
3546 {
3547         struct inode *inode = sb_dqopt(sb)->files[type];
3548         ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3549         int err = 0;
3550         int offset = off & (sb->s_blocksize - 1);
3551         int tocopy;
3552         size_t toread;
3553         struct buffer_head *bh;
3554         loff_t i_size = i_size_read(inode);
3555
3556         if (off > i_size)
3557                 return 0;
3558         if (off+len > i_size)
3559                 len = i_size-off;
3560         toread = len;
3561         while (toread > 0) {
3562                 tocopy = sb->s_blocksize - offset < toread ?
3563                                 sb->s_blocksize - offset : toread;
3564                 bh = ext4_bread(NULL, inode, blk, 0, &err);
3565                 if (err)
3566                         return err;
3567                 if (!bh)        /* A hole? */
3568                         memset(data, 0, tocopy);
3569                 else
3570                         memcpy(data, bh->b_data+offset, tocopy);
3571                 brelse(bh);
3572                 offset = 0;
3573                 toread -= tocopy;
3574                 data += tocopy;
3575                 blk++;
3576         }
3577         return len;
3578 }
3579
3580 /* Write to quotafile (we know the transaction is already started and has
3581  * enough credits) */
3582 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3583                                 const char *data, size_t len, loff_t off)
3584 {
3585         struct inode *inode = sb_dqopt(sb)->files[type];
3586         ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3587         int err = 0;
3588         int offset = off & (sb->s_blocksize - 1);
3589         int tocopy;
3590         int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3591         size_t towrite = len;
3592         struct buffer_head *bh;
3593         handle_t *handle = journal_current_handle();
3594
3595         if (EXT4_SB(sb)->s_journal && !handle) {
3596                 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
3597                         " cancelled because transaction is not started.\n",
3598                         (unsigned long long)off, (unsigned long long)len);
3599                 return -EIO;
3600         }
3601         mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3602         while (towrite > 0) {
3603                 tocopy = sb->s_blocksize - offset < towrite ?
3604                                 sb->s_blocksize - offset : towrite;
3605                 bh = ext4_bread(handle, inode, blk, 1, &err);
3606                 if (!bh)
3607                         goto out;
3608                 if (journal_quota) {
3609                         err = ext4_journal_get_write_access(handle, bh);
3610                         if (err) {
3611                                 brelse(bh);
3612                                 goto out;
3613                         }
3614                 }
3615                 lock_buffer(bh);
3616                 memcpy(bh->b_data+offset, data, tocopy);
3617                 flush_dcache_page(bh->b_page);
3618                 unlock_buffer(bh);
3619                 if (journal_quota)
3620                         err = ext4_handle_dirty_metadata(handle, NULL, bh);
3621                 else {
3622                         /* Always do at least ordered writes for quotas */
3623                         err = ext4_jbd2_file_inode(handle, inode);
3624                         mark_buffer_dirty(bh);
3625                 }
3626                 brelse(bh);
3627                 if (err)
3628                         goto out;
3629                 offset = 0;
3630                 towrite -= tocopy;
3631                 data += tocopy;
3632                 blk++;
3633         }
3634 out:
3635         if (len == towrite) {
3636                 mutex_unlock(&inode->i_mutex);
3637                 return err;
3638         }
3639         if (inode->i_size < off+len-towrite) {
3640                 i_size_write(inode, off+len-towrite);
3641                 EXT4_I(inode)->i_disksize = inode->i_size;
3642         }
3643         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3644         ext4_mark_inode_dirty(handle, inode);
3645         mutex_unlock(&inode->i_mutex);
3646         return len - towrite;
3647 }
3648
3649 #endif
3650
3651 static int ext4_get_sb(struct file_system_type *fs_type,
3652         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3653 {
3654         return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3655 }
3656
3657 #ifdef CONFIG_PROC_FS
3658 static int ext4_ui_proc_show(struct seq_file *m, void *v)
3659 {
3660         unsigned int *p = m->private;
3661
3662         seq_printf(m, "%u\n", *p);
3663         return 0;
3664 }
3665
3666 static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3667 {
3668         return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3669 }
3670
3671 static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3672                                size_t cnt, loff_t *ppos)
3673 {
3674         unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
3675         char str[32];
3676
3677         if (cnt >= sizeof(str))
3678                 return -EINVAL;
3679         if (copy_from_user(str, buf, cnt))
3680                 return -EFAULT;
3681
3682         *p = simple_strtoul(str, NULL, 0);
3683         return cnt;
3684 }
3685
3686 const struct file_operations ext4_ui_proc_fops = {
3687         .owner          = THIS_MODULE,
3688         .open           = ext4_ui_proc_open,
3689         .read           = seq_read,
3690         .llseek         = seq_lseek,
3691         .release        = single_release,
3692         .write          = ext4_ui_proc_write,
3693 };
3694 #endif
3695
3696 static struct file_system_type ext4_fs_type = {
3697         .owner          = THIS_MODULE,
3698         .name           = "ext4",
3699         .get_sb         = ext4_get_sb,
3700         .kill_sb        = kill_block_super,
3701         .fs_flags       = FS_REQUIRES_DEV,
3702 };
3703
3704 #ifdef CONFIG_EXT4DEV_COMPAT
3705 static int ext4dev_get_sb(struct file_system_type *fs_type,
3706         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3707 {
3708         printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3709                "to mount using ext4\n");
3710         printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3711                "will go away by 2.6.31\n");
3712         return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3713 }
3714
3715 static struct file_system_type ext4dev_fs_type = {
3716         .owner          = THIS_MODULE,
3717         .name           = "ext4dev",
3718         .get_sb         = ext4dev_get_sb,
3719         .kill_sb        = kill_block_super,
3720         .fs_flags       = FS_REQUIRES_DEV,
3721 };
3722 MODULE_ALIAS("ext4dev");
3723 #endif
3724
3725 static int __init init_ext4_fs(void)
3726 {
3727         int err;
3728
3729         ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3730         err = init_ext4_mballoc();
3731         if (err)
3732                 return err;
3733
3734         err = init_ext4_xattr();
3735         if (err)
3736                 goto out2;
3737         err = init_inodecache();
3738         if (err)
3739                 goto out1;
3740         err = register_filesystem(&ext4_fs_type);
3741         if (err)
3742                 goto out;
3743 #ifdef CONFIG_EXT4DEV_COMPAT
3744         err = register_filesystem(&ext4dev_fs_type);
3745         if (err) {
3746                 unregister_filesystem(&ext4_fs_type);
3747                 goto out;
3748         }
3749 #endif
3750         return 0;
3751 out:
3752         destroy_inodecache();
3753 out1:
3754         exit_ext4_xattr();
3755 out2:
3756         exit_ext4_mballoc();
3757         return err;
3758 }
3759
3760 static void __exit exit_ext4_fs(void)
3761 {
3762         unregister_filesystem(&ext4_fs_type);
3763 #ifdef CONFIG_EXT4DEV_COMPAT
3764         unregister_filesystem(&ext4dev_fs_type);
3765 #endif
3766         destroy_inodecache();
3767         exit_ext4_xattr();
3768         exit_ext4_mballoc();
3769         remove_proc_entry("fs/ext4", NULL);
3770 }
3771
3772 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3773 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3774 MODULE_LICENSE("GPL");
3775 module_init(init_ext4_fs)
3776 module_exit(exit_ext4_fs)