mac80211: Reset dynamic ps timer in Rx path.
[linux-2.6/mini2440.git] / fs / ext4 / super.c
blob92943f2ca2abcb036f8cad4681d0124ca44d78a0
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 /* Special case here: if the journal has aborted behind our
231 * backs (eg. EIO in the commit thread), then we still need to
232 * take the FS itself readonly cleanly. */
233 journal = EXT4_SB(sb)->s_journal;
234 if (journal) {
235 if (is_journal_aborted(journal)) {
236 ext4_abort(sb, __func__, "Detected aborted journal");
237 return ERR_PTR(-EROFS);
239 return jbd2_journal_start(journal, nblocks);
241 return ext4_get_nojournal();
245 * The only special thing we need to do here is to make sure that all
246 * jbd2_journal_stop calls result in the superblock being marked dirty, so
247 * that sync() will call the filesystem's write_super callback if
248 * appropriate.
250 int __ext4_journal_stop(const char *where, handle_t *handle)
252 struct super_block *sb;
253 int err;
254 int rc;
256 if (!ext4_handle_valid(handle)) {
257 ext4_put_nojournal(handle);
258 return 0;
260 sb = handle->h_transaction->t_journal->j_private;
261 err = handle->h_err;
262 rc = jbd2_journal_stop(handle);
264 if (!err)
265 err = rc;
266 if (err)
267 __ext4_std_error(sb, where, err);
268 return err;
271 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
272 struct buffer_head *bh, handle_t *handle, int err)
274 char nbuf[16];
275 const char *errstr = ext4_decode_error(NULL, err, nbuf);
277 BUG_ON(!ext4_handle_valid(handle));
279 if (bh)
280 BUFFER_TRACE(bh, "abort");
282 if (!handle->h_err)
283 handle->h_err = err;
285 if (is_handle_aborted(handle))
286 return;
288 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
289 caller, errstr, err_fn);
291 jbd2_journal_abort_handle(handle);
294 /* Deal with the reporting of failure conditions on a filesystem such as
295 * inconsistencies detected or read IO failures.
297 * On ext2, we can store the error state of the filesystem in the
298 * superblock. That is not possible on ext4, because we may have other
299 * write ordering constraints on the superblock which prevent us from
300 * writing it out straight away; and given that the journal is about to
301 * be aborted, we can't rely on the current, or future, transactions to
302 * write out the superblock safely.
304 * We'll just use the jbd2_journal_abort() error code to record an error in
305 * the journal instead. On recovery, the journal will compain about
306 * that error until we've noted it down and cleared it.
309 static void ext4_handle_error(struct super_block *sb)
311 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
313 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
314 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
316 if (sb->s_flags & MS_RDONLY)
317 return;
319 if (!test_opt(sb, ERRORS_CONT)) {
320 journal_t *journal = EXT4_SB(sb)->s_journal;
322 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
323 if (journal)
324 jbd2_journal_abort(journal, -EIO);
326 if (test_opt(sb, ERRORS_RO)) {
327 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
328 sb->s_flags |= MS_RDONLY;
330 ext4_commit_super(sb, 1);
331 if (test_opt(sb, ERRORS_PANIC))
332 panic("EXT4-fs (device %s): panic forced after error\n",
333 sb->s_id);
336 void ext4_error(struct super_block *sb, const char *function,
337 const char *fmt, ...)
339 va_list args;
341 va_start(args, fmt);
342 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
343 vprintk(fmt, args);
344 printk("\n");
345 va_end(args);
347 ext4_handle_error(sb);
350 static const char *ext4_decode_error(struct super_block *sb, int errno,
351 char nbuf[16])
353 char *errstr = NULL;
355 switch (errno) {
356 case -EIO:
357 errstr = "IO failure";
358 break;
359 case -ENOMEM:
360 errstr = "Out of memory";
361 break;
362 case -EROFS:
363 if (!sb || (EXT4_SB(sb)->s_journal &&
364 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
365 errstr = "Journal has aborted";
366 else
367 errstr = "Readonly filesystem";
368 break;
369 default:
370 /* If the caller passed in an extra buffer for unknown
371 * errors, textualise them now. Else we just return
372 * NULL. */
373 if (nbuf) {
374 /* Check for truncated error codes... */
375 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
376 errstr = nbuf;
378 break;
381 return errstr;
384 /* __ext4_std_error decodes expected errors from journaling functions
385 * automatically and invokes the appropriate error response. */
387 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
389 char nbuf[16];
390 const char *errstr;
392 /* Special case: if the error is EROFS, and we're not already
393 * inside a transaction, then there's really no point in logging
394 * an error. */
395 if (errno == -EROFS && journal_current_handle() == NULL &&
396 (sb->s_flags & MS_RDONLY))
397 return;
399 errstr = ext4_decode_error(sb, errno, nbuf);
400 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
401 sb->s_id, function, errstr);
403 ext4_handle_error(sb);
407 * ext4_abort is a much stronger failure handler than ext4_error. The
408 * abort function may be used to deal with unrecoverable failures such
409 * as journal IO errors or ENOMEM at a critical moment in log management.
411 * We unconditionally force the filesystem into an ABORT|READONLY state,
412 * unless the error response on the fs has been set to panic in which
413 * case we take the easy way out and panic immediately.
416 void ext4_abort(struct super_block *sb, const char *function,
417 const char *fmt, ...)
419 va_list args;
421 va_start(args, fmt);
422 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
423 vprintk(fmt, args);
424 printk("\n");
425 va_end(args);
427 if (test_opt(sb, ERRORS_PANIC))
428 panic("EXT4-fs panic from previous error\n");
430 if (sb->s_flags & MS_RDONLY)
431 return;
433 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
434 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
435 sb->s_flags |= MS_RDONLY;
436 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
437 if (EXT4_SB(sb)->s_journal)
438 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
441 void ext4_msg (struct super_block * sb, const char *prefix,
442 const char *fmt, ...)
444 va_list args;
446 va_start(args, fmt);
447 printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
448 vprintk(fmt, args);
449 printk("\n");
450 va_end(args);
453 void ext4_warning(struct super_block *sb, const char *function,
454 const char *fmt, ...)
456 va_list args;
458 va_start(args, fmt);
459 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
460 sb->s_id, function);
461 vprintk(fmt, args);
462 printk("\n");
463 va_end(args);
466 void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
467 const char *function, const char *fmt, ...)
468 __releases(bitlock)
469 __acquires(bitlock)
471 va_list args;
472 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
474 va_start(args, fmt);
475 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
476 vprintk(fmt, args);
477 printk("\n");
478 va_end(args);
480 if (test_opt(sb, ERRORS_CONT)) {
481 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
482 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
483 ext4_commit_super(sb, 0);
484 return;
486 ext4_unlock_group(sb, grp);
487 ext4_handle_error(sb);
489 * We only get here in the ERRORS_RO case; relocking the group
490 * may be dangerous, but nothing bad will happen since the
491 * filesystem will have already been marked read/only and the
492 * journal has been aborted. We return 1 as a hint to callers
493 * who might what to use the return value from
494 * ext4_grp_locked_error() to distinguish beween the
495 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
496 * aggressively from the ext4 function in question, with a
497 * more appropriate error code.
499 ext4_lock_group(sb, grp);
500 return;
503 void ext4_update_dynamic_rev(struct super_block *sb)
505 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
507 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
508 return;
510 ext4_warning(sb, __func__,
511 "updating to rev %d because of new feature flag, "
512 "running e2fsck is recommended",
513 EXT4_DYNAMIC_REV);
515 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
516 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
517 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
518 /* leave es->s_feature_*compat flags alone */
519 /* es->s_uuid will be set by e2fsck if empty */
522 * The rest of the superblock fields should be zero, and if not it
523 * means they are likely already in use, so leave them alone. We
524 * can leave it up to e2fsck to clean up any inconsistencies there.
529 * Open the external journal device
531 static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
533 struct block_device *bdev;
534 char b[BDEVNAME_SIZE];
536 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
537 if (IS_ERR(bdev))
538 goto fail;
539 return bdev;
541 fail:
542 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
543 __bdevname(dev, b), PTR_ERR(bdev));
544 return NULL;
548 * Release the journal device
550 static int ext4_blkdev_put(struct block_device *bdev)
552 bd_release(bdev);
553 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
556 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
558 struct block_device *bdev;
559 int ret = -ENODEV;
561 bdev = sbi->journal_bdev;
562 if (bdev) {
563 ret = ext4_blkdev_put(bdev);
564 sbi->journal_bdev = NULL;
566 return ret;
569 static inline struct inode *orphan_list_entry(struct list_head *l)
571 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
574 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
576 struct list_head *l;
578 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
579 le32_to_cpu(sbi->s_es->s_last_orphan));
581 printk(KERN_ERR "sb_info orphan list:\n");
582 list_for_each(l, &sbi->s_orphan) {
583 struct inode *inode = orphan_list_entry(l);
584 printk(KERN_ERR " "
585 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
586 inode->i_sb->s_id, inode->i_ino, inode,
587 inode->i_mode, inode->i_nlink,
588 NEXT_ORPHAN(inode));
592 static void ext4_put_super(struct super_block *sb)
594 struct ext4_sb_info *sbi = EXT4_SB(sb);
595 struct ext4_super_block *es = sbi->s_es;
596 int i, err;
598 flush_workqueue(sbi->dio_unwritten_wq);
599 destroy_workqueue(sbi->dio_unwritten_wq);
601 lock_super(sb);
602 lock_kernel();
603 if (sb->s_dirt)
604 ext4_commit_super(sb, 1);
606 if (sbi->s_journal) {
607 err = jbd2_journal_destroy(sbi->s_journal);
608 sbi->s_journal = NULL;
609 if (err < 0)
610 ext4_abort(sb, __func__,
611 "Couldn't clean up the journal");
614 ext4_release_system_zone(sb);
615 ext4_mb_release(sb);
616 ext4_ext_release(sb);
617 ext4_xattr_put_super(sb);
619 if (!(sb->s_flags & MS_RDONLY)) {
620 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
621 es->s_state = cpu_to_le16(sbi->s_mount_state);
622 ext4_commit_super(sb, 1);
624 if (sbi->s_proc) {
625 remove_proc_entry(sb->s_id, ext4_proc_root);
627 kobject_del(&sbi->s_kobj);
629 for (i = 0; i < sbi->s_gdb_count; i++)
630 brelse(sbi->s_group_desc[i]);
631 kfree(sbi->s_group_desc);
632 if (is_vmalloc_addr(sbi->s_flex_groups))
633 vfree(sbi->s_flex_groups);
634 else
635 kfree(sbi->s_flex_groups);
636 percpu_counter_destroy(&sbi->s_freeblocks_counter);
637 percpu_counter_destroy(&sbi->s_freeinodes_counter);
638 percpu_counter_destroy(&sbi->s_dirs_counter);
639 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
640 brelse(sbi->s_sbh);
641 #ifdef CONFIG_QUOTA
642 for (i = 0; i < MAXQUOTAS; i++)
643 kfree(sbi->s_qf_names[i]);
644 #endif
646 /* Debugging code just in case the in-memory inode orphan list
647 * isn't empty. The on-disk one can be non-empty if we've
648 * detected an error and taken the fs readonly, but the
649 * in-memory list had better be clean by this point. */
650 if (!list_empty(&sbi->s_orphan))
651 dump_orphan_list(sb, sbi);
652 J_ASSERT(list_empty(&sbi->s_orphan));
654 invalidate_bdev(sb->s_bdev);
655 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
657 * Invalidate the journal device's buffers. We don't want them
658 * floating about in memory - the physical journal device may
659 * hotswapped, and it breaks the `ro-after' testing code.
661 sync_blockdev(sbi->journal_bdev);
662 invalidate_bdev(sbi->journal_bdev);
663 ext4_blkdev_remove(sbi);
665 sb->s_fs_info = NULL;
667 * Now that we are completely done shutting down the
668 * superblock, we need to actually destroy the kobject.
670 unlock_kernel();
671 unlock_super(sb);
672 kobject_put(&sbi->s_kobj);
673 wait_for_completion(&sbi->s_kobj_unregister);
674 kfree(sbi->s_blockgroup_lock);
675 kfree(sbi);
678 static struct kmem_cache *ext4_inode_cachep;
681 * Called inside transaction, so use GFP_NOFS
683 static struct inode *ext4_alloc_inode(struct super_block *sb)
685 struct ext4_inode_info *ei;
687 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
688 if (!ei)
689 return NULL;
691 ei->vfs_inode.i_version = 1;
692 ei->vfs_inode.i_data.writeback_index = 0;
693 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
694 INIT_LIST_HEAD(&ei->i_prealloc_list);
695 spin_lock_init(&ei->i_prealloc_lock);
697 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
698 * therefore it can be null here. Don't check it, just initialize
699 * jinode.
701 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
702 ei->i_reserved_data_blocks = 0;
703 ei->i_reserved_meta_blocks = 0;
704 ei->i_allocated_meta_blocks = 0;
705 ei->i_delalloc_reserved_flag = 0;
706 spin_lock_init(&(ei->i_block_reservation_lock));
707 #ifdef CONFIG_QUOTA
708 ei->i_reserved_quota = 0;
709 #endif
710 INIT_LIST_HEAD(&ei->i_aio_dio_complete_list);
711 ei->cur_aio_dio = NULL;
712 ei->i_sync_tid = 0;
713 ei->i_datasync_tid = 0;
715 return &ei->vfs_inode;
718 static void ext4_destroy_inode(struct inode *inode)
720 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
721 ext4_msg(inode->i_sb, KERN_ERR,
722 "Inode %lu (%p): orphan list check failed!",
723 inode->i_ino, EXT4_I(inode));
724 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
725 EXT4_I(inode), sizeof(struct ext4_inode_info),
726 true);
727 dump_stack();
729 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
732 static void init_once(void *foo)
734 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
736 INIT_LIST_HEAD(&ei->i_orphan);
737 #ifdef CONFIG_EXT4_FS_XATTR
738 init_rwsem(&ei->xattr_sem);
739 #endif
740 init_rwsem(&ei->i_data_sem);
741 inode_init_once(&ei->vfs_inode);
744 static int init_inodecache(void)
746 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
747 sizeof(struct ext4_inode_info),
748 0, (SLAB_RECLAIM_ACCOUNT|
749 SLAB_MEM_SPREAD),
750 init_once);
751 if (ext4_inode_cachep == NULL)
752 return -ENOMEM;
753 return 0;
756 static void destroy_inodecache(void)
758 kmem_cache_destroy(ext4_inode_cachep);
761 static void ext4_clear_inode(struct inode *inode)
763 ext4_discard_preallocations(inode);
764 if (EXT4_JOURNAL(inode))
765 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
766 &EXT4_I(inode)->jinode);
769 static inline void ext4_show_quota_options(struct seq_file *seq,
770 struct super_block *sb)
772 #if defined(CONFIG_QUOTA)
773 struct ext4_sb_info *sbi = EXT4_SB(sb);
775 if (sbi->s_jquota_fmt)
776 seq_printf(seq, ",jqfmt=%s",
777 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
779 if (sbi->s_qf_names[USRQUOTA])
780 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
782 if (sbi->s_qf_names[GRPQUOTA])
783 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
785 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
786 seq_puts(seq, ",usrquota");
788 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
789 seq_puts(seq, ",grpquota");
790 #endif
794 * Show an option if
795 * - it's set to a non-default value OR
796 * - if the per-sb default is different from the global default
798 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
800 int def_errors;
801 unsigned long def_mount_opts;
802 struct super_block *sb = vfs->mnt_sb;
803 struct ext4_sb_info *sbi = EXT4_SB(sb);
804 struct ext4_super_block *es = sbi->s_es;
806 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
807 def_errors = le16_to_cpu(es->s_errors);
809 if (sbi->s_sb_block != 1)
810 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
811 if (test_opt(sb, MINIX_DF))
812 seq_puts(seq, ",minixdf");
813 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
814 seq_puts(seq, ",grpid");
815 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
816 seq_puts(seq, ",nogrpid");
817 if (sbi->s_resuid != EXT4_DEF_RESUID ||
818 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
819 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
821 if (sbi->s_resgid != EXT4_DEF_RESGID ||
822 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
823 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
825 if (test_opt(sb, ERRORS_RO)) {
826 if (def_errors == EXT4_ERRORS_PANIC ||
827 def_errors == EXT4_ERRORS_CONTINUE) {
828 seq_puts(seq, ",errors=remount-ro");
831 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
832 seq_puts(seq, ",errors=continue");
833 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
834 seq_puts(seq, ",errors=panic");
835 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
836 seq_puts(seq, ",nouid32");
837 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
838 seq_puts(seq, ",debug");
839 if (test_opt(sb, OLDALLOC))
840 seq_puts(seq, ",oldalloc");
841 #ifdef CONFIG_EXT4_FS_XATTR
842 if (test_opt(sb, XATTR_USER) &&
843 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
844 seq_puts(seq, ",user_xattr");
845 if (!test_opt(sb, XATTR_USER) &&
846 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
847 seq_puts(seq, ",nouser_xattr");
849 #endif
850 #ifdef CONFIG_EXT4_FS_POSIX_ACL
851 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
852 seq_puts(seq, ",acl");
853 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
854 seq_puts(seq, ",noacl");
855 #endif
856 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
857 seq_printf(seq, ",commit=%u",
858 (unsigned) (sbi->s_commit_interval / HZ));
860 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
861 seq_printf(seq, ",min_batch_time=%u",
862 (unsigned) sbi->s_min_batch_time);
864 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
865 seq_printf(seq, ",max_batch_time=%u",
866 (unsigned) sbi->s_min_batch_time);
870 * We're changing the default of barrier mount option, so
871 * let's always display its mount state so it's clear what its
872 * status is.
874 seq_puts(seq, ",barrier=");
875 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
876 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
877 seq_puts(seq, ",journal_async_commit");
878 if (test_opt(sb, NOBH))
879 seq_puts(seq, ",nobh");
880 if (test_opt(sb, I_VERSION))
881 seq_puts(seq, ",i_version");
882 if (!test_opt(sb, DELALLOC))
883 seq_puts(seq, ",nodelalloc");
886 if (sbi->s_stripe)
887 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
889 * journal mode get enabled in different ways
890 * So just print the value even if we didn't specify it
892 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
893 seq_puts(seq, ",data=journal");
894 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
895 seq_puts(seq, ",data=ordered");
896 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
897 seq_puts(seq, ",data=writeback");
899 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
900 seq_printf(seq, ",inode_readahead_blks=%u",
901 sbi->s_inode_readahead_blks);
903 if (test_opt(sb, DATA_ERR_ABORT))
904 seq_puts(seq, ",data_err=abort");
906 if (test_opt(sb, NO_AUTO_DA_ALLOC))
907 seq_puts(seq, ",noauto_da_alloc");
909 if (test_opt(sb, DISCARD))
910 seq_puts(seq, ",discard");
912 if (test_opt(sb, NOLOAD))
913 seq_puts(seq, ",norecovery");
915 ext4_show_quota_options(seq, sb);
917 return 0;
920 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
921 u64 ino, u32 generation)
923 struct inode *inode;
925 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
926 return ERR_PTR(-ESTALE);
927 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
928 return ERR_PTR(-ESTALE);
930 /* iget isn't really right if the inode is currently unallocated!!
932 * ext4_read_inode will return a bad_inode if the inode had been
933 * deleted, so we should be safe.
935 * Currently we don't know the generation for parent directory, so
936 * a generation of 0 means "accept any"
938 inode = ext4_iget(sb, ino);
939 if (IS_ERR(inode))
940 return ERR_CAST(inode);
941 if (generation && inode->i_generation != generation) {
942 iput(inode);
943 return ERR_PTR(-ESTALE);
946 return inode;
949 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
950 int fh_len, int fh_type)
952 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
953 ext4_nfs_get_inode);
956 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
957 int fh_len, int fh_type)
959 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
960 ext4_nfs_get_inode);
964 * Try to release metadata pages (indirect blocks, directories) which are
965 * mapped via the block device. Since these pages could have journal heads
966 * which would prevent try_to_free_buffers() from freeing them, we must use
967 * jbd2 layer's try_to_free_buffers() function to release them.
969 static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
970 gfp_t wait)
972 journal_t *journal = EXT4_SB(sb)->s_journal;
974 WARN_ON(PageChecked(page));
975 if (!page_has_buffers(page))
976 return 0;
977 if (journal)
978 return jbd2_journal_try_to_free_buffers(journal, page,
979 wait & ~__GFP_WAIT);
980 return try_to_free_buffers(page);
983 #ifdef CONFIG_QUOTA
984 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
985 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
987 static int ext4_write_dquot(struct dquot *dquot);
988 static int ext4_acquire_dquot(struct dquot *dquot);
989 static int ext4_release_dquot(struct dquot *dquot);
990 static int ext4_mark_dquot_dirty(struct dquot *dquot);
991 static int ext4_write_info(struct super_block *sb, int type);
992 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
993 char *path, int remount);
994 static int ext4_quota_on_mount(struct super_block *sb, int type);
995 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
996 size_t len, loff_t off);
997 static ssize_t ext4_quota_write(struct super_block *sb, int type,
998 const char *data, size_t len, loff_t off);
1000 static const struct dquot_operations ext4_quota_operations = {
1001 .initialize = dquot_initialize,
1002 .drop = dquot_drop,
1003 .alloc_space = dquot_alloc_space,
1004 .reserve_space = dquot_reserve_space,
1005 .claim_space = dquot_claim_space,
1006 .release_rsv = dquot_release_reserved_space,
1007 #ifdef CONFIG_QUOTA
1008 .get_reserved_space = ext4_get_reserved_space,
1009 #endif
1010 .alloc_inode = dquot_alloc_inode,
1011 .free_space = dquot_free_space,
1012 .free_inode = dquot_free_inode,
1013 .transfer = dquot_transfer,
1014 .write_dquot = ext4_write_dquot,
1015 .acquire_dquot = ext4_acquire_dquot,
1016 .release_dquot = ext4_release_dquot,
1017 .mark_dirty = ext4_mark_dquot_dirty,
1018 .write_info = ext4_write_info,
1019 .alloc_dquot = dquot_alloc,
1020 .destroy_dquot = dquot_destroy,
1023 static const struct quotactl_ops ext4_qctl_operations = {
1024 .quota_on = ext4_quota_on,
1025 .quota_off = vfs_quota_off,
1026 .quota_sync = vfs_quota_sync,
1027 .get_info = vfs_get_dqinfo,
1028 .set_info = vfs_set_dqinfo,
1029 .get_dqblk = vfs_get_dqblk,
1030 .set_dqblk = vfs_set_dqblk
1032 #endif
1034 static const struct super_operations ext4_sops = {
1035 .alloc_inode = ext4_alloc_inode,
1036 .destroy_inode = ext4_destroy_inode,
1037 .write_inode = ext4_write_inode,
1038 .dirty_inode = ext4_dirty_inode,
1039 .delete_inode = ext4_delete_inode,
1040 .put_super = ext4_put_super,
1041 .sync_fs = ext4_sync_fs,
1042 .freeze_fs = ext4_freeze,
1043 .unfreeze_fs = ext4_unfreeze,
1044 .statfs = ext4_statfs,
1045 .remount_fs = ext4_remount,
1046 .clear_inode = ext4_clear_inode,
1047 .show_options = ext4_show_options,
1048 #ifdef CONFIG_QUOTA
1049 .quota_read = ext4_quota_read,
1050 .quota_write = ext4_quota_write,
1051 #endif
1052 .bdev_try_to_free_page = bdev_try_to_free_page,
1055 static const struct super_operations ext4_nojournal_sops = {
1056 .alloc_inode = ext4_alloc_inode,
1057 .destroy_inode = ext4_destroy_inode,
1058 .write_inode = ext4_write_inode,
1059 .dirty_inode = ext4_dirty_inode,
1060 .delete_inode = ext4_delete_inode,
1061 .write_super = ext4_write_super,
1062 .put_super = ext4_put_super,
1063 .statfs = ext4_statfs,
1064 .remount_fs = ext4_remount,
1065 .clear_inode = ext4_clear_inode,
1066 .show_options = ext4_show_options,
1067 #ifdef CONFIG_QUOTA
1068 .quota_read = ext4_quota_read,
1069 .quota_write = ext4_quota_write,
1070 #endif
1071 .bdev_try_to_free_page = bdev_try_to_free_page,
1074 static const struct export_operations ext4_export_ops = {
1075 .fh_to_dentry = ext4_fh_to_dentry,
1076 .fh_to_parent = ext4_fh_to_parent,
1077 .get_parent = ext4_get_parent,
1080 enum {
1081 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1082 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
1083 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
1084 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1085 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
1086 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1087 Opt_journal_update, Opt_journal_dev,
1088 Opt_journal_checksum, Opt_journal_async_commit,
1089 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1090 Opt_data_err_abort, Opt_data_err_ignore,
1091 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1092 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
1093 Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
1094 Opt_usrquota, Opt_grpquota, Opt_i_version,
1095 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
1096 Opt_block_validity, Opt_noblock_validity,
1097 Opt_inode_readahead_blks, Opt_journal_ioprio,
1098 Opt_discard, Opt_nodiscard,
1101 static const match_table_t tokens = {
1102 {Opt_bsd_df, "bsddf"},
1103 {Opt_minix_df, "minixdf"},
1104 {Opt_grpid, "grpid"},
1105 {Opt_grpid, "bsdgroups"},
1106 {Opt_nogrpid, "nogrpid"},
1107 {Opt_nogrpid, "sysvgroups"},
1108 {Opt_resgid, "resgid=%u"},
1109 {Opt_resuid, "resuid=%u"},
1110 {Opt_sb, "sb=%u"},
1111 {Opt_err_cont, "errors=continue"},
1112 {Opt_err_panic, "errors=panic"},
1113 {Opt_err_ro, "errors=remount-ro"},
1114 {Opt_nouid32, "nouid32"},
1115 {Opt_debug, "debug"},
1116 {Opt_oldalloc, "oldalloc"},
1117 {Opt_orlov, "orlov"},
1118 {Opt_user_xattr, "user_xattr"},
1119 {Opt_nouser_xattr, "nouser_xattr"},
1120 {Opt_acl, "acl"},
1121 {Opt_noacl, "noacl"},
1122 {Opt_noload, "noload"},
1123 {Opt_noload, "norecovery"},
1124 {Opt_nobh, "nobh"},
1125 {Opt_bh, "bh"},
1126 {Opt_commit, "commit=%u"},
1127 {Opt_min_batch_time, "min_batch_time=%u"},
1128 {Opt_max_batch_time, "max_batch_time=%u"},
1129 {Opt_journal_update, "journal=update"},
1130 {Opt_journal_dev, "journal_dev=%u"},
1131 {Opt_journal_checksum, "journal_checksum"},
1132 {Opt_journal_async_commit, "journal_async_commit"},
1133 {Opt_abort, "abort"},
1134 {Opt_data_journal, "data=journal"},
1135 {Opt_data_ordered, "data=ordered"},
1136 {Opt_data_writeback, "data=writeback"},
1137 {Opt_data_err_abort, "data_err=abort"},
1138 {Opt_data_err_ignore, "data_err=ignore"},
1139 {Opt_offusrjquota, "usrjquota="},
1140 {Opt_usrjquota, "usrjquota=%s"},
1141 {Opt_offgrpjquota, "grpjquota="},
1142 {Opt_grpjquota, "grpjquota=%s"},
1143 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1144 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1145 {Opt_grpquota, "grpquota"},
1146 {Opt_noquota, "noquota"},
1147 {Opt_quota, "quota"},
1148 {Opt_usrquota, "usrquota"},
1149 {Opt_barrier, "barrier=%u"},
1150 {Opt_barrier, "barrier"},
1151 {Opt_nobarrier, "nobarrier"},
1152 {Opt_i_version, "i_version"},
1153 {Opt_stripe, "stripe=%u"},
1154 {Opt_resize, "resize"},
1155 {Opt_delalloc, "delalloc"},
1156 {Opt_nodelalloc, "nodelalloc"},
1157 {Opt_block_validity, "block_validity"},
1158 {Opt_noblock_validity, "noblock_validity"},
1159 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1160 {Opt_journal_ioprio, "journal_ioprio=%u"},
1161 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
1162 {Opt_auto_da_alloc, "auto_da_alloc"},
1163 {Opt_noauto_da_alloc, "noauto_da_alloc"},
1164 {Opt_discard, "discard"},
1165 {Opt_nodiscard, "nodiscard"},
1166 {Opt_err, NULL},
1169 static ext4_fsblk_t get_sb_block(void **data)
1171 ext4_fsblk_t sb_block;
1172 char *options = (char *) *data;
1174 if (!options || strncmp(options, "sb=", 3) != 0)
1175 return 1; /* Default location */
1177 options += 3;
1178 /* TODO: use simple_strtoll with >32bit ext4 */
1179 sb_block = simple_strtoul(options, &options, 0);
1180 if (*options && *options != ',') {
1181 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1182 (char *) *data);
1183 return 1;
1185 if (*options == ',')
1186 options++;
1187 *data = (void *) options;
1189 return sb_block;
1192 #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1194 static int parse_options(char *options, struct super_block *sb,
1195 unsigned long *journal_devnum,
1196 unsigned int *journal_ioprio,
1197 ext4_fsblk_t *n_blocks_count, int is_remount)
1199 struct ext4_sb_info *sbi = EXT4_SB(sb);
1200 char *p;
1201 substring_t args[MAX_OPT_ARGS];
1202 int data_opt = 0;
1203 int option;
1204 #ifdef CONFIG_QUOTA
1205 int qtype, qfmt;
1206 char *qname;
1207 #endif
1209 if (!options)
1210 return 1;
1212 while ((p = strsep(&options, ",")) != NULL) {
1213 int token;
1214 if (!*p)
1215 continue;
1217 token = match_token(p, tokens, args);
1218 switch (token) {
1219 case Opt_bsd_df:
1220 clear_opt(sbi->s_mount_opt, MINIX_DF);
1221 break;
1222 case Opt_minix_df:
1223 set_opt(sbi->s_mount_opt, MINIX_DF);
1224 break;
1225 case Opt_grpid:
1226 set_opt(sbi->s_mount_opt, GRPID);
1227 break;
1228 case Opt_nogrpid:
1229 clear_opt(sbi->s_mount_opt, GRPID);
1230 break;
1231 case Opt_resuid:
1232 if (match_int(&args[0], &option))
1233 return 0;
1234 sbi->s_resuid = option;
1235 break;
1236 case Opt_resgid:
1237 if (match_int(&args[0], &option))
1238 return 0;
1239 sbi->s_resgid = option;
1240 break;
1241 case Opt_sb:
1242 /* handled by get_sb_block() instead of here */
1243 /* *sb_block = match_int(&args[0]); */
1244 break;
1245 case Opt_err_panic:
1246 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1247 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1248 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1249 break;
1250 case Opt_err_ro:
1251 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1252 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1253 set_opt(sbi->s_mount_opt, ERRORS_RO);
1254 break;
1255 case Opt_err_cont:
1256 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1257 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1258 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1259 break;
1260 case Opt_nouid32:
1261 set_opt(sbi->s_mount_opt, NO_UID32);
1262 break;
1263 case Opt_debug:
1264 set_opt(sbi->s_mount_opt, DEBUG);
1265 break;
1266 case Opt_oldalloc:
1267 set_opt(sbi->s_mount_opt, OLDALLOC);
1268 break;
1269 case Opt_orlov:
1270 clear_opt(sbi->s_mount_opt, OLDALLOC);
1271 break;
1272 #ifdef CONFIG_EXT4_FS_XATTR
1273 case Opt_user_xattr:
1274 set_opt(sbi->s_mount_opt, XATTR_USER);
1275 break;
1276 case Opt_nouser_xattr:
1277 clear_opt(sbi->s_mount_opt, XATTR_USER);
1278 break;
1279 #else
1280 case Opt_user_xattr:
1281 case Opt_nouser_xattr:
1282 ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
1283 break;
1284 #endif
1285 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1286 case Opt_acl:
1287 set_opt(sbi->s_mount_opt, POSIX_ACL);
1288 break;
1289 case Opt_noacl:
1290 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1291 break;
1292 #else
1293 case Opt_acl:
1294 case Opt_noacl:
1295 ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
1296 break;
1297 #endif
1298 case Opt_journal_update:
1299 /* @@@ FIXME */
1300 /* Eventually we will want to be able to create
1301 a journal file here. For now, only allow the
1302 user to specify an existing inode to be the
1303 journal file. */
1304 if (is_remount) {
1305 ext4_msg(sb, KERN_ERR,
1306 "Cannot specify journal on remount");
1307 return 0;
1309 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1310 break;
1311 case Opt_journal_dev:
1312 if (is_remount) {
1313 ext4_msg(sb, KERN_ERR,
1314 "Cannot specify journal on remount");
1315 return 0;
1317 if (match_int(&args[0], &option))
1318 return 0;
1319 *journal_devnum = option;
1320 break;
1321 case Opt_journal_checksum:
1322 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1323 break;
1324 case Opt_journal_async_commit:
1325 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1326 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1327 break;
1328 case Opt_noload:
1329 set_opt(sbi->s_mount_opt, NOLOAD);
1330 break;
1331 case Opt_commit:
1332 if (match_int(&args[0], &option))
1333 return 0;
1334 if (option < 0)
1335 return 0;
1336 if (option == 0)
1337 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1338 sbi->s_commit_interval = HZ * option;
1339 break;
1340 case Opt_max_batch_time:
1341 if (match_int(&args[0], &option))
1342 return 0;
1343 if (option < 0)
1344 return 0;
1345 if (option == 0)
1346 option = EXT4_DEF_MAX_BATCH_TIME;
1347 sbi->s_max_batch_time = option;
1348 break;
1349 case Opt_min_batch_time:
1350 if (match_int(&args[0], &option))
1351 return 0;
1352 if (option < 0)
1353 return 0;
1354 sbi->s_min_batch_time = option;
1355 break;
1356 case Opt_data_journal:
1357 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1358 goto datacheck;
1359 case Opt_data_ordered:
1360 data_opt = EXT4_MOUNT_ORDERED_DATA;
1361 goto datacheck;
1362 case Opt_data_writeback:
1363 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1364 datacheck:
1365 if (is_remount) {
1366 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1367 != data_opt) {
1368 ext4_msg(sb, KERN_ERR,
1369 "Cannot change data mode on remount");
1370 return 0;
1372 } else {
1373 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1374 sbi->s_mount_opt |= data_opt;
1376 break;
1377 case Opt_data_err_abort:
1378 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1379 break;
1380 case Opt_data_err_ignore:
1381 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1382 break;
1383 #ifdef CONFIG_QUOTA
1384 case Opt_usrjquota:
1385 qtype = USRQUOTA;
1386 goto set_qf_name;
1387 case Opt_grpjquota:
1388 qtype = GRPQUOTA;
1389 set_qf_name:
1390 if (sb_any_quota_loaded(sb) &&
1391 !sbi->s_qf_names[qtype]) {
1392 ext4_msg(sb, KERN_ERR,
1393 "Cannot change journaled "
1394 "quota options when quota turned on");
1395 return 0;
1397 qname = match_strdup(&args[0]);
1398 if (!qname) {
1399 ext4_msg(sb, KERN_ERR,
1400 "Not enough memory for "
1401 "storing quotafile name");
1402 return 0;
1404 if (sbi->s_qf_names[qtype] &&
1405 strcmp(sbi->s_qf_names[qtype], qname)) {
1406 ext4_msg(sb, KERN_ERR,
1407 "%s quota file already "
1408 "specified", QTYPE2NAME(qtype));
1409 kfree(qname);
1410 return 0;
1412 sbi->s_qf_names[qtype] = qname;
1413 if (strchr(sbi->s_qf_names[qtype], '/')) {
1414 ext4_msg(sb, KERN_ERR,
1415 "quotafile must be on "
1416 "filesystem root");
1417 kfree(sbi->s_qf_names[qtype]);
1418 sbi->s_qf_names[qtype] = NULL;
1419 return 0;
1421 set_opt(sbi->s_mount_opt, QUOTA);
1422 break;
1423 case Opt_offusrjquota:
1424 qtype = USRQUOTA;
1425 goto clear_qf_name;
1426 case Opt_offgrpjquota:
1427 qtype = GRPQUOTA;
1428 clear_qf_name:
1429 if (sb_any_quota_loaded(sb) &&
1430 sbi->s_qf_names[qtype]) {
1431 ext4_msg(sb, KERN_ERR, "Cannot change "
1432 "journaled quota options when "
1433 "quota turned on");
1434 return 0;
1437 * The space will be released later when all options
1438 * are confirmed to be correct
1440 sbi->s_qf_names[qtype] = NULL;
1441 break;
1442 case Opt_jqfmt_vfsold:
1443 qfmt = QFMT_VFS_OLD;
1444 goto set_qf_format;
1445 case Opt_jqfmt_vfsv0:
1446 qfmt = QFMT_VFS_V0;
1447 set_qf_format:
1448 if (sb_any_quota_loaded(sb) &&
1449 sbi->s_jquota_fmt != qfmt) {
1450 ext4_msg(sb, KERN_ERR, "Cannot change "
1451 "journaled quota options when "
1452 "quota turned on");
1453 return 0;
1455 sbi->s_jquota_fmt = qfmt;
1456 break;
1457 case Opt_quota:
1458 case Opt_usrquota:
1459 set_opt(sbi->s_mount_opt, QUOTA);
1460 set_opt(sbi->s_mount_opt, USRQUOTA);
1461 break;
1462 case Opt_grpquota:
1463 set_opt(sbi->s_mount_opt, QUOTA);
1464 set_opt(sbi->s_mount_opt, GRPQUOTA);
1465 break;
1466 case Opt_noquota:
1467 if (sb_any_quota_loaded(sb)) {
1468 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1469 "options when quota turned on");
1470 return 0;
1472 clear_opt(sbi->s_mount_opt, QUOTA);
1473 clear_opt(sbi->s_mount_opt, USRQUOTA);
1474 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1475 break;
1476 #else
1477 case Opt_quota:
1478 case Opt_usrquota:
1479 case Opt_grpquota:
1480 ext4_msg(sb, KERN_ERR,
1481 "quota options not supported");
1482 break;
1483 case Opt_usrjquota:
1484 case Opt_grpjquota:
1485 case Opt_offusrjquota:
1486 case Opt_offgrpjquota:
1487 case Opt_jqfmt_vfsold:
1488 case Opt_jqfmt_vfsv0:
1489 ext4_msg(sb, KERN_ERR,
1490 "journaled quota options not supported");
1491 break;
1492 case Opt_noquota:
1493 break;
1494 #endif
1495 case Opt_abort:
1496 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1497 break;
1498 case Opt_nobarrier:
1499 clear_opt(sbi->s_mount_opt, BARRIER);
1500 break;
1501 case Opt_barrier:
1502 if (match_int(&args[0], &option)) {
1503 set_opt(sbi->s_mount_opt, BARRIER);
1504 break;
1506 if (option)
1507 set_opt(sbi->s_mount_opt, BARRIER);
1508 else
1509 clear_opt(sbi->s_mount_opt, BARRIER);
1510 break;
1511 case Opt_ignore:
1512 break;
1513 case Opt_resize:
1514 if (!is_remount) {
1515 ext4_msg(sb, KERN_ERR,
1516 "resize option only available "
1517 "for remount");
1518 return 0;
1520 if (match_int(&args[0], &option) != 0)
1521 return 0;
1522 *n_blocks_count = option;
1523 break;
1524 case Opt_nobh:
1525 set_opt(sbi->s_mount_opt, NOBH);
1526 break;
1527 case Opt_bh:
1528 clear_opt(sbi->s_mount_opt, NOBH);
1529 break;
1530 case Opt_i_version:
1531 set_opt(sbi->s_mount_opt, I_VERSION);
1532 sb->s_flags |= MS_I_VERSION;
1533 break;
1534 case Opt_nodelalloc:
1535 clear_opt(sbi->s_mount_opt, DELALLOC);
1536 break;
1537 case Opt_stripe:
1538 if (match_int(&args[0], &option))
1539 return 0;
1540 if (option < 0)
1541 return 0;
1542 sbi->s_stripe = option;
1543 break;
1544 case Opt_delalloc:
1545 set_opt(sbi->s_mount_opt, DELALLOC);
1546 break;
1547 case Opt_block_validity:
1548 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1549 break;
1550 case Opt_noblock_validity:
1551 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1552 break;
1553 case Opt_inode_readahead_blks:
1554 if (match_int(&args[0], &option))
1555 return 0;
1556 if (option < 0 || option > (1 << 30))
1557 return 0;
1558 if (!is_power_of_2(option)) {
1559 ext4_msg(sb, KERN_ERR,
1560 "EXT4-fs: inode_readahead_blks"
1561 " must be a power of 2");
1562 return 0;
1564 sbi->s_inode_readahead_blks = option;
1565 break;
1566 case Opt_journal_ioprio:
1567 if (match_int(&args[0], &option))
1568 return 0;
1569 if (option < 0 || option > 7)
1570 break;
1571 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1572 option);
1573 break;
1574 case Opt_noauto_da_alloc:
1575 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1576 break;
1577 case Opt_auto_da_alloc:
1578 if (match_int(&args[0], &option)) {
1579 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1580 break;
1582 if (option)
1583 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1584 else
1585 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1586 break;
1587 case Opt_discard:
1588 set_opt(sbi->s_mount_opt, DISCARD);
1589 break;
1590 case Opt_nodiscard:
1591 clear_opt(sbi->s_mount_opt, DISCARD);
1592 break;
1593 default:
1594 ext4_msg(sb, KERN_ERR,
1595 "Unrecognized mount option \"%s\" "
1596 "or missing value", p);
1597 return 0;
1600 #ifdef CONFIG_QUOTA
1601 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1602 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1603 sbi->s_qf_names[USRQUOTA])
1604 clear_opt(sbi->s_mount_opt, USRQUOTA);
1606 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1607 sbi->s_qf_names[GRPQUOTA])
1608 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1610 if ((sbi->s_qf_names[USRQUOTA] &&
1611 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1612 (sbi->s_qf_names[GRPQUOTA] &&
1613 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1614 ext4_msg(sb, KERN_ERR, "old and new quota "
1615 "format mixing");
1616 return 0;
1619 if (!sbi->s_jquota_fmt) {
1620 ext4_msg(sb, KERN_ERR, "journaled quota format "
1621 "not specified");
1622 return 0;
1624 } else {
1625 if (sbi->s_jquota_fmt) {
1626 ext4_msg(sb, KERN_ERR, "journaled quota format "
1627 "specified with no journaling "
1628 "enabled");
1629 return 0;
1632 #endif
1633 return 1;
1636 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1637 int read_only)
1639 struct ext4_sb_info *sbi = EXT4_SB(sb);
1640 int res = 0;
1642 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1643 ext4_msg(sb, KERN_ERR, "revision level too high, "
1644 "forcing read-only mode");
1645 res = MS_RDONLY;
1647 if (read_only)
1648 return res;
1649 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1650 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1651 "running e2fsck is recommended");
1652 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1653 ext4_msg(sb, KERN_WARNING,
1654 "warning: mounting fs with errors, "
1655 "running e2fsck is recommended");
1656 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1657 le16_to_cpu(es->s_mnt_count) >=
1658 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1659 ext4_msg(sb, KERN_WARNING,
1660 "warning: maximal mount count reached, "
1661 "running e2fsck is recommended");
1662 else if (le32_to_cpu(es->s_checkinterval) &&
1663 (le32_to_cpu(es->s_lastcheck) +
1664 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1665 ext4_msg(sb, KERN_WARNING,
1666 "warning: checktime reached, "
1667 "running e2fsck is recommended");
1668 if (!sbi->s_journal)
1669 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1670 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1671 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1672 le16_add_cpu(&es->s_mnt_count, 1);
1673 es->s_mtime = cpu_to_le32(get_seconds());
1674 ext4_update_dynamic_rev(sb);
1675 if (sbi->s_journal)
1676 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1678 ext4_commit_super(sb, 1);
1679 if (test_opt(sb, DEBUG))
1680 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1681 "bpg=%lu, ipg=%lu, mo=%04x]\n",
1682 sb->s_blocksize,
1683 sbi->s_groups_count,
1684 EXT4_BLOCKS_PER_GROUP(sb),
1685 EXT4_INODES_PER_GROUP(sb),
1686 sbi->s_mount_opt);
1688 return res;
1691 static int ext4_fill_flex_info(struct super_block *sb)
1693 struct ext4_sb_info *sbi = EXT4_SB(sb);
1694 struct ext4_group_desc *gdp = NULL;
1695 ext4_group_t flex_group_count;
1696 ext4_group_t flex_group;
1697 int groups_per_flex = 0;
1698 size_t size;
1699 int i;
1701 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1702 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1704 if (groups_per_flex < 2) {
1705 sbi->s_log_groups_per_flex = 0;
1706 return 1;
1709 /* We allocate both existing and potentially added groups */
1710 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1711 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1712 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1713 size = flex_group_count * sizeof(struct flex_groups);
1714 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1715 if (sbi->s_flex_groups == NULL) {
1716 sbi->s_flex_groups = vmalloc(size);
1717 if (sbi->s_flex_groups)
1718 memset(sbi->s_flex_groups, 0, size);
1720 if (sbi->s_flex_groups == NULL) {
1721 ext4_msg(sb, KERN_ERR, "not enough memory for "
1722 "%u flex groups", flex_group_count);
1723 goto failed;
1726 for (i = 0; i < sbi->s_groups_count; i++) {
1727 gdp = ext4_get_group_desc(sb, i, NULL);
1729 flex_group = ext4_flex_group(sbi, i);
1730 atomic_add(ext4_free_inodes_count(sb, gdp),
1731 &sbi->s_flex_groups[flex_group].free_inodes);
1732 atomic_add(ext4_free_blks_count(sb, gdp),
1733 &sbi->s_flex_groups[flex_group].free_blocks);
1734 atomic_add(ext4_used_dirs_count(sb, gdp),
1735 &sbi->s_flex_groups[flex_group].used_dirs);
1738 return 1;
1739 failed:
1740 return 0;
1743 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1744 struct ext4_group_desc *gdp)
1746 __u16 crc = 0;
1748 if (sbi->s_es->s_feature_ro_compat &
1749 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1750 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1751 __le32 le_group = cpu_to_le32(block_group);
1753 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1754 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1755 crc = crc16(crc, (__u8 *)gdp, offset);
1756 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1757 /* for checksum of struct ext4_group_desc do the rest...*/
1758 if ((sbi->s_es->s_feature_incompat &
1759 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1760 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1761 crc = crc16(crc, (__u8 *)gdp + offset,
1762 le16_to_cpu(sbi->s_es->s_desc_size) -
1763 offset);
1766 return cpu_to_le16(crc);
1769 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1770 struct ext4_group_desc *gdp)
1772 if ((sbi->s_es->s_feature_ro_compat &
1773 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1774 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1775 return 0;
1777 return 1;
1780 /* Called at mount-time, super-block is locked */
1781 static int ext4_check_descriptors(struct super_block *sb)
1783 struct ext4_sb_info *sbi = EXT4_SB(sb);
1784 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1785 ext4_fsblk_t last_block;
1786 ext4_fsblk_t block_bitmap;
1787 ext4_fsblk_t inode_bitmap;
1788 ext4_fsblk_t inode_table;
1789 int flexbg_flag = 0;
1790 ext4_group_t i;
1792 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1793 flexbg_flag = 1;
1795 ext4_debug("Checking group descriptors");
1797 for (i = 0; i < sbi->s_groups_count; i++) {
1798 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1800 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1801 last_block = ext4_blocks_count(sbi->s_es) - 1;
1802 else
1803 last_block = first_block +
1804 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1806 block_bitmap = ext4_block_bitmap(sb, gdp);
1807 if (block_bitmap < first_block || block_bitmap > last_block) {
1808 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1809 "Block bitmap for group %u not in group "
1810 "(block %llu)!", i, block_bitmap);
1811 return 0;
1813 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1814 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1815 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1816 "Inode bitmap for group %u not in group "
1817 "(block %llu)!", i, inode_bitmap);
1818 return 0;
1820 inode_table = ext4_inode_table(sb, gdp);
1821 if (inode_table < first_block ||
1822 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1823 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1824 "Inode table for group %u not in group "
1825 "(block %llu)!", i, inode_table);
1826 return 0;
1828 ext4_lock_group(sb, i);
1829 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1830 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1831 "Checksum for group %u failed (%u!=%u)",
1832 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1833 gdp)), le16_to_cpu(gdp->bg_checksum));
1834 if (!(sb->s_flags & MS_RDONLY)) {
1835 ext4_unlock_group(sb, i);
1836 return 0;
1839 ext4_unlock_group(sb, i);
1840 if (!flexbg_flag)
1841 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1844 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1845 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
1846 return 1;
1849 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1850 * the superblock) which were deleted from all directories, but held open by
1851 * a process at the time of a crash. We walk the list and try to delete these
1852 * inodes at recovery time (only with a read-write filesystem).
1854 * In order to keep the orphan inode chain consistent during traversal (in
1855 * case of crash during recovery), we link each inode into the superblock
1856 * orphan list_head and handle it the same way as an inode deletion during
1857 * normal operation (which journals the operations for us).
1859 * We only do an iget() and an iput() on each inode, which is very safe if we
1860 * accidentally point at an in-use or already deleted inode. The worst that
1861 * can happen in this case is that we get a "bit already cleared" message from
1862 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1863 * e2fsck was run on this filesystem, and it must have already done the orphan
1864 * inode cleanup for us, so we can safely abort without any further action.
1866 static void ext4_orphan_cleanup(struct super_block *sb,
1867 struct ext4_super_block *es)
1869 unsigned int s_flags = sb->s_flags;
1870 int nr_orphans = 0, nr_truncates = 0;
1871 #ifdef CONFIG_QUOTA
1872 int i;
1873 #endif
1874 if (!es->s_last_orphan) {
1875 jbd_debug(4, "no orphan inodes to clean up\n");
1876 return;
1879 if (bdev_read_only(sb->s_bdev)) {
1880 ext4_msg(sb, KERN_ERR, "write access "
1881 "unavailable, skipping orphan cleanup");
1882 return;
1885 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1886 if (es->s_last_orphan)
1887 jbd_debug(1, "Errors on filesystem, "
1888 "clearing orphan list.\n");
1889 es->s_last_orphan = 0;
1890 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1891 return;
1894 if (s_flags & MS_RDONLY) {
1895 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1896 sb->s_flags &= ~MS_RDONLY;
1898 #ifdef CONFIG_QUOTA
1899 /* Needed for iput() to work correctly and not trash data */
1900 sb->s_flags |= MS_ACTIVE;
1901 /* Turn on quotas so that they are updated correctly */
1902 for (i = 0; i < MAXQUOTAS; i++) {
1903 if (EXT4_SB(sb)->s_qf_names[i]) {
1904 int ret = ext4_quota_on_mount(sb, i);
1905 if (ret < 0)
1906 ext4_msg(sb, KERN_ERR,
1907 "Cannot turn on journaled "
1908 "quota: error %d", ret);
1911 #endif
1913 while (es->s_last_orphan) {
1914 struct inode *inode;
1916 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1917 if (IS_ERR(inode)) {
1918 es->s_last_orphan = 0;
1919 break;
1922 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1923 vfs_dq_init(inode);
1924 if (inode->i_nlink) {
1925 ext4_msg(sb, KERN_DEBUG,
1926 "%s: truncating inode %lu to %lld bytes",
1927 __func__, inode->i_ino, inode->i_size);
1928 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1929 inode->i_ino, inode->i_size);
1930 ext4_truncate(inode);
1931 nr_truncates++;
1932 } else {
1933 ext4_msg(sb, KERN_DEBUG,
1934 "%s: deleting unreferenced inode %lu",
1935 __func__, inode->i_ino);
1936 jbd_debug(2, "deleting unreferenced inode %lu\n",
1937 inode->i_ino);
1938 nr_orphans++;
1940 iput(inode); /* The delete magic happens here! */
1943 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1945 if (nr_orphans)
1946 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1947 PLURAL(nr_orphans));
1948 if (nr_truncates)
1949 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1950 PLURAL(nr_truncates));
1951 #ifdef CONFIG_QUOTA
1952 /* Turn quotas off */
1953 for (i = 0; i < MAXQUOTAS; i++) {
1954 if (sb_dqopt(sb)->files[i])
1955 vfs_quota_off(sb, i, 0);
1957 #endif
1958 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1962 * Maximal extent format file size.
1963 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1964 * extent format containers, within a sector_t, and within i_blocks
1965 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1966 * so that won't be a limiting factor.
1968 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1970 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1972 loff_t res;
1973 loff_t upper_limit = MAX_LFS_FILESIZE;
1975 /* small i_blocks in vfs inode? */
1976 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1978 * CONFIG_LBDAF is not enabled implies the inode
1979 * i_block represent total blocks in 512 bytes
1980 * 32 == size of vfs inode i_blocks * 8
1982 upper_limit = (1LL << 32) - 1;
1984 /* total blocks in file system block size */
1985 upper_limit >>= (blkbits - 9);
1986 upper_limit <<= blkbits;
1989 /* 32-bit extent-start container, ee_block */
1990 res = 1LL << 32;
1991 res <<= blkbits;
1992 res -= 1;
1994 /* Sanity check against vm- & vfs- imposed limits */
1995 if (res > upper_limit)
1996 res = upper_limit;
1998 return res;
2002 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
2003 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2004 * We need to be 1 filesystem block less than the 2^48 sector limit.
2006 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2008 loff_t res = EXT4_NDIR_BLOCKS;
2009 int meta_blocks;
2010 loff_t upper_limit;
2011 /* This is calculated to be the largest file size for a dense, block
2012 * mapped file such that the file's total number of 512-byte sectors,
2013 * including data and all indirect blocks, does not exceed (2^48 - 1).
2015 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2016 * number of 512-byte sectors of the file.
2019 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2021 * !has_huge_files or CONFIG_LBDAF not enabled implies that
2022 * the inode i_block field represents total file blocks in
2023 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
2025 upper_limit = (1LL << 32) - 1;
2027 /* total blocks in file system block size */
2028 upper_limit >>= (bits - 9);
2030 } else {
2032 * We use 48 bit ext4_inode i_blocks
2033 * With EXT4_HUGE_FILE_FL set the i_blocks
2034 * represent total number of blocks in
2035 * file system block size
2037 upper_limit = (1LL << 48) - 1;
2041 /* indirect blocks */
2042 meta_blocks = 1;
2043 /* double indirect blocks */
2044 meta_blocks += 1 + (1LL << (bits-2));
2045 /* tripple indirect blocks */
2046 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2048 upper_limit -= meta_blocks;
2049 upper_limit <<= bits;
2051 res += 1LL << (bits-2);
2052 res += 1LL << (2*(bits-2));
2053 res += 1LL << (3*(bits-2));
2054 res <<= bits;
2055 if (res > upper_limit)
2056 res = upper_limit;
2058 if (res > MAX_LFS_FILESIZE)
2059 res = MAX_LFS_FILESIZE;
2061 return res;
2064 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
2065 ext4_fsblk_t logical_sb_block, int nr)
2067 struct ext4_sb_info *sbi = EXT4_SB(sb);
2068 ext4_group_t bg, first_meta_bg;
2069 int has_super = 0;
2071 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2073 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2074 nr < first_meta_bg)
2075 return logical_sb_block + nr + 1;
2076 bg = sbi->s_desc_per_block * nr;
2077 if (ext4_bg_has_super(sb, bg))
2078 has_super = 1;
2080 return (has_super + ext4_group_first_block_no(sb, bg));
2084 * ext4_get_stripe_size: Get the stripe size.
2085 * @sbi: In memory super block info
2087 * If we have specified it via mount option, then
2088 * use the mount option value. If the value specified at mount time is
2089 * greater than the blocks per group use the super block value.
2090 * If the super block value is greater than blocks per group return 0.
2091 * Allocator needs it be less than blocks per group.
2094 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2096 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2097 unsigned long stripe_width =
2098 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2100 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2101 return sbi->s_stripe;
2103 if (stripe_width <= sbi->s_blocks_per_group)
2104 return stripe_width;
2106 if (stride <= sbi->s_blocks_per_group)
2107 return stride;
2109 return 0;
2112 /* sysfs supprt */
2114 struct ext4_attr {
2115 struct attribute attr;
2116 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2117 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2118 const char *, size_t);
2119 int offset;
2122 static int parse_strtoul(const char *buf,
2123 unsigned long max, unsigned long *value)
2125 char *endp;
2127 while (*buf && isspace(*buf))
2128 buf++;
2129 *value = simple_strtoul(buf, &endp, 0);
2130 while (*endp && isspace(*endp))
2131 endp++;
2132 if (*endp || *value > max)
2133 return -EINVAL;
2135 return 0;
2138 static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2139 struct ext4_sb_info *sbi,
2140 char *buf)
2142 return snprintf(buf, PAGE_SIZE, "%llu\n",
2143 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2146 static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2147 struct ext4_sb_info *sbi, char *buf)
2149 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2151 return snprintf(buf, PAGE_SIZE, "%lu\n",
2152 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2153 sbi->s_sectors_written_start) >> 1);
2156 static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2157 struct ext4_sb_info *sbi, char *buf)
2159 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2161 return snprintf(buf, PAGE_SIZE, "%llu\n",
2162 sbi->s_kbytes_written +
2163 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2164 EXT4_SB(sb)->s_sectors_written_start) >> 1));
2167 static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2168 struct ext4_sb_info *sbi,
2169 const char *buf, size_t count)
2171 unsigned long t;
2173 if (parse_strtoul(buf, 0x40000000, &t))
2174 return -EINVAL;
2176 if (!is_power_of_2(t))
2177 return -EINVAL;
2179 sbi->s_inode_readahead_blks = t;
2180 return count;
2183 static ssize_t sbi_ui_show(struct ext4_attr *a,
2184 struct ext4_sb_info *sbi, char *buf)
2186 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2188 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2191 static ssize_t sbi_ui_store(struct ext4_attr *a,
2192 struct ext4_sb_info *sbi,
2193 const char *buf, size_t count)
2195 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2196 unsigned long t;
2198 if (parse_strtoul(buf, 0xffffffff, &t))
2199 return -EINVAL;
2200 *ui = t;
2201 return count;
2204 #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2205 static struct ext4_attr ext4_attr_##_name = { \
2206 .attr = {.name = __stringify(_name), .mode = _mode }, \
2207 .show = _show, \
2208 .store = _store, \
2209 .offset = offsetof(struct ext4_sb_info, _elname), \
2211 #define EXT4_ATTR(name, mode, show, store) \
2212 static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2214 #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2215 #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2216 #define EXT4_RW_ATTR_SBI_UI(name, elname) \
2217 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2218 #define ATTR_LIST(name) &ext4_attr_##name.attr
2220 EXT4_RO_ATTR(delayed_allocation_blocks);
2221 EXT4_RO_ATTR(session_write_kbytes);
2222 EXT4_RO_ATTR(lifetime_write_kbytes);
2223 EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2224 inode_readahead_blks_store, s_inode_readahead_blks);
2225 EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
2226 EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2227 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2228 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2229 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2230 EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2231 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2232 EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
2234 static struct attribute *ext4_attrs[] = {
2235 ATTR_LIST(delayed_allocation_blocks),
2236 ATTR_LIST(session_write_kbytes),
2237 ATTR_LIST(lifetime_write_kbytes),
2238 ATTR_LIST(inode_readahead_blks),
2239 ATTR_LIST(inode_goal),
2240 ATTR_LIST(mb_stats),
2241 ATTR_LIST(mb_max_to_scan),
2242 ATTR_LIST(mb_min_to_scan),
2243 ATTR_LIST(mb_order2_req),
2244 ATTR_LIST(mb_stream_req),
2245 ATTR_LIST(mb_group_prealloc),
2246 ATTR_LIST(max_writeback_mb_bump),
2247 NULL,
2250 static ssize_t ext4_attr_show(struct kobject *kobj,
2251 struct attribute *attr, char *buf)
2253 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2254 s_kobj);
2255 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2257 return a->show ? a->show(a, sbi, buf) : 0;
2260 static ssize_t ext4_attr_store(struct kobject *kobj,
2261 struct attribute *attr,
2262 const char *buf, size_t len)
2264 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2265 s_kobj);
2266 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2268 return a->store ? a->store(a, sbi, buf, len) : 0;
2271 static void ext4_sb_release(struct kobject *kobj)
2273 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2274 s_kobj);
2275 complete(&sbi->s_kobj_unregister);
2279 static struct sysfs_ops ext4_attr_ops = {
2280 .show = ext4_attr_show,
2281 .store = ext4_attr_store,
2284 static struct kobj_type ext4_ktype = {
2285 .default_attrs = ext4_attrs,
2286 .sysfs_ops = &ext4_attr_ops,
2287 .release = ext4_sb_release,
2291 * Check whether this filesystem can be mounted based on
2292 * the features present and the RDONLY/RDWR mount requested.
2293 * Returns 1 if this filesystem can be mounted as requested,
2294 * 0 if it cannot be.
2296 static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2298 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2299 ext4_msg(sb, KERN_ERR,
2300 "Couldn't mount because of "
2301 "unsupported optional features (%x)",
2302 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2303 ~EXT4_FEATURE_INCOMPAT_SUPP));
2304 return 0;
2307 if (readonly)
2308 return 1;
2310 /* Check that feature set is OK for a read-write mount */
2311 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2312 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2313 "unsupported optional features (%x)",
2314 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2315 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2316 return 0;
2319 * Large file size enabled file system can only be mounted
2320 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2322 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2323 if (sizeof(blkcnt_t) < sizeof(u64)) {
2324 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2325 "cannot be mounted RDWR without "
2326 "CONFIG_LBDAF");
2327 return 0;
2330 return 1;
2333 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
2334 __releases(kernel_lock)
2335 __acquires(kernel_lock)
2337 struct buffer_head *bh;
2338 struct ext4_super_block *es = NULL;
2339 struct ext4_sb_info *sbi;
2340 ext4_fsblk_t block;
2341 ext4_fsblk_t sb_block = get_sb_block(&data);
2342 ext4_fsblk_t logical_sb_block;
2343 unsigned long offset = 0;
2344 unsigned long journal_devnum = 0;
2345 unsigned long def_mount_opts;
2346 struct inode *root;
2347 char *cp;
2348 const char *descr;
2349 int ret = -EINVAL;
2350 int blocksize;
2351 unsigned int db_count;
2352 unsigned int i;
2353 int needs_recovery, has_huge_files;
2354 __u64 blocks_count;
2355 int err;
2356 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
2358 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2359 if (!sbi)
2360 return -ENOMEM;
2362 sbi->s_blockgroup_lock =
2363 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2364 if (!sbi->s_blockgroup_lock) {
2365 kfree(sbi);
2366 return -ENOMEM;
2368 sb->s_fs_info = sbi;
2369 sbi->s_mount_opt = 0;
2370 sbi->s_resuid = EXT4_DEF_RESUID;
2371 sbi->s_resgid = EXT4_DEF_RESGID;
2372 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
2373 sbi->s_sb_block = sb_block;
2374 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2375 sectors[1]);
2377 unlock_kernel();
2379 /* Cleanup superblock name */
2380 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2381 *cp = '!';
2383 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
2384 if (!blocksize) {
2385 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
2386 goto out_fail;
2390 * The ext4 superblock will not be buffer aligned for other than 1kB
2391 * block sizes. We need to calculate the offset from buffer start.
2393 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
2394 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2395 offset = do_div(logical_sb_block, blocksize);
2396 } else {
2397 logical_sb_block = sb_block;
2400 if (!(bh = sb_bread(sb, logical_sb_block))) {
2401 ext4_msg(sb, KERN_ERR, "unable to read superblock");
2402 goto out_fail;
2405 * Note: s_es must be initialized as soon as possible because
2406 * some ext4 macro-instructions depend on its value
2408 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2409 sbi->s_es = es;
2410 sb->s_magic = le16_to_cpu(es->s_magic);
2411 if (sb->s_magic != EXT4_SUPER_MAGIC)
2412 goto cantfind_ext4;
2413 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
2415 /* Set defaults before we parse the mount options */
2416 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
2417 if (def_mount_opts & EXT4_DEFM_DEBUG)
2418 set_opt(sbi->s_mount_opt, DEBUG);
2419 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
2420 set_opt(sbi->s_mount_opt, GRPID);
2421 if (def_mount_opts & EXT4_DEFM_UID16)
2422 set_opt(sbi->s_mount_opt, NO_UID32);
2423 #ifdef CONFIG_EXT4_FS_XATTR
2424 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
2425 set_opt(sbi->s_mount_opt, XATTR_USER);
2426 #endif
2427 #ifdef CONFIG_EXT4_FS_POSIX_ACL
2428 if (def_mount_opts & EXT4_DEFM_ACL)
2429 set_opt(sbi->s_mount_opt, POSIX_ACL);
2430 #endif
2431 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2432 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2433 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2434 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2435 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2436 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
2438 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
2439 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
2440 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
2441 set_opt(sbi->s_mount_opt, ERRORS_CONT);
2442 else
2443 set_opt(sbi->s_mount_opt, ERRORS_RO);
2445 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2446 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2447 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2448 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2449 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
2451 set_opt(sbi->s_mount_opt, BARRIER);
2454 * enable delayed allocation by default
2455 * Use -o nodelalloc to turn it off
2457 set_opt(sbi->s_mount_opt, DELALLOC);
2459 if (!parse_options((char *) data, sb, &journal_devnum,
2460 &journal_ioprio, NULL, 0))
2461 goto failed_mount;
2463 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2464 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2466 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2467 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2468 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2469 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2470 ext4_msg(sb, KERN_WARNING,
2471 "feature flags set on rev 0 fs, "
2472 "running e2fsck is recommended");
2475 * Check feature flags regardless of the revision level, since we
2476 * previously didn't change the revision level when setting the flags,
2477 * so there is a chance incompat flags are set on a rev 0 filesystem.
2479 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
2480 goto failed_mount;
2482 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2484 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2485 blocksize > EXT4_MAX_BLOCK_SIZE) {
2486 ext4_msg(sb, KERN_ERR,
2487 "Unsupported filesystem blocksize %d", blocksize);
2488 goto failed_mount;
2491 if (sb->s_blocksize != blocksize) {
2492 /* Validate the filesystem blocksize */
2493 if (!sb_set_blocksize(sb, blocksize)) {
2494 ext4_msg(sb, KERN_ERR, "bad block size %d",
2495 blocksize);
2496 goto failed_mount;
2499 brelse(bh);
2500 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2501 offset = do_div(logical_sb_block, blocksize);
2502 bh = sb_bread(sb, logical_sb_block);
2503 if (!bh) {
2504 ext4_msg(sb, KERN_ERR,
2505 "Can't read superblock on 2nd try");
2506 goto failed_mount;
2508 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2509 sbi->s_es = es;
2510 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2511 ext4_msg(sb, KERN_ERR,
2512 "Magic mismatch, very weird!");
2513 goto failed_mount;
2517 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2518 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2519 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2520 has_huge_files);
2521 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2523 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2524 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2525 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2526 } else {
2527 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2528 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2529 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2530 (!is_power_of_2(sbi->s_inode_size)) ||
2531 (sbi->s_inode_size > blocksize)) {
2532 ext4_msg(sb, KERN_ERR,
2533 "unsupported inode size: %d",
2534 sbi->s_inode_size);
2535 goto failed_mount;
2537 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2538 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2541 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2542 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2543 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2544 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2545 !is_power_of_2(sbi->s_desc_size)) {
2546 ext4_msg(sb, KERN_ERR,
2547 "unsupported descriptor size %lu",
2548 sbi->s_desc_size);
2549 goto failed_mount;
2551 } else
2552 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2554 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2555 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2556 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2557 goto cantfind_ext4;
2559 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2560 if (sbi->s_inodes_per_block == 0)
2561 goto cantfind_ext4;
2562 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2563 sbi->s_inodes_per_block;
2564 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2565 sbi->s_sbh = bh;
2566 sbi->s_mount_state = le16_to_cpu(es->s_state);
2567 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2568 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2570 for (i = 0; i < 4; i++)
2571 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2572 sbi->s_def_hash_version = es->s_def_hash_version;
2573 i = le32_to_cpu(es->s_flags);
2574 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2575 sbi->s_hash_unsigned = 3;
2576 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2577 #ifdef __CHAR_UNSIGNED__
2578 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2579 sbi->s_hash_unsigned = 3;
2580 #else
2581 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2582 #endif
2583 sb->s_dirt = 1;
2586 if (sbi->s_blocks_per_group > blocksize * 8) {
2587 ext4_msg(sb, KERN_ERR,
2588 "#blocks per group too big: %lu",
2589 sbi->s_blocks_per_group);
2590 goto failed_mount;
2592 if (sbi->s_inodes_per_group > blocksize * 8) {
2593 ext4_msg(sb, KERN_ERR,
2594 "#inodes per group too big: %lu",
2595 sbi->s_inodes_per_group);
2596 goto failed_mount;
2600 * Test whether we have more sectors than will fit in sector_t,
2601 * and whether the max offset is addressable by the page cache.
2603 if ((ext4_blocks_count(es) >
2604 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2605 (ext4_blocks_count(es) >
2606 (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
2607 ext4_msg(sb, KERN_ERR, "filesystem"
2608 " too large to mount safely on this system");
2609 if (sizeof(sector_t) < 8)
2610 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
2611 ret = -EFBIG;
2612 goto failed_mount;
2615 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2616 goto cantfind_ext4;
2618 /* check blocks count against device size */
2619 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2620 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2621 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2622 "exceeds size of device (%llu blocks)",
2623 ext4_blocks_count(es), blocks_count);
2624 goto failed_mount;
2628 * It makes no sense for the first data block to be beyond the end
2629 * of the filesystem.
2631 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2632 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2633 "block %u is beyond end of filesystem (%llu)",
2634 le32_to_cpu(es->s_first_data_block),
2635 ext4_blocks_count(es));
2636 goto failed_mount;
2638 blocks_count = (ext4_blocks_count(es) -
2639 le32_to_cpu(es->s_first_data_block) +
2640 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2641 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2642 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2643 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
2644 "(block count %llu, first data block %u, "
2645 "blocks per group %lu)", sbi->s_groups_count,
2646 ext4_blocks_count(es),
2647 le32_to_cpu(es->s_first_data_block),
2648 EXT4_BLOCKS_PER_GROUP(sb));
2649 goto failed_mount;
2651 sbi->s_groups_count = blocks_count;
2652 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2653 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
2654 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2655 EXT4_DESC_PER_BLOCK(sb);
2656 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2657 GFP_KERNEL);
2658 if (sbi->s_group_desc == NULL) {
2659 ext4_msg(sb, KERN_ERR, "not enough memory");
2660 goto failed_mount;
2663 #ifdef CONFIG_PROC_FS
2664 if (ext4_proc_root)
2665 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2666 #endif
2668 bgl_lock_init(sbi->s_blockgroup_lock);
2670 for (i = 0; i < db_count; i++) {
2671 block = descriptor_loc(sb, logical_sb_block, i);
2672 sbi->s_group_desc[i] = sb_bread(sb, block);
2673 if (!sbi->s_group_desc[i]) {
2674 ext4_msg(sb, KERN_ERR,
2675 "can't read group descriptor %d", i);
2676 db_count = i;
2677 goto failed_mount2;
2680 if (!ext4_check_descriptors(sb)) {
2681 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
2682 goto failed_mount2;
2684 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2685 if (!ext4_fill_flex_info(sb)) {
2686 ext4_msg(sb, KERN_ERR,
2687 "unable to initialize "
2688 "flex_bg meta info!");
2689 goto failed_mount2;
2692 sbi->s_gdb_count = db_count;
2693 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2694 spin_lock_init(&sbi->s_next_gen_lock);
2696 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2697 ext4_count_free_blocks(sb));
2698 if (!err) {
2699 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2700 ext4_count_free_inodes(sb));
2702 if (!err) {
2703 err = percpu_counter_init(&sbi->s_dirs_counter,
2704 ext4_count_dirs(sb));
2706 if (!err) {
2707 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2709 if (err) {
2710 ext4_msg(sb, KERN_ERR, "insufficient memory");
2711 goto failed_mount3;
2714 sbi->s_stripe = ext4_get_stripe_size(sbi);
2715 sbi->s_max_writeback_mb_bump = 128;
2718 * set up enough so that it can read an inode
2720 if (!test_opt(sb, NOLOAD) &&
2721 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2722 sb->s_op = &ext4_sops;
2723 else
2724 sb->s_op = &ext4_nojournal_sops;
2725 sb->s_export_op = &ext4_export_ops;
2726 sb->s_xattr = ext4_xattr_handlers;
2727 #ifdef CONFIG_QUOTA
2728 sb->s_qcop = &ext4_qctl_operations;
2729 sb->dq_op = &ext4_quota_operations;
2730 #endif
2731 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2732 mutex_init(&sbi->s_orphan_lock);
2733 mutex_init(&sbi->s_resize_lock);
2735 sb->s_root = NULL;
2737 needs_recovery = (es->s_last_orphan != 0 ||
2738 EXT4_HAS_INCOMPAT_FEATURE(sb,
2739 EXT4_FEATURE_INCOMPAT_RECOVER));
2742 * The first inode we look at is the journal inode. Don't try
2743 * root first: it may be modified in the journal!
2745 if (!test_opt(sb, NOLOAD) &&
2746 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2747 if (ext4_load_journal(sb, es, journal_devnum))
2748 goto failed_mount3;
2749 if (!(sb->s_flags & MS_RDONLY) &&
2750 EXT4_SB(sb)->s_journal->j_failed_commit) {
2751 ext4_msg(sb, KERN_CRIT, "error: "
2752 "ext4_fill_super: Journal transaction "
2753 "%u is corrupt",
2754 EXT4_SB(sb)->s_journal->j_failed_commit);
2755 if (test_opt(sb, ERRORS_RO)) {
2756 ext4_msg(sb, KERN_CRIT,
2757 "Mounting filesystem read-only");
2758 sb->s_flags |= MS_RDONLY;
2759 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2760 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2762 if (test_opt(sb, ERRORS_PANIC)) {
2763 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2764 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2765 ext4_commit_super(sb, 1);
2766 goto failed_mount4;
2769 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2770 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2771 ext4_msg(sb, KERN_ERR, "required journal recovery "
2772 "suppressed and not mounted read-only");
2773 goto failed_mount4;
2774 } else {
2775 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2776 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2777 sbi->s_journal = NULL;
2778 needs_recovery = 0;
2779 goto no_journal;
2782 if (ext4_blocks_count(es) > 0xffffffffULL &&
2783 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2784 JBD2_FEATURE_INCOMPAT_64BIT)) {
2785 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
2786 goto failed_mount4;
2789 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2790 jbd2_journal_set_features(sbi->s_journal,
2791 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2792 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2793 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2794 jbd2_journal_set_features(sbi->s_journal,
2795 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2796 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2797 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2798 } else {
2799 jbd2_journal_clear_features(sbi->s_journal,
2800 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2801 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2804 /* We have now updated the journal if required, so we can
2805 * validate the data journaling mode. */
2806 switch (test_opt(sb, DATA_FLAGS)) {
2807 case 0:
2808 /* No mode set, assume a default based on the journal
2809 * capabilities: ORDERED_DATA if the journal can
2810 * cope, else JOURNAL_DATA
2812 if (jbd2_journal_check_available_features
2813 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2814 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2815 else
2816 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2817 break;
2819 case EXT4_MOUNT_ORDERED_DATA:
2820 case EXT4_MOUNT_WRITEBACK_DATA:
2821 if (!jbd2_journal_check_available_features
2822 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2823 ext4_msg(sb, KERN_ERR, "Journal does not support "
2824 "requested data journaling mode");
2825 goto failed_mount4;
2827 default:
2828 break;
2830 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
2832 no_journal:
2834 if (test_opt(sb, NOBH)) {
2835 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2836 ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2837 "its supported only with writeback mode");
2838 clear_opt(sbi->s_mount_opt, NOBH);
2841 EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
2842 if (!EXT4_SB(sb)->dio_unwritten_wq) {
2843 printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
2844 goto failed_mount_wq;
2848 * The jbd2_journal_load will have done any necessary log recovery,
2849 * so we can safely mount the rest of the filesystem now.
2852 root = ext4_iget(sb, EXT4_ROOT_INO);
2853 if (IS_ERR(root)) {
2854 ext4_msg(sb, KERN_ERR, "get root inode failed");
2855 ret = PTR_ERR(root);
2856 goto failed_mount4;
2858 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2859 iput(root);
2860 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
2861 goto failed_mount4;
2863 sb->s_root = d_alloc_root(root);
2864 if (!sb->s_root) {
2865 ext4_msg(sb, KERN_ERR, "get root dentry failed");
2866 iput(root);
2867 ret = -ENOMEM;
2868 goto failed_mount4;
2871 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2873 /* determine the minimum size of new large inodes, if present */
2874 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2875 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2876 EXT4_GOOD_OLD_INODE_SIZE;
2877 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2878 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2879 if (sbi->s_want_extra_isize <
2880 le16_to_cpu(es->s_want_extra_isize))
2881 sbi->s_want_extra_isize =
2882 le16_to_cpu(es->s_want_extra_isize);
2883 if (sbi->s_want_extra_isize <
2884 le16_to_cpu(es->s_min_extra_isize))
2885 sbi->s_want_extra_isize =
2886 le16_to_cpu(es->s_min_extra_isize);
2889 /* Check if enough inode space is available */
2890 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2891 sbi->s_inode_size) {
2892 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2893 EXT4_GOOD_OLD_INODE_SIZE;
2894 ext4_msg(sb, KERN_INFO, "required extra inode space not"
2895 "available");
2898 if (test_opt(sb, DELALLOC) &&
2899 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
2900 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2901 "requested data journaling mode");
2902 clear_opt(sbi->s_mount_opt, DELALLOC);
2905 err = ext4_setup_system_zone(sb);
2906 if (err) {
2907 ext4_msg(sb, KERN_ERR, "failed to initialize system "
2908 "zone (%d)\n", err);
2909 goto failed_mount4;
2912 ext4_ext_init(sb);
2913 err = ext4_mb_init(sb, needs_recovery);
2914 if (err) {
2915 ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2916 err);
2917 goto failed_mount4;
2920 sbi->s_kobj.kset = ext4_kset;
2921 init_completion(&sbi->s_kobj_unregister);
2922 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
2923 "%s", sb->s_id);
2924 if (err) {
2925 ext4_mb_release(sb);
2926 ext4_ext_release(sb);
2927 goto failed_mount4;
2930 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2931 ext4_orphan_cleanup(sb, es);
2932 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2933 if (needs_recovery) {
2934 ext4_msg(sb, KERN_INFO, "recovery complete");
2935 ext4_mark_recovery_complete(sb, es);
2937 if (EXT4_SB(sb)->s_journal) {
2938 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2939 descr = " journalled data mode";
2940 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2941 descr = " ordered data mode";
2942 else
2943 descr = " writeback data mode";
2944 } else
2945 descr = "out journal";
2947 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
2949 lock_kernel();
2950 return 0;
2952 cantfind_ext4:
2953 if (!silent)
2954 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
2955 goto failed_mount;
2957 failed_mount4:
2958 ext4_msg(sb, KERN_ERR, "mount failed");
2959 destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
2960 failed_mount_wq:
2961 ext4_release_system_zone(sb);
2962 if (sbi->s_journal) {
2963 jbd2_journal_destroy(sbi->s_journal);
2964 sbi->s_journal = NULL;
2966 failed_mount3:
2967 if (sbi->s_flex_groups) {
2968 if (is_vmalloc_addr(sbi->s_flex_groups))
2969 vfree(sbi->s_flex_groups);
2970 else
2971 kfree(sbi->s_flex_groups);
2973 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2974 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2975 percpu_counter_destroy(&sbi->s_dirs_counter);
2976 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2977 failed_mount2:
2978 for (i = 0; i < db_count; i++)
2979 brelse(sbi->s_group_desc[i]);
2980 kfree(sbi->s_group_desc);
2981 failed_mount:
2982 if (sbi->s_proc) {
2983 remove_proc_entry(sb->s_id, ext4_proc_root);
2985 #ifdef CONFIG_QUOTA
2986 for (i = 0; i < MAXQUOTAS; i++)
2987 kfree(sbi->s_qf_names[i]);
2988 #endif
2989 ext4_blkdev_remove(sbi);
2990 brelse(bh);
2991 out_fail:
2992 sb->s_fs_info = NULL;
2993 kfree(sbi->s_blockgroup_lock);
2994 kfree(sbi);
2995 lock_kernel();
2996 return ret;
3000 * Setup any per-fs journal parameters now. We'll do this both on
3001 * initial mount, once the journal has been initialised but before we've
3002 * done any recovery; and again on any subsequent remount.
3004 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
3006 struct ext4_sb_info *sbi = EXT4_SB(sb);
3008 journal->j_commit_interval = sbi->s_commit_interval;
3009 journal->j_min_batch_time = sbi->s_min_batch_time;
3010 journal->j_max_batch_time = sbi->s_max_batch_time;
3012 spin_lock(&journal->j_state_lock);
3013 if (test_opt(sb, BARRIER))
3014 journal->j_flags |= JBD2_BARRIER;
3015 else
3016 journal->j_flags &= ~JBD2_BARRIER;
3017 if (test_opt(sb, DATA_ERR_ABORT))
3018 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
3019 else
3020 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
3021 spin_unlock(&journal->j_state_lock);
3024 static journal_t *ext4_get_journal(struct super_block *sb,
3025 unsigned int journal_inum)
3027 struct inode *journal_inode;
3028 journal_t *journal;
3030 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3032 /* First, test for the existence of a valid inode on disk. Bad
3033 * things happen if we iget() an unused inode, as the subsequent
3034 * iput() will try to delete it. */
3036 journal_inode = ext4_iget(sb, journal_inum);
3037 if (IS_ERR(journal_inode)) {
3038 ext4_msg(sb, KERN_ERR, "no journal found");
3039 return NULL;
3041 if (!journal_inode->i_nlink) {
3042 make_bad_inode(journal_inode);
3043 iput(journal_inode);
3044 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
3045 return NULL;
3048 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
3049 journal_inode, journal_inode->i_size);
3050 if (!S_ISREG(journal_inode->i_mode)) {
3051 ext4_msg(sb, KERN_ERR, "invalid journal inode");
3052 iput(journal_inode);
3053 return NULL;
3056 journal = jbd2_journal_init_inode(journal_inode);
3057 if (!journal) {
3058 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
3059 iput(journal_inode);
3060 return NULL;
3062 journal->j_private = sb;
3063 ext4_init_journal_params(sb, journal);
3064 return journal;
3067 static journal_t *ext4_get_dev_journal(struct super_block *sb,
3068 dev_t j_dev)
3070 struct buffer_head *bh;
3071 journal_t *journal;
3072 ext4_fsblk_t start;
3073 ext4_fsblk_t len;
3074 int hblock, blocksize;
3075 ext4_fsblk_t sb_block;
3076 unsigned long offset;
3077 struct ext4_super_block *es;
3078 struct block_device *bdev;
3080 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3082 bdev = ext4_blkdev_get(j_dev, sb);
3083 if (bdev == NULL)
3084 return NULL;
3086 if (bd_claim(bdev, sb)) {
3087 ext4_msg(sb, KERN_ERR,
3088 "failed to claim external journal device");
3089 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3090 return NULL;
3093 blocksize = sb->s_blocksize;
3094 hblock = bdev_logical_block_size(bdev);
3095 if (blocksize < hblock) {
3096 ext4_msg(sb, KERN_ERR,
3097 "blocksize too small for journal device");
3098 goto out_bdev;
3101 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3102 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
3103 set_blocksize(bdev, blocksize);
3104 if (!(bh = __bread(bdev, sb_block, blocksize))) {
3105 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3106 "external journal");
3107 goto out_bdev;
3110 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3111 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
3112 !(le32_to_cpu(es->s_feature_incompat) &
3113 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3114 ext4_msg(sb, KERN_ERR, "external journal has "
3115 "bad superblock");
3116 brelse(bh);
3117 goto out_bdev;
3120 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3121 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
3122 brelse(bh);
3123 goto out_bdev;
3126 len = ext4_blocks_count(es);
3127 start = sb_block + 1;
3128 brelse(bh); /* we're done with the superblock */
3130 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
3131 start, len, blocksize);
3132 if (!journal) {
3133 ext4_msg(sb, KERN_ERR, "failed to create device journal");
3134 goto out_bdev;
3136 journal->j_private = sb;
3137 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3138 wait_on_buffer(journal->j_sb_buffer);
3139 if (!buffer_uptodate(journal->j_sb_buffer)) {
3140 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
3141 goto out_journal;
3143 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
3144 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3145 "user (unsupported) - %d",
3146 be32_to_cpu(journal->j_superblock->s_nr_users));
3147 goto out_journal;
3149 EXT4_SB(sb)->journal_bdev = bdev;
3150 ext4_init_journal_params(sb, journal);
3151 return journal;
3153 out_journal:
3154 jbd2_journal_destroy(journal);
3155 out_bdev:
3156 ext4_blkdev_put(bdev);
3157 return NULL;
3160 static int ext4_load_journal(struct super_block *sb,
3161 struct ext4_super_block *es,
3162 unsigned long journal_devnum)
3164 journal_t *journal;
3165 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3166 dev_t journal_dev;
3167 int err = 0;
3168 int really_read_only;
3170 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3172 if (journal_devnum &&
3173 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3174 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3175 "numbers have changed");
3176 journal_dev = new_decode_dev(journal_devnum);
3177 } else
3178 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3180 really_read_only = bdev_read_only(sb->s_bdev);
3183 * Are we loading a blank journal or performing recovery after a
3184 * crash? For recovery, we need to check in advance whether we
3185 * can get read-write access to the device.
3187 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
3188 if (sb->s_flags & MS_RDONLY) {
3189 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3190 "required on readonly filesystem");
3191 if (really_read_only) {
3192 ext4_msg(sb, KERN_ERR, "write access "
3193 "unavailable, cannot proceed");
3194 return -EROFS;
3196 ext4_msg(sb, KERN_INFO, "write access will "
3197 "be enabled during recovery");
3201 if (journal_inum && journal_dev) {
3202 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3203 "and inode journals!");
3204 return -EINVAL;
3207 if (journal_inum) {
3208 if (!(journal = ext4_get_journal(sb, journal_inum)))
3209 return -EINVAL;
3210 } else {
3211 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
3212 return -EINVAL;
3215 if (!(journal->j_flags & JBD2_BARRIER))
3216 ext4_msg(sb, KERN_INFO, "barriers disabled");
3218 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
3219 err = jbd2_journal_update_format(journal);
3220 if (err) {
3221 ext4_msg(sb, KERN_ERR, "error updating journal");
3222 jbd2_journal_destroy(journal);
3223 return err;
3227 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
3228 err = jbd2_journal_wipe(journal, !really_read_only);
3229 if (!err)
3230 err = jbd2_journal_load(journal);
3232 if (err) {
3233 ext4_msg(sb, KERN_ERR, "error loading journal");
3234 jbd2_journal_destroy(journal);
3235 return err;
3238 EXT4_SB(sb)->s_journal = journal;
3239 ext4_clear_journal_err(sb, es);
3241 if (journal_devnum &&
3242 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3243 es->s_journal_dev = cpu_to_le32(journal_devnum);
3245 /* Make sure we flush the recovery flag to disk. */
3246 ext4_commit_super(sb, 1);
3249 return 0;
3252 static int ext4_commit_super(struct super_block *sb, int sync)
3254 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3255 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
3256 int error = 0;
3258 if (!sbh)
3259 return error;
3260 if (buffer_write_io_error(sbh)) {
3262 * Oh, dear. A previous attempt to write the
3263 * superblock failed. This could happen because the
3264 * USB device was yanked out. Or it could happen to
3265 * be a transient write error and maybe the block will
3266 * be remapped. Nothing we can do but to retry the
3267 * write and hope for the best.
3269 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3270 "superblock detected");
3271 clear_buffer_write_io_error(sbh);
3272 set_buffer_uptodate(sbh);
3275 * If the file system is mounted read-only, don't update the
3276 * superblock write time. This avoids updating the superblock
3277 * write time when we are mounting the root file system
3278 * read/only but we need to replay the journal; at that point,
3279 * for people who are east of GMT and who make their clock
3280 * tick in localtime for Windows bug-for-bug compatibility,
3281 * the clock is set in the future, and this will cause e2fsck
3282 * to complain and force a full file system check.
3284 if (!(sb->s_flags & MS_RDONLY))
3285 es->s_wtime = cpu_to_le32(get_seconds());
3286 es->s_kbytes_written =
3287 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3288 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3289 EXT4_SB(sb)->s_sectors_written_start) >> 1));
3290 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3291 &EXT4_SB(sb)->s_freeblocks_counter));
3292 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3293 &EXT4_SB(sb)->s_freeinodes_counter));
3294 sb->s_dirt = 0;
3295 BUFFER_TRACE(sbh, "marking dirty");
3296 mark_buffer_dirty(sbh);
3297 if (sync) {
3298 error = sync_dirty_buffer(sbh);
3299 if (error)
3300 return error;
3302 error = buffer_write_io_error(sbh);
3303 if (error) {
3304 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3305 "superblock");
3306 clear_buffer_write_io_error(sbh);
3307 set_buffer_uptodate(sbh);
3310 return error;
3314 * Have we just finished recovery? If so, and if we are mounting (or
3315 * remounting) the filesystem readonly, then we will end up with a
3316 * consistent fs on disk. Record that fact.
3318 static void ext4_mark_recovery_complete(struct super_block *sb,
3319 struct ext4_super_block *es)
3321 journal_t *journal = EXT4_SB(sb)->s_journal;
3323 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3324 BUG_ON(journal != NULL);
3325 return;
3327 jbd2_journal_lock_updates(journal);
3328 if (jbd2_journal_flush(journal) < 0)
3329 goto out;
3331 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
3332 sb->s_flags & MS_RDONLY) {
3333 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3334 ext4_commit_super(sb, 1);
3337 out:
3338 jbd2_journal_unlock_updates(journal);
3342 * If we are mounting (or read-write remounting) a filesystem whose journal
3343 * has recorded an error from a previous lifetime, move that error to the
3344 * main filesystem now.
3346 static void ext4_clear_journal_err(struct super_block *sb,
3347 struct ext4_super_block *es)
3349 journal_t *journal;
3350 int j_errno;
3351 const char *errstr;
3353 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3355 journal = EXT4_SB(sb)->s_journal;
3358 * Now check for any error status which may have been recorded in the
3359 * journal by a prior ext4_error() or ext4_abort()
3362 j_errno = jbd2_journal_errno(journal);
3363 if (j_errno) {
3364 char nbuf[16];
3366 errstr = ext4_decode_error(sb, j_errno, nbuf);
3367 ext4_warning(sb, __func__, "Filesystem error recorded "
3368 "from previous mount: %s", errstr);
3369 ext4_warning(sb, __func__, "Marking fs in need of "
3370 "filesystem check.");
3372 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3373 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3374 ext4_commit_super(sb, 1);
3376 jbd2_journal_clear_err(journal);
3381 * Force the running and committing transactions to commit,
3382 * and wait on the commit.
3384 int ext4_force_commit(struct super_block *sb)
3386 journal_t *journal;
3387 int ret = 0;
3389 if (sb->s_flags & MS_RDONLY)
3390 return 0;
3392 journal = EXT4_SB(sb)->s_journal;
3393 if (journal)
3394 ret = ext4_journal_force_commit(journal);
3396 return ret;
3399 static void ext4_write_super(struct super_block *sb)
3401 lock_super(sb);
3402 ext4_commit_super(sb, 1);
3403 unlock_super(sb);
3406 static int ext4_sync_fs(struct super_block *sb, int wait)
3408 int ret = 0;
3409 tid_t target;
3410 struct ext4_sb_info *sbi = EXT4_SB(sb);
3412 trace_ext4_sync_fs(sb, wait);
3413 flush_workqueue(sbi->dio_unwritten_wq);
3414 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
3415 if (wait)
3416 jbd2_log_wait_commit(sbi->s_journal, target);
3418 return ret;
3422 * LVM calls this function before a (read-only) snapshot is created. This
3423 * gives us a chance to flush the journal completely and mark the fs clean.
3425 static int ext4_freeze(struct super_block *sb)
3427 int error = 0;
3428 journal_t *journal;
3430 if (sb->s_flags & MS_RDONLY)
3431 return 0;
3433 journal = EXT4_SB(sb)->s_journal;
3435 /* Now we set up the journal barrier. */
3436 jbd2_journal_lock_updates(journal);
3439 * Don't clear the needs_recovery flag if we failed to flush
3440 * the journal.
3442 error = jbd2_journal_flush(journal);
3443 if (error < 0) {
3444 out:
3445 jbd2_journal_unlock_updates(journal);
3446 return error;
3449 /* Journal blocked and flushed, clear needs_recovery flag. */
3450 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3451 error = ext4_commit_super(sb, 1);
3452 if (error)
3453 goto out;
3454 return 0;
3458 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3459 * flag here, even though the filesystem is not technically dirty yet.
3461 static int ext4_unfreeze(struct super_block *sb)
3463 if (sb->s_flags & MS_RDONLY)
3464 return 0;
3466 lock_super(sb);
3467 /* Reset the needs_recovery flag before the fs is unlocked. */
3468 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3469 ext4_commit_super(sb, 1);
3470 unlock_super(sb);
3471 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3472 return 0;
3475 static int ext4_remount(struct super_block *sb, int *flags, char *data)
3477 struct ext4_super_block *es;
3478 struct ext4_sb_info *sbi = EXT4_SB(sb);
3479 ext4_fsblk_t n_blocks_count = 0;
3480 unsigned long old_sb_flags;
3481 struct ext4_mount_options old_opts;
3482 ext4_group_t g;
3483 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3484 int err;
3485 #ifdef CONFIG_QUOTA
3486 int i;
3487 #endif
3489 lock_kernel();
3491 /* Store the original options */
3492 lock_super(sb);
3493 old_sb_flags = sb->s_flags;
3494 old_opts.s_mount_opt = sbi->s_mount_opt;
3495 old_opts.s_resuid = sbi->s_resuid;
3496 old_opts.s_resgid = sbi->s_resgid;
3497 old_opts.s_commit_interval = sbi->s_commit_interval;
3498 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3499 old_opts.s_max_batch_time = sbi->s_max_batch_time;
3500 #ifdef CONFIG_QUOTA
3501 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3502 for (i = 0; i < MAXQUOTAS; i++)
3503 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3504 #endif
3505 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3506 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
3509 * Allow the "check" option to be passed as a remount option.
3511 if (!parse_options(data, sb, NULL, &journal_ioprio,
3512 &n_blocks_count, 1)) {
3513 err = -EINVAL;
3514 goto restore_opts;
3517 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
3518 ext4_abort(sb, __func__, "Abort forced by user");
3520 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3521 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
3523 es = sbi->s_es;
3525 if (sbi->s_journal) {
3526 ext4_init_journal_params(sb, sbi->s_journal);
3527 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3530 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
3531 n_blocks_count > ext4_blocks_count(es)) {
3532 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
3533 err = -EROFS;
3534 goto restore_opts;
3537 if (*flags & MS_RDONLY) {
3539 * First of all, the unconditional stuff we have to do
3540 * to disable replay of the journal when we next remount
3542 sb->s_flags |= MS_RDONLY;
3545 * OK, test if we are remounting a valid rw partition
3546 * readonly, and if so set the rdonly flag and then
3547 * mark the partition as valid again.
3549 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3550 (sbi->s_mount_state & EXT4_VALID_FS))
3551 es->s_state = cpu_to_le16(sbi->s_mount_state);
3553 if (sbi->s_journal)
3554 ext4_mark_recovery_complete(sb, es);
3555 } else {
3556 /* Make sure we can mount this feature set readwrite */
3557 if (!ext4_feature_set_ok(sb, 0)) {
3558 err = -EROFS;
3559 goto restore_opts;
3562 * Make sure the group descriptor checksums
3563 * are sane. If they aren't, refuse to remount r/w.
3565 for (g = 0; g < sbi->s_groups_count; g++) {
3566 struct ext4_group_desc *gdp =
3567 ext4_get_group_desc(sb, g, NULL);
3569 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3570 ext4_msg(sb, KERN_ERR,
3571 "ext4_remount: Checksum for group %u failed (%u!=%u)",
3572 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3573 le16_to_cpu(gdp->bg_checksum));
3574 err = -EINVAL;
3575 goto restore_opts;
3580 * If we have an unprocessed orphan list hanging
3581 * around from a previously readonly bdev mount,
3582 * require a full umount/remount for now.
3584 if (es->s_last_orphan) {
3585 ext4_msg(sb, KERN_WARNING, "Couldn't "
3586 "remount RDWR because of unprocessed "
3587 "orphan inode list. Please "
3588 "umount/remount instead");
3589 err = -EINVAL;
3590 goto restore_opts;
3594 * Mounting a RDONLY partition read-write, so reread
3595 * and store the current valid flag. (It may have
3596 * been changed by e2fsck since we originally mounted
3597 * the partition.)
3599 if (sbi->s_journal)
3600 ext4_clear_journal_err(sb, es);
3601 sbi->s_mount_state = le16_to_cpu(es->s_state);
3602 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3603 goto restore_opts;
3604 if (!ext4_setup_super(sb, es, 0))
3605 sb->s_flags &= ~MS_RDONLY;
3608 ext4_setup_system_zone(sb);
3609 if (sbi->s_journal == NULL)
3610 ext4_commit_super(sb, 1);
3612 #ifdef CONFIG_QUOTA
3613 /* Release old quota file names */
3614 for (i = 0; i < MAXQUOTAS; i++)
3615 if (old_opts.s_qf_names[i] &&
3616 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3617 kfree(old_opts.s_qf_names[i]);
3618 #endif
3619 unlock_super(sb);
3620 unlock_kernel();
3621 return 0;
3623 restore_opts:
3624 sb->s_flags = old_sb_flags;
3625 sbi->s_mount_opt = old_opts.s_mount_opt;
3626 sbi->s_resuid = old_opts.s_resuid;
3627 sbi->s_resgid = old_opts.s_resgid;
3628 sbi->s_commit_interval = old_opts.s_commit_interval;
3629 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3630 sbi->s_max_batch_time = old_opts.s_max_batch_time;
3631 #ifdef CONFIG_QUOTA
3632 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3633 for (i = 0; i < MAXQUOTAS; i++) {
3634 if (sbi->s_qf_names[i] &&
3635 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3636 kfree(sbi->s_qf_names[i]);
3637 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3639 #endif
3640 unlock_super(sb);
3641 unlock_kernel();
3642 return err;
3645 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3647 struct super_block *sb = dentry->d_sb;
3648 struct ext4_sb_info *sbi = EXT4_SB(sb);
3649 struct ext4_super_block *es = sbi->s_es;
3650 u64 fsid;
3652 if (test_opt(sb, MINIX_DF)) {
3653 sbi->s_overhead_last = 0;
3654 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3655 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3656 ext4_fsblk_t overhead = 0;
3659 * Compute the overhead (FS structures). This is constant
3660 * for a given filesystem unless the number of block groups
3661 * changes so we cache the previous value until it does.
3665 * All of the blocks before first_data_block are
3666 * overhead
3668 overhead = le32_to_cpu(es->s_first_data_block);
3671 * Add the overhead attributed to the superblock and
3672 * block group descriptors. If the sparse superblocks
3673 * feature is turned on, then not all groups have this.
3675 for (i = 0; i < ngroups; i++) {
3676 overhead += ext4_bg_has_super(sb, i) +
3677 ext4_bg_num_gdb(sb, i);
3678 cond_resched();
3682 * Every block group has an inode bitmap, a block
3683 * bitmap, and an inode table.
3685 overhead += ngroups * (2 + sbi->s_itb_per_group);
3686 sbi->s_overhead_last = overhead;
3687 smp_wmb();
3688 sbi->s_blocks_last = ext4_blocks_count(es);
3691 buf->f_type = EXT4_SUPER_MAGIC;
3692 buf->f_bsize = sb->s_blocksize;
3693 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3694 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3695 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3696 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3697 if (buf->f_bfree < ext4_r_blocks_count(es))
3698 buf->f_bavail = 0;
3699 buf->f_files = le32_to_cpu(es->s_inodes_count);
3700 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3701 buf->f_namelen = EXT4_NAME_LEN;
3702 fsid = le64_to_cpup((void *)es->s_uuid) ^
3703 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3704 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3705 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3707 return 0;
3710 /* Helper function for writing quotas on sync - we need to start transaction
3711 * before quota file is locked for write. Otherwise the are possible deadlocks:
3712 * Process 1 Process 2
3713 * ext4_create() quota_sync()
3714 * jbd2_journal_start() write_dquot()
3715 * vfs_dq_init() down(dqio_mutex)
3716 * down(dqio_mutex) jbd2_journal_start()
3720 #ifdef CONFIG_QUOTA
3722 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3724 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3727 static int ext4_write_dquot(struct dquot *dquot)
3729 int ret, err;
3730 handle_t *handle;
3731 struct inode *inode;
3733 inode = dquot_to_inode(dquot);
3734 handle = ext4_journal_start(inode,
3735 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3736 if (IS_ERR(handle))
3737 return PTR_ERR(handle);
3738 ret = dquot_commit(dquot);
3739 err = ext4_journal_stop(handle);
3740 if (!ret)
3741 ret = err;
3742 return ret;
3745 static int ext4_acquire_dquot(struct dquot *dquot)
3747 int ret, err;
3748 handle_t *handle;
3750 handle = ext4_journal_start(dquot_to_inode(dquot),
3751 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3752 if (IS_ERR(handle))
3753 return PTR_ERR(handle);
3754 ret = dquot_acquire(dquot);
3755 err = ext4_journal_stop(handle);
3756 if (!ret)
3757 ret = err;
3758 return ret;
3761 static int ext4_release_dquot(struct dquot *dquot)
3763 int ret, err;
3764 handle_t *handle;
3766 handle = ext4_journal_start(dquot_to_inode(dquot),
3767 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3768 if (IS_ERR(handle)) {
3769 /* Release dquot anyway to avoid endless cycle in dqput() */
3770 dquot_release(dquot);
3771 return PTR_ERR(handle);
3773 ret = dquot_release(dquot);
3774 err = ext4_journal_stop(handle);
3775 if (!ret)
3776 ret = err;
3777 return ret;
3780 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3782 /* Are we journaling quotas? */
3783 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3784 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3785 dquot_mark_dquot_dirty(dquot);
3786 return ext4_write_dquot(dquot);
3787 } else {
3788 return dquot_mark_dquot_dirty(dquot);
3792 static int ext4_write_info(struct super_block *sb, int type)
3794 int ret, err;
3795 handle_t *handle;
3797 /* Data block + inode block */
3798 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3799 if (IS_ERR(handle))
3800 return PTR_ERR(handle);
3801 ret = dquot_commit_info(sb, type);
3802 err = ext4_journal_stop(handle);
3803 if (!ret)
3804 ret = err;
3805 return ret;
3809 * Turn on quotas during mount time - we need to find
3810 * the quota file and such...
3812 static int ext4_quota_on_mount(struct super_block *sb, int type)
3814 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3815 EXT4_SB(sb)->s_jquota_fmt, type);
3819 * Standard function to be called on quota_on
3821 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3822 char *name, int remount)
3824 int err;
3825 struct path path;
3827 if (!test_opt(sb, QUOTA))
3828 return -EINVAL;
3829 /* When remounting, no checks are needed and in fact, name is NULL */
3830 if (remount)
3831 return vfs_quota_on(sb, type, format_id, name, remount);
3833 err = kern_path(name, LOOKUP_FOLLOW, &path);
3834 if (err)
3835 return err;
3837 /* Quotafile not on the same filesystem? */
3838 if (path.mnt->mnt_sb != sb) {
3839 path_put(&path);
3840 return -EXDEV;
3842 /* Journaling quota? */
3843 if (EXT4_SB(sb)->s_qf_names[type]) {
3844 /* Quotafile not in fs root? */
3845 if (path.dentry->d_parent != sb->s_root)
3846 ext4_msg(sb, KERN_WARNING,
3847 "Quota file not on filesystem root. "
3848 "Journaled quota will not work");
3852 * When we journal data on quota file, we have to flush journal to see
3853 * all updates to the file when we bypass pagecache...
3855 if (EXT4_SB(sb)->s_journal &&
3856 ext4_should_journal_data(path.dentry->d_inode)) {
3858 * We don't need to lock updates but journal_flush() could
3859 * otherwise be livelocked...
3861 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3862 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3863 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3864 if (err) {
3865 path_put(&path);
3866 return err;
3870 err = vfs_quota_on_path(sb, type, format_id, &path);
3871 path_put(&path);
3872 return err;
3875 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3876 * acquiring the locks... As quota files are never truncated and quota code
3877 * itself serializes the operations (and noone else should touch the files)
3878 * we don't have to be afraid of races */
3879 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3880 size_t len, loff_t off)
3882 struct inode *inode = sb_dqopt(sb)->files[type];
3883 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3884 int err = 0;
3885 int offset = off & (sb->s_blocksize - 1);
3886 int tocopy;
3887 size_t toread;
3888 struct buffer_head *bh;
3889 loff_t i_size = i_size_read(inode);
3891 if (off > i_size)
3892 return 0;
3893 if (off+len > i_size)
3894 len = i_size-off;
3895 toread = len;
3896 while (toread > 0) {
3897 tocopy = sb->s_blocksize - offset < toread ?
3898 sb->s_blocksize - offset : toread;
3899 bh = ext4_bread(NULL, inode, blk, 0, &err);
3900 if (err)
3901 return err;
3902 if (!bh) /* A hole? */
3903 memset(data, 0, tocopy);
3904 else
3905 memcpy(data, bh->b_data+offset, tocopy);
3906 brelse(bh);
3907 offset = 0;
3908 toread -= tocopy;
3909 data += tocopy;
3910 blk++;
3912 return len;
3915 /* Write to quotafile (we know the transaction is already started and has
3916 * enough credits) */
3917 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3918 const char *data, size_t len, loff_t off)
3920 struct inode *inode = sb_dqopt(sb)->files[type];
3921 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3922 int err = 0;
3923 int offset = off & (sb->s_blocksize - 1);
3924 int tocopy;
3925 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3926 size_t towrite = len;
3927 struct buffer_head *bh;
3928 handle_t *handle = journal_current_handle();
3930 if (EXT4_SB(sb)->s_journal && !handle) {
3931 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3932 " cancelled because transaction is not started",
3933 (unsigned long long)off, (unsigned long long)len);
3934 return -EIO;
3936 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3937 while (towrite > 0) {
3938 tocopy = sb->s_blocksize - offset < towrite ?
3939 sb->s_blocksize - offset : towrite;
3940 bh = ext4_bread(handle, inode, blk, 1, &err);
3941 if (!bh)
3942 goto out;
3943 if (journal_quota) {
3944 err = ext4_journal_get_write_access(handle, bh);
3945 if (err) {
3946 brelse(bh);
3947 goto out;
3950 lock_buffer(bh);
3951 memcpy(bh->b_data+offset, data, tocopy);
3952 flush_dcache_page(bh->b_page);
3953 unlock_buffer(bh);
3954 if (journal_quota)
3955 err = ext4_handle_dirty_metadata(handle, NULL, bh);
3956 else {
3957 /* Always do at least ordered writes for quotas */
3958 err = ext4_jbd2_file_inode(handle, inode);
3959 mark_buffer_dirty(bh);
3961 brelse(bh);
3962 if (err)
3963 goto out;
3964 offset = 0;
3965 towrite -= tocopy;
3966 data += tocopy;
3967 blk++;
3969 out:
3970 if (len == towrite) {
3971 mutex_unlock(&inode->i_mutex);
3972 return err;
3974 if (inode->i_size < off+len-towrite) {
3975 i_size_write(inode, off+len-towrite);
3976 EXT4_I(inode)->i_disksize = inode->i_size;
3978 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3979 ext4_mark_inode_dirty(handle, inode);
3980 mutex_unlock(&inode->i_mutex);
3981 return len - towrite;
3984 #endif
3986 static int ext4_get_sb(struct file_system_type *fs_type, int flags,
3987 const char *dev_name, void *data, struct vfsmount *mnt)
3989 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
3992 static struct file_system_type ext4_fs_type = {
3993 .owner = THIS_MODULE,
3994 .name = "ext4",
3995 .get_sb = ext4_get_sb,
3996 .kill_sb = kill_block_super,
3997 .fs_flags = FS_REQUIRES_DEV,
4000 static int __init init_ext4_fs(void)
4002 int err;
4004 err = init_ext4_system_zone();
4005 if (err)
4006 return err;
4007 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
4008 if (!ext4_kset)
4009 goto out4;
4010 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
4011 err = init_ext4_mballoc();
4012 if (err)
4013 goto out3;
4015 err = init_ext4_xattr();
4016 if (err)
4017 goto out2;
4018 err = init_inodecache();
4019 if (err)
4020 goto out1;
4021 err = register_filesystem(&ext4_fs_type);
4022 if (err)
4023 goto out;
4024 return 0;
4025 out:
4026 destroy_inodecache();
4027 out1:
4028 exit_ext4_xattr();
4029 out2:
4030 exit_ext4_mballoc();
4031 out3:
4032 remove_proc_entry("fs/ext4", NULL);
4033 kset_unregister(ext4_kset);
4034 out4:
4035 exit_ext4_system_zone();
4036 return err;
4039 static void __exit exit_ext4_fs(void)
4041 unregister_filesystem(&ext4_fs_type);
4042 destroy_inodecache();
4043 exit_ext4_xattr();
4044 exit_ext4_mballoc();
4045 remove_proc_entry("fs/ext4", NULL);
4046 kset_unregister(ext4_kset);
4047 exit_ext4_system_zone();
4050 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
4051 MODULE_DESCRIPTION("Fourth Extended Filesystem");
4052 MODULE_LICENSE("GPL");
4053 module_init(init_ext4_fs)
4054 module_exit(exit_ext4_fs)