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