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