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