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