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