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