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