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