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