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/uaccess.h>
43 #include <linux/iversion.h>
44 #include <linux/unicode.h>
45 #include <linux/part_stat.h>
46 #include <linux/kthread.h>
47 #include <linux/freezer.h>
48 #include <linux/fsnotify.h>
49 #include <linux/fs_context.h>
50 #include <linux/fs_parser.h>
53 #include "ext4_extents.h" /* Needed for trace points definition */
54 #include "ext4_jbd2.h"
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/ext4.h>
63 static struct ext4_lazy_init *ext4_li_info;
64 static DEFINE_MUTEX(ext4_li_mtx);
65 static struct ratelimit_state ext4_mount_msg_ratelimit;
67 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
68 unsigned long journal_devnum);
69 static int ext4_show_options(struct seq_file *seq, struct dentry *root);
70 static void ext4_update_super(struct super_block *sb);
71 static int ext4_commit_super(struct super_block *sb);
72 static int ext4_mark_recovery_complete(struct super_block *sb,
73 struct ext4_super_block *es);
74 static int ext4_clear_journal_err(struct super_block *sb,
75 struct ext4_super_block *es);
76 static int ext4_sync_fs(struct super_block *sb, int wait);
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 inline int ext2_feature_set_ok(struct super_block *sb);
81 static inline int ext3_feature_set_ok(struct super_block *sb);
82 static void ext4_destroy_lazyinit_thread(void);
83 static void ext4_unregister_li_request(struct super_block *sb);
84 static void ext4_clear_request_list(void);
85 static struct inode *ext4_get_journal_inode(struct super_block *sb,
86 unsigned int journal_inum);
87 static int ext4_validate_options(struct fs_context *fc);
88 static int ext4_check_opt_consistency(struct fs_context *fc,
89 struct super_block *sb);
90 static void ext4_apply_options(struct fs_context *fc, struct super_block *sb);
91 static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param);
92 static int ext4_get_tree(struct fs_context *fc);
93 static int ext4_reconfigure(struct fs_context *fc);
94 static void ext4_fc_free(struct fs_context *fc);
95 static int ext4_init_fs_context(struct fs_context *fc);
96 static void ext4_kill_sb(struct super_block *sb);
97 static const struct fs_parameter_spec ext4_param_specs[];
103 * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
104 * -> page lock -> i_data_sem (rw)
106 * buffered write path:
107 * sb_start_write -> i_mutex -> mmap_lock
108 * sb_start_write -> i_mutex -> transaction start -> page lock ->
112 * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
114 * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
118 * sb_start_write -> i_mutex -> mmap_lock
119 * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
122 * transaction start -> page lock(s) -> i_data_sem (rw)
125 static const struct fs_context_operations ext4_context_ops = {
126 .parse_param = ext4_parse_param,
127 .get_tree = ext4_get_tree,
128 .reconfigure = ext4_reconfigure,
129 .free = ext4_fc_free,
133 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
134 static struct file_system_type ext2_fs_type = {
135 .owner = THIS_MODULE,
137 .init_fs_context = ext4_init_fs_context,
138 .parameters = ext4_param_specs,
139 .kill_sb = ext4_kill_sb,
140 .fs_flags = FS_REQUIRES_DEV,
142 MODULE_ALIAS_FS("ext2");
143 MODULE_ALIAS("ext2");
144 #define IS_EXT2_SB(sb) ((sb)->s_type == &ext2_fs_type)
146 #define IS_EXT2_SB(sb) (0)
150 static struct file_system_type ext3_fs_type = {
151 .owner = THIS_MODULE,
153 .init_fs_context = ext4_init_fs_context,
154 .parameters = ext4_param_specs,
155 .kill_sb = ext4_kill_sb,
156 .fs_flags = FS_REQUIRES_DEV,
158 MODULE_ALIAS_FS("ext3");
159 MODULE_ALIAS("ext3");
160 #define IS_EXT3_SB(sb) ((sb)->s_type == &ext3_fs_type)
163 static inline void __ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
167 * buffer's verified bit is no longer valid after reading from
168 * disk again due to write out error, clear it to make sure we
169 * recheck the buffer contents.
171 clear_buffer_verified(bh);
173 bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
175 submit_bh(REQ_OP_READ | op_flags, bh);
178 void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags,
181 BUG_ON(!buffer_locked(bh));
183 if (ext4_buffer_uptodate(bh)) {
187 __ext4_read_bh(bh, op_flags, end_io);
190 int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags, bh_end_io_t *end_io)
192 BUG_ON(!buffer_locked(bh));
194 if (ext4_buffer_uptodate(bh)) {
199 __ext4_read_bh(bh, op_flags, end_io);
202 if (buffer_uptodate(bh))
207 int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait)
211 ext4_read_bh_nowait(bh, op_flags, NULL);
214 return ext4_read_bh(bh, op_flags, NULL);
218 * This works like __bread_gfp() except it uses ERR_PTR for error
219 * returns. Currently with sb_bread it's impossible to distinguish
220 * between ENOMEM and EIO situations (since both result in a NULL
223 static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
225 blk_opf_t op_flags, gfp_t gfp)
227 struct buffer_head *bh;
230 bh = sb_getblk_gfp(sb, block, gfp);
232 return ERR_PTR(-ENOMEM);
233 if (ext4_buffer_uptodate(bh))
236 ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
244 struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
247 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
248 ~__GFP_FS) | __GFP_MOVABLE;
250 return __ext4_sb_bread_gfp(sb, block, op_flags, gfp);
253 struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
256 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
259 return __ext4_sb_bread_gfp(sb, block, 0, gfp);
262 void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
264 struct buffer_head *bh = bdev_getblk(sb->s_bdev, block,
265 sb->s_blocksize, GFP_NOWAIT | __GFP_NOWARN);
268 if (trylock_buffer(bh))
269 ext4_read_bh_nowait(bh, REQ_RAHEAD, NULL);
274 static int ext4_verify_csum_type(struct super_block *sb,
275 struct ext4_super_block *es)
277 if (!ext4_has_feature_metadata_csum(sb))
280 return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
283 __le32 ext4_superblock_csum(struct super_block *sb,
284 struct ext4_super_block *es)
286 struct ext4_sb_info *sbi = EXT4_SB(sb);
287 int offset = offsetof(struct ext4_super_block, s_checksum);
290 csum = ext4_chksum(sbi, ~0, (char *)es, offset);
292 return cpu_to_le32(csum);
295 static int ext4_superblock_csum_verify(struct super_block *sb,
296 struct ext4_super_block *es)
298 if (!ext4_has_metadata_csum(sb))
301 return es->s_checksum == ext4_superblock_csum(sb, es);
304 void ext4_superblock_csum_set(struct super_block *sb)
306 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
308 if (!ext4_has_metadata_csum(sb))
311 es->s_checksum = ext4_superblock_csum(sb, es);
314 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
315 struct ext4_group_desc *bg)
317 return le32_to_cpu(bg->bg_block_bitmap_lo) |
318 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
319 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
322 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
323 struct ext4_group_desc *bg)
325 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
326 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
327 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
330 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
331 struct ext4_group_desc *bg)
333 return le32_to_cpu(bg->bg_inode_table_lo) |
334 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
335 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
338 __u32 ext4_free_group_clusters(struct super_block *sb,
339 struct ext4_group_desc *bg)
341 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
342 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
343 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
346 __u32 ext4_free_inodes_count(struct super_block *sb,
347 struct ext4_group_desc *bg)
349 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
350 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
351 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
354 __u32 ext4_used_dirs_count(struct super_block *sb,
355 struct ext4_group_desc *bg)
357 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
358 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
359 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
362 __u32 ext4_itable_unused_count(struct super_block *sb,
363 struct ext4_group_desc *bg)
365 return le16_to_cpu(bg->bg_itable_unused_lo) |
366 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
367 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
370 void ext4_block_bitmap_set(struct super_block *sb,
371 struct ext4_group_desc *bg, ext4_fsblk_t blk)
373 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
374 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
375 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
378 void ext4_inode_bitmap_set(struct super_block *sb,
379 struct ext4_group_desc *bg, ext4_fsblk_t blk)
381 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
382 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
383 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
386 void ext4_inode_table_set(struct super_block *sb,
387 struct ext4_group_desc *bg, ext4_fsblk_t blk)
389 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
390 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
391 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
394 void ext4_free_group_clusters_set(struct super_block *sb,
395 struct ext4_group_desc *bg, __u32 count)
397 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
398 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
399 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
402 void ext4_free_inodes_set(struct super_block *sb,
403 struct ext4_group_desc *bg, __u32 count)
405 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
406 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
407 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
410 void ext4_used_dirs_set(struct super_block *sb,
411 struct ext4_group_desc *bg, __u32 count)
413 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
414 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
415 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
418 void ext4_itable_unused_set(struct super_block *sb,
419 struct ext4_group_desc *bg, __u32 count)
421 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
422 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
423 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
426 static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
428 now = clamp_val(now, 0, (1ull << 40) - 1);
430 *lo = cpu_to_le32(lower_32_bits(now));
431 *hi = upper_32_bits(now);
434 static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
436 return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
438 #define ext4_update_tstamp(es, tstamp) \
439 __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
440 ktime_get_real_seconds())
441 #define ext4_get_tstamp(es, tstamp) \
442 __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
444 #define EXT4_SB_REFRESH_INTERVAL_SEC (3600) /* seconds (1 hour) */
445 #define EXT4_SB_REFRESH_INTERVAL_KB (16384) /* kilobytes (16MB) */
448 * The ext4_maybe_update_superblock() function checks and updates the
449 * superblock if needed.
451 * This function is designed to update the on-disk superblock only under
452 * certain conditions to prevent excessive disk writes and unnecessary
453 * waking of the disk from sleep. The superblock will be updated if:
454 * 1. More than an hour has passed since the last superblock update, and
455 * 2. More than 16MB have been written since the last superblock update.
457 * @sb: The superblock
459 static void ext4_maybe_update_superblock(struct super_block *sb)
461 struct ext4_sb_info *sbi = EXT4_SB(sb);
462 struct ext4_super_block *es = sbi->s_es;
463 journal_t *journal = sbi->s_journal;
466 __u64 lifetime_write_kbytes;
469 if (sb_rdonly(sb) || !(sb->s_flags & SB_ACTIVE) ||
470 !journal || (journal->j_flags & JBD2_UNMOUNT))
473 now = ktime_get_real_seconds();
474 last_update = ext4_get_tstamp(es, s_wtime);
476 if (likely(now - last_update < EXT4_SB_REFRESH_INTERVAL_SEC))
479 lifetime_write_kbytes = sbi->s_kbytes_written +
480 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
481 sbi->s_sectors_written_start) >> 1);
483 /* Get the number of kilobytes not written to disk to account
484 * for statistics and compare with a multiple of 16 MB. This
485 * is used to determine when the next superblock commit should
486 * occur (i.e. not more often than once per 16MB if there was
487 * less written in an hour).
489 diff_size = lifetime_write_kbytes - le64_to_cpu(es->s_kbytes_written);
491 if (diff_size > EXT4_SB_REFRESH_INTERVAL_KB)
492 schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
495 static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
497 struct super_block *sb = journal->j_private;
498 struct ext4_sb_info *sbi = EXT4_SB(sb);
499 int error = is_journal_aborted(journal);
500 struct ext4_journal_cb_entry *jce;
502 BUG_ON(txn->t_state == T_FINISHED);
504 ext4_process_freed_data(sb, txn->t_tid);
505 ext4_maybe_update_superblock(sb);
507 spin_lock(&sbi->s_md_lock);
508 while (!list_empty(&txn->t_private_list)) {
509 jce = list_entry(txn->t_private_list.next,
510 struct ext4_journal_cb_entry, jce_list);
511 list_del_init(&jce->jce_list);
512 spin_unlock(&sbi->s_md_lock);
513 jce->jce_func(sb, jce, error);
514 spin_lock(&sbi->s_md_lock);
516 spin_unlock(&sbi->s_md_lock);
520 * This writepage callback for write_cache_pages()
521 * takes care of a few cases after page cleaning.
523 * write_cache_pages() already checks for dirty pages
524 * and calls clear_page_dirty_for_io(), which we want,
525 * to write protect the pages.
527 * However, we may have to redirty a page (see below.)
529 static int ext4_journalled_writepage_callback(struct folio *folio,
530 struct writeback_control *wbc,
533 transaction_t *transaction = (transaction_t *) data;
534 struct buffer_head *bh, *head;
535 struct journal_head *jh;
537 bh = head = folio_buffers(folio);
540 * We have to redirty a page in these cases:
541 * 1) If buffer is dirty, it means the page was dirty because it
542 * contains a buffer that needs checkpointing. So the dirty bit
543 * needs to be preserved so that checkpointing writes the buffer
545 * 2) If buffer is not part of the committing transaction
546 * (we may have just accidentally come across this buffer because
547 * inode range tracking is not exact) or if the currently running
548 * transaction already contains this buffer as well, dirty bit
549 * needs to be preserved so that the buffer gets writeprotected
550 * properly on running transaction's commit.
553 if (buffer_dirty(bh) ||
554 (jh && (jh->b_transaction != transaction ||
555 jh->b_next_transaction))) {
556 folio_redirty_for_writepage(wbc, folio);
559 } while ((bh = bh->b_this_page) != head);
562 return AOP_WRITEPAGE_ACTIVATE;
565 static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
567 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
568 struct writeback_control wbc = {
569 .sync_mode = WB_SYNC_ALL,
570 .nr_to_write = LONG_MAX,
571 .range_start = jinode->i_dirty_start,
572 .range_end = jinode->i_dirty_end,
575 return write_cache_pages(mapping, &wbc,
576 ext4_journalled_writepage_callback,
577 jinode->i_transaction);
580 static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
584 if (ext4_should_journal_data(jinode->i_vfs_inode))
585 ret = ext4_journalled_submit_inode_data_buffers(jinode);
587 ret = ext4_normal_submit_inode_data_buffers(jinode);
591 static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
595 if (!ext4_should_journal_data(jinode->i_vfs_inode))
596 ret = jbd2_journal_finish_inode_data_buffers(jinode);
601 static bool system_going_down(void)
603 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
604 || system_state == SYSTEM_RESTART;
607 struct ext4_err_translation {
612 #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
614 static struct ext4_err_translation err_translation[] = {
615 EXT4_ERR_TRANSLATE(EIO),
616 EXT4_ERR_TRANSLATE(ENOMEM),
617 EXT4_ERR_TRANSLATE(EFSBADCRC),
618 EXT4_ERR_TRANSLATE(EFSCORRUPTED),
619 EXT4_ERR_TRANSLATE(ENOSPC),
620 EXT4_ERR_TRANSLATE(ENOKEY),
621 EXT4_ERR_TRANSLATE(EROFS),
622 EXT4_ERR_TRANSLATE(EFBIG),
623 EXT4_ERR_TRANSLATE(EEXIST),
624 EXT4_ERR_TRANSLATE(ERANGE),
625 EXT4_ERR_TRANSLATE(EOVERFLOW),
626 EXT4_ERR_TRANSLATE(EBUSY),
627 EXT4_ERR_TRANSLATE(ENOTDIR),
628 EXT4_ERR_TRANSLATE(ENOTEMPTY),
629 EXT4_ERR_TRANSLATE(ESHUTDOWN),
630 EXT4_ERR_TRANSLATE(EFAULT),
633 static int ext4_errno_to_code(int errno)
637 for (i = 0; i < ARRAY_SIZE(err_translation); i++)
638 if (err_translation[i].errno == errno)
639 return err_translation[i].code;
640 return EXT4_ERR_UNKNOWN;
643 static void save_error_info(struct super_block *sb, int error,
644 __u32 ino, __u64 block,
645 const char *func, unsigned int line)
647 struct ext4_sb_info *sbi = EXT4_SB(sb);
649 /* We default to EFSCORRUPTED error... */
651 error = EFSCORRUPTED;
653 spin_lock(&sbi->s_error_lock);
654 sbi->s_add_error_count++;
655 sbi->s_last_error_code = error;
656 sbi->s_last_error_line = line;
657 sbi->s_last_error_ino = ino;
658 sbi->s_last_error_block = block;
659 sbi->s_last_error_func = func;
660 sbi->s_last_error_time = ktime_get_real_seconds();
661 if (!sbi->s_first_error_time) {
662 sbi->s_first_error_code = error;
663 sbi->s_first_error_line = line;
664 sbi->s_first_error_ino = ino;
665 sbi->s_first_error_block = block;
666 sbi->s_first_error_func = func;
667 sbi->s_first_error_time = sbi->s_last_error_time;
669 spin_unlock(&sbi->s_error_lock);
672 /* Deal with the reporting of failure conditions on a filesystem such as
673 * inconsistencies detected or read IO failures.
675 * On ext2, we can store the error state of the filesystem in the
676 * superblock. That is not possible on ext4, because we may have other
677 * write ordering constraints on the superblock which prevent us from
678 * writing it out straight away; and given that the journal is about to
679 * be aborted, we can't rely on the current, or future, transactions to
680 * write out the superblock safely.
682 * We'll just use the jbd2_journal_abort() error code to record an error in
683 * the journal instead. On recovery, the journal will complain about
684 * that error until we've noted it down and cleared it.
686 * If force_ro is set, we unconditionally force the filesystem into an
687 * ABORT|READONLY state, unless the error response on the fs has been set to
688 * panic in which case we take the easy way out and panic immediately. This is
689 * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
690 * at a critical moment in log management.
692 static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
693 __u32 ino, __u64 block,
694 const char *func, unsigned int line)
696 journal_t *journal = EXT4_SB(sb)->s_journal;
697 bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
699 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
700 if (test_opt(sb, WARN_ON_ERROR))
703 if (!continue_fs && !sb_rdonly(sb)) {
704 set_bit(EXT4_FLAGS_SHUTDOWN, &EXT4_SB(sb)->s_ext4_flags);
706 jbd2_journal_abort(journal, -EIO);
709 if (!bdev_read_only(sb->s_bdev)) {
710 save_error_info(sb, error, ino, block, func, line);
712 * In case the fs should keep running, we need to writeout
713 * superblock through the journal. Due to lock ordering
714 * constraints, it may not be safe to do it right here so we
715 * defer superblock flushing to a workqueue.
717 if (continue_fs && journal)
718 schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
720 ext4_commit_super(sb);
724 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
725 * could panic during 'reboot -f' as the underlying device got already
728 if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
729 panic("EXT4-fs (device %s): panic forced after error\n",
733 if (sb_rdonly(sb) || continue_fs)
736 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
738 * Make sure updated value of ->s_mount_flags will be visible before
742 sb->s_flags |= SB_RDONLY;
745 static void update_super_work(struct work_struct *work)
747 struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
749 journal_t *journal = sbi->s_journal;
753 * If the journal is still running, we have to write out superblock
754 * through the journal to avoid collisions of other journalled sb
757 * We use directly jbd2 functions here to avoid recursing back into
758 * ext4 error handling code during handling of previous errors.
760 if (!sb_rdonly(sbi->s_sb) && journal) {
761 struct buffer_head *sbh = sbi->s_sbh;
762 bool call_notify_err = false;
764 handle = jbd2_journal_start(journal, 1);
767 if (jbd2_journal_get_write_access(handle, sbh)) {
768 jbd2_journal_stop(handle);
772 if (sbi->s_add_error_count > 0)
773 call_notify_err = true;
775 ext4_update_super(sbi->s_sb);
776 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
777 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
778 "superblock detected");
779 clear_buffer_write_io_error(sbh);
780 set_buffer_uptodate(sbh);
783 if (jbd2_journal_dirty_metadata(handle, sbh)) {
784 jbd2_journal_stop(handle);
787 jbd2_journal_stop(handle);
790 ext4_notify_error_sysfs(sbi);
796 * Write through journal failed. Write sb directly to get error info
797 * out and hope for the best.
799 ext4_commit_super(sbi->s_sb);
800 ext4_notify_error_sysfs(sbi);
803 #define ext4_error_ratelimit(sb) \
804 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
807 void __ext4_error(struct super_block *sb, const char *function,
808 unsigned int line, bool force_ro, int error, __u64 block,
809 const char *fmt, ...)
811 struct va_format vaf;
814 if (unlikely(ext4_forced_shutdown(sb)))
817 trace_ext4_error(sb, function, line);
818 if (ext4_error_ratelimit(sb)) {
823 "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
824 sb->s_id, function, line, current->comm, &vaf);
827 fsnotify_sb_error(sb, NULL, error ? error : EFSCORRUPTED);
829 ext4_handle_error(sb, force_ro, error, 0, block, function, line);
832 void __ext4_error_inode(struct inode *inode, const char *function,
833 unsigned int line, ext4_fsblk_t block, int error,
834 const char *fmt, ...)
837 struct va_format vaf;
839 if (unlikely(ext4_forced_shutdown(inode->i_sb)))
842 trace_ext4_error(inode->i_sb, function, line);
843 if (ext4_error_ratelimit(inode->i_sb)) {
848 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
849 "inode #%lu: block %llu: comm %s: %pV\n",
850 inode->i_sb->s_id, function, line, inode->i_ino,
851 block, current->comm, &vaf);
853 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
854 "inode #%lu: comm %s: %pV\n",
855 inode->i_sb->s_id, function, line, inode->i_ino,
856 current->comm, &vaf);
859 fsnotify_sb_error(inode->i_sb, inode, error ? error : EFSCORRUPTED);
861 ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
865 void __ext4_error_file(struct file *file, const char *function,
866 unsigned int line, ext4_fsblk_t block,
867 const char *fmt, ...)
870 struct va_format vaf;
871 struct inode *inode = file_inode(file);
872 char pathname[80], *path;
874 if (unlikely(ext4_forced_shutdown(inode->i_sb)))
877 trace_ext4_error(inode->i_sb, function, line);
878 if (ext4_error_ratelimit(inode->i_sb)) {
879 path = file_path(file, pathname, sizeof(pathname));
887 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
888 "block %llu: comm %s: path %s: %pV\n",
889 inode->i_sb->s_id, function, line, inode->i_ino,
890 block, current->comm, path, &vaf);
893 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
894 "comm %s: path %s: %pV\n",
895 inode->i_sb->s_id, function, line, inode->i_ino,
896 current->comm, path, &vaf);
899 fsnotify_sb_error(inode->i_sb, inode, EFSCORRUPTED);
901 ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
905 const char *ext4_decode_error(struct super_block *sb, int errno,
912 errstr = "Corrupt filesystem";
915 errstr = "Filesystem failed CRC";
918 errstr = "IO failure";
921 errstr = "Out of memory";
924 if (!sb || (EXT4_SB(sb)->s_journal &&
925 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
926 errstr = "Journal has aborted";
928 errstr = "Readonly filesystem";
931 /* If the caller passed in an extra buffer for unknown
932 * errors, textualise them now. Else we just return
935 /* Check for truncated error codes... */
936 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
945 /* __ext4_std_error decodes expected errors from journaling functions
946 * automatically and invokes the appropriate error response. */
948 void __ext4_std_error(struct super_block *sb, const char *function,
949 unsigned int line, int errno)
954 if (unlikely(ext4_forced_shutdown(sb)))
957 /* Special case: if the error is EROFS, and we're not already
958 * inside a transaction, then there's really no point in logging
960 if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
963 if (ext4_error_ratelimit(sb)) {
964 errstr = ext4_decode_error(sb, errno, nbuf);
965 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
966 sb->s_id, function, line, errstr);
968 fsnotify_sb_error(sb, NULL, errno ? errno : EFSCORRUPTED);
970 ext4_handle_error(sb, false, -errno, 0, 0, function, line);
973 void __ext4_msg(struct super_block *sb,
974 const char *prefix, const char *fmt, ...)
976 struct va_format vaf;
980 atomic_inc(&EXT4_SB(sb)->s_msg_count);
981 if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state),
990 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
992 printk("%sEXT4-fs: %pV\n", prefix, &vaf);
996 static int ext4_warning_ratelimit(struct super_block *sb)
998 atomic_inc(&EXT4_SB(sb)->s_warning_count);
999 return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
1003 void __ext4_warning(struct super_block *sb, const char *function,
1004 unsigned int line, const char *fmt, ...)
1006 struct va_format vaf;
1009 if (!ext4_warning_ratelimit(sb))
1012 va_start(args, fmt);
1015 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
1016 sb->s_id, function, line, &vaf);
1020 void __ext4_warning_inode(const struct inode *inode, const char *function,
1021 unsigned int line, const char *fmt, ...)
1023 struct va_format vaf;
1026 if (!ext4_warning_ratelimit(inode->i_sb))
1029 va_start(args, fmt);
1032 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
1033 "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
1034 function, line, inode->i_ino, current->comm, &vaf);
1038 void __ext4_grp_locked_error(const char *function, unsigned int line,
1039 struct super_block *sb, ext4_group_t grp,
1040 unsigned long ino, ext4_fsblk_t block,
1041 const char *fmt, ...)
1045 struct va_format vaf;
1048 if (unlikely(ext4_forced_shutdown(sb)))
1051 trace_ext4_error(sb, function, line);
1052 if (ext4_error_ratelimit(sb)) {
1053 va_start(args, fmt);
1056 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
1057 sb->s_id, function, line, grp);
1059 printk(KERN_CONT "inode %lu: ", ino);
1061 printk(KERN_CONT "block %llu:",
1062 (unsigned long long) block);
1063 printk(KERN_CONT "%pV\n", &vaf);
1067 if (test_opt(sb, ERRORS_CONT)) {
1068 if (test_opt(sb, WARN_ON_ERROR))
1070 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
1071 if (!bdev_read_only(sb->s_bdev)) {
1072 save_error_info(sb, EFSCORRUPTED, ino, block, function,
1074 schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
1078 ext4_unlock_group(sb, grp);
1079 ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
1081 * We only get here in the ERRORS_RO case; relocking the group
1082 * may be dangerous, but nothing bad will happen since the
1083 * filesystem will have already been marked read/only and the
1084 * journal has been aborted. We return 1 as a hint to callers
1085 * who might what to use the return value from
1086 * ext4_grp_locked_error() to distinguish between the
1087 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
1088 * aggressively from the ext4 function in question, with a
1089 * more appropriate error code.
1091 ext4_lock_group(sb, grp);
1095 void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1099 struct ext4_sb_info *sbi = EXT4_SB(sb);
1100 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1101 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
1106 if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
1107 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1110 percpu_counter_sub(&sbi->s_freeclusters_counter,
1114 if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
1115 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
1120 count = ext4_free_inodes_count(sb, gdp);
1121 percpu_counter_sub(&sbi->s_freeinodes_counter,
1127 void ext4_update_dynamic_rev(struct super_block *sb)
1129 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1131 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1135 "updating to rev %d because of new feature flag, "
1136 "running e2fsck is recommended",
1139 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1140 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1141 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1142 /* leave es->s_feature_*compat flags alone */
1143 /* es->s_uuid will be set by e2fsck if empty */
1146 * The rest of the superblock fields should be zero, and if not it
1147 * means they are likely already in use, so leave them alone. We
1148 * can leave it up to e2fsck to clean up any inconsistencies there.
1152 static inline struct inode *orphan_list_entry(struct list_head *l)
1154 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1157 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1159 struct list_head *l;
1161 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1162 le32_to_cpu(sbi->s_es->s_last_orphan));
1164 printk(KERN_ERR "sb_info orphan list:\n");
1165 list_for_each(l, &sbi->s_orphan) {
1166 struct inode *inode = orphan_list_entry(l);
1168 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1169 inode->i_sb->s_id, inode->i_ino, inode,
1170 inode->i_mode, inode->i_nlink,
1171 NEXT_ORPHAN(inode));
1176 static int ext4_quota_off(struct super_block *sb, int type);
1178 static inline void ext4_quotas_off(struct super_block *sb, int type)
1180 BUG_ON(type > EXT4_MAXQUOTAS);
1182 /* Use our quota_off function to clear inode flags etc. */
1183 for (type--; type >= 0; type--)
1184 ext4_quota_off(sb, type);
1188 * This is a helper function which is used in the mount/remount
1189 * codepaths (which holds s_umount) to fetch the quota file name.
1191 static inline char *get_qf_name(struct super_block *sb,
1192 struct ext4_sb_info *sbi,
1195 return rcu_dereference_protected(sbi->s_qf_names[type],
1196 lockdep_is_held(&sb->s_umount));
1199 static inline void ext4_quotas_off(struct super_block *sb, int type)
1204 static int ext4_percpu_param_init(struct ext4_sb_info *sbi)
1209 block = ext4_count_free_clusters(sbi->s_sb);
1210 ext4_free_blocks_count_set(sbi->s_es, EXT4_C2B(sbi, block));
1211 err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
1214 unsigned long freei = ext4_count_free_inodes(sbi->s_sb);
1215 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
1216 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
1220 err = percpu_counter_init(&sbi->s_dirs_counter,
1221 ext4_count_dirs(sbi->s_sb), GFP_KERNEL);
1223 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
1226 err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
1229 err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
1232 ext4_msg(sbi->s_sb, KERN_ERR, "insufficient memory");
1237 static void ext4_percpu_param_destroy(struct ext4_sb_info *sbi)
1239 percpu_counter_destroy(&sbi->s_freeclusters_counter);
1240 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1241 percpu_counter_destroy(&sbi->s_dirs_counter);
1242 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1243 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
1244 percpu_free_rwsem(&sbi->s_writepages_rwsem);
1247 static void ext4_group_desc_free(struct ext4_sb_info *sbi)
1249 struct buffer_head **group_desc;
1253 group_desc = rcu_dereference(sbi->s_group_desc);
1254 for (i = 0; i < sbi->s_gdb_count; i++)
1255 brelse(group_desc[i]);
1260 static void ext4_flex_groups_free(struct ext4_sb_info *sbi)
1262 struct flex_groups **flex_groups;
1266 flex_groups = rcu_dereference(sbi->s_flex_groups);
1268 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1269 kvfree(flex_groups[i]);
1270 kvfree(flex_groups);
1275 static void ext4_put_super(struct super_block *sb)
1277 struct ext4_sb_info *sbi = EXT4_SB(sb);
1278 struct ext4_super_block *es = sbi->s_es;
1283 * Unregister sysfs before destroying jbd2 journal.
1284 * Since we could still access attr_journal_task attribute via sysfs
1285 * path which could have sbi->s_journal->j_task as NULL
1286 * Unregister sysfs before flush sbi->s_sb_upd_work.
1287 * Since user may read /proc/fs/ext4/xx/mb_groups during umount, If
1288 * read metadata verify failed then will queue error work.
1289 * update_super_work will call start_this_handle may trigger
1292 ext4_unregister_sysfs(sb);
1294 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs unmount"))
1295 ext4_msg(sb, KERN_INFO, "unmounting filesystem %pU.",
1298 ext4_unregister_li_request(sb);
1299 ext4_quotas_off(sb, EXT4_MAXQUOTAS);
1301 flush_work(&sbi->s_sb_upd_work);
1302 destroy_workqueue(sbi->rsv_conversion_wq);
1303 ext4_release_orphan_info(sb);
1305 if (sbi->s_journal) {
1306 aborted = is_journal_aborted(sbi->s_journal);
1307 err = jbd2_journal_destroy(sbi->s_journal);
1308 sbi->s_journal = NULL;
1309 if ((err < 0) && !aborted) {
1310 ext4_abort(sb, -err, "Couldn't clean up the journal");
1314 ext4_es_unregister_shrinker(sbi);
1315 timer_shutdown_sync(&sbi->s_err_report);
1316 ext4_release_system_zone(sb);
1317 ext4_mb_release(sb);
1318 ext4_ext_release(sb);
1320 if (!sb_rdonly(sb) && !aborted) {
1321 ext4_clear_feature_journal_needs_recovery(sb);
1322 ext4_clear_feature_orphan_present(sb);
1323 es->s_state = cpu_to_le16(sbi->s_mount_state);
1326 ext4_commit_super(sb);
1328 ext4_group_desc_free(sbi);
1329 ext4_flex_groups_free(sbi);
1330 ext4_percpu_param_destroy(sbi);
1332 for (int i = 0; i < EXT4_MAXQUOTAS; i++)
1333 kfree(get_qf_name(sb, sbi, i));
1336 /* Debugging code just in case the in-memory inode orphan list
1337 * isn't empty. The on-disk one can be non-empty if we've
1338 * detected an error and taken the fs readonly, but the
1339 * in-memory list had better be clean by this point. */
1340 if (!list_empty(&sbi->s_orphan))
1341 dump_orphan_list(sb, sbi);
1342 ASSERT(list_empty(&sbi->s_orphan));
1344 sync_blockdev(sb->s_bdev);
1345 invalidate_bdev(sb->s_bdev);
1346 if (sbi->s_journal_bdev_file) {
1348 * Invalidate the journal device's buffers. We don't want them
1349 * floating about in memory - the physical journal device may
1350 * hotswapped, and it breaks the `ro-after' testing code.
1352 sync_blockdev(file_bdev(sbi->s_journal_bdev_file));
1353 invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
1356 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1357 sbi->s_ea_inode_cache = NULL;
1359 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
1360 sbi->s_ea_block_cache = NULL;
1362 ext4_stop_mmpd(sbi);
1365 sb->s_fs_info = NULL;
1367 * Now that we are completely done shutting down the
1368 * superblock, we need to actually destroy the kobject.
1370 kobject_put(&sbi->s_kobj);
1371 wait_for_completion(&sbi->s_kobj_unregister);
1372 if (sbi->s_chksum_driver)
1373 crypto_free_shash(sbi->s_chksum_driver);
1374 kfree(sbi->s_blockgroup_lock);
1375 fs_put_dax(sbi->s_daxdev, NULL);
1376 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
1377 #if IS_ENABLED(CONFIG_UNICODE)
1378 utf8_unload(sb->s_encoding);
1383 static struct kmem_cache *ext4_inode_cachep;
1386 * Called inside transaction, so use GFP_NOFS
1388 static struct inode *ext4_alloc_inode(struct super_block *sb)
1390 struct ext4_inode_info *ei;
1392 ei = alloc_inode_sb(sb, ext4_inode_cachep, GFP_NOFS);
1396 inode_set_iversion(&ei->vfs_inode, 1);
1398 spin_lock_init(&ei->i_raw_lock);
1399 ei->i_prealloc_node = RB_ROOT;
1400 atomic_set(&ei->i_prealloc_active, 0);
1401 rwlock_init(&ei->i_prealloc_lock);
1402 ext4_es_init_tree(&ei->i_es_tree);
1403 rwlock_init(&ei->i_es_lock);
1404 INIT_LIST_HEAD(&ei->i_es_list);
1405 ei->i_es_all_nr = 0;
1406 ei->i_es_shk_nr = 0;
1407 ei->i_es_shrink_lblk = 0;
1408 ei->i_reserved_data_blocks = 0;
1409 spin_lock_init(&(ei->i_block_reservation_lock));
1410 ext4_init_pending_tree(&ei->i_pending_tree);
1412 ei->i_reserved_quota = 0;
1413 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1416 INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1417 spin_lock_init(&ei->i_completed_io_lock);
1419 ei->i_datasync_tid = 0;
1420 atomic_set(&ei->i_unwritten, 0);
1421 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1422 ext4_fc_init_inode(&ei->vfs_inode);
1423 mutex_init(&ei->i_fc_lock);
1424 return &ei->vfs_inode;
1427 static int ext4_drop_inode(struct inode *inode)
1429 int drop = generic_drop_inode(inode);
1432 drop = fscrypt_drop_inode(inode);
1434 trace_ext4_drop_inode(inode, drop);
1438 static void ext4_free_in_core_inode(struct inode *inode)
1440 fscrypt_free_inode(inode);
1441 if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1442 pr_warn("%s: inode %ld still in fc list",
1443 __func__, inode->i_ino);
1445 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1448 static void ext4_destroy_inode(struct inode *inode)
1450 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1451 ext4_msg(inode->i_sb, KERN_ERR,
1452 "Inode %lu (%p): orphan list check failed!",
1453 inode->i_ino, EXT4_I(inode));
1454 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
1455 EXT4_I(inode), sizeof(struct ext4_inode_info),
1460 if (EXT4_I(inode)->i_reserved_data_blocks)
1461 ext4_msg(inode->i_sb, KERN_ERR,
1462 "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
1463 inode->i_ino, EXT4_I(inode),
1464 EXT4_I(inode)->i_reserved_data_blocks);
1467 static void ext4_shutdown(struct super_block *sb)
1469 ext4_force_shutdown(sb, EXT4_GOING_FLAGS_NOLOGFLUSH);
1472 static void init_once(void *foo)
1474 struct ext4_inode_info *ei = foo;
1476 INIT_LIST_HEAD(&ei->i_orphan);
1477 init_rwsem(&ei->xattr_sem);
1478 init_rwsem(&ei->i_data_sem);
1479 inode_init_once(&ei->vfs_inode);
1480 ext4_fc_init_inode(&ei->vfs_inode);
1483 static int __init init_inodecache(void)
1485 ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1486 sizeof(struct ext4_inode_info), 0,
1487 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
1488 offsetof(struct ext4_inode_info, i_data),
1489 sizeof_field(struct ext4_inode_info, i_data),
1491 if (ext4_inode_cachep == NULL)
1496 static void destroy_inodecache(void)
1499 * Make sure all delayed rcu free inodes are flushed before we
1503 kmem_cache_destroy(ext4_inode_cachep);
1506 void ext4_clear_inode(struct inode *inode)
1509 invalidate_inode_buffers(inode);
1511 ext4_discard_preallocations(inode);
1512 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1514 if (EXT4_I(inode)->jinode) {
1515 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1516 EXT4_I(inode)->jinode);
1517 jbd2_free_inode(EXT4_I(inode)->jinode);
1518 EXT4_I(inode)->jinode = NULL;
1520 fscrypt_put_encryption_info(inode);
1521 fsverity_cleanup_inode(inode);
1524 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
1525 u64 ino, u32 generation)
1527 struct inode *inode;
1530 * Currently we don't know the generation for parent directory, so
1531 * a generation of 0 means "accept any"
1533 inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
1535 return ERR_CAST(inode);
1536 if (generation && inode->i_generation != generation) {
1538 return ERR_PTR(-ESTALE);
1544 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
1545 int fh_len, int fh_type)
1547 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1548 ext4_nfs_get_inode);
1551 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
1552 int fh_len, int fh_type)
1554 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1555 ext4_nfs_get_inode);
1558 static int ext4_nfs_commit_metadata(struct inode *inode)
1560 struct writeback_control wbc = {
1561 .sync_mode = WB_SYNC_ALL
1564 trace_ext4_nfs_commit_metadata(inode);
1565 return ext4_write_inode(inode, &wbc);
1569 static const char * const quotatypes[] = INITQFNAMES;
1570 #define QTYPE2NAME(t) (quotatypes[t])
1572 static int ext4_write_dquot(struct dquot *dquot);
1573 static int ext4_acquire_dquot(struct dquot *dquot);
1574 static int ext4_release_dquot(struct dquot *dquot);
1575 static int ext4_mark_dquot_dirty(struct dquot *dquot);
1576 static int ext4_write_info(struct super_block *sb, int type);
1577 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1578 const struct path *path);
1579 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1580 size_t len, loff_t off);
1581 static ssize_t ext4_quota_write(struct super_block *sb, int type,
1582 const char *data, size_t len, loff_t off);
1583 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1584 unsigned int flags);
1586 static struct dquot __rcu **ext4_get_dquots(struct inode *inode)
1588 return EXT4_I(inode)->i_dquot;
1591 static const struct dquot_operations ext4_quota_operations = {
1592 .get_reserved_space = ext4_get_reserved_space,
1593 .write_dquot = ext4_write_dquot,
1594 .acquire_dquot = ext4_acquire_dquot,
1595 .release_dquot = ext4_release_dquot,
1596 .mark_dirty = ext4_mark_dquot_dirty,
1597 .write_info = ext4_write_info,
1598 .alloc_dquot = dquot_alloc,
1599 .destroy_dquot = dquot_destroy,
1600 .get_projid = ext4_get_projid,
1601 .get_inode_usage = ext4_get_inode_usage,
1602 .get_next_id = dquot_get_next_id,
1605 static const struct quotactl_ops ext4_qctl_operations = {
1606 .quota_on = ext4_quota_on,
1607 .quota_off = ext4_quota_off,
1608 .quota_sync = dquot_quota_sync,
1609 .get_state = dquot_get_state,
1610 .set_info = dquot_set_dqinfo,
1611 .get_dqblk = dquot_get_dqblk,
1612 .set_dqblk = dquot_set_dqblk,
1613 .get_nextdqblk = dquot_get_next_dqblk,
1617 static const struct super_operations ext4_sops = {
1618 .alloc_inode = ext4_alloc_inode,
1619 .free_inode = ext4_free_in_core_inode,
1620 .destroy_inode = ext4_destroy_inode,
1621 .write_inode = ext4_write_inode,
1622 .dirty_inode = ext4_dirty_inode,
1623 .drop_inode = ext4_drop_inode,
1624 .evict_inode = ext4_evict_inode,
1625 .put_super = ext4_put_super,
1626 .sync_fs = ext4_sync_fs,
1627 .freeze_fs = ext4_freeze,
1628 .unfreeze_fs = ext4_unfreeze,
1629 .statfs = ext4_statfs,
1630 .show_options = ext4_show_options,
1631 .shutdown = ext4_shutdown,
1633 .quota_read = ext4_quota_read,
1634 .quota_write = ext4_quota_write,
1635 .get_dquots = ext4_get_dquots,
1639 static const struct export_operations ext4_export_ops = {
1640 .encode_fh = generic_encode_ino32_fh,
1641 .fh_to_dentry = ext4_fh_to_dentry,
1642 .fh_to_parent = ext4_fh_to_parent,
1643 .get_parent = ext4_get_parent,
1644 .commit_metadata = ext4_nfs_commit_metadata,
1648 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1649 Opt_resgid, Opt_resuid, Opt_sb,
1650 Opt_nouid32, Opt_debug, Opt_removed,
1651 Opt_user_xattr, Opt_acl,
1652 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1653 Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1654 Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1655 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1656 Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1658 Opt_usrjquota, Opt_grpjquota, Opt_quota,
1659 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1660 Opt_usrquota, Opt_grpquota, Opt_prjquota,
1661 Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1662 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1663 Opt_nowarn_on_error, Opt_mblk_io_submit, Opt_debug_want_extra_isize,
1664 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
1665 Opt_inode_readahead_blks, Opt_journal_ioprio,
1666 Opt_dioread_nolock, Opt_dioread_lock,
1667 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1668 Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1669 Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
1670 Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
1671 #ifdef CONFIG_EXT4_DEBUG
1672 Opt_fc_debug_max_replay, Opt_fc_debug_force
1676 static const struct constant_table ext4_param_errors[] = {
1677 {"continue", EXT4_MOUNT_ERRORS_CONT},
1678 {"panic", EXT4_MOUNT_ERRORS_PANIC},
1679 {"remount-ro", EXT4_MOUNT_ERRORS_RO},
1683 static const struct constant_table ext4_param_data[] = {
1684 {"journal", EXT4_MOUNT_JOURNAL_DATA},
1685 {"ordered", EXT4_MOUNT_ORDERED_DATA},
1686 {"writeback", EXT4_MOUNT_WRITEBACK_DATA},
1690 static const struct constant_table ext4_param_data_err[] = {
1691 {"abort", Opt_data_err_abort},
1692 {"ignore", Opt_data_err_ignore},
1696 static const struct constant_table ext4_param_jqfmt[] = {
1697 {"vfsold", QFMT_VFS_OLD},
1698 {"vfsv0", QFMT_VFS_V0},
1699 {"vfsv1", QFMT_VFS_V1},
1703 static const struct constant_table ext4_param_dax[] = {
1704 {"always", Opt_dax_always},
1705 {"inode", Opt_dax_inode},
1706 {"never", Opt_dax_never},
1711 * Mount option specification
1712 * We don't use fsparam_flag_no because of the way we set the
1713 * options and the way we show them in _ext4_show_options(). To
1714 * keep the changes to a minimum, let's keep the negative options
1717 static const struct fs_parameter_spec ext4_param_specs[] = {
1718 fsparam_flag ("bsddf", Opt_bsd_df),
1719 fsparam_flag ("minixdf", Opt_minix_df),
1720 fsparam_flag ("grpid", Opt_grpid),
1721 fsparam_flag ("bsdgroups", Opt_grpid),
1722 fsparam_flag ("nogrpid", Opt_nogrpid),
1723 fsparam_flag ("sysvgroups", Opt_nogrpid),
1724 fsparam_u32 ("resgid", Opt_resgid),
1725 fsparam_u32 ("resuid", Opt_resuid),
1726 fsparam_u32 ("sb", Opt_sb),
1727 fsparam_enum ("errors", Opt_errors, ext4_param_errors),
1728 fsparam_flag ("nouid32", Opt_nouid32),
1729 fsparam_flag ("debug", Opt_debug),
1730 fsparam_flag ("oldalloc", Opt_removed),
1731 fsparam_flag ("orlov", Opt_removed),
1732 fsparam_flag ("user_xattr", Opt_user_xattr),
1733 fsparam_flag ("acl", Opt_acl),
1734 fsparam_flag ("norecovery", Opt_noload),
1735 fsparam_flag ("noload", Opt_noload),
1736 fsparam_flag ("bh", Opt_removed),
1737 fsparam_flag ("nobh", Opt_removed),
1738 fsparam_u32 ("commit", Opt_commit),
1739 fsparam_u32 ("min_batch_time", Opt_min_batch_time),
1740 fsparam_u32 ("max_batch_time", Opt_max_batch_time),
1741 fsparam_u32 ("journal_dev", Opt_journal_dev),
1742 fsparam_bdev ("journal_path", Opt_journal_path),
1743 fsparam_flag ("journal_checksum", Opt_journal_checksum),
1744 fsparam_flag ("nojournal_checksum", Opt_nojournal_checksum),
1745 fsparam_flag ("journal_async_commit",Opt_journal_async_commit),
1746 fsparam_flag ("abort", Opt_abort),
1747 fsparam_enum ("data", Opt_data, ext4_param_data),
1748 fsparam_enum ("data_err", Opt_data_err,
1749 ext4_param_data_err),
1750 fsparam_string_empty
1751 ("usrjquota", Opt_usrjquota),
1752 fsparam_string_empty
1753 ("grpjquota", Opt_grpjquota),
1754 fsparam_enum ("jqfmt", Opt_jqfmt, ext4_param_jqfmt),
1755 fsparam_flag ("grpquota", Opt_grpquota),
1756 fsparam_flag ("quota", Opt_quota),
1757 fsparam_flag ("noquota", Opt_noquota),
1758 fsparam_flag ("usrquota", Opt_usrquota),
1759 fsparam_flag ("prjquota", Opt_prjquota),
1760 fsparam_flag ("barrier", Opt_barrier),
1761 fsparam_u32 ("barrier", Opt_barrier),
1762 fsparam_flag ("nobarrier", Opt_nobarrier),
1763 fsparam_flag ("i_version", Opt_removed),
1764 fsparam_flag ("dax", Opt_dax),
1765 fsparam_enum ("dax", Opt_dax_type, ext4_param_dax),
1766 fsparam_u32 ("stripe", Opt_stripe),
1767 fsparam_flag ("delalloc", Opt_delalloc),
1768 fsparam_flag ("nodelalloc", Opt_nodelalloc),
1769 fsparam_flag ("warn_on_error", Opt_warn_on_error),
1770 fsparam_flag ("nowarn_on_error", Opt_nowarn_on_error),
1771 fsparam_u32 ("debug_want_extra_isize",
1772 Opt_debug_want_extra_isize),
1773 fsparam_flag ("mblk_io_submit", Opt_removed),
1774 fsparam_flag ("nomblk_io_submit", Opt_removed),
1775 fsparam_flag ("block_validity", Opt_block_validity),
1776 fsparam_flag ("noblock_validity", Opt_noblock_validity),
1777 fsparam_u32 ("inode_readahead_blks",
1778 Opt_inode_readahead_blks),
1779 fsparam_u32 ("journal_ioprio", Opt_journal_ioprio),
1780 fsparam_u32 ("auto_da_alloc", Opt_auto_da_alloc),
1781 fsparam_flag ("auto_da_alloc", Opt_auto_da_alloc),
1782 fsparam_flag ("noauto_da_alloc", Opt_noauto_da_alloc),
1783 fsparam_flag ("dioread_nolock", Opt_dioread_nolock),
1784 fsparam_flag ("nodioread_nolock", Opt_dioread_lock),
1785 fsparam_flag ("dioread_lock", Opt_dioread_lock),
1786 fsparam_flag ("discard", Opt_discard),
1787 fsparam_flag ("nodiscard", Opt_nodiscard),
1788 fsparam_u32 ("init_itable", Opt_init_itable),
1789 fsparam_flag ("init_itable", Opt_init_itable),
1790 fsparam_flag ("noinit_itable", Opt_noinit_itable),
1791 #ifdef CONFIG_EXT4_DEBUG
1792 fsparam_flag ("fc_debug_force", Opt_fc_debug_force),
1793 fsparam_u32 ("fc_debug_max_replay", Opt_fc_debug_max_replay),
1795 fsparam_u32 ("max_dir_size_kb", Opt_max_dir_size_kb),
1796 fsparam_flag ("test_dummy_encryption",
1797 Opt_test_dummy_encryption),
1798 fsparam_string ("test_dummy_encryption",
1799 Opt_test_dummy_encryption),
1800 fsparam_flag ("inlinecrypt", Opt_inlinecrypt),
1801 fsparam_flag ("nombcache", Opt_nombcache),
1802 fsparam_flag ("no_mbcache", Opt_nombcache), /* for backward compatibility */
1803 fsparam_flag ("prefetch_block_bitmaps",
1805 fsparam_flag ("no_prefetch_block_bitmaps",
1806 Opt_no_prefetch_block_bitmaps),
1807 fsparam_s32 ("mb_optimize_scan", Opt_mb_optimize_scan),
1808 fsparam_string ("check", Opt_removed), /* mount option from ext2/3 */
1809 fsparam_flag ("nocheck", Opt_removed), /* mount option from ext2/3 */
1810 fsparam_flag ("reservation", Opt_removed), /* mount option from ext2/3 */
1811 fsparam_flag ("noreservation", Opt_removed), /* mount option from ext2/3 */
1812 fsparam_u32 ("journal", Opt_removed), /* mount option from ext2/3 */
1816 #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1818 #define MOPT_SET 0x0001
1819 #define MOPT_CLEAR 0x0002
1820 #define MOPT_NOSUPPORT 0x0004
1821 #define MOPT_EXPLICIT 0x0008
1824 #define MOPT_QFMT 0x0010
1826 #define MOPT_Q MOPT_NOSUPPORT
1827 #define MOPT_QFMT MOPT_NOSUPPORT
1829 #define MOPT_NO_EXT2 0x0020
1830 #define MOPT_NO_EXT3 0x0040
1831 #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
1832 #define MOPT_SKIP 0x0080
1833 #define MOPT_2 0x0100
1835 static const struct mount_opts {
1839 } ext4_mount_opts[] = {
1840 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1841 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1842 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1843 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
1844 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1845 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
1846 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1847 MOPT_EXT4_ONLY | MOPT_SET},
1848 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1849 MOPT_EXT4_ONLY | MOPT_CLEAR},
1850 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1851 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
1852 {Opt_delalloc, EXT4_MOUNT_DELALLOC,
1853 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1854 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
1855 MOPT_EXT4_ONLY | MOPT_CLEAR},
1856 {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1857 {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1858 {Opt_commit, 0, MOPT_NO_EXT2},
1859 {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1860 MOPT_EXT4_ONLY | MOPT_CLEAR},
1861 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1862 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1863 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
1864 EXT4_MOUNT_JOURNAL_CHECKSUM),
1865 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1866 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
1867 {Opt_data_err, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_NO_EXT2},
1868 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1869 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1870 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1871 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1872 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1873 {Opt_dax_type, 0, MOPT_EXT4_ONLY},
1874 {Opt_journal_dev, 0, MOPT_NO_EXT2},
1875 {Opt_journal_path, 0, MOPT_NO_EXT2},
1876 {Opt_journal_ioprio, 0, MOPT_NO_EXT2},
1877 {Opt_data, 0, MOPT_NO_EXT2},
1878 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1879 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1880 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1882 {Opt_acl, 0, MOPT_NOSUPPORT},
1884 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1885 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1886 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1887 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1889 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1891 {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
1893 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
1894 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
1895 MOPT_CLEAR | MOPT_Q},
1896 {Opt_usrjquota, 0, MOPT_Q},
1897 {Opt_grpjquota, 0, MOPT_Q},
1898 {Opt_jqfmt, 0, MOPT_QFMT},
1899 {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
1900 {Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
1902 #ifdef CONFIG_EXT4_DEBUG
1903 {Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
1904 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
1906 {Opt_abort, EXT4_MOUNT2_ABORT, MOPT_SET | MOPT_2},
1910 #if IS_ENABLED(CONFIG_UNICODE)
1911 static const struct ext4_sb_encodings {
1914 unsigned int version;
1915 } ext4_sb_encoding_map[] = {
1916 {EXT4_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
1919 static const struct ext4_sb_encodings *
1920 ext4_sb_read_encoding(const struct ext4_super_block *es)
1922 __u16 magic = le16_to_cpu(es->s_encoding);
1925 for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1926 if (magic == ext4_sb_encoding_map[i].magic)
1927 return &ext4_sb_encoding_map[i];
1933 #define EXT4_SPEC_JQUOTA (1 << 0)
1934 #define EXT4_SPEC_JQFMT (1 << 1)
1935 #define EXT4_SPEC_DATAJ (1 << 2)
1936 #define EXT4_SPEC_SB_BLOCK (1 << 3)
1937 #define EXT4_SPEC_JOURNAL_DEV (1 << 4)
1938 #define EXT4_SPEC_JOURNAL_IOPRIO (1 << 5)
1939 #define EXT4_SPEC_s_want_extra_isize (1 << 7)
1940 #define EXT4_SPEC_s_max_batch_time (1 << 8)
1941 #define EXT4_SPEC_s_min_batch_time (1 << 9)
1942 #define EXT4_SPEC_s_inode_readahead_blks (1 << 10)
1943 #define EXT4_SPEC_s_li_wait_mult (1 << 11)
1944 #define EXT4_SPEC_s_max_dir_size_kb (1 << 12)
1945 #define EXT4_SPEC_s_stripe (1 << 13)
1946 #define EXT4_SPEC_s_resuid (1 << 14)
1947 #define EXT4_SPEC_s_resgid (1 << 15)
1948 #define EXT4_SPEC_s_commit_interval (1 << 16)
1949 #define EXT4_SPEC_s_fc_debug_max_replay (1 << 17)
1950 #define EXT4_SPEC_s_sb_block (1 << 18)
1951 #define EXT4_SPEC_mb_optimize_scan (1 << 19)
1953 struct ext4_fs_context {
1954 char *s_qf_names[EXT4_MAXQUOTAS];
1955 struct fscrypt_dummy_policy dummy_enc_policy;
1956 int s_jquota_fmt; /* Format of quota to use */
1957 #ifdef CONFIG_EXT4_DEBUG
1958 int s_fc_debug_max_replay;
1960 unsigned short qname_spec;
1961 unsigned long vals_s_flags; /* Bits to set in s_flags */
1962 unsigned long mask_s_flags; /* Bits changed in s_flags */
1963 unsigned long journal_devnum;
1964 unsigned long s_commit_interval;
1965 unsigned long s_stripe;
1966 unsigned int s_inode_readahead_blks;
1967 unsigned int s_want_extra_isize;
1968 unsigned int s_li_wait_mult;
1969 unsigned int s_max_dir_size_kb;
1970 unsigned int journal_ioprio;
1971 unsigned int vals_s_mount_opt;
1972 unsigned int mask_s_mount_opt;
1973 unsigned int vals_s_mount_opt2;
1974 unsigned int mask_s_mount_opt2;
1975 unsigned int opt_flags; /* MOPT flags */
1977 u32 s_max_batch_time;
1978 u32 s_min_batch_time;
1981 ext4_fsblk_t s_sb_block;
1984 static void ext4_fc_free(struct fs_context *fc)
1986 struct ext4_fs_context *ctx = fc->fs_private;
1992 for (i = 0; i < EXT4_MAXQUOTAS; i++)
1993 kfree(ctx->s_qf_names[i]);
1995 fscrypt_free_dummy_policy(&ctx->dummy_enc_policy);
1999 int ext4_init_fs_context(struct fs_context *fc)
2001 struct ext4_fs_context *ctx;
2003 ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
2007 fc->fs_private = ctx;
2008 fc->ops = &ext4_context_ops;
2015 * Note the name of the specified quota file.
2017 static int note_qf_name(struct fs_context *fc, int qtype,
2018 struct fs_parameter *param)
2020 struct ext4_fs_context *ctx = fc->fs_private;
2023 if (param->size < 1) {
2024 ext4_msg(NULL, KERN_ERR, "Missing quota name");
2027 if (strchr(param->string, '/')) {
2028 ext4_msg(NULL, KERN_ERR,
2029 "quotafile must be on filesystem root");
2032 if (ctx->s_qf_names[qtype]) {
2033 if (strcmp(ctx->s_qf_names[qtype], param->string) != 0) {
2034 ext4_msg(NULL, KERN_ERR,
2035 "%s quota file already specified",
2042 qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
2044 ext4_msg(NULL, KERN_ERR,
2045 "Not enough memory for storing quotafile name");
2048 ctx->s_qf_names[qtype] = qname;
2049 ctx->qname_spec |= 1 << qtype;
2050 ctx->spec |= EXT4_SPEC_JQUOTA;
2055 * Clear the name of the specified quota file.
2057 static int unnote_qf_name(struct fs_context *fc, int qtype)
2059 struct ext4_fs_context *ctx = fc->fs_private;
2061 kfree(ctx->s_qf_names[qtype]);
2063 ctx->s_qf_names[qtype] = NULL;
2064 ctx->qname_spec |= 1 << qtype;
2065 ctx->spec |= EXT4_SPEC_JQUOTA;
2070 static int ext4_parse_test_dummy_encryption(const struct fs_parameter *param,
2071 struct ext4_fs_context *ctx)
2075 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
2076 ext4_msg(NULL, KERN_WARNING,
2077 "test_dummy_encryption option not supported");
2080 err = fscrypt_parse_test_dummy_encryption(param,
2081 &ctx->dummy_enc_policy);
2082 if (err == -EINVAL) {
2083 ext4_msg(NULL, KERN_WARNING,
2084 "Value of option \"%s\" is unrecognized", param->key);
2085 } else if (err == -EEXIST) {
2086 ext4_msg(NULL, KERN_WARNING,
2087 "Conflicting test_dummy_encryption options");
2093 #define EXT4_SET_CTX(name) \
2094 static inline void ctx_set_##name(struct ext4_fs_context *ctx, \
2095 unsigned long flag) \
2097 ctx->mask_s_##name |= flag; \
2098 ctx->vals_s_##name |= flag; \
2101 #define EXT4_CLEAR_CTX(name) \
2102 static inline void ctx_clear_##name(struct ext4_fs_context *ctx, \
2103 unsigned long flag) \
2105 ctx->mask_s_##name |= flag; \
2106 ctx->vals_s_##name &= ~flag; \
2109 #define EXT4_TEST_CTX(name) \
2110 static inline unsigned long \
2111 ctx_test_##name(struct ext4_fs_context *ctx, unsigned long flag) \
2113 return (ctx->vals_s_##name & flag); \
2116 EXT4_SET_CTX(flags); /* set only */
2117 EXT4_SET_CTX(mount_opt);
2118 EXT4_CLEAR_CTX(mount_opt);
2119 EXT4_TEST_CTX(mount_opt);
2120 EXT4_SET_CTX(mount_opt2);
2121 EXT4_CLEAR_CTX(mount_opt2);
2122 EXT4_TEST_CTX(mount_opt2);
2124 static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param)
2126 struct ext4_fs_context *ctx = fc->fs_private;
2127 struct fs_parse_result result;
2128 const struct mount_opts *m;
2134 token = fs_parse(fc, ext4_param_specs, param, &result);
2137 is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
2139 for (m = ext4_mount_opts; m->token != Opt_err; m++)
2140 if (token == m->token)
2143 ctx->opt_flags |= m->flags;
2145 if (m->flags & MOPT_EXPLICIT) {
2146 if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2147 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_EXPLICIT_DELALLOC);
2148 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2149 ctx_set_mount_opt2(ctx,
2150 EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM);
2155 if (m->flags & MOPT_NOSUPPORT) {
2156 ext4_msg(NULL, KERN_ERR, "%s option not supported",
2164 if (!*param->string)
2165 return unnote_qf_name(fc, USRQUOTA);
2167 return note_qf_name(fc, USRQUOTA, param);
2169 if (!*param->string)
2170 return unnote_qf_name(fc, GRPQUOTA);
2172 return note_qf_name(fc, GRPQUOTA, param);
2175 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2176 ext4_msg(NULL, KERN_WARNING,
2177 "Ignoring %s option on remount", param->key);
2179 ctx->s_sb_block = result.uint_32;
2180 ctx->spec |= EXT4_SPEC_s_sb_block;
2184 ext4_msg(NULL, KERN_WARNING, "Ignoring removed %s option",
2187 case Opt_inlinecrypt:
2188 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
2189 ctx_set_flags(ctx, SB_INLINECRYPT);
2191 ext4_msg(NULL, KERN_ERR, "inline encryption not supported");
2195 ctx_clear_mount_opt(ctx, EXT4_MOUNT_ERRORS_MASK);
2196 ctx_set_mount_opt(ctx, result.uint_32);
2200 ctx->s_jquota_fmt = result.uint_32;
2201 ctx->spec |= EXT4_SPEC_JQFMT;
2205 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
2206 ctx_set_mount_opt(ctx, result.uint_32);
2207 ctx->spec |= EXT4_SPEC_DATAJ;
2210 if (result.uint_32 == 0)
2211 result.uint_32 = JBD2_DEFAULT_MAX_COMMIT_AGE;
2212 else if (result.uint_32 > INT_MAX / HZ) {
2213 ext4_msg(NULL, KERN_ERR,
2214 "Invalid commit interval %d, "
2215 "must be smaller than %d",
2216 result.uint_32, INT_MAX / HZ);
2219 ctx->s_commit_interval = HZ * result.uint_32;
2220 ctx->spec |= EXT4_SPEC_s_commit_interval;
2222 case Opt_debug_want_extra_isize:
2223 if ((result.uint_32 & 1) || (result.uint_32 < 4)) {
2224 ext4_msg(NULL, KERN_ERR,
2225 "Invalid want_extra_isize %d", result.uint_32);
2228 ctx->s_want_extra_isize = result.uint_32;
2229 ctx->spec |= EXT4_SPEC_s_want_extra_isize;
2231 case Opt_max_batch_time:
2232 ctx->s_max_batch_time = result.uint_32;
2233 ctx->spec |= EXT4_SPEC_s_max_batch_time;
2235 case Opt_min_batch_time:
2236 ctx->s_min_batch_time = result.uint_32;
2237 ctx->spec |= EXT4_SPEC_s_min_batch_time;
2239 case Opt_inode_readahead_blks:
2240 if (result.uint_32 &&
2241 (result.uint_32 > (1 << 30) ||
2242 !is_power_of_2(result.uint_32))) {
2243 ext4_msg(NULL, KERN_ERR,
2244 "EXT4-fs: inode_readahead_blks must be "
2245 "0 or a power of 2 smaller than 2^31");
2248 ctx->s_inode_readahead_blks = result.uint_32;
2249 ctx->spec |= EXT4_SPEC_s_inode_readahead_blks;
2251 case Opt_init_itable:
2252 ctx_set_mount_opt(ctx, EXT4_MOUNT_INIT_INODE_TABLE);
2253 ctx->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
2254 if (param->type == fs_value_is_string)
2255 ctx->s_li_wait_mult = result.uint_32;
2256 ctx->spec |= EXT4_SPEC_s_li_wait_mult;
2258 case Opt_max_dir_size_kb:
2259 ctx->s_max_dir_size_kb = result.uint_32;
2260 ctx->spec |= EXT4_SPEC_s_max_dir_size_kb;
2262 #ifdef CONFIG_EXT4_DEBUG
2263 case Opt_fc_debug_max_replay:
2264 ctx->s_fc_debug_max_replay = result.uint_32;
2265 ctx->spec |= EXT4_SPEC_s_fc_debug_max_replay;
2269 ctx->s_stripe = result.uint_32;
2270 ctx->spec |= EXT4_SPEC_s_stripe;
2273 uid = make_kuid(current_user_ns(), result.uint_32);
2274 if (!uid_valid(uid)) {
2275 ext4_msg(NULL, KERN_ERR, "Invalid uid value %d",
2279 ctx->s_resuid = uid;
2280 ctx->spec |= EXT4_SPEC_s_resuid;
2283 gid = make_kgid(current_user_ns(), result.uint_32);
2284 if (!gid_valid(gid)) {
2285 ext4_msg(NULL, KERN_ERR, "Invalid gid value %d",
2289 ctx->s_resgid = gid;
2290 ctx->spec |= EXT4_SPEC_s_resgid;
2292 case Opt_journal_dev:
2294 ext4_msg(NULL, KERN_ERR,
2295 "Cannot specify journal on remount");
2298 ctx->journal_devnum = result.uint_32;
2299 ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
2301 case Opt_journal_path:
2303 struct inode *journal_inode;
2308 ext4_msg(NULL, KERN_ERR,
2309 "Cannot specify journal on remount");
2313 error = fs_lookup_param(fc, param, 1, LOOKUP_FOLLOW, &path);
2315 ext4_msg(NULL, KERN_ERR, "error: could not find "
2316 "journal device path");
2320 journal_inode = d_inode(path.dentry);
2321 ctx->journal_devnum = new_encode_dev(journal_inode->i_rdev);
2322 ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
2326 case Opt_journal_ioprio:
2327 if (result.uint_32 > 7) {
2328 ext4_msg(NULL, KERN_ERR, "Invalid journal IO priority"
2332 ctx->journal_ioprio =
2333 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, result.uint_32);
2334 ctx->spec |= EXT4_SPEC_JOURNAL_IOPRIO;
2336 case Opt_test_dummy_encryption:
2337 return ext4_parse_test_dummy_encryption(param, ctx);
2340 #ifdef CONFIG_FS_DAX
2342 int type = (token == Opt_dax) ?
2343 Opt_dax : result.uint_32;
2347 case Opt_dax_always:
2348 ctx_set_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
2349 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
2352 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
2353 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
2356 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
2357 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
2358 /* Strictly for printing options */
2359 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE);
2365 ext4_msg(NULL, KERN_INFO, "dax option not supported");
2369 if (result.uint_32 == Opt_data_err_abort)
2370 ctx_set_mount_opt(ctx, m->mount_opt);
2371 else if (result.uint_32 == Opt_data_err_ignore)
2372 ctx_clear_mount_opt(ctx, m->mount_opt);
2374 case Opt_mb_optimize_scan:
2375 if (result.int_32 == 1) {
2376 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
2377 ctx->spec |= EXT4_SPEC_mb_optimize_scan;
2378 } else if (result.int_32 == 0) {
2379 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
2380 ctx->spec |= EXT4_SPEC_mb_optimize_scan;
2382 ext4_msg(NULL, KERN_WARNING,
2383 "mb_optimize_scan should be set to 0 or 1.");
2390 * At this point we should only be getting options requiring MOPT_SET,
2391 * or MOPT_CLEAR. Anything else is a bug
2393 if (m->token == Opt_err) {
2394 ext4_msg(NULL, KERN_WARNING, "buggy handling of option %s",
2401 unsigned int set = 0;
2403 if ((param->type == fs_value_is_flag) ||
2407 if (m->flags & MOPT_CLEAR)
2409 else if (unlikely(!(m->flags & MOPT_SET))) {
2410 ext4_msg(NULL, KERN_WARNING,
2411 "buggy handling of option %s",
2416 if (m->flags & MOPT_2) {
2418 ctx_set_mount_opt2(ctx, m->mount_opt);
2420 ctx_clear_mount_opt2(ctx, m->mount_opt);
2423 ctx_set_mount_opt(ctx, m->mount_opt);
2425 ctx_clear_mount_opt(ctx, m->mount_opt);
2432 static int parse_options(struct fs_context *fc, char *options)
2434 struct fs_parameter param;
2441 while ((key = strsep(&options, ",")) != NULL) {
2444 char *value = strchr(key, '=');
2446 param.type = fs_value_is_flag;
2447 param.string = NULL;
2454 v_len = strlen(value);
2455 param.string = kmemdup_nul(value, v_len,
2459 param.type = fs_value_is_string;
2465 ret = ext4_parse_param(fc, ¶m);
2466 kfree(param.string);
2472 ret = ext4_validate_options(fc);
2479 static int parse_apply_sb_mount_options(struct super_block *sb,
2480 struct ext4_fs_context *m_ctx)
2482 struct ext4_sb_info *sbi = EXT4_SB(sb);
2483 char *s_mount_opts = NULL;
2484 struct ext4_fs_context *s_ctx = NULL;
2485 struct fs_context *fc = NULL;
2488 if (!sbi->s_es->s_mount_opts[0])
2491 s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
2492 sizeof(sbi->s_es->s_mount_opts),
2497 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
2501 s_ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
2505 fc->fs_private = s_ctx;
2506 fc->s_fs_info = sbi;
2508 ret = parse_options(fc, s_mount_opts);
2512 ret = ext4_check_opt_consistency(fc, sb);
2515 ext4_msg(sb, KERN_WARNING,
2516 "failed to parse options in superblock: %s",
2522 if (s_ctx->spec & EXT4_SPEC_JOURNAL_DEV)
2523 m_ctx->journal_devnum = s_ctx->journal_devnum;
2524 if (s_ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)
2525 m_ctx->journal_ioprio = s_ctx->journal_ioprio;
2527 ext4_apply_options(fc, sb);
2535 kfree(s_mount_opts);
2539 static void ext4_apply_quota_options(struct fs_context *fc,
2540 struct super_block *sb)
2543 bool quota_feature = ext4_has_feature_quota(sb);
2544 struct ext4_fs_context *ctx = fc->fs_private;
2545 struct ext4_sb_info *sbi = EXT4_SB(sb);
2552 if (ctx->spec & EXT4_SPEC_JQUOTA) {
2553 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2554 if (!(ctx->qname_spec & (1 << i)))
2557 qname = ctx->s_qf_names[i]; /* May be NULL */
2560 ctx->s_qf_names[i] = NULL;
2561 qname = rcu_replace_pointer(sbi->s_qf_names[i], qname,
2562 lockdep_is_held(&sb->s_umount));
2564 kfree_rcu_mightsleep(qname);
2568 if (ctx->spec & EXT4_SPEC_JQFMT)
2569 sbi->s_jquota_fmt = ctx->s_jquota_fmt;
2574 * Check quota settings consistency.
2576 static int ext4_check_quota_consistency(struct fs_context *fc,
2577 struct super_block *sb)
2580 struct ext4_fs_context *ctx = fc->fs_private;
2581 struct ext4_sb_info *sbi = EXT4_SB(sb);
2582 bool quota_feature = ext4_has_feature_quota(sb);
2583 bool quota_loaded = sb_any_quota_loaded(sb);
2584 bool usr_qf_name, grp_qf_name, usrquota, grpquota;
2588 * We do the test below only for project quotas. 'usrquota' and
2589 * 'grpquota' mount options are allowed even without quota feature
2590 * to support legacy quotas in quota files.
2592 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_PRJQUOTA) &&
2593 !ext4_has_feature_project(sb)) {
2594 ext4_msg(NULL, KERN_ERR, "Project quota feature not enabled. "
2595 "Cannot enable project quota enforcement.");
2599 quota_flags = EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
2600 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA;
2602 ctx->mask_s_mount_opt & quota_flags &&
2603 !ctx_test_mount_opt(ctx, quota_flags))
2604 goto err_quota_change;
2606 if (ctx->spec & EXT4_SPEC_JQUOTA) {
2608 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2609 if (!(ctx->qname_spec & (1 << i)))
2613 !!sbi->s_qf_names[i] != !!ctx->s_qf_names[i])
2614 goto err_jquota_change;
2616 if (sbi->s_qf_names[i] && ctx->s_qf_names[i] &&
2617 strcmp(get_qf_name(sb, sbi, i),
2618 ctx->s_qf_names[i]) != 0)
2619 goto err_jquota_specified;
2622 if (quota_feature) {
2623 ext4_msg(NULL, KERN_INFO,
2624 "Journaled quota options ignored when "
2625 "QUOTA feature is enabled");
2630 if (ctx->spec & EXT4_SPEC_JQFMT) {
2631 if (sbi->s_jquota_fmt != ctx->s_jquota_fmt && quota_loaded)
2632 goto err_jquota_change;
2633 if (quota_feature) {
2634 ext4_msg(NULL, KERN_INFO, "Quota format mount options "
2635 "ignored when QUOTA feature is enabled");
2640 /* Make sure we don't mix old and new quota format */
2641 usr_qf_name = (get_qf_name(sb, sbi, USRQUOTA) ||
2642 ctx->s_qf_names[USRQUOTA]);
2643 grp_qf_name = (get_qf_name(sb, sbi, GRPQUOTA) ||
2644 ctx->s_qf_names[GRPQUOTA]);
2646 usrquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
2647 test_opt(sb, USRQUOTA));
2649 grpquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) ||
2650 test_opt(sb, GRPQUOTA));
2653 ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
2657 ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
2661 if (usr_qf_name || grp_qf_name) {
2662 if (usrquota || grpquota) {
2663 ext4_msg(NULL, KERN_ERR, "old and new quota "
2668 if (!(ctx->spec & EXT4_SPEC_JQFMT || sbi->s_jquota_fmt)) {
2669 ext4_msg(NULL, KERN_ERR, "journaled quota format "
2678 ext4_msg(NULL, KERN_ERR,
2679 "Cannot change quota options when quota turned on");
2682 ext4_msg(NULL, KERN_ERR, "Cannot change journaled quota "
2683 "options when quota turned on");
2685 err_jquota_specified:
2686 ext4_msg(NULL, KERN_ERR, "%s quota file already specified",
2694 static int ext4_check_test_dummy_encryption(const struct fs_context *fc,
2695 struct super_block *sb)
2697 const struct ext4_fs_context *ctx = fc->fs_private;
2698 const struct ext4_sb_info *sbi = EXT4_SB(sb);
2700 if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy))
2703 if (!ext4_has_feature_encrypt(sb)) {
2704 ext4_msg(NULL, KERN_WARNING,
2705 "test_dummy_encryption requires encrypt feature");
2709 * This mount option is just for testing, and it's not worthwhile to
2710 * implement the extra complexity (e.g. RCU protection) that would be
2711 * needed to allow it to be set or changed during remount. We do allow
2712 * it to be specified during remount, but only if there is no change.
2714 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2715 if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
2716 &ctx->dummy_enc_policy))
2718 ext4_msg(NULL, KERN_WARNING,
2719 "Can't set or change test_dummy_encryption on remount");
2722 /* Also make sure s_mount_opts didn't contain a conflicting value. */
2723 if (fscrypt_is_dummy_policy_set(&sbi->s_dummy_enc_policy)) {
2724 if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
2725 &ctx->dummy_enc_policy))
2727 ext4_msg(NULL, KERN_WARNING,
2728 "Conflicting test_dummy_encryption options");
2734 static void ext4_apply_test_dummy_encryption(struct ext4_fs_context *ctx,
2735 struct super_block *sb)
2737 if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy) ||
2738 /* if already set, it was already verified to be the same */
2739 fscrypt_is_dummy_policy_set(&EXT4_SB(sb)->s_dummy_enc_policy))
2741 EXT4_SB(sb)->s_dummy_enc_policy = ctx->dummy_enc_policy;
2742 memset(&ctx->dummy_enc_policy, 0, sizeof(ctx->dummy_enc_policy));
2743 ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2746 static int ext4_check_opt_consistency(struct fs_context *fc,
2747 struct super_block *sb)
2749 struct ext4_fs_context *ctx = fc->fs_private;
2750 struct ext4_sb_info *sbi = fc->s_fs_info;
2751 int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
2754 if ((ctx->opt_flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2755 ext4_msg(NULL, KERN_ERR,
2756 "Mount option(s) incompatible with ext2");
2759 if ((ctx->opt_flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2760 ext4_msg(NULL, KERN_ERR,
2761 "Mount option(s) incompatible with ext3");
2765 if (ctx->s_want_extra_isize >
2766 (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE)) {
2767 ext4_msg(NULL, KERN_ERR,
2768 "Invalid want_extra_isize %d",
2769 ctx->s_want_extra_isize);
2773 err = ext4_check_test_dummy_encryption(fc, sb);
2777 if ((ctx->spec & EXT4_SPEC_DATAJ) && is_remount) {
2778 if (!sbi->s_journal) {
2779 ext4_msg(NULL, KERN_WARNING,
2780 "Remounting file system with no journal "
2781 "so ignoring journalled data option");
2782 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
2783 } else if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS) !=
2784 test_opt(sb, DATA_FLAGS)) {
2785 ext4_msg(NULL, KERN_ERR, "Cannot change data mode "
2792 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
2793 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
2794 ext4_msg(NULL, KERN_ERR, "can't mount with "
2795 "both data=journal and dax");
2799 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
2800 (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2801 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2802 fail_dax_change_remount:
2803 ext4_msg(NULL, KERN_ERR, "can't change "
2804 "dax mount option while remounting");
2806 } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER) &&
2807 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2808 (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) {
2809 goto fail_dax_change_remount;
2810 } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE) &&
2811 ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2812 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2813 !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) {
2814 goto fail_dax_change_remount;
2818 return ext4_check_quota_consistency(fc, sb);
2821 static void ext4_apply_options(struct fs_context *fc, struct super_block *sb)
2823 struct ext4_fs_context *ctx = fc->fs_private;
2824 struct ext4_sb_info *sbi = fc->s_fs_info;
2826 sbi->s_mount_opt &= ~ctx->mask_s_mount_opt;
2827 sbi->s_mount_opt |= ctx->vals_s_mount_opt;
2828 sbi->s_mount_opt2 &= ~ctx->mask_s_mount_opt2;
2829 sbi->s_mount_opt2 |= ctx->vals_s_mount_opt2;
2830 sb->s_flags &= ~ctx->mask_s_flags;
2831 sb->s_flags |= ctx->vals_s_flags;
2833 #define APPLY(X) ({ if (ctx->spec & EXT4_SPEC_##X) sbi->X = ctx->X; })
2834 APPLY(s_commit_interval);
2836 APPLY(s_max_batch_time);
2837 APPLY(s_min_batch_time);
2838 APPLY(s_want_extra_isize);
2839 APPLY(s_inode_readahead_blks);
2840 APPLY(s_max_dir_size_kb);
2841 APPLY(s_li_wait_mult);
2845 #ifdef CONFIG_EXT4_DEBUG
2846 APPLY(s_fc_debug_max_replay);
2849 ext4_apply_quota_options(fc, sb);
2850 ext4_apply_test_dummy_encryption(ctx, sb);
2854 static int ext4_validate_options(struct fs_context *fc)
2857 struct ext4_fs_context *ctx = fc->fs_private;
2858 char *usr_qf_name, *grp_qf_name;
2860 usr_qf_name = ctx->s_qf_names[USRQUOTA];
2861 grp_qf_name = ctx->s_qf_names[GRPQUOTA];
2863 if (usr_qf_name || grp_qf_name) {
2864 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) && usr_qf_name)
2865 ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
2867 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) && grp_qf_name)
2868 ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
2870 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
2871 ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA)) {
2872 ext4_msg(NULL, KERN_ERR, "old and new quota "
2881 static inline void ext4_show_quota_options(struct seq_file *seq,
2882 struct super_block *sb)
2884 #if defined(CONFIG_QUOTA)
2885 struct ext4_sb_info *sbi = EXT4_SB(sb);
2886 char *usr_qf_name, *grp_qf_name;
2888 if (sbi->s_jquota_fmt) {
2891 switch (sbi->s_jquota_fmt) {
2902 seq_printf(seq, ",jqfmt=%s", fmtname);
2906 usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
2907 grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
2909 seq_show_option(seq, "usrjquota", usr_qf_name);
2911 seq_show_option(seq, "grpjquota", grp_qf_name);
2916 static const char *token2str(int token)
2918 const struct fs_parameter_spec *spec;
2920 for (spec = ext4_param_specs; spec->name != NULL; spec++)
2921 if (spec->opt == token && !spec->type)
2928 * - it's set to a non-default value OR
2929 * - if the per-sb default is different from the global default
2931 static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
2934 struct ext4_sb_info *sbi = EXT4_SB(sb);
2935 struct ext4_super_block *es = sbi->s_es;
2937 const struct mount_opts *m;
2938 char sep = nodefs ? '\n' : ',';
2940 #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
2941 #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
2943 if (sbi->s_sb_block != 1)
2944 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
2946 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
2947 int want_set = m->flags & MOPT_SET;
2948 int opt_2 = m->flags & MOPT_2;
2949 unsigned int mount_opt, def_mount_opt;
2951 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
2952 m->flags & MOPT_SKIP)
2956 mount_opt = sbi->s_mount_opt2;
2957 def_mount_opt = sbi->s_def_mount_opt2;
2959 mount_opt = sbi->s_mount_opt;
2960 def_mount_opt = sbi->s_def_mount_opt;
2962 /* skip if same as the default */
2963 if (!nodefs && !(m->mount_opt & (mount_opt ^ def_mount_opt)))
2965 /* select Opt_noFoo vs Opt_Foo */
2967 (mount_opt & m->mount_opt) != m->mount_opt) ||
2968 (!want_set && (mount_opt & m->mount_opt)))
2970 SEQ_OPTS_PRINT("%s", token2str(m->token));
2973 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
2974 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
2975 SEQ_OPTS_PRINT("resuid=%u",
2976 from_kuid_munged(&init_user_ns, sbi->s_resuid));
2977 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
2978 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
2979 SEQ_OPTS_PRINT("resgid=%u",
2980 from_kgid_munged(&init_user_ns, sbi->s_resgid));
2981 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
2982 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
2983 SEQ_OPTS_PUTS("errors=remount-ro");
2984 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
2985 SEQ_OPTS_PUTS("errors=continue");
2986 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
2987 SEQ_OPTS_PUTS("errors=panic");
2988 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
2989 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
2990 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
2991 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
2992 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
2993 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2994 if (nodefs || sbi->s_stripe)
2995 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
2996 if (nodefs || EXT4_MOUNT_DATA_FLAGS &
2997 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
2998 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2999 SEQ_OPTS_PUTS("data=journal");
3000 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
3001 SEQ_OPTS_PUTS("data=ordered");
3002 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
3003 SEQ_OPTS_PUTS("data=writeback");
3006 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
3007 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
3008 sbi->s_inode_readahead_blks);
3010 if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
3011 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
3012 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
3013 if (nodefs || sbi->s_max_dir_size_kb)
3014 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
3015 if (test_opt(sb, DATA_ERR_ABORT))
3016 SEQ_OPTS_PUTS("data_err=abort");
3018 fscrypt_show_test_dummy_encryption(seq, sep, sb);
3020 if (sb->s_flags & SB_INLINECRYPT)
3021 SEQ_OPTS_PUTS("inlinecrypt");
3023 if (test_opt(sb, DAX_ALWAYS)) {
3025 SEQ_OPTS_PUTS("dax");
3027 SEQ_OPTS_PUTS("dax=always");
3028 } else if (test_opt2(sb, DAX_NEVER)) {
3029 SEQ_OPTS_PUTS("dax=never");
3030 } else if (test_opt2(sb, DAX_INODE)) {
3031 SEQ_OPTS_PUTS("dax=inode");
3034 if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
3035 !test_opt2(sb, MB_OPTIMIZE_SCAN)) {
3036 SEQ_OPTS_PUTS("mb_optimize_scan=0");
3037 } else if (sbi->s_groups_count < MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
3038 test_opt2(sb, MB_OPTIMIZE_SCAN)) {
3039 SEQ_OPTS_PUTS("mb_optimize_scan=1");
3042 ext4_show_quota_options(seq, sb);
3046 static int ext4_show_options(struct seq_file *seq, struct dentry *root)
3048 return _ext4_show_options(seq, root->d_sb, 0);
3051 int ext4_seq_options_show(struct seq_file *seq, void *offset)
3053 struct super_block *sb = seq->private;
3056 seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
3057 rc = _ext4_show_options(seq, sb, 1);
3058 seq_puts(seq, "\n");
3062 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
3065 struct ext4_sb_info *sbi = EXT4_SB(sb);
3068 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
3069 ext4_msg(sb, KERN_ERR, "revision level too high, "
3070 "forcing read-only mode");
3076 if (!(sbi->s_mount_state & EXT4_VALID_FS))
3077 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
3078 "running e2fsck is recommended");
3079 else if (sbi->s_mount_state & EXT4_ERROR_FS)
3080 ext4_msg(sb, KERN_WARNING,
3081 "warning: mounting fs with errors, "
3082 "running e2fsck is recommended");
3083 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
3084 le16_to_cpu(es->s_mnt_count) >=
3085 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
3086 ext4_msg(sb, KERN_WARNING,
3087 "warning: maximal mount count reached, "
3088 "running e2fsck is recommended");
3089 else if (le32_to_cpu(es->s_checkinterval) &&
3090 (ext4_get_tstamp(es, s_lastcheck) +
3091 le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
3092 ext4_msg(sb, KERN_WARNING,
3093 "warning: checktime reached, "
3094 "running e2fsck is recommended");
3095 if (!sbi->s_journal)
3096 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
3097 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
3098 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
3099 le16_add_cpu(&es->s_mnt_count, 1);
3100 ext4_update_tstamp(es, s_mtime);
3101 if (sbi->s_journal) {
3102 ext4_set_feature_journal_needs_recovery(sb);
3103 if (ext4_has_feature_orphan_file(sb))
3104 ext4_set_feature_orphan_present(sb);
3107 err = ext4_commit_super(sb);
3109 if (test_opt(sb, DEBUG))
3110 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
3111 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
3113 sbi->s_groups_count,
3114 EXT4_BLOCKS_PER_GROUP(sb),
3115 EXT4_INODES_PER_GROUP(sb),
3116 sbi->s_mount_opt, sbi->s_mount_opt2);
3120 int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
3122 struct ext4_sb_info *sbi = EXT4_SB(sb);
3123 struct flex_groups **old_groups, **new_groups;
3126 if (!sbi->s_log_groups_per_flex)
3129 size = ext4_flex_group(sbi, ngroup - 1) + 1;
3130 if (size <= sbi->s_flex_groups_allocated)
3133 new_groups = kvzalloc(roundup_pow_of_two(size *
3134 sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
3136 ext4_msg(sb, KERN_ERR,
3137 "not enough memory for %d flex group pointers", size);
3140 for (i = sbi->s_flex_groups_allocated; i < size; i++) {
3141 new_groups[i] = kvzalloc(roundup_pow_of_two(
3142 sizeof(struct flex_groups)),
3144 if (!new_groups[i]) {
3145 for (j = sbi->s_flex_groups_allocated; j < i; j++)
3146 kvfree(new_groups[j]);
3148 ext4_msg(sb, KERN_ERR,
3149 "not enough memory for %d flex groups", size);
3154 old_groups = rcu_dereference(sbi->s_flex_groups);
3156 memcpy(new_groups, old_groups,
3157 (sbi->s_flex_groups_allocated *
3158 sizeof(struct flex_groups *)));
3160 rcu_assign_pointer(sbi->s_flex_groups, new_groups);
3161 sbi->s_flex_groups_allocated = size;
3163 ext4_kvfree_array_rcu(old_groups);
3167 static int ext4_fill_flex_info(struct super_block *sb)
3169 struct ext4_sb_info *sbi = EXT4_SB(sb);
3170 struct ext4_group_desc *gdp = NULL;
3171 struct flex_groups *fg;
3172 ext4_group_t flex_group;
3175 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
3176 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
3177 sbi->s_log_groups_per_flex = 0;
3181 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
3185 for (i = 0; i < sbi->s_groups_count; i++) {
3186 gdp = ext4_get_group_desc(sb, i, NULL);
3188 flex_group = ext4_flex_group(sbi, i);
3189 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
3190 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
3191 atomic64_add(ext4_free_group_clusters(sb, gdp),
3192 &fg->free_clusters);
3193 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
3201 static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
3202 struct ext4_group_desc *gdp)
3204 int offset = offsetof(struct ext4_group_desc, bg_checksum);
3206 __le32 le_group = cpu_to_le32(block_group);
3207 struct ext4_sb_info *sbi = EXT4_SB(sb);
3209 if (ext4_has_metadata_csum(sbi->s_sb)) {
3210 /* Use new metadata_csum algorithm */
3212 __u16 dummy_csum = 0;
3214 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
3216 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
3217 csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
3218 sizeof(dummy_csum));
3219 offset += sizeof(dummy_csum);
3220 if (offset < sbi->s_desc_size)
3221 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
3222 sbi->s_desc_size - offset);
3224 crc = csum32 & 0xFFFF;
3228 /* old crc16 code */
3229 if (!ext4_has_feature_gdt_csum(sb))
3232 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
3233 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
3234 crc = crc16(crc, (__u8 *)gdp, offset);
3235 offset += sizeof(gdp->bg_checksum); /* skip checksum */
3236 /* for checksum of struct ext4_group_desc do the rest...*/
3237 if (ext4_has_feature_64bit(sb) && offset < sbi->s_desc_size)
3238 crc = crc16(crc, (__u8 *)gdp + offset,
3239 sbi->s_desc_size - offset);
3242 return cpu_to_le16(crc);
3245 int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
3246 struct ext4_group_desc *gdp)
3248 if (ext4_has_group_desc_csum(sb) &&
3249 (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
3255 void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
3256 struct ext4_group_desc *gdp)
3258 if (!ext4_has_group_desc_csum(sb))
3260 gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
3263 /* Called at mount-time, super-block is locked */
3264 static int ext4_check_descriptors(struct super_block *sb,
3265 ext4_fsblk_t sb_block,
3266 ext4_group_t *first_not_zeroed)
3268 struct ext4_sb_info *sbi = EXT4_SB(sb);
3269 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
3270 ext4_fsblk_t last_block;
3271 ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
3272 ext4_fsblk_t block_bitmap;
3273 ext4_fsblk_t inode_bitmap;
3274 ext4_fsblk_t inode_table;
3275 int flexbg_flag = 0;
3276 ext4_group_t i, grp = sbi->s_groups_count;
3278 if (ext4_has_feature_flex_bg(sb))
3281 ext4_debug("Checking group descriptors");
3283 for (i = 0; i < sbi->s_groups_count; i++) {
3284 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
3286 if (i == sbi->s_groups_count - 1 || flexbg_flag)
3287 last_block = ext4_blocks_count(sbi->s_es) - 1;
3289 last_block = first_block +
3290 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
3292 if ((grp == sbi->s_groups_count) &&
3293 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3296 block_bitmap = ext4_block_bitmap(sb, gdp);
3297 if (block_bitmap == sb_block) {
3298 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3299 "Block bitmap for group %u overlaps "
3304 if (block_bitmap >= sb_block + 1 &&
3305 block_bitmap <= last_bg_block) {
3306 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3307 "Block bitmap for group %u overlaps "
3308 "block group descriptors", i);
3312 if (block_bitmap < first_block || block_bitmap > last_block) {
3313 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3314 "Block bitmap for group %u not in group "
3315 "(block %llu)!", i, block_bitmap);
3318 inode_bitmap = ext4_inode_bitmap(sb, gdp);
3319 if (inode_bitmap == sb_block) {
3320 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3321 "Inode bitmap for group %u overlaps "
3326 if (inode_bitmap >= sb_block + 1 &&
3327 inode_bitmap <= last_bg_block) {
3328 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3329 "Inode bitmap for group %u overlaps "
3330 "block group descriptors", i);
3334 if (inode_bitmap < first_block || inode_bitmap > last_block) {
3335 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3336 "Inode bitmap for group %u not in group "
3337 "(block %llu)!", i, inode_bitmap);
3340 inode_table = ext4_inode_table(sb, gdp);
3341 if (inode_table == sb_block) {
3342 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3343 "Inode table for group %u overlaps "
3348 if (inode_table >= sb_block + 1 &&
3349 inode_table <= last_bg_block) {
3350 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3351 "Inode table for group %u overlaps "
3352 "block group descriptors", i);
3356 if (inode_table < first_block ||
3357 inode_table + sbi->s_itb_per_group - 1 > last_block) {
3358 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3359 "Inode table for group %u not in group "
3360 "(block %llu)!", i, inode_table);
3363 ext4_lock_group(sb, i);
3364 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
3365 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3366 "Checksum for group %u failed (%u!=%u)",
3367 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
3368 gdp)), le16_to_cpu(gdp->bg_checksum));
3369 if (!sb_rdonly(sb)) {
3370 ext4_unlock_group(sb, i);
3374 ext4_unlock_group(sb, i);
3376 first_block += EXT4_BLOCKS_PER_GROUP(sb);
3378 if (NULL != first_not_zeroed)
3379 *first_not_zeroed = grp;
3384 * Maximal extent format file size.
3385 * Resulting logical blkno at s_maxbytes must fit in our on-disk
3386 * extent format containers, within a sector_t, and within i_blocks
3387 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
3388 * so that won't be a limiting factor.
3390 * However there is other limiting factor. We do store extents in the form
3391 * of starting block and length, hence the resulting length of the extent
3392 * covering maximum file size must fit into on-disk format containers as
3393 * well. Given that length is always by 1 unit bigger than max unit (because
3394 * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3396 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3398 static loff_t ext4_max_size(int blkbits, int has_huge_files)
3401 loff_t upper_limit = MAX_LFS_FILESIZE;
3403 BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
3405 if (!has_huge_files) {
3406 upper_limit = (1LL << 32) - 1;
3408 /* total blocks in file system block size */
3409 upper_limit >>= (blkbits - 9);
3410 upper_limit <<= blkbits;
3414 * 32-bit extent-start container, ee_block. We lower the maxbytes
3415 * by one fs block, so ee_len can cover the extent of maximum file
3418 res = (1LL << 32) - 1;
3421 /* Sanity check against vm- & vfs- imposed limits */
3422 if (res > upper_limit)
3429 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
3430 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
3431 * We need to be 1 filesystem block less than the 2^48 sector limit.
3433 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3435 loff_t upper_limit, res = EXT4_NDIR_BLOCKS;
3437 unsigned int ppb = 1 << (bits - 2);
3440 * This is calculated to be the largest file size for a dense, block
3441 * mapped file such that the file's total number of 512-byte sectors,
3442 * including data and all indirect blocks, does not exceed (2^48 - 1).
3444 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
3445 * number of 512-byte sectors of the file.
3447 if (!has_huge_files) {
3449 * !has_huge_files or implies that the inode i_block field
3450 * represents total file blocks in 2^32 512-byte sectors ==
3451 * size of vfs inode i_blocks * 8
3453 upper_limit = (1LL << 32) - 1;
3455 /* total blocks in file system block size */
3456 upper_limit >>= (bits - 9);
3460 * We use 48 bit ext4_inode i_blocks
3461 * With EXT4_HUGE_FILE_FL set the i_blocks
3462 * represent total number of blocks in
3463 * file system block size
3465 upper_limit = (1LL << 48) - 1;
3469 /* Compute how many blocks we can address by block tree */
3472 res += ((loff_t)ppb) * ppb * ppb;
3473 /* Compute how many metadata blocks are needed */
3475 meta_blocks += 1 + ppb;
3476 meta_blocks += 1 + ppb + ppb * ppb;
3477 /* Does block tree limit file size? */
3478 if (res + meta_blocks <= upper_limit)
3482 /* How many metadata blocks are needed for addressing upper_limit? */
3483 upper_limit -= EXT4_NDIR_BLOCKS;
3484 /* indirect blocks */
3487 /* double indirect blocks */
3488 if (upper_limit < ppb * ppb) {
3489 meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb);
3493 meta_blocks += 1 + ppb;
3494 upper_limit -= ppb * ppb;
3495 /* tripple indirect blocks for the rest */
3496 meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) +
3497 DIV_ROUND_UP_ULL(upper_limit, ppb*ppb);
3501 if (res > MAX_LFS_FILESIZE)
3502 res = MAX_LFS_FILESIZE;
3507 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
3508 ext4_fsblk_t logical_sb_block, int nr)
3510 struct ext4_sb_info *sbi = EXT4_SB(sb);
3511 ext4_group_t bg, first_meta_bg;
3514 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3516 if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
3517 return logical_sb_block + nr + 1;
3518 bg = sbi->s_desc_per_block * nr;
3519 if (ext4_bg_has_super(sb, bg))
3523 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3524 * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
3525 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3528 if (sb->s_blocksize == 1024 && nr == 0 &&
3529 le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3532 return (has_super + ext4_group_first_block_no(sb, bg));
3536 * ext4_get_stripe_size: Get the stripe size.
3537 * @sbi: In memory super block info
3539 * If we have specified it via mount option, then
3540 * use the mount option value. If the value specified at mount time is
3541 * greater than the blocks per group use the super block value.
3542 * If the super block value is greater than blocks per group return 0.
3543 * Allocator needs it be less than blocks per group.
3546 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3548 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3549 unsigned long stripe_width =
3550 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
3553 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
3554 ret = sbi->s_stripe;
3555 else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
3557 else if (stride && stride <= sbi->s_blocks_per_group)
3563 * If the stripe width is 1, this makes no sense and
3564 * we set it to 0 to turn off stripe handling code.
3573 * Check whether this filesystem can be mounted based on
3574 * the features present and the RDONLY/RDWR mount requested.
3575 * Returns 1 if this filesystem can be mounted as requested,
3576 * 0 if it cannot be.
3578 int ext4_feature_set_ok(struct super_block *sb, int readonly)
3580 if (ext4_has_unknown_ext4_incompat_features(sb)) {
3581 ext4_msg(sb, KERN_ERR,
3582 "Couldn't mount because of "
3583 "unsupported optional features (%x)",
3584 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3585 ~EXT4_FEATURE_INCOMPAT_SUPP));
3589 #if !IS_ENABLED(CONFIG_UNICODE)
3590 if (ext4_has_feature_casefold(sb)) {
3591 ext4_msg(sb, KERN_ERR,
3592 "Filesystem with casefold feature cannot be "
3593 "mounted without CONFIG_UNICODE");
3601 if (ext4_has_feature_readonly(sb)) {
3602 ext4_msg(sb, KERN_INFO, "filesystem is read-only");
3603 sb->s_flags |= SB_RDONLY;
3607 /* Check that feature set is OK for a read-write mount */
3608 if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3609 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3610 "unsupported optional features (%x)",
3611 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3612 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3615 if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3616 ext4_msg(sb, KERN_ERR,
3617 "Can't support bigalloc feature without "
3618 "extents feature\n");
3622 #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3623 if (!readonly && (ext4_has_feature_quota(sb) ||
3624 ext4_has_feature_project(sb))) {
3625 ext4_msg(sb, KERN_ERR,
3626 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3629 #endif /* CONFIG_QUOTA */
3634 * This function is called once a day if we have errors logged
3635 * on the file system
3637 static void print_daily_error_info(struct timer_list *t)
3639 struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3640 struct super_block *sb = sbi->s_sb;
3641 struct ext4_super_block *es = sbi->s_es;
3643 if (es->s_error_count)
3644 /* fsck newer than v1.41.13 is needed to clean this condition. */
3645 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
3646 le32_to_cpu(es->s_error_count));
3647 if (es->s_first_error_time) {
3648 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
3650 ext4_get_tstamp(es, s_first_error_time),
3651 (int) sizeof(es->s_first_error_func),
3652 es->s_first_error_func,
3653 le32_to_cpu(es->s_first_error_line));
3654 if (es->s_first_error_ino)
3655 printk(KERN_CONT ": inode %u",
3656 le32_to_cpu(es->s_first_error_ino));
3657 if (es->s_first_error_block)
3658 printk(KERN_CONT ": block %llu", (unsigned long long)
3659 le64_to_cpu(es->s_first_error_block));
3660 printk(KERN_CONT "\n");
3662 if (es->s_last_error_time) {
3663 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
3665 ext4_get_tstamp(es, s_last_error_time),
3666 (int) sizeof(es->s_last_error_func),
3667 es->s_last_error_func,
3668 le32_to_cpu(es->s_last_error_line));
3669 if (es->s_last_error_ino)
3670 printk(KERN_CONT ": inode %u",
3671 le32_to_cpu(es->s_last_error_ino));
3672 if (es->s_last_error_block)
3673 printk(KERN_CONT ": block %llu", (unsigned long long)
3674 le64_to_cpu(es->s_last_error_block));
3675 printk(KERN_CONT "\n");
3677 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
3680 /* Find next suitable group and run ext4_init_inode_table */
3681 static int ext4_run_li_request(struct ext4_li_request *elr)
3683 struct ext4_group_desc *gdp = NULL;
3684 struct super_block *sb = elr->lr_super;
3685 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3686 ext4_group_t group = elr->lr_next_group;
3687 unsigned int prefetch_ios = 0;
3689 int nr = EXT4_SB(sb)->s_mb_prefetch;
3692 if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
3693 elr->lr_next_group = ext4_mb_prefetch(sb, group, nr, &prefetch_ios);
3694 ext4_mb_prefetch_fini(sb, elr->lr_next_group, nr);
3695 trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group, nr);
3696 if (group >= elr->lr_next_group) {
3698 if (elr->lr_first_not_zeroed != ngroups &&
3699 !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
3700 elr->lr_next_group = elr->lr_first_not_zeroed;
3701 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3708 for (; group < ngroups; group++) {
3709 gdp = ext4_get_group_desc(sb, group, NULL);
3715 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3719 if (group >= ngroups)
3723 start_time = ktime_get_real_ns();
3724 ret = ext4_init_inode_table(sb, group,
3725 elr->lr_timeout ? 0 : 1);
3726 trace_ext4_lazy_itable_init(sb, group);
3727 if (elr->lr_timeout == 0) {
3728 elr->lr_timeout = nsecs_to_jiffies((ktime_get_real_ns() - start_time) *
3729 EXT4_SB(elr->lr_super)->s_li_wait_mult);
3731 elr->lr_next_sched = jiffies + elr->lr_timeout;
3732 elr->lr_next_group = group + 1;
3738 * Remove lr_request from the list_request and free the
3739 * request structure. Should be called with li_list_mtx held
3741 static void ext4_remove_li_request(struct ext4_li_request *elr)
3746 list_del(&elr->lr_request);
3747 EXT4_SB(elr->lr_super)->s_li_request = NULL;
3751 static void ext4_unregister_li_request(struct super_block *sb)
3753 mutex_lock(&ext4_li_mtx);
3754 if (!ext4_li_info) {
3755 mutex_unlock(&ext4_li_mtx);
3759 mutex_lock(&ext4_li_info->li_list_mtx);
3760 ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3761 mutex_unlock(&ext4_li_info->li_list_mtx);
3762 mutex_unlock(&ext4_li_mtx);
3765 static struct task_struct *ext4_lazyinit_task;
3768 * This is the function where ext4lazyinit thread lives. It walks
3769 * through the request list searching for next scheduled filesystem.
3770 * When such a fs is found, run the lazy initialization request
3771 * (ext4_rn_li_request) and keep track of the time spend in this
3772 * function. Based on that time we compute next schedule time of
3773 * the request. When walking through the list is complete, compute
3774 * next waking time and put itself into sleep.
3776 static int ext4_lazyinit_thread(void *arg)
3778 struct ext4_lazy_init *eli = arg;
3779 struct list_head *pos, *n;
3780 struct ext4_li_request *elr;
3781 unsigned long next_wakeup, cur;
3783 BUG_ON(NULL == eli);
3788 next_wakeup = MAX_JIFFY_OFFSET;
3790 mutex_lock(&eli->li_list_mtx);
3791 if (list_empty(&eli->li_request_list)) {
3792 mutex_unlock(&eli->li_list_mtx);
3795 list_for_each_safe(pos, n, &eli->li_request_list) {
3798 elr = list_entry(pos, struct ext4_li_request,
3801 if (time_before(jiffies, elr->lr_next_sched)) {
3802 if (time_before(elr->lr_next_sched, next_wakeup))
3803 next_wakeup = elr->lr_next_sched;
3806 if (down_read_trylock(&elr->lr_super->s_umount)) {
3807 if (sb_start_write_trylock(elr->lr_super)) {
3810 * We hold sb->s_umount, sb can not
3811 * be removed from the list, it is
3812 * now safe to drop li_list_mtx
3814 mutex_unlock(&eli->li_list_mtx);
3815 err = ext4_run_li_request(elr);
3816 sb_end_write(elr->lr_super);
3817 mutex_lock(&eli->li_list_mtx);
3820 up_read((&elr->lr_super->s_umount));
3822 /* error, remove the lazy_init job */
3824 ext4_remove_li_request(elr);
3828 elr->lr_next_sched = jiffies +
3829 get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
3831 if (time_before(elr->lr_next_sched, next_wakeup))
3832 next_wakeup = elr->lr_next_sched;
3834 mutex_unlock(&eli->li_list_mtx);
3839 if ((time_after_eq(cur, next_wakeup)) ||
3840 (MAX_JIFFY_OFFSET == next_wakeup)) {
3845 schedule_timeout_interruptible(next_wakeup - cur);
3847 if (kthread_should_stop()) {
3848 ext4_clear_request_list();
3855 * It looks like the request list is empty, but we need
3856 * to check it under the li_list_mtx lock, to prevent any
3857 * additions into it, and of course we should lock ext4_li_mtx
3858 * to atomically free the list and ext4_li_info, because at
3859 * this point another ext4 filesystem could be registering
3862 mutex_lock(&ext4_li_mtx);
3863 mutex_lock(&eli->li_list_mtx);
3864 if (!list_empty(&eli->li_request_list)) {
3865 mutex_unlock(&eli->li_list_mtx);
3866 mutex_unlock(&ext4_li_mtx);
3869 mutex_unlock(&eli->li_list_mtx);
3870 kfree(ext4_li_info);
3871 ext4_li_info = NULL;
3872 mutex_unlock(&ext4_li_mtx);
3877 static void ext4_clear_request_list(void)
3879 struct list_head *pos, *n;
3880 struct ext4_li_request *elr;
3882 mutex_lock(&ext4_li_info->li_list_mtx);
3883 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3884 elr = list_entry(pos, struct ext4_li_request,
3886 ext4_remove_li_request(elr);
3888 mutex_unlock(&ext4_li_info->li_list_mtx);
3891 static int ext4_run_lazyinit_thread(void)
3893 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3894 ext4_li_info, "ext4lazyinit");
3895 if (IS_ERR(ext4_lazyinit_task)) {
3896 int err = PTR_ERR(ext4_lazyinit_task);
3897 ext4_clear_request_list();
3898 kfree(ext4_li_info);
3899 ext4_li_info = NULL;
3900 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3901 "initialization thread\n",
3905 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3910 * Check whether it make sense to run itable init. thread or not.
3911 * If there is at least one uninitialized inode table, return
3912 * corresponding group number, else the loop goes through all
3913 * groups and return total number of groups.
3915 static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3917 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3918 struct ext4_group_desc *gdp = NULL;
3920 if (!ext4_has_group_desc_csum(sb))
3923 for (group = 0; group < ngroups; group++) {
3924 gdp = ext4_get_group_desc(sb, group, NULL);
3928 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3935 static int ext4_li_info_new(void)
3937 struct ext4_lazy_init *eli = NULL;
3939 eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3943 INIT_LIST_HEAD(&eli->li_request_list);
3944 mutex_init(&eli->li_list_mtx);
3946 eli->li_state |= EXT4_LAZYINIT_QUIT;
3953 static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3956 struct ext4_li_request *elr;
3958 elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3963 elr->lr_first_not_zeroed = start;
3964 if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
3965 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3966 elr->lr_next_group = start;
3968 elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
3972 * Randomize first schedule time of the request to
3973 * spread the inode table initialization requests
3976 elr->lr_next_sched = jiffies + get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
3980 int ext4_register_li_request(struct super_block *sb,
3981 ext4_group_t first_not_zeroed)
3983 struct ext4_sb_info *sbi = EXT4_SB(sb);
3984 struct ext4_li_request *elr = NULL;
3985 ext4_group_t ngroups = sbi->s_groups_count;
3988 mutex_lock(&ext4_li_mtx);
3989 if (sbi->s_li_request != NULL) {
3991 * Reset timeout so it can be computed again, because
3992 * s_li_wait_mult might have changed.
3994 sbi->s_li_request->lr_timeout = 0;
3998 if (sb_rdonly(sb) ||
3999 (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
4000 (first_not_zeroed == ngroups || !test_opt(sb, INIT_INODE_TABLE))))
4003 elr = ext4_li_request_new(sb, first_not_zeroed);
4009 if (NULL == ext4_li_info) {
4010 ret = ext4_li_info_new();
4015 mutex_lock(&ext4_li_info->li_list_mtx);
4016 list_add(&elr->lr_request, &ext4_li_info->li_request_list);
4017 mutex_unlock(&ext4_li_info->li_list_mtx);
4019 sbi->s_li_request = elr;
4021 * set elr to NULL here since it has been inserted to
4022 * the request_list and the removal and free of it is
4023 * handled by ext4_clear_request_list from now on.
4027 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
4028 ret = ext4_run_lazyinit_thread();
4033 mutex_unlock(&ext4_li_mtx);
4040 * We do not need to lock anything since this is called on
4043 static void ext4_destroy_lazyinit_thread(void)
4046 * If thread exited earlier
4047 * there's nothing to be done.
4049 if (!ext4_li_info || !ext4_lazyinit_task)
4052 kthread_stop(ext4_lazyinit_task);
4055 static int set_journal_csum_feature_set(struct super_block *sb)
4058 int compat, incompat;
4059 struct ext4_sb_info *sbi = EXT4_SB(sb);
4061 if (ext4_has_metadata_csum(sb)) {
4062 /* journal checksum v3 */
4064 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
4066 /* journal checksum v1 */
4067 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
4071 jbd2_journal_clear_features(sbi->s_journal,
4072 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
4073 JBD2_FEATURE_INCOMPAT_CSUM_V3 |
4074 JBD2_FEATURE_INCOMPAT_CSUM_V2);
4075 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4076 ret = jbd2_journal_set_features(sbi->s_journal,
4078 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
4080 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
4081 ret = jbd2_journal_set_features(sbi->s_journal,
4084 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
4085 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
4087 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
4088 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
4095 * Note: calculating the overhead so we can be compatible with
4096 * historical BSD practice is quite difficult in the face of
4097 * clusters/bigalloc. This is because multiple metadata blocks from
4098 * different block group can end up in the same allocation cluster.
4099 * Calculating the exact overhead in the face of clustered allocation
4100 * requires either O(all block bitmaps) in memory or O(number of block
4101 * groups**2) in time. We will still calculate the superblock for
4102 * older file systems --- and if we come across with a bigalloc file
4103 * system with zero in s_overhead_clusters the estimate will be close to
4104 * correct especially for very large cluster sizes --- but for newer
4105 * file systems, it's better to calculate this figure once at mkfs
4106 * time, and store it in the superblock. If the superblock value is
4107 * present (even for non-bigalloc file systems), we will use it.
4109 static int count_overhead(struct super_block *sb, ext4_group_t grp,
4112 struct ext4_sb_info *sbi = EXT4_SB(sb);
4113 struct ext4_group_desc *gdp;
4114 ext4_fsblk_t first_block, last_block, b;
4115 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4116 int s, j, count = 0;
4117 int has_super = ext4_bg_has_super(sb, grp);
4119 if (!ext4_has_feature_bigalloc(sb))
4120 return (has_super + ext4_bg_num_gdb(sb, grp) +
4121 (has_super ? le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0) +
4122 sbi->s_itb_per_group + 2);
4124 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
4125 (grp * EXT4_BLOCKS_PER_GROUP(sb));
4126 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
4127 for (i = 0; i < ngroups; i++) {
4128 gdp = ext4_get_group_desc(sb, i, NULL);
4129 b = ext4_block_bitmap(sb, gdp);
4130 if (b >= first_block && b <= last_block) {
4131 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4134 b = ext4_inode_bitmap(sb, gdp);
4135 if (b >= first_block && b <= last_block) {
4136 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4139 b = ext4_inode_table(sb, gdp);
4140 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
4141 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
4142 int c = EXT4_B2C(sbi, b - first_block);
4143 ext4_set_bit(c, buf);
4149 if (ext4_bg_has_super(sb, grp)) {
4150 ext4_set_bit(s++, buf);
4153 j = ext4_bg_num_gdb(sb, grp);
4154 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
4155 ext4_error(sb, "Invalid number of block group "
4156 "descriptor blocks: %d", j);
4157 j = EXT4_BLOCKS_PER_GROUP(sb) - s;
4161 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
4165 return EXT4_CLUSTERS_PER_GROUP(sb) -
4166 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
4170 * Compute the overhead and stash it in sbi->s_overhead
4172 int ext4_calculate_overhead(struct super_block *sb)
4174 struct ext4_sb_info *sbi = EXT4_SB(sb);
4175 struct ext4_super_block *es = sbi->s_es;
4176 struct inode *j_inode;
4177 unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
4178 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4179 ext4_fsblk_t overhead = 0;
4180 char *buf = (char *) get_zeroed_page(GFP_NOFS);
4186 * Compute the overhead (FS structures). This is constant
4187 * for a given filesystem unless the number of block groups
4188 * changes so we cache the previous value until it does.
4192 * All of the blocks before first_data_block are overhead
4194 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
4197 * Add the overhead found in each block group
4199 for (i = 0; i < ngroups; i++) {
4202 blks = count_overhead(sb, i, buf);
4205 memset(buf, 0, PAGE_SIZE);
4210 * Add the internal journal blocks whether the journal has been
4213 if (sbi->s_journal && !sbi->s_journal_bdev_file)
4214 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
4215 else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
4216 /* j_inum for internal journal is non-zero */
4217 j_inode = ext4_get_journal_inode(sb, j_inum);
4218 if (!IS_ERR(j_inode)) {
4219 j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
4220 overhead += EXT4_NUM_B2C(sbi, j_blocks);
4223 ext4_msg(sb, KERN_ERR, "can't get journal size");
4226 sbi->s_overhead = overhead;
4228 free_page((unsigned long) buf);
4232 static void ext4_set_resv_clusters(struct super_block *sb)
4234 ext4_fsblk_t resv_clusters;
4235 struct ext4_sb_info *sbi = EXT4_SB(sb);
4238 * There's no need to reserve anything when we aren't using extents.
4239 * The space estimates are exact, there are no unwritten extents,
4240 * hole punching doesn't need new metadata... This is needed especially
4241 * to keep ext2/3 backward compatibility.
4243 if (!ext4_has_feature_extents(sb))
4246 * By default we reserve 2% or 4096 clusters, whichever is smaller.
4247 * This should cover the situations where we can not afford to run
4248 * out of space like for example punch hole, or converting
4249 * unwritten extents in delalloc path. In most cases such
4250 * allocation would require 1, or 2 blocks, higher numbers are
4253 resv_clusters = (ext4_blocks_count(sbi->s_es) >>
4254 sbi->s_cluster_bits);
4256 do_div(resv_clusters, 50);
4257 resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
4259 atomic64_set(&sbi->s_resv_clusters, resv_clusters);
4262 static const char *ext4_quota_mode(struct super_block *sb)
4265 if (!ext4_quota_capable(sb))
4268 if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
4269 return "journalled";
4277 static void ext4_setup_csum_trigger(struct super_block *sb,
4278 enum ext4_journal_trigger_type type,
4280 struct jbd2_buffer_trigger_type *type,
4281 struct buffer_head *bh,
4285 struct ext4_sb_info *sbi = EXT4_SB(sb);
4287 sbi->s_journal_triggers[type].sb = sb;
4288 sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
4291 static void ext4_free_sbi(struct ext4_sb_info *sbi)
4296 kfree(sbi->s_blockgroup_lock);
4297 fs_put_dax(sbi->s_daxdev, NULL);
4301 static struct ext4_sb_info *ext4_alloc_sbi(struct super_block *sb)
4303 struct ext4_sb_info *sbi;
4305 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
4309 sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->s_dax_part_off,
4312 sbi->s_blockgroup_lock =
4313 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
4315 if (!sbi->s_blockgroup_lock)
4318 sb->s_fs_info = sbi;
4322 fs_put_dax(sbi->s_daxdev, NULL);
4327 static void ext4_set_def_opts(struct super_block *sb,
4328 struct ext4_super_block *es)
4330 unsigned long def_mount_opts;
4332 /* Set defaults before we parse the mount options */
4333 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4334 set_opt(sb, INIT_INODE_TABLE);
4335 if (def_mount_opts & EXT4_DEFM_DEBUG)
4337 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4339 if (def_mount_opts & EXT4_DEFM_UID16)
4340 set_opt(sb, NO_UID32);
4341 /* xattr user namespace & acls are now defaulted on */
4342 set_opt(sb, XATTR_USER);
4343 #ifdef CONFIG_EXT4_FS_POSIX_ACL
4344 set_opt(sb, POSIX_ACL);
4346 if (ext4_has_feature_fast_commit(sb))
4347 set_opt2(sb, JOURNAL_FAST_COMMIT);
4348 /* don't forget to enable journal_csum when metadata_csum is enabled. */
4349 if (ext4_has_metadata_csum(sb))
4350 set_opt(sb, JOURNAL_CHECKSUM);
4352 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4353 set_opt(sb, JOURNAL_DATA);
4354 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4355 set_opt(sb, ORDERED_DATA);
4356 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4357 set_opt(sb, WRITEBACK_DATA);
4359 if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_PANIC)
4360 set_opt(sb, ERRORS_PANIC);
4361 else if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_CONTINUE)
4362 set_opt(sb, ERRORS_CONT);
4364 set_opt(sb, ERRORS_RO);
4365 /* block_validity enabled by default; disable with noblock_validity */
4366 set_opt(sb, BLOCK_VALIDITY);
4367 if (def_mount_opts & EXT4_DEFM_DISCARD)
4368 set_opt(sb, DISCARD);
4370 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4371 set_opt(sb, BARRIER);
4374 * enable delayed allocation by default
4375 * Use -o nodelalloc to turn it off
4377 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
4378 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4379 set_opt(sb, DELALLOC);
4381 if (sb->s_blocksize <= PAGE_SIZE)
4382 set_opt(sb, DIOREAD_NOLOCK);
4385 static int ext4_handle_clustersize(struct super_block *sb)
4387 struct ext4_sb_info *sbi = EXT4_SB(sb);
4388 struct ext4_super_block *es = sbi->s_es;
4391 /* Handle clustersize */
4392 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4393 if (ext4_has_feature_bigalloc(sb)) {
4394 if (clustersize < sb->s_blocksize) {
4395 ext4_msg(sb, KERN_ERR,
4396 "cluster size (%d) smaller than "
4397 "block size (%lu)", clustersize, sb->s_blocksize);
4400 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4401 le32_to_cpu(es->s_log_block_size);
4403 if (clustersize != sb->s_blocksize) {
4404 ext4_msg(sb, KERN_ERR,
4405 "fragment/cluster size (%d) != "
4406 "block size (%lu)", clustersize, sb->s_blocksize);
4409 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
4410 ext4_msg(sb, KERN_ERR,
4411 "#blocks per group too big: %lu",
4412 sbi->s_blocks_per_group);
4415 sbi->s_cluster_bits = 0;
4417 sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group);
4418 if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
4419 ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu",
4420 sbi->s_clusters_per_group);
4423 if (sbi->s_blocks_per_group !=
4424 (sbi->s_clusters_per_group * (clustersize / sb->s_blocksize))) {
4425 ext4_msg(sb, KERN_ERR,
4426 "blocks per group (%lu) and clusters per group (%lu) inconsistent",
4427 sbi->s_blocks_per_group, sbi->s_clusters_per_group);
4430 sbi->s_cluster_ratio = clustersize / sb->s_blocksize;
4432 /* Do we have standard group size of clustersize * 8 blocks ? */
4433 if (sbi->s_blocks_per_group == clustersize << 3)
4434 set_opt2(sb, STD_GROUP_SIZE);
4439 static void ext4_fast_commit_init(struct super_block *sb)
4441 struct ext4_sb_info *sbi = EXT4_SB(sb);
4443 /* Initialize fast commit stuff */
4444 atomic_set(&sbi->s_fc_subtid, 0);
4445 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4446 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4447 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4448 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4449 sbi->s_fc_bytes = 0;
4450 ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
4451 sbi->s_fc_ineligible_tid = 0;
4452 spin_lock_init(&sbi->s_fc_lock);
4453 memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
4454 sbi->s_fc_replay_state.fc_regions = NULL;
4455 sbi->s_fc_replay_state.fc_regions_size = 0;
4456 sbi->s_fc_replay_state.fc_regions_used = 0;
4457 sbi->s_fc_replay_state.fc_regions_valid = 0;
4458 sbi->s_fc_replay_state.fc_modified_inodes = NULL;
4459 sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
4460 sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4463 static int ext4_inode_info_init(struct super_block *sb,
4464 struct ext4_super_block *es)
4466 struct ext4_sb_info *sbi = EXT4_SB(sb);
4468 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4469 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4470 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4472 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4473 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4474 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4475 ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4479 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4480 (!is_power_of_2(sbi->s_inode_size)) ||
4481 (sbi->s_inode_size > sb->s_blocksize)) {
4482 ext4_msg(sb, KERN_ERR,
4483 "unsupported inode size: %d",
4485 ext4_msg(sb, KERN_ERR, "blocksize: %lu", sb->s_blocksize);
4489 * i_atime_extra is the last extra field available for
4490 * [acm]times in struct ext4_inode. Checking for that
4491 * field should suffice to ensure we have extra space
4494 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4495 sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4496 sb->s_time_gran = 1;
4497 sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4499 sb->s_time_gran = NSEC_PER_SEC;
4500 sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4502 sb->s_time_min = EXT4_TIMESTAMP_MIN;
4505 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4506 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4507 EXT4_GOOD_OLD_INODE_SIZE;
4508 if (ext4_has_feature_extra_isize(sb)) {
4509 unsigned v, max = (sbi->s_inode_size -
4510 EXT4_GOOD_OLD_INODE_SIZE);
4512 v = le16_to_cpu(es->s_want_extra_isize);
4514 ext4_msg(sb, KERN_ERR,
4515 "bad s_want_extra_isize: %d", v);
4518 if (sbi->s_want_extra_isize < v)
4519 sbi->s_want_extra_isize = v;
4521 v = le16_to_cpu(es->s_min_extra_isize);
4523 ext4_msg(sb, KERN_ERR,
4524 "bad s_min_extra_isize: %d", v);
4527 if (sbi->s_want_extra_isize < v)
4528 sbi->s_want_extra_isize = v;
4535 #if IS_ENABLED(CONFIG_UNICODE)
4536 static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
4538 const struct ext4_sb_encodings *encoding_info;
4539 struct unicode_map *encoding;
4540 __u16 encoding_flags = le16_to_cpu(es->s_encoding_flags);
4542 if (!ext4_has_feature_casefold(sb) || sb->s_encoding)
4545 encoding_info = ext4_sb_read_encoding(es);
4546 if (!encoding_info) {
4547 ext4_msg(sb, KERN_ERR,
4548 "Encoding requested by superblock is unknown");
4552 encoding = utf8_load(encoding_info->version);
4553 if (IS_ERR(encoding)) {
4554 ext4_msg(sb, KERN_ERR,
4555 "can't mount with superblock charset: %s-%u.%u.%u "
4556 "not supported by the kernel. flags: 0x%x.",
4557 encoding_info->name,
4558 unicode_major(encoding_info->version),
4559 unicode_minor(encoding_info->version),
4560 unicode_rev(encoding_info->version),
4564 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4565 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4566 unicode_major(encoding_info->version),
4567 unicode_minor(encoding_info->version),
4568 unicode_rev(encoding_info->version),
4571 sb->s_encoding = encoding;
4572 sb->s_encoding_flags = encoding_flags;
4577 static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
4583 static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_block *es)
4585 struct ext4_sb_info *sbi = EXT4_SB(sb);
4587 /* Warn if metadata_csum and gdt_csum are both set. */
4588 if (ext4_has_feature_metadata_csum(sb) &&
4589 ext4_has_feature_gdt_csum(sb))
4590 ext4_warning(sb, "metadata_csum and uninit_bg are "
4591 "redundant flags; please run fsck.");
4593 /* Check for a known checksum algorithm */
4594 if (!ext4_verify_csum_type(sb, es)) {
4595 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4596 "unknown checksum algorithm.");
4599 ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
4600 ext4_orphan_file_block_trigger);
4602 /* Load the checksum driver */
4603 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
4604 if (IS_ERR(sbi->s_chksum_driver)) {
4605 int ret = PTR_ERR(sbi->s_chksum_driver);
4606 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
4607 sbi->s_chksum_driver = NULL;
4611 /* Check superblock checksum */
4612 if (!ext4_superblock_csum_verify(sb, es)) {
4613 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4614 "invalid superblock checksum. Run e2fsck?");
4618 /* Precompute checksum seed for all metadata */
4619 if (ext4_has_feature_csum_seed(sb))
4620 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4621 else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4622 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4623 sizeof(es->s_uuid));
4627 static int ext4_check_feature_compatibility(struct super_block *sb,
4628 struct ext4_super_block *es,
4631 struct ext4_sb_info *sbi = EXT4_SB(sb);
4633 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4634 (ext4_has_compat_features(sb) ||
4635 ext4_has_ro_compat_features(sb) ||
4636 ext4_has_incompat_features(sb)))
4637 ext4_msg(sb, KERN_WARNING,
4638 "feature flags set on rev 0 fs, "
4639 "running e2fsck is recommended");
4641 if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4642 set_opt2(sb, HURD_COMPAT);
4643 if (ext4_has_feature_64bit(sb)) {
4644 ext4_msg(sb, KERN_ERR,
4645 "The Hurd can't support 64-bit file systems");
4650 * ea_inode feature uses l_i_version field which is not
4651 * available in HURD_COMPAT mode.
4653 if (ext4_has_feature_ea_inode(sb)) {
4654 ext4_msg(sb, KERN_ERR,
4655 "ea_inode feature is not supported for Hurd");
4660 if (IS_EXT2_SB(sb)) {
4661 if (ext2_feature_set_ok(sb))
4662 ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4663 "using the ext4 subsystem");
4666 * If we're probing be silent, if this looks like
4667 * it's actually an ext[34] filesystem.
4669 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4671 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4672 "to feature incompatibilities");
4677 if (IS_EXT3_SB(sb)) {
4678 if (ext3_feature_set_ok(sb))
4679 ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4680 "using the ext4 subsystem");
4683 * If we're probing be silent, if this looks like
4684 * it's actually an ext4 filesystem.
4686 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4688 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4689 "to feature incompatibilities");
4695 * Check feature flags regardless of the revision level, since we
4696 * previously didn't change the revision level when setting the flags,
4697 * so there is a chance incompat flags are set on a rev 0 filesystem.
4699 if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4702 if (sbi->s_daxdev) {
4703 if (sb->s_blocksize == PAGE_SIZE)
4704 set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4706 ext4_msg(sb, KERN_ERR, "unsupported blocksize for DAX\n");
4709 if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4710 if (ext4_has_feature_inline_data(sb)) {
4711 ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4712 " that may contain inline data");
4715 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
4716 ext4_msg(sb, KERN_ERR,
4717 "DAX unsupported by block device.");
4722 if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
4723 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
4724 es->s_encryption_level);
4731 static int ext4_check_geometry(struct super_block *sb,
4732 struct ext4_super_block *es)
4734 struct ext4_sb_info *sbi = EXT4_SB(sb);
4738 if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (sb->s_blocksize / 4)) {
4739 ext4_msg(sb, KERN_ERR,
4740 "Number of reserved GDT blocks insanely large: %d",
4741 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4745 * Test whether we have more sectors than will fit in sector_t,
4746 * and whether the max offset is addressable by the page cache.
4748 err = generic_check_addressable(sb->s_blocksize_bits,
4749 ext4_blocks_count(es));
4751 ext4_msg(sb, KERN_ERR, "filesystem"
4752 " too large to mount safely on this system");
4756 /* check blocks count against device size */
4757 blocks_count = sb_bdev_nr_blocks(sb);
4758 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4759 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4760 "exceeds size of device (%llu blocks)",
4761 ext4_blocks_count(es), blocks_count);
4766 * It makes no sense for the first data block to be beyond the end
4767 * of the filesystem.
4769 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4770 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4771 "block %u is beyond end of filesystem (%llu)",
4772 le32_to_cpu(es->s_first_data_block),
4773 ext4_blocks_count(es));
4776 if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4777 (sbi->s_cluster_ratio == 1)) {
4778 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4779 "block is 0 with a 1k block and cluster size");
4783 blocks_count = (ext4_blocks_count(es) -
4784 le32_to_cpu(es->s_first_data_block) +
4785 EXT4_BLOCKS_PER_GROUP(sb) - 1);
4786 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4787 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4788 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
4789 "(block count %llu, first data block %u, "
4790 "blocks per group %lu)", blocks_count,
4791 ext4_blocks_count(es),
4792 le32_to_cpu(es->s_first_data_block),
4793 EXT4_BLOCKS_PER_GROUP(sb));
4796 sbi->s_groups_count = blocks_count;
4797 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4798 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
4799 if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4800 le32_to_cpu(es->s_inodes_count)) {
4801 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4802 le32_to_cpu(es->s_inodes_count),
4803 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4810 static int ext4_group_desc_init(struct super_block *sb,
4811 struct ext4_super_block *es,
4812 ext4_fsblk_t logical_sb_block,
4813 ext4_group_t *first_not_zeroed)
4815 struct ext4_sb_info *sbi = EXT4_SB(sb);
4816 unsigned int db_count;
4820 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4821 EXT4_DESC_PER_BLOCK(sb);
4822 if (ext4_has_feature_meta_bg(sb)) {
4823 if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
4824 ext4_msg(sb, KERN_WARNING,
4825 "first meta block group too large: %u "
4826 "(group descriptor block count %u)",
4827 le32_to_cpu(es->s_first_meta_bg), db_count);
4831 rcu_assign_pointer(sbi->s_group_desc,
4832 kvmalloc_array(db_count,
4833 sizeof(struct buffer_head *),
4835 if (sbi->s_group_desc == NULL) {
4836 ext4_msg(sb, KERN_ERR, "not enough memory");
4840 bgl_lock_init(sbi->s_blockgroup_lock);
4842 /* Pre-read the descriptors into the buffer cache */
4843 for (i = 0; i < db_count; i++) {
4844 block = descriptor_loc(sb, logical_sb_block, i);
4845 ext4_sb_breadahead_unmovable(sb, block);
4848 for (i = 0; i < db_count; i++) {
4849 struct buffer_head *bh;
4851 block = descriptor_loc(sb, logical_sb_block, i);
4852 bh = ext4_sb_bread_unmovable(sb, block);
4854 ext4_msg(sb, KERN_ERR,
4855 "can't read group descriptor %d", i);
4856 sbi->s_gdb_count = i;
4860 rcu_dereference(sbi->s_group_desc)[i] = bh;
4863 sbi->s_gdb_count = db_count;
4864 if (!ext4_check_descriptors(sb, logical_sb_block, first_not_zeroed)) {
4865 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
4866 return -EFSCORRUPTED;
4872 static int ext4_load_and_init_journal(struct super_block *sb,
4873 struct ext4_super_block *es,
4874 struct ext4_fs_context *ctx)
4876 struct ext4_sb_info *sbi = EXT4_SB(sb);
4879 err = ext4_load_journal(sb, es, ctx->journal_devnum);
4883 if (ext4_has_feature_64bit(sb) &&
4884 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4885 JBD2_FEATURE_INCOMPAT_64BIT)) {
4886 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4890 if (!set_journal_csum_feature_set(sb)) {
4891 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4896 if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4897 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4898 JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4899 ext4_msg(sb, KERN_ERR,
4900 "Failed to set fast commit journal feature");
4904 /* We have now updated the journal if required, so we can
4905 * validate the data journaling mode. */
4906 switch (test_opt(sb, DATA_FLAGS)) {
4908 /* No mode set, assume a default based on the journal
4909 * capabilities: ORDERED_DATA if the journal can
4910 * cope, else JOURNAL_DATA
4912 if (jbd2_journal_check_available_features
4913 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4914 set_opt(sb, ORDERED_DATA);
4915 sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
4917 set_opt(sb, JOURNAL_DATA);
4918 sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
4922 case EXT4_MOUNT_ORDERED_DATA:
4923 case EXT4_MOUNT_WRITEBACK_DATA:
4924 if (!jbd2_journal_check_available_features
4925 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4926 ext4_msg(sb, KERN_ERR, "Journal does not support "
4927 "requested data journaling mode");
4935 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4936 test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4937 ext4_msg(sb, KERN_ERR, "can't mount with "
4938 "journal_async_commit in data=ordered mode");
4942 set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
4944 sbi->s_journal->j_submit_inode_data_buffers =
4945 ext4_journal_submit_inode_data_buffers;
4946 sbi->s_journal->j_finish_inode_data_buffers =
4947 ext4_journal_finish_inode_data_buffers;
4952 /* flush s_sb_upd_work before destroying the journal. */
4953 flush_work(&sbi->s_sb_upd_work);
4954 jbd2_journal_destroy(sbi->s_journal);
4955 sbi->s_journal = NULL;
4959 static int ext4_check_journal_data_mode(struct super_block *sb)
4961 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4962 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with "
4963 "data=journal disables delayed allocation, "
4964 "dioread_nolock, O_DIRECT and fast_commit support!\n");
4965 /* can't mount with both data=journal and dioread_nolock. */
4966 clear_opt(sb, DIOREAD_NOLOCK);
4967 clear_opt2(sb, JOURNAL_FAST_COMMIT);
4968 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4969 ext4_msg(sb, KERN_ERR, "can't mount with "
4970 "both data=journal and delalloc");
4973 if (test_opt(sb, DAX_ALWAYS)) {
4974 ext4_msg(sb, KERN_ERR, "can't mount with "
4975 "both data=journal and dax");
4978 if (ext4_has_feature_encrypt(sb)) {
4979 ext4_msg(sb, KERN_WARNING,
4980 "encrypted files will use data=ordered "
4981 "instead of data journaling mode");
4983 if (test_opt(sb, DELALLOC))
4984 clear_opt(sb, DELALLOC);
4986 sb->s_iflags |= SB_I_CGROUPWB;
4992 static int ext4_load_super(struct super_block *sb, ext4_fsblk_t *lsb,
4995 struct ext4_sb_info *sbi = EXT4_SB(sb);
4996 struct ext4_super_block *es;
4997 ext4_fsblk_t logical_sb_block;
4998 unsigned long offset = 0;
4999 struct buffer_head *bh;
5003 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
5005 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
5010 * The ext4 superblock will not be buffer aligned for other than 1kB
5011 * block sizes. We need to calculate the offset from buffer start.
5013 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
5014 logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
5015 offset = do_div(logical_sb_block, blocksize);
5017 logical_sb_block = sbi->s_sb_block;
5020 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
5022 ext4_msg(sb, KERN_ERR, "unable to read superblock");
5026 * Note: s_es must be initialized as soon as possible because
5027 * some ext4 macro-instructions depend on its value
5029 es = (struct ext4_super_block *) (bh->b_data + offset);
5031 sb->s_magic = le16_to_cpu(es->s_magic);
5032 if (sb->s_magic != EXT4_SUPER_MAGIC) {
5034 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5038 if (le32_to_cpu(es->s_log_block_size) >
5039 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
5040 ext4_msg(sb, KERN_ERR,
5041 "Invalid log block size: %u",
5042 le32_to_cpu(es->s_log_block_size));
5045 if (le32_to_cpu(es->s_log_cluster_size) >
5046 (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
5047 ext4_msg(sb, KERN_ERR,
5048 "Invalid log cluster size: %u",
5049 le32_to_cpu(es->s_log_cluster_size));
5053 blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
5056 * If the default block size is not the same as the real block size,
5057 * we need to reload it.
5059 if (sb->s_blocksize == blocksize) {
5060 *lsb = logical_sb_block;
5066 * bh must be released before kill_bdev(), otherwise
5067 * it won't be freed and its page also. kill_bdev()
5068 * is called by sb_set_blocksize().
5071 /* Validate the filesystem blocksize */
5072 if (!sb_set_blocksize(sb, blocksize)) {
5073 ext4_msg(sb, KERN_ERR, "bad block size %d",
5079 logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
5080 offset = do_div(logical_sb_block, blocksize);
5081 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
5083 ext4_msg(sb, KERN_ERR, "Can't read superblock on 2nd try");
5088 es = (struct ext4_super_block *)(bh->b_data + offset);
5090 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
5091 ext4_msg(sb, KERN_ERR, "Magic mismatch, very weird!");
5094 *lsb = logical_sb_block;
5102 static void ext4_hash_info_init(struct super_block *sb)
5104 struct ext4_sb_info *sbi = EXT4_SB(sb);
5105 struct ext4_super_block *es = sbi->s_es;
5108 for (i = 0; i < 4; i++)
5109 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
5111 sbi->s_def_hash_version = es->s_def_hash_version;
5112 if (ext4_has_feature_dir_index(sb)) {
5113 i = le32_to_cpu(es->s_flags);
5114 if (i & EXT2_FLAGS_UNSIGNED_HASH)
5115 sbi->s_hash_unsigned = 3;
5116 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
5117 #ifdef __CHAR_UNSIGNED__
5120 cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
5121 sbi->s_hash_unsigned = 3;
5125 cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
5131 static int ext4_block_group_meta_init(struct super_block *sb, int silent)
5133 struct ext4_sb_info *sbi = EXT4_SB(sb);
5134 struct ext4_super_block *es = sbi->s_es;
5137 has_huge_files = ext4_has_feature_huge_file(sb);
5138 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
5140 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
5142 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
5143 if (ext4_has_feature_64bit(sb)) {
5144 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
5145 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
5146 !is_power_of_2(sbi->s_desc_size)) {
5147 ext4_msg(sb, KERN_ERR,
5148 "unsupported descriptor size %lu",
5153 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
5155 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
5156 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
5158 sbi->s_inodes_per_block = sb->s_blocksize / EXT4_INODE_SIZE(sb);
5159 if (sbi->s_inodes_per_block == 0 || sbi->s_blocks_per_group == 0) {
5161 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5164 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
5165 sbi->s_inodes_per_group > sb->s_blocksize * 8) {
5166 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
5167 sbi->s_inodes_per_group);
5170 sbi->s_itb_per_group = sbi->s_inodes_per_group /
5171 sbi->s_inodes_per_block;
5172 sbi->s_desc_per_block = sb->s_blocksize / EXT4_DESC_SIZE(sb);
5173 sbi->s_mount_state = le16_to_cpu(es->s_state) & ~EXT4_FC_REPLAY;
5174 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
5175 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
5180 static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
5182 struct ext4_super_block *es = NULL;
5183 struct ext4_sb_info *sbi = EXT4_SB(sb);
5184 ext4_fsblk_t logical_sb_block;
5188 ext4_group_t first_not_zeroed;
5189 struct ext4_fs_context *ctx = fc->fs_private;
5190 int silent = fc->sb_flags & SB_SILENT;
5192 /* Set defaults for the variables that will be set during parsing */
5193 if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO))
5194 ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5196 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
5197 sbi->s_sectors_written_start =
5198 part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
5200 err = ext4_load_super(sb, &logical_sb_block, silent);
5205 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
5207 err = ext4_init_metadata_csum(sb, es);
5211 ext4_set_def_opts(sb, es);
5213 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
5214 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
5215 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
5216 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
5217 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
5220 * set default s_li_wait_mult for lazyinit, for the case there is
5221 * no mount option specified.
5223 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
5225 err = ext4_inode_info_init(sb, es);
5229 err = parse_apply_sb_mount_options(sb, ctx);
5233 sbi->s_def_mount_opt = sbi->s_mount_opt;
5234 sbi->s_def_mount_opt2 = sbi->s_mount_opt2;
5236 err = ext4_check_opt_consistency(fc, sb);
5240 ext4_apply_options(fc, sb);
5242 err = ext4_encoding_init(sb, es);
5246 err = ext4_check_journal_data_mode(sb);
5250 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5251 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5253 /* i_version is always enabled now */
5254 sb->s_flags |= SB_I_VERSION;
5256 err = ext4_check_feature_compatibility(sb, es, silent);
5260 err = ext4_block_group_meta_init(sb, silent);
5264 ext4_hash_info_init(sb);
5266 err = ext4_handle_clustersize(sb);
5270 err = ext4_check_geometry(sb, es);
5274 timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
5275 spin_lock_init(&sbi->s_error_lock);
5276 INIT_WORK(&sbi->s_sb_upd_work, update_super_work);
5278 err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed);
5282 err = ext4_es_register_shrinker(sbi);
5286 sbi->s_stripe = ext4_get_stripe_size(sbi);
5288 * It's hard to get stripe aligned blocks if stripe is not aligned with
5289 * cluster, just disable stripe and alert user to simpfy code and avoid
5290 * stripe aligned allocation which will rarely successes.
5292 if (sbi->s_stripe > 0 && sbi->s_cluster_ratio > 1 &&
5293 sbi->s_stripe % sbi->s_cluster_ratio != 0) {
5294 ext4_msg(sb, KERN_WARNING,
5295 "stripe (%lu) is not aligned with cluster size (%u), "
5296 "stripe is disabled",
5297 sbi->s_stripe, sbi->s_cluster_ratio);
5300 sbi->s_extent_max_zeroout_kb = 32;
5303 * set up enough so that it can read an inode
5305 sb->s_op = &ext4_sops;
5306 sb->s_export_op = &ext4_export_ops;
5307 sb->s_xattr = ext4_xattr_handlers;
5308 #ifdef CONFIG_FS_ENCRYPTION
5309 sb->s_cop = &ext4_cryptops;
5311 #ifdef CONFIG_FS_VERITY
5312 sb->s_vop = &ext4_verityops;
5315 sb->dq_op = &ext4_quota_operations;
5316 if (ext4_has_feature_quota(sb))
5317 sb->s_qcop = &dquot_quotactl_sysfile_ops;
5319 sb->s_qcop = &ext4_qctl_operations;
5320 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
5322 super_set_uuid(sb, es->s_uuid, sizeof(es->s_uuid));
5323 super_set_sysfs_name_bdev(sb);
5325 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
5326 mutex_init(&sbi->s_orphan_lock);
5328 ext4_fast_commit_init(sb);
5332 needs_recovery = (es->s_last_orphan != 0 ||
5333 ext4_has_feature_orphan_present(sb) ||
5334 ext4_has_feature_journal_needs_recovery(sb));
5336 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) {
5337 err = ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block));
5339 goto failed_mount3a;
5344 * The first inode we look at is the journal inode. Don't try
5345 * root first: it may be modified in the journal!
5347 if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
5348 err = ext4_load_and_init_journal(sb, es, ctx);
5350 goto failed_mount3a;
5351 } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
5352 ext4_has_feature_journal_needs_recovery(sb)) {
5353 ext4_msg(sb, KERN_ERR, "required journal recovery "
5354 "suppressed and not mounted read-only");
5355 goto failed_mount3a;
5357 /* Nojournal mode, all journal mount options are illegal */
5358 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5359 ext4_msg(sb, KERN_ERR, "can't mount with "
5360 "journal_async_commit, fs mounted w/o journal");
5361 goto failed_mount3a;
5364 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
5365 ext4_msg(sb, KERN_ERR, "can't mount with "
5366 "journal_checksum, fs mounted w/o journal");
5367 goto failed_mount3a;
5369 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
5370 ext4_msg(sb, KERN_ERR, "can't mount with "
5371 "commit=%lu, fs mounted w/o journal",
5372 sbi->s_commit_interval / HZ);
5373 goto failed_mount3a;
5375 if (EXT4_MOUNT_DATA_FLAGS &
5376 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
5377 ext4_msg(sb, KERN_ERR, "can't mount with "
5378 "data=, fs mounted w/o journal");
5379 goto failed_mount3a;
5381 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
5382 clear_opt(sb, JOURNAL_CHECKSUM);
5383 clear_opt(sb, DATA_FLAGS);
5384 clear_opt2(sb, JOURNAL_FAST_COMMIT);
5385 sbi->s_journal = NULL;
5389 if (!test_opt(sb, NO_MBCACHE)) {
5390 sbi->s_ea_block_cache = ext4_xattr_create_cache();
5391 if (!sbi->s_ea_block_cache) {
5392 ext4_msg(sb, KERN_ERR,
5393 "Failed to create ea_block_cache");
5395 goto failed_mount_wq;
5398 if (ext4_has_feature_ea_inode(sb)) {
5399 sbi->s_ea_inode_cache = ext4_xattr_create_cache();
5400 if (!sbi->s_ea_inode_cache) {
5401 ext4_msg(sb, KERN_ERR,
5402 "Failed to create ea_inode_cache");
5404 goto failed_mount_wq;
5410 * Get the # of file system overhead blocks from the
5411 * superblock if present.
5413 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
5414 /* ignore the precalculated value if it is ridiculous */
5415 if (sbi->s_overhead > ext4_blocks_count(es))
5416 sbi->s_overhead = 0;
5418 * If the bigalloc feature is not enabled recalculating the
5419 * overhead doesn't take long, so we might as well just redo
5420 * it to make sure we are using the correct value.
5422 if (!ext4_has_feature_bigalloc(sb))
5423 sbi->s_overhead = 0;
5424 if (sbi->s_overhead == 0) {
5425 err = ext4_calculate_overhead(sb);
5427 goto failed_mount_wq;
5431 * The maximum number of concurrent works can be high and
5432 * concurrency isn't really necessary. Limit it to 1.
5434 EXT4_SB(sb)->rsv_conversion_wq =
5435 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
5436 if (!EXT4_SB(sb)->rsv_conversion_wq) {
5437 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
5443 * The jbd2_journal_load will have done any necessary log recovery,
5444 * so we can safely mount the rest of the filesystem now.
5447 root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
5449 ext4_msg(sb, KERN_ERR, "get root inode failed");
5450 err = PTR_ERR(root);
5454 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
5455 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
5457 err = -EFSCORRUPTED;
5461 generic_set_sb_d_ops(sb);
5462 sb->s_root = d_make_root(root);
5464 ext4_msg(sb, KERN_ERR, "get root dentry failed");
5469 err = ext4_setup_super(sb, es, sb_rdonly(sb));
5470 if (err == -EROFS) {
5471 sb->s_flags |= SB_RDONLY;
5473 goto failed_mount4a;
5475 ext4_set_resv_clusters(sb);
5477 if (test_opt(sb, BLOCK_VALIDITY)) {
5478 err = ext4_setup_system_zone(sb);
5480 ext4_msg(sb, KERN_ERR, "failed to initialize system "
5482 goto failed_mount4a;
5485 ext4_fc_replay_cleanup(sb);
5490 * Enable optimize_scan if number of groups is > threshold. This can be
5491 * turned off by passing "mb_optimize_scan=0". This can also be
5492 * turned on forcefully by passing "mb_optimize_scan=1".
5494 if (!(ctx->spec & EXT4_SPEC_mb_optimize_scan)) {
5495 if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
5496 set_opt2(sb, MB_OPTIMIZE_SCAN);
5498 clear_opt2(sb, MB_OPTIMIZE_SCAN);
5501 err = ext4_mb_init(sb);
5503 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5509 * We can only set up the journal commit callback once
5510 * mballoc is initialized
5513 sbi->s_journal->j_commit_callback =
5514 ext4_journal_commit_callback;
5516 err = ext4_percpu_param_init(sbi);
5520 if (ext4_has_feature_flex_bg(sb))
5521 if (!ext4_fill_flex_info(sb)) {
5522 ext4_msg(sb, KERN_ERR,
5523 "unable to initialize "
5524 "flex_bg meta info!");
5529 err = ext4_register_li_request(sb, first_not_zeroed);
5533 err = ext4_init_orphan_info(sb);
5537 /* Enable quota usage during mount. */
5538 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
5539 err = ext4_enable_quotas(sb);
5543 #endif /* CONFIG_QUOTA */
5546 * Save the original bdev mapping's wb_err value which could be
5547 * used to detect the metadata async write error.
5549 spin_lock_init(&sbi->s_bdev_wb_lock);
5550 errseq_check_and_advance(&sb->s_bdev->bd_mapping->wb_err,
5551 &sbi->s_bdev_wb_err);
5552 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5553 ext4_orphan_cleanup(sb, es);
5554 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
5556 * Update the checksum after updating free space/inode counters and
5557 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
5558 * checksum in the buffer cache until it is written out and
5559 * e2fsprogs programs trying to open a file system immediately
5560 * after it is mounted can fail.
5562 ext4_superblock_csum_set(sb);
5563 if (needs_recovery) {
5564 ext4_msg(sb, KERN_INFO, "recovery complete");
5565 err = ext4_mark_recovery_complete(sb, es);
5570 if (test_opt(sb, DISCARD) && !bdev_max_discard_sectors(sb->s_bdev))
5571 ext4_msg(sb, KERN_WARNING,
5572 "mounting with \"discard\" option, but the device does not support discard");
5574 if (es->s_error_count)
5575 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5577 /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5578 ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5579 ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5580 ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
5581 atomic_set(&sbi->s_warning_count, 0);
5582 atomic_set(&sbi->s_msg_count, 0);
5584 /* Register sysfs after all initializations are complete. */
5585 err = ext4_register_sysfs(sb);
5592 ext4_quotas_off(sb, EXT4_MAXQUOTAS);
5593 failed_mount8: __maybe_unused
5594 ext4_release_orphan_info(sb);
5596 ext4_unregister_li_request(sb);
5598 ext4_mb_release(sb);
5599 ext4_flex_groups_free(sbi);
5600 ext4_percpu_param_destroy(sbi);
5602 ext4_ext_release(sb);
5603 ext4_release_system_zone(sb);
5608 ext4_msg(sb, KERN_ERR, "mount failed");
5609 if (EXT4_SB(sb)->rsv_conversion_wq)
5610 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
5612 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5613 sbi->s_ea_inode_cache = NULL;
5615 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
5616 sbi->s_ea_block_cache = NULL;
5618 if (sbi->s_journal) {
5619 /* flush s_sb_upd_work before journal destroy. */
5620 flush_work(&sbi->s_sb_upd_work);
5621 jbd2_journal_destroy(sbi->s_journal);
5622 sbi->s_journal = NULL;
5625 ext4_es_unregister_shrinker(sbi);
5627 /* flush s_sb_upd_work before sbi destroy */
5628 flush_work(&sbi->s_sb_upd_work);
5629 del_timer_sync(&sbi->s_err_report);
5630 ext4_stop_mmpd(sbi);
5631 ext4_group_desc_free(sbi);
5633 if (sbi->s_chksum_driver)
5634 crypto_free_shash(sbi->s_chksum_driver);
5636 #if IS_ENABLED(CONFIG_UNICODE)
5637 utf8_unload(sb->s_encoding);
5641 for (unsigned int i = 0; i < EXT4_MAXQUOTAS; i++)
5642 kfree(get_qf_name(sb, sbi, i));
5644 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5646 if (sbi->s_journal_bdev_file) {
5647 invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
5648 bdev_fput(sbi->s_journal_bdev_file);
5651 invalidate_bdev(sb->s_bdev);
5652 sb->s_fs_info = NULL;
5656 static int ext4_fill_super(struct super_block *sb, struct fs_context *fc)
5658 struct ext4_fs_context *ctx = fc->fs_private;
5659 struct ext4_sb_info *sbi;
5663 sbi = ext4_alloc_sbi(sb);
5667 fc->s_fs_info = sbi;
5669 /* Cleanup superblock name */
5670 strreplace(sb->s_id, '/', '!');
5672 sbi->s_sb_block = 1; /* Default super block location */
5673 if (ctx->spec & EXT4_SPEC_s_sb_block)
5674 sbi->s_sb_block = ctx->s_sb_block;
5676 ret = __ext4_fill_super(fc, sb);
5680 if (sbi->s_journal) {
5681 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
5682 descr = " journalled data mode";
5683 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
5684 descr = " ordered data mode";
5686 descr = " writeback data mode";
5688 descr = "out journal";
5690 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
5691 ext4_msg(sb, KERN_INFO, "mounted filesystem %pU %s with%s. "
5692 "Quota mode: %s.", &sb->s_uuid,
5693 sb_rdonly(sb) ? "ro" : "r/w", descr,
5694 ext4_quota_mode(sb));
5696 /* Update the s_overhead_clusters if necessary */
5697 ext4_update_overhead(sb, false);
5702 fc->s_fs_info = NULL;
5706 static int ext4_get_tree(struct fs_context *fc)
5708 return get_tree_bdev(fc, ext4_fill_super);
5712 * Setup any per-fs journal parameters now. We'll do this both on
5713 * initial mount, once the journal has been initialised but before we've
5714 * done any recovery; and again on any subsequent remount.
5716 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5718 struct ext4_sb_info *sbi = EXT4_SB(sb);
5720 journal->j_commit_interval = sbi->s_commit_interval;
5721 journal->j_min_batch_time = sbi->s_min_batch_time;
5722 journal->j_max_batch_time = sbi->s_max_batch_time;
5723 ext4_fc_init(sb, journal);
5725 write_lock(&journal->j_state_lock);
5726 if (test_opt(sb, BARRIER))
5727 journal->j_flags |= JBD2_BARRIER;
5729 journal->j_flags &= ~JBD2_BARRIER;
5730 if (test_opt(sb, DATA_ERR_ABORT))
5731 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
5733 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5735 * Always enable journal cycle record option, letting the journal
5736 * records log transactions continuously between each mount.
5738 journal->j_flags |= JBD2_CYCLE_RECORD;
5739 write_unlock(&journal->j_state_lock);
5742 static struct inode *ext4_get_journal_inode(struct super_block *sb,
5743 unsigned int journal_inum)
5745 struct inode *journal_inode;
5748 * Test for the existence of a valid inode on disk. Bad things
5749 * happen if we iget() an unused inode, as the subsequent iput()
5750 * will try to delete it.
5752 journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
5753 if (IS_ERR(journal_inode)) {
5754 ext4_msg(sb, KERN_ERR, "no journal found");
5755 return ERR_CAST(journal_inode);
5757 if (!journal_inode->i_nlink) {
5758 make_bad_inode(journal_inode);
5759 iput(journal_inode);
5760 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5761 return ERR_PTR(-EFSCORRUPTED);
5763 if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
5764 ext4_msg(sb, KERN_ERR, "invalid journal inode");
5765 iput(journal_inode);
5766 return ERR_PTR(-EFSCORRUPTED);
5769 ext4_debug("Journal inode found at %p: %lld bytes\n",
5770 journal_inode, journal_inode->i_size);
5771 return journal_inode;
5774 static int ext4_journal_bmap(journal_t *journal, sector_t *block)
5776 struct ext4_map_blocks map;
5779 if (journal->j_inode == NULL)
5782 map.m_lblk = *block;
5784 ret = ext4_map_blocks(NULL, journal->j_inode, &map, 0);
5786 ext4_msg(journal->j_inode->i_sb, KERN_CRIT,
5787 "journal bmap failed: block %llu ret %d\n",
5789 jbd2_journal_abort(journal, ret ? ret : -EIO);
5792 *block = map.m_pblk;
5796 static journal_t *ext4_open_inode_journal(struct super_block *sb,
5797 unsigned int journal_inum)
5799 struct inode *journal_inode;
5802 journal_inode = ext4_get_journal_inode(sb, journal_inum);
5803 if (IS_ERR(journal_inode))
5804 return ERR_CAST(journal_inode);
5806 journal = jbd2_journal_init_inode(journal_inode);
5807 if (IS_ERR(journal)) {
5808 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5809 iput(journal_inode);
5810 return ERR_CAST(journal);
5812 journal->j_private = sb;
5813 journal->j_bmap = ext4_journal_bmap;
5814 ext4_init_journal_params(sb, journal);
5818 static struct file *ext4_get_journal_blkdev(struct super_block *sb,
5819 dev_t j_dev, ext4_fsblk_t *j_start,
5820 ext4_fsblk_t *j_len)
5822 struct buffer_head *bh;
5823 struct block_device *bdev;
5824 struct file *bdev_file;
5825 int hblock, blocksize;
5826 ext4_fsblk_t sb_block;
5827 unsigned long offset;
5828 struct ext4_super_block *es;
5831 bdev_file = bdev_file_open_by_dev(j_dev,
5832 BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES,
5833 sb, &fs_holder_ops);
5834 if (IS_ERR(bdev_file)) {
5835 ext4_msg(sb, KERN_ERR,
5836 "failed to open journal device unknown-block(%u,%u) %ld",
5837 MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev_file));
5841 bdev = file_bdev(bdev_file);
5842 blocksize = sb->s_blocksize;
5843 hblock = bdev_logical_block_size(bdev);
5844 if (blocksize < hblock) {
5845 ext4_msg(sb, KERN_ERR,
5846 "blocksize too small for journal device");
5851 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5852 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5853 set_blocksize(bdev_file, blocksize);
5854 bh = __bread(bdev, sb_block, blocksize);
5856 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5857 "external journal");
5862 es = (struct ext4_super_block *) (bh->b_data + offset);
5863 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5864 !(le32_to_cpu(es->s_feature_incompat) &
5865 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5866 ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
5867 errno = -EFSCORRUPTED;
5871 if ((le32_to_cpu(es->s_feature_ro_compat) &
5872 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5873 es->s_checksum != ext4_superblock_csum(sb, es)) {
5874 ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
5875 errno = -EFSCORRUPTED;
5879 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5880 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5881 errno = -EFSCORRUPTED;
5885 *j_start = sb_block + 1;
5886 *j_len = ext4_blocks_count(es);
5893 bdev_fput(bdev_file);
5894 return ERR_PTR(errno);
5897 static journal_t *ext4_open_dev_journal(struct super_block *sb,
5901 ext4_fsblk_t j_start;
5903 struct file *bdev_file;
5906 bdev_file = ext4_get_journal_blkdev(sb, j_dev, &j_start, &j_len);
5907 if (IS_ERR(bdev_file))
5908 return ERR_CAST(bdev_file);
5910 journal = jbd2_journal_init_dev(file_bdev(bdev_file), sb->s_bdev, j_start,
5911 j_len, sb->s_blocksize);
5912 if (IS_ERR(journal)) {
5913 ext4_msg(sb, KERN_ERR, "failed to create device journal");
5914 errno = PTR_ERR(journal);
5917 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5918 ext4_msg(sb, KERN_ERR, "External journal has more than one "
5919 "user (unsupported) - %d",
5920 be32_to_cpu(journal->j_superblock->s_nr_users));
5924 journal->j_private = sb;
5925 EXT4_SB(sb)->s_journal_bdev_file = bdev_file;
5926 ext4_init_journal_params(sb, journal);
5930 jbd2_journal_destroy(journal);
5932 bdev_fput(bdev_file);
5933 return ERR_PTR(errno);
5936 static int ext4_load_journal(struct super_block *sb,
5937 struct ext4_super_block *es,
5938 unsigned long journal_devnum)
5941 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5944 int really_read_only;
5947 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5948 return -EFSCORRUPTED;
5950 if (journal_devnum &&
5951 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5952 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5953 "numbers have changed");
5954 journal_dev = new_decode_dev(journal_devnum);
5956 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5958 if (journal_inum && journal_dev) {
5959 ext4_msg(sb, KERN_ERR,
5960 "filesystem has both journal inode and journal device!");
5965 journal = ext4_open_inode_journal(sb, journal_inum);
5966 if (IS_ERR(journal))
5967 return PTR_ERR(journal);
5969 journal = ext4_open_dev_journal(sb, journal_dev);
5970 if (IS_ERR(journal))
5971 return PTR_ERR(journal);
5974 journal_dev_ro = bdev_read_only(journal->j_dev);
5975 really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5977 if (journal_dev_ro && !sb_rdonly(sb)) {
5978 ext4_msg(sb, KERN_ERR,
5979 "journal device read-only, try mounting with '-o ro'");
5985 * Are we loading a blank journal or performing recovery after a
5986 * crash? For recovery, we need to check in advance whether we
5987 * can get read-write access to the device.
5989 if (ext4_has_feature_journal_needs_recovery(sb)) {
5990 if (sb_rdonly(sb)) {
5991 ext4_msg(sb, KERN_INFO, "INFO: recovery "
5992 "required on readonly filesystem");
5993 if (really_read_only) {
5994 ext4_msg(sb, KERN_ERR, "write access "
5995 "unavailable, cannot proceed "
5996 "(try mounting with noload)");
6000 ext4_msg(sb, KERN_INFO, "write access will "
6001 "be enabled during recovery");
6005 if (!(journal->j_flags & JBD2_BARRIER))
6006 ext4_msg(sb, KERN_INFO, "barriers disabled");
6008 if (!ext4_has_feature_journal_needs_recovery(sb))
6009 err = jbd2_journal_wipe(journal, !really_read_only);
6011 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
6013 bool changed = false;
6016 memcpy(save, ((char *) es) +
6017 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
6018 err = jbd2_journal_load(journal);
6019 if (save && memcmp(((char *) es) + EXT4_S_ERR_START,
6020 save, EXT4_S_ERR_LEN)) {
6021 memcpy(((char *) es) + EXT4_S_ERR_START,
6022 save, EXT4_S_ERR_LEN);
6026 orig_state = es->s_state;
6027 es->s_state |= cpu_to_le16(EXT4_SB(sb)->s_mount_state &
6029 if (orig_state != es->s_state)
6031 /* Write out restored error information to the superblock */
6032 if (changed && !really_read_only) {
6034 err2 = ext4_commit_super(sb);
6040 ext4_msg(sb, KERN_ERR, "error loading journal");
6044 EXT4_SB(sb)->s_journal = journal;
6045 err = ext4_clear_journal_err(sb, es);
6047 EXT4_SB(sb)->s_journal = NULL;
6048 jbd2_journal_destroy(journal);
6052 if (!really_read_only && journal_devnum &&
6053 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
6054 es->s_journal_dev = cpu_to_le32(journal_devnum);
6055 ext4_commit_super(sb);
6057 if (!really_read_only && journal_inum &&
6058 journal_inum != le32_to_cpu(es->s_journal_inum)) {
6059 es->s_journal_inum = cpu_to_le32(journal_inum);
6060 ext4_commit_super(sb);
6066 jbd2_journal_destroy(journal);
6070 /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
6071 static void ext4_update_super(struct super_block *sb)
6073 struct ext4_sb_info *sbi = EXT4_SB(sb);
6074 struct ext4_super_block *es = sbi->s_es;
6075 struct buffer_head *sbh = sbi->s_sbh;
6079 * If the file system is mounted read-only, don't update the
6080 * superblock write time. This avoids updating the superblock
6081 * write time when we are mounting the root file system
6082 * read/only but we need to replay the journal; at that point,
6083 * for people who are east of GMT and who make their clock
6084 * tick in localtime for Windows bug-for-bug compatibility,
6085 * the clock is set in the future, and this will cause e2fsck
6086 * to complain and force a full file system check.
6089 ext4_update_tstamp(es, s_wtime);
6090 es->s_kbytes_written =
6091 cpu_to_le64(sbi->s_kbytes_written +
6092 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
6093 sbi->s_sectors_written_start) >> 1));
6094 if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
6095 ext4_free_blocks_count_set(es,
6096 EXT4_C2B(sbi, percpu_counter_sum_positive(
6097 &sbi->s_freeclusters_counter)));
6098 if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
6099 es->s_free_inodes_count =
6100 cpu_to_le32(percpu_counter_sum_positive(
6101 &sbi->s_freeinodes_counter));
6102 /* Copy error information to the on-disk superblock */
6103 spin_lock(&sbi->s_error_lock);
6104 if (sbi->s_add_error_count > 0) {
6105 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
6106 if (!es->s_first_error_time && !es->s_first_error_time_hi) {
6107 __ext4_update_tstamp(&es->s_first_error_time,
6108 &es->s_first_error_time_hi,
6109 sbi->s_first_error_time);
6110 strtomem_pad(es->s_first_error_func,
6111 sbi->s_first_error_func, 0);
6112 es->s_first_error_line =
6113 cpu_to_le32(sbi->s_first_error_line);
6114 es->s_first_error_ino =
6115 cpu_to_le32(sbi->s_first_error_ino);
6116 es->s_first_error_block =
6117 cpu_to_le64(sbi->s_first_error_block);
6118 es->s_first_error_errcode =
6119 ext4_errno_to_code(sbi->s_first_error_code);
6121 __ext4_update_tstamp(&es->s_last_error_time,
6122 &es->s_last_error_time_hi,
6123 sbi->s_last_error_time);
6124 strtomem_pad(es->s_last_error_func, sbi->s_last_error_func, 0);
6125 es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
6126 es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
6127 es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
6128 es->s_last_error_errcode =
6129 ext4_errno_to_code(sbi->s_last_error_code);
6131 * Start the daily error reporting function if it hasn't been
6134 if (!es->s_error_count)
6135 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
6136 le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
6137 sbi->s_add_error_count = 0;
6139 spin_unlock(&sbi->s_error_lock);
6141 ext4_superblock_csum_set(sb);
6145 static int ext4_commit_super(struct super_block *sb)
6147 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
6152 ext4_update_super(sb);
6155 /* Buffer got discarded which means block device got invalidated */
6156 if (!buffer_mapped(sbh)) {
6161 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
6163 * Oh, dear. A previous attempt to write the
6164 * superblock failed. This could happen because the
6165 * USB device was yanked out. Or it could happen to
6166 * be a transient write error and maybe the block will
6167 * be remapped. Nothing we can do but to retry the
6168 * write and hope for the best.
6170 ext4_msg(sb, KERN_ERR, "previous I/O error to "
6171 "superblock detected");
6172 clear_buffer_write_io_error(sbh);
6173 set_buffer_uptodate(sbh);
6176 /* Clear potential dirty bit if it was journalled update */
6177 clear_buffer_dirty(sbh);
6178 sbh->b_end_io = end_buffer_write_sync;
6179 submit_bh(REQ_OP_WRITE | REQ_SYNC |
6180 (test_opt(sb, BARRIER) ? REQ_FUA : 0), sbh);
6181 wait_on_buffer(sbh);
6182 if (buffer_write_io_error(sbh)) {
6183 ext4_msg(sb, KERN_ERR, "I/O error while writing "
6185 clear_buffer_write_io_error(sbh);
6186 set_buffer_uptodate(sbh);
6193 * Have we just finished recovery? If so, and if we are mounting (or
6194 * remounting) the filesystem readonly, then we will end up with a
6195 * consistent fs on disk. Record that fact.
6197 static int ext4_mark_recovery_complete(struct super_block *sb,
6198 struct ext4_super_block *es)
6201 journal_t *journal = EXT4_SB(sb)->s_journal;
6203 if (!ext4_has_feature_journal(sb)) {
6204 if (journal != NULL) {
6205 ext4_error(sb, "Journal got removed while the fs was "
6207 return -EFSCORRUPTED;
6211 jbd2_journal_lock_updates(journal);
6212 err = jbd2_journal_flush(journal, 0);
6216 if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
6217 ext4_has_feature_orphan_present(sb))) {
6218 if (!ext4_orphan_file_empty(sb)) {
6219 ext4_error(sb, "Orphan file not empty on read-only fs.");
6220 err = -EFSCORRUPTED;
6223 ext4_clear_feature_journal_needs_recovery(sb);
6224 ext4_clear_feature_orphan_present(sb);
6225 ext4_commit_super(sb);
6228 jbd2_journal_unlock_updates(journal);
6233 * If we are mounting (or read-write remounting) a filesystem whose journal
6234 * has recorded an error from a previous lifetime, move that error to the
6235 * main filesystem now.
6237 static int ext4_clear_journal_err(struct super_block *sb,
6238 struct ext4_super_block *es)
6244 if (!ext4_has_feature_journal(sb)) {
6245 ext4_error(sb, "Journal got removed while the fs was mounted!");
6246 return -EFSCORRUPTED;
6249 journal = EXT4_SB(sb)->s_journal;
6252 * Now check for any error status which may have been recorded in the
6253 * journal by a prior ext4_error() or ext4_abort()
6256 j_errno = jbd2_journal_errno(journal);
6260 errstr = ext4_decode_error(sb, j_errno, nbuf);
6261 ext4_warning(sb, "Filesystem error recorded "
6262 "from previous mount: %s", errstr);
6264 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
6265 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
6266 j_errno = ext4_commit_super(sb);
6269 ext4_warning(sb, "Marked fs in need of filesystem check.");
6271 jbd2_journal_clear_err(journal);
6272 jbd2_journal_update_sb_errno(journal);
6278 * Force the running and committing transactions to commit,
6279 * and wait on the commit.
6281 int ext4_force_commit(struct super_block *sb)
6283 return ext4_journal_force_commit(EXT4_SB(sb)->s_journal);
6286 static int ext4_sync_fs(struct super_block *sb, int wait)
6290 bool needs_barrier = false;
6291 struct ext4_sb_info *sbi = EXT4_SB(sb);
6293 if (unlikely(ext4_forced_shutdown(sb)))
6296 trace_ext4_sync_fs(sb, wait);
6297 flush_workqueue(sbi->rsv_conversion_wq);
6299 * Writeback quota in non-journalled quota case - journalled quota has
6302 dquot_writeback_dquots(sb, -1);
6304 * Data writeback is possible w/o journal transaction, so barrier must
6305 * being sent at the end of the function. But we can skip it if
6306 * transaction_commit will do it for us.
6308 if (sbi->s_journal) {
6309 target = jbd2_get_latest_transaction(sbi->s_journal);
6310 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
6311 !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
6312 needs_barrier = true;
6314 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
6316 ret = jbd2_log_wait_commit(sbi->s_journal,
6319 } else if (wait && test_opt(sb, BARRIER))
6320 needs_barrier = true;
6321 if (needs_barrier) {
6323 err = blkdev_issue_flush(sb->s_bdev);
6332 * LVM calls this function before a (read-only) snapshot is created. This
6333 * gives us a chance to flush the journal completely and mark the fs clean.
6335 * Note that only this function cannot bring a filesystem to be in a clean
6336 * state independently. It relies on upper layer to stop all data & metadata
6339 static int ext4_freeze(struct super_block *sb)
6342 journal_t *journal = EXT4_SB(sb)->s_journal;
6345 /* Now we set up the journal barrier. */
6346 jbd2_journal_lock_updates(journal);
6349 * Don't clear the needs_recovery flag if we failed to
6350 * flush the journal.
6352 error = jbd2_journal_flush(journal, 0);
6356 /* Journal blocked and flushed, clear needs_recovery flag. */
6357 ext4_clear_feature_journal_needs_recovery(sb);
6358 if (ext4_orphan_file_empty(sb))
6359 ext4_clear_feature_orphan_present(sb);
6362 error = ext4_commit_super(sb);
6365 /* we rely on upper layer to stop further updates */
6366 jbd2_journal_unlock_updates(journal);
6371 * Called by LVM after the snapshot is done. We need to reset the RECOVER
6372 * flag here, even though the filesystem is not technically dirty yet.
6374 static int ext4_unfreeze(struct super_block *sb)
6376 if (ext4_forced_shutdown(sb))
6379 if (EXT4_SB(sb)->s_journal) {
6380 /* Reset the needs_recovery flag before the fs is unlocked. */
6381 ext4_set_feature_journal_needs_recovery(sb);
6382 if (ext4_has_feature_orphan_file(sb))
6383 ext4_set_feature_orphan_present(sb);
6386 ext4_commit_super(sb);
6391 * Structure to save mount options for ext4_remount's benefit
6393 struct ext4_mount_options {
6394 unsigned long s_mount_opt;
6395 unsigned long s_mount_opt2;
6398 unsigned long s_commit_interval;
6399 u32 s_min_batch_time, s_max_batch_time;
6402 char *s_qf_names[EXT4_MAXQUOTAS];
6406 static int __ext4_remount(struct fs_context *fc, struct super_block *sb)
6408 struct ext4_fs_context *ctx = fc->fs_private;
6409 struct ext4_super_block *es;
6410 struct ext4_sb_info *sbi = EXT4_SB(sb);
6411 unsigned long old_sb_flags;
6412 struct ext4_mount_options old_opts;
6417 int enable_quota = 0;
6419 char *to_free[EXT4_MAXQUOTAS];
6423 /* Store the original options */
6424 old_sb_flags = sb->s_flags;
6425 old_opts.s_mount_opt = sbi->s_mount_opt;
6426 old_opts.s_mount_opt2 = sbi->s_mount_opt2;
6427 old_opts.s_resuid = sbi->s_resuid;
6428 old_opts.s_resgid = sbi->s_resgid;
6429 old_opts.s_commit_interval = sbi->s_commit_interval;
6430 old_opts.s_min_batch_time = sbi->s_min_batch_time;
6431 old_opts.s_max_batch_time = sbi->s_max_batch_time;
6433 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
6434 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6435 if (sbi->s_qf_names[i]) {
6436 char *qf_name = get_qf_name(sb, sbi, i);
6438 old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
6439 if (!old_opts.s_qf_names[i]) {
6440 for (j = 0; j < i; j++)
6441 kfree(old_opts.s_qf_names[j]);
6445 old_opts.s_qf_names[i] = NULL;
6447 if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)) {
6448 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
6449 ctx->journal_ioprio =
6450 sbi->s_journal->j_task->io_context->ioprio;
6452 ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
6457 * Changing the DIOREAD_NOLOCK or DELALLOC mount options may cause
6458 * two calls to ext4_should_dioread_nolock() to return inconsistent
6459 * values, triggering WARN_ON in ext4_add_complete_io(). we grab
6460 * here s_writepages_rwsem to avoid race between writepages ops and
6463 alloc_ctx = ext4_writepages_down_write(sb);
6464 ext4_apply_options(fc, sb);
6465 ext4_writepages_up_write(sb, alloc_ctx);
6467 if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
6468 test_opt(sb, JOURNAL_CHECKSUM)) {
6469 ext4_msg(sb, KERN_ERR, "changing journal_checksum "
6470 "during remount not supported; ignoring");
6471 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
6474 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
6475 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
6476 ext4_msg(sb, KERN_ERR, "can't mount with "
6477 "both data=journal and delalloc");
6481 if (test_opt(sb, DIOREAD_NOLOCK)) {
6482 ext4_msg(sb, KERN_ERR, "can't mount with "
6483 "both data=journal and dioread_nolock");
6487 } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
6488 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
6489 ext4_msg(sb, KERN_ERR, "can't mount with "
6490 "journal_async_commit in data=ordered mode");
6496 if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
6497 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
6502 if (test_opt2(sb, ABORT))
6503 ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
6505 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
6506 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
6510 if (sbi->s_journal) {
6511 ext4_init_journal_params(sb, sbi->s_journal);
6512 set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
6515 /* Flush outstanding errors before changing fs state */
6516 flush_work(&sbi->s_sb_upd_work);
6518 if ((bool)(fc->sb_flags & SB_RDONLY) != sb_rdonly(sb)) {
6519 if (ext4_forced_shutdown(sb)) {
6524 if (fc->sb_flags & SB_RDONLY) {
6525 err = sync_filesystem(sb);
6528 err = dquot_suspend(sb, -1);
6533 * First of all, the unconditional stuff we have to do
6534 * to disable replay of the journal when we next remount
6536 sb->s_flags |= SB_RDONLY;
6539 * OK, test if we are remounting a valid rw partition
6540 * readonly, and if so set the rdonly flag and then
6541 * mark the partition as valid again.
6543 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
6544 (sbi->s_mount_state & EXT4_VALID_FS))
6545 es->s_state = cpu_to_le16(sbi->s_mount_state);
6547 if (sbi->s_journal) {
6549 * We let remount-ro finish even if marking fs
6550 * as clean failed...
6552 ext4_mark_recovery_complete(sb, es);
6555 /* Make sure we can mount this feature set readwrite */
6556 if (ext4_has_feature_readonly(sb) ||
6557 !ext4_feature_set_ok(sb, 0)) {
6562 * Make sure the group descriptor checksums
6563 * are sane. If they aren't, refuse to remount r/w.
6565 for (g = 0; g < sbi->s_groups_count; g++) {
6566 struct ext4_group_desc *gdp =
6567 ext4_get_group_desc(sb, g, NULL);
6569 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
6570 ext4_msg(sb, KERN_ERR,
6571 "ext4_remount: Checksum for group %u failed (%u!=%u)",
6572 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
6573 le16_to_cpu(gdp->bg_checksum));
6580 * If we have an unprocessed orphan list hanging
6581 * around from a previously readonly bdev mount,
6582 * require a full umount/remount for now.
6584 if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
6585 ext4_msg(sb, KERN_WARNING, "Couldn't "
6586 "remount RDWR because of unprocessed "
6587 "orphan inode list. Please "
6588 "umount/remount instead");
6594 * Mounting a RDONLY partition read-write, so reread
6595 * and store the current valid flag. (It may have
6596 * been changed by e2fsck since we originally mounted
6599 if (sbi->s_journal) {
6600 err = ext4_clear_journal_err(sb, es);
6604 sbi->s_mount_state = (le16_to_cpu(es->s_state) &
6607 err = ext4_setup_super(sb, es, 0);
6611 sb->s_flags &= ~SB_RDONLY;
6612 if (ext4_has_feature_mmp(sb)) {
6613 err = ext4_multi_mount_protect(sb,
6614 le64_to_cpu(es->s_mmp_block));
6625 * Handle creation of system zone data early because it can fail.
6626 * Releasing of existing data is done when we are sure remount will
6629 if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
6630 err = ext4_setup_system_zone(sb);
6635 if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
6636 err = ext4_commit_super(sb);
6643 if (sb_any_quota_suspended(sb))
6644 dquot_resume(sb, -1);
6645 else if (ext4_has_feature_quota(sb)) {
6646 err = ext4_enable_quotas(sb);
6651 /* Release old quota file names */
6652 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6653 kfree(old_opts.s_qf_names[i]);
6655 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
6656 ext4_release_system_zone(sb);
6659 * Reinitialize lazy itable initialization thread based on
6662 if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
6663 ext4_unregister_li_request(sb);
6665 ext4_group_t first_not_zeroed;
6666 first_not_zeroed = ext4_has_uninit_itable(sb);
6667 ext4_register_li_request(sb, first_not_zeroed);
6670 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
6671 ext4_stop_mmpd(sbi);
6677 * If there was a failing r/w to ro transition, we may need to
6680 if (sb_rdonly(sb) && !(old_sb_flags & SB_RDONLY) &&
6681 sb_any_quota_suspended(sb))
6682 dquot_resume(sb, -1);
6684 alloc_ctx = ext4_writepages_down_write(sb);
6685 sb->s_flags = old_sb_flags;
6686 sbi->s_mount_opt = old_opts.s_mount_opt;
6687 sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6688 sbi->s_resuid = old_opts.s_resuid;
6689 sbi->s_resgid = old_opts.s_resgid;
6690 sbi->s_commit_interval = old_opts.s_commit_interval;
6691 sbi->s_min_batch_time = old_opts.s_min_batch_time;
6692 sbi->s_max_batch_time = old_opts.s_max_batch_time;
6693 ext4_writepages_up_write(sb, alloc_ctx);
6695 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
6696 ext4_release_system_zone(sb);
6698 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6699 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
6700 to_free[i] = get_qf_name(sb, sbi, i);
6701 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6704 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6707 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
6708 ext4_stop_mmpd(sbi);
6712 static int ext4_reconfigure(struct fs_context *fc)
6714 struct super_block *sb = fc->root->d_sb;
6717 fc->s_fs_info = EXT4_SB(sb);
6719 ret = ext4_check_opt_consistency(fc, sb);
6723 ret = __ext4_remount(fc, sb);
6727 ext4_msg(sb, KERN_INFO, "re-mounted %pU %s. Quota mode: %s.",
6728 &sb->s_uuid, sb_rdonly(sb) ? "ro" : "r/w",
6729 ext4_quota_mode(sb));
6735 static int ext4_statfs_project(struct super_block *sb,
6736 kprojid_t projid, struct kstatfs *buf)
6739 struct dquot *dquot;
6743 qid = make_kqid_projid(projid);
6744 dquot = dqget(sb, qid);
6746 return PTR_ERR(dquot);
6747 spin_lock(&dquot->dq_dqb_lock);
6749 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6750 dquot->dq_dqb.dqb_bhardlimit);
6751 limit >>= sb->s_blocksize_bits;
6753 if (limit && buf->f_blocks > limit) {
6754 curblock = (dquot->dq_dqb.dqb_curspace +
6755 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6756 buf->f_blocks = limit;
6757 buf->f_bfree = buf->f_bavail =
6758 (buf->f_blocks > curblock) ?
6759 (buf->f_blocks - curblock) : 0;
6762 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6763 dquot->dq_dqb.dqb_ihardlimit);
6764 if (limit && buf->f_files > limit) {
6765 buf->f_files = limit;
6767 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6768 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6771 spin_unlock(&dquot->dq_dqb_lock);
6777 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6779 struct super_block *sb = dentry->d_sb;
6780 struct ext4_sb_info *sbi = EXT4_SB(sb);
6781 struct ext4_super_block *es = sbi->s_es;
6782 ext4_fsblk_t overhead = 0, resv_blocks;
6784 resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6786 if (!test_opt(sb, MINIX_DF))
6787 overhead = sbi->s_overhead;
6789 buf->f_type = EXT4_SUPER_MAGIC;
6790 buf->f_bsize = sb->s_blocksize;
6791 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
6792 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
6793 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6794 /* prevent underflow in case that few free space is available */
6795 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
6796 buf->f_bavail = buf->f_bfree -
6797 (ext4_r_blocks_count(es) + resv_blocks);
6798 if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6800 buf->f_files = le32_to_cpu(es->s_inodes_count);
6801 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6802 buf->f_namelen = EXT4_NAME_LEN;
6803 buf->f_fsid = uuid_to_fsid(es->s_uuid);
6806 if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6807 sb_has_quota_limits_enabled(sb, PRJQUOTA))
6808 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6817 * Helper functions so that transaction is started before we acquire dqio_sem
6818 * to keep correct lock ordering of transaction > dqio_sem
6820 static inline struct inode *dquot_to_inode(struct dquot *dquot)
6822 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6825 static int ext4_write_dquot(struct dquot *dquot)
6829 struct inode *inode;
6831 inode = dquot_to_inode(dquot);
6832 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6833 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6835 return PTR_ERR(handle);
6836 ret = dquot_commit(dquot);
6838 ext4_error_err(dquot->dq_sb, -ret,
6839 "Failed to commit dquot type %d",
6841 err = ext4_journal_stop(handle);
6847 static int ext4_acquire_dquot(struct dquot *dquot)
6852 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6853 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6855 return PTR_ERR(handle);
6856 ret = dquot_acquire(dquot);
6858 ext4_error_err(dquot->dq_sb, -ret,
6859 "Failed to acquire dquot type %d",
6861 err = ext4_journal_stop(handle);
6867 static int ext4_release_dquot(struct dquot *dquot)
6872 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6873 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
6874 if (IS_ERR(handle)) {
6875 /* Release dquot anyway to avoid endless cycle in dqput() */
6876 dquot_release(dquot);
6877 return PTR_ERR(handle);
6879 ret = dquot_release(dquot);
6881 ext4_error_err(dquot->dq_sb, -ret,
6882 "Failed to release dquot type %d",
6884 err = ext4_journal_stop(handle);
6890 static int ext4_mark_dquot_dirty(struct dquot *dquot)
6892 struct super_block *sb = dquot->dq_sb;
6894 if (ext4_is_quota_journalled(sb)) {
6895 dquot_mark_dquot_dirty(dquot);
6896 return ext4_write_dquot(dquot);
6898 return dquot_mark_dquot_dirty(dquot);
6902 static int ext4_write_info(struct super_block *sb, int type)
6907 /* Data block + inode block */
6908 handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
6910 return PTR_ERR(handle);
6911 ret = dquot_commit_info(sb, type);
6912 err = ext4_journal_stop(handle);
6918 static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6920 struct ext4_inode_info *ei = EXT4_I(inode);
6922 /* The first argument of lockdep_set_subclass has to be
6923 * *exactly* the same as the argument to init_rwsem() --- in
6924 * this case, in init_once() --- or lockdep gets unhappy
6925 * because the name of the lock is set using the
6926 * stringification of the argument to init_rwsem().
6928 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
6929 lockdep_set_subclass(&ei->i_data_sem, subclass);
6933 * Standard function to be called on quota_on
6935 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
6936 const struct path *path)
6940 if (!test_opt(sb, QUOTA))
6943 /* Quotafile not on the same filesystem? */
6944 if (path->dentry->d_sb != sb)
6947 /* Quota already enabled for this file? */
6948 if (IS_NOQUOTA(d_inode(path->dentry)))
6951 /* Journaling quota? */
6952 if (EXT4_SB(sb)->s_qf_names[type]) {
6953 /* Quotafile not in fs root? */
6954 if (path->dentry->d_parent != sb->s_root)
6955 ext4_msg(sb, KERN_WARNING,
6956 "Quota file not on filesystem root. "
6957 "Journaled quota will not work");
6958 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
6961 * Clear the flag just in case mount options changed since
6964 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
6967 lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6968 err = dquot_quota_on(sb, type, format_id, path);
6970 struct inode *inode = d_inode(path->dentry);
6974 * Set inode flags to prevent userspace from messing with quota
6975 * files. If this fails, we return success anyway since quotas
6976 * are already enabled and this is not a hard failure.
6979 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6982 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6983 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6984 S_NOATIME | S_IMMUTABLE);
6985 err = ext4_mark_inode_dirty(handle, inode);
6986 ext4_journal_stop(handle);
6988 inode_unlock(inode);
6990 dquot_quota_off(sb, type);
6993 lockdep_set_quota_inode(path->dentry->d_inode,
6998 static inline bool ext4_check_quota_inum(int type, unsigned long qf_inum)
7002 return qf_inum == EXT4_USR_QUOTA_INO;
7004 return qf_inum == EXT4_GRP_QUOTA_INO;
7006 return qf_inum >= EXT4_GOOD_OLD_FIRST_INO;
7012 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
7016 struct inode *qf_inode;
7017 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
7018 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
7019 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
7020 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
7023 BUG_ON(!ext4_has_feature_quota(sb));
7025 if (!qf_inums[type])
7028 if (!ext4_check_quota_inum(type, qf_inums[type])) {
7029 ext4_error(sb, "Bad quota inum: %lu, type: %d",
7030 qf_inums[type], type);
7034 qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
7035 if (IS_ERR(qf_inode)) {
7036 ext4_error(sb, "Bad quota inode: %lu, type: %d",
7037 qf_inums[type], type);
7038 return PTR_ERR(qf_inode);
7041 /* Don't account quota for quota files to avoid recursion */
7042 qf_inode->i_flags |= S_NOQUOTA;
7043 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
7044 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
7046 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
7052 /* Enable usage tracking for all quota types. */
7053 int ext4_enable_quotas(struct super_block *sb)
7056 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
7057 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
7058 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
7059 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
7061 bool quota_mopt[EXT4_MAXQUOTAS] = {
7062 test_opt(sb, USRQUOTA),
7063 test_opt(sb, GRPQUOTA),
7064 test_opt(sb, PRJQUOTA),
7067 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
7068 for (type = 0; type < EXT4_MAXQUOTAS; type++) {
7069 if (qf_inums[type]) {
7070 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
7071 DQUOT_USAGE_ENABLED |
7072 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
7075 "Failed to enable quota tracking "
7076 "(type=%d, err=%d, ino=%lu). "
7077 "Please run e2fsck to fix.", type,
7078 err, qf_inums[type]);
7080 ext4_quotas_off(sb, type);
7088 static int ext4_quota_off(struct super_block *sb, int type)
7090 struct inode *inode = sb_dqopt(sb)->files[type];
7094 /* Force all delayed allocation blocks to be allocated.
7095 * Caller already holds s_umount sem */
7096 if (test_opt(sb, DELALLOC))
7097 sync_filesystem(sb);
7099 if (!inode || !igrab(inode))
7102 err = dquot_quota_off(sb, type);
7103 if (err || ext4_has_feature_quota(sb))
7106 * When the filesystem was remounted read-only first, we cannot cleanup
7107 * inode flags here. Bad luck but people should be using QUOTA feature
7108 * these days anyway.
7115 * Update modification times of quota files when userspace can
7116 * start looking at them. If we fail, we return success anyway since
7117 * this is not a hard failure and quotas are already disabled.
7119 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
7120 if (IS_ERR(handle)) {
7121 err = PTR_ERR(handle);
7124 EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
7125 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
7126 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
7127 err = ext4_mark_inode_dirty(handle, inode);
7128 ext4_journal_stop(handle);
7130 inode_unlock(inode);
7132 lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
7136 return dquot_quota_off(sb, type);
7139 /* Read data from quotafile - avoid pagecache and such because we cannot afford
7140 * acquiring the locks... As quota files are never truncated and quota code
7141 * itself serializes the operations (and no one else should touch the files)
7142 * we don't have to be afraid of races */
7143 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
7144 size_t len, loff_t off)
7146 struct inode *inode = sb_dqopt(sb)->files[type];
7147 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
7148 int offset = off & (sb->s_blocksize - 1);
7151 struct buffer_head *bh;
7152 loff_t i_size = i_size_read(inode);
7156 if (off+len > i_size)
7159 while (toread > 0) {
7160 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
7161 bh = ext4_bread(NULL, inode, blk, 0);
7164 if (!bh) /* A hole? */
7165 memset(data, 0, tocopy);
7167 memcpy(data, bh->b_data+offset, tocopy);
7177 /* Write to quotafile (we know the transaction is already started and has
7178 * enough credits) */
7179 static ssize_t ext4_quota_write(struct super_block *sb, int type,
7180 const char *data, size_t len, loff_t off)
7182 struct inode *inode = sb_dqopt(sb)->files[type];
7183 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
7184 int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
7186 struct buffer_head *bh;
7187 handle_t *handle = journal_current_handle();
7190 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
7191 " cancelled because transaction is not started",
7192 (unsigned long long)off, (unsigned long long)len);
7196 * Since we account only one data block in transaction credits,
7197 * then it is impossible to cross a block boundary.
7199 if (sb->s_blocksize - offset < len) {
7200 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
7201 " cancelled because not block aligned",
7202 (unsigned long long)off, (unsigned long long)len);
7207 bh = ext4_bread(handle, inode, blk,
7208 EXT4_GET_BLOCKS_CREATE |
7209 EXT4_GET_BLOCKS_METADATA_NOFAIL);
7210 } while (PTR_ERR(bh) == -ENOSPC &&
7211 ext4_should_retry_alloc(inode->i_sb, &retries));
7216 BUFFER_TRACE(bh, "get write access");
7217 err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
7223 memcpy(bh->b_data+offset, data, len);
7224 flush_dcache_page(bh->b_page);
7226 err = ext4_handle_dirty_metadata(handle, NULL, bh);
7229 if (inode->i_size < off + len) {
7230 i_size_write(inode, off + len);
7231 EXT4_I(inode)->i_disksize = inode->i_size;
7232 err2 = ext4_mark_inode_dirty(handle, inode);
7233 if (unlikely(err2 && !err))
7236 return err ? err : len;
7240 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
7241 static inline void register_as_ext2(void)
7243 int err = register_filesystem(&ext2_fs_type);
7246 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
7249 static inline void unregister_as_ext2(void)
7251 unregister_filesystem(&ext2_fs_type);
7254 static inline int ext2_feature_set_ok(struct super_block *sb)
7256 if (ext4_has_unknown_ext2_incompat_features(sb))
7260 if (ext4_has_unknown_ext2_ro_compat_features(sb))
7265 static inline void register_as_ext2(void) { }
7266 static inline void unregister_as_ext2(void) { }
7267 static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
7270 static inline void register_as_ext3(void)
7272 int err = register_filesystem(&ext3_fs_type);
7275 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
7278 static inline void unregister_as_ext3(void)
7280 unregister_filesystem(&ext3_fs_type);
7283 static inline int ext3_feature_set_ok(struct super_block *sb)
7285 if (ext4_has_unknown_ext3_incompat_features(sb))
7287 if (!ext4_has_feature_journal(sb))
7291 if (ext4_has_unknown_ext3_ro_compat_features(sb))
7296 static void ext4_kill_sb(struct super_block *sb)
7298 struct ext4_sb_info *sbi = EXT4_SB(sb);
7299 struct file *bdev_file = sbi ? sbi->s_journal_bdev_file : NULL;
7301 kill_block_super(sb);
7304 bdev_fput(bdev_file);
7307 static struct file_system_type ext4_fs_type = {
7308 .owner = THIS_MODULE,
7310 .init_fs_context = ext4_init_fs_context,
7311 .parameters = ext4_param_specs,
7312 .kill_sb = ext4_kill_sb,
7313 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
7315 MODULE_ALIAS_FS("ext4");
7317 /* Shared across all ext4 file systems */
7318 wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
7320 static int __init ext4_init_fs(void)
7324 ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
7325 ext4_li_info = NULL;
7327 /* Build-time check for flags consistency */
7328 ext4_check_flag_values();
7330 for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
7331 init_waitqueue_head(&ext4__ioend_wq[i]);
7333 err = ext4_init_es();
7337 err = ext4_init_pending();
7341 err = ext4_init_post_read_processing();
7345 err = ext4_init_pageio();
7349 err = ext4_init_system_zone();
7353 err = ext4_init_sysfs();
7357 err = ext4_init_mballoc();
7360 err = init_inodecache();
7364 err = ext4_fc_init_dentry_cache();
7370 err = register_filesystem(&ext4_fs_type);
7376 unregister_as_ext2();
7377 unregister_as_ext3();
7378 ext4_fc_destroy_dentry_cache();
7380 destroy_inodecache();
7382 ext4_exit_mballoc();
7386 ext4_exit_system_zone();
7390 ext4_exit_post_read_processing();
7392 ext4_exit_pending();
7399 static void __exit ext4_exit_fs(void)
7401 ext4_destroy_lazyinit_thread();
7402 unregister_as_ext2();
7403 unregister_as_ext3();
7404 unregister_filesystem(&ext4_fs_type);
7405 ext4_fc_destroy_dentry_cache();
7406 destroy_inodecache();
7407 ext4_exit_mballoc();
7409 ext4_exit_system_zone();
7411 ext4_exit_post_read_processing();
7413 ext4_exit_pending();
7416 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
7417 MODULE_DESCRIPTION("Fourth Extended Filesystem");
7418 MODULE_LICENSE("GPL");
7419 MODULE_SOFTDEP("pre: crc32c");
7420 module_init(ext4_init_fs)
7421 module_exit(ext4_exit_fs)