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