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