1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/super.c
5 * Copyright (C) 1992, 1993, 1994, 1995
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/inode.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * Big-endian to little-endian byte-swapping/bitmaps by
20 #include <linux/module.h>
21 #include <linux/string.h>
23 #include <linux/time.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/blkdev.h>
28 #include <linux/backing-dev.h>
29 #include <linux/parser.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/ctype.h>
39 #include <linux/log2.h>
40 #include <linux/crc16.h>
41 #include <linux/dax.h>
42 #include <linux/cleancache.h>
43 #include <linux/uaccess.h>
44 #include <linux/iversion.h>
45 #include <linux/unicode.h>
46 #include <linux/part_stat.h>
47 #include <linux/kthread.h>
48 #include <linux/freezer.h>
49 #include <linux/fsnotify.h>
52 #include "ext4_extents.h" /* Needed for trace points definition */
53 #include "ext4_jbd2.h"
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/ext4.h>
62 static struct ext4_lazy_init *ext4_li_info;
63 static DEFINE_MUTEX(ext4_li_mtx);
64 static struct ratelimit_state ext4_mount_msg_ratelimit;
66 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
67 unsigned long journal_devnum);
68 static int ext4_show_options(struct seq_file *seq, struct dentry *root);
69 static void ext4_update_super(struct super_block *sb);
70 static int ext4_commit_super(struct super_block *sb);
71 static int ext4_mark_recovery_complete(struct super_block *sb,
72 struct ext4_super_block *es);
73 static int ext4_clear_journal_err(struct super_block *sb,
74 struct ext4_super_block *es);
75 static int ext4_sync_fs(struct super_block *sb, int wait);
76 static int ext4_remount(struct super_block *sb, int *flags, char *data);
77 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
78 static int ext4_unfreeze(struct super_block *sb);
79 static int ext4_freeze(struct super_block *sb);
80 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
81 const char *dev_name, void *data);
82 static inline int ext2_feature_set_ok(struct super_block *sb);
83 static inline int ext3_feature_set_ok(struct super_block *sb);
84 static void ext4_destroy_lazyinit_thread(void);
85 static void ext4_unregister_li_request(struct super_block *sb);
86 static void ext4_clear_request_list(void);
87 static struct inode *ext4_get_journal_inode(struct super_block *sb,
88 unsigned int journal_inum);
94 * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
95 * -> page lock -> i_data_sem (rw)
97 * buffered write path:
98 * sb_start_write -> i_mutex -> mmap_lock
99 * sb_start_write -> i_mutex -> transaction start -> page lock ->
103 * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
105 * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
109 * sb_start_write -> i_mutex -> mmap_lock
110 * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
113 * transaction start -> page lock(s) -> i_data_sem (rw)
116 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
117 static struct file_system_type ext2_fs_type = {
118 .owner = THIS_MODULE,
121 .kill_sb = kill_block_super,
122 .fs_flags = FS_REQUIRES_DEV,
124 MODULE_ALIAS_FS("ext2");
125 MODULE_ALIAS("ext2");
126 #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
128 #define IS_EXT2_SB(sb) (0)
132 static struct file_system_type ext3_fs_type = {
133 .owner = THIS_MODULE,
136 .kill_sb = kill_block_super,
137 .fs_flags = FS_REQUIRES_DEV,
139 MODULE_ALIAS_FS("ext3");
140 MODULE_ALIAS("ext3");
141 #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
144 static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
148 * buffer's verified bit is no longer valid after reading from
149 * disk again due to write out error, clear it to make sure we
150 * recheck the buffer contents.
152 clear_buffer_verified(bh);
154 bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
156 submit_bh(REQ_OP_READ, op_flags, bh);
159 void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
162 BUG_ON(!buffer_locked(bh));
164 if (ext4_buffer_uptodate(bh)) {
168 __ext4_read_bh(bh, op_flags, end_io);
171 int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
173 BUG_ON(!buffer_locked(bh));
175 if (ext4_buffer_uptodate(bh)) {
180 __ext4_read_bh(bh, op_flags, end_io);
183 if (buffer_uptodate(bh))
188 int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
190 if (trylock_buffer(bh)) {
192 return ext4_read_bh(bh, op_flags, NULL);
193 ext4_read_bh_nowait(bh, op_flags, NULL);
198 if (buffer_uptodate(bh))
206 * This works like __bread_gfp() except it uses ERR_PTR for error
207 * returns. Currently with sb_bread it's impossible to distinguish
208 * between ENOMEM and EIO situations (since both result in a NULL
211 static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
212 sector_t block, int op_flags,
215 struct buffer_head *bh;
218 bh = sb_getblk_gfp(sb, block, gfp);
220 return ERR_PTR(-ENOMEM);
221 if (ext4_buffer_uptodate(bh))
224 ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
232 struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
235 return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
238 struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
241 return __ext4_sb_bread_gfp(sb, block, 0, 0);
244 void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
246 struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
249 ext4_read_bh_lock(bh, REQ_RAHEAD, false);
254 static int ext4_verify_csum_type(struct super_block *sb,
255 struct ext4_super_block *es)
257 if (!ext4_has_feature_metadata_csum(sb))
260 return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
263 static __le32 ext4_superblock_csum(struct super_block *sb,
264 struct ext4_super_block *es)
266 struct ext4_sb_info *sbi = EXT4_SB(sb);
267 int offset = offsetof(struct ext4_super_block, s_checksum);
270 csum = ext4_chksum(sbi, ~0, (char *)es, offset);
272 return cpu_to_le32(csum);
275 static int ext4_superblock_csum_verify(struct super_block *sb,
276 struct ext4_super_block *es)
278 if (!ext4_has_metadata_csum(sb))
281 return es->s_checksum == ext4_superblock_csum(sb, es);
284 void ext4_superblock_csum_set(struct super_block *sb)
286 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
288 if (!ext4_has_metadata_csum(sb))
291 es->s_checksum = ext4_superblock_csum(sb, es);
294 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
295 struct ext4_group_desc *bg)
297 return le32_to_cpu(bg->bg_block_bitmap_lo) |
298 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
299 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
302 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
303 struct ext4_group_desc *bg)
305 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
306 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
307 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
310 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
311 struct ext4_group_desc *bg)
313 return le32_to_cpu(bg->bg_inode_table_lo) |
314 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
315 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
318 __u32 ext4_free_group_clusters(struct super_block *sb,
319 struct ext4_group_desc *bg)
321 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
322 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
323 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
326 __u32 ext4_free_inodes_count(struct super_block *sb,
327 struct ext4_group_desc *bg)
329 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
330 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
331 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
334 __u32 ext4_used_dirs_count(struct super_block *sb,
335 struct ext4_group_desc *bg)
337 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
338 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
339 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
342 __u32 ext4_itable_unused_count(struct super_block *sb,
343 struct ext4_group_desc *bg)
345 return le16_to_cpu(bg->bg_itable_unused_lo) |
346 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
347 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
350 void ext4_block_bitmap_set(struct super_block *sb,
351 struct ext4_group_desc *bg, ext4_fsblk_t blk)
353 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
354 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
355 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
358 void ext4_inode_bitmap_set(struct super_block *sb,
359 struct ext4_group_desc *bg, ext4_fsblk_t blk)
361 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
362 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
363 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
366 void ext4_inode_table_set(struct super_block *sb,
367 struct ext4_group_desc *bg, ext4_fsblk_t blk)
369 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
370 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
371 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
374 void ext4_free_group_clusters_set(struct super_block *sb,
375 struct ext4_group_desc *bg, __u32 count)
377 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
378 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
379 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
382 void ext4_free_inodes_set(struct super_block *sb,
383 struct ext4_group_desc *bg, __u32 count)
385 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
386 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
387 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
390 void ext4_used_dirs_set(struct super_block *sb,
391 struct ext4_group_desc *bg, __u32 count)
393 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
394 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
395 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
398 void ext4_itable_unused_set(struct super_block *sb,
399 struct ext4_group_desc *bg, __u32 count)
401 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
402 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
403 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
406 static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
408 now = clamp_val(now, 0, (1ull << 40) - 1);
410 *lo = cpu_to_le32(lower_32_bits(now));
411 *hi = upper_32_bits(now);
414 static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
416 return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
418 #define ext4_update_tstamp(es, tstamp) \
419 __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
420 ktime_get_real_seconds())
421 #define ext4_get_tstamp(es, tstamp) \
422 __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
425 * The del_gendisk() function uninitializes the disk-specific data
426 * structures, including the bdi structure, without telling anyone
427 * else. Once this happens, any attempt to call mark_buffer_dirty()
428 * (for example, by ext4_commit_super), will cause a kernel OOPS.
429 * This is a kludge to prevent these oops until we can put in a proper
430 * hook in del_gendisk() to inform the VFS and file system layers.
432 static int block_device_ejected(struct super_block *sb)
434 struct inode *bd_inode = sb->s_bdev->bd_inode;
435 struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
437 return bdi->dev == NULL;
440 static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
442 struct super_block *sb = journal->j_private;
443 struct ext4_sb_info *sbi = EXT4_SB(sb);
444 int error = is_journal_aborted(journal);
445 struct ext4_journal_cb_entry *jce;
447 BUG_ON(txn->t_state == T_FINISHED);
449 ext4_process_freed_data(sb, txn->t_tid);
451 spin_lock(&sbi->s_md_lock);
452 while (!list_empty(&txn->t_private_list)) {
453 jce = list_entry(txn->t_private_list.next,
454 struct ext4_journal_cb_entry, jce_list);
455 list_del_init(&jce->jce_list);
456 spin_unlock(&sbi->s_md_lock);
457 jce->jce_func(sb, jce, error);
458 spin_lock(&sbi->s_md_lock);
460 spin_unlock(&sbi->s_md_lock);
464 * This writepage callback for write_cache_pages()
465 * takes care of a few cases after page cleaning.
467 * write_cache_pages() already checks for dirty pages
468 * and calls clear_page_dirty_for_io(), which we want,
469 * to write protect the pages.
471 * However, we may have to redirty a page (see below.)
473 static int ext4_journalled_writepage_callback(struct page *page,
474 struct writeback_control *wbc,
477 transaction_t *transaction = (transaction_t *) data;
478 struct buffer_head *bh, *head;
479 struct journal_head *jh;
481 bh = head = page_buffers(page);
484 * We have to redirty a page in these cases:
485 * 1) If buffer is dirty, it means the page was dirty because it
486 * contains a buffer that needs checkpointing. So the dirty bit
487 * needs to be preserved so that checkpointing writes the buffer
489 * 2) If buffer is not part of the committing transaction
490 * (we may have just accidentally come across this buffer because
491 * inode range tracking is not exact) or if the currently running
492 * transaction already contains this buffer as well, dirty bit
493 * needs to be preserved so that the buffer gets writeprotected
494 * properly on running transaction's commit.
497 if (buffer_dirty(bh) ||
498 (jh && (jh->b_transaction != transaction ||
499 jh->b_next_transaction))) {
500 redirty_page_for_writepage(wbc, page);
503 } while ((bh = bh->b_this_page) != head);
506 return AOP_WRITEPAGE_ACTIVATE;
509 static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
511 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
512 struct writeback_control wbc = {
513 .sync_mode = WB_SYNC_ALL,
514 .nr_to_write = LONG_MAX,
515 .range_start = jinode->i_dirty_start,
516 .range_end = jinode->i_dirty_end,
519 return write_cache_pages(mapping, &wbc,
520 ext4_journalled_writepage_callback,
521 jinode->i_transaction);
524 static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
528 if (ext4_should_journal_data(jinode->i_vfs_inode))
529 ret = ext4_journalled_submit_inode_data_buffers(jinode);
531 ret = jbd2_journal_submit_inode_data_buffers(jinode);
536 static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
540 if (!ext4_should_journal_data(jinode->i_vfs_inode))
541 ret = jbd2_journal_finish_inode_data_buffers(jinode);
546 static bool system_going_down(void)
548 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
549 || system_state == SYSTEM_RESTART;
552 struct ext4_err_translation {
557 #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
559 static struct ext4_err_translation err_translation[] = {
560 EXT4_ERR_TRANSLATE(EIO),
561 EXT4_ERR_TRANSLATE(ENOMEM),
562 EXT4_ERR_TRANSLATE(EFSBADCRC),
563 EXT4_ERR_TRANSLATE(EFSCORRUPTED),
564 EXT4_ERR_TRANSLATE(ENOSPC),
565 EXT4_ERR_TRANSLATE(ENOKEY),
566 EXT4_ERR_TRANSLATE(EROFS),
567 EXT4_ERR_TRANSLATE(EFBIG),
568 EXT4_ERR_TRANSLATE(EEXIST),
569 EXT4_ERR_TRANSLATE(ERANGE),
570 EXT4_ERR_TRANSLATE(EOVERFLOW),
571 EXT4_ERR_TRANSLATE(EBUSY),
572 EXT4_ERR_TRANSLATE(ENOTDIR),
573 EXT4_ERR_TRANSLATE(ENOTEMPTY),
574 EXT4_ERR_TRANSLATE(ESHUTDOWN),
575 EXT4_ERR_TRANSLATE(EFAULT),
578 static int ext4_errno_to_code(int errno)
582 for (i = 0; i < ARRAY_SIZE(err_translation); i++)
583 if (err_translation[i].errno == errno)
584 return err_translation[i].code;
585 return EXT4_ERR_UNKNOWN;
588 static void save_error_info(struct super_block *sb, int error,
589 __u32 ino, __u64 block,
590 const char *func, unsigned int line)
592 struct ext4_sb_info *sbi = EXT4_SB(sb);
594 /* We default to EFSCORRUPTED error... */
596 error = EFSCORRUPTED;
598 spin_lock(&sbi->s_error_lock);
599 sbi->s_add_error_count++;
600 sbi->s_last_error_code = error;
601 sbi->s_last_error_line = line;
602 sbi->s_last_error_ino = ino;
603 sbi->s_last_error_block = block;
604 sbi->s_last_error_func = func;
605 sbi->s_last_error_time = ktime_get_real_seconds();
606 if (!sbi->s_first_error_time) {
607 sbi->s_first_error_code = error;
608 sbi->s_first_error_line = line;
609 sbi->s_first_error_ino = ino;
610 sbi->s_first_error_block = block;
611 sbi->s_first_error_func = func;
612 sbi->s_first_error_time = sbi->s_last_error_time;
614 spin_unlock(&sbi->s_error_lock);
617 /* Deal with the reporting of failure conditions on a filesystem such as
618 * inconsistencies detected or read IO failures.
620 * On ext2, we can store the error state of the filesystem in the
621 * superblock. That is not possible on ext4, because we may have other
622 * write ordering constraints on the superblock which prevent us from
623 * writing it out straight away; and given that the journal is about to
624 * be aborted, we can't rely on the current, or future, transactions to
625 * write out the superblock safely.
627 * We'll just use the jbd2_journal_abort() error code to record an error in
628 * the journal instead. On recovery, the journal will complain about
629 * that error until we've noted it down and cleared it.
631 * If force_ro is set, we unconditionally force the filesystem into an
632 * ABORT|READONLY state, unless the error response on the fs has been set to
633 * panic in which case we take the easy way out and panic immediately. This is
634 * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
635 * at a critical moment in log management.
637 static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
638 __u32 ino, __u64 block,
639 const char *func, unsigned int line)
641 journal_t *journal = EXT4_SB(sb)->s_journal;
642 bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
644 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
645 if (test_opt(sb, WARN_ON_ERROR))
648 if (!continue_fs && !sb_rdonly(sb)) {
649 ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
651 jbd2_journal_abort(journal, -EIO);
654 if (!bdev_read_only(sb->s_bdev)) {
655 save_error_info(sb, error, ino, block, func, line);
657 * In case the fs should keep running, we need to writeout
658 * superblock through the journal. Due to lock ordering
659 * constraints, it may not be safe to do it right here so we
660 * defer superblock flushing to a workqueue.
662 if (continue_fs && journal)
663 schedule_work(&EXT4_SB(sb)->s_error_work);
665 ext4_commit_super(sb);
669 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
670 * could panic during 'reboot -f' as the underlying device got already
673 if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
674 panic("EXT4-fs (device %s): panic forced after error\n",
678 if (sb_rdonly(sb) || continue_fs)
681 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
683 * Make sure updated value of ->s_mount_flags will be visible before
687 sb->s_flags |= SB_RDONLY;
690 static void flush_stashed_error_work(struct work_struct *work)
692 struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
694 journal_t *journal = sbi->s_journal;
698 * If the journal is still running, we have to write out superblock
699 * through the journal to avoid collisions of other journalled sb
702 * We use directly jbd2 functions here to avoid recursing back into
703 * ext4 error handling code during handling of previous errors.
705 if (!sb_rdonly(sbi->s_sb) && journal) {
706 struct buffer_head *sbh = sbi->s_sbh;
707 handle = jbd2_journal_start(journal, 1);
710 if (jbd2_journal_get_write_access(handle, sbh)) {
711 jbd2_journal_stop(handle);
714 ext4_update_super(sbi->s_sb);
715 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
716 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
717 "superblock detected");
718 clear_buffer_write_io_error(sbh);
719 set_buffer_uptodate(sbh);
722 if (jbd2_journal_dirty_metadata(handle, sbh)) {
723 jbd2_journal_stop(handle);
726 jbd2_journal_stop(handle);
727 ext4_notify_error_sysfs(sbi);
732 * Write through journal failed. Write sb directly to get error info
733 * out and hope for the best.
735 ext4_commit_super(sbi->s_sb);
736 ext4_notify_error_sysfs(sbi);
739 #define ext4_error_ratelimit(sb) \
740 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
743 void __ext4_error(struct super_block *sb, const char *function,
744 unsigned int line, bool force_ro, int error, __u64 block,
745 const char *fmt, ...)
747 struct va_format vaf;
750 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
753 trace_ext4_error(sb, function, line);
754 if (ext4_error_ratelimit(sb)) {
759 "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
760 sb->s_id, function, line, current->comm, &vaf);
763 fsnotify_sb_error(sb, NULL, error ? error : EFSCORRUPTED);
765 ext4_handle_error(sb, force_ro, error, 0, block, function, line);
768 void __ext4_error_inode(struct inode *inode, const char *function,
769 unsigned int line, ext4_fsblk_t block, int error,
770 const char *fmt, ...)
773 struct va_format vaf;
775 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
778 trace_ext4_error(inode->i_sb, function, line);
779 if (ext4_error_ratelimit(inode->i_sb)) {
784 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
785 "inode #%lu: block %llu: comm %s: %pV\n",
786 inode->i_sb->s_id, function, line, inode->i_ino,
787 block, current->comm, &vaf);
789 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
790 "inode #%lu: comm %s: %pV\n",
791 inode->i_sb->s_id, function, line, inode->i_ino,
792 current->comm, &vaf);
795 fsnotify_sb_error(inode->i_sb, inode, error ? error : EFSCORRUPTED);
797 ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
801 void __ext4_error_file(struct file *file, const char *function,
802 unsigned int line, ext4_fsblk_t block,
803 const char *fmt, ...)
806 struct va_format vaf;
807 struct inode *inode = file_inode(file);
808 char pathname[80], *path;
810 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
813 trace_ext4_error(inode->i_sb, function, line);
814 if (ext4_error_ratelimit(inode->i_sb)) {
815 path = file_path(file, pathname, sizeof(pathname));
823 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
824 "block %llu: comm %s: path %s: %pV\n",
825 inode->i_sb->s_id, function, line, inode->i_ino,
826 block, current->comm, path, &vaf);
829 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
830 "comm %s: path %s: %pV\n",
831 inode->i_sb->s_id, function, line, inode->i_ino,
832 current->comm, path, &vaf);
835 fsnotify_sb_error(inode->i_sb, inode, EFSCORRUPTED);
837 ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
841 const char *ext4_decode_error(struct super_block *sb, int errno,
848 errstr = "Corrupt filesystem";
851 errstr = "Filesystem failed CRC";
854 errstr = "IO failure";
857 errstr = "Out of memory";
860 if (!sb || (EXT4_SB(sb)->s_journal &&
861 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
862 errstr = "Journal has aborted";
864 errstr = "Readonly filesystem";
867 /* If the caller passed in an extra buffer for unknown
868 * errors, textualise them now. Else we just return
871 /* Check for truncated error codes... */
872 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
881 /* __ext4_std_error decodes expected errors from journaling functions
882 * automatically and invokes the appropriate error response. */
884 void __ext4_std_error(struct super_block *sb, const char *function,
885 unsigned int line, int errno)
890 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
893 /* Special case: if the error is EROFS, and we're not already
894 * inside a transaction, then there's really no point in logging
896 if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
899 if (ext4_error_ratelimit(sb)) {
900 errstr = ext4_decode_error(sb, errno, nbuf);
901 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
902 sb->s_id, function, line, errstr);
904 fsnotify_sb_error(sb, NULL, errno ? errno : EFSCORRUPTED);
906 ext4_handle_error(sb, false, -errno, 0, 0, function, line);
909 void __ext4_msg(struct super_block *sb,
910 const char *prefix, const char *fmt, ...)
912 struct va_format vaf;
915 atomic_inc(&EXT4_SB(sb)->s_msg_count);
916 if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
922 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
926 static int ext4_warning_ratelimit(struct super_block *sb)
928 atomic_inc(&EXT4_SB(sb)->s_warning_count);
929 return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
933 void __ext4_warning(struct super_block *sb, const char *function,
934 unsigned int line, const char *fmt, ...)
936 struct va_format vaf;
939 if (!ext4_warning_ratelimit(sb))
945 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
946 sb->s_id, function, line, &vaf);
950 void __ext4_warning_inode(const struct inode *inode, const char *function,
951 unsigned int line, const char *fmt, ...)
953 struct va_format vaf;
956 if (!ext4_warning_ratelimit(inode->i_sb))
962 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
963 "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
964 function, line, inode->i_ino, current->comm, &vaf);
968 void __ext4_grp_locked_error(const char *function, unsigned int line,
969 struct super_block *sb, ext4_group_t grp,
970 unsigned long ino, ext4_fsblk_t block,
971 const char *fmt, ...)
975 struct va_format vaf;
978 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
981 trace_ext4_error(sb, function, line);
982 if (ext4_error_ratelimit(sb)) {
986 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
987 sb->s_id, function, line, grp);
989 printk(KERN_CONT "inode %lu: ", ino);
991 printk(KERN_CONT "block %llu:",
992 (unsigned long long) block);
993 printk(KERN_CONT "%pV\n", &vaf);
997 if (test_opt(sb, ERRORS_CONT)) {
998 if (test_opt(sb, WARN_ON_ERROR))
1000 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
1001 if (!bdev_read_only(sb->s_bdev)) {
1002 save_error_info(sb, EFSCORRUPTED, ino, block, function,
1004 schedule_work(&EXT4_SB(sb)->s_error_work);
1008 ext4_unlock_group(sb, grp);
1009 ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
1011 * We only get here in the ERRORS_RO case; relocking the group
1012 * may be dangerous, but nothing bad will happen since the
1013 * filesystem will have already been marked read/only and the
1014 * journal has been aborted. We return 1 as a hint to callers
1015 * who might what to use the return value from
1016 * ext4_grp_locked_error() to distinguish between the
1017 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
1018 * aggressively from the ext4 function in question, with a
1019 * more appropriate error code.
1021 ext4_lock_group(sb, grp);
1025 void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1029 struct ext4_sb_info *sbi = EXT4_SB(sb);
1030 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1031 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
1034 if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
1035 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1038 percpu_counter_sub(&sbi->s_freeclusters_counter,
1042 if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
1043 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
1048 count = ext4_free_inodes_count(sb, gdp);
1049 percpu_counter_sub(&sbi->s_freeinodes_counter,
1055 void ext4_update_dynamic_rev(struct super_block *sb)
1057 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1059 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1063 "updating to rev %d because of new feature flag, "
1064 "running e2fsck is recommended",
1067 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1068 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1069 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1070 /* leave es->s_feature_*compat flags alone */
1071 /* es->s_uuid will be set by e2fsck if empty */
1074 * The rest of the superblock fields should be zero, and if not it
1075 * means they are likely already in use, so leave them alone. We
1076 * can leave it up to e2fsck to clean up any inconsistencies there.
1081 * Open the external journal device
1083 static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1085 struct block_device *bdev;
1087 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1093 ext4_msg(sb, KERN_ERR,
1094 "failed to open journal device unknown-block(%u,%u) %ld",
1095 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1100 * Release the journal device
1102 static void ext4_blkdev_put(struct block_device *bdev)
1104 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1107 static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1109 struct block_device *bdev;
1110 bdev = sbi->s_journal_bdev;
1112 ext4_blkdev_put(bdev);
1113 sbi->s_journal_bdev = NULL;
1117 static inline struct inode *orphan_list_entry(struct list_head *l)
1119 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1122 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1124 struct list_head *l;
1126 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1127 le32_to_cpu(sbi->s_es->s_last_orphan));
1129 printk(KERN_ERR "sb_info orphan list:\n");
1130 list_for_each(l, &sbi->s_orphan) {
1131 struct inode *inode = orphan_list_entry(l);
1133 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1134 inode->i_sb->s_id, inode->i_ino, inode,
1135 inode->i_mode, inode->i_nlink,
1136 NEXT_ORPHAN(inode));
1141 static int ext4_quota_off(struct super_block *sb, int type);
1143 static inline void ext4_quota_off_umount(struct super_block *sb)
1147 /* Use our quota_off function to clear inode flags etc. */
1148 for (type = 0; type < EXT4_MAXQUOTAS; type++)
1149 ext4_quota_off(sb, type);
1153 * This is a helper function which is used in the mount/remount
1154 * codepaths (which holds s_umount) to fetch the quota file name.
1156 static inline char *get_qf_name(struct super_block *sb,
1157 struct ext4_sb_info *sbi,
1160 return rcu_dereference_protected(sbi->s_qf_names[type],
1161 lockdep_is_held(&sb->s_umount));
1164 static inline void ext4_quota_off_umount(struct super_block *sb)
1169 static void ext4_put_super(struct super_block *sb)
1171 struct ext4_sb_info *sbi = EXT4_SB(sb);
1172 struct ext4_super_block *es = sbi->s_es;
1173 struct buffer_head **group_desc;
1174 struct flex_groups **flex_groups;
1178 ext4_unregister_li_request(sb);
1179 ext4_quota_off_umount(sb);
1181 flush_work(&sbi->s_error_work);
1182 destroy_workqueue(sbi->rsv_conversion_wq);
1183 ext4_release_orphan_info(sb);
1186 * Unregister sysfs before destroying jbd2 journal.
1187 * Since we could still access attr_journal_task attribute via sysfs
1188 * path which could have sbi->s_journal->j_task as NULL
1190 ext4_unregister_sysfs(sb);
1192 if (sbi->s_journal) {
1193 aborted = is_journal_aborted(sbi->s_journal);
1194 err = jbd2_journal_destroy(sbi->s_journal);
1195 sbi->s_journal = NULL;
1196 if ((err < 0) && !aborted) {
1197 ext4_abort(sb, -err, "Couldn't clean up the journal");
1201 ext4_es_unregister_shrinker(sbi);
1202 del_timer_sync(&sbi->s_err_report);
1203 ext4_release_system_zone(sb);
1204 ext4_mb_release(sb);
1205 ext4_ext_release(sb);
1207 if (!sb_rdonly(sb) && !aborted) {
1208 ext4_clear_feature_journal_needs_recovery(sb);
1209 ext4_clear_feature_orphan_present(sb);
1210 es->s_state = cpu_to_le16(sbi->s_mount_state);
1213 ext4_commit_super(sb);
1216 group_desc = rcu_dereference(sbi->s_group_desc);
1217 for (i = 0; i < sbi->s_gdb_count; i++)
1218 brelse(group_desc[i]);
1220 flex_groups = rcu_dereference(sbi->s_flex_groups);
1222 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1223 kvfree(flex_groups[i]);
1224 kvfree(flex_groups);
1227 percpu_counter_destroy(&sbi->s_freeclusters_counter);
1228 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1229 percpu_counter_destroy(&sbi->s_dirs_counter);
1230 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1231 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
1232 percpu_free_rwsem(&sbi->s_writepages_rwsem);
1234 for (i = 0; i < EXT4_MAXQUOTAS; i++)
1235 kfree(get_qf_name(sb, sbi, i));
1238 /* Debugging code just in case the in-memory inode orphan list
1239 * isn't empty. The on-disk one can be non-empty if we've
1240 * detected an error and taken the fs readonly, but the
1241 * in-memory list had better be clean by this point. */
1242 if (!list_empty(&sbi->s_orphan))
1243 dump_orphan_list(sb, sbi);
1244 ASSERT(list_empty(&sbi->s_orphan));
1246 sync_blockdev(sb->s_bdev);
1247 invalidate_bdev(sb->s_bdev);
1248 if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
1250 * Invalidate the journal device's buffers. We don't want them
1251 * floating about in memory - the physical journal device may
1252 * hotswapped, and it breaks the `ro-after' testing code.
1254 sync_blockdev(sbi->s_journal_bdev);
1255 invalidate_bdev(sbi->s_journal_bdev);
1256 ext4_blkdev_remove(sbi);
1259 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1260 sbi->s_ea_inode_cache = NULL;
1262 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
1263 sbi->s_ea_block_cache = NULL;
1265 ext4_stop_mmpd(sbi);
1268 sb->s_fs_info = NULL;
1270 * Now that we are completely done shutting down the
1271 * superblock, we need to actually destroy the kobject.
1273 kobject_put(&sbi->s_kobj);
1274 wait_for_completion(&sbi->s_kobj_unregister);
1275 if (sbi->s_chksum_driver)
1276 crypto_free_shash(sbi->s_chksum_driver);
1277 kfree(sbi->s_blockgroup_lock);
1278 fs_put_dax(sbi->s_daxdev);
1279 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
1280 #ifdef CONFIG_UNICODE
1281 utf8_unload(sb->s_encoding);
1286 static struct kmem_cache *ext4_inode_cachep;
1289 * Called inside transaction, so use GFP_NOFS
1291 static struct inode *ext4_alloc_inode(struct super_block *sb)
1293 struct ext4_inode_info *ei;
1295 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1299 inode_set_iversion(&ei->vfs_inode, 1);
1300 spin_lock_init(&ei->i_raw_lock);
1301 INIT_LIST_HEAD(&ei->i_prealloc_list);
1302 atomic_set(&ei->i_prealloc_active, 0);
1303 spin_lock_init(&ei->i_prealloc_lock);
1304 ext4_es_init_tree(&ei->i_es_tree);
1305 rwlock_init(&ei->i_es_lock);
1306 INIT_LIST_HEAD(&ei->i_es_list);
1307 ei->i_es_all_nr = 0;
1308 ei->i_es_shk_nr = 0;
1309 ei->i_es_shrink_lblk = 0;
1310 ei->i_reserved_data_blocks = 0;
1311 spin_lock_init(&(ei->i_block_reservation_lock));
1312 ext4_init_pending_tree(&ei->i_pending_tree);
1314 ei->i_reserved_quota = 0;
1315 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1318 INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1319 spin_lock_init(&ei->i_completed_io_lock);
1321 ei->i_datasync_tid = 0;
1322 atomic_set(&ei->i_unwritten, 0);
1323 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1324 ext4_fc_init_inode(&ei->vfs_inode);
1325 mutex_init(&ei->i_fc_lock);
1326 return &ei->vfs_inode;
1329 static int ext4_drop_inode(struct inode *inode)
1331 int drop = generic_drop_inode(inode);
1334 drop = fscrypt_drop_inode(inode);
1336 trace_ext4_drop_inode(inode, drop);
1340 static void ext4_free_in_core_inode(struct inode *inode)
1342 fscrypt_free_inode(inode);
1343 if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1344 pr_warn("%s: inode %ld still in fc list",
1345 __func__, inode->i_ino);
1347 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1350 static void ext4_destroy_inode(struct inode *inode)
1352 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1353 ext4_msg(inode->i_sb, KERN_ERR,
1354 "Inode %lu (%p): orphan list check failed!",
1355 inode->i_ino, EXT4_I(inode));
1356 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
1357 EXT4_I(inode), sizeof(struct ext4_inode_info),
1362 if (EXT4_I(inode)->i_reserved_data_blocks)
1363 ext4_msg(inode->i_sb, KERN_ERR,
1364 "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
1365 inode->i_ino, EXT4_I(inode),
1366 EXT4_I(inode)->i_reserved_data_blocks);
1369 static void init_once(void *foo)
1371 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1373 INIT_LIST_HEAD(&ei->i_orphan);
1374 init_rwsem(&ei->xattr_sem);
1375 init_rwsem(&ei->i_data_sem);
1376 inode_init_once(&ei->vfs_inode);
1377 ext4_fc_init_inode(&ei->vfs_inode);
1380 static int __init init_inodecache(void)
1382 ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1383 sizeof(struct ext4_inode_info), 0,
1384 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1386 offsetof(struct ext4_inode_info, i_data),
1387 sizeof_field(struct ext4_inode_info, i_data),
1389 if (ext4_inode_cachep == NULL)
1394 static void destroy_inodecache(void)
1397 * Make sure all delayed rcu free inodes are flushed before we
1401 kmem_cache_destroy(ext4_inode_cachep);
1404 void ext4_clear_inode(struct inode *inode)
1407 invalidate_inode_buffers(inode);
1409 ext4_discard_preallocations(inode, 0);
1410 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1412 if (EXT4_I(inode)->jinode) {
1413 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1414 EXT4_I(inode)->jinode);
1415 jbd2_free_inode(EXT4_I(inode)->jinode);
1416 EXT4_I(inode)->jinode = NULL;
1418 fscrypt_put_encryption_info(inode);
1419 fsverity_cleanup_inode(inode);
1422 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
1423 u64 ino, u32 generation)
1425 struct inode *inode;
1428 * Currently we don't know the generation for parent directory, so
1429 * a generation of 0 means "accept any"
1431 inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
1433 return ERR_CAST(inode);
1434 if (generation && inode->i_generation != generation) {
1436 return ERR_PTR(-ESTALE);
1442 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
1443 int fh_len, int fh_type)
1445 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1446 ext4_nfs_get_inode);
1449 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
1450 int fh_len, int fh_type)
1452 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1453 ext4_nfs_get_inode);
1456 static int ext4_nfs_commit_metadata(struct inode *inode)
1458 struct writeback_control wbc = {
1459 .sync_mode = WB_SYNC_ALL
1462 trace_ext4_nfs_commit_metadata(inode);
1463 return ext4_write_inode(inode, &wbc);
1466 #ifdef CONFIG_FS_ENCRYPTION
1467 static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1469 return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1470 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1473 static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1476 handle_t *handle = fs_data;
1477 int res, res2, credits, retries = 0;
1480 * Encrypting the root directory is not allowed because e2fsck expects
1481 * lost+found to exist and be unencrypted, and encrypting the root
1482 * directory would imply encrypting the lost+found directory as well as
1483 * the filename "lost+found" itself.
1485 if (inode->i_ino == EXT4_ROOT_INO)
1488 if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
1491 if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1494 res = ext4_convert_inline_data(inode);
1499 * If a journal handle was specified, then the encryption context is
1500 * being set on a new inode via inheritance and is part of a larger
1501 * transaction to create the inode. Otherwise the encryption context is
1502 * being set on an existing inode in its own transaction. Only in the
1503 * latter case should the "retry on ENOSPC" logic be used.
1507 res = ext4_xattr_set_handle(handle, inode,
1508 EXT4_XATTR_INDEX_ENCRYPTION,
1509 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1512 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1513 ext4_clear_inode_state(inode,
1514 EXT4_STATE_MAY_INLINE_DATA);
1516 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1517 * S_DAX may be disabled
1519 ext4_set_inode_flags(inode, false);
1524 res = dquot_initialize(inode);
1528 res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1533 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1535 return PTR_ERR(handle);
1537 res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
1538 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1541 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1543 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1544 * S_DAX may be disabled
1546 ext4_set_inode_flags(inode, false);
1547 res = ext4_mark_inode_dirty(handle, inode);
1549 EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1551 res2 = ext4_journal_stop(handle);
1553 if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
1560 static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
1562 return EXT4_SB(sb)->s_dummy_enc_policy.policy;
1565 static bool ext4_has_stable_inodes(struct super_block *sb)
1567 return ext4_has_feature_stable_inodes(sb);
1570 static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1571 int *ino_bits_ret, int *lblk_bits_ret)
1573 *ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1574 *lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1577 static const struct fscrypt_operations ext4_cryptops = {
1578 .key_prefix = "ext4:",
1579 .get_context = ext4_get_context,
1580 .set_context = ext4_set_context,
1581 .get_dummy_policy = ext4_get_dummy_policy,
1582 .empty_dir = ext4_empty_dir,
1583 .has_stable_inodes = ext4_has_stable_inodes,
1584 .get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits,
1589 static const char * const quotatypes[] = INITQFNAMES;
1590 #define QTYPE2NAME(t) (quotatypes[t])
1592 static int ext4_write_dquot(struct dquot *dquot);
1593 static int ext4_acquire_dquot(struct dquot *dquot);
1594 static int ext4_release_dquot(struct dquot *dquot);
1595 static int ext4_mark_dquot_dirty(struct dquot *dquot);
1596 static int ext4_write_info(struct super_block *sb, int type);
1597 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1598 const struct path *path);
1599 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1600 size_t len, loff_t off);
1601 static ssize_t ext4_quota_write(struct super_block *sb, int type,
1602 const char *data, size_t len, loff_t off);
1603 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1604 unsigned int flags);
1606 static struct dquot **ext4_get_dquots(struct inode *inode)
1608 return EXT4_I(inode)->i_dquot;
1611 static const struct dquot_operations ext4_quota_operations = {
1612 .get_reserved_space = ext4_get_reserved_space,
1613 .write_dquot = ext4_write_dquot,
1614 .acquire_dquot = ext4_acquire_dquot,
1615 .release_dquot = ext4_release_dquot,
1616 .mark_dirty = ext4_mark_dquot_dirty,
1617 .write_info = ext4_write_info,
1618 .alloc_dquot = dquot_alloc,
1619 .destroy_dquot = dquot_destroy,
1620 .get_projid = ext4_get_projid,
1621 .get_inode_usage = ext4_get_inode_usage,
1622 .get_next_id = dquot_get_next_id,
1625 static const struct quotactl_ops ext4_qctl_operations = {
1626 .quota_on = ext4_quota_on,
1627 .quota_off = ext4_quota_off,
1628 .quota_sync = dquot_quota_sync,
1629 .get_state = dquot_get_state,
1630 .set_info = dquot_set_dqinfo,
1631 .get_dqblk = dquot_get_dqblk,
1632 .set_dqblk = dquot_set_dqblk,
1633 .get_nextdqblk = dquot_get_next_dqblk,
1637 static const struct super_operations ext4_sops = {
1638 .alloc_inode = ext4_alloc_inode,
1639 .free_inode = ext4_free_in_core_inode,
1640 .destroy_inode = ext4_destroy_inode,
1641 .write_inode = ext4_write_inode,
1642 .dirty_inode = ext4_dirty_inode,
1643 .drop_inode = ext4_drop_inode,
1644 .evict_inode = ext4_evict_inode,
1645 .put_super = ext4_put_super,
1646 .sync_fs = ext4_sync_fs,
1647 .freeze_fs = ext4_freeze,
1648 .unfreeze_fs = ext4_unfreeze,
1649 .statfs = ext4_statfs,
1650 .remount_fs = ext4_remount,
1651 .show_options = ext4_show_options,
1653 .quota_read = ext4_quota_read,
1654 .quota_write = ext4_quota_write,
1655 .get_dquots = ext4_get_dquots,
1659 static const struct export_operations ext4_export_ops = {
1660 .fh_to_dentry = ext4_fh_to_dentry,
1661 .fh_to_parent = ext4_fh_to_parent,
1662 .get_parent = ext4_get_parent,
1663 .commit_metadata = ext4_nfs_commit_metadata,
1667 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1668 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
1669 Opt_nouid32, Opt_debug, Opt_removed,
1670 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1671 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1672 Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1673 Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1674 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1675 Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1677 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1678 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1679 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1680 Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
1681 Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1682 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1683 Opt_nowarn_on_error, Opt_mblk_io_submit,
1684 Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
1685 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
1686 Opt_inode_readahead_blks, Opt_journal_ioprio,
1687 Opt_dioread_nolock, Opt_dioread_lock,
1688 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1689 Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1690 Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
1691 #ifdef CONFIG_EXT4_DEBUG
1692 Opt_fc_debug_max_replay, Opt_fc_debug_force
1696 static const match_table_t tokens = {
1697 {Opt_bsd_df, "bsddf"},
1698 {Opt_minix_df, "minixdf"},
1699 {Opt_grpid, "grpid"},
1700 {Opt_grpid, "bsdgroups"},
1701 {Opt_nogrpid, "nogrpid"},
1702 {Opt_nogrpid, "sysvgroups"},
1703 {Opt_resgid, "resgid=%u"},
1704 {Opt_resuid, "resuid=%u"},
1706 {Opt_err_cont, "errors=continue"},
1707 {Opt_err_panic, "errors=panic"},
1708 {Opt_err_ro, "errors=remount-ro"},
1709 {Opt_nouid32, "nouid32"},
1710 {Opt_debug, "debug"},
1711 {Opt_removed, "oldalloc"},
1712 {Opt_removed, "orlov"},
1713 {Opt_user_xattr, "user_xattr"},
1714 {Opt_nouser_xattr, "nouser_xattr"},
1716 {Opt_noacl, "noacl"},
1717 {Opt_noload, "norecovery"},
1718 {Opt_noload, "noload"},
1719 {Opt_removed, "nobh"},
1720 {Opt_removed, "bh"},
1721 {Opt_commit, "commit=%u"},
1722 {Opt_min_batch_time, "min_batch_time=%u"},
1723 {Opt_max_batch_time, "max_batch_time=%u"},
1724 {Opt_journal_dev, "journal_dev=%u"},
1725 {Opt_journal_path, "journal_path=%s"},
1726 {Opt_journal_checksum, "journal_checksum"},
1727 {Opt_nojournal_checksum, "nojournal_checksum"},
1728 {Opt_journal_async_commit, "journal_async_commit"},
1729 {Opt_abort, "abort"},
1730 {Opt_data_journal, "data=journal"},
1731 {Opt_data_ordered, "data=ordered"},
1732 {Opt_data_writeback, "data=writeback"},
1733 {Opt_data_err_abort, "data_err=abort"},
1734 {Opt_data_err_ignore, "data_err=ignore"},
1735 {Opt_offusrjquota, "usrjquota="},
1736 {Opt_usrjquota, "usrjquota=%s"},
1737 {Opt_offgrpjquota, "grpjquota="},
1738 {Opt_grpjquota, "grpjquota=%s"},
1739 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1740 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1741 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1742 {Opt_grpquota, "grpquota"},
1743 {Opt_noquota, "noquota"},
1744 {Opt_quota, "quota"},
1745 {Opt_usrquota, "usrquota"},
1746 {Opt_prjquota, "prjquota"},
1747 {Opt_barrier, "barrier=%u"},
1748 {Opt_barrier, "barrier"},
1749 {Opt_nobarrier, "nobarrier"},
1750 {Opt_i_version, "i_version"},
1752 {Opt_dax_always, "dax=always"},
1753 {Opt_dax_inode, "dax=inode"},
1754 {Opt_dax_never, "dax=never"},
1755 {Opt_stripe, "stripe=%u"},
1756 {Opt_delalloc, "delalloc"},
1757 {Opt_warn_on_error, "warn_on_error"},
1758 {Opt_nowarn_on_error, "nowarn_on_error"},
1759 {Opt_lazytime, "lazytime"},
1760 {Opt_nolazytime, "nolazytime"},
1761 {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1762 {Opt_nodelalloc, "nodelalloc"},
1763 {Opt_removed, "mblk_io_submit"},
1764 {Opt_removed, "nomblk_io_submit"},
1765 {Opt_block_validity, "block_validity"},
1766 {Opt_noblock_validity, "noblock_validity"},
1767 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1768 {Opt_journal_ioprio, "journal_ioprio=%u"},
1769 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
1770 {Opt_auto_da_alloc, "auto_da_alloc"},
1771 {Opt_noauto_da_alloc, "noauto_da_alloc"},
1772 {Opt_dioread_nolock, "dioread_nolock"},
1773 {Opt_dioread_lock, "nodioread_nolock"},
1774 {Opt_dioread_lock, "dioread_lock"},
1775 {Opt_discard, "discard"},
1776 {Opt_nodiscard, "nodiscard"},
1777 {Opt_init_itable, "init_itable=%u"},
1778 {Opt_init_itable, "init_itable"},
1779 {Opt_noinit_itable, "noinit_itable"},
1780 #ifdef CONFIG_EXT4_DEBUG
1781 {Opt_fc_debug_force, "fc_debug_force"},
1782 {Opt_fc_debug_max_replay, "fc_debug_max_replay=%u"},
1784 {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1785 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
1786 {Opt_test_dummy_encryption, "test_dummy_encryption"},
1787 {Opt_inlinecrypt, "inlinecrypt"},
1788 {Opt_nombcache, "nombcache"},
1789 {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */
1790 {Opt_removed, "prefetch_block_bitmaps"},
1791 {Opt_no_prefetch_block_bitmaps, "no_prefetch_block_bitmaps"},
1792 {Opt_mb_optimize_scan, "mb_optimize_scan=%d"},
1793 {Opt_removed, "check=none"}, /* mount option from ext2/3 */
1794 {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
1795 {Opt_removed, "reservation"}, /* mount option from ext2/3 */
1796 {Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1797 {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */
1801 static ext4_fsblk_t get_sb_block(void **data)
1803 ext4_fsblk_t sb_block;
1804 char *options = (char *) *data;
1806 if (!options || strncmp(options, "sb=", 3) != 0)
1807 return 1; /* Default location */
1810 /* TODO: use simple_strtoll with >32bit ext4 */
1811 sb_block = simple_strtoul(options, &options, 0);
1812 if (*options && *options != ',') {
1813 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1817 if (*options == ',')
1819 *data = (void *) options;
1824 #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1825 #define DEFAULT_MB_OPTIMIZE_SCAN (-1)
1827 static const char deprecated_msg[] =
1828 "Mount option \"%s\" will be removed by %s\n"
1832 static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1834 struct ext4_sb_info *sbi = EXT4_SB(sb);
1835 char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
1838 if (sb_any_quota_loaded(sb) && !old_qname) {
1839 ext4_msg(sb, KERN_ERR,
1840 "Cannot change journaled "
1841 "quota options when quota turned on");
1844 if (ext4_has_feature_quota(sb)) {
1845 ext4_msg(sb, KERN_INFO, "Journaled quota options "
1846 "ignored when QUOTA feature is enabled");
1849 qname = match_strdup(args);
1851 ext4_msg(sb, KERN_ERR,
1852 "Not enough memory for storing quotafile name");
1856 if (strcmp(old_qname, qname) == 0)
1859 ext4_msg(sb, KERN_ERR,
1860 "%s quota file already specified",
1864 if (strchr(qname, '/')) {
1865 ext4_msg(sb, KERN_ERR,
1866 "quotafile must be on filesystem root");
1869 rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1877 static int clear_qf_name(struct super_block *sb, int qtype)
1880 struct ext4_sb_info *sbi = EXT4_SB(sb);
1881 char *old_qname = get_qf_name(sb, sbi, qtype);
1883 if (sb_any_quota_loaded(sb) && old_qname) {
1884 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1885 " when quota turned on");
1888 rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
1895 #define MOPT_SET 0x0001
1896 #define MOPT_CLEAR 0x0002
1897 #define MOPT_NOSUPPORT 0x0004
1898 #define MOPT_EXPLICIT 0x0008
1899 #define MOPT_CLEAR_ERR 0x0010
1900 #define MOPT_GTE0 0x0020
1903 #define MOPT_QFMT 0x0040
1905 #define MOPT_Q MOPT_NOSUPPORT
1906 #define MOPT_QFMT MOPT_NOSUPPORT
1908 #define MOPT_DATAJ 0x0080
1909 #define MOPT_NO_EXT2 0x0100
1910 #define MOPT_NO_EXT3 0x0200
1911 #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
1912 #define MOPT_STRING 0x0400
1913 #define MOPT_SKIP 0x0800
1914 #define MOPT_2 0x1000
1916 static const struct mount_opts {
1920 } ext4_mount_opts[] = {
1921 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1922 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1923 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1924 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
1925 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1926 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
1927 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1928 MOPT_EXT4_ONLY | MOPT_SET},
1929 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1930 MOPT_EXT4_ONLY | MOPT_CLEAR},
1931 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1932 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
1933 {Opt_delalloc, EXT4_MOUNT_DELALLOC,
1934 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1935 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
1936 MOPT_EXT4_ONLY | MOPT_CLEAR},
1937 {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1938 {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1939 {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1940 MOPT_EXT4_ONLY | MOPT_CLEAR},
1941 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1942 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1943 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
1944 EXT4_MOUNT_JOURNAL_CHECKSUM),
1945 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1946 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
1947 {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
1948 {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
1949 {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
1950 {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
1952 {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
1954 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1955 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1956 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1957 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1958 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1959 {Opt_commit, 0, MOPT_GTE0},
1960 {Opt_max_batch_time, 0, MOPT_GTE0},
1961 {Opt_min_batch_time, 0, MOPT_GTE0},
1962 {Opt_inode_readahead_blks, 0, MOPT_GTE0},
1963 {Opt_init_itable, 0, MOPT_GTE0},
1964 {Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
1965 {Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
1966 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1967 {Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
1968 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1969 {Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
1970 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1971 {Opt_stripe, 0, MOPT_GTE0},
1972 {Opt_resuid, 0, MOPT_GTE0},
1973 {Opt_resgid, 0, MOPT_GTE0},
1974 {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
1975 {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
1976 {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
1977 {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1978 {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1979 {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
1980 MOPT_NO_EXT2 | MOPT_DATAJ},
1981 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1982 {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
1983 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1984 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1985 {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
1987 {Opt_acl, 0, MOPT_NOSUPPORT},
1988 {Opt_noacl, 0, MOPT_NOSUPPORT},
1990 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1991 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1992 {Opt_debug_want_extra_isize, 0, MOPT_GTE0},
1993 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1994 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1996 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1998 {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
2000 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
2001 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
2002 MOPT_CLEAR | MOPT_Q},
2003 {Opt_usrjquota, 0, MOPT_Q | MOPT_STRING},
2004 {Opt_grpjquota, 0, MOPT_Q | MOPT_STRING},
2005 {Opt_offusrjquota, 0, MOPT_Q},
2006 {Opt_offgrpjquota, 0, MOPT_Q},
2007 {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
2008 {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
2009 {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
2010 {Opt_max_dir_size_kb, 0, MOPT_GTE0},
2011 {Opt_test_dummy_encryption, 0, MOPT_STRING},
2012 {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
2013 {Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
2015 {Opt_mb_optimize_scan, EXT4_MOUNT2_MB_OPTIMIZE_SCAN, MOPT_GTE0},
2016 #ifdef CONFIG_EXT4_DEBUG
2017 {Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
2018 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
2019 {Opt_fc_debug_max_replay, 0, MOPT_GTE0},
2024 #ifdef CONFIG_UNICODE
2025 static const struct ext4_sb_encodings {
2029 } ext4_sb_encoding_map[] = {
2030 {EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
2033 static int ext4_sb_read_encoding(const struct ext4_super_block *es,
2034 const struct ext4_sb_encodings **encoding,
2037 __u16 magic = le16_to_cpu(es->s_encoding);
2040 for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
2041 if (magic == ext4_sb_encoding_map[i].magic)
2044 if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
2047 *encoding = &ext4_sb_encoding_map[i];
2048 *flags = le16_to_cpu(es->s_encoding_flags);
2054 static int ext4_set_test_dummy_encryption(struct super_block *sb,
2056 const substring_t *arg,
2059 #ifdef CONFIG_FS_ENCRYPTION
2060 struct ext4_sb_info *sbi = EXT4_SB(sb);
2064 * This mount option is just for testing, and it's not worthwhile to
2065 * implement the extra complexity (e.g. RCU protection) that would be
2066 * needed to allow it to be set or changed during remount. We do allow
2067 * it to be specified during remount, but only if there is no change.
2069 if (is_remount && !sbi->s_dummy_enc_policy.policy) {
2070 ext4_msg(sb, KERN_WARNING,
2071 "Can't set test_dummy_encryption on remount");
2074 err = fscrypt_set_test_dummy_encryption(sb, arg->from,
2075 &sbi->s_dummy_enc_policy);
2078 ext4_msg(sb, KERN_WARNING,
2079 "Can't change test_dummy_encryption on remount");
2080 else if (err == -EINVAL)
2081 ext4_msg(sb, KERN_WARNING,
2082 "Value of option \"%s\" is unrecognized", opt);
2084 ext4_msg(sb, KERN_WARNING,
2085 "Error processing option \"%s\" [%d]",
2089 ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2091 ext4_msg(sb, KERN_WARNING,
2092 "Test dummy encryption mount option ignored");
2097 struct ext4_parsed_options {
2098 unsigned long journal_devnum;
2099 unsigned int journal_ioprio;
2100 int mb_optimize_scan;
2103 static int handle_mount_opt(struct super_block *sb, char *opt, int token,
2104 substring_t *args, struct ext4_parsed_options *parsed_opts,
2107 struct ext4_sb_info *sbi = EXT4_SB(sb);
2108 const struct mount_opts *m;
2114 if (token == Opt_usrjquota)
2115 return set_qf_name(sb, USRQUOTA, &args[0]);
2116 else if (token == Opt_grpjquota)
2117 return set_qf_name(sb, GRPQUOTA, &args[0]);
2118 else if (token == Opt_offusrjquota)
2119 return clear_qf_name(sb, USRQUOTA);
2120 else if (token == Opt_offgrpjquota)
2121 return clear_qf_name(sb, GRPQUOTA);
2125 case Opt_nouser_xattr:
2126 ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
2129 return 1; /* handled by get_sb_block() */
2131 ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
2134 ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
2137 sb->s_flags |= SB_I_VERSION;
2140 sb->s_flags |= SB_LAZYTIME;
2142 case Opt_nolazytime:
2143 sb->s_flags &= ~SB_LAZYTIME;
2145 case Opt_inlinecrypt:
2146 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
2147 sb->s_flags |= SB_INLINECRYPT;
2149 ext4_msg(sb, KERN_ERR, "inline encryption not supported");
2154 for (m = ext4_mount_opts; m->token != Opt_err; m++)
2155 if (token == m->token)
2158 if (m->token == Opt_err) {
2159 ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
2160 "or missing value", opt);
2164 if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2165 ext4_msg(sb, KERN_ERR,
2166 "Mount option \"%s\" incompatible with ext2", opt);
2169 if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2170 ext4_msg(sb, KERN_ERR,
2171 "Mount option \"%s\" incompatible with ext3", opt);
2175 if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
2177 if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
2179 if (m->flags & MOPT_EXPLICIT) {
2180 if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2181 set_opt2(sb, EXPLICIT_DELALLOC);
2182 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2183 set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
2187 if (m->flags & MOPT_CLEAR_ERR)
2188 clear_opt(sb, ERRORS_MASK);
2189 if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
2190 ext4_msg(sb, KERN_ERR, "Cannot change quota "
2191 "options when quota turned on");
2195 if (m->flags & MOPT_NOSUPPORT) {
2196 ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
2197 } else if (token == Opt_commit) {
2199 arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
2200 else if (arg > INT_MAX / HZ) {
2201 ext4_msg(sb, KERN_ERR,
2202 "Invalid commit interval %d, "
2203 "must be smaller than %d",
2207 sbi->s_commit_interval = HZ * arg;
2208 } else if (token == Opt_debug_want_extra_isize) {
2211 (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
2212 ext4_msg(sb, KERN_ERR,
2213 "Invalid want_extra_isize %d", arg);
2216 sbi->s_want_extra_isize = arg;
2217 } else if (token == Opt_max_batch_time) {
2218 sbi->s_max_batch_time = arg;
2219 } else if (token == Opt_min_batch_time) {
2220 sbi->s_min_batch_time = arg;
2221 } else if (token == Opt_inode_readahead_blks) {
2222 if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2223 ext4_msg(sb, KERN_ERR,
2224 "EXT4-fs: inode_readahead_blks must be "
2225 "0 or a power of 2 smaller than 2^31");
2228 sbi->s_inode_readahead_blks = arg;
2229 } else if (token == Opt_init_itable) {
2230 set_opt(sb, INIT_INODE_TABLE);
2232 arg = EXT4_DEF_LI_WAIT_MULT;
2233 sbi->s_li_wait_mult = arg;
2234 } else if (token == Opt_max_dir_size_kb) {
2235 sbi->s_max_dir_size_kb = arg;
2236 #ifdef CONFIG_EXT4_DEBUG
2237 } else if (token == Opt_fc_debug_max_replay) {
2238 sbi->s_fc_debug_max_replay = arg;
2240 } else if (token == Opt_stripe) {
2241 sbi->s_stripe = arg;
2242 } else if (token == Opt_resuid) {
2243 uid = make_kuid(current_user_ns(), arg);
2244 if (!uid_valid(uid)) {
2245 ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
2248 sbi->s_resuid = uid;
2249 } else if (token == Opt_resgid) {
2250 gid = make_kgid(current_user_ns(), arg);
2251 if (!gid_valid(gid)) {
2252 ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
2255 sbi->s_resgid = gid;
2256 } else if (token == Opt_journal_dev) {
2258 ext4_msg(sb, KERN_ERR,
2259 "Cannot specify journal on remount");
2262 parsed_opts->journal_devnum = arg;
2263 } else if (token == Opt_journal_path) {
2265 struct inode *journal_inode;
2270 ext4_msg(sb, KERN_ERR,
2271 "Cannot specify journal on remount");
2274 journal_path = match_strdup(&args[0]);
2275 if (!journal_path) {
2276 ext4_msg(sb, KERN_ERR, "error: could not dup "
2277 "journal device string");
2281 error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2283 ext4_msg(sb, KERN_ERR, "error: could not find "
2284 "journal device path: error %d", error);
2285 kfree(journal_path);
2289 journal_inode = d_inode(path.dentry);
2290 if (!S_ISBLK(journal_inode->i_mode)) {
2291 ext4_msg(sb, KERN_ERR, "error: journal path %s "
2292 "is not a block device", journal_path);
2294 kfree(journal_path);
2298 parsed_opts->journal_devnum = new_encode_dev(journal_inode->i_rdev);
2300 kfree(journal_path);
2301 } else if (token == Opt_journal_ioprio) {
2303 ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
2307 parsed_opts->journal_ioprio =
2308 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
2309 } else if (token == Opt_test_dummy_encryption) {
2310 return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2312 } else if (m->flags & MOPT_DATAJ) {
2314 if (!sbi->s_journal)
2315 ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
2316 else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
2317 ext4_msg(sb, KERN_ERR,
2318 "Cannot change data mode on remount");
2322 clear_opt(sb, DATA_FLAGS);
2323 sbi->s_mount_opt |= m->mount_opt;
2326 } else if (m->flags & MOPT_QFMT) {
2327 if (sb_any_quota_loaded(sb) &&
2328 sbi->s_jquota_fmt != m->mount_opt) {
2329 ext4_msg(sb, KERN_ERR, "Cannot change journaled "
2330 "quota options when quota turned on");
2333 if (ext4_has_feature_quota(sb)) {
2334 ext4_msg(sb, KERN_INFO,
2335 "Quota format mount options ignored "
2336 "when QUOTA feature is enabled");
2339 sbi->s_jquota_fmt = m->mount_opt;
2341 } else if (token == Opt_dax || token == Opt_dax_always ||
2342 token == Opt_dax_inode || token == Opt_dax_never) {
2343 #ifdef CONFIG_FS_DAX
2346 case Opt_dax_always:
2348 (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2349 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2350 fail_dax_change_remount:
2351 ext4_msg(sb, KERN_ERR, "can't change "
2352 "dax mount option while remounting");
2356 (test_opt(sb, DATA_FLAGS) ==
2357 EXT4_MOUNT_JOURNAL_DATA)) {
2358 ext4_msg(sb, KERN_ERR, "can't mount with "
2359 "both data=journal and dax");
2362 ext4_msg(sb, KERN_WARNING,
2363 "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2364 sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
2365 sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2369 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2370 (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2371 goto fail_dax_change_remount;
2372 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2373 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2377 ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2378 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2379 !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2380 goto fail_dax_change_remount;
2381 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2382 sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2383 /* Strictly for printing options */
2384 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
2388 ext4_msg(sb, KERN_INFO, "dax option not supported");
2389 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2390 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2393 } else if (token == Opt_data_err_abort) {
2394 sbi->s_mount_opt |= m->mount_opt;
2395 } else if (token == Opt_data_err_ignore) {
2396 sbi->s_mount_opt &= ~m->mount_opt;
2397 } else if (token == Opt_mb_optimize_scan) {
2398 if (arg != 0 && arg != 1) {
2399 ext4_msg(sb, KERN_WARNING,
2400 "mb_optimize_scan should be set to 0 or 1.");
2403 parsed_opts->mb_optimize_scan = arg;
2407 if (m->flags & MOPT_CLEAR)
2409 else if (unlikely(!(m->flags & MOPT_SET))) {
2410 ext4_msg(sb, KERN_WARNING,
2411 "buggy handling of option %s", opt);
2415 if (m->flags & MOPT_2) {
2417 sbi->s_mount_opt2 |= m->mount_opt;
2419 sbi->s_mount_opt2 &= ~m->mount_opt;
2422 sbi->s_mount_opt |= m->mount_opt;
2424 sbi->s_mount_opt &= ~m->mount_opt;
2430 static int parse_options(char *options, struct super_block *sb,
2431 struct ext4_parsed_options *ret_opts,
2434 struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
2435 char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2436 substring_t args[MAX_OPT_ARGS];
2442 while ((p = strsep(&options, ",")) != NULL) {
2446 * Initialize args struct so we know whether arg was
2447 * found; some options take optional arguments.
2449 args[0].to = args[0].from = NULL;
2450 token = match_token(p, tokens, args);
2451 if (handle_mount_opt(sb, p, token, args, ret_opts,
2457 * We do the test below only for project quotas. 'usrquota' and
2458 * 'grpquota' mount options are allowed even without quota feature
2459 * to support legacy quotas in quota files.
2461 if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
2462 ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
2463 "Cannot enable project quota enforcement.");
2466 usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
2467 grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
2468 if (usr_qf_name || grp_qf_name) {
2469 if (test_opt(sb, USRQUOTA) && usr_qf_name)
2470 clear_opt(sb, USRQUOTA);
2472 if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2473 clear_opt(sb, GRPQUOTA);
2475 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2476 ext4_msg(sb, KERN_ERR, "old and new quota "
2481 if (!sbi->s_jquota_fmt) {
2482 ext4_msg(sb, KERN_ERR, "journaled quota format "
2488 if (test_opt(sb, DIOREAD_NOLOCK)) {
2490 BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2491 if (blocksize < PAGE_SIZE)
2492 ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2493 "experimental mount option 'dioread_nolock' "
2494 "for blocksize < PAGE_SIZE");
2499 static inline void ext4_show_quota_options(struct seq_file *seq,
2500 struct super_block *sb)
2502 #if defined(CONFIG_QUOTA)
2503 struct ext4_sb_info *sbi = EXT4_SB(sb);
2504 char *usr_qf_name, *grp_qf_name;
2506 if (sbi->s_jquota_fmt) {
2509 switch (sbi->s_jquota_fmt) {
2520 seq_printf(seq, ",jqfmt=%s", fmtname);
2524 usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
2525 grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
2527 seq_show_option(seq, "usrjquota", usr_qf_name);
2529 seq_show_option(seq, "grpjquota", grp_qf_name);
2534 static const char *token2str(int token)
2536 const struct match_token *t;
2538 for (t = tokens; t->token != Opt_err; t++)
2539 if (t->token == token && !strchr(t->pattern, '='))
2546 * - it's set to a non-default value OR
2547 * - if the per-sb default is different from the global default
2549 static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
2552 struct ext4_sb_info *sbi = EXT4_SB(sb);
2553 struct ext4_super_block *es = sbi->s_es;
2554 int def_errors, def_mount_opt = sbi->s_def_mount_opt;
2555 const struct mount_opts *m;
2556 char sep = nodefs ? '\n' : ',';
2558 #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
2559 #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
2561 if (sbi->s_sb_block != 1)
2562 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
2564 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
2565 int want_set = m->flags & MOPT_SET;
2566 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
2567 (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
2569 if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
2570 continue; /* skip if same as the default */
2572 (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
2573 (!want_set && (sbi->s_mount_opt & m->mount_opt)))
2574 continue; /* select Opt_noFoo vs Opt_Foo */
2575 SEQ_OPTS_PRINT("%s", token2str(m->token));
2578 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
2579 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
2580 SEQ_OPTS_PRINT("resuid=%u",
2581 from_kuid_munged(&init_user_ns, sbi->s_resuid));
2582 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
2583 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
2584 SEQ_OPTS_PRINT("resgid=%u",
2585 from_kgid_munged(&init_user_ns, sbi->s_resgid));
2586 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
2587 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
2588 SEQ_OPTS_PUTS("errors=remount-ro");
2589 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
2590 SEQ_OPTS_PUTS("errors=continue");
2591 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
2592 SEQ_OPTS_PUTS("errors=panic");
2593 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
2594 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
2595 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
2596 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
2597 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
2598 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2599 if (sb->s_flags & SB_I_VERSION)
2600 SEQ_OPTS_PUTS("i_version");
2601 if (nodefs || sbi->s_stripe)
2602 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
2603 if (nodefs || EXT4_MOUNT_DATA_FLAGS &
2604 (sbi->s_mount_opt ^ def_mount_opt)) {
2605 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2606 SEQ_OPTS_PUTS("data=journal");
2607 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2608 SEQ_OPTS_PUTS("data=ordered");
2609 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
2610 SEQ_OPTS_PUTS("data=writeback");
2613 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
2614 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
2615 sbi->s_inode_readahead_blks);
2617 if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
2618 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
2619 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2620 if (nodefs || sbi->s_max_dir_size_kb)
2621 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
2622 if (test_opt(sb, DATA_ERR_ABORT))
2623 SEQ_OPTS_PUTS("data_err=abort");
2625 fscrypt_show_test_dummy_encryption(seq, sep, sb);
2627 if (sb->s_flags & SB_INLINECRYPT)
2628 SEQ_OPTS_PUTS("inlinecrypt");
2630 if (test_opt(sb, DAX_ALWAYS)) {
2632 SEQ_OPTS_PUTS("dax");
2634 SEQ_OPTS_PUTS("dax=always");
2635 } else if (test_opt2(sb, DAX_NEVER)) {
2636 SEQ_OPTS_PUTS("dax=never");
2637 } else if (test_opt2(sb, DAX_INODE)) {
2638 SEQ_OPTS_PUTS("dax=inode");
2640 ext4_show_quota_options(seq, sb);
2644 static int ext4_show_options(struct seq_file *seq, struct dentry *root)
2646 return _ext4_show_options(seq, root->d_sb, 0);
2649 int ext4_seq_options_show(struct seq_file *seq, void *offset)
2651 struct super_block *sb = seq->private;
2654 seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
2655 rc = _ext4_show_options(seq, sb, 1);
2656 seq_puts(seq, "\n");
2660 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2663 struct ext4_sb_info *sbi = EXT4_SB(sb);
2666 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2667 ext4_msg(sb, KERN_ERR, "revision level too high, "
2668 "forcing read-only mode");
2674 if (!(sbi->s_mount_state & EXT4_VALID_FS))
2675 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2676 "running e2fsck is recommended");
2677 else if (sbi->s_mount_state & EXT4_ERROR_FS)
2678 ext4_msg(sb, KERN_WARNING,
2679 "warning: mounting fs with errors, "
2680 "running e2fsck is recommended");
2681 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2682 le16_to_cpu(es->s_mnt_count) >=
2683 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2684 ext4_msg(sb, KERN_WARNING,
2685 "warning: maximal mount count reached, "
2686 "running e2fsck is recommended");
2687 else if (le32_to_cpu(es->s_checkinterval) &&
2688 (ext4_get_tstamp(es, s_lastcheck) +
2689 le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2690 ext4_msg(sb, KERN_WARNING,
2691 "warning: checktime reached, "
2692 "running e2fsck is recommended");
2693 if (!sbi->s_journal)
2694 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2695 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2696 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2697 le16_add_cpu(&es->s_mnt_count, 1);
2698 ext4_update_tstamp(es, s_mtime);
2699 if (sbi->s_journal) {
2700 ext4_set_feature_journal_needs_recovery(sb);
2701 if (ext4_has_feature_orphan_file(sb))
2702 ext4_set_feature_orphan_present(sb);
2705 err = ext4_commit_super(sb);
2707 if (test_opt(sb, DEBUG))
2708 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2709 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2711 sbi->s_groups_count,
2712 EXT4_BLOCKS_PER_GROUP(sb),
2713 EXT4_INODES_PER_GROUP(sb),
2714 sbi->s_mount_opt, sbi->s_mount_opt2);
2716 cleancache_init_fs(sb);
2720 int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2722 struct ext4_sb_info *sbi = EXT4_SB(sb);
2723 struct flex_groups **old_groups, **new_groups;
2726 if (!sbi->s_log_groups_per_flex)
2729 size = ext4_flex_group(sbi, ngroup - 1) + 1;
2730 if (size <= sbi->s_flex_groups_allocated)
2733 new_groups = kvzalloc(roundup_pow_of_two(size *
2734 sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2736 ext4_msg(sb, KERN_ERR,
2737 "not enough memory for %d flex group pointers", size);
2740 for (i = sbi->s_flex_groups_allocated; i < size; i++) {
2741 new_groups[i] = kvzalloc(roundup_pow_of_two(
2742 sizeof(struct flex_groups)),
2744 if (!new_groups[i]) {
2745 for (j = sbi->s_flex_groups_allocated; j < i; j++)
2746 kvfree(new_groups[j]);
2748 ext4_msg(sb, KERN_ERR,
2749 "not enough memory for %d flex groups", size);
2754 old_groups = rcu_dereference(sbi->s_flex_groups);
2756 memcpy(new_groups, old_groups,
2757 (sbi->s_flex_groups_allocated *
2758 sizeof(struct flex_groups *)));
2760 rcu_assign_pointer(sbi->s_flex_groups, new_groups);
2761 sbi->s_flex_groups_allocated = size;
2763 ext4_kvfree_array_rcu(old_groups);
2767 static int ext4_fill_flex_info(struct super_block *sb)
2769 struct ext4_sb_info *sbi = EXT4_SB(sb);
2770 struct ext4_group_desc *gdp = NULL;
2771 struct flex_groups *fg;
2772 ext4_group_t flex_group;
2775 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2776 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2777 sbi->s_log_groups_per_flex = 0;
2781 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2785 for (i = 0; i < sbi->s_groups_count; i++) {
2786 gdp = ext4_get_group_desc(sb, i, NULL);
2788 flex_group = ext4_flex_group(sbi, i);
2789 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
2790 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
2791 atomic64_add(ext4_free_group_clusters(sb, gdp),
2792 &fg->free_clusters);
2793 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2801 static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2802 struct ext4_group_desc *gdp)
2804 int offset = offsetof(struct ext4_group_desc, bg_checksum);
2806 __le32 le_group = cpu_to_le32(block_group);
2807 struct ext4_sb_info *sbi = EXT4_SB(sb);
2809 if (ext4_has_metadata_csum(sbi->s_sb)) {
2810 /* Use new metadata_csum algorithm */
2812 __u16 dummy_csum = 0;
2814 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2816 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2817 csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2818 sizeof(dummy_csum));
2819 offset += sizeof(dummy_csum);
2820 if (offset < sbi->s_desc_size)
2821 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2822 sbi->s_desc_size - offset);
2824 crc = csum32 & 0xFFFF;
2828 /* old crc16 code */
2829 if (!ext4_has_feature_gdt_csum(sb))
2832 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2833 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2834 crc = crc16(crc, (__u8 *)gdp, offset);
2835 offset += sizeof(gdp->bg_checksum); /* skip checksum */
2836 /* for checksum of struct ext4_group_desc do the rest...*/
2837 if (ext4_has_feature_64bit(sb) &&
2838 offset < le16_to_cpu(sbi->s_es->s_desc_size))
2839 crc = crc16(crc, (__u8 *)gdp + offset,
2840 le16_to_cpu(sbi->s_es->s_desc_size) -
2844 return cpu_to_le16(crc);
2847 int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2848 struct ext4_group_desc *gdp)
2850 if (ext4_has_group_desc_csum(sb) &&
2851 (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2857 void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2858 struct ext4_group_desc *gdp)
2860 if (!ext4_has_group_desc_csum(sb))
2862 gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2865 /* Called at mount-time, super-block is locked */
2866 static int ext4_check_descriptors(struct super_block *sb,
2867 ext4_fsblk_t sb_block,
2868 ext4_group_t *first_not_zeroed)
2870 struct ext4_sb_info *sbi = EXT4_SB(sb);
2871 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2872 ext4_fsblk_t last_block;
2873 ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2874 ext4_fsblk_t block_bitmap;
2875 ext4_fsblk_t inode_bitmap;
2876 ext4_fsblk_t inode_table;
2877 int flexbg_flag = 0;
2878 ext4_group_t i, grp = sbi->s_groups_count;
2880 if (ext4_has_feature_flex_bg(sb))
2883 ext4_debug("Checking group descriptors");
2885 for (i = 0; i < sbi->s_groups_count; i++) {
2886 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2888 if (i == sbi->s_groups_count - 1 || flexbg_flag)
2889 last_block = ext4_blocks_count(sbi->s_es) - 1;
2891 last_block = first_block +
2892 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
2894 if ((grp == sbi->s_groups_count) &&
2895 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2898 block_bitmap = ext4_block_bitmap(sb, gdp);
2899 if (block_bitmap == sb_block) {
2900 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2901 "Block bitmap for group %u overlaps "
2906 if (block_bitmap >= sb_block + 1 &&
2907 block_bitmap <= last_bg_block) {
2908 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2909 "Block bitmap for group %u overlaps "
2910 "block group descriptors", i);
2914 if (block_bitmap < first_block || block_bitmap > last_block) {
2915 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2916 "Block bitmap for group %u not in group "
2917 "(block %llu)!", i, block_bitmap);
2920 inode_bitmap = ext4_inode_bitmap(sb, gdp);
2921 if (inode_bitmap == sb_block) {
2922 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2923 "Inode bitmap for group %u overlaps "
2928 if (inode_bitmap >= sb_block + 1 &&
2929 inode_bitmap <= last_bg_block) {
2930 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2931 "Inode bitmap for group %u overlaps "
2932 "block group descriptors", i);
2936 if (inode_bitmap < first_block || inode_bitmap > last_block) {
2937 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2938 "Inode bitmap for group %u not in group "
2939 "(block %llu)!", i, inode_bitmap);
2942 inode_table = ext4_inode_table(sb, gdp);
2943 if (inode_table == sb_block) {
2944 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2945 "Inode table for group %u overlaps "
2950 if (inode_table >= sb_block + 1 &&
2951 inode_table <= last_bg_block) {
2952 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2953 "Inode table for group %u overlaps "
2954 "block group descriptors", i);
2958 if (inode_table < first_block ||
2959 inode_table + sbi->s_itb_per_group - 1 > last_block) {
2960 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2961 "Inode table for group %u not in group "
2962 "(block %llu)!", i, inode_table);
2965 ext4_lock_group(sb, i);
2966 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2967 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2968 "Checksum for group %u failed (%u!=%u)",
2969 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2970 gdp)), le16_to_cpu(gdp->bg_checksum));
2971 if (!sb_rdonly(sb)) {
2972 ext4_unlock_group(sb, i);
2976 ext4_unlock_group(sb, i);
2978 first_block += EXT4_BLOCKS_PER_GROUP(sb);
2980 if (NULL != first_not_zeroed)
2981 *first_not_zeroed = grp;
2986 * Maximal extent format file size.
2987 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2988 * extent format containers, within a sector_t, and within i_blocks
2989 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2990 * so that won't be a limiting factor.
2992 * However there is other limiting factor. We do store extents in the form
2993 * of starting block and length, hence the resulting length of the extent
2994 * covering maximum file size must fit into on-disk format containers as
2995 * well. Given that length is always by 1 unit bigger than max unit (because
2996 * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2998 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3000 static loff_t ext4_max_size(int blkbits, int has_huge_files)
3003 loff_t upper_limit = MAX_LFS_FILESIZE;
3005 BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
3007 if (!has_huge_files) {
3008 upper_limit = (1LL << 32) - 1;
3010 /* total blocks in file system block size */
3011 upper_limit >>= (blkbits - 9);
3012 upper_limit <<= blkbits;
3016 * 32-bit extent-start container, ee_block. We lower the maxbytes
3017 * by one fs block, so ee_len can cover the extent of maximum file
3020 res = (1LL << 32) - 1;
3023 /* Sanity check against vm- & vfs- imposed limits */
3024 if (res > upper_limit)
3031 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
3032 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
3033 * We need to be 1 filesystem block less than the 2^48 sector limit.
3035 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3037 unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
3041 * This is calculated to be the largest file size for a dense, block
3042 * mapped file such that the file's total number of 512-byte sectors,
3043 * including data and all indirect blocks, does not exceed (2^48 - 1).
3045 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
3046 * number of 512-byte sectors of the file.
3048 if (!has_huge_files) {
3050 * !has_huge_files or implies that the inode i_block field
3051 * represents total file blocks in 2^32 512-byte sectors ==
3052 * size of vfs inode i_blocks * 8
3054 upper_limit = (1LL << 32) - 1;
3056 /* total blocks in file system block size */
3057 upper_limit >>= (bits - 9);
3061 * We use 48 bit ext4_inode i_blocks
3062 * With EXT4_HUGE_FILE_FL set the i_blocks
3063 * represent total number of blocks in
3064 * file system block size
3066 upper_limit = (1LL << 48) - 1;
3070 /* indirect blocks */
3072 /* double indirect blocks */
3073 meta_blocks += 1 + (1LL << (bits-2));
3074 /* tripple indirect blocks */
3075 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
3077 upper_limit -= meta_blocks;
3078 upper_limit <<= bits;
3080 res += 1LL << (bits-2);
3081 res += 1LL << (2*(bits-2));
3082 res += 1LL << (3*(bits-2));
3084 if (res > upper_limit)
3087 if (res > MAX_LFS_FILESIZE)
3088 res = MAX_LFS_FILESIZE;
3093 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
3094 ext4_fsblk_t logical_sb_block, int nr)
3096 struct ext4_sb_info *sbi = EXT4_SB(sb);
3097 ext4_group_t bg, first_meta_bg;
3100 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3102 if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
3103 return logical_sb_block + nr + 1;
3104 bg = sbi->s_desc_per_block * nr;
3105 if (ext4_bg_has_super(sb, bg))
3109 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3110 * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
3111 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3114 if (sb->s_blocksize == 1024 && nr == 0 &&
3115 le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3118 return (has_super + ext4_group_first_block_no(sb, bg));
3122 * ext4_get_stripe_size: Get the stripe size.
3123 * @sbi: In memory super block info
3125 * If we have specified it via mount option, then
3126 * use the mount option value. If the value specified at mount time is
3127 * greater than the blocks per group use the super block value.
3128 * If the super block value is greater than blocks per group return 0.
3129 * Allocator needs it be less than blocks per group.
3132 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3134 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3135 unsigned long stripe_width =
3136 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
3139 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
3140 ret = sbi->s_stripe;
3141 else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
3143 else if (stride && stride <= sbi->s_blocks_per_group)
3149 * If the stripe width is 1, this makes no sense and
3150 * we set it to 0 to turn off stripe handling code.
3159 * Check whether this filesystem can be mounted based on
3160 * the features present and the RDONLY/RDWR mount requested.
3161 * Returns 1 if this filesystem can be mounted as requested,
3162 * 0 if it cannot be.
3164 int ext4_feature_set_ok(struct super_block *sb, int readonly)
3166 if (ext4_has_unknown_ext4_incompat_features(sb)) {
3167 ext4_msg(sb, KERN_ERR,
3168 "Couldn't mount because of "
3169 "unsupported optional features (%x)",
3170 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3171 ~EXT4_FEATURE_INCOMPAT_SUPP));
3175 #ifndef CONFIG_UNICODE
3176 if (ext4_has_feature_casefold(sb)) {
3177 ext4_msg(sb, KERN_ERR,
3178 "Filesystem with casefold feature cannot be "
3179 "mounted without CONFIG_UNICODE");
3187 if (ext4_has_feature_readonly(sb)) {
3188 ext4_msg(sb, KERN_INFO, "filesystem is read-only");
3189 sb->s_flags |= SB_RDONLY;
3193 /* Check that feature set is OK for a read-write mount */
3194 if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3195 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3196 "unsupported optional features (%x)",
3197 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3198 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3201 if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3202 ext4_msg(sb, KERN_ERR,
3203 "Can't support bigalloc feature without "
3204 "extents feature\n");
3208 #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3209 if (!readonly && (ext4_has_feature_quota(sb) ||
3210 ext4_has_feature_project(sb))) {
3211 ext4_msg(sb, KERN_ERR,
3212 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3215 #endif /* CONFIG_QUOTA */
3220 * This function is called once a day if we have errors logged
3221 * on the file system
3223 static void print_daily_error_info(struct timer_list *t)
3225 struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3226 struct super_block *sb = sbi->s_sb;
3227 struct ext4_super_block *es = sbi->s_es;
3229 if (es->s_error_count)
3230 /* fsck newer than v1.41.13 is needed to clean this condition. */
3231 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
3232 le32_to_cpu(es->s_error_count));
3233 if (es->s_first_error_time) {
3234 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
3236 ext4_get_tstamp(es, s_first_error_time),
3237 (int) sizeof(es->s_first_error_func),
3238 es->s_first_error_func,
3239 le32_to_cpu(es->s_first_error_line));
3240 if (es->s_first_error_ino)
3241 printk(KERN_CONT ": inode %u",
3242 le32_to_cpu(es->s_first_error_ino));
3243 if (es->s_first_error_block)
3244 printk(KERN_CONT ": block %llu", (unsigned long long)
3245 le64_to_cpu(es->s_first_error_block));
3246 printk(KERN_CONT "\n");
3248 if (es->s_last_error_time) {
3249 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
3251 ext4_get_tstamp(es, s_last_error_time),
3252 (int) sizeof(es->s_last_error_func),
3253 es->s_last_error_func,
3254 le32_to_cpu(es->s_last_error_line));
3255 if (es->s_last_error_ino)
3256 printk(KERN_CONT ": inode %u",
3257 le32_to_cpu(es->s_last_error_ino));
3258 if (es->s_last_error_block)
3259 printk(KERN_CONT ": block %llu", (unsigned long long)
3260 le64_to_cpu(es->s_last_error_block));
3261 printk(KERN_CONT "\n");
3263 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
3266 /* Find next suitable group and run ext4_init_inode_table */
3267 static int ext4_run_li_request(struct ext4_li_request *elr)
3269 struct ext4_group_desc *gdp = NULL;
3270 struct super_block *sb = elr->lr_super;
3271 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3272 ext4_group_t group = elr->lr_next_group;
3273 unsigned int prefetch_ios = 0;
3277 if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
3278 elr->lr_next_group = ext4_mb_prefetch(sb, group,
3279 EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
3281 ext4_mb_prefetch_fini(sb, elr->lr_next_group,
3283 trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
3285 if (group >= elr->lr_next_group) {
3287 if (elr->lr_first_not_zeroed != ngroups &&
3288 !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
3289 elr->lr_next_group = elr->lr_first_not_zeroed;
3290 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3297 for (; group < ngroups; group++) {
3298 gdp = ext4_get_group_desc(sb, group, NULL);
3304 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3308 if (group >= ngroups)
3312 start_time = ktime_get_real_ns();
3313 ret = ext4_init_inode_table(sb, group,
3314 elr->lr_timeout ? 0 : 1);
3315 trace_ext4_lazy_itable_init(sb, group);
3316 if (elr->lr_timeout == 0) {
3317 elr->lr_timeout = nsecs_to_jiffies((ktime_get_real_ns() - start_time) *
3318 EXT4_SB(elr->lr_super)->s_li_wait_mult);
3320 elr->lr_next_sched = jiffies + elr->lr_timeout;
3321 elr->lr_next_group = group + 1;
3327 * Remove lr_request from the list_request and free the
3328 * request structure. Should be called with li_list_mtx held
3330 static void ext4_remove_li_request(struct ext4_li_request *elr)
3335 list_del(&elr->lr_request);
3336 EXT4_SB(elr->lr_super)->s_li_request = NULL;
3340 static void ext4_unregister_li_request(struct super_block *sb)
3342 mutex_lock(&ext4_li_mtx);
3343 if (!ext4_li_info) {
3344 mutex_unlock(&ext4_li_mtx);
3348 mutex_lock(&ext4_li_info->li_list_mtx);
3349 ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3350 mutex_unlock(&ext4_li_info->li_list_mtx);
3351 mutex_unlock(&ext4_li_mtx);
3354 static struct task_struct *ext4_lazyinit_task;
3357 * This is the function where ext4lazyinit thread lives. It walks
3358 * through the request list searching for next scheduled filesystem.
3359 * When such a fs is found, run the lazy initialization request
3360 * (ext4_rn_li_request) and keep track of the time spend in this
3361 * function. Based on that time we compute next schedule time of
3362 * the request. When walking through the list is complete, compute
3363 * next waking time and put itself into sleep.
3365 static int ext4_lazyinit_thread(void *arg)
3367 struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3368 struct list_head *pos, *n;
3369 struct ext4_li_request *elr;
3370 unsigned long next_wakeup, cur;
3372 BUG_ON(NULL == eli);
3376 next_wakeup = MAX_JIFFY_OFFSET;
3378 mutex_lock(&eli->li_list_mtx);
3379 if (list_empty(&eli->li_request_list)) {
3380 mutex_unlock(&eli->li_list_mtx);
3383 list_for_each_safe(pos, n, &eli->li_request_list) {
3386 elr = list_entry(pos, struct ext4_li_request,
3389 if (time_before(jiffies, elr->lr_next_sched)) {
3390 if (time_before(elr->lr_next_sched, next_wakeup))
3391 next_wakeup = elr->lr_next_sched;
3394 if (down_read_trylock(&elr->lr_super->s_umount)) {
3395 if (sb_start_write_trylock(elr->lr_super)) {
3398 * We hold sb->s_umount, sb can not
3399 * be removed from the list, it is
3400 * now safe to drop li_list_mtx
3402 mutex_unlock(&eli->li_list_mtx);
3403 err = ext4_run_li_request(elr);
3404 sb_end_write(elr->lr_super);
3405 mutex_lock(&eli->li_list_mtx);
3408 up_read((&elr->lr_super->s_umount));
3410 /* error, remove the lazy_init job */
3412 ext4_remove_li_request(elr);
3416 elr->lr_next_sched = jiffies +
3418 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3420 if (time_before(elr->lr_next_sched, next_wakeup))
3421 next_wakeup = elr->lr_next_sched;
3423 mutex_unlock(&eli->li_list_mtx);
3428 if ((time_after_eq(cur, next_wakeup)) ||
3429 (MAX_JIFFY_OFFSET == next_wakeup)) {
3434 schedule_timeout_interruptible(next_wakeup - cur);
3436 if (kthread_should_stop()) {
3437 ext4_clear_request_list();
3444 * It looks like the request list is empty, but we need
3445 * to check it under the li_list_mtx lock, to prevent any
3446 * additions into it, and of course we should lock ext4_li_mtx
3447 * to atomically free the list and ext4_li_info, because at
3448 * this point another ext4 filesystem could be registering
3451 mutex_lock(&ext4_li_mtx);
3452 mutex_lock(&eli->li_list_mtx);
3453 if (!list_empty(&eli->li_request_list)) {
3454 mutex_unlock(&eli->li_list_mtx);
3455 mutex_unlock(&ext4_li_mtx);
3458 mutex_unlock(&eli->li_list_mtx);
3459 kfree(ext4_li_info);
3460 ext4_li_info = NULL;
3461 mutex_unlock(&ext4_li_mtx);
3466 static void ext4_clear_request_list(void)
3468 struct list_head *pos, *n;
3469 struct ext4_li_request *elr;
3471 mutex_lock(&ext4_li_info->li_list_mtx);
3472 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3473 elr = list_entry(pos, struct ext4_li_request,
3475 ext4_remove_li_request(elr);
3477 mutex_unlock(&ext4_li_info->li_list_mtx);
3480 static int ext4_run_lazyinit_thread(void)
3482 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3483 ext4_li_info, "ext4lazyinit");
3484 if (IS_ERR(ext4_lazyinit_task)) {
3485 int err = PTR_ERR(ext4_lazyinit_task);
3486 ext4_clear_request_list();
3487 kfree(ext4_li_info);
3488 ext4_li_info = NULL;
3489 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3490 "initialization thread\n",
3494 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3499 * Check whether it make sense to run itable init. thread or not.
3500 * If there is at least one uninitialized inode table, return
3501 * corresponding group number, else the loop goes through all
3502 * groups and return total number of groups.
3504 static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3506 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3507 struct ext4_group_desc *gdp = NULL;
3509 if (!ext4_has_group_desc_csum(sb))
3512 for (group = 0; group < ngroups; group++) {
3513 gdp = ext4_get_group_desc(sb, group, NULL);
3517 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3524 static int ext4_li_info_new(void)
3526 struct ext4_lazy_init *eli = NULL;
3528 eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3532 INIT_LIST_HEAD(&eli->li_request_list);
3533 mutex_init(&eli->li_list_mtx);
3535 eli->li_state |= EXT4_LAZYINIT_QUIT;
3542 static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3545 struct ext4_li_request *elr;
3547 elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3552 elr->lr_first_not_zeroed = start;
3553 if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
3554 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3555 elr->lr_next_group = start;
3557 elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
3561 * Randomize first schedule time of the request to
3562 * spread the inode table initialization requests
3565 elr->lr_next_sched = jiffies + (prandom_u32() %
3566 (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3570 int ext4_register_li_request(struct super_block *sb,
3571 ext4_group_t first_not_zeroed)
3573 struct ext4_sb_info *sbi = EXT4_SB(sb);
3574 struct ext4_li_request *elr = NULL;
3575 ext4_group_t ngroups = sbi->s_groups_count;
3578 mutex_lock(&ext4_li_mtx);
3579 if (sbi->s_li_request != NULL) {
3581 * Reset timeout so it can be computed again, because
3582 * s_li_wait_mult might have changed.
3584 sbi->s_li_request->lr_timeout = 0;
3588 if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
3589 (first_not_zeroed == ngroups || sb_rdonly(sb) ||
3590 !test_opt(sb, INIT_INODE_TABLE)))
3593 elr = ext4_li_request_new(sb, first_not_zeroed);
3599 if (NULL == ext4_li_info) {
3600 ret = ext4_li_info_new();
3605 mutex_lock(&ext4_li_info->li_list_mtx);
3606 list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3607 mutex_unlock(&ext4_li_info->li_list_mtx);
3609 sbi->s_li_request = elr;
3611 * set elr to NULL here since it has been inserted to
3612 * the request_list and the removal and free of it is
3613 * handled by ext4_clear_request_list from now on.
3617 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3618 ret = ext4_run_lazyinit_thread();
3623 mutex_unlock(&ext4_li_mtx);
3630 * We do not need to lock anything since this is called on
3633 static void ext4_destroy_lazyinit_thread(void)
3636 * If thread exited earlier
3637 * there's nothing to be done.
3639 if (!ext4_li_info || !ext4_lazyinit_task)
3642 kthread_stop(ext4_lazyinit_task);
3645 static int set_journal_csum_feature_set(struct super_block *sb)
3648 int compat, incompat;
3649 struct ext4_sb_info *sbi = EXT4_SB(sb);
3651 if (ext4_has_metadata_csum(sb)) {
3652 /* journal checksum v3 */
3654 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
3656 /* journal checksum v1 */
3657 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
3661 jbd2_journal_clear_features(sbi->s_journal,
3662 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3663 JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3664 JBD2_FEATURE_INCOMPAT_CSUM_V2);
3665 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3666 ret = jbd2_journal_set_features(sbi->s_journal,
3668 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3670 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3671 ret = jbd2_journal_set_features(sbi->s_journal,
3674 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3675 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3677 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3678 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3685 * Note: calculating the overhead so we can be compatible with
3686 * historical BSD practice is quite difficult in the face of
3687 * clusters/bigalloc. This is because multiple metadata blocks from
3688 * different block group can end up in the same allocation cluster.
3689 * Calculating the exact overhead in the face of clustered allocation
3690 * requires either O(all block bitmaps) in memory or O(number of block
3691 * groups**2) in time. We will still calculate the superblock for
3692 * older file systems --- and if we come across with a bigalloc file
3693 * system with zero in s_overhead_clusters the estimate will be close to
3694 * correct especially for very large cluster sizes --- but for newer
3695 * file systems, it's better to calculate this figure once at mkfs
3696 * time, and store it in the superblock. If the superblock value is
3697 * present (even for non-bigalloc file systems), we will use it.
3699 static int count_overhead(struct super_block *sb, ext4_group_t grp,
3702 struct ext4_sb_info *sbi = EXT4_SB(sb);
3703 struct ext4_group_desc *gdp;
3704 ext4_fsblk_t first_block, last_block, b;
3705 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3706 int s, j, count = 0;
3708 if (!ext4_has_feature_bigalloc(sb))
3709 return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
3710 sbi->s_itb_per_group + 2);
3712 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3713 (grp * EXT4_BLOCKS_PER_GROUP(sb));
3714 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3715 for (i = 0; i < ngroups; i++) {
3716 gdp = ext4_get_group_desc(sb, i, NULL);
3717 b = ext4_block_bitmap(sb, gdp);
3718 if (b >= first_block && b <= last_block) {
3719 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3722 b = ext4_inode_bitmap(sb, gdp);
3723 if (b >= first_block && b <= last_block) {
3724 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3727 b = ext4_inode_table(sb, gdp);
3728 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3729 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3730 int c = EXT4_B2C(sbi, b - first_block);
3731 ext4_set_bit(c, buf);
3737 if (ext4_bg_has_super(sb, grp)) {
3738 ext4_set_bit(s++, buf);
3741 j = ext4_bg_num_gdb(sb, grp);
3742 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3743 ext4_error(sb, "Invalid number of block group "
3744 "descriptor blocks: %d", j);
3745 j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3749 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3753 return EXT4_CLUSTERS_PER_GROUP(sb) -
3754 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3758 * Compute the overhead and stash it in sbi->s_overhead
3760 int ext4_calculate_overhead(struct super_block *sb)
3762 struct ext4_sb_info *sbi = EXT4_SB(sb);
3763 struct ext4_super_block *es = sbi->s_es;
3764 struct inode *j_inode;
3765 unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3766 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3767 ext4_fsblk_t overhead = 0;
3768 char *buf = (char *) get_zeroed_page(GFP_NOFS);
3774 * Compute the overhead (FS structures). This is constant
3775 * for a given filesystem unless the number of block groups
3776 * changes so we cache the previous value until it does.
3780 * All of the blocks before first_data_block are overhead
3782 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3785 * Add the overhead found in each block group
3787 for (i = 0; i < ngroups; i++) {
3790 blks = count_overhead(sb, i, buf);
3793 memset(buf, 0, PAGE_SIZE);
3798 * Add the internal journal blocks whether the journal has been
3801 if (sbi->s_journal && !sbi->s_journal_bdev)
3802 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
3803 else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3804 /* j_inum for internal journal is non-zero */
3805 j_inode = ext4_get_journal_inode(sb, j_inum);
3807 j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
3808 overhead += EXT4_NUM_B2C(sbi, j_blocks);
3811 ext4_msg(sb, KERN_ERR, "can't get journal size");
3814 sbi->s_overhead = overhead;
3816 free_page((unsigned long) buf);
3820 static void ext4_set_resv_clusters(struct super_block *sb)
3822 ext4_fsblk_t resv_clusters;
3823 struct ext4_sb_info *sbi = EXT4_SB(sb);
3826 * There's no need to reserve anything when we aren't using extents.
3827 * The space estimates are exact, there are no unwritten extents,
3828 * hole punching doesn't need new metadata... This is needed especially
3829 * to keep ext2/3 backward compatibility.
3831 if (!ext4_has_feature_extents(sb))
3834 * By default we reserve 2% or 4096 clusters, whichever is smaller.
3835 * This should cover the situations where we can not afford to run
3836 * out of space like for example punch hole, or converting
3837 * unwritten extents in delalloc path. In most cases such
3838 * allocation would require 1, or 2 blocks, higher numbers are
3841 resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3842 sbi->s_cluster_bits);
3844 do_div(resv_clusters, 50);
3845 resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
3847 atomic64_set(&sbi->s_resv_clusters, resv_clusters);
3850 static const char *ext4_quota_mode(struct super_block *sb)
3853 if (!ext4_quota_capable(sb))
3856 if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
3857 return "journalled";
3865 static void ext4_setup_csum_trigger(struct super_block *sb,
3866 enum ext4_journal_trigger_type type,
3868 struct jbd2_buffer_trigger_type *type,
3869 struct buffer_head *bh,
3873 struct ext4_sb_info *sbi = EXT4_SB(sb);
3875 sbi->s_journal_triggers[type].sb = sb;
3876 sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
3879 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3881 struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3882 char *orig_data = kstrdup(data, GFP_KERNEL);
3883 struct buffer_head *bh, **group_desc;
3884 struct ext4_super_block *es = NULL;
3885 struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3886 struct flex_groups **flex_groups;
3888 ext4_fsblk_t sb_block = get_sb_block(&data);
3889 ext4_fsblk_t logical_sb_block;
3890 unsigned long offset = 0;
3891 unsigned long def_mount_opts;
3895 int blocksize, clustersize;
3896 unsigned int db_count;
3898 int needs_recovery, has_huge_files;
3901 ext4_group_t first_not_zeroed;
3902 struct ext4_parsed_options parsed_opts;
3904 /* Set defaults for the variables that will be set during parsing */
3905 parsed_opts.journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3906 parsed_opts.journal_devnum = 0;
3907 parsed_opts.mb_optimize_scan = DEFAULT_MB_OPTIMIZE_SCAN;
3909 if ((data && !orig_data) || !sbi)
3912 sbi->s_daxdev = dax_dev;
3913 sbi->s_blockgroup_lock =
3914 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3915 if (!sbi->s_blockgroup_lock)
3918 sb->s_fs_info = sbi;
3920 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3921 sbi->s_sb_block = sb_block;
3922 sbi->s_sectors_written_start =
3923 part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
3925 /* Cleanup superblock name */
3926 strreplace(sb->s_id, '/', '!');
3928 /* -EINVAL is default */
3930 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3932 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3937 * The ext4 superblock will not be buffer aligned for other than 1kB
3938 * block sizes. We need to calculate the offset from buffer start.
3940 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
3941 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3942 offset = do_div(logical_sb_block, blocksize);
3944 logical_sb_block = sb_block;
3947 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
3949 ext4_msg(sb, KERN_ERR, "unable to read superblock");
3954 * Note: s_es must be initialized as soon as possible because
3955 * some ext4 macro-instructions depend on its value
3957 es = (struct ext4_super_block *) (bh->b_data + offset);
3959 sb->s_magic = le16_to_cpu(es->s_magic);
3960 if (sb->s_magic != EXT4_SUPER_MAGIC)
3962 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3964 /* Warn if metadata_csum and gdt_csum are both set. */
3965 if (ext4_has_feature_metadata_csum(sb) &&
3966 ext4_has_feature_gdt_csum(sb))
3967 ext4_warning(sb, "metadata_csum and uninit_bg are "
3968 "redundant flags; please run fsck.");
3970 /* Check for a known checksum algorithm */
3971 if (!ext4_verify_csum_type(sb, es)) {
3972 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3973 "unknown checksum algorithm.");
3977 ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
3978 ext4_orphan_file_block_trigger);
3980 /* Load the checksum driver */
3981 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
3982 if (IS_ERR(sbi->s_chksum_driver)) {
3983 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
3984 ret = PTR_ERR(sbi->s_chksum_driver);
3985 sbi->s_chksum_driver = NULL;
3989 /* Check superblock checksum */
3990 if (!ext4_superblock_csum_verify(sb, es)) {
3991 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3992 "invalid superblock checksum. Run e2fsck?");
3998 /* Precompute checksum seed for all metadata */
3999 if (ext4_has_feature_csum_seed(sb))
4000 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4001 else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4002 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4003 sizeof(es->s_uuid));
4005 /* Set defaults before we parse the mount options */
4006 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4007 set_opt(sb, INIT_INODE_TABLE);
4008 if (def_mount_opts & EXT4_DEFM_DEBUG)
4010 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4012 if (def_mount_opts & EXT4_DEFM_UID16)
4013 set_opt(sb, NO_UID32);
4014 /* xattr user namespace & acls are now defaulted on */
4015 set_opt(sb, XATTR_USER);
4016 #ifdef CONFIG_EXT4_FS_POSIX_ACL
4017 set_opt(sb, POSIX_ACL);
4019 if (ext4_has_feature_fast_commit(sb))
4020 set_opt2(sb, JOURNAL_FAST_COMMIT);
4021 /* don't forget to enable journal_csum when metadata_csum is enabled. */
4022 if (ext4_has_metadata_csum(sb))
4023 set_opt(sb, JOURNAL_CHECKSUM);
4025 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4026 set_opt(sb, JOURNAL_DATA);
4027 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4028 set_opt(sb, ORDERED_DATA);
4029 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4030 set_opt(sb, WRITEBACK_DATA);
4032 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
4033 set_opt(sb, ERRORS_PANIC);
4034 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
4035 set_opt(sb, ERRORS_CONT);
4037 set_opt(sb, ERRORS_RO);
4038 /* block_validity enabled by default; disable with noblock_validity */
4039 set_opt(sb, BLOCK_VALIDITY);
4040 if (def_mount_opts & EXT4_DEFM_DISCARD)
4041 set_opt(sb, DISCARD);
4043 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
4044 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
4045 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
4046 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
4047 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
4049 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4050 set_opt(sb, BARRIER);
4053 * enable delayed allocation by default
4054 * Use -o nodelalloc to turn it off
4056 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
4057 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4058 set_opt(sb, DELALLOC);
4061 * set default s_li_wait_mult for lazyinit, for the case there is
4062 * no mount option specified.
4064 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
4066 if (le32_to_cpu(es->s_log_block_size) >
4067 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4068 ext4_msg(sb, KERN_ERR,
4069 "Invalid log block size: %u",
4070 le32_to_cpu(es->s_log_block_size));
4073 if (le32_to_cpu(es->s_log_cluster_size) >
4074 (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4075 ext4_msg(sb, KERN_ERR,
4076 "Invalid log cluster size: %u",
4077 le32_to_cpu(es->s_log_cluster_size));
4081 blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4083 if (blocksize == PAGE_SIZE)
4084 set_opt(sb, DIOREAD_NOLOCK);
4086 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4087 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4088 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4090 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4091 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4092 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4093 ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4097 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4098 (!is_power_of_2(sbi->s_inode_size)) ||
4099 (sbi->s_inode_size > blocksize)) {
4100 ext4_msg(sb, KERN_ERR,
4101 "unsupported inode size: %d",
4103 ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
4107 * i_atime_extra is the last extra field available for
4108 * [acm]times in struct ext4_inode. Checking for that
4109 * field should suffice to ensure we have extra space
4112 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4113 sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4114 sb->s_time_gran = 1;
4115 sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4117 sb->s_time_gran = NSEC_PER_SEC;
4118 sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4120 sb->s_time_min = EXT4_TIMESTAMP_MIN;
4122 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4123 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4124 EXT4_GOOD_OLD_INODE_SIZE;
4125 if (ext4_has_feature_extra_isize(sb)) {
4126 unsigned v, max = (sbi->s_inode_size -
4127 EXT4_GOOD_OLD_INODE_SIZE);
4129 v = le16_to_cpu(es->s_want_extra_isize);
4131 ext4_msg(sb, KERN_ERR,
4132 "bad s_want_extra_isize: %d", v);
4135 if (sbi->s_want_extra_isize < v)
4136 sbi->s_want_extra_isize = v;
4138 v = le16_to_cpu(es->s_min_extra_isize);
4140 ext4_msg(sb, KERN_ERR,
4141 "bad s_min_extra_isize: %d", v);
4144 if (sbi->s_want_extra_isize < v)
4145 sbi->s_want_extra_isize = v;
4149 if (sbi->s_es->s_mount_opts[0]) {
4150 char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
4151 sizeof(sbi->s_es->s_mount_opts),
4155 if (!parse_options(s_mount_opts, sb, &parsed_opts, 0)) {
4156 ext4_msg(sb, KERN_WARNING,
4157 "failed to parse options in superblock: %s",
4160 kfree(s_mount_opts);
4162 sbi->s_def_mount_opt = sbi->s_mount_opt;
4163 if (!parse_options((char *) data, sb, &parsed_opts, 0))
4166 #ifdef CONFIG_UNICODE
4167 if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
4168 const struct ext4_sb_encodings *encoding_info;
4169 struct unicode_map *encoding;
4170 __u16 encoding_flags;
4172 if (ext4_sb_read_encoding(es, &encoding_info,
4174 ext4_msg(sb, KERN_ERR,
4175 "Encoding requested by superblock is unknown");
4179 encoding = utf8_load(encoding_info->version);
4180 if (IS_ERR(encoding)) {
4181 ext4_msg(sb, KERN_ERR,
4182 "can't mount with superblock charset: %s-%s "
4183 "not supported by the kernel. flags: 0x%x.",
4184 encoding_info->name, encoding_info->version,
4188 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4189 "%s-%s with flags 0x%hx", encoding_info->name,
4190 encoding_info->version?:"\b", encoding_flags);
4192 sb->s_encoding = encoding;
4193 sb->s_encoding_flags = encoding_flags;
4197 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4198 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, O_DIRECT and fast_commit support!\n");
4199 /* can't mount with both data=journal and dioread_nolock. */
4200 clear_opt(sb, DIOREAD_NOLOCK);
4201 clear_opt2(sb, JOURNAL_FAST_COMMIT);
4202 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4203 ext4_msg(sb, KERN_ERR, "can't mount with "
4204 "both data=journal and delalloc");
4207 if (test_opt(sb, DAX_ALWAYS)) {
4208 ext4_msg(sb, KERN_ERR, "can't mount with "
4209 "both data=journal and dax");
4212 if (ext4_has_feature_encrypt(sb)) {
4213 ext4_msg(sb, KERN_WARNING,
4214 "encrypted files will use data=ordered "
4215 "instead of data journaling mode");
4217 if (test_opt(sb, DELALLOC))
4218 clear_opt(sb, DELALLOC);
4220 sb->s_iflags |= SB_I_CGROUPWB;
4223 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4224 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4226 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4227 (ext4_has_compat_features(sb) ||
4228 ext4_has_ro_compat_features(sb) ||
4229 ext4_has_incompat_features(sb)))
4230 ext4_msg(sb, KERN_WARNING,
4231 "feature flags set on rev 0 fs, "
4232 "running e2fsck is recommended");
4234 if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4235 set_opt2(sb, HURD_COMPAT);
4236 if (ext4_has_feature_64bit(sb)) {
4237 ext4_msg(sb, KERN_ERR,
4238 "The Hurd can't support 64-bit file systems");
4243 * ea_inode feature uses l_i_version field which is not
4244 * available in HURD_COMPAT mode.
4246 if (ext4_has_feature_ea_inode(sb)) {
4247 ext4_msg(sb, KERN_ERR,
4248 "ea_inode feature is not supported for Hurd");
4253 if (IS_EXT2_SB(sb)) {
4254 if (ext2_feature_set_ok(sb))
4255 ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4256 "using the ext4 subsystem");
4259 * If we're probing be silent, if this looks like
4260 * it's actually an ext[34] filesystem.
4262 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4264 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4265 "to feature incompatibilities");
4270 if (IS_EXT3_SB(sb)) {
4271 if (ext3_feature_set_ok(sb))
4272 ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4273 "using the ext4 subsystem");
4276 * If we're probing be silent, if this looks like
4277 * it's actually an ext4 filesystem.
4279 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4281 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4282 "to feature incompatibilities");
4288 * Check feature flags regardless of the revision level, since we
4289 * previously didn't change the revision level when setting the flags,
4290 * so there is a chance incompat flags are set on a rev 0 filesystem.
4292 if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4295 if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
4296 ext4_msg(sb, KERN_ERR,
4297 "Number of reserved GDT blocks insanely large: %d",
4298 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4302 if (dax_supported(dax_dev, sb->s_bdev, blocksize, 0,
4303 bdev_nr_sectors(sb->s_bdev)))
4304 set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4306 if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4307 if (ext4_has_feature_inline_data(sb)) {
4308 ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4309 " that may contain inline data");
4312 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
4313 ext4_msg(sb, KERN_ERR,
4314 "DAX unsupported by block device.");
4319 if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
4320 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
4321 es->s_encryption_level);
4325 if (sb->s_blocksize != blocksize) {
4327 * bh must be released before kill_bdev(), otherwise
4328 * it won't be freed and its page also. kill_bdev()
4329 * is called by sb_set_blocksize().
4332 /* Validate the filesystem blocksize */
4333 if (!sb_set_blocksize(sb, blocksize)) {
4334 ext4_msg(sb, KERN_ERR, "bad block size %d",
4340 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
4341 offset = do_div(logical_sb_block, blocksize);
4342 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
4344 ext4_msg(sb, KERN_ERR,
4345 "Can't read superblock on 2nd try");
4350 es = (struct ext4_super_block *)(bh->b_data + offset);
4352 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4353 ext4_msg(sb, KERN_ERR,
4354 "Magic mismatch, very weird!");
4359 has_huge_files = ext4_has_feature_huge_file(sb);
4360 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4362 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4364 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4365 if (ext4_has_feature_64bit(sb)) {
4366 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
4367 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4368 !is_power_of_2(sbi->s_desc_size)) {
4369 ext4_msg(sb, KERN_ERR,
4370 "unsupported descriptor size %lu",
4375 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
4377 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4378 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
4380 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4381 if (sbi->s_inodes_per_block == 0)
4383 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4384 sbi->s_inodes_per_group > blocksize * 8) {
4385 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4386 sbi->s_inodes_per_group);
4389 sbi->s_itb_per_group = sbi->s_inodes_per_group /
4390 sbi->s_inodes_per_block;
4391 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4393 sbi->s_mount_state = le16_to_cpu(es->s_state);
4394 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4395 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
4397 for (i = 0; i < 4; i++)
4398 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4399 sbi->s_def_hash_version = es->s_def_hash_version;
4400 if (ext4_has_feature_dir_index(sb)) {
4401 i = le32_to_cpu(es->s_flags);
4402 if (i & EXT2_FLAGS_UNSIGNED_HASH)
4403 sbi->s_hash_unsigned = 3;
4404 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4405 #ifdef __CHAR_UNSIGNED__
4408 cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4409 sbi->s_hash_unsigned = 3;
4413 cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4418 /* Handle clustersize */
4419 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4420 if (ext4_has_feature_bigalloc(sb)) {
4421 if (clustersize < blocksize) {
4422 ext4_msg(sb, KERN_ERR,
4423 "cluster size (%d) smaller than "
4424 "block size (%d)", clustersize, blocksize);
4427 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4428 le32_to_cpu(es->s_log_block_size);
4429 sbi->s_clusters_per_group =
4430 le32_to_cpu(es->s_clusters_per_group);
4431 if (sbi->s_clusters_per_group > blocksize * 8) {
4432 ext4_msg(sb, KERN_ERR,
4433 "#clusters per group too big: %lu",
4434 sbi->s_clusters_per_group);
4437 if (sbi->s_blocks_per_group !=
4438 (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4439 ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4440 "clusters per group (%lu) inconsistent",
4441 sbi->s_blocks_per_group,
4442 sbi->s_clusters_per_group);
4446 if (clustersize != blocksize) {
4447 ext4_msg(sb, KERN_ERR,
4448 "fragment/cluster size (%d) != "
4449 "block size (%d)", clustersize, blocksize);
4452 if (sbi->s_blocks_per_group > blocksize * 8) {
4453 ext4_msg(sb, KERN_ERR,
4454 "#blocks per group too big: %lu",
4455 sbi->s_blocks_per_group);
4458 sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4459 sbi->s_cluster_bits = 0;
4461 sbi->s_cluster_ratio = clustersize / blocksize;
4463 /* Do we have standard group size of clustersize * 8 blocks ? */
4464 if (sbi->s_blocks_per_group == clustersize << 3)
4465 set_opt2(sb, STD_GROUP_SIZE);
4468 * Test whether we have more sectors than will fit in sector_t,
4469 * and whether the max offset is addressable by the page cache.
4471 err = generic_check_addressable(sb->s_blocksize_bits,
4472 ext4_blocks_count(es));
4474 ext4_msg(sb, KERN_ERR, "filesystem"
4475 " too large to mount safely on this system");
4479 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4482 /* check blocks count against device size */
4483 blocks_count = sb_bdev_nr_blocks(sb);
4484 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4485 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4486 "exceeds size of device (%llu blocks)",
4487 ext4_blocks_count(es), blocks_count);
4492 * It makes no sense for the first data block to be beyond the end
4493 * of the filesystem.
4495 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4496 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4497 "block %u is beyond end of filesystem (%llu)",
4498 le32_to_cpu(es->s_first_data_block),
4499 ext4_blocks_count(es));
4502 if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4503 (sbi->s_cluster_ratio == 1)) {
4504 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4505 "block is 0 with a 1k block and cluster size");
4509 blocks_count = (ext4_blocks_count(es) -
4510 le32_to_cpu(es->s_first_data_block) +
4511 EXT4_BLOCKS_PER_GROUP(sb) - 1);
4512 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4513 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4514 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
4515 "(block count %llu, first data block %u, "
4516 "blocks per group %lu)", blocks_count,
4517 ext4_blocks_count(es),
4518 le32_to_cpu(es->s_first_data_block),
4519 EXT4_BLOCKS_PER_GROUP(sb));
4522 sbi->s_groups_count = blocks_count;
4523 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4524 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
4525 if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4526 le32_to_cpu(es->s_inodes_count)) {
4527 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4528 le32_to_cpu(es->s_inodes_count),
4529 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4533 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4534 EXT4_DESC_PER_BLOCK(sb);
4535 if (ext4_has_feature_meta_bg(sb)) {
4536 if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
4537 ext4_msg(sb, KERN_WARNING,
4538 "first meta block group too large: %u "
4539 "(group descriptor block count %u)",
4540 le32_to_cpu(es->s_first_meta_bg), db_count);
4544 rcu_assign_pointer(sbi->s_group_desc,
4545 kvmalloc_array(db_count,
4546 sizeof(struct buffer_head *),
4548 if (sbi->s_group_desc == NULL) {
4549 ext4_msg(sb, KERN_ERR, "not enough memory");
4554 bgl_lock_init(sbi->s_blockgroup_lock);
4556 /* Pre-read the descriptors into the buffer cache */
4557 for (i = 0; i < db_count; i++) {
4558 block = descriptor_loc(sb, logical_sb_block, i);
4559 ext4_sb_breadahead_unmovable(sb, block);
4562 for (i = 0; i < db_count; i++) {
4563 struct buffer_head *bh;
4565 block = descriptor_loc(sb, logical_sb_block, i);
4566 bh = ext4_sb_bread_unmovable(sb, block);
4568 ext4_msg(sb, KERN_ERR,
4569 "can't read group descriptor %d", i);
4575 rcu_dereference(sbi->s_group_desc)[i] = bh;
4578 sbi->s_gdb_count = db_count;
4579 if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4580 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
4581 ret = -EFSCORRUPTED;
4585 timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
4586 spin_lock_init(&sbi->s_error_lock);
4587 INIT_WORK(&sbi->s_error_work, flush_stashed_error_work);
4589 /* Register extent status tree shrinker */
4590 if (ext4_es_register_shrinker(sbi))
4593 sbi->s_stripe = ext4_get_stripe_size(sbi);
4594 sbi->s_extent_max_zeroout_kb = 32;
4597 * set up enough so that it can read an inode
4599 sb->s_op = &ext4_sops;
4600 sb->s_export_op = &ext4_export_ops;
4601 sb->s_xattr = ext4_xattr_handlers;
4602 #ifdef CONFIG_FS_ENCRYPTION
4603 sb->s_cop = &ext4_cryptops;
4605 #ifdef CONFIG_FS_VERITY
4606 sb->s_vop = &ext4_verityops;
4609 sb->dq_op = &ext4_quota_operations;
4610 if (ext4_has_feature_quota(sb))
4611 sb->s_qcop = &dquot_quotactl_sysfile_ops;
4613 sb->s_qcop = &ext4_qctl_operations;
4614 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4616 memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4618 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
4619 mutex_init(&sbi->s_orphan_lock);
4621 /* Initialize fast commit stuff */
4622 atomic_set(&sbi->s_fc_subtid, 0);
4623 atomic_set(&sbi->s_fc_ineligible_updates, 0);
4624 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4625 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4626 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4627 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4628 sbi->s_fc_bytes = 0;
4629 ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
4630 ext4_clear_mount_flag(sb, EXT4_MF_FC_COMMITTING);
4631 spin_lock_init(&sbi->s_fc_lock);
4632 memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
4633 sbi->s_fc_replay_state.fc_regions = NULL;
4634 sbi->s_fc_replay_state.fc_regions_size = 0;
4635 sbi->s_fc_replay_state.fc_regions_used = 0;
4636 sbi->s_fc_replay_state.fc_regions_valid = 0;
4637 sbi->s_fc_replay_state.fc_modified_inodes = NULL;
4638 sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
4639 sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4643 needs_recovery = (es->s_last_orphan != 0 ||
4644 ext4_has_feature_orphan_present(sb) ||
4645 ext4_has_feature_journal_needs_recovery(sb));
4647 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4648 if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
4649 goto failed_mount3a;
4652 * The first inode we look at is the journal inode. Don't try
4653 * root first: it may be modified in the journal!
4655 if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
4656 err = ext4_load_journal(sb, es, parsed_opts.journal_devnum);
4658 goto failed_mount3a;
4659 } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4660 ext4_has_feature_journal_needs_recovery(sb)) {
4661 ext4_msg(sb, KERN_ERR, "required journal recovery "
4662 "suppressed and not mounted read-only");
4663 goto failed_mount_wq;
4665 /* Nojournal mode, all journal mount options are illegal */
4666 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
4667 ext4_msg(sb, KERN_ERR, "can't mount with "
4668 "journal_checksum, fs mounted w/o journal");
4669 goto failed_mount_wq;
4671 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4672 ext4_msg(sb, KERN_ERR, "can't mount with "
4673 "journal_async_commit, fs mounted w/o journal");
4674 goto failed_mount_wq;
4676 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
4677 ext4_msg(sb, KERN_ERR, "can't mount with "
4678 "commit=%lu, fs mounted w/o journal",
4679 sbi->s_commit_interval / HZ);
4680 goto failed_mount_wq;
4682 if (EXT4_MOUNT_DATA_FLAGS &
4683 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
4684 ext4_msg(sb, KERN_ERR, "can't mount with "
4685 "data=, fs mounted w/o journal");
4686 goto failed_mount_wq;
4688 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
4689 clear_opt(sb, JOURNAL_CHECKSUM);
4690 clear_opt(sb, DATA_FLAGS);
4691 clear_opt2(sb, JOURNAL_FAST_COMMIT);
4692 sbi->s_journal = NULL;
4697 if (ext4_has_feature_64bit(sb) &&
4698 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4699 JBD2_FEATURE_INCOMPAT_64BIT)) {
4700 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4701 goto failed_mount_wq;
4704 if (!set_journal_csum_feature_set(sb)) {
4705 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4707 goto failed_mount_wq;
4710 if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4711 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4712 JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4713 ext4_msg(sb, KERN_ERR,
4714 "Failed to set fast commit journal feature");
4715 goto failed_mount_wq;
4718 /* We have now updated the journal if required, so we can
4719 * validate the data journaling mode. */
4720 switch (test_opt(sb, DATA_FLAGS)) {
4722 /* No mode set, assume a default based on the journal
4723 * capabilities: ORDERED_DATA if the journal can
4724 * cope, else JOURNAL_DATA
4726 if (jbd2_journal_check_available_features
4727 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4728 set_opt(sb, ORDERED_DATA);
4729 sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
4731 set_opt(sb, JOURNAL_DATA);
4732 sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
4736 case EXT4_MOUNT_ORDERED_DATA:
4737 case EXT4_MOUNT_WRITEBACK_DATA:
4738 if (!jbd2_journal_check_available_features
4739 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4740 ext4_msg(sb, KERN_ERR, "Journal does not support "
4741 "requested data journaling mode");
4742 goto failed_mount_wq;
4749 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4750 test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4751 ext4_msg(sb, KERN_ERR, "can't mount with "
4752 "journal_async_commit in data=ordered mode");
4753 goto failed_mount_wq;
4756 set_task_ioprio(sbi->s_journal->j_task, parsed_opts.journal_ioprio);
4758 sbi->s_journal->j_submit_inode_data_buffers =
4759 ext4_journal_submit_inode_data_buffers;
4760 sbi->s_journal->j_finish_inode_data_buffers =
4761 ext4_journal_finish_inode_data_buffers;
4764 if (!test_opt(sb, NO_MBCACHE)) {
4765 sbi->s_ea_block_cache = ext4_xattr_create_cache();
4766 if (!sbi->s_ea_block_cache) {
4767 ext4_msg(sb, KERN_ERR,
4768 "Failed to create ea_block_cache");
4769 goto failed_mount_wq;
4772 if (ext4_has_feature_ea_inode(sb)) {
4773 sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4774 if (!sbi->s_ea_inode_cache) {
4775 ext4_msg(sb, KERN_ERR,
4776 "Failed to create ea_inode_cache");
4777 goto failed_mount_wq;
4782 if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4783 ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4784 goto failed_mount_wq;
4787 if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4788 !ext4_has_feature_encrypt(sb)) {
4789 ext4_set_feature_encrypt(sb);
4790 ext4_commit_super(sb);
4794 * Get the # of file system overhead blocks from the
4795 * superblock if present.
4797 if (es->s_overhead_clusters)
4798 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4800 err = ext4_calculate_overhead(sb);
4802 goto failed_mount_wq;
4806 * The maximum number of concurrent works can be high and
4807 * concurrency isn't really necessary. Limit it to 1.
4809 EXT4_SB(sb)->rsv_conversion_wq =
4810 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
4811 if (!EXT4_SB(sb)->rsv_conversion_wq) {
4812 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
4818 * The jbd2_journal_load will have done any necessary log recovery,
4819 * so we can safely mount the rest of the filesystem now.
4822 root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
4824 ext4_msg(sb, KERN_ERR, "get root inode failed");
4825 ret = PTR_ERR(root);
4829 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4830 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
4835 sb->s_root = d_make_root(root);
4837 ext4_msg(sb, KERN_ERR, "get root dentry failed");
4842 ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4843 if (ret == -EROFS) {
4844 sb->s_flags |= SB_RDONLY;
4847 goto failed_mount4a;
4849 ext4_set_resv_clusters(sb);
4851 if (test_opt(sb, BLOCK_VALIDITY)) {
4852 err = ext4_setup_system_zone(sb);
4854 ext4_msg(sb, KERN_ERR, "failed to initialize system "
4856 goto failed_mount4a;
4859 ext4_fc_replay_cleanup(sb);
4864 * Enable optimize_scan if number of groups is > threshold. This can be
4865 * turned off by passing "mb_optimize_scan=0". This can also be
4866 * turned on forcefully by passing "mb_optimize_scan=1".
4868 if (parsed_opts.mb_optimize_scan == 1)
4869 set_opt2(sb, MB_OPTIMIZE_SCAN);
4870 else if (parsed_opts.mb_optimize_scan == 0)
4871 clear_opt2(sb, MB_OPTIMIZE_SCAN);
4872 else if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
4873 set_opt2(sb, MB_OPTIMIZE_SCAN);
4875 err = ext4_mb_init(sb);
4877 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4883 * We can only set up the journal commit callback once
4884 * mballoc is initialized
4887 sbi->s_journal->j_commit_callback =
4888 ext4_journal_commit_callback;
4890 block = ext4_count_free_clusters(sb);
4891 ext4_free_blocks_count_set(sbi->s_es,
4892 EXT4_C2B(sbi, block));
4893 err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4896 unsigned long freei = ext4_count_free_inodes(sb);
4897 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
4898 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4902 * Update the checksum after updating free space/inode
4903 * counters. Otherwise the superblock can have an incorrect
4904 * checksum in the buffer cache until it is written out and
4905 * e2fsprogs programs trying to open a file system immediately
4906 * after it is mounted can fail.
4908 ext4_superblock_csum_set(sb);
4910 err = percpu_counter_init(&sbi->s_dirs_counter,
4911 ext4_count_dirs(sb), GFP_KERNEL);
4913 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4916 err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
4919 err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4922 ext4_msg(sb, KERN_ERR, "insufficient memory");
4926 if (ext4_has_feature_flex_bg(sb))
4927 if (!ext4_fill_flex_info(sb)) {
4928 ext4_msg(sb, KERN_ERR,
4929 "unable to initialize "
4930 "flex_bg meta info!");
4935 err = ext4_register_li_request(sb, first_not_zeroed);
4939 err = ext4_register_sysfs(sb);
4943 err = ext4_init_orphan_info(sb);
4947 /* Enable quota usage during mount. */
4948 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
4949 err = ext4_enable_quotas(sb);
4953 #endif /* CONFIG_QUOTA */
4956 * Save the original bdev mapping's wb_err value which could be
4957 * used to detect the metadata async write error.
4959 spin_lock_init(&sbi->s_bdev_wb_lock);
4960 errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
4961 &sbi->s_bdev_wb_err);
4962 sb->s_bdev->bd_super = sb;
4963 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4964 ext4_orphan_cleanup(sb, es);
4965 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
4966 if (needs_recovery) {
4967 ext4_msg(sb, KERN_INFO, "recovery complete");
4968 err = ext4_mark_recovery_complete(sb, es);
4972 if (EXT4_SB(sb)->s_journal) {
4973 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
4974 descr = " journalled data mode";
4975 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
4976 descr = " ordered data mode";
4978 descr = " writeback data mode";
4980 descr = "out journal";
4982 if (test_opt(sb, DISCARD)) {
4983 struct request_queue *q = bdev_get_queue(sb->s_bdev);
4984 if (!blk_queue_discard(q))
4985 ext4_msg(sb, KERN_WARNING,
4986 "mounting with \"discard\" option, but "
4987 "the device does not support discard");
4990 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4991 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
4992 "Opts: %.*s%s%s. Quota mode: %s.", descr,
4993 (int) sizeof(sbi->s_es->s_mount_opts),
4994 sbi->s_es->s_mount_opts,
4995 *sbi->s_es->s_mount_opts ? "; " : "", orig_data,
4996 ext4_quota_mode(sb));
4998 if (es->s_error_count)
4999 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5001 /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5002 ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5003 ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5004 ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
5005 atomic_set(&sbi->s_warning_count, 0);
5006 atomic_set(&sbi->s_msg_count, 0);
5013 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5017 ext4_release_orphan_info(sb);
5019 ext4_unregister_sysfs(sb);
5020 kobject_put(&sbi->s_kobj);
5022 ext4_unregister_li_request(sb);
5024 ext4_mb_release(sb);
5026 flex_groups = rcu_dereference(sbi->s_flex_groups);
5028 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
5029 kvfree(flex_groups[i]);
5030 kvfree(flex_groups);
5033 percpu_counter_destroy(&sbi->s_freeclusters_counter);
5034 percpu_counter_destroy(&sbi->s_freeinodes_counter);
5035 percpu_counter_destroy(&sbi->s_dirs_counter);
5036 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5037 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
5038 percpu_free_rwsem(&sbi->s_writepages_rwsem);
5040 ext4_ext_release(sb);
5041 ext4_release_system_zone(sb);
5046 ext4_msg(sb, KERN_ERR, "mount failed");
5047 if (EXT4_SB(sb)->rsv_conversion_wq)
5048 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
5050 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5051 sbi->s_ea_inode_cache = NULL;
5053 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
5054 sbi->s_ea_block_cache = NULL;
5056 if (sbi->s_journal) {
5057 /* flush s_error_work before journal destroy. */
5058 flush_work(&sbi->s_error_work);
5059 jbd2_journal_destroy(sbi->s_journal);
5060 sbi->s_journal = NULL;
5063 ext4_es_unregister_shrinker(sbi);
5065 /* flush s_error_work before sbi destroy */
5066 flush_work(&sbi->s_error_work);
5067 del_timer_sync(&sbi->s_err_report);
5068 ext4_stop_mmpd(sbi);
5071 group_desc = rcu_dereference(sbi->s_group_desc);
5072 for (i = 0; i < db_count; i++)
5073 brelse(group_desc[i]);
5077 if (sbi->s_chksum_driver)
5078 crypto_free_shash(sbi->s_chksum_driver);
5080 #ifdef CONFIG_UNICODE
5081 utf8_unload(sb->s_encoding);
5085 for (i = 0; i < EXT4_MAXQUOTAS; i++)
5086 kfree(get_qf_name(sb, sbi, i));
5088 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5089 /* ext4_blkdev_remove() calls kill_bdev(), release bh before it. */
5091 ext4_blkdev_remove(sbi);
5093 sb->s_fs_info = NULL;
5094 kfree(sbi->s_blockgroup_lock);
5098 fs_put_dax(dax_dev);
5099 return err ? err : ret;
5103 * Setup any per-fs journal parameters now. We'll do this both on
5104 * initial mount, once the journal has been initialised but before we've
5105 * done any recovery; and again on any subsequent remount.
5107 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5109 struct ext4_sb_info *sbi = EXT4_SB(sb);
5111 journal->j_commit_interval = sbi->s_commit_interval;
5112 journal->j_min_batch_time = sbi->s_min_batch_time;
5113 journal->j_max_batch_time = sbi->s_max_batch_time;
5114 ext4_fc_init(sb, journal);
5116 write_lock(&journal->j_state_lock);
5117 if (test_opt(sb, BARRIER))
5118 journal->j_flags |= JBD2_BARRIER;
5120 journal->j_flags &= ~JBD2_BARRIER;
5121 if (test_opt(sb, DATA_ERR_ABORT))
5122 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
5124 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5125 write_unlock(&journal->j_state_lock);
5128 static struct inode *ext4_get_journal_inode(struct super_block *sb,
5129 unsigned int journal_inum)
5131 struct inode *journal_inode;
5134 * Test for the existence of a valid inode on disk. Bad things
5135 * happen if we iget() an unused inode, as the subsequent iput()
5136 * will try to delete it.
5138 journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
5139 if (IS_ERR(journal_inode)) {
5140 ext4_msg(sb, KERN_ERR, "no journal found");
5143 if (!journal_inode->i_nlink) {
5144 make_bad_inode(journal_inode);
5145 iput(journal_inode);
5146 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5150 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5151 journal_inode, journal_inode->i_size);
5152 if (!S_ISREG(journal_inode->i_mode)) {
5153 ext4_msg(sb, KERN_ERR, "invalid journal inode");
5154 iput(journal_inode);
5157 return journal_inode;
5160 static journal_t *ext4_get_journal(struct super_block *sb,
5161 unsigned int journal_inum)
5163 struct inode *journal_inode;
5166 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5169 journal_inode = ext4_get_journal_inode(sb, journal_inum);
5173 journal = jbd2_journal_init_inode(journal_inode);
5175 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5176 iput(journal_inode);
5179 journal->j_private = sb;
5180 ext4_init_journal_params(sb, journal);
5184 static journal_t *ext4_get_dev_journal(struct super_block *sb,
5187 struct buffer_head *bh;
5191 int hblock, blocksize;
5192 ext4_fsblk_t sb_block;
5193 unsigned long offset;
5194 struct ext4_super_block *es;
5195 struct block_device *bdev;
5197 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5200 bdev = ext4_blkdev_get(j_dev, sb);
5204 blocksize = sb->s_blocksize;
5205 hblock = bdev_logical_block_size(bdev);
5206 if (blocksize < hblock) {
5207 ext4_msg(sb, KERN_ERR,
5208 "blocksize too small for journal device");
5212 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5213 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5214 set_blocksize(bdev, blocksize);
5215 if (!(bh = __bread(bdev, sb_block, blocksize))) {
5216 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5217 "external journal");
5221 es = (struct ext4_super_block *) (bh->b_data + offset);
5222 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5223 !(le32_to_cpu(es->s_feature_incompat) &
5224 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5225 ext4_msg(sb, KERN_ERR, "external journal has "
5231 if ((le32_to_cpu(es->s_feature_ro_compat) &
5232 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5233 es->s_checksum != ext4_superblock_csum(sb, es)) {
5234 ext4_msg(sb, KERN_ERR, "external journal has "
5235 "corrupt superblock");
5240 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5241 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5246 len = ext4_blocks_count(es);
5247 start = sb_block + 1;
5248 brelse(bh); /* we're done with the superblock */
5250 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5251 start, len, blocksize);
5253 ext4_msg(sb, KERN_ERR, "failed to create device journal");
5256 journal->j_private = sb;
5257 if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5258 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5261 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5262 ext4_msg(sb, KERN_ERR, "External journal has more than one "
5263 "user (unsupported) - %d",
5264 be32_to_cpu(journal->j_superblock->s_nr_users));
5267 EXT4_SB(sb)->s_journal_bdev = bdev;
5268 ext4_init_journal_params(sb, journal);
5272 jbd2_journal_destroy(journal);
5274 ext4_blkdev_put(bdev);
5278 static int ext4_load_journal(struct super_block *sb,
5279 struct ext4_super_block *es,
5280 unsigned long journal_devnum)
5283 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5286 int really_read_only;
5289 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5290 return -EFSCORRUPTED;
5292 if (journal_devnum &&
5293 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5294 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5295 "numbers have changed");
5296 journal_dev = new_decode_dev(journal_devnum);
5298 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5300 if (journal_inum && journal_dev) {
5301 ext4_msg(sb, KERN_ERR,
5302 "filesystem has both journal inode and journal device!");
5307 journal = ext4_get_journal(sb, journal_inum);
5311 journal = ext4_get_dev_journal(sb, journal_dev);
5316 journal_dev_ro = bdev_read_only(journal->j_dev);
5317 really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5319 if (journal_dev_ro && !sb_rdonly(sb)) {
5320 ext4_msg(sb, KERN_ERR,
5321 "journal device read-only, try mounting with '-o ro'");
5327 * Are we loading a blank journal or performing recovery after a
5328 * crash? For recovery, we need to check in advance whether we
5329 * can get read-write access to the device.
5331 if (ext4_has_feature_journal_needs_recovery(sb)) {
5332 if (sb_rdonly(sb)) {
5333 ext4_msg(sb, KERN_INFO, "INFO: recovery "
5334 "required on readonly filesystem");
5335 if (really_read_only) {
5336 ext4_msg(sb, KERN_ERR, "write access "
5337 "unavailable, cannot proceed "
5338 "(try mounting with noload)");
5342 ext4_msg(sb, KERN_INFO, "write access will "
5343 "be enabled during recovery");
5347 if (!(journal->j_flags & JBD2_BARRIER))
5348 ext4_msg(sb, KERN_INFO, "barriers disabled");
5350 if (!ext4_has_feature_journal_needs_recovery(sb))
5351 err = jbd2_journal_wipe(journal, !really_read_only);
5353 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
5355 memcpy(save, ((char *) es) +
5356 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5357 err = jbd2_journal_load(journal);
5359 memcpy(((char *) es) + EXT4_S_ERR_START,
5360 save, EXT4_S_ERR_LEN);
5365 ext4_msg(sb, KERN_ERR, "error loading journal");
5369 EXT4_SB(sb)->s_journal = journal;
5370 err = ext4_clear_journal_err(sb, es);
5372 EXT4_SB(sb)->s_journal = NULL;
5373 jbd2_journal_destroy(journal);
5377 if (!really_read_only && journal_devnum &&
5378 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5379 es->s_journal_dev = cpu_to_le32(journal_devnum);
5381 /* Make sure we flush the recovery flag to disk. */
5382 ext4_commit_super(sb);
5388 jbd2_journal_destroy(journal);
5392 /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
5393 static void ext4_update_super(struct super_block *sb)
5395 struct ext4_sb_info *sbi = EXT4_SB(sb);
5396 struct ext4_super_block *es = sbi->s_es;
5397 struct buffer_head *sbh = sbi->s_sbh;
5401 * If the file system is mounted read-only, don't update the
5402 * superblock write time. This avoids updating the superblock
5403 * write time when we are mounting the root file system
5404 * read/only but we need to replay the journal; at that point,
5405 * for people who are east of GMT and who make their clock
5406 * tick in localtime for Windows bug-for-bug compatibility,
5407 * the clock is set in the future, and this will cause e2fsck
5408 * to complain and force a full file system check.
5410 if (!(sb->s_flags & SB_RDONLY))
5411 ext4_update_tstamp(es, s_wtime);
5412 es->s_kbytes_written =
5413 cpu_to_le64(sbi->s_kbytes_written +
5414 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
5415 sbi->s_sectors_written_start) >> 1));
5416 if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
5417 ext4_free_blocks_count_set(es,
5418 EXT4_C2B(sbi, percpu_counter_sum_positive(
5419 &sbi->s_freeclusters_counter)));
5420 if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
5421 es->s_free_inodes_count =
5422 cpu_to_le32(percpu_counter_sum_positive(
5423 &sbi->s_freeinodes_counter));
5424 /* Copy error information to the on-disk superblock */
5425 spin_lock(&sbi->s_error_lock);
5426 if (sbi->s_add_error_count > 0) {
5427 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5428 if (!es->s_first_error_time && !es->s_first_error_time_hi) {
5429 __ext4_update_tstamp(&es->s_first_error_time,
5430 &es->s_first_error_time_hi,
5431 sbi->s_first_error_time);
5432 strncpy(es->s_first_error_func, sbi->s_first_error_func,
5433 sizeof(es->s_first_error_func));
5434 es->s_first_error_line =
5435 cpu_to_le32(sbi->s_first_error_line);
5436 es->s_first_error_ino =
5437 cpu_to_le32(sbi->s_first_error_ino);
5438 es->s_first_error_block =
5439 cpu_to_le64(sbi->s_first_error_block);
5440 es->s_first_error_errcode =
5441 ext4_errno_to_code(sbi->s_first_error_code);
5443 __ext4_update_tstamp(&es->s_last_error_time,
5444 &es->s_last_error_time_hi,
5445 sbi->s_last_error_time);
5446 strncpy(es->s_last_error_func, sbi->s_last_error_func,
5447 sizeof(es->s_last_error_func));
5448 es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
5449 es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
5450 es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
5451 es->s_last_error_errcode =
5452 ext4_errno_to_code(sbi->s_last_error_code);
5454 * Start the daily error reporting function if it hasn't been
5457 if (!es->s_error_count)
5458 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
5459 le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
5460 sbi->s_add_error_count = 0;
5462 spin_unlock(&sbi->s_error_lock);
5464 ext4_superblock_csum_set(sb);
5468 static int ext4_commit_super(struct super_block *sb)
5470 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5475 if (block_device_ejected(sb))
5478 ext4_update_super(sb);
5480 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
5482 * Oh, dear. A previous attempt to write the
5483 * superblock failed. This could happen because the
5484 * USB device was yanked out. Or it could happen to
5485 * be a transient write error and maybe the block will
5486 * be remapped. Nothing we can do but to retry the
5487 * write and hope for the best.
5489 ext4_msg(sb, KERN_ERR, "previous I/O error to "
5490 "superblock detected");
5491 clear_buffer_write_io_error(sbh);
5492 set_buffer_uptodate(sbh);
5494 BUFFER_TRACE(sbh, "marking dirty");
5495 mark_buffer_dirty(sbh);
5496 error = __sync_dirty_buffer(sbh,
5497 REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5498 if (buffer_write_io_error(sbh)) {
5499 ext4_msg(sb, KERN_ERR, "I/O error while writing "
5501 clear_buffer_write_io_error(sbh);
5502 set_buffer_uptodate(sbh);
5508 * Have we just finished recovery? If so, and if we are mounting (or
5509 * remounting) the filesystem readonly, then we will end up with a
5510 * consistent fs on disk. Record that fact.
5512 static int ext4_mark_recovery_complete(struct super_block *sb,
5513 struct ext4_super_block *es)
5516 journal_t *journal = EXT4_SB(sb)->s_journal;
5518 if (!ext4_has_feature_journal(sb)) {
5519 if (journal != NULL) {
5520 ext4_error(sb, "Journal got removed while the fs was "
5522 return -EFSCORRUPTED;
5526 jbd2_journal_lock_updates(journal);
5527 err = jbd2_journal_flush(journal, 0);
5531 if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
5532 ext4_has_feature_orphan_present(sb))) {
5533 if (!ext4_orphan_file_empty(sb)) {
5534 ext4_error(sb, "Orphan file not empty on read-only fs.");
5535 err = -EFSCORRUPTED;
5538 ext4_clear_feature_journal_needs_recovery(sb);
5539 ext4_clear_feature_orphan_present(sb);
5540 ext4_commit_super(sb);
5543 jbd2_journal_unlock_updates(journal);
5548 * If we are mounting (or read-write remounting) a filesystem whose journal
5549 * has recorded an error from a previous lifetime, move that error to the
5550 * main filesystem now.
5552 static int ext4_clear_journal_err(struct super_block *sb,
5553 struct ext4_super_block *es)
5559 if (!ext4_has_feature_journal(sb)) {
5560 ext4_error(sb, "Journal got removed while the fs was mounted!");
5561 return -EFSCORRUPTED;
5564 journal = EXT4_SB(sb)->s_journal;
5567 * Now check for any error status which may have been recorded in the
5568 * journal by a prior ext4_error() or ext4_abort()
5571 j_errno = jbd2_journal_errno(journal);
5575 errstr = ext4_decode_error(sb, j_errno, nbuf);
5576 ext4_warning(sb, "Filesystem error recorded "
5577 "from previous mount: %s", errstr);
5578 ext4_warning(sb, "Marking fs in need of filesystem check.");
5580 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5581 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5582 ext4_commit_super(sb);
5584 jbd2_journal_clear_err(journal);
5585 jbd2_journal_update_sb_errno(journal);
5591 * Force the running and committing transactions to commit,
5592 * and wait on the commit.
5594 int ext4_force_commit(struct super_block *sb)
5601 journal = EXT4_SB(sb)->s_journal;
5602 return ext4_journal_force_commit(journal);
5605 static int ext4_sync_fs(struct super_block *sb, int wait)
5609 bool needs_barrier = false;
5610 struct ext4_sb_info *sbi = EXT4_SB(sb);
5612 if (unlikely(ext4_forced_shutdown(sbi)))
5615 trace_ext4_sync_fs(sb, wait);
5616 flush_workqueue(sbi->rsv_conversion_wq);
5618 * Writeback quota in non-journalled quota case - journalled quota has
5621 dquot_writeback_dquots(sb, -1);
5623 * Data writeback is possible w/o journal transaction, so barrier must
5624 * being sent at the end of the function. But we can skip it if
5625 * transaction_commit will do it for us.
5627 if (sbi->s_journal) {
5628 target = jbd2_get_latest_transaction(sbi->s_journal);
5629 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
5630 !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
5631 needs_barrier = true;
5633 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5635 ret = jbd2_log_wait_commit(sbi->s_journal,
5638 } else if (wait && test_opt(sb, BARRIER))
5639 needs_barrier = true;
5640 if (needs_barrier) {
5642 err = blkdev_issue_flush(sb->s_bdev);
5651 * LVM calls this function before a (read-only) snapshot is created. This
5652 * gives us a chance to flush the journal completely and mark the fs clean.
5654 * Note that only this function cannot bring a filesystem to be in a clean
5655 * state independently. It relies on upper layer to stop all data & metadata
5658 static int ext4_freeze(struct super_block *sb)
5666 journal = EXT4_SB(sb)->s_journal;
5669 /* Now we set up the journal barrier. */
5670 jbd2_journal_lock_updates(journal);
5673 * Don't clear the needs_recovery flag if we failed to
5674 * flush the journal.
5676 error = jbd2_journal_flush(journal, 0);
5680 /* Journal blocked and flushed, clear needs_recovery flag. */
5681 ext4_clear_feature_journal_needs_recovery(sb);
5682 if (ext4_orphan_file_empty(sb))
5683 ext4_clear_feature_orphan_present(sb);
5686 error = ext4_commit_super(sb);
5689 /* we rely on upper layer to stop further updates */
5690 jbd2_journal_unlock_updates(journal);
5695 * Called by LVM after the snapshot is done. We need to reset the RECOVER
5696 * flag here, even though the filesystem is not technically dirty yet.
5698 static int ext4_unfreeze(struct super_block *sb)
5700 if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
5703 if (EXT4_SB(sb)->s_journal) {
5704 /* Reset the needs_recovery flag before the fs is unlocked. */
5705 ext4_set_feature_journal_needs_recovery(sb);
5706 if (ext4_has_feature_orphan_file(sb))
5707 ext4_set_feature_orphan_present(sb);
5710 ext4_commit_super(sb);
5715 * Structure to save mount options for ext4_remount's benefit
5717 struct ext4_mount_options {
5718 unsigned long s_mount_opt;
5719 unsigned long s_mount_opt2;
5722 unsigned long s_commit_interval;
5723 u32 s_min_batch_time, s_max_batch_time;
5726 char *s_qf_names[EXT4_MAXQUOTAS];
5730 static int ext4_remount(struct super_block *sb, int *flags, char *data)
5732 struct ext4_super_block *es;
5733 struct ext4_sb_info *sbi = EXT4_SB(sb);
5734 unsigned long old_sb_flags, vfs_flags;
5735 struct ext4_mount_options old_opts;
5739 int enable_quota = 0;
5741 char *to_free[EXT4_MAXQUOTAS];
5743 char *orig_data = kstrdup(data, GFP_KERNEL);
5744 struct ext4_parsed_options parsed_opts;
5746 parsed_opts.journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5747 parsed_opts.journal_devnum = 0;
5749 if (data && !orig_data)
5752 /* Store the original options */
5753 old_sb_flags = sb->s_flags;
5754 old_opts.s_mount_opt = sbi->s_mount_opt;
5755 old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5756 old_opts.s_resuid = sbi->s_resuid;
5757 old_opts.s_resgid = sbi->s_resgid;
5758 old_opts.s_commit_interval = sbi->s_commit_interval;
5759 old_opts.s_min_batch_time = sbi->s_min_batch_time;
5760 old_opts.s_max_batch_time = sbi->s_max_batch_time;
5762 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5763 for (i = 0; i < EXT4_MAXQUOTAS; i++)
5764 if (sbi->s_qf_names[i]) {
5765 char *qf_name = get_qf_name(sb, sbi, i);
5767 old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
5768 if (!old_opts.s_qf_names[i]) {
5769 for (j = 0; j < i; j++)
5770 kfree(old_opts.s_qf_names[j]);
5775 old_opts.s_qf_names[i] = NULL;
5777 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5778 parsed_opts.journal_ioprio =
5779 sbi->s_journal->j_task->io_context->ioprio;
5782 * Some options can be enabled by ext4 and/or by VFS mount flag
5783 * either way we need to make sure it matches in both *flags and
5784 * s_flags. Copy those selected flags from *flags to s_flags
5786 vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5787 sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5789 if (!parse_options(data, sb, &parsed_opts, 1)) {
5794 if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
5795 test_opt(sb, JOURNAL_CHECKSUM)) {
5796 ext4_msg(sb, KERN_ERR, "changing journal_checksum "
5797 "during remount not supported; ignoring");
5798 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5801 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
5802 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
5803 ext4_msg(sb, KERN_ERR, "can't mount with "
5804 "both data=journal and delalloc");
5808 if (test_opt(sb, DIOREAD_NOLOCK)) {
5809 ext4_msg(sb, KERN_ERR, "can't mount with "
5810 "both data=journal and dioread_nolock");
5814 } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5815 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5816 ext4_msg(sb, KERN_ERR, "can't mount with "
5817 "journal_async_commit in data=ordered mode");
5823 if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5824 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5829 if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
5830 ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
5832 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5833 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5837 if (sbi->s_journal) {
5838 ext4_init_journal_params(sb, sbi->s_journal);
5839 set_task_ioprio(sbi->s_journal->j_task, parsed_opts.journal_ioprio);
5842 /* Flush outstanding errors before changing fs state */
5843 flush_work(&sbi->s_error_work);
5845 if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
5846 if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) {
5851 if (*flags & SB_RDONLY) {
5852 err = sync_filesystem(sb);
5855 err = dquot_suspend(sb, -1);
5860 * First of all, the unconditional stuff we have to do
5861 * to disable replay of the journal when we next remount
5863 sb->s_flags |= SB_RDONLY;
5866 * OK, test if we are remounting a valid rw partition
5867 * readonly, and if so set the rdonly flag and then
5868 * mark the partition as valid again.
5870 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5871 (sbi->s_mount_state & EXT4_VALID_FS))
5872 es->s_state = cpu_to_le16(sbi->s_mount_state);
5874 if (sbi->s_journal) {
5876 * We let remount-ro finish even if marking fs
5877 * as clean failed...
5879 ext4_mark_recovery_complete(sb, es);
5882 /* Make sure we can mount this feature set readwrite */
5883 if (ext4_has_feature_readonly(sb) ||
5884 !ext4_feature_set_ok(sb, 0)) {
5889 * Make sure the group descriptor checksums
5890 * are sane. If they aren't, refuse to remount r/w.
5892 for (g = 0; g < sbi->s_groups_count; g++) {
5893 struct ext4_group_desc *gdp =
5894 ext4_get_group_desc(sb, g, NULL);
5896 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5897 ext4_msg(sb, KERN_ERR,
5898 "ext4_remount: Checksum for group %u failed (%u!=%u)",
5899 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
5900 le16_to_cpu(gdp->bg_checksum));
5907 * If we have an unprocessed orphan list hanging
5908 * around from a previously readonly bdev mount,
5909 * require a full umount/remount for now.
5911 if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
5912 ext4_msg(sb, KERN_WARNING, "Couldn't "
5913 "remount RDWR because of unprocessed "
5914 "orphan inode list. Please "
5915 "umount/remount instead");
5921 * Mounting a RDONLY partition read-write, so reread
5922 * and store the current valid flag. (It may have
5923 * been changed by e2fsck since we originally mounted
5926 if (sbi->s_journal) {
5927 err = ext4_clear_journal_err(sb, es);
5931 sbi->s_mount_state = le16_to_cpu(es->s_state);
5933 err = ext4_setup_super(sb, es, 0);
5937 sb->s_flags &= ~SB_RDONLY;
5938 if (ext4_has_feature_mmp(sb))
5939 if (ext4_multi_mount_protect(sb,
5940 le64_to_cpu(es->s_mmp_block))) {
5951 * Reinitialize lazy itable initialization thread based on
5954 if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5955 ext4_unregister_li_request(sb);
5957 ext4_group_t first_not_zeroed;
5958 first_not_zeroed = ext4_has_uninit_itable(sb);
5959 ext4_register_li_request(sb, first_not_zeroed);
5963 * Handle creation of system zone data early because it can fail.
5964 * Releasing of existing data is done when we are sure remount will
5967 if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
5968 err = ext4_setup_system_zone(sb);
5973 if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5974 err = ext4_commit_super(sb);
5980 /* Release old quota file names */
5981 for (i = 0; i < EXT4_MAXQUOTAS; i++)
5982 kfree(old_opts.s_qf_names[i]);
5984 if (sb_any_quota_suspended(sb))
5985 dquot_resume(sb, -1);
5986 else if (ext4_has_feature_quota(sb)) {
5987 err = ext4_enable_quotas(sb);
5993 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
5994 ext4_release_system_zone(sb);
5996 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
5997 ext4_stop_mmpd(sbi);
6000 * Some options can be enabled by ext4 and/or by VFS mount flag
6001 * either way we need to make sure it matches in both *flags and
6002 * s_flags. Copy those selected flags from s_flags to *flags
6004 *flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
6006 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s. Quota mode: %s.",
6007 orig_data, ext4_quota_mode(sb));
6012 sb->s_flags = old_sb_flags;
6013 sbi->s_mount_opt = old_opts.s_mount_opt;
6014 sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6015 sbi->s_resuid = old_opts.s_resuid;
6016 sbi->s_resgid = old_opts.s_resgid;
6017 sbi->s_commit_interval = old_opts.s_commit_interval;
6018 sbi->s_min_batch_time = old_opts.s_min_batch_time;
6019 sbi->s_max_batch_time = old_opts.s_max_batch_time;
6020 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
6021 ext4_release_system_zone(sb);
6023 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6024 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
6025 to_free[i] = get_qf_name(sb, sbi, i);
6026 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6029 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6032 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
6033 ext4_stop_mmpd(sbi);
6039 static int ext4_statfs_project(struct super_block *sb,
6040 kprojid_t projid, struct kstatfs *buf)
6043 struct dquot *dquot;
6047 qid = make_kqid_projid(projid);
6048 dquot = dqget(sb, qid);
6050 return PTR_ERR(dquot);
6051 spin_lock(&dquot->dq_dqb_lock);
6053 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6054 dquot->dq_dqb.dqb_bhardlimit);
6055 limit >>= sb->s_blocksize_bits;
6057 if (limit && buf->f_blocks > limit) {
6058 curblock = (dquot->dq_dqb.dqb_curspace +
6059 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6060 buf->f_blocks = limit;
6061 buf->f_bfree = buf->f_bavail =
6062 (buf->f_blocks > curblock) ?
6063 (buf->f_blocks - curblock) : 0;
6066 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6067 dquot->dq_dqb.dqb_ihardlimit);
6068 if (limit && buf->f_files > limit) {
6069 buf->f_files = limit;
6071 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6072 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6075 spin_unlock(&dquot->dq_dqb_lock);
6081 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6083 struct super_block *sb = dentry->d_sb;
6084 struct ext4_sb_info *sbi = EXT4_SB(sb);
6085 struct ext4_super_block *es = sbi->s_es;
6086 ext4_fsblk_t overhead = 0, resv_blocks;
6088 resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6090 if (!test_opt(sb, MINIX_DF))
6091 overhead = sbi->s_overhead;
6093 buf->f_type = EXT4_SUPER_MAGIC;
6094 buf->f_bsize = sb->s_blocksize;
6095 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
6096 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
6097 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6098 /* prevent underflow in case that few free space is available */
6099 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
6100 buf->f_bavail = buf->f_bfree -
6101 (ext4_r_blocks_count(es) + resv_blocks);
6102 if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6104 buf->f_files = le32_to_cpu(es->s_inodes_count);
6105 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6106 buf->f_namelen = EXT4_NAME_LEN;
6107 buf->f_fsid = uuid_to_fsid(es->s_uuid);
6110 if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6111 sb_has_quota_limits_enabled(sb, PRJQUOTA))
6112 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6121 * Helper functions so that transaction is started before we acquire dqio_sem
6122 * to keep correct lock ordering of transaction > dqio_sem
6124 static inline struct inode *dquot_to_inode(struct dquot *dquot)
6126 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6129 static int ext4_write_dquot(struct dquot *dquot)
6133 struct inode *inode;
6135 inode = dquot_to_inode(dquot);
6136 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6137 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6139 return PTR_ERR(handle);
6140 ret = dquot_commit(dquot);
6141 err = ext4_journal_stop(handle);
6147 static int ext4_acquire_dquot(struct dquot *dquot)
6152 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6153 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6155 return PTR_ERR(handle);
6156 ret = dquot_acquire(dquot);
6157 err = ext4_journal_stop(handle);
6163 static int ext4_release_dquot(struct dquot *dquot)
6168 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6169 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
6170 if (IS_ERR(handle)) {
6171 /* Release dquot anyway to avoid endless cycle in dqput() */
6172 dquot_release(dquot);
6173 return PTR_ERR(handle);
6175 ret = dquot_release(dquot);
6176 err = ext4_journal_stop(handle);
6182 static int ext4_mark_dquot_dirty(struct dquot *dquot)
6184 struct super_block *sb = dquot->dq_sb;
6186 if (ext4_is_quota_journalled(sb)) {
6187 dquot_mark_dquot_dirty(dquot);
6188 return ext4_write_dquot(dquot);
6190 return dquot_mark_dquot_dirty(dquot);
6194 static int ext4_write_info(struct super_block *sb, int type)
6199 /* Data block + inode block */
6200 handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6202 return PTR_ERR(handle);
6203 ret = dquot_commit_info(sb, type);
6204 err = ext4_journal_stop(handle);
6210 static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6212 struct ext4_inode_info *ei = EXT4_I(inode);
6214 /* The first argument of lockdep_set_subclass has to be
6215 * *exactly* the same as the argument to init_rwsem() --- in
6216 * this case, in init_once() --- or lockdep gets unhappy
6217 * because the name of the lock is set using the
6218 * stringification of the argument to init_rwsem().
6220 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
6221 lockdep_set_subclass(&ei->i_data_sem, subclass);
6225 * Standard function to be called on quota_on
6227 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
6228 const struct path *path)
6232 if (!test_opt(sb, QUOTA))
6235 /* Quotafile not on the same filesystem? */
6236 if (path->dentry->d_sb != sb)
6239 /* Quota already enabled for this file? */
6240 if (IS_NOQUOTA(d_inode(path->dentry)))
6243 /* Journaling quota? */
6244 if (EXT4_SB(sb)->s_qf_names[type]) {
6245 /* Quotafile not in fs root? */
6246 if (path->dentry->d_parent != sb->s_root)
6247 ext4_msg(sb, KERN_WARNING,
6248 "Quota file not on filesystem root. "
6249 "Journaled quota will not work");
6250 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
6253 * Clear the flag just in case mount options changed since
6256 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
6260 * When we journal data on quota file, we have to flush journal to see
6261 * all updates to the file when we bypass pagecache...
6263 if (EXT4_SB(sb)->s_journal &&
6264 ext4_should_journal_data(d_inode(path->dentry))) {
6266 * We don't need to lock updates but journal_flush() could
6267 * otherwise be livelocked...
6269 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
6270 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
6271 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6276 lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6277 err = dquot_quota_on(sb, type, format_id, path);
6279 lockdep_set_quota_inode(path->dentry->d_inode,
6282 struct inode *inode = d_inode(path->dentry);
6286 * Set inode flags to prevent userspace from messing with quota
6287 * files. If this fails, we return success anyway since quotas
6288 * are already enabled and this is not a hard failure.
6291 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6294 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6295 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6296 S_NOATIME | S_IMMUTABLE);
6297 err = ext4_mark_inode_dirty(handle, inode);
6298 ext4_journal_stop(handle);
6300 inode_unlock(inode);
6305 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
6309 struct inode *qf_inode;
6310 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
6311 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6312 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6313 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
6316 BUG_ON(!ext4_has_feature_quota(sb));
6318 if (!qf_inums[type])
6321 qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
6322 if (IS_ERR(qf_inode)) {
6323 ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
6324 return PTR_ERR(qf_inode);
6327 /* Don't account quota for quota files to avoid recursion */
6328 qf_inode->i_flags |= S_NOQUOTA;
6329 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
6330 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6332 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
6338 /* Enable usage tracking for all quota types. */
6339 int ext4_enable_quotas(struct super_block *sb)
6342 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
6343 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6344 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6345 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
6347 bool quota_mopt[EXT4_MAXQUOTAS] = {
6348 test_opt(sb, USRQUOTA),
6349 test_opt(sb, GRPQUOTA),
6350 test_opt(sb, PRJQUOTA),
6353 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6354 for (type = 0; type < EXT4_MAXQUOTAS; type++) {
6355 if (qf_inums[type]) {
6356 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
6357 DQUOT_USAGE_ENABLED |
6358 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
6361 "Failed to enable quota tracking "
6362 "(type=%d, err=%d). Please run "
6363 "e2fsck to fix.", type, err);
6364 for (type--; type >= 0; type--)
6365 dquot_quota_off(sb, type);
6374 static int ext4_quota_off(struct super_block *sb, int type)
6376 struct inode *inode = sb_dqopt(sb)->files[type];
6380 /* Force all delayed allocation blocks to be allocated.
6381 * Caller already holds s_umount sem */
6382 if (test_opt(sb, DELALLOC))
6383 sync_filesystem(sb);
6385 if (!inode || !igrab(inode))
6388 err = dquot_quota_off(sb, type);
6389 if (err || ext4_has_feature_quota(sb))
6394 * Update modification times of quota files when userspace can
6395 * start looking at them. If we fail, we return success anyway since
6396 * this is not a hard failure and quotas are already disabled.
6398 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6399 if (IS_ERR(handle)) {
6400 err = PTR_ERR(handle);
6403 EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6404 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6405 inode->i_mtime = inode->i_ctime = current_time(inode);
6406 err = ext4_mark_inode_dirty(handle, inode);
6407 ext4_journal_stop(handle);
6409 inode_unlock(inode);
6411 lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6415 return dquot_quota_off(sb, type);
6418 /* Read data from quotafile - avoid pagecache and such because we cannot afford
6419 * acquiring the locks... As quota files are never truncated and quota code
6420 * itself serializes the operations (and no one else should touch the files)
6421 * we don't have to be afraid of races */
6422 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6423 size_t len, loff_t off)
6425 struct inode *inode = sb_dqopt(sb)->files[type];
6426 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6427 int offset = off & (sb->s_blocksize - 1);
6430 struct buffer_head *bh;
6431 loff_t i_size = i_size_read(inode);
6435 if (off+len > i_size)
6438 while (toread > 0) {
6439 tocopy = sb->s_blocksize - offset < toread ?
6440 sb->s_blocksize - offset : toread;
6441 bh = ext4_bread(NULL, inode, blk, 0);
6444 if (!bh) /* A hole? */
6445 memset(data, 0, tocopy);
6447 memcpy(data, bh->b_data+offset, tocopy);
6457 /* Write to quotafile (we know the transaction is already started and has
6458 * enough credits) */
6459 static ssize_t ext4_quota_write(struct super_block *sb, int type,
6460 const char *data, size_t len, loff_t off)
6462 struct inode *inode = sb_dqopt(sb)->files[type];
6463 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6464 int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6466 struct buffer_head *bh;
6467 handle_t *handle = journal_current_handle();
6469 if (EXT4_SB(sb)->s_journal && !handle) {
6470 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6471 " cancelled because transaction is not started",
6472 (unsigned long long)off, (unsigned long long)len);
6476 * Since we account only one data block in transaction credits,
6477 * then it is impossible to cross a block boundary.
6479 if (sb->s_blocksize - offset < len) {
6480 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6481 " cancelled because not block aligned",
6482 (unsigned long long)off, (unsigned long long)len);
6487 bh = ext4_bread(handle, inode, blk,
6488 EXT4_GET_BLOCKS_CREATE |
6489 EXT4_GET_BLOCKS_METADATA_NOFAIL);
6490 } while (PTR_ERR(bh) == -ENOSPC &&
6491 ext4_should_retry_alloc(inode->i_sb, &retries));
6496 BUFFER_TRACE(bh, "get write access");
6497 err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
6503 memcpy(bh->b_data+offset, data, len);
6504 flush_dcache_page(bh->b_page);
6506 err = ext4_handle_dirty_metadata(handle, NULL, bh);
6509 if (inode->i_size < off + len) {
6510 i_size_write(inode, off + len);
6511 EXT4_I(inode)->i_disksize = inode->i_size;
6512 err2 = ext4_mark_inode_dirty(handle, inode);
6513 if (unlikely(err2 && !err))
6516 return err ? err : len;
6520 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6521 const char *dev_name, void *data)
6523 return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6526 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
6527 static inline void register_as_ext2(void)
6529 int err = register_filesystem(&ext2_fs_type);
6532 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
6535 static inline void unregister_as_ext2(void)
6537 unregister_filesystem(&ext2_fs_type);
6540 static inline int ext2_feature_set_ok(struct super_block *sb)
6542 if (ext4_has_unknown_ext2_incompat_features(sb))
6546 if (ext4_has_unknown_ext2_ro_compat_features(sb))
6551 static inline void register_as_ext2(void) { }
6552 static inline void unregister_as_ext2(void) { }
6553 static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
6556 static inline void register_as_ext3(void)
6558 int err = register_filesystem(&ext3_fs_type);
6561 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
6564 static inline void unregister_as_ext3(void)
6566 unregister_filesystem(&ext3_fs_type);
6569 static inline int ext3_feature_set_ok(struct super_block *sb)
6571 if (ext4_has_unknown_ext3_incompat_features(sb))
6573 if (!ext4_has_feature_journal(sb))
6577 if (ext4_has_unknown_ext3_ro_compat_features(sb))
6582 static struct file_system_type ext4_fs_type = {
6583 .owner = THIS_MODULE,
6585 .mount = ext4_mount,
6586 .kill_sb = kill_block_super,
6587 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
6589 MODULE_ALIAS_FS("ext4");
6591 /* Shared across all ext4 file systems */
6592 wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6594 static int __init ext4_init_fs(void)
6598 ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
6599 ext4_li_info = NULL;
6601 /* Build-time check for flags consistency */
6602 ext4_check_flag_values();
6604 for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6605 init_waitqueue_head(&ext4__ioend_wq[i]);
6607 err = ext4_init_es();
6611 err = ext4_init_pending();
6615 err = ext4_init_post_read_processing();
6619 err = ext4_init_pageio();
6623 err = ext4_init_system_zone();
6627 err = ext4_init_sysfs();
6631 err = ext4_init_mballoc();
6634 err = init_inodecache();
6638 err = ext4_fc_init_dentry_cache();
6644 err = register_filesystem(&ext4_fs_type);
6650 unregister_as_ext2();
6651 unregister_as_ext3();
6653 destroy_inodecache();
6655 ext4_exit_mballoc();
6659 ext4_exit_system_zone();
6663 ext4_exit_post_read_processing();
6665 ext4_exit_pending();
6672 static void __exit ext4_exit_fs(void)
6674 ext4_destroy_lazyinit_thread();
6675 unregister_as_ext2();
6676 unregister_as_ext3();
6677 unregister_filesystem(&ext4_fs_type);
6678 destroy_inodecache();
6679 ext4_exit_mballoc();
6681 ext4_exit_system_zone();
6683 ext4_exit_post_read_processing();
6685 ext4_exit_pending();
6688 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
6689 MODULE_DESCRIPTION("Fourth Extended Filesystem");
6690 MODULE_LICENSE("GPL");
6691 MODULE_SOFTDEP("pre: crc32c");
6692 module_init(ext4_init_fs)
6693 module_exit(ext4_exit_fs)