xref: /linux/fs/ext4/super.c (revision 2ef6c32a914b85217b44a0a2418e830e520b085e)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/super.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  from
11ac27a0ecSDave Kleikamp  *
12ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
13ac27a0ecSDave Kleikamp  *
14ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
15ac27a0ecSDave Kleikamp  *
16ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
17ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
18ac27a0ecSDave Kleikamp  */
19ac27a0ecSDave Kleikamp 
20ac27a0ecSDave Kleikamp #include <linux/module.h>
21ac27a0ecSDave Kleikamp #include <linux/string.h>
22ac27a0ecSDave Kleikamp #include <linux/fs.h>
23ac27a0ecSDave Kleikamp #include <linux/time.h>
24c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
2866114cadSTejun Heo #include <linux/backing-dev.h>
29ac27a0ecSDave Kleikamp #include <linux/parser.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.h>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ef510424SDan Williams #include <linux/dax.h>
427c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
43ee73f9a5SJeff Layton #include <linux/iversion.h>
44c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h>
45c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
46bfff6873SLukas Czerner #include <linux/kthread.h>
47bfff6873SLukas Czerner #include <linux/freezer.h>
489a089b21SGabriel Krisman Bertazi #include <linux/fsnotify.h>
49e5a185c2SLukas Czerner #include <linux/fs_context.h>
50e5a185c2SLukas Czerner #include <linux/fs_parser.h>
51bfff6873SLukas Czerner 
523dcf5451SChristoph Hellwig #include "ext4.h"
534a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
543dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
55ac27a0ecSDave Kleikamp #include "xattr.h"
56ac27a0ecSDave Kleikamp #include "acl.h"
573661d286STheodore Ts'o #include "mballoc.h"
580c9ec4beSDarrick J. Wong #include "fsmap.h"
59ac27a0ecSDave Kleikamp 
609bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
619bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
629bffad1eSTheodore Ts'o 
630b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
6459ebc7fdSZheng Yongjun static DEFINE_MUTEX(ext4_li_mtx);
65e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
669f6200bbSTheodore Ts'o 
67617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
68ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
692adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
702d01ddc8SJan Kara static void ext4_update_super(struct super_block *sb);
714392fbc4SJan Kara static int ext4_commit_super(struct super_block *sb);
7211215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
73617ba13bSMingming Cao 					struct ext4_super_block *es);
7411215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
75617ba13bSMingming Cao 				  struct ext4_super_block *es);
76617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
77617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
78c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
79c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
82bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
83bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
848f1f7453SEric Sandeen static void ext4_clear_request_list(void);
85c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
86c6cb7e77SEric Whitney 					    unsigned int journal_inum);
87da812f61SLukas Czerner static int ext4_validate_options(struct fs_context *fc);
88b6bd2435SLukas Czerner static int ext4_check_opt_consistency(struct fs_context *fc,
89e6e268cbSLukas Czerner 				      struct super_block *sb);
9085456054SEric Biggers static void ext4_apply_options(struct fs_context *fc, struct super_block *sb);
9102f960f8SLukas Czerner static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param);
92cebe85d5SLukas Czerner static int ext4_get_tree(struct fs_context *fc);
93cebe85d5SLukas Czerner static int ext4_reconfigure(struct fs_context *fc);
94cebe85d5SLukas Czerner static void ext4_fc_free(struct fs_context *fc);
95cebe85d5SLukas Czerner static int ext4_init_fs_context(struct fs_context *fc);
96cebe85d5SLukas Czerner static const struct fs_parameter_spec ext4_param_specs[];
97ac27a0ecSDave Kleikamp 
98e74031fdSJan Kara /*
99e74031fdSJan Kara  * Lock ordering
100e74031fdSJan Kara  *
101e74031fdSJan Kara  * page fault path:
102d4f5258eSJan Kara  * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
103d4f5258eSJan Kara  *   -> page lock -> i_data_sem (rw)
104e74031fdSJan Kara  *
105e74031fdSJan Kara  * buffered write path:
106c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
107e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
108e74031fdSJan Kara  *   i_data_sem (rw)
109e74031fdSJan Kara  *
110e74031fdSJan Kara  * truncate:
111d4f5258eSJan Kara  * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
112d4f5258eSJan Kara  *   page lock
113d4f5258eSJan Kara  * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
1141d39834fSNikolay Borisov  *   i_data_sem (rw)
115e74031fdSJan Kara  *
116e74031fdSJan Kara  * direct IO:
117c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
1181d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
119e74031fdSJan Kara  *
120e74031fdSJan Kara  * writepages:
121e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
122e74031fdSJan Kara  */
123e74031fdSJan Kara 
12402f960f8SLukas Czerner static const struct fs_context_operations ext4_context_ops = {
12502f960f8SLukas Czerner 	.parse_param	= ext4_parse_param,
126cebe85d5SLukas Czerner 	.get_tree	= ext4_get_tree,
127cebe85d5SLukas Czerner 	.reconfigure	= ext4_reconfigure,
128cebe85d5SLukas Czerner 	.free		= ext4_fc_free,
12902f960f8SLukas Czerner };
13002f960f8SLukas Czerner 
13102f960f8SLukas Czerner 
132c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1332035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1342035e776STheodore Ts'o 	.owner			= THIS_MODULE,
1352035e776STheodore Ts'o 	.name			= "ext2",
136cebe85d5SLukas Czerner 	.init_fs_context	= ext4_init_fs_context,
137cebe85d5SLukas Czerner 	.parameters		= ext4_param_specs,
1382035e776STheodore Ts'o 	.kill_sb		= kill_block_super,
1392035e776STheodore Ts'o 	.fs_flags		= FS_REQUIRES_DEV,
1402035e776STheodore Ts'o };
1417f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
142fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1432035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1442035e776STheodore Ts'o #else
1452035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1462035e776STheodore Ts'o #endif
1472035e776STheodore Ts'o 
1482035e776STheodore Ts'o 
149ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
150ba69f9abSJan Kara 	.owner			= THIS_MODULE,
151ba69f9abSJan Kara 	.name			= "ext3",
152cebe85d5SLukas Czerner 	.init_fs_context	= ext4_init_fs_context,
153cebe85d5SLukas Czerner 	.parameters		= ext4_param_specs,
154ba69f9abSJan Kara 	.kill_sb		= kill_block_super,
155ba69f9abSJan Kara 	.fs_flags		= FS_REQUIRES_DEV,
156ba69f9abSJan Kara };
1577f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
158fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
159ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
160bd81d8eeSLaurent Vivier 
161fa491b14Szhangyi (F) 
16267c0f556SBart Van Assche static inline void __ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
163fa491b14Szhangyi (F) 				  bh_end_io_t *end_io)
164fa491b14Szhangyi (F) {
165fb265c9cSTheodore Ts'o 	/*
166fa491b14Szhangyi (F) 	 * buffer's verified bit is no longer valid after reading from
167fa491b14Szhangyi (F) 	 * disk again due to write out error, clear it to make sure we
168fa491b14Szhangyi (F) 	 * recheck the buffer contents.
169fa491b14Szhangyi (F) 	 */
170fa491b14Szhangyi (F) 	clear_buffer_verified(bh);
171fa491b14Szhangyi (F) 
172fa491b14Szhangyi (F) 	bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
173fa491b14Szhangyi (F) 	get_bh(bh);
1741420c4a5SBart Van Assche 	submit_bh(REQ_OP_READ | op_flags, bh);
175fa491b14Szhangyi (F) }
176fa491b14Szhangyi (F) 
17767c0f556SBart Van Assche void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags,
178fa491b14Szhangyi (F) 			 bh_end_io_t *end_io)
179fa491b14Szhangyi (F) {
180fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
181fa491b14Szhangyi (F) 
182fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
183fa491b14Szhangyi (F) 		unlock_buffer(bh);
184fa491b14Szhangyi (F) 		return;
185fa491b14Szhangyi (F) 	}
186fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
187fa491b14Szhangyi (F) }
188fa491b14Szhangyi (F) 
18967c0f556SBart Van Assche int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags, bh_end_io_t *end_io)
190fa491b14Szhangyi (F) {
191fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
192fa491b14Szhangyi (F) 
193fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
194fa491b14Szhangyi (F) 		unlock_buffer(bh);
195fa491b14Szhangyi (F) 		return 0;
196fa491b14Szhangyi (F) 	}
197fa491b14Szhangyi (F) 
198fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
199fa491b14Szhangyi (F) 
200fa491b14Szhangyi (F) 	wait_on_buffer(bh);
201fa491b14Szhangyi (F) 	if (buffer_uptodate(bh))
202fa491b14Szhangyi (F) 		return 0;
203fa491b14Szhangyi (F) 	return -EIO;
204fa491b14Szhangyi (F) }
205fa491b14Szhangyi (F) 
20667c0f556SBart Van Assche int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait)
207fa491b14Szhangyi (F) {
2080b73284cSZhang Yi 	lock_buffer(bh);
2090b73284cSZhang Yi 	if (!wait) {
210fa491b14Szhangyi (F) 		ext4_read_bh_nowait(bh, op_flags, NULL);
211fa491b14Szhangyi (F) 		return 0;
212fa491b14Szhangyi (F) 	}
2130b73284cSZhang Yi 	return ext4_read_bh(bh, op_flags, NULL);
214fa491b14Szhangyi (F) }
215fa491b14Szhangyi (F) 
216fb265c9cSTheodore Ts'o /*
2178394a6abSzhangyi (F)  * This works like __bread_gfp() except it uses ERR_PTR for error
218fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
219fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
220fb265c9cSTheodore Ts'o  * return.
221fb265c9cSTheodore Ts'o  */
2228394a6abSzhangyi (F) static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
22367c0f556SBart Van Assche 					       sector_t block,
22467c0f556SBart Van Assche 					       blk_opf_t op_flags, gfp_t gfp)
225fb265c9cSTheodore Ts'o {
2262d069c08Szhangyi (F) 	struct buffer_head *bh;
2272d069c08Szhangyi (F) 	int ret;
228fb265c9cSTheodore Ts'o 
2298394a6abSzhangyi (F) 	bh = sb_getblk_gfp(sb, block, gfp);
230fb265c9cSTheodore Ts'o 	if (bh == NULL)
231fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
232cf2834a5STheodore Ts'o 	if (ext4_buffer_uptodate(bh))
233fb265c9cSTheodore Ts'o 		return bh;
2342d069c08Szhangyi (F) 
2352d069c08Szhangyi (F) 	ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
2362d069c08Szhangyi (F) 	if (ret) {
237fb265c9cSTheodore Ts'o 		put_bh(bh);
2382d069c08Szhangyi (F) 		return ERR_PTR(ret);
2392d069c08Szhangyi (F) 	}
2402d069c08Szhangyi (F) 	return bh;
241fb265c9cSTheodore Ts'o }
242fb265c9cSTheodore Ts'o 
2438394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
24467c0f556SBart Van Assche 				   blk_opf_t op_flags)
2458394a6abSzhangyi (F) {
2468394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
2478394a6abSzhangyi (F) }
2488394a6abSzhangyi (F) 
2498394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
2508394a6abSzhangyi (F) 					    sector_t block)
2518394a6abSzhangyi (F) {
2528394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, 0, 0);
2538394a6abSzhangyi (F) }
2548394a6abSzhangyi (F) 
2555df1d412Szhangyi (F) void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
2565df1d412Szhangyi (F) {
2575df1d412Szhangyi (F) 	struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
2585df1d412Szhangyi (F) 
2595df1d412Szhangyi (F) 	if (likely(bh)) {
2600b73284cSZhang Yi 		if (trylock_buffer(bh))
2610b73284cSZhang Yi 			ext4_read_bh_nowait(bh, REQ_RAHEAD, NULL);
2625df1d412Szhangyi (F) 		brelse(bh);
2635df1d412Szhangyi (F) 	}
264c197855eSStephen Hemminger }
265a9c47317SDarrick J. Wong 
266a9c47317SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
2679aa5d32bSDmitry Monakhov 				 struct ext4_super_block *es)
268a9c47317SDarrick J. Wong {
269a9c47317SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
270a9c47317SDarrick J. Wong 		return 1;
271a9c47317SDarrick J. Wong 
272a9c47317SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
27306db49e6STheodore Ts'o }
274a9c47317SDarrick J. Wong 
275bbc605cdSLukas Czerner __le32 ext4_superblock_csum(struct super_block *sb,
276a9c47317SDarrick J. Wong 			    struct ext4_super_block *es)
277a9c47317SDarrick J. Wong {
278a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
279a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
280a9c47317SDarrick J. Wong 	__u32 csum;
281a9c47317SDarrick J. Wong 
282a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
283a9c47317SDarrick J. Wong 
284a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
285a9c47317SDarrick J. Wong }
286a9c47317SDarrick J. Wong 
287a9c47317SDarrick J. Wong static int ext4_superblock_csum_verify(struct super_block *sb,
288a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
289a9c47317SDarrick J. Wong {
290a9c47317SDarrick J. Wong 	if (!ext4_has_metadata_csum(sb))
291a9c47317SDarrick J. Wong 		return 1;
292a9c47317SDarrick J. Wong 
293a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
294a9c47317SDarrick J. Wong }
295a9c47317SDarrick J. Wong 
296a9c47317SDarrick J. Wong void ext4_superblock_csum_set(struct super_block *sb)
297a9c47317SDarrick J. Wong {
29806db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
29906db49e6STheodore Ts'o 
3009aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
301a9c47317SDarrick J. Wong 		return;
302a9c47317SDarrick J. Wong 
303a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
304a9c47317SDarrick J. Wong }
305a9c47317SDarrick J. Wong 
3068fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
3078fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
308bd81d8eeSLaurent Vivier {
3093a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
3108fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3118fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
312bd81d8eeSLaurent Vivier }
313bd81d8eeSLaurent Vivier 
3148fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
3158fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
316bd81d8eeSLaurent Vivier {
3175272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
3188fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3198fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
320bd81d8eeSLaurent Vivier }
321bd81d8eeSLaurent Vivier 
3228fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
3238fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
324bd81d8eeSLaurent Vivier {
3255272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
3268fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3278fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
328bd81d8eeSLaurent Vivier }
329bd81d8eeSLaurent Vivier 
330021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
331560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
332560671a0SAneesh Kumar K.V {
333560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
334560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
335560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
336560671a0SAneesh Kumar K.V }
337560671a0SAneesh Kumar K.V 
338560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
339560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
340560671a0SAneesh Kumar K.V {
341560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
342560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
343560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
344560671a0SAneesh Kumar K.V }
345560671a0SAneesh Kumar K.V 
346560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
347560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
348560671a0SAneesh Kumar K.V {
349560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
350560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
351560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
352560671a0SAneesh Kumar K.V }
353560671a0SAneesh Kumar K.V 
354560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
355560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
356560671a0SAneesh Kumar K.V {
357560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
358560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
359560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
360560671a0SAneesh Kumar K.V }
361560671a0SAneesh Kumar K.V 
3628fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
3638fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
364bd81d8eeSLaurent Vivier {
3653a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
3668fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3678fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
368bd81d8eeSLaurent Vivier }
369bd81d8eeSLaurent Vivier 
3708fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
3718fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
372bd81d8eeSLaurent Vivier {
3735272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
3748fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3758fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
376bd81d8eeSLaurent Vivier }
377bd81d8eeSLaurent Vivier 
3788fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
3798fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
380bd81d8eeSLaurent Vivier {
3815272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
3828fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3838fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
384bd81d8eeSLaurent Vivier }
385bd81d8eeSLaurent Vivier 
386021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
387560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
388560671a0SAneesh Kumar K.V {
389560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
390560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
391560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
392560671a0SAneesh Kumar K.V }
393560671a0SAneesh Kumar K.V 
394560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
395560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
396560671a0SAneesh Kumar K.V {
397560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
398560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
399560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
400560671a0SAneesh Kumar K.V }
401560671a0SAneesh Kumar K.V 
402560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
403560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
404560671a0SAneesh Kumar K.V {
405560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
406560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
407560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
408560671a0SAneesh Kumar K.V }
409560671a0SAneesh Kumar K.V 
410560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
411560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
412560671a0SAneesh Kumar K.V {
413560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
414560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
415560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
416560671a0SAneesh Kumar K.V }
417560671a0SAneesh Kumar K.V 
418c92dc856SJan Kara static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
4196a0678a7SArnd Bergmann {
4206a0678a7SArnd Bergmann 	now = clamp_val(now, 0, (1ull << 40) - 1);
4216a0678a7SArnd Bergmann 
4226a0678a7SArnd Bergmann 	*lo = cpu_to_le32(lower_32_bits(now));
4236a0678a7SArnd Bergmann 	*hi = upper_32_bits(now);
4246a0678a7SArnd Bergmann }
4256a0678a7SArnd Bergmann 
4266a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
4276a0678a7SArnd Bergmann {
4286a0678a7SArnd Bergmann 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
4296a0678a7SArnd Bergmann }
4306a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \
431c92dc856SJan Kara 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
432c92dc856SJan Kara 			     ktime_get_real_seconds())
4336a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \
4346a0678a7SArnd Bergmann 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
435d3d1faf6SCurt Wohlgemuth 
436bdfe0cbdSTheodore Ts'o /*
437bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
438bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
439bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
440bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
441bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
442bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
443bdfe0cbdSTheodore Ts'o  */
444bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
445bdfe0cbdSTheodore Ts'o {
446bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
447bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
448bdfe0cbdSTheodore Ts'o 
449bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
450bdfe0cbdSTheodore Ts'o }
451bdfe0cbdSTheodore Ts'o 
45218aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
45318aadd47SBobi Jam {
45418aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
45518aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
45618aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
4575d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
45818aadd47SBobi Jam 
4595d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
460a0154344SDaeho Jeong 
461a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
462a0154344SDaeho Jeong 
46318aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
4645d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
4655d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
4665d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
46718aadd47SBobi Jam 		list_del_init(&jce->jce_list);
46818aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
46918aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
47018aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
47118aadd47SBobi Jam 	}
47218aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
47318aadd47SBobi Jam }
4741c13d5c0STheodore Ts'o 
475afb585a9SMauricio Faria de Oliveira /*
476afb585a9SMauricio Faria de Oliveira  * This writepage callback for write_cache_pages()
477afb585a9SMauricio Faria de Oliveira  * takes care of a few cases after page cleaning.
478afb585a9SMauricio Faria de Oliveira  *
479afb585a9SMauricio Faria de Oliveira  * write_cache_pages() already checks for dirty pages
480afb585a9SMauricio Faria de Oliveira  * and calls clear_page_dirty_for_io(), which we want,
481afb585a9SMauricio Faria de Oliveira  * to write protect the pages.
482afb585a9SMauricio Faria de Oliveira  *
483afb585a9SMauricio Faria de Oliveira  * However, we may have to redirty a page (see below.)
484afb585a9SMauricio Faria de Oliveira  */
485d585bdbeSMatthew Wilcox (Oracle) static int ext4_journalled_writepage_callback(struct folio *folio,
486afb585a9SMauricio Faria de Oliveira 					      struct writeback_control *wbc,
487afb585a9SMauricio Faria de Oliveira 					      void *data)
488afb585a9SMauricio Faria de Oliveira {
489afb585a9SMauricio Faria de Oliveira 	transaction_t *transaction = (transaction_t *) data;
490afb585a9SMauricio Faria de Oliveira 	struct buffer_head *bh, *head;
491afb585a9SMauricio Faria de Oliveira 	struct journal_head *jh;
492afb585a9SMauricio Faria de Oliveira 
493d585bdbeSMatthew Wilcox (Oracle) 	bh = head = folio_buffers(folio);
494afb585a9SMauricio Faria de Oliveira 	do {
495afb585a9SMauricio Faria de Oliveira 		/*
496afb585a9SMauricio Faria de Oliveira 		 * We have to redirty a page in these cases:
497afb585a9SMauricio Faria de Oliveira 		 * 1) If buffer is dirty, it means the page was dirty because it
498afb585a9SMauricio Faria de Oliveira 		 * contains a buffer that needs checkpointing. So the dirty bit
499afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that checkpointing writes the buffer
500afb585a9SMauricio Faria de Oliveira 		 * properly.
501afb585a9SMauricio Faria de Oliveira 		 * 2) If buffer is not part of the committing transaction
502afb585a9SMauricio Faria de Oliveira 		 * (we may have just accidentally come across this buffer because
503afb585a9SMauricio Faria de Oliveira 		 * inode range tracking is not exact) or if the currently running
504afb585a9SMauricio Faria de Oliveira 		 * transaction already contains this buffer as well, dirty bit
505afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that the buffer gets writeprotected
506afb585a9SMauricio Faria de Oliveira 		 * properly on running transaction's commit.
507afb585a9SMauricio Faria de Oliveira 		 */
508afb585a9SMauricio Faria de Oliveira 		jh = bh2jh(bh);
509afb585a9SMauricio Faria de Oliveira 		if (buffer_dirty(bh) ||
510afb585a9SMauricio Faria de Oliveira 		    (jh && (jh->b_transaction != transaction ||
511afb585a9SMauricio Faria de Oliveira 			    jh->b_next_transaction))) {
512d585bdbeSMatthew Wilcox (Oracle) 			folio_redirty_for_writepage(wbc, folio);
513afb585a9SMauricio Faria de Oliveira 			goto out;
514afb585a9SMauricio Faria de Oliveira 		}
515afb585a9SMauricio Faria de Oliveira 	} while ((bh = bh->b_this_page) != head);
516afb585a9SMauricio Faria de Oliveira 
517afb585a9SMauricio Faria de Oliveira out:
518afb585a9SMauricio Faria de Oliveira 	return AOP_WRITEPAGE_ACTIVATE;
519afb585a9SMauricio Faria de Oliveira }
520afb585a9SMauricio Faria de Oliveira 
521afb585a9SMauricio Faria de Oliveira static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
522afb585a9SMauricio Faria de Oliveira {
523afb585a9SMauricio Faria de Oliveira 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
524afb585a9SMauricio Faria de Oliveira 	struct writeback_control wbc = {
525afb585a9SMauricio Faria de Oliveira 		.sync_mode =  WB_SYNC_ALL,
526afb585a9SMauricio Faria de Oliveira 		.nr_to_write = LONG_MAX,
527afb585a9SMauricio Faria de Oliveira 		.range_start = jinode->i_dirty_start,
528afb585a9SMauricio Faria de Oliveira 		.range_end = jinode->i_dirty_end,
529afb585a9SMauricio Faria de Oliveira         };
530afb585a9SMauricio Faria de Oliveira 
531afb585a9SMauricio Faria de Oliveira 	return write_cache_pages(mapping, &wbc,
532afb585a9SMauricio Faria de Oliveira 				 ext4_journalled_writepage_callback,
533afb585a9SMauricio Faria de Oliveira 				 jinode->i_transaction);
534afb585a9SMauricio Faria de Oliveira }
535afb585a9SMauricio Faria de Oliveira 
536afb585a9SMauricio Faria de Oliveira static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
537afb585a9SMauricio Faria de Oliveira {
538afb585a9SMauricio Faria de Oliveira 	int ret;
539afb585a9SMauricio Faria de Oliveira 
540afb585a9SMauricio Faria de Oliveira 	if (ext4_should_journal_data(jinode->i_vfs_inode))
541afb585a9SMauricio Faria de Oliveira 		ret = ext4_journalled_submit_inode_data_buffers(jinode);
542afb585a9SMauricio Faria de Oliveira 	else
54359205c8dSJan Kara 		ret = ext4_normal_submit_inode_data_buffers(jinode);
544afb585a9SMauricio Faria de Oliveira 	return ret;
545afb585a9SMauricio Faria de Oliveira }
546afb585a9SMauricio Faria de Oliveira 
547afb585a9SMauricio Faria de Oliveira static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
548afb585a9SMauricio Faria de Oliveira {
549afb585a9SMauricio Faria de Oliveira 	int ret = 0;
550afb585a9SMauricio Faria de Oliveira 
551afb585a9SMauricio Faria de Oliveira 	if (!ext4_should_journal_data(jinode->i_vfs_inode))
552afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_finish_inode_data_buffers(jinode);
553afb585a9SMauricio Faria de Oliveira 
554afb585a9SMauricio Faria de Oliveira 	return ret;
555afb585a9SMauricio Faria de Oliveira }
556afb585a9SMauricio Faria de Oliveira 
5571dc1097fSJan Kara static bool system_going_down(void)
5581dc1097fSJan Kara {
5591dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
5601dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
5611dc1097fSJan Kara }
5621dc1097fSJan Kara 
56302a7780eSJan Kara struct ext4_err_translation {
56402a7780eSJan Kara 	int code;
56502a7780eSJan Kara 	int errno;
56602a7780eSJan Kara };
56702a7780eSJan Kara 
56802a7780eSJan Kara #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
56902a7780eSJan Kara 
57002a7780eSJan Kara static struct ext4_err_translation err_translation[] = {
57102a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EIO),
57202a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOMEM),
57302a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFSBADCRC),
57402a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFSCORRUPTED),
57502a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOSPC),
57602a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOKEY),
57702a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EROFS),
57802a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFBIG),
57902a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EEXIST),
58002a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ERANGE),
58102a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EOVERFLOW),
58202a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EBUSY),
58302a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOTDIR),
58402a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOTEMPTY),
58502a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ESHUTDOWN),
58602a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFAULT),
58702a7780eSJan Kara };
58802a7780eSJan Kara 
58902a7780eSJan Kara static int ext4_errno_to_code(int errno)
59002a7780eSJan Kara {
59102a7780eSJan Kara 	int i;
59202a7780eSJan Kara 
59302a7780eSJan Kara 	for (i = 0; i < ARRAY_SIZE(err_translation); i++)
59402a7780eSJan Kara 		if (err_translation[i].errno == errno)
59502a7780eSJan Kara 			return err_translation[i].code;
59602a7780eSJan Kara 	return EXT4_ERR_UNKNOWN;
59702a7780eSJan Kara }
59802a7780eSJan Kara 
5992d01ddc8SJan Kara static void save_error_info(struct super_block *sb, int error,
60040676623SJan Kara 			    __u32 ino, __u64 block,
60140676623SJan Kara 			    const char *func, unsigned int line)
60240676623SJan Kara {
603c92dc856SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
60440676623SJan Kara 
60502a7780eSJan Kara 	/* We default to EFSCORRUPTED error... */
60602a7780eSJan Kara 	if (error == 0)
60702a7780eSJan Kara 		error = EFSCORRUPTED;
608c92dc856SJan Kara 
609c92dc856SJan Kara 	spin_lock(&sbi->s_error_lock);
610c92dc856SJan Kara 	sbi->s_add_error_count++;
611c92dc856SJan Kara 	sbi->s_last_error_code = error;
612c92dc856SJan Kara 	sbi->s_last_error_line = line;
613c92dc856SJan Kara 	sbi->s_last_error_ino = ino;
614c92dc856SJan Kara 	sbi->s_last_error_block = block;
615c92dc856SJan Kara 	sbi->s_last_error_func = func;
616c92dc856SJan Kara 	sbi->s_last_error_time = ktime_get_real_seconds();
617c92dc856SJan Kara 	if (!sbi->s_first_error_time) {
618c92dc856SJan Kara 		sbi->s_first_error_code = error;
619c92dc856SJan Kara 		sbi->s_first_error_line = line;
620c92dc856SJan Kara 		sbi->s_first_error_ino = ino;
621c92dc856SJan Kara 		sbi->s_first_error_block = block;
622c92dc856SJan Kara 		sbi->s_first_error_func = func;
623c92dc856SJan Kara 		sbi->s_first_error_time = sbi->s_last_error_time;
62440676623SJan Kara 	}
625c92dc856SJan Kara 	spin_unlock(&sbi->s_error_lock);
62640676623SJan Kara }
62740676623SJan Kara 
628ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
629ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
630ac27a0ecSDave Kleikamp  *
631ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
632617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
633ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
634ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
635ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
636ac27a0ecSDave Kleikamp  * write out the superblock safely.
637ac27a0ecSDave Kleikamp  *
638dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
639d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
640ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
641014c9caaSJan Kara  *
642014c9caaSJan Kara  * If force_ro is set, we unconditionally force the filesystem into an
643014c9caaSJan Kara  * ABORT|READONLY state, unless the error response on the fs has been set to
644014c9caaSJan Kara  * panic in which case we take the easy way out and panic immediately. This is
645014c9caaSJan Kara  * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
646014c9caaSJan Kara  * at a critical moment in log management.
647ac27a0ecSDave Kleikamp  */
648e789ca0cSJan Kara static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
649e789ca0cSJan Kara 			      __u32 ino, __u64 block,
650e789ca0cSJan Kara 			      const char *func, unsigned int line)
651ac27a0ecSDave Kleikamp {
652b08070ecSJan Kara 	journal_t *journal = EXT4_SB(sb)->s_journal;
6532d01ddc8SJan Kara 	bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
654b08070ecSJan Kara 
655e789ca0cSJan Kara 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
656327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
657327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
658327eaf73STheodore Ts'o 
6592d01ddc8SJan Kara 	if (!continue_fs && !sb_rdonly(sb)) {
6609b5f6c9bSHarshad Shirwadkar 		ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
661ac27a0ecSDave Kleikamp 		if (journal)
662dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
6632d01ddc8SJan Kara 	}
6642d01ddc8SJan Kara 
6652d01ddc8SJan Kara 	if (!bdev_read_only(sb->s_bdev)) {
6662d01ddc8SJan Kara 		save_error_info(sb, error, ino, block, func, line);
6672d01ddc8SJan Kara 		/*
6682d01ddc8SJan Kara 		 * In case the fs should keep running, we need to writeout
6692d01ddc8SJan Kara 		 * superblock through the journal. Due to lock ordering
6702d01ddc8SJan Kara 		 * constraints, it may not be safe to do it right here so we
6712d01ddc8SJan Kara 		 * defer superblock flushing to a workqueue.
6722d01ddc8SJan Kara 		 */
673bb9464e0Syangerkun 		if (continue_fs && journal)
6742d01ddc8SJan Kara 			schedule_work(&EXT4_SB(sb)->s_error_work);
6752d01ddc8SJan Kara 		else
6762d01ddc8SJan Kara 			ext4_commit_super(sb);
6772d01ddc8SJan Kara 	}
6782d01ddc8SJan Kara 
6791dc1097fSJan Kara 	/*
6801dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
6811dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
6821dc1097fSJan Kara 	 * disabled.
6831dc1097fSJan Kara 	 */
684014c9caaSJan Kara 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
685617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
686ac27a0ecSDave Kleikamp 			sb->s_id);
687ac27a0ecSDave Kleikamp 	}
688ac2f7ca5SYe Bin 
689ac2f7ca5SYe Bin 	if (sb_rdonly(sb) || continue_fs)
690ac2f7ca5SYe Bin 		return;
691ac2f7ca5SYe Bin 
692014c9caaSJan Kara 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
693014c9caaSJan Kara 	/*
694014c9caaSJan Kara 	 * Make sure updated value of ->s_mount_flags will be visible before
695014c9caaSJan Kara 	 * ->s_flags update
696014c9caaSJan Kara 	 */
697014c9caaSJan Kara 	smp_wmb();
698014c9caaSJan Kara 	sb->s_flags |= SB_RDONLY;
6994327ba52SDaeho Jeong }
700ac27a0ecSDave Kleikamp 
701c92dc856SJan Kara static void flush_stashed_error_work(struct work_struct *work)
702c92dc856SJan Kara {
703c92dc856SJan Kara 	struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
704c92dc856SJan Kara 						s_error_work);
7052d01ddc8SJan Kara 	journal_t *journal = sbi->s_journal;
7062d01ddc8SJan Kara 	handle_t *handle;
707c92dc856SJan Kara 
7082d01ddc8SJan Kara 	/*
7092d01ddc8SJan Kara 	 * If the journal is still running, we have to write out superblock
7102d01ddc8SJan Kara 	 * through the journal to avoid collisions of other journalled sb
7112d01ddc8SJan Kara 	 * updates.
7122d01ddc8SJan Kara 	 *
7132d01ddc8SJan Kara 	 * We use directly jbd2 functions here to avoid recursing back into
7142d01ddc8SJan Kara 	 * ext4 error handling code during handling of previous errors.
7152d01ddc8SJan Kara 	 */
7162d01ddc8SJan Kara 	if (!sb_rdonly(sbi->s_sb) && journal) {
717558d6450SYe Bin 		struct buffer_head *sbh = sbi->s_sbh;
7182d01ddc8SJan Kara 		handle = jbd2_journal_start(journal, 1);
7192d01ddc8SJan Kara 		if (IS_ERR(handle))
7202d01ddc8SJan Kara 			goto write_directly;
721558d6450SYe Bin 		if (jbd2_journal_get_write_access(handle, sbh)) {
7222d01ddc8SJan Kara 			jbd2_journal_stop(handle);
7232d01ddc8SJan Kara 			goto write_directly;
7242d01ddc8SJan Kara 		}
7252d01ddc8SJan Kara 		ext4_update_super(sbi->s_sb);
726558d6450SYe Bin 		if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
727558d6450SYe Bin 			ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
728558d6450SYe Bin 				 "superblock detected");
729558d6450SYe Bin 			clear_buffer_write_io_error(sbh);
730558d6450SYe Bin 			set_buffer_uptodate(sbh);
731558d6450SYe Bin 		}
732558d6450SYe Bin 
733558d6450SYe Bin 		if (jbd2_journal_dirty_metadata(handle, sbh)) {
7342d01ddc8SJan Kara 			jbd2_journal_stop(handle);
7352d01ddc8SJan Kara 			goto write_directly;
7362d01ddc8SJan Kara 		}
7372d01ddc8SJan Kara 		jbd2_journal_stop(handle);
738d578b994SJonathan Davies 		ext4_notify_error_sysfs(sbi);
7392d01ddc8SJan Kara 		return;
7402d01ddc8SJan Kara 	}
7412d01ddc8SJan Kara write_directly:
7422d01ddc8SJan Kara 	/*
7432d01ddc8SJan Kara 	 * Write through journal failed. Write sb directly to get error info
7442d01ddc8SJan Kara 	 * out and hope for the best.
7452d01ddc8SJan Kara 	 */
7464392fbc4SJan Kara 	ext4_commit_super(sbi->s_sb);
747d578b994SJonathan Davies 	ext4_notify_error_sysfs(sbi);
748ac27a0ecSDave Kleikamp }
749ac27a0ecSDave Kleikamp 
750efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
751efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
752efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
753efbed4dcSTheodore Ts'o 
75412062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
755014c9caaSJan Kara 		  unsigned int line, bool force_ro, int error, __u64 block,
75654d3adbcSTheodore Ts'o 		  const char *fmt, ...)
757ac27a0ecSDave Kleikamp {
7580ff2ea7dSJoe Perches 	struct va_format vaf;
759ac27a0ecSDave Kleikamp 	va_list args;
760ac27a0ecSDave Kleikamp 
7610db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7620db1ff22STheodore Ts'o 		return;
7630db1ff22STheodore Ts'o 
764ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
765efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
766ac27a0ecSDave Kleikamp 		va_start(args, fmt);
7670ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7680ff2ea7dSJoe Perches 		vaf.va = &args;
769efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
770efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
7710ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
772ac27a0ecSDave Kleikamp 		va_end(args);
773efbed4dcSTheodore Ts'o 	}
7749a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(sb, NULL, error ? error : EFSCORRUPTED);
7759a089b21SGabriel Krisman Bertazi 
776e789ca0cSJan Kara 	ext4_handle_error(sb, force_ro, error, 0, block, function, line);
777ac27a0ecSDave Kleikamp }
778ac27a0ecSDave Kleikamp 
779e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
78054d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
781273df556SFrank Mayhar 			const char *fmt, ...)
782273df556SFrank Mayhar {
783273df556SFrank Mayhar 	va_list args;
784f7c21177STheodore Ts'o 	struct va_format vaf;
785273df556SFrank Mayhar 
7860db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
7870db1ff22STheodore Ts'o 		return;
7880db1ff22STheodore Ts'o 
789ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
790efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
791273df556SFrank Mayhar 		va_start(args, fmt);
792f7c21177STheodore Ts'o 		vaf.fmt = fmt;
793f7c21177STheodore Ts'o 		vaf.va = &args;
794c398eda0STheodore Ts'o 		if (block)
795d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
796d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
797d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
798d9ee81daSJoe Perches 			       block, current->comm, &vaf);
799d9ee81daSJoe Perches 		else
800d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
801d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
802d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
803d9ee81daSJoe Perches 			       current->comm, &vaf);
804273df556SFrank Mayhar 		va_end(args);
805efbed4dcSTheodore Ts'o 	}
8069a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(inode->i_sb, inode, error ? error : EFSCORRUPTED);
8079a089b21SGabriel Krisman Bertazi 
808e789ca0cSJan Kara 	ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
80954d3adbcSTheodore Ts'o 			  function, line);
810273df556SFrank Mayhar }
811273df556SFrank Mayhar 
812e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
813f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
814f7c21177STheodore Ts'o 		       const char *fmt, ...)
815273df556SFrank Mayhar {
816273df556SFrank Mayhar 	va_list args;
817f7c21177STheodore Ts'o 	struct va_format vaf;
818496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
819273df556SFrank Mayhar 	char pathname[80], *path;
820273df556SFrank Mayhar 
8210db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
8220db1ff22STheodore Ts'o 		return;
8230db1ff22STheodore Ts'o 
824ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
825efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
8269bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
827f9a62d09SDan Carpenter 		if (IS_ERR(path))
828273df556SFrank Mayhar 			path = "(unknown)";
829f7c21177STheodore Ts'o 		va_start(args, fmt);
830f7c21177STheodore Ts'o 		vaf.fmt = fmt;
831f7c21177STheodore Ts'o 		vaf.va = &args;
832d9ee81daSJoe Perches 		if (block)
833d9ee81daSJoe Perches 			printk(KERN_CRIT
834d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
835d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
836d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
837d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
838d9ee81daSJoe Perches 		else
839d9ee81daSJoe Perches 			printk(KERN_CRIT
840d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
841d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
842d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
843d9ee81daSJoe Perches 			       current->comm, path, &vaf);
844273df556SFrank Mayhar 		va_end(args);
845efbed4dcSTheodore Ts'o 	}
8469a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(inode->i_sb, inode, EFSCORRUPTED);
8479a089b21SGabriel Krisman Bertazi 
848e789ca0cSJan Kara 	ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
84954d3adbcSTheodore Ts'o 			  function, line);
850273df556SFrank Mayhar }
851273df556SFrank Mayhar 
852722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
853ac27a0ecSDave Kleikamp 			      char nbuf[16])
854ac27a0ecSDave Kleikamp {
855ac27a0ecSDave Kleikamp 	char *errstr = NULL;
856ac27a0ecSDave Kleikamp 
857ac27a0ecSDave Kleikamp 	switch (errno) {
8586a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
8596a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
8606a797d27SDarrick J. Wong 		break;
8616a797d27SDarrick J. Wong 	case -EFSBADCRC:
8626a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
8636a797d27SDarrick J. Wong 		break;
864ac27a0ecSDave Kleikamp 	case -EIO:
865ac27a0ecSDave Kleikamp 		errstr = "IO failure";
866ac27a0ecSDave Kleikamp 		break;
867ac27a0ecSDave Kleikamp 	case -ENOMEM:
868ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
869ac27a0ecSDave Kleikamp 		break;
870ac27a0ecSDave Kleikamp 	case -EROFS:
87178f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
87278f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
873ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
874ac27a0ecSDave Kleikamp 		else
875ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
876ac27a0ecSDave Kleikamp 		break;
877ac27a0ecSDave Kleikamp 	default:
878ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
879ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
880ac27a0ecSDave Kleikamp 		 * NULL. */
881ac27a0ecSDave Kleikamp 		if (nbuf) {
882ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
883ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
884ac27a0ecSDave Kleikamp 				errstr = nbuf;
885ac27a0ecSDave Kleikamp 		}
886ac27a0ecSDave Kleikamp 		break;
887ac27a0ecSDave Kleikamp 	}
888ac27a0ecSDave Kleikamp 
889ac27a0ecSDave Kleikamp 	return errstr;
890ac27a0ecSDave Kleikamp }
891ac27a0ecSDave Kleikamp 
892617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
893ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
894ac27a0ecSDave Kleikamp 
895c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
896c398eda0STheodore Ts'o 		      unsigned int line, int errno)
897ac27a0ecSDave Kleikamp {
898ac27a0ecSDave Kleikamp 	char nbuf[16];
899ac27a0ecSDave Kleikamp 	const char *errstr;
900ac27a0ecSDave Kleikamp 
9010db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
9020db1ff22STheodore Ts'o 		return;
9030db1ff22STheodore Ts'o 
904ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
905ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
906ac27a0ecSDave Kleikamp 	 * an error. */
907bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
908ac27a0ecSDave Kleikamp 		return;
909ac27a0ecSDave Kleikamp 
910efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
911617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
912c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
913c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
914efbed4dcSTheodore Ts'o 	}
9159a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(sb, NULL, errno ? errno : EFSCORRUPTED);
916ac27a0ecSDave Kleikamp 
917e789ca0cSJan Kara 	ext4_handle_error(sb, false, -errno, 0, 0, function, line);
918ac27a0ecSDave Kleikamp }
919ac27a0ecSDave Kleikamp 
920e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
921e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
922b31e1552SEric Sandeen {
9230ff2ea7dSJoe Perches 	struct va_format vaf;
924b31e1552SEric Sandeen 	va_list args;
925b31e1552SEric Sandeen 
926da812f61SLukas Czerner 	if (sb) {
9271cf006edSDmitry Monakhov 		atomic_inc(&EXT4_SB(sb)->s_msg_count);
928da812f61SLukas Czerner 		if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state),
929da812f61SLukas Czerner 				  "EXT4-fs"))
930efbed4dcSTheodore Ts'o 			return;
931da812f61SLukas Czerner 	}
932efbed4dcSTheodore Ts'o 
933b31e1552SEric Sandeen 	va_start(args, fmt);
9340ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9350ff2ea7dSJoe Perches 	vaf.va = &args;
936da812f61SLukas Czerner 	if (sb)
9370ff2ea7dSJoe Perches 		printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
938da812f61SLukas Czerner 	else
939da812f61SLukas Czerner 		printk("%sEXT4-fs: %pV\n", prefix, &vaf);
940b31e1552SEric Sandeen 	va_end(args);
941b31e1552SEric Sandeen }
942b31e1552SEric Sandeen 
9431cf006edSDmitry Monakhov static int ext4_warning_ratelimit(struct super_block *sb)
9441cf006edSDmitry Monakhov {
9451cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_warning_count);
9461cf006edSDmitry Monakhov 	return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
9471cf006edSDmitry Monakhov 			    "EXT4-fs warning");
9481cf006edSDmitry Monakhov }
949b03a2f7eSAndreas Dilger 
95012062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
951c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
952ac27a0ecSDave Kleikamp {
9530ff2ea7dSJoe Perches 	struct va_format vaf;
954ac27a0ecSDave Kleikamp 	va_list args;
955ac27a0ecSDave Kleikamp 
956b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
957efbed4dcSTheodore Ts'o 		return;
958efbed4dcSTheodore Ts'o 
959ac27a0ecSDave Kleikamp 	va_start(args, fmt);
9600ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9610ff2ea7dSJoe Perches 	vaf.va = &args;
9620ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
9630ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
964ac27a0ecSDave Kleikamp 	va_end(args);
965ac27a0ecSDave Kleikamp }
966ac27a0ecSDave Kleikamp 
967b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
968b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
969b03a2f7eSAndreas Dilger {
970b03a2f7eSAndreas Dilger 	struct va_format vaf;
971b03a2f7eSAndreas Dilger 	va_list args;
972b03a2f7eSAndreas Dilger 
973b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
974b03a2f7eSAndreas Dilger 		return;
975b03a2f7eSAndreas Dilger 
976b03a2f7eSAndreas Dilger 	va_start(args, fmt);
977b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
978b03a2f7eSAndreas Dilger 	vaf.va = &args;
979b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
980b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
981b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
982b03a2f7eSAndreas Dilger 	va_end(args);
983b03a2f7eSAndreas Dilger }
984b03a2f7eSAndreas Dilger 
985e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
986e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
987e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
988e29136f8STheodore Ts'o 			     const char *fmt, ...)
9895d1b1b3fSAneesh Kumar K.V __releases(bitlock)
9905d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
9915d1b1b3fSAneesh Kumar K.V {
9920ff2ea7dSJoe Perches 	struct va_format vaf;
9935d1b1b3fSAneesh Kumar K.V 	va_list args;
9945d1b1b3fSAneesh Kumar K.V 
9950db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
9960db1ff22STheodore Ts'o 		return;
9970db1ff22STheodore Ts'o 
998ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
999efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
10005d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
10010ff2ea7dSJoe Perches 		vaf.fmt = fmt;
10020ff2ea7dSJoe Perches 		vaf.va = &args;
100321149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
1004e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
1005e29136f8STheodore Ts'o 		if (ino)
10060ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
1007e29136f8STheodore Ts'o 		if (block)
1008efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
1009efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
10100ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
10115d1b1b3fSAneesh Kumar K.V 		va_end(args);
1012efbed4dcSTheodore Ts'o 	}
10135d1b1b3fSAneesh Kumar K.V 
1014c92dc856SJan Kara 	if (test_opt(sb, ERRORS_CONT)) {
1015327eaf73STheodore Ts'o 		if (test_opt(sb, WARN_ON_ERROR))
1016327eaf73STheodore Ts'o 			WARN_ON_ONCE(1);
1017e789ca0cSJan Kara 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
10182d01ddc8SJan Kara 		if (!bdev_read_only(sb->s_bdev)) {
10192d01ddc8SJan Kara 			save_error_info(sb, EFSCORRUPTED, ino, block, function,
10202d01ddc8SJan Kara 					line);
1021c92dc856SJan Kara 			schedule_work(&EXT4_SB(sb)->s_error_work);
10222d01ddc8SJan Kara 		}
10235d1b1b3fSAneesh Kumar K.V 		return;
10245d1b1b3fSAneesh Kumar K.V 	}
10255d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
1026e789ca0cSJan Kara 	ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
10275d1b1b3fSAneesh Kumar K.V 	/*
10285d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
10295d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
10305d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
10315d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
10325d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
103325985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
10345d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
10355d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
10365d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
10375d1b1b3fSAneesh Kumar K.V 	 */
10385d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
10395d1b1b3fSAneesh Kumar K.V 	return;
10405d1b1b3fSAneesh Kumar K.V }
10415d1b1b3fSAneesh Kumar K.V 
1042db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1043db79e6d1SWang Shilong 				     ext4_group_t group,
1044db79e6d1SWang Shilong 				     unsigned int flags)
1045db79e6d1SWang Shilong {
1046db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1047db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1048db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
10499af0b3d1SWang Shilong 	int ret;
1050db79e6d1SWang Shilong 
10515354b2afSTheodore Ts'o 	if (!grp || !gdp)
10525354b2afSTheodore Ts'o 		return;
10539af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
10549af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
10559af0b3d1SWang Shilong 					    &grp->bb_state);
10569af0b3d1SWang Shilong 		if (!ret)
1057db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
1058db79e6d1SWang Shilong 					   grp->bb_free);
1059db79e6d1SWang Shilong 	}
1060db79e6d1SWang Shilong 
10619af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
10629af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
10639af0b3d1SWang Shilong 					    &grp->bb_state);
10649af0b3d1SWang Shilong 		if (!ret && gdp) {
1065db79e6d1SWang Shilong 			int count;
1066db79e6d1SWang Shilong 
1067db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
1068db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1069db79e6d1SWang Shilong 					   count);
1070db79e6d1SWang Shilong 		}
1071db79e6d1SWang Shilong 	}
1072db79e6d1SWang Shilong }
1073db79e6d1SWang Shilong 
1074617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
1075ac27a0ecSDave Kleikamp {
1076617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1077ac27a0ecSDave Kleikamp 
1078617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1079ac27a0ecSDave Kleikamp 		return;
1080ac27a0ecSDave Kleikamp 
108112062dddSEric Sandeen 	ext4_warning(sb,
1082ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
1083ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
1084617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
1085ac27a0ecSDave Kleikamp 
1086617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1087617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1088617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1089ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
1090ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
1091ac27a0ecSDave Kleikamp 
1092ac27a0ecSDave Kleikamp 	/*
1093ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
1094ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
1095ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
1096ac27a0ecSDave Kleikamp 	 */
1097ac27a0ecSDave Kleikamp }
1098ac27a0ecSDave Kleikamp 
1099ac27a0ecSDave Kleikamp /*
1100ac27a0ecSDave Kleikamp  * Open the external journal device
1101ac27a0ecSDave Kleikamp  */
1102b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1103ac27a0ecSDave Kleikamp {
1104ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1105ac27a0ecSDave Kleikamp 
1106d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1107ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
1108ac27a0ecSDave Kleikamp 		goto fail;
1109ac27a0ecSDave Kleikamp 	return bdev;
1110ac27a0ecSDave Kleikamp 
1111ac27a0ecSDave Kleikamp fail:
1112ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
1113ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
1114ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1115ac27a0ecSDave Kleikamp 	return NULL;
1116ac27a0ecSDave Kleikamp }
1117ac27a0ecSDave Kleikamp 
1118ac27a0ecSDave Kleikamp /*
1119ac27a0ecSDave Kleikamp  * Release the journal device
1120ac27a0ecSDave Kleikamp  */
11214385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
1122ac27a0ecSDave Kleikamp {
11234385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1124ac27a0ecSDave Kleikamp }
1125ac27a0ecSDave Kleikamp 
11264385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1127ac27a0ecSDave Kleikamp {
1128ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1129ee7ed3aaSChunguang Xu 	bdev = sbi->s_journal_bdev;
1130ac27a0ecSDave Kleikamp 	if (bdev) {
113126fb5290SZhihao Cheng 		/*
113226fb5290SZhihao Cheng 		 * Invalidate the journal device's buffers.  We don't want them
113326fb5290SZhihao Cheng 		 * floating about in memory - the physical journal device may
113426fb5290SZhihao Cheng 		 * hotswapped, and it breaks the `ro-after' testing code.
113526fb5290SZhihao Cheng 		 */
113626fb5290SZhihao Cheng 		invalidate_bdev(bdev);
11374385bab1SAl Viro 		ext4_blkdev_put(bdev);
1138ee7ed3aaSChunguang Xu 		sbi->s_journal_bdev = NULL;
1139ac27a0ecSDave Kleikamp 	}
1140ac27a0ecSDave Kleikamp }
1141ac27a0ecSDave Kleikamp 
1142ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
1143ac27a0ecSDave Kleikamp {
1144617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1145ac27a0ecSDave Kleikamp }
1146ac27a0ecSDave Kleikamp 
1147617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1148ac27a0ecSDave Kleikamp {
1149ac27a0ecSDave Kleikamp 	struct list_head *l;
1150ac27a0ecSDave Kleikamp 
1151b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1152ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
1153ac27a0ecSDave Kleikamp 
1154ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
1155ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
1156ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
1157ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
1158ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1159ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
1160ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
1161ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1162ac27a0ecSDave Kleikamp 	}
1163ac27a0ecSDave Kleikamp }
1164ac27a0ecSDave Kleikamp 
1165957153fcSJan Kara #ifdef CONFIG_QUOTA
1166957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
1167957153fcSJan Kara 
1168f3c1c42eSBaokun Li static inline void ext4_quotas_off(struct super_block *sb, int type)
1169957153fcSJan Kara {
1170f3c1c42eSBaokun Li 	BUG_ON(type > EXT4_MAXQUOTAS);
1171957153fcSJan Kara 
1172957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
1173f3c1c42eSBaokun Li 	for (type--; type >= 0; type--)
1174957153fcSJan Kara 		ext4_quota_off(sb, type);
1175957153fcSJan Kara }
117633458eabSTheodore Ts'o 
117733458eabSTheodore Ts'o /*
117833458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
117933458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
118033458eabSTheodore Ts'o  */
118133458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
118233458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
118333458eabSTheodore Ts'o 				int type)
118433458eabSTheodore Ts'o {
118533458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
118633458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
118733458eabSTheodore Ts'o }
1188957153fcSJan Kara #else
1189f3c1c42eSBaokun Li static inline void ext4_quotas_off(struct super_block *sb, int type)
1190957153fcSJan Kara {
1191957153fcSJan Kara }
1192957153fcSJan Kara #endif
1193957153fcSJan Kara 
11941f79467cSJason Yan static int ext4_percpu_param_init(struct ext4_sb_info *sbi)
11951f79467cSJason Yan {
11961f79467cSJason Yan 	ext4_fsblk_t block;
11971f79467cSJason Yan 	int err;
11981f79467cSJason Yan 
11991f79467cSJason Yan 	block = ext4_count_free_clusters(sbi->s_sb);
12001f79467cSJason Yan 	ext4_free_blocks_count_set(sbi->s_es, EXT4_C2B(sbi, block));
12011f79467cSJason Yan 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
12021f79467cSJason Yan 				  GFP_KERNEL);
12031f79467cSJason Yan 	if (!err) {
12041f79467cSJason Yan 		unsigned long freei = ext4_count_free_inodes(sbi->s_sb);
12051f79467cSJason Yan 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
12061f79467cSJason Yan 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
12071f79467cSJason Yan 					  GFP_KERNEL);
12081f79467cSJason Yan 	}
12091f79467cSJason Yan 	if (!err)
12101f79467cSJason Yan 		err = percpu_counter_init(&sbi->s_dirs_counter,
12111f79467cSJason Yan 					  ext4_count_dirs(sbi->s_sb), GFP_KERNEL);
12121f79467cSJason Yan 	if (!err)
12131f79467cSJason Yan 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
12141f79467cSJason Yan 					  GFP_KERNEL);
12151f79467cSJason Yan 	if (!err)
12161f79467cSJason Yan 		err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
12171f79467cSJason Yan 					  GFP_KERNEL);
12181f79467cSJason Yan 	if (!err)
12191f79467cSJason Yan 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
12201f79467cSJason Yan 
12211f79467cSJason Yan 	if (err)
12221f79467cSJason Yan 		ext4_msg(sbi->s_sb, KERN_ERR, "insufficient memory");
12231f79467cSJason Yan 
12241f79467cSJason Yan 	return err;
12251f79467cSJason Yan }
12261f79467cSJason Yan 
12271f79467cSJason Yan static void ext4_percpu_param_destroy(struct ext4_sb_info *sbi)
12281f79467cSJason Yan {
12291f79467cSJason Yan 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
12301f79467cSJason Yan 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
12311f79467cSJason Yan 	percpu_counter_destroy(&sbi->s_dirs_counter);
12321f79467cSJason Yan 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
12331f79467cSJason Yan 	percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
12341f79467cSJason Yan 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
12351f79467cSJason Yan }
12361f79467cSJason Yan 
12376ef68498SJason Yan static void ext4_group_desc_free(struct ext4_sb_info *sbi)
12386ef68498SJason Yan {
12396ef68498SJason Yan 	struct buffer_head **group_desc;
12406ef68498SJason Yan 	int i;
12416ef68498SJason Yan 
12426ef68498SJason Yan 	rcu_read_lock();
12436ef68498SJason Yan 	group_desc = rcu_dereference(sbi->s_group_desc);
12446ef68498SJason Yan 	for (i = 0; i < sbi->s_gdb_count; i++)
12456ef68498SJason Yan 		brelse(group_desc[i]);
12466ef68498SJason Yan 	kvfree(group_desc);
12476ef68498SJason Yan 	rcu_read_unlock();
12486ef68498SJason Yan }
12496ef68498SJason Yan 
1250dcbf8758SJason Yan static void ext4_flex_groups_free(struct ext4_sb_info *sbi)
1251dcbf8758SJason Yan {
1252dcbf8758SJason Yan 	struct flex_groups **flex_groups;
1253dcbf8758SJason Yan 	int i;
1254dcbf8758SJason Yan 
1255dcbf8758SJason Yan 	rcu_read_lock();
1256dcbf8758SJason Yan 	flex_groups = rcu_dereference(sbi->s_flex_groups);
1257dcbf8758SJason Yan 	if (flex_groups) {
1258dcbf8758SJason Yan 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1259dcbf8758SJason Yan 			kvfree(flex_groups[i]);
1260dcbf8758SJason Yan 		kvfree(flex_groups);
1261dcbf8758SJason Yan 	}
1262dcbf8758SJason Yan 	rcu_read_unlock();
1263dcbf8758SJason Yan }
1264dcbf8758SJason Yan 
1265617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1266ac27a0ecSDave Kleikamp {
1267617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1268617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
126997abd7d4STheodore Ts'o 	int aborted = 0;
1270856dd6c5SNathan Chancellor 	int err;
1271ac27a0ecSDave Kleikamp 
1272b98535d0SYe Bin 	/*
1273b98535d0SYe Bin 	 * Unregister sysfs before destroying jbd2 journal.
1274b98535d0SYe Bin 	 * Since we could still access attr_journal_task attribute via sysfs
1275b98535d0SYe Bin 	 * path which could have sbi->s_journal->j_task as NULL
1276b98535d0SYe Bin 	 * Unregister sysfs before flush sbi->s_error_work.
1277b98535d0SYe Bin 	 * Since user may read /proc/fs/ext4/xx/mb_groups during umount, If
1278b98535d0SYe Bin 	 * read metadata verify failed then will queue error work.
1279b98535d0SYe Bin 	 * flush_stashed_error_work will call start_this_handle may trigger
1280b98535d0SYe Bin 	 * BUG_ON.
1281b98535d0SYe Bin 	 */
1282b98535d0SYe Bin 	ext4_unregister_sysfs(sb);
1283b98535d0SYe Bin 
12844808cb5bSZhang Yi 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs unmount"))
1285bb0fbc78SLukas Czerner 		ext4_msg(sb, KERN_INFO, "unmounting filesystem %pU.",
1286bb0fbc78SLukas Czerner 			 &sb->s_uuid);
12874808cb5bSZhang Yi 
1288857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1289f3c1c42eSBaokun Li 	ext4_quotas_off(sb, EXT4_MAXQUOTAS);
1290e0ccfd95SChristoph Hellwig 
1291c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
12922e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
129302f310fcSJan Kara 	ext4_release_orphan_info(sb);
12944c0425ffSMingming Cao 
12950390131bSFrank Mayhar 	if (sbi->s_journal) {
129697abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1297ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
129847b4a50bSJan Kara 		sbi->s_journal = NULL;
1299878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
130054d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
13010390131bSFrank Mayhar 		}
1302878520acSTheodore Ts'o 	}
1303d4edac31SJosef Bacik 
1304d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
1305292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&sbi->s_err_report);
1306d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1307d4edac31SJosef Bacik 	ext4_mb_release(sb);
1308d4edac31SJosef Bacik 	ext4_ext_release(sb);
1309d4edac31SJosef Bacik 
1310bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1311e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
131202f310fcSJan Kara 		ext4_clear_feature_orphan_present(sb);
1313ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1314ac27a0ecSDave Kleikamp 	}
1315bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
13164392fbc4SJan Kara 		ext4_commit_super(sb);
1317a8e25a83SArtem Bityutskiy 
13186ef68498SJason Yan 	ext4_group_desc_free(sbi);
1319dcbf8758SJason Yan 	ext4_flex_groups_free(sbi);
13201f79467cSJason Yan 	ext4_percpu_param_destroy(sbi);
1321ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1322856dd6c5SNathan Chancellor 	for (int i = 0; i < EXT4_MAXQUOTAS; i++)
132333458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1324ac27a0ecSDave Kleikamp #endif
1325ac27a0ecSDave Kleikamp 
1326ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1327ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1328ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1329ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1330ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1331ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1332837c23fbSChunguang Xu 	ASSERT(list_empty(&sbi->s_orphan));
1333ac27a0ecSDave Kleikamp 
133489d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1335f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
133693e92cfcSZhihao Cheng 	if (sbi->s_journal_bdev) {
1337ee7ed3aaSChunguang Xu 		sync_blockdev(sbi->s_journal_bdev);
1338617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1339ac27a0ecSDave Kleikamp 	}
134050c15df6SChengguang Xu 
1341dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1342dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
134350c15df6SChengguang Xu 
134447387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
134547387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
134650c15df6SChengguang Xu 
1347618f0031SPavel Skripkin 	ext4_stop_mmpd(sbi);
1348618f0031SPavel Skripkin 
13499060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1350ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
13513197ebdbSTheodore Ts'o 	/*
13523197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
13533197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
13543197ebdbSTheodore Ts'o 	 */
13553197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
13563197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
13570441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
13580441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1359705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
13608012b866SShiyang Ruan 	fs_put_dax(sbi->s_daxdev, NULL);
1361ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
13625298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1363f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
1364c83ad55eSGabriel Krisman Bertazi #endif
1365ac27a0ecSDave Kleikamp 	kfree(sbi);
1366ac27a0ecSDave Kleikamp }
1367ac27a0ecSDave Kleikamp 
1368e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1369ac27a0ecSDave Kleikamp 
1370ac27a0ecSDave Kleikamp /*
1371ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1372ac27a0ecSDave Kleikamp  */
1373617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1374ac27a0ecSDave Kleikamp {
1375617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1376ac27a0ecSDave Kleikamp 
1377fd60b288SMuchun Song 	ei = alloc_inode_sb(sb, ext4_inode_cachep, GFP_NOFS);
1378ac27a0ecSDave Kleikamp 	if (!ei)
1379ac27a0ecSDave Kleikamp 		return NULL;
13800b8e58a1SAndreas Dilger 
1381ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
13827ea71af9SYe Bin 	ei->i_flags = 0;
1383202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
138438727786SOjaswin Mujoo 	ei->i_prealloc_node = RB_ROOT;
138527bc446eSbrookxu 	atomic_set(&ei->i_prealloc_active, 0);
138638727786SOjaswin Mujoo 	rwlock_init(&ei->i_prealloc_lock);
13879a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
13889a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1389edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1390eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1391edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1392dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1393d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1394d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
13951dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1396a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1397a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
139896c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1399a9e7f447SDmitry Monakhov #endif
14008aefcd55STheodore Ts'o 	ei->jinode = NULL;
14012e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1402744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1403b436b9beSJan Kara 	ei->i_sync_tid = 0;
1404b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1405e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
14062e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1407aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1408aa75f4d3SHarshad Shirwadkar 	mutex_init(&ei->i_fc_lock);
1409ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1410ac27a0ecSDave Kleikamp }
1411ac27a0ecSDave Kleikamp 
14127ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
14137ff9c073STheodore Ts'o {
14147ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
14157ff9c073STheodore Ts'o 
141629b3692eSEric Biggers 	if (!drop)
141729b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
141829b3692eSEric Biggers 
14197ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
14207ff9c073STheodore Ts'o 	return drop;
14217ff9c073STheodore Ts'o }
14227ff9c073STheodore Ts'o 
142394053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1424fa0d7e3dSNick Piggin {
14252c58d548SEric Biggers 	fscrypt_free_inode(inode);
1426aa75f4d3SHarshad Shirwadkar 	if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1427aa75f4d3SHarshad Shirwadkar 		pr_warn("%s: inode %ld still in fc list",
1428aa75f4d3SHarshad Shirwadkar 			__func__, inode->i_ino);
1429aa75f4d3SHarshad Shirwadkar 	}
1430fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1431fa0d7e3dSNick Piggin }
1432fa0d7e3dSNick Piggin 
1433617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1434ac27a0ecSDave Kleikamp {
14359f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1436b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1437b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1438b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
14399f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
14409f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
14419f7dd93dSVasily Averin 				true);
14429f7dd93dSVasily Averin 		dump_stack();
14439f7dd93dSVasily Averin 	}
14446fed8395SJeffle Xu 
14456fed8395SJeffle Xu 	if (EXT4_I(inode)->i_reserved_data_blocks)
14466fed8395SJeffle Xu 		ext4_msg(inode->i_sb, KERN_ERR,
14476fed8395SJeffle Xu 			 "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
14486fed8395SJeffle Xu 			 inode->i_ino, EXT4_I(inode),
14496fed8395SJeffle Xu 			 EXT4_I(inode)->i_reserved_data_blocks);
1450ac27a0ecSDave Kleikamp }
1451ac27a0ecSDave Kleikamp 
145251cc5068SAlexey Dobriyan static void init_once(void *foo)
1453ac27a0ecSDave Kleikamp {
1454c30365b9SYu Zhe 	struct ext4_inode_info *ei = foo;
1455ac27a0ecSDave Kleikamp 
1456ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1457ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
14580e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1459ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1460aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1461ac27a0ecSDave Kleikamp }
1462ac27a0ecSDave Kleikamp 
1463e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1464ac27a0ecSDave Kleikamp {
1465f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1466f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1467f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1468f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1469f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1470f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
147120c2df83SPaul Mundt 				init_once);
1472617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1473ac27a0ecSDave Kleikamp 		return -ENOMEM;
1474ac27a0ecSDave Kleikamp 	return 0;
1475ac27a0ecSDave Kleikamp }
1476ac27a0ecSDave Kleikamp 
1477ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1478ac27a0ecSDave Kleikamp {
14798c0a8537SKirill A. Shutemov 	/*
14808c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
14818c0a8537SKirill A. Shutemov 	 * destroy cache.
14828c0a8537SKirill A. Shutemov 	 */
14838c0a8537SKirill A. Shutemov 	rcu_barrier();
1484617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1485ac27a0ecSDave Kleikamp }
1486ac27a0ecSDave Kleikamp 
14870930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1488ac27a0ecSDave Kleikamp {
1489aa75f4d3SHarshad Shirwadkar 	ext4_fc_del(inode);
14900930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1491dbd5768fSJan Kara 	clear_inode(inode);
149227bc446eSbrookxu 	ext4_discard_preallocations(inode, 0);
149351865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1494f4c2d372SJan Kara 	dquot_drop(inode);
14958aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
14968aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
14978aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
14988aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
14998aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
15008aefcd55STheodore Ts'o 	}
15013d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1502c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1503ac27a0ecSDave Kleikamp }
1504ac27a0ecSDave Kleikamp 
15051b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
15061b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1507ac27a0ecSDave Kleikamp {
1508ac27a0ecSDave Kleikamp 	struct inode *inode;
1509ac27a0ecSDave Kleikamp 
15108a363970STheodore Ts'o 	/*
1511ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1512ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1513ac27a0ecSDave Kleikamp 	 */
15148a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
15151d1fe1eeSDavid Howells 	if (IS_ERR(inode))
15161d1fe1eeSDavid Howells 		return ERR_CAST(inode);
15171d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1518ac27a0ecSDave Kleikamp 		iput(inode);
1519ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1520ac27a0ecSDave Kleikamp 	}
15211b961ac0SChristoph Hellwig 
15221b961ac0SChristoph Hellwig 	return inode;
1523ac27a0ecSDave Kleikamp }
15241b961ac0SChristoph Hellwig 
15251b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
15261b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
15271b961ac0SChristoph Hellwig {
15281b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
15291b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
15301b961ac0SChristoph Hellwig }
15311b961ac0SChristoph Hellwig 
15321b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
15331b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
15341b961ac0SChristoph Hellwig {
15351b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
15361b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1537ac27a0ecSDave Kleikamp }
1538ac27a0ecSDave Kleikamp 
1539fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1540fde87268STheodore Ts'o {
1541fde87268STheodore Ts'o 	struct writeback_control wbc = {
1542fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1543fde87268STheodore Ts'o 	};
1544fde87268STheodore Ts'o 
1545fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1546fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1547fde87268STheodore Ts'o }
1548fde87268STheodore Ts'o 
1549ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1550d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1551689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1552ac27a0ecSDave Kleikamp 
1553617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1554617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1555617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1556617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1557617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
15586f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
15598c54ca9cSAl Viro 			 const struct path *path);
1560617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1561ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1562617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1563ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
15647c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
15657c319d32SAditya Kali 			     unsigned int flags);
1566ac27a0ecSDave Kleikamp 
156796c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
156896c7e0d9SJan Kara {
156996c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
157096c7e0d9SJan Kara }
157196c7e0d9SJan Kara 
157261e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
157360e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1574617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1575617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1576617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1577617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1578a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1579a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1580a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1581040cb378SLi Xi 	.get_projid		= ext4_get_projid,
15827a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1583ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1584ac27a0ecSDave Kleikamp };
1585ac27a0ecSDave Kleikamp 
15860d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1587617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1588ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1589287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
15900a240339SJan Kara 	.get_state	= dquot_get_state,
1591287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1592287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
15936332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
15946332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1595ac27a0ecSDave Kleikamp };
1596ac27a0ecSDave Kleikamp #endif
1597ac27a0ecSDave Kleikamp 
1598ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1599617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
160094053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1601617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1602617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1603617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
16047ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
16050930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1606617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1607617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1608c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1609c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1610617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1611617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1612ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1613617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1614617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
161596c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1616ac27a0ecSDave Kleikamp #endif
1617ac27a0ecSDave Kleikamp };
1618ac27a0ecSDave Kleikamp 
161939655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
16201b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
16211b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1622617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1623fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1624ac27a0ecSDave Kleikamp };
1625ac27a0ecSDave Kleikamp 
1626ac27a0ecSDave Kleikamp enum {
1627ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1628ba2e524dSLukas Czerner 	Opt_resgid, Opt_resuid, Opt_sb,
162972578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
16302d544ec9SYang Xu 	Opt_user_xattr, Opt_acl,
163172578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1632ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1633ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1634ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
16356ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
16364f74d15fSEric Biggers 	Opt_inlinecrypt,
1637ba2e524dSLukas Czerner 	Opt_usrjquota, Opt_grpjquota, Opt_quota,
1638ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
16391ff20307SJeff Layton 	Opt_usrquota, Opt_grpquota, Opt_prjquota,
16409cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1641327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
16424437992bSLukas Czerner 	Opt_nowarn_on_error, Opt_mblk_io_submit, Opt_debug_want_extra_isize,
16431449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
16445328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1645744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1646fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1647cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
164821175ca4SHarshad Shirwadkar 	Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
1649e5a185c2SLukas Czerner 	Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
16508016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
165199c880deSHarshad Shirwadkar 	Opt_fc_debug_max_replay, Opt_fc_debug_force
16528016e29fSHarshad Shirwadkar #endif
1653ac27a0ecSDave Kleikamp };
1654ac27a0ecSDave Kleikamp 
1655e5a185c2SLukas Czerner static const struct constant_table ext4_param_errors[] = {
1656ba2e524dSLukas Czerner 	{"continue",	EXT4_MOUNT_ERRORS_CONT},
1657ba2e524dSLukas Czerner 	{"panic",	EXT4_MOUNT_ERRORS_PANIC},
1658ba2e524dSLukas Czerner 	{"remount-ro",	EXT4_MOUNT_ERRORS_RO},
1659e5a185c2SLukas Czerner 	{}
1660e5a185c2SLukas Czerner };
1661e5a185c2SLukas Czerner 
1662e5a185c2SLukas Czerner static const struct constant_table ext4_param_data[] = {
1663ba2e524dSLukas Czerner 	{"journal",	EXT4_MOUNT_JOURNAL_DATA},
1664ba2e524dSLukas Czerner 	{"ordered",	EXT4_MOUNT_ORDERED_DATA},
1665ba2e524dSLukas Czerner 	{"writeback",	EXT4_MOUNT_WRITEBACK_DATA},
1666e5a185c2SLukas Czerner 	{}
1667e5a185c2SLukas Czerner };
1668e5a185c2SLukas Czerner 
1669e5a185c2SLukas Czerner static const struct constant_table ext4_param_data_err[] = {
1670e5a185c2SLukas Czerner 	{"abort",	Opt_data_err_abort},
1671e5a185c2SLukas Czerner 	{"ignore",	Opt_data_err_ignore},
1672e5a185c2SLukas Czerner 	{}
1673e5a185c2SLukas Czerner };
1674e5a185c2SLukas Czerner 
1675e5a185c2SLukas Czerner static const struct constant_table ext4_param_jqfmt[] = {
1676ba2e524dSLukas Czerner 	{"vfsold",	QFMT_VFS_OLD},
1677ba2e524dSLukas Czerner 	{"vfsv0",	QFMT_VFS_V0},
1678ba2e524dSLukas Czerner 	{"vfsv1",	QFMT_VFS_V1},
1679e5a185c2SLukas Czerner 	{}
1680e5a185c2SLukas Czerner };
1681e5a185c2SLukas Czerner 
1682e5a185c2SLukas Czerner static const struct constant_table ext4_param_dax[] = {
1683e5a185c2SLukas Czerner 	{"always",	Opt_dax_always},
1684e5a185c2SLukas Czerner 	{"inode",	Opt_dax_inode},
1685e5a185c2SLukas Czerner 	{"never",	Opt_dax_never},
1686e5a185c2SLukas Czerner 	{}
1687e5a185c2SLukas Czerner };
1688e5a185c2SLukas Czerner 
1689e5a185c2SLukas Czerner /* String parameter that allows empty argument */
1690e5a185c2SLukas Czerner #define fsparam_string_empty(NAME, OPT) \
1691e5a185c2SLukas Czerner 	__fsparam(fs_param_is_string, NAME, OPT, fs_param_can_be_empty, NULL)
1692e5a185c2SLukas Czerner 
1693e5a185c2SLukas Czerner /*
1694e5a185c2SLukas Czerner  * Mount option specification
1695e5a185c2SLukas Czerner  * We don't use fsparam_flag_no because of the way we set the
1696e5a185c2SLukas Czerner  * options and the way we show them in _ext4_show_options(). To
1697e5a185c2SLukas Czerner  * keep the changes to a minimum, let's keep the negative options
1698e5a185c2SLukas Czerner  * separate for now.
1699e5a185c2SLukas Czerner  */
1700e5a185c2SLukas Czerner static const struct fs_parameter_spec ext4_param_specs[] = {
1701e5a185c2SLukas Czerner 	fsparam_flag	("bsddf",		Opt_bsd_df),
1702e5a185c2SLukas Czerner 	fsparam_flag	("minixdf",		Opt_minix_df),
1703e5a185c2SLukas Czerner 	fsparam_flag	("grpid",		Opt_grpid),
1704e5a185c2SLukas Czerner 	fsparam_flag	("bsdgroups",		Opt_grpid),
1705e5a185c2SLukas Czerner 	fsparam_flag	("nogrpid",		Opt_nogrpid),
1706e5a185c2SLukas Czerner 	fsparam_flag	("sysvgroups",		Opt_nogrpid),
1707e5a185c2SLukas Czerner 	fsparam_u32	("resgid",		Opt_resgid),
1708e5a185c2SLukas Czerner 	fsparam_u32	("resuid",		Opt_resuid),
1709e5a185c2SLukas Czerner 	fsparam_u32	("sb",			Opt_sb),
1710e5a185c2SLukas Czerner 	fsparam_enum	("errors",		Opt_errors, ext4_param_errors),
1711e5a185c2SLukas Czerner 	fsparam_flag	("nouid32",		Opt_nouid32),
1712e5a185c2SLukas Czerner 	fsparam_flag	("debug",		Opt_debug),
1713e5a185c2SLukas Czerner 	fsparam_flag	("oldalloc",		Opt_removed),
1714e5a185c2SLukas Czerner 	fsparam_flag	("orlov",		Opt_removed),
1715e5a185c2SLukas Czerner 	fsparam_flag	("user_xattr",		Opt_user_xattr),
1716e5a185c2SLukas Czerner 	fsparam_flag	("acl",			Opt_acl),
1717e5a185c2SLukas Czerner 	fsparam_flag	("norecovery",		Opt_noload),
1718e5a185c2SLukas Czerner 	fsparam_flag	("noload",		Opt_noload),
1719e5a185c2SLukas Czerner 	fsparam_flag	("bh",			Opt_removed),
1720e5a185c2SLukas Czerner 	fsparam_flag	("nobh",		Opt_removed),
1721e5a185c2SLukas Czerner 	fsparam_u32	("commit",		Opt_commit),
1722e5a185c2SLukas Czerner 	fsparam_u32	("min_batch_time",	Opt_min_batch_time),
1723e5a185c2SLukas Czerner 	fsparam_u32	("max_batch_time",	Opt_max_batch_time),
1724e5a185c2SLukas Czerner 	fsparam_u32	("journal_dev",		Opt_journal_dev),
1725e5a185c2SLukas Czerner 	fsparam_bdev	("journal_path",	Opt_journal_path),
1726e5a185c2SLukas Czerner 	fsparam_flag	("journal_checksum",	Opt_journal_checksum),
1727e5a185c2SLukas Czerner 	fsparam_flag	("nojournal_checksum",	Opt_nojournal_checksum),
1728e5a185c2SLukas Czerner 	fsparam_flag	("journal_async_commit",Opt_journal_async_commit),
1729e5a185c2SLukas Czerner 	fsparam_flag	("abort",		Opt_abort),
1730e5a185c2SLukas Czerner 	fsparam_enum	("data",		Opt_data, ext4_param_data),
1731e5a185c2SLukas Czerner 	fsparam_enum	("data_err",		Opt_data_err,
1732e5a185c2SLukas Czerner 						ext4_param_data_err),
1733e5a185c2SLukas Czerner 	fsparam_string_empty
1734e5a185c2SLukas Czerner 			("usrjquota",		Opt_usrjquota),
1735e5a185c2SLukas Czerner 	fsparam_string_empty
1736e5a185c2SLukas Czerner 			("grpjquota",		Opt_grpjquota),
1737e5a185c2SLukas Czerner 	fsparam_enum	("jqfmt",		Opt_jqfmt, ext4_param_jqfmt),
1738e5a185c2SLukas Czerner 	fsparam_flag	("grpquota",		Opt_grpquota),
1739e5a185c2SLukas Czerner 	fsparam_flag	("quota",		Opt_quota),
1740e5a185c2SLukas Czerner 	fsparam_flag	("noquota",		Opt_noquota),
1741e5a185c2SLukas Czerner 	fsparam_flag	("usrquota",		Opt_usrquota),
1742e5a185c2SLukas Czerner 	fsparam_flag	("prjquota",		Opt_prjquota),
1743e5a185c2SLukas Czerner 	fsparam_flag	("barrier",		Opt_barrier),
1744e5a185c2SLukas Czerner 	fsparam_u32	("barrier",		Opt_barrier),
1745e5a185c2SLukas Czerner 	fsparam_flag	("nobarrier",		Opt_nobarrier),
17461ff20307SJeff Layton 	fsparam_flag	("i_version",		Opt_removed),
1747e5a185c2SLukas Czerner 	fsparam_flag	("dax",			Opt_dax),
1748e5a185c2SLukas Czerner 	fsparam_enum	("dax",			Opt_dax_type, ext4_param_dax),
1749e5a185c2SLukas Czerner 	fsparam_u32	("stripe",		Opt_stripe),
1750e5a185c2SLukas Czerner 	fsparam_flag	("delalloc",		Opt_delalloc),
1751e5a185c2SLukas Czerner 	fsparam_flag	("nodelalloc",		Opt_nodelalloc),
1752e5a185c2SLukas Czerner 	fsparam_flag	("warn_on_error",	Opt_warn_on_error),
1753e5a185c2SLukas Czerner 	fsparam_flag	("nowarn_on_error",	Opt_nowarn_on_error),
1754e5a185c2SLukas Czerner 	fsparam_u32	("debug_want_extra_isize",
1755e5a185c2SLukas Czerner 						Opt_debug_want_extra_isize),
1756e5a185c2SLukas Czerner 	fsparam_flag	("mblk_io_submit",	Opt_removed),
1757e5a185c2SLukas Czerner 	fsparam_flag	("nomblk_io_submit",	Opt_removed),
1758e5a185c2SLukas Czerner 	fsparam_flag	("block_validity",	Opt_block_validity),
1759e5a185c2SLukas Czerner 	fsparam_flag	("noblock_validity",	Opt_noblock_validity),
1760e5a185c2SLukas Czerner 	fsparam_u32	("inode_readahead_blks",
1761e5a185c2SLukas Czerner 						Opt_inode_readahead_blks),
1762e5a185c2SLukas Czerner 	fsparam_u32	("journal_ioprio",	Opt_journal_ioprio),
1763e5a185c2SLukas Czerner 	fsparam_u32	("auto_da_alloc",	Opt_auto_da_alloc),
1764e5a185c2SLukas Czerner 	fsparam_flag	("auto_da_alloc",	Opt_auto_da_alloc),
1765e5a185c2SLukas Czerner 	fsparam_flag	("noauto_da_alloc",	Opt_noauto_da_alloc),
1766e5a185c2SLukas Czerner 	fsparam_flag	("dioread_nolock",	Opt_dioread_nolock),
1767e5a185c2SLukas Czerner 	fsparam_flag	("nodioread_nolock",	Opt_dioread_lock),
1768e5a185c2SLukas Czerner 	fsparam_flag	("dioread_lock",	Opt_dioread_lock),
1769e5a185c2SLukas Czerner 	fsparam_flag	("discard",		Opt_discard),
1770e5a185c2SLukas Czerner 	fsparam_flag	("nodiscard",		Opt_nodiscard),
1771e5a185c2SLukas Czerner 	fsparam_u32	("init_itable",		Opt_init_itable),
1772e5a185c2SLukas Czerner 	fsparam_flag	("init_itable",		Opt_init_itable),
1773e5a185c2SLukas Czerner 	fsparam_flag	("noinit_itable",	Opt_noinit_itable),
1774e5a185c2SLukas Czerner #ifdef CONFIG_EXT4_DEBUG
1775e5a185c2SLukas Czerner 	fsparam_flag	("fc_debug_force",	Opt_fc_debug_force),
1776e5a185c2SLukas Czerner 	fsparam_u32	("fc_debug_max_replay",	Opt_fc_debug_max_replay),
1777e5a185c2SLukas Czerner #endif
1778e5a185c2SLukas Czerner 	fsparam_u32	("max_dir_size_kb",	Opt_max_dir_size_kb),
1779e5a185c2SLukas Czerner 	fsparam_flag	("test_dummy_encryption",
1780e5a185c2SLukas Czerner 						Opt_test_dummy_encryption),
1781e5a185c2SLukas Czerner 	fsparam_string	("test_dummy_encryption",
1782e5a185c2SLukas Czerner 						Opt_test_dummy_encryption),
1783e5a185c2SLukas Czerner 	fsparam_flag	("inlinecrypt",		Opt_inlinecrypt),
1784e5a185c2SLukas Czerner 	fsparam_flag	("nombcache",		Opt_nombcache),
1785e5a185c2SLukas Czerner 	fsparam_flag	("no_mbcache",		Opt_nombcache),	/* for backward compatibility */
1786e5a185c2SLukas Czerner 	fsparam_flag	("prefetch_block_bitmaps",
1787e5a185c2SLukas Czerner 						Opt_removed),
1788e5a185c2SLukas Czerner 	fsparam_flag	("no_prefetch_block_bitmaps",
1789e5a185c2SLukas Czerner 						Opt_no_prefetch_block_bitmaps),
1790e5a185c2SLukas Czerner 	fsparam_s32	("mb_optimize_scan",	Opt_mb_optimize_scan),
1791e5a185c2SLukas Czerner 	fsparam_string	("check",		Opt_removed),	/* mount option from ext2/3 */
1792e5a185c2SLukas Czerner 	fsparam_flag	("nocheck",		Opt_removed),	/* mount option from ext2/3 */
1793e5a185c2SLukas Czerner 	fsparam_flag	("reservation",		Opt_removed),	/* mount option from ext2/3 */
1794e5a185c2SLukas Czerner 	fsparam_flag	("noreservation",	Opt_removed),	/* mount option from ext2/3 */
1795e5a185c2SLukas Czerner 	fsparam_u32	("journal",		Opt_removed),	/* mount option from ext2/3 */
1796e5a185c2SLukas Czerner 	{}
1797e5a185c2SLukas Czerner };
1798e5a185c2SLukas Czerner 
1799b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1800196e402aSHarshad Shirwadkar 
180126092bf5STheodore Ts'o #define MOPT_SET	0x0001
180226092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
180326092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
180426092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
180526092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
180626092bf5STheodore Ts'o #define MOPT_Q		0
1807ba2e524dSLukas Czerner #define MOPT_QFMT	0x0010
180826092bf5STheodore Ts'o #else
180926092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
181026092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
181126092bf5STheodore Ts'o #endif
1812ba2e524dSLukas Czerner #define MOPT_NO_EXT2	0x0020
1813ba2e524dSLukas Czerner #define MOPT_NO_EXT3	0x0040
18148dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1815ba2e524dSLukas Czerner #define MOPT_SKIP	0x0080
1816ba2e524dSLukas Czerner #define	MOPT_2		0x0100
181726092bf5STheodore Ts'o 
181826092bf5STheodore Ts'o static const struct mount_opts {
181926092bf5STheodore Ts'o 	int	token;
182026092bf5STheodore Ts'o 	int	mount_opt;
182126092bf5STheodore Ts'o 	int	flags;
182226092bf5STheodore Ts'o } ext4_mount_opts[] = {
182326092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
182426092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
182526092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
182626092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
182726092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
182826092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
18298dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
18308dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
18318dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
18328dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
183326092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
183426092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
18358dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
18368dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
18378dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
183859d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1839327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1840327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1841cb8435dcSEric Biggers 	{Opt_commit, 0, MOPT_NO_EXT2},
1842c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1843c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
18448dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
18451e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
184626092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
18478dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
18481e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
18498dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
1850ba2e524dSLukas Czerner 	{Opt_data_err, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_NO_EXT2},
185126092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
185226092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
185326092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
185426092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
185526092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1856ba2e524dSLukas Czerner 	{Opt_dax_type, 0, MOPT_EXT4_ONLY},
1857ba2e524dSLukas Czerner 	{Opt_journal_dev, 0, MOPT_NO_EXT2},
1858ba2e524dSLukas Czerner 	{Opt_journal_path, 0, MOPT_NO_EXT2},
1859ba2e524dSLukas Czerner 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2},
1860ba2e524dSLukas Czerner 	{Opt_data, 0, MOPT_NO_EXT2},
186126092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
186226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
186326092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
186426092bf5STheodore Ts'o #else
186526092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
186626092bf5STheodore Ts'o #endif
186726092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
186826092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
186926092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
187026092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
187126092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
187226092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
187326092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
187449da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
187549da9392SJan Kara 							MOPT_SET | MOPT_Q},
187626092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
187749da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
187849da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
1879ba2e524dSLukas Czerner 	{Opt_usrjquota, 0, MOPT_Q},
1880ba2e524dSLukas Czerner 	{Opt_grpjquota, 0, MOPT_Q},
1881ba2e524dSLukas Czerner 	{Opt_jqfmt, 0, MOPT_QFMT},
1882cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
188321175ca4SHarshad Shirwadkar 	{Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
18843d392b26STheodore Ts'o 	 MOPT_SET},
188599c880deSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
18860f0672ffSHarshad Shirwadkar 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
18870f0672ffSHarshad Shirwadkar 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
18888016e29fSHarshad Shirwadkar #endif
188926092bf5STheodore Ts'o 	{Opt_err, 0, 0}
189026092bf5STheodore Ts'o };
189126092bf5STheodore Ts'o 
18925298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1893c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1894c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1895c83ad55eSGabriel Krisman Bertazi 	char *name;
189649bd03ccSChristoph Hellwig 	unsigned int version;
1897c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
189849bd03ccSChristoph Hellwig 	{EXT4_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
1899c83ad55eSGabriel Krisman Bertazi };
1900c83ad55eSGabriel Krisman Bertazi 
1901aa8bf298SChristoph Hellwig static const struct ext4_sb_encodings *
1902aa8bf298SChristoph Hellwig ext4_sb_read_encoding(const struct ext4_super_block *es)
1903c83ad55eSGabriel Krisman Bertazi {
1904c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1905c83ad55eSGabriel Krisman Bertazi 	int i;
1906c83ad55eSGabriel Krisman Bertazi 
1907c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1908c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1909aa8bf298SChristoph Hellwig 			return &ext4_sb_encoding_map[i];
1910c83ad55eSGabriel Krisman Bertazi 
1911aa8bf298SChristoph Hellwig 	return NULL;
1912c83ad55eSGabriel Krisman Bertazi }
1913c83ad55eSGabriel Krisman Bertazi #endif
1914c83ad55eSGabriel Krisman Bertazi 
19156e47a3ccSLukas Czerner #define EXT4_SPEC_JQUOTA			(1 <<  0)
19166e47a3ccSLukas Czerner #define EXT4_SPEC_JQFMT				(1 <<  1)
19176e47a3ccSLukas Czerner #define EXT4_SPEC_DATAJ				(1 <<  2)
19186e47a3ccSLukas Czerner #define EXT4_SPEC_SB_BLOCK			(1 <<  3)
19196e47a3ccSLukas Czerner #define EXT4_SPEC_JOURNAL_DEV			(1 <<  4)
19206e47a3ccSLukas Czerner #define EXT4_SPEC_JOURNAL_IOPRIO		(1 <<  5)
19216e47a3ccSLukas Czerner #define EXT4_SPEC_s_want_extra_isize		(1 <<  7)
19226e47a3ccSLukas Czerner #define EXT4_SPEC_s_max_batch_time		(1 <<  8)
19236e47a3ccSLukas Czerner #define EXT4_SPEC_s_min_batch_time		(1 <<  9)
19246e47a3ccSLukas Czerner #define EXT4_SPEC_s_inode_readahead_blks	(1 << 10)
19256e47a3ccSLukas Czerner #define EXT4_SPEC_s_li_wait_mult		(1 << 11)
19266e47a3ccSLukas Czerner #define EXT4_SPEC_s_max_dir_size_kb		(1 << 12)
19276e47a3ccSLukas Czerner #define EXT4_SPEC_s_stripe			(1 << 13)
19286e47a3ccSLukas Czerner #define EXT4_SPEC_s_resuid			(1 << 14)
19296e47a3ccSLukas Czerner #define EXT4_SPEC_s_resgid			(1 << 15)
19306e47a3ccSLukas Czerner #define EXT4_SPEC_s_commit_interval		(1 << 16)
19316e47a3ccSLukas Czerner #define EXT4_SPEC_s_fc_debug_max_replay		(1 << 17)
19327edfd85bSLukas Czerner #define EXT4_SPEC_s_sb_block			(1 << 18)
193327b38686SOjaswin Mujoo #define EXT4_SPEC_mb_optimize_scan		(1 << 19)
19346e47a3ccSLukas Czerner 
1935461c3af0SLukas Czerner struct ext4_fs_context {
1936e6e268cbSLukas Czerner 	char		*s_qf_names[EXT4_MAXQUOTAS];
193785456054SEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy;
1938e6e268cbSLukas Czerner 	int		s_jquota_fmt;	/* Format of quota to use */
19396e47a3ccSLukas Czerner #ifdef CONFIG_EXT4_DEBUG
19406e47a3ccSLukas Czerner 	int s_fc_debug_max_replay;
19416e47a3ccSLukas Czerner #endif
19426e47a3ccSLukas Czerner 	unsigned short	qname_spec;
19436e47a3ccSLukas Czerner 	unsigned long	vals_s_flags;	/* Bits to set in s_flags */
19446e47a3ccSLukas Czerner 	unsigned long	mask_s_flags;	/* Bits changed in s_flags */
19456e47a3ccSLukas Czerner 	unsigned long	journal_devnum;
19466e47a3ccSLukas Czerner 	unsigned long	s_commit_interval;
19476e47a3ccSLukas Czerner 	unsigned long	s_stripe;
19486e47a3ccSLukas Czerner 	unsigned int	s_inode_readahead_blks;
19496e47a3ccSLukas Czerner 	unsigned int	s_want_extra_isize;
19506e47a3ccSLukas Czerner 	unsigned int	s_li_wait_mult;
19516e47a3ccSLukas Czerner 	unsigned int	s_max_dir_size_kb;
19526e47a3ccSLukas Czerner 	unsigned int	journal_ioprio;
19536e47a3ccSLukas Czerner 	unsigned int	vals_s_mount_opt;
19546e47a3ccSLukas Czerner 	unsigned int	mask_s_mount_opt;
19556e47a3ccSLukas Czerner 	unsigned int	vals_s_mount_opt2;
19566e47a3ccSLukas Czerner 	unsigned int	mask_s_mount_opt2;
1957e3952fccSLukas Czerner 	unsigned long	vals_s_mount_flags;
1958e3952fccSLukas Czerner 	unsigned long	mask_s_mount_flags;
1959b6bd2435SLukas Czerner 	unsigned int	opt_flags;	/* MOPT flags */
19606e47a3ccSLukas Czerner 	unsigned int	spec;
19616e47a3ccSLukas Czerner 	u32		s_max_batch_time;
19626e47a3ccSLukas Czerner 	u32		s_min_batch_time;
19636e47a3ccSLukas Czerner 	kuid_t		s_resuid;
19646e47a3ccSLukas Czerner 	kgid_t		s_resgid;
19657edfd85bSLukas Czerner 	ext4_fsblk_t	s_sb_block;
1966b237e304SHarshad Shirwadkar };
1967b237e304SHarshad Shirwadkar 
1968cebe85d5SLukas Czerner static void ext4_fc_free(struct fs_context *fc)
1969cebe85d5SLukas Czerner {
1970cebe85d5SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
1971cebe85d5SLukas Czerner 	int i;
1972cebe85d5SLukas Czerner 
1973cebe85d5SLukas Czerner 	if (!ctx)
1974cebe85d5SLukas Czerner 		return;
1975cebe85d5SLukas Czerner 
1976cebe85d5SLukas Czerner 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
1977cebe85d5SLukas Czerner 		kfree(ctx->s_qf_names[i]);
1978cebe85d5SLukas Czerner 
197985456054SEric Biggers 	fscrypt_free_dummy_policy(&ctx->dummy_enc_policy);
1980cebe85d5SLukas Czerner 	kfree(ctx);
1981cebe85d5SLukas Czerner }
1982cebe85d5SLukas Czerner 
1983cebe85d5SLukas Czerner int ext4_init_fs_context(struct fs_context *fc)
1984cebe85d5SLukas Czerner {
1985da9e4802SDan Carpenter 	struct ext4_fs_context *ctx;
1986cebe85d5SLukas Czerner 
1987cebe85d5SLukas Czerner 	ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
1988cebe85d5SLukas Czerner 	if (!ctx)
1989cebe85d5SLukas Czerner 		return -ENOMEM;
1990cebe85d5SLukas Czerner 
1991cebe85d5SLukas Czerner 	fc->fs_private = ctx;
1992cebe85d5SLukas Czerner 	fc->ops = &ext4_context_ops;
1993cebe85d5SLukas Czerner 
1994cebe85d5SLukas Czerner 	return 0;
1995cebe85d5SLukas Czerner }
1996cebe85d5SLukas Czerner 
1997e6e268cbSLukas Czerner #ifdef CONFIG_QUOTA
1998e6e268cbSLukas Czerner /*
1999e6e268cbSLukas Czerner  * Note the name of the specified quota file.
2000e6e268cbSLukas Czerner  */
2001e6e268cbSLukas Czerner static int note_qf_name(struct fs_context *fc, int qtype,
2002e6e268cbSLukas Czerner 		       struct fs_parameter *param)
2003e6e268cbSLukas Czerner {
2004e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2005e6e268cbSLukas Czerner 	char *qname;
2006e6e268cbSLukas Czerner 
2007e6e268cbSLukas Czerner 	if (param->size < 1) {
2008e6e268cbSLukas Czerner 		ext4_msg(NULL, KERN_ERR, "Missing quota name");
2009e6e268cbSLukas Czerner 		return -EINVAL;
2010e6e268cbSLukas Czerner 	}
2011e6e268cbSLukas Czerner 	if (strchr(param->string, '/')) {
2012e6e268cbSLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2013e6e268cbSLukas Czerner 			 "quotafile must be on filesystem root");
2014e6e268cbSLukas Czerner 		return -EINVAL;
2015e6e268cbSLukas Czerner 	}
2016e6e268cbSLukas Czerner 	if (ctx->s_qf_names[qtype]) {
2017e6e268cbSLukas Czerner 		if (strcmp(ctx->s_qf_names[qtype], param->string) != 0) {
2018e6e268cbSLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2019e6e268cbSLukas Czerner 				 "%s quota file already specified",
2020e6e268cbSLukas Czerner 				 QTYPE2NAME(qtype));
2021e6e268cbSLukas Czerner 			return -EINVAL;
2022e6e268cbSLukas Czerner 		}
2023e6e268cbSLukas Czerner 		return 0;
2024e6e268cbSLukas Czerner 	}
2025e6e268cbSLukas Czerner 
2026e6e268cbSLukas Czerner 	qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
2027e6e268cbSLukas Czerner 	if (!qname) {
2028e6e268cbSLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2029e6e268cbSLukas Czerner 			 "Not enough memory for storing quotafile name");
2030e6e268cbSLukas Czerner 		return -ENOMEM;
2031e6e268cbSLukas Czerner 	}
2032e6e268cbSLukas Czerner 	ctx->s_qf_names[qtype] = qname;
2033e6e268cbSLukas Czerner 	ctx->qname_spec |= 1 << qtype;
20346e47a3ccSLukas Czerner 	ctx->spec |= EXT4_SPEC_JQUOTA;
2035e6e268cbSLukas Czerner 	return 0;
2036e6e268cbSLukas Czerner }
2037e6e268cbSLukas Czerner 
2038e6e268cbSLukas Czerner /*
2039e6e268cbSLukas Czerner  * Clear the name of the specified quota file.
2040e6e268cbSLukas Czerner  */
2041e6e268cbSLukas Czerner static int unnote_qf_name(struct fs_context *fc, int qtype)
2042e6e268cbSLukas Czerner {
2043e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2044e6e268cbSLukas Czerner 
2045e6e268cbSLukas Czerner 	if (ctx->s_qf_names[qtype])
2046e6e268cbSLukas Czerner 		kfree(ctx->s_qf_names[qtype]);
2047e6e268cbSLukas Czerner 
2048e6e268cbSLukas Czerner 	ctx->s_qf_names[qtype] = NULL;
2049e6e268cbSLukas Czerner 	ctx->qname_spec |= 1 << qtype;
20506e47a3ccSLukas Czerner 	ctx->spec |= EXT4_SPEC_JQUOTA;
2051e6e268cbSLukas Czerner 	return 0;
2052e6e268cbSLukas Czerner }
2053e6e268cbSLukas Czerner #endif
2054e6e268cbSLukas Czerner 
205585456054SEric Biggers static int ext4_parse_test_dummy_encryption(const struct fs_parameter *param,
205685456054SEric Biggers 					    struct ext4_fs_context *ctx)
205785456054SEric Biggers {
205885456054SEric Biggers 	int err;
205985456054SEric Biggers 
206085456054SEric Biggers 	if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
206185456054SEric Biggers 		ext4_msg(NULL, KERN_WARNING,
206285456054SEric Biggers 			 "test_dummy_encryption option not supported");
206385456054SEric Biggers 		return -EINVAL;
206485456054SEric Biggers 	}
206585456054SEric Biggers 	err = fscrypt_parse_test_dummy_encryption(param,
206685456054SEric Biggers 						  &ctx->dummy_enc_policy);
206785456054SEric Biggers 	if (err == -EINVAL) {
206885456054SEric Biggers 		ext4_msg(NULL, KERN_WARNING,
206985456054SEric Biggers 			 "Value of option \"%s\" is unrecognized", param->key);
207085456054SEric Biggers 	} else if (err == -EEXIST) {
207185456054SEric Biggers 		ext4_msg(NULL, KERN_WARNING,
207285456054SEric Biggers 			 "Conflicting test_dummy_encryption options");
207385456054SEric Biggers 		return -EINVAL;
207485456054SEric Biggers 	}
207585456054SEric Biggers 	return err;
207685456054SEric Biggers }
207785456054SEric Biggers 
20786e47a3ccSLukas Czerner #define EXT4_SET_CTX(name)						\
20794c246728SLukas Czerner static inline void ctx_set_##name(struct ext4_fs_context *ctx,		\
20804c246728SLukas Czerner 				  unsigned long flag)			\
20816e47a3ccSLukas Czerner {									\
20826e47a3ccSLukas Czerner 	ctx->mask_s_##name |= flag;					\
20836e47a3ccSLukas Czerner 	ctx->vals_s_##name |= flag;					\
2084e3952fccSLukas Czerner }
2085e3952fccSLukas Czerner 
2086e3952fccSLukas Czerner #define EXT4_CLEAR_CTX(name)						\
20874c246728SLukas Czerner static inline void ctx_clear_##name(struct ext4_fs_context *ctx,	\
20884c246728SLukas Czerner 				    unsigned long flag)			\
20896e47a3ccSLukas Czerner {									\
20906e47a3ccSLukas Czerner 	ctx->mask_s_##name |= flag;					\
20916e47a3ccSLukas Czerner 	ctx->vals_s_##name &= ~flag;					\
2092e3952fccSLukas Czerner }
2093e3952fccSLukas Czerner 
2094e3952fccSLukas Czerner #define EXT4_TEST_CTX(name)						\
20954c246728SLukas Czerner static inline unsigned long						\
20964c246728SLukas Czerner ctx_test_##name(struct ext4_fs_context *ctx, unsigned long flag)	\
20976e47a3ccSLukas Czerner {									\
20984c246728SLukas Czerner 	return (ctx->vals_s_##name & flag);				\
2099e3952fccSLukas Czerner }
21006e47a3ccSLukas Czerner 
2101e3952fccSLukas Czerner EXT4_SET_CTX(flags); /* set only */
21026e47a3ccSLukas Czerner EXT4_SET_CTX(mount_opt);
2103e3952fccSLukas Czerner EXT4_CLEAR_CTX(mount_opt);
2104e3952fccSLukas Czerner EXT4_TEST_CTX(mount_opt);
21056e47a3ccSLukas Czerner EXT4_SET_CTX(mount_opt2);
2106e3952fccSLukas Czerner EXT4_CLEAR_CTX(mount_opt2);
2107e3952fccSLukas Czerner EXT4_TEST_CTX(mount_opt2);
2108e3952fccSLukas Czerner 
2109e3952fccSLukas Czerner static inline void ctx_set_mount_flag(struct ext4_fs_context *ctx, int bit)
2110e3952fccSLukas Czerner {
2111e3952fccSLukas Czerner 	set_bit(bit, &ctx->mask_s_mount_flags);
2112e3952fccSLukas Czerner 	set_bit(bit, &ctx->vals_s_mount_flags);
2113e3952fccSLukas Czerner }
21146e47a3ccSLukas Czerner 
211502f960f8SLukas Czerner static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param)
211626092bf5STheodore Ts'o {
2117461c3af0SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2118461c3af0SLukas Czerner 	struct fs_parse_result result;
211926092bf5STheodore Ts'o 	const struct mount_opts *m;
2120461c3af0SLukas Czerner 	int is_remount;
212108cefc7aSEric W. Biederman 	kuid_t uid;
212208cefc7aSEric W. Biederman 	kgid_t gid;
2123461c3af0SLukas Czerner 	int token;
2124461c3af0SLukas Czerner 
2125461c3af0SLukas Czerner 	token = fs_parse(fc, ext4_param_specs, param, &result);
2126461c3af0SLukas Czerner 	if (token < 0)
2127461c3af0SLukas Czerner 		return token;
2128461c3af0SLukas Czerner 	is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
212926092bf5STheodore Ts'o 
2130ba2e524dSLukas Czerner 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
2131ba2e524dSLukas Czerner 		if (token == m->token)
2132ba2e524dSLukas Czerner 			break;
2133ba2e524dSLukas Czerner 
2134ba2e524dSLukas Czerner 	ctx->opt_flags |= m->flags;
2135ba2e524dSLukas Czerner 
2136ba2e524dSLukas Czerner 	if (m->flags & MOPT_EXPLICIT) {
2137ba2e524dSLukas Czerner 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2138ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_EXPLICIT_DELALLOC);
2139ba2e524dSLukas Czerner 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2140ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx,
2141ba2e524dSLukas Czerner 				       EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM);
2142ba2e524dSLukas Czerner 		} else
2143ba2e524dSLukas Czerner 			return -EINVAL;
2144ba2e524dSLukas Czerner 	}
2145ba2e524dSLukas Czerner 
2146ba2e524dSLukas Czerner 	if (m->flags & MOPT_NOSUPPORT) {
2147ba2e524dSLukas Czerner 		ext4_msg(NULL, KERN_ERR, "%s option not supported",
2148ba2e524dSLukas Czerner 			 param->key);
2149ba2e524dSLukas Czerner 		return 0;
2150ba2e524dSLukas Czerner 	}
2151ba2e524dSLukas Czerner 
2152ba2e524dSLukas Czerner 	switch (token) {
215357f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
2154ba2e524dSLukas Czerner 	case Opt_usrjquota:
2155461c3af0SLukas Czerner 		if (!*param->string)
2156e6e268cbSLukas Czerner 			return unnote_qf_name(fc, USRQUOTA);
2157461c3af0SLukas Czerner 		else
2158e6e268cbSLukas Czerner 			return note_qf_name(fc, USRQUOTA, param);
2159ba2e524dSLukas Czerner 	case Opt_grpjquota:
2160461c3af0SLukas Czerner 		if (!*param->string)
2161e6e268cbSLukas Czerner 			return unnote_qf_name(fc, GRPQUOTA);
2162461c3af0SLukas Czerner 		else
2163e6e268cbSLukas Czerner 			return note_qf_name(fc, GRPQUOTA, param);
216457f73c2cSTheodore Ts'o #endif
216526092bf5STheodore Ts'o 	case Opt_sb:
21667edfd85bSLukas Czerner 		if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
21677edfd85bSLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
21687edfd85bSLukas Czerner 				 "Ignoring %s option on remount", param->key);
21697edfd85bSLukas Czerner 		} else {
21707edfd85bSLukas Czerner 			ctx->s_sb_block = result.uint_32;
21717edfd85bSLukas Czerner 			ctx->spec |= EXT4_SPEC_s_sb_block;
21727edfd85bSLukas Czerner 		}
217302f960f8SLukas Czerner 		return 0;
217426092bf5STheodore Ts'o 	case Opt_removed:
2175da812f61SLukas Czerner 		ext4_msg(NULL, KERN_WARNING, "Ignoring removed %s option",
2176461c3af0SLukas Czerner 			 param->key);
217702f960f8SLukas Czerner 		return 0;
217826092bf5STheodore Ts'o 	case Opt_abort:
2179e3952fccSLukas Czerner 		ctx_set_mount_flag(ctx, EXT4_MF_FS_ABORTED);
218002f960f8SLukas Czerner 		return 0;
21814f74d15fSEric Biggers 	case Opt_inlinecrypt:
21824f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
21836e47a3ccSLukas Czerner 		ctx_set_flags(ctx, SB_INLINECRYPT);
21844f74d15fSEric Biggers #else
2185da812f61SLukas Czerner 		ext4_msg(NULL, KERN_ERR, "inline encryption not supported");
21864f74d15fSEric Biggers #endif
218702f960f8SLukas Czerner 		return 0;
2188461c3af0SLukas Czerner 	case Opt_errors:
21896e47a3ccSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_ERRORS_MASK);
2190ba2e524dSLukas Czerner 		ctx_set_mount_opt(ctx, result.uint_32);
2191ba2e524dSLukas Czerner 		return 0;
2192ba2e524dSLukas Czerner #ifdef CONFIG_QUOTA
2193ba2e524dSLukas Czerner 	case Opt_jqfmt:
2194ba2e524dSLukas Czerner 		ctx->s_jquota_fmt = result.uint_32;
2195ba2e524dSLukas Czerner 		ctx->spec |= EXT4_SPEC_JQFMT;
2196ba2e524dSLukas Czerner 		return 0;
2197ba2e524dSLukas Czerner #endif
2198ba2e524dSLukas Czerner 	case Opt_data:
2199ba2e524dSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
2200ba2e524dSLukas Czerner 		ctx_set_mount_opt(ctx, result.uint_32);
2201ba2e524dSLukas Czerner 		ctx->spec |= EXT4_SPEC_DATAJ;
2202ba2e524dSLukas Czerner 		return 0;
2203ba2e524dSLukas Czerner 	case Opt_commit:
2204461c3af0SLukas Czerner 		if (result.uint_32 == 0)
2205934b0de1SWang Jianjian 			result.uint_32 = JBD2_DEFAULT_MAX_COMMIT_AGE;
2206461c3af0SLukas Czerner 		else if (result.uint_32 > INT_MAX / HZ) {
2207da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
22089ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
22099ba55543Szhangyi (F) 				 "must be smaller than %d",
2210461c3af0SLukas Czerner 				 result.uint_32, INT_MAX / HZ);
2211da812f61SLukas Czerner 			return -EINVAL;
22129ba55543Szhangyi (F) 		}
22136e47a3ccSLukas Czerner 		ctx->s_commit_interval = HZ * result.uint_32;
22146e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_commit_interval;
2215ba2e524dSLukas Czerner 		return 0;
2216ba2e524dSLukas Czerner 	case Opt_debug_want_extra_isize:
22176e47a3ccSLukas Czerner 		if ((result.uint_32 & 1) || (result.uint_32 < 4)) {
2218da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2219461c3af0SLukas Czerner 				 "Invalid want_extra_isize %d", result.uint_32);
2220da812f61SLukas Czerner 			return -EINVAL;
22219803387cSTheodore Ts'o 		}
22226e47a3ccSLukas Czerner 		ctx->s_want_extra_isize = result.uint_32;
22236e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_want_extra_isize;
2224ba2e524dSLukas Czerner 		return 0;
2225ba2e524dSLukas Czerner 	case Opt_max_batch_time:
22266e47a3ccSLukas Czerner 		ctx->s_max_batch_time = result.uint_32;
22276e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_max_batch_time;
2228ba2e524dSLukas Czerner 		return 0;
2229ba2e524dSLukas Czerner 	case Opt_min_batch_time:
22306e47a3ccSLukas Czerner 		ctx->s_min_batch_time = result.uint_32;
22316e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_min_batch_time;
2232ba2e524dSLukas Czerner 		return 0;
2233ba2e524dSLukas Czerner 	case Opt_inode_readahead_blks:
2234461c3af0SLukas Czerner 		if (result.uint_32 &&
2235461c3af0SLukas Czerner 		    (result.uint_32 > (1 << 30) ||
2236461c3af0SLukas Czerner 		     !is_power_of_2(result.uint_32))) {
2237da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2238e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2239e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
2240da812f61SLukas Czerner 			return -EINVAL;
224126092bf5STheodore Ts'o 		}
22426e47a3ccSLukas Czerner 		ctx->s_inode_readahead_blks = result.uint_32;
22436e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_inode_readahead_blks;
2244ba2e524dSLukas Czerner 		return 0;
2245ba2e524dSLukas Czerner 	case Opt_init_itable:
22466e47a3ccSLukas Czerner 		ctx_set_mount_opt(ctx, EXT4_MOUNT_INIT_INODE_TABLE);
22476e47a3ccSLukas Czerner 		ctx->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
2248461c3af0SLukas Czerner 		if (param->type == fs_value_is_string)
22496e47a3ccSLukas Czerner 			ctx->s_li_wait_mult = result.uint_32;
22506e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_li_wait_mult;
2251ba2e524dSLukas Czerner 		return 0;
2252ba2e524dSLukas Czerner 	case Opt_max_dir_size_kb:
22536e47a3ccSLukas Czerner 		ctx->s_max_dir_size_kb = result.uint_32;
22546e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_max_dir_size_kb;
2255ba2e524dSLukas Czerner 		return 0;
22568016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
2257ba2e524dSLukas Czerner 	case Opt_fc_debug_max_replay:
22586e47a3ccSLukas Czerner 		ctx->s_fc_debug_max_replay = result.uint_32;
22596e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_fc_debug_max_replay;
2260ba2e524dSLukas Czerner 		return 0;
22618016e29fSHarshad Shirwadkar #endif
2262ba2e524dSLukas Czerner 	case Opt_stripe:
22636e47a3ccSLukas Czerner 		ctx->s_stripe = result.uint_32;
22646e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_stripe;
2265ba2e524dSLukas Czerner 		return 0;
2266ba2e524dSLukas Czerner 	case Opt_resuid:
2267461c3af0SLukas Czerner 		uid = make_kuid(current_user_ns(), result.uint_32);
22680efb3b23SJan Kara 		if (!uid_valid(uid)) {
2269da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Invalid uid value %d",
2270461c3af0SLukas Czerner 				 result.uint_32);
2271da812f61SLukas Czerner 			return -EINVAL;
22720efb3b23SJan Kara 		}
22736e47a3ccSLukas Czerner 		ctx->s_resuid = uid;
22746e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_resuid;
2275ba2e524dSLukas Czerner 		return 0;
2276ba2e524dSLukas Czerner 	case Opt_resgid:
2277461c3af0SLukas Czerner 		gid = make_kgid(current_user_ns(), result.uint_32);
22780efb3b23SJan Kara 		if (!gid_valid(gid)) {
2279da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Invalid gid value %d",
2280461c3af0SLukas Czerner 				 result.uint_32);
2281da812f61SLukas Czerner 			return -EINVAL;
22820efb3b23SJan Kara 		}
22836e47a3ccSLukas Czerner 		ctx->s_resgid = gid;
22846e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_resgid;
2285ba2e524dSLukas Czerner 		return 0;
2286ba2e524dSLukas Czerner 	case Opt_journal_dev:
22870efb3b23SJan Kara 		if (is_remount) {
2288da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
22890efb3b23SJan Kara 				 "Cannot specify journal on remount");
2290da812f61SLukas Czerner 			return -EINVAL;
22910efb3b23SJan Kara 		}
2292461c3af0SLukas Czerner 		ctx->journal_devnum = result.uint_32;
22936e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
2294ba2e524dSLukas Czerner 		return 0;
2295ba2e524dSLukas Czerner 	case Opt_journal_path:
2296ba2e524dSLukas Czerner 	{
2297ad4eec61SEric Sandeen 		struct inode *journal_inode;
2298ad4eec61SEric Sandeen 		struct path path;
2299ad4eec61SEric Sandeen 		int error;
2300ad4eec61SEric Sandeen 
2301ad4eec61SEric Sandeen 		if (is_remount) {
2302da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2303ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2304da812f61SLukas Czerner 			return -EINVAL;
2305ad4eec61SEric Sandeen 		}
2306ad4eec61SEric Sandeen 
2307e3ea75eeSLukas Czerner 		error = fs_lookup_param(fc, param, 1, LOOKUP_FOLLOW, &path);
2308ad4eec61SEric Sandeen 		if (error) {
2309da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "error: could not find "
2310461c3af0SLukas Czerner 				 "journal device path");
2311da812f61SLukas Czerner 			return -EINVAL;
2312ad4eec61SEric Sandeen 		}
2313ad4eec61SEric Sandeen 
23142b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2315461c3af0SLukas Czerner 		ctx->journal_devnum = new_encode_dev(journal_inode->i_rdev);
23166e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
2317ad4eec61SEric Sandeen 		path_put(&path);
2318ba2e524dSLukas Czerner 		return 0;
2319ba2e524dSLukas Czerner 	}
2320ba2e524dSLukas Czerner 	case Opt_journal_ioprio:
2321461c3af0SLukas Czerner 		if (result.uint_32 > 7) {
2322da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Invalid journal IO priority"
23230efb3b23SJan Kara 				 " (must be 0-7)");
2324da812f61SLukas Czerner 			return -EINVAL;
23250efb3b23SJan Kara 		}
2326461c3af0SLukas Czerner 		ctx->journal_ioprio =
2327461c3af0SLukas Czerner 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, result.uint_32);
23286e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_JOURNAL_IOPRIO;
2329ba2e524dSLukas Czerner 		return 0;
2330ba2e524dSLukas Czerner 	case Opt_test_dummy_encryption:
233185456054SEric Biggers 		return ext4_parse_test_dummy_encryption(param, ctx);
2332ba2e524dSLukas Czerner 	case Opt_dax:
2333ba2e524dSLukas Czerner 	case Opt_dax_type:
2334ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2335ba2e524dSLukas Czerner 	{
2336ba2e524dSLukas Czerner 		int type = (token == Opt_dax) ?
2337ba2e524dSLukas Czerner 			   Opt_dax : result.uint_32;
2338ba2e524dSLukas Czerner 
2339ba2e524dSLukas Czerner 		switch (type) {
23409cb20f94SIra Weiny 		case Opt_dax:
23419cb20f94SIra Weiny 		case Opt_dax_always:
2342ba2e524dSLukas Czerner 			ctx_set_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
23436e47a3ccSLukas Czerner 			ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
23449cb20f94SIra Weiny 			break;
23459cb20f94SIra Weiny 		case Opt_dax_never:
2346ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
23476e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
23489cb20f94SIra Weiny 			break;
23499cb20f94SIra Weiny 		case Opt_dax_inode:
23506e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
23516e47a3ccSLukas Czerner 			ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
23529cb20f94SIra Weiny 			/* Strictly for printing options */
2353ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE);
23549cb20f94SIra Weiny 			break;
23559cb20f94SIra Weiny 		}
2356ba2e524dSLukas Czerner 		return 0;
2357ba2e524dSLukas Czerner 	}
2358ef83b6e8SDan Williams #else
2359da812f61SLukas Czerner 		ext4_msg(NULL, KERN_INFO, "dax option not supported");
2360da812f61SLukas Czerner 		return -EINVAL;
2361923ae0ffSRoss Zwisler #endif
2362ba2e524dSLukas Czerner 	case Opt_data_err:
2363ba2e524dSLukas Czerner 		if (result.uint_32 == Opt_data_err_abort)
23646e47a3ccSLukas Czerner 			ctx_set_mount_opt(ctx, m->mount_opt);
2365ba2e524dSLukas Czerner 		else if (result.uint_32 == Opt_data_err_ignore)
23666e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, m->mount_opt);
2367ba2e524dSLukas Czerner 		return 0;
2368ba2e524dSLukas Czerner 	case Opt_mb_optimize_scan:
236927b38686SOjaswin Mujoo 		if (result.int_32 == 1) {
237027b38686SOjaswin Mujoo 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
237127b38686SOjaswin Mujoo 			ctx->spec |= EXT4_SPEC_mb_optimize_scan;
237227b38686SOjaswin Mujoo 		} else if (result.int_32 == 0) {
237327b38686SOjaswin Mujoo 			ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
237427b38686SOjaswin Mujoo 			ctx->spec |= EXT4_SPEC_mb_optimize_scan;
237527b38686SOjaswin Mujoo 		} else {
2376da812f61SLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
2377196e402aSHarshad Shirwadkar 				 "mb_optimize_scan should be set to 0 or 1.");
2378da812f61SLukas Czerner 			return -EINVAL;
2379196e402aSHarshad Shirwadkar 		}
2380ba2e524dSLukas Czerner 		return 0;
2381ba2e524dSLukas Czerner 	}
2382ba2e524dSLukas Czerner 
2383ba2e524dSLukas Czerner 	/*
2384ba2e524dSLukas Czerner 	 * At this point we should only be getting options requiring MOPT_SET,
2385ba2e524dSLukas Czerner 	 * or MOPT_CLEAR. Anything else is a bug
2386ba2e524dSLukas Czerner 	 */
2387ba2e524dSLukas Czerner 	if (m->token == Opt_err) {
2388ba2e524dSLukas Czerner 		ext4_msg(NULL, KERN_WARNING, "buggy handling of option %s",
2389ba2e524dSLukas Czerner 			 param->key);
2390ba2e524dSLukas Czerner 		WARN_ON(1);
2391ba2e524dSLukas Czerner 		return -EINVAL;
2392ba2e524dSLukas Czerner 	}
2393ba2e524dSLukas Czerner 
2394ba2e524dSLukas Czerner 	else {
2395461c3af0SLukas Czerner 		unsigned int set = 0;
2396461c3af0SLukas Czerner 
2397461c3af0SLukas Czerner 		if ((param->type == fs_value_is_flag) ||
2398461c3af0SLukas Czerner 		    result.uint_32 > 0)
2399461c3af0SLukas Czerner 			set = 1;
2400461c3af0SLukas Czerner 
240126092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
2402461c3af0SLukas Czerner 			set = !set;
240326092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
2404da812f61SLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
2405461c3af0SLukas Czerner 				 "buggy handling of option %s",
2406461c3af0SLukas Czerner 				 param->key);
240726092bf5STheodore Ts'o 			WARN_ON(1);
2408da812f61SLukas Czerner 			return -EINVAL;
240926092bf5STheodore Ts'o 		}
2410995a3ed6SHarshad Shirwadkar 		if (m->flags & MOPT_2) {
2411461c3af0SLukas Czerner 			if (set != 0)
24126e47a3ccSLukas Czerner 				ctx_set_mount_opt2(ctx, m->mount_opt);
2413995a3ed6SHarshad Shirwadkar 			else
24146e47a3ccSLukas Czerner 				ctx_clear_mount_opt2(ctx, m->mount_opt);
2415995a3ed6SHarshad Shirwadkar 		} else {
2416461c3af0SLukas Czerner 			if (set != 0)
24176e47a3ccSLukas Czerner 				ctx_set_mount_opt(ctx, m->mount_opt);
241826092bf5STheodore Ts'o 			else
24196e47a3ccSLukas Czerner 				ctx_clear_mount_opt(ctx, m->mount_opt);
242026092bf5STheodore Ts'o 		}
2421995a3ed6SHarshad Shirwadkar 	}
2422ba2e524dSLukas Czerner 
242302f960f8SLukas Czerner 	return 0;
242426092bf5STheodore Ts'o }
242526092bf5STheodore Ts'o 
24267edfd85bSLukas Czerner static int parse_options(struct fs_context *fc, char *options)
2427ac27a0ecSDave Kleikamp {
2428461c3af0SLukas Czerner 	struct fs_parameter param;
2429461c3af0SLukas Czerner 	int ret;
2430461c3af0SLukas Czerner 	char *key;
2431ac27a0ecSDave Kleikamp 
2432ac27a0ecSDave Kleikamp 	if (!options)
24337edfd85bSLukas Czerner 		return 0;
2434461c3af0SLukas Czerner 
2435461c3af0SLukas Czerner 	while ((key = strsep(&options, ",")) != NULL) {
2436461c3af0SLukas Czerner 		if (*key) {
2437461c3af0SLukas Czerner 			size_t v_len = 0;
2438461c3af0SLukas Czerner 			char *value = strchr(key, '=');
2439461c3af0SLukas Czerner 
2440461c3af0SLukas Czerner 			param.type = fs_value_is_flag;
2441461c3af0SLukas Czerner 			param.string = NULL;
2442461c3af0SLukas Czerner 
2443461c3af0SLukas Czerner 			if (value) {
2444461c3af0SLukas Czerner 				if (value == key)
2445ac27a0ecSDave Kleikamp 					continue;
2446461c3af0SLukas Czerner 
2447461c3af0SLukas Czerner 				*value++ = 0;
2448461c3af0SLukas Czerner 				v_len = strlen(value);
2449461c3af0SLukas Czerner 				param.string = kmemdup_nul(value, v_len,
2450461c3af0SLukas Czerner 							   GFP_KERNEL);
2451461c3af0SLukas Czerner 				if (!param.string)
24527edfd85bSLukas Czerner 					return -ENOMEM;
2453461c3af0SLukas Czerner 				param.type = fs_value_is_string;
2454461c3af0SLukas Czerner 			}
2455461c3af0SLukas Czerner 
2456461c3af0SLukas Czerner 			param.key = key;
2457461c3af0SLukas Czerner 			param.size = v_len;
2458461c3af0SLukas Czerner 
245902f960f8SLukas Czerner 			ret = ext4_parse_param(fc, &param);
2460461c3af0SLukas Czerner 			if (param.string)
2461461c3af0SLukas Czerner 				kfree(param.string);
2462461c3af0SLukas Czerner 			if (ret < 0)
24637edfd85bSLukas Czerner 				return ret;
2464ac27a0ecSDave Kleikamp 		}
2465461c3af0SLukas Czerner 	}
2466461c3af0SLukas Czerner 
24677edfd85bSLukas Czerner 	ret = ext4_validate_options(fc);
2468da812f61SLukas Czerner 	if (ret < 0)
24697edfd85bSLukas Czerner 		return ret;
24707edfd85bSLukas Czerner 
24717edfd85bSLukas Czerner 	return 0;
24727edfd85bSLukas Czerner }
24737edfd85bSLukas Czerner 
24747edfd85bSLukas Czerner static int parse_apply_sb_mount_options(struct super_block *sb,
24757edfd85bSLukas Czerner 					struct ext4_fs_context *m_ctx)
24767edfd85bSLukas Czerner {
24777edfd85bSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
24787edfd85bSLukas Czerner 	char *s_mount_opts = NULL;
24797edfd85bSLukas Czerner 	struct ext4_fs_context *s_ctx = NULL;
24807edfd85bSLukas Czerner 	struct fs_context *fc = NULL;
24817edfd85bSLukas Czerner 	int ret = -ENOMEM;
24827edfd85bSLukas Czerner 
24837edfd85bSLukas Czerner 	if (!sbi->s_es->s_mount_opts[0])
2484da812f61SLukas Czerner 		return 0;
2485da812f61SLukas Czerner 
24867edfd85bSLukas Czerner 	s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
24877edfd85bSLukas Czerner 				sizeof(sbi->s_es->s_mount_opts),
24887edfd85bSLukas Czerner 				GFP_KERNEL);
24897edfd85bSLukas Czerner 	if (!s_mount_opts)
24907edfd85bSLukas Czerner 		return ret;
2491e6e268cbSLukas Czerner 
24927edfd85bSLukas Czerner 	fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
24937edfd85bSLukas Czerner 	if (!fc)
24947edfd85bSLukas Czerner 		goto out_free;
2495e6e268cbSLukas Czerner 
24967edfd85bSLukas Czerner 	s_ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
24977edfd85bSLukas Czerner 	if (!s_ctx)
24987edfd85bSLukas Czerner 		goto out_free;
24997edfd85bSLukas Czerner 
25007edfd85bSLukas Czerner 	fc->fs_private = s_ctx;
25017edfd85bSLukas Czerner 	fc->s_fs_info = sbi;
25027edfd85bSLukas Czerner 
25037edfd85bSLukas Czerner 	ret = parse_options(fc, s_mount_opts);
25047edfd85bSLukas Czerner 	if (ret < 0)
25057edfd85bSLukas Czerner 		goto parse_failed;
25067edfd85bSLukas Czerner 
25077edfd85bSLukas Czerner 	ret = ext4_check_opt_consistency(fc, sb);
25087edfd85bSLukas Czerner 	if (ret < 0) {
25097edfd85bSLukas Czerner parse_failed:
25107edfd85bSLukas Czerner 		ext4_msg(sb, KERN_WARNING,
25117edfd85bSLukas Czerner 			 "failed to parse options in superblock: %s",
25127edfd85bSLukas Czerner 			 s_mount_opts);
25137edfd85bSLukas Czerner 		ret = 0;
25147edfd85bSLukas Czerner 		goto out_free;
25157edfd85bSLukas Czerner 	}
25167edfd85bSLukas Czerner 
25177edfd85bSLukas Czerner 	if (s_ctx->spec & EXT4_SPEC_JOURNAL_DEV)
25187edfd85bSLukas Czerner 		m_ctx->journal_devnum = s_ctx->journal_devnum;
25197edfd85bSLukas Czerner 	if (s_ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)
25207edfd85bSLukas Czerner 		m_ctx->journal_ioprio = s_ctx->journal_ioprio;
25217edfd85bSLukas Czerner 
252285456054SEric Biggers 	ext4_apply_options(fc, sb);
252385456054SEric Biggers 	ret = 0;
25247edfd85bSLukas Czerner 
25257edfd85bSLukas Czerner out_free:
2526c069db76SEric Biggers 	if (fc) {
2527c069db76SEric Biggers 		ext4_fc_free(fc);
25287edfd85bSLukas Czerner 		kfree(fc);
2529c069db76SEric Biggers 	}
25307edfd85bSLukas Czerner 	kfree(s_mount_opts);
25317edfd85bSLukas Czerner 	return ret;
25324c94bff9SLukas Czerner }
25334c94bff9SLukas Czerner 
2534e6e268cbSLukas Czerner static void ext4_apply_quota_options(struct fs_context *fc,
2535e6e268cbSLukas Czerner 				     struct super_block *sb)
2536e6e268cbSLukas Czerner {
2537e6e268cbSLukas Czerner #ifdef CONFIG_QUOTA
25386e47a3ccSLukas Czerner 	bool quota_feature = ext4_has_feature_quota(sb);
2539e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2540e6e268cbSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2541e6e268cbSLukas Czerner 	char *qname;
2542e6e268cbSLukas Czerner 	int i;
2543e6e268cbSLukas Czerner 
25446e47a3ccSLukas Czerner 	if (quota_feature)
25456e47a3ccSLukas Czerner 		return;
25466e47a3ccSLukas Czerner 
25476e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQUOTA) {
2548e6e268cbSLukas Czerner 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2549e6e268cbSLukas Czerner 			if (!(ctx->qname_spec & (1 << i)))
2550e6e268cbSLukas Czerner 				continue;
25516e47a3ccSLukas Czerner 
2552e6e268cbSLukas Czerner 			qname = ctx->s_qf_names[i]; /* May be NULL */
25534c1bd5a9SLukas Czerner 			if (qname)
25544c1bd5a9SLukas Czerner 				set_opt(sb, QUOTA);
2555e6e268cbSLukas Czerner 			ctx->s_qf_names[i] = NULL;
255613b215a9SLukas Czerner 			qname = rcu_replace_pointer(sbi->s_qf_names[i], qname,
255713b215a9SLukas Czerner 						lockdep_is_held(&sb->s_umount));
255813b215a9SLukas Czerner 			if (qname)
255910e4f310SUladzislau Rezki (Sony) 				kfree_rcu_mightsleep(qname);
2560e6e268cbSLukas Czerner 		}
25616e47a3ccSLukas Czerner 	}
25626e47a3ccSLukas Czerner 
25636e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQFMT)
25646e47a3ccSLukas Czerner 		sbi->s_jquota_fmt = ctx->s_jquota_fmt;
2565e6e268cbSLukas Czerner #endif
2566e6e268cbSLukas Czerner }
2567e6e268cbSLukas Czerner 
2568e6e268cbSLukas Czerner /*
2569e6e268cbSLukas Czerner  * Check quota settings consistency.
2570e6e268cbSLukas Czerner  */
2571e6e268cbSLukas Czerner static int ext4_check_quota_consistency(struct fs_context *fc,
2572e6e268cbSLukas Czerner 					struct super_block *sb)
2573e6e268cbSLukas Czerner {
2574e6e268cbSLukas Czerner #ifdef CONFIG_QUOTA
2575e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2576e6e268cbSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2577e6e268cbSLukas Czerner 	bool quota_feature = ext4_has_feature_quota(sb);
2578e6e268cbSLukas Czerner 	bool quota_loaded = sb_any_quota_loaded(sb);
25796e47a3ccSLukas Czerner 	bool usr_qf_name, grp_qf_name, usrquota, grpquota;
25806e47a3ccSLukas Czerner 	int quota_flags, i;
2581e6e268cbSLukas Czerner 
25826e47a3ccSLukas Czerner 	/*
25836e47a3ccSLukas Czerner 	 * We do the test below only for project quotas. 'usrquota' and
25846e47a3ccSLukas Czerner 	 * 'grpquota' mount options are allowed even without quota feature
25856e47a3ccSLukas Czerner 	 * to support legacy quotas in quota files.
25866e47a3ccSLukas Czerner 	 */
25876e47a3ccSLukas Czerner 	if (ctx_test_mount_opt(ctx, EXT4_MOUNT_PRJQUOTA) &&
25886e47a3ccSLukas Czerner 	    !ext4_has_feature_project(sb)) {
25896e47a3ccSLukas Czerner 		ext4_msg(NULL, KERN_ERR, "Project quota feature not enabled. "
25906e47a3ccSLukas Czerner 			 "Cannot enable project quota enforcement.");
25916e47a3ccSLukas Czerner 		return -EINVAL;
25926e47a3ccSLukas Czerner 	}
25936e47a3ccSLukas Czerner 
25946e47a3ccSLukas Czerner 	quota_flags = EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
25956e47a3ccSLukas Czerner 		      EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA;
25966e47a3ccSLukas Czerner 	if (quota_loaded &&
25976e47a3ccSLukas Czerner 	    ctx->mask_s_mount_opt & quota_flags &&
25986e47a3ccSLukas Czerner 	    !ctx_test_mount_opt(ctx, quota_flags))
25996e47a3ccSLukas Czerner 		goto err_quota_change;
26006e47a3ccSLukas Czerner 
26016e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQUOTA) {
2602e6e268cbSLukas Czerner 
2603e6e268cbSLukas Czerner 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2604e6e268cbSLukas Czerner 			if (!(ctx->qname_spec & (1 << i)))
2605e6e268cbSLukas Czerner 				continue;
2606e6e268cbSLukas Czerner 
26076e47a3ccSLukas Czerner 			if (quota_loaded &&
26086e47a3ccSLukas Czerner 			    !!sbi->s_qf_names[i] != !!ctx->s_qf_names[i])
2609e6e268cbSLukas Czerner 				goto err_jquota_change;
2610e6e268cbSLukas Czerner 
2611e6e268cbSLukas Czerner 			if (sbi->s_qf_names[i] && ctx->s_qf_names[i] &&
261213b215a9SLukas Czerner 			    strcmp(get_qf_name(sb, sbi, i),
2613e6e268cbSLukas Czerner 				   ctx->s_qf_names[i]) != 0)
2614e6e268cbSLukas Czerner 				goto err_jquota_specified;
2615e6e268cbSLukas Czerner 		}
26166e47a3ccSLukas Czerner 
26176e47a3ccSLukas Czerner 		if (quota_feature) {
26186e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_INFO,
26196e47a3ccSLukas Czerner 				 "Journaled quota options ignored when "
26206e47a3ccSLukas Czerner 				 "QUOTA feature is enabled");
26216e47a3ccSLukas Czerner 			return 0;
26226e47a3ccSLukas Czerner 		}
2623e6e268cbSLukas Czerner 	}
2624e6e268cbSLukas Czerner 
26256e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQFMT) {
2626e6e268cbSLukas Czerner 		if (sbi->s_jquota_fmt != ctx->s_jquota_fmt && quota_loaded)
26276e47a3ccSLukas Czerner 			goto err_jquota_change;
2628e6e268cbSLukas Czerner 		if (quota_feature) {
2629e6e268cbSLukas Czerner 			ext4_msg(NULL, KERN_INFO, "Quota format mount options "
2630e6e268cbSLukas Czerner 				 "ignored when QUOTA feature is enabled");
2631e6e268cbSLukas Czerner 			return 0;
2632e6e268cbSLukas Czerner 		}
2633e6e268cbSLukas Czerner 	}
26346e47a3ccSLukas Czerner 
26356e47a3ccSLukas Czerner 	/* Make sure we don't mix old and new quota format */
26366e47a3ccSLukas Czerner 	usr_qf_name = (get_qf_name(sb, sbi, USRQUOTA) ||
26376e47a3ccSLukas Czerner 		       ctx->s_qf_names[USRQUOTA]);
26386e47a3ccSLukas Czerner 	grp_qf_name = (get_qf_name(sb, sbi, GRPQUOTA) ||
26396e47a3ccSLukas Czerner 		       ctx->s_qf_names[GRPQUOTA]);
26406e47a3ccSLukas Czerner 
26416e47a3ccSLukas Czerner 	usrquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
26426e47a3ccSLukas Czerner 		    test_opt(sb, USRQUOTA));
26436e47a3ccSLukas Czerner 
26446e47a3ccSLukas Czerner 	grpquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) ||
26456e47a3ccSLukas Czerner 		    test_opt(sb, GRPQUOTA));
26466e47a3ccSLukas Czerner 
26476e47a3ccSLukas Czerner 	if (usr_qf_name) {
26486e47a3ccSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
26496e47a3ccSLukas Czerner 		usrquota = false;
26506e47a3ccSLukas Czerner 	}
26516e47a3ccSLukas Czerner 	if (grp_qf_name) {
26526e47a3ccSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
26536e47a3ccSLukas Czerner 		grpquota = false;
26546e47a3ccSLukas Czerner 	}
26556e47a3ccSLukas Czerner 
26566e47a3ccSLukas Czerner 	if (usr_qf_name || grp_qf_name) {
26576e47a3ccSLukas Czerner 		if (usrquota || grpquota) {
26586e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "old and new quota "
26596e47a3ccSLukas Czerner 				 "format mixing");
26606e47a3ccSLukas Czerner 			return -EINVAL;
26616e47a3ccSLukas Czerner 		}
26626e47a3ccSLukas Czerner 
26636e47a3ccSLukas Czerner 		if (!(ctx->spec & EXT4_SPEC_JQFMT || sbi->s_jquota_fmt)) {
26646e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "journaled quota format "
26656e47a3ccSLukas Czerner 				 "not specified");
26666e47a3ccSLukas Czerner 			return -EINVAL;
26676e47a3ccSLukas Czerner 		}
26686e47a3ccSLukas Czerner 	}
26696e47a3ccSLukas Czerner 
2670e6e268cbSLukas Czerner 	return 0;
2671e6e268cbSLukas Czerner 
2672e6e268cbSLukas Czerner err_quota_change:
2673e6e268cbSLukas Czerner 	ext4_msg(NULL, KERN_ERR,
2674e6e268cbSLukas Czerner 		 "Cannot change quota options when quota turned on");
2675e6e268cbSLukas Czerner 	return -EINVAL;
2676e6e268cbSLukas Czerner err_jquota_change:
2677e6e268cbSLukas Czerner 	ext4_msg(NULL, KERN_ERR, "Cannot change journaled quota "
2678e6e268cbSLukas Czerner 		 "options when quota turned on");
2679e6e268cbSLukas Czerner 	return -EINVAL;
2680e6e268cbSLukas Czerner err_jquota_specified:
2681e6e268cbSLukas Czerner 	ext4_msg(NULL, KERN_ERR, "%s quota file already specified",
2682e6e268cbSLukas Czerner 		 QTYPE2NAME(i));
2683e6e268cbSLukas Czerner 	return -EINVAL;
2684e6e268cbSLukas Czerner #else
2685e6e268cbSLukas Czerner 	return 0;
2686e6e268cbSLukas Czerner #endif
2687e6e268cbSLukas Czerner }
2688e6e268cbSLukas Czerner 
26895f41fdaeSEric Biggers static int ext4_check_test_dummy_encryption(const struct fs_context *fc,
26905f41fdaeSEric Biggers 					    struct super_block *sb)
26915f41fdaeSEric Biggers {
26925f41fdaeSEric Biggers 	const struct ext4_fs_context *ctx = fc->fs_private;
26935f41fdaeSEric Biggers 	const struct ext4_sb_info *sbi = EXT4_SB(sb);
26945f41fdaeSEric Biggers 
269585456054SEric Biggers 	if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy))
26965f41fdaeSEric Biggers 		return 0;
26975f41fdaeSEric Biggers 
26985f41fdaeSEric Biggers 	if (!ext4_has_feature_encrypt(sb)) {
26995f41fdaeSEric Biggers 		ext4_msg(NULL, KERN_WARNING,
27005f41fdaeSEric Biggers 			 "test_dummy_encryption requires encrypt feature");
27015f41fdaeSEric Biggers 		return -EINVAL;
27025f41fdaeSEric Biggers 	}
27035f41fdaeSEric Biggers 	/*
27045f41fdaeSEric Biggers 	 * This mount option is just for testing, and it's not worthwhile to
27055f41fdaeSEric Biggers 	 * implement the extra complexity (e.g. RCU protection) that would be
27065f41fdaeSEric Biggers 	 * needed to allow it to be set or changed during remount.  We do allow
27075f41fdaeSEric Biggers 	 * it to be specified during remount, but only if there is no change.
27085f41fdaeSEric Biggers 	 */
270985456054SEric Biggers 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
271085456054SEric Biggers 		if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
271185456054SEric Biggers 						 &ctx->dummy_enc_policy))
271285456054SEric Biggers 			return 0;
27135f41fdaeSEric Biggers 		ext4_msg(NULL, KERN_WARNING,
271485456054SEric Biggers 			 "Can't set or change test_dummy_encryption on remount");
27155f41fdaeSEric Biggers 		return -EINVAL;
27165f41fdaeSEric Biggers 	}
271785456054SEric Biggers 	/* Also make sure s_mount_opts didn't contain a conflicting value. */
271885456054SEric Biggers 	if (fscrypt_is_dummy_policy_set(&sbi->s_dummy_enc_policy)) {
271985456054SEric Biggers 		if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
272085456054SEric Biggers 						 &ctx->dummy_enc_policy))
27215f41fdaeSEric Biggers 			return 0;
272285456054SEric Biggers 		ext4_msg(NULL, KERN_WARNING,
272385456054SEric Biggers 			 "Conflicting test_dummy_encryption options");
272485456054SEric Biggers 		return -EINVAL;
272585456054SEric Biggers 	}
27267959eb19SEric Biggers 	return 0;
272785456054SEric Biggers }
272885456054SEric Biggers 
272985456054SEric Biggers static void ext4_apply_test_dummy_encryption(struct ext4_fs_context *ctx,
273085456054SEric Biggers 					     struct super_block *sb)
273185456054SEric Biggers {
273285456054SEric Biggers 	if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy) ||
273385456054SEric Biggers 	    /* if already set, it was already verified to be the same */
273485456054SEric Biggers 	    fscrypt_is_dummy_policy_set(&EXT4_SB(sb)->s_dummy_enc_policy))
273585456054SEric Biggers 		return;
273685456054SEric Biggers 	EXT4_SB(sb)->s_dummy_enc_policy = ctx->dummy_enc_policy;
273785456054SEric Biggers 	memset(&ctx->dummy_enc_policy, 0, sizeof(ctx->dummy_enc_policy));
273885456054SEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
27395f41fdaeSEric Biggers }
27405f41fdaeSEric Biggers 
2741b6bd2435SLukas Czerner static int ext4_check_opt_consistency(struct fs_context *fc,
2742b6bd2435SLukas Czerner 				      struct super_block *sb)
2743b6bd2435SLukas Czerner {
2744b6bd2435SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
27456e47a3ccSLukas Czerner 	struct ext4_sb_info *sbi = fc->s_fs_info;
27466e47a3ccSLukas Czerner 	int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
27475f41fdaeSEric Biggers 	int err;
2748b6bd2435SLukas Czerner 
2749b6bd2435SLukas Czerner 	if ((ctx->opt_flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2750b6bd2435SLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2751b6bd2435SLukas Czerner 			 "Mount option(s) incompatible with ext2");
2752b6bd2435SLukas Czerner 		return -EINVAL;
2753b6bd2435SLukas Czerner 	}
2754b6bd2435SLukas Czerner 	if ((ctx->opt_flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2755b6bd2435SLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2756b6bd2435SLukas Czerner 			 "Mount option(s) incompatible with ext3");
2757b6bd2435SLukas Czerner 		return -EINVAL;
2758b6bd2435SLukas Czerner 	}
2759b6bd2435SLukas Czerner 
27606e47a3ccSLukas Czerner 	if (ctx->s_want_extra_isize >
27616e47a3ccSLukas Czerner 	    (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE)) {
27626e47a3ccSLukas Czerner 		ext4_msg(NULL, KERN_ERR,
27636e47a3ccSLukas Czerner 			 "Invalid want_extra_isize %d",
27646e47a3ccSLukas Czerner 			 ctx->s_want_extra_isize);
27656e47a3ccSLukas Czerner 		return -EINVAL;
27666e47a3ccSLukas Czerner 	}
27676e47a3ccSLukas Czerner 
27686e47a3ccSLukas Czerner 	if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DIOREAD_NOLOCK)) {
27696e47a3ccSLukas Czerner 		int blocksize =
27706e47a3ccSLukas Czerner 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
27716e47a3ccSLukas Czerner 		if (blocksize < PAGE_SIZE)
27726e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_WARNING, "Warning: mounting with an "
27736e47a3ccSLukas Czerner 				 "experimental mount option 'dioread_nolock' "
27746e47a3ccSLukas Czerner 				 "for blocksize < PAGE_SIZE");
27756e47a3ccSLukas Czerner 	}
27766e47a3ccSLukas Czerner 
27775f41fdaeSEric Biggers 	err = ext4_check_test_dummy_encryption(fc, sb);
27785f41fdaeSEric Biggers 	if (err)
27795f41fdaeSEric Biggers 		return err;
27806e47a3ccSLukas Czerner 
27816e47a3ccSLukas Czerner 	if ((ctx->spec & EXT4_SPEC_DATAJ) && is_remount) {
27826e47a3ccSLukas Czerner 		if (!sbi->s_journal) {
27836e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
27846e47a3ccSLukas Czerner 				 "Remounting file system with no journal "
27856e47a3ccSLukas Czerner 				 "so ignoring journalled data option");
27866e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
27874c246728SLukas Czerner 		} else if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS) !=
27884c246728SLukas Czerner 			   test_opt(sb, DATA_FLAGS)) {
27896e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Cannot change data mode "
27906e47a3ccSLukas Czerner 				 "on remount");
27916e47a3ccSLukas Czerner 			return -EINVAL;
27926e47a3ccSLukas Czerner 		}
27936e47a3ccSLukas Czerner 	}
27946e47a3ccSLukas Czerner 
27956e47a3ccSLukas Czerner 	if (is_remount) {
27966e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
27976e47a3ccSLukas Czerner 		    (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
27986e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "can't mount with "
27996e47a3ccSLukas Czerner 				 "both data=journal and dax");
28006e47a3ccSLukas Czerner 			return -EINVAL;
28016e47a3ccSLukas Czerner 		}
28026e47a3ccSLukas Czerner 
28036e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
28046e47a3ccSLukas Czerner 		    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
28056e47a3ccSLukas Czerner 		     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
28066e47a3ccSLukas Czerner fail_dax_change_remount:
28076e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "can't change "
28086e47a3ccSLukas Czerner 				 "dax mount option while remounting");
28096e47a3ccSLukas Czerner 			return -EINVAL;
28106e47a3ccSLukas Czerner 		} else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER) &&
28116e47a3ccSLukas Czerner 			 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
28126e47a3ccSLukas Czerner 			  (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) {
28136e47a3ccSLukas Czerner 			goto fail_dax_change_remount;
28146e47a3ccSLukas Czerner 		} else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE) &&
28156e47a3ccSLukas Czerner 			   ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
28166e47a3ccSLukas Czerner 			    (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
28176e47a3ccSLukas Czerner 			    !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) {
28186e47a3ccSLukas Czerner 			goto fail_dax_change_remount;
28196e47a3ccSLukas Czerner 		}
28206e47a3ccSLukas Czerner 	}
28216e47a3ccSLukas Czerner 
2822b6bd2435SLukas Czerner 	return ext4_check_quota_consistency(fc, sb);
2823b6bd2435SLukas Czerner }
2824b6bd2435SLukas Czerner 
282585456054SEric Biggers static void ext4_apply_options(struct fs_context *fc, struct super_block *sb)
28266e47a3ccSLukas Czerner {
28276e47a3ccSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
28286e47a3ccSLukas Czerner 	struct ext4_sb_info *sbi = fc->s_fs_info;
28296e47a3ccSLukas Czerner 
28306e47a3ccSLukas Czerner 	sbi->s_mount_opt &= ~ctx->mask_s_mount_opt;
28316e47a3ccSLukas Czerner 	sbi->s_mount_opt |= ctx->vals_s_mount_opt;
28326e47a3ccSLukas Czerner 	sbi->s_mount_opt2 &= ~ctx->mask_s_mount_opt2;
28336e47a3ccSLukas Czerner 	sbi->s_mount_opt2 |= ctx->vals_s_mount_opt2;
28346e47a3ccSLukas Czerner 	sbi->s_mount_flags &= ~ctx->mask_s_mount_flags;
28356e47a3ccSLukas Czerner 	sbi->s_mount_flags |= ctx->vals_s_mount_flags;
28366e47a3ccSLukas Czerner 	sb->s_flags &= ~ctx->mask_s_flags;
28376e47a3ccSLukas Czerner 	sb->s_flags |= ctx->vals_s_flags;
28386e47a3ccSLukas Czerner 
28396e47a3ccSLukas Czerner #define APPLY(X) ({ if (ctx->spec & EXT4_SPEC_##X) sbi->X = ctx->X; })
28406e47a3ccSLukas Czerner 	APPLY(s_commit_interval);
28416e47a3ccSLukas Czerner 	APPLY(s_stripe);
28426e47a3ccSLukas Czerner 	APPLY(s_max_batch_time);
28436e47a3ccSLukas Czerner 	APPLY(s_min_batch_time);
28446e47a3ccSLukas Czerner 	APPLY(s_want_extra_isize);
28456e47a3ccSLukas Czerner 	APPLY(s_inode_readahead_blks);
28466e47a3ccSLukas Czerner 	APPLY(s_max_dir_size_kb);
28476e47a3ccSLukas Czerner 	APPLY(s_li_wait_mult);
28486e47a3ccSLukas Czerner 	APPLY(s_resgid);
28496e47a3ccSLukas Czerner 	APPLY(s_resuid);
28506e47a3ccSLukas Czerner 
28516e47a3ccSLukas Czerner #ifdef CONFIG_EXT4_DEBUG
28526e47a3ccSLukas Czerner 	APPLY(s_fc_debug_max_replay);
28536e47a3ccSLukas Czerner #endif
28546e47a3ccSLukas Czerner 
28556e47a3ccSLukas Czerner 	ext4_apply_quota_options(fc, sb);
285685456054SEric Biggers 	ext4_apply_test_dummy_encryption(ctx, sb);
28576e47a3ccSLukas Czerner }
28586e47a3ccSLukas Czerner 
28596e47a3ccSLukas Czerner 
2860da812f61SLukas Czerner static int ext4_validate_options(struct fs_context *fc)
28614c94bff9SLukas Czerner {
2862ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
28636e47a3ccSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
28644c94bff9SLukas Czerner 	char *usr_qf_name, *grp_qf_name;
28656e47a3ccSLukas Czerner 
28666e47a3ccSLukas Czerner 	usr_qf_name = ctx->s_qf_names[USRQUOTA];
28676e47a3ccSLukas Czerner 	grp_qf_name = ctx->s_qf_names[GRPQUOTA];
28686e47a3ccSLukas Czerner 
286933458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
28706e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) && usr_qf_name)
28716e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
2872ac27a0ecSDave Kleikamp 
28736e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) && grp_qf_name)
28746e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
2875ac27a0ecSDave Kleikamp 
28766e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
28776e47a3ccSLukas Czerner 		    ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA)) {
2878da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "old and new quota "
2879b31e1552SEric Sandeen 				 "format mixing");
2880da812f61SLukas Czerner 			return -EINVAL;
2881ac27a0ecSDave Kleikamp 		}
2882ac27a0ecSDave Kleikamp 	}
2883ac27a0ecSDave Kleikamp #endif
28846e47a3ccSLukas Czerner 	return 1;
2885ac27a0ecSDave Kleikamp }
2886ac27a0ecSDave Kleikamp 
28872adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
28882adf6da8STheodore Ts'o 					   struct super_block *sb)
28892adf6da8STheodore Ts'o {
28902adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
28912adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
289233458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
28932adf6da8STheodore Ts'o 
28942adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
28952adf6da8STheodore Ts'o 		char *fmtname = "";
28962adf6da8STheodore Ts'o 
28972adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
28982adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
28992adf6da8STheodore Ts'o 			fmtname = "vfsold";
29002adf6da8STheodore Ts'o 			break;
29012adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
29022adf6da8STheodore Ts'o 			fmtname = "vfsv0";
29032adf6da8STheodore Ts'o 			break;
29042adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
29052adf6da8STheodore Ts'o 			fmtname = "vfsv1";
29062adf6da8STheodore Ts'o 			break;
29072adf6da8STheodore Ts'o 		}
29082adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
29092adf6da8STheodore Ts'o 	}
29102adf6da8STheodore Ts'o 
291133458eabSTheodore Ts'o 	rcu_read_lock();
291233458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
291333458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
291433458eabSTheodore Ts'o 	if (usr_qf_name)
291533458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
291633458eabSTheodore Ts'o 	if (grp_qf_name)
291733458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
291833458eabSTheodore Ts'o 	rcu_read_unlock();
29192adf6da8STheodore Ts'o #endif
29202adf6da8STheodore Ts'o }
29212adf6da8STheodore Ts'o 
29225a916be1STheodore Ts'o static const char *token2str(int token)
29235a916be1STheodore Ts'o {
292497d8a670SLukas Czerner 	const struct fs_parameter_spec *spec;
29255a916be1STheodore Ts'o 
292697d8a670SLukas Czerner 	for (spec = ext4_param_specs; spec->name != NULL; spec++)
292797d8a670SLukas Czerner 		if (spec->opt == token && !spec->type)
29285a916be1STheodore Ts'o 			break;
292997d8a670SLukas Czerner 	return spec->name;
29305a916be1STheodore Ts'o }
29315a916be1STheodore Ts'o 
29322adf6da8STheodore Ts'o /*
29332adf6da8STheodore Ts'o  * Show an option if
29342adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
29352adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
29362adf6da8STheodore Ts'o  */
293766acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
293866acdcf4STheodore Ts'o 			      int nodefs)
29392adf6da8STheodore Ts'o {
29402adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
29412adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
2942e3645d72SZhang Yi 	int def_errors;
29435a916be1STheodore Ts'o 	const struct mount_opts *m;
294466acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
29452adf6da8STheodore Ts'o 
294666acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
294766acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
29482adf6da8STheodore Ts'o 
29492adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
29505a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
29515a916be1STheodore Ts'o 
29525a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
29535a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
2954e3645d72SZhang Yi 		int opt_2 = m->flags & MOPT_2;
2955e3645d72SZhang Yi 		unsigned int mount_opt, def_mount_opt;
2956e3645d72SZhang Yi 
29575a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
2958ba2e524dSLukas Czerner 		    m->flags & MOPT_SKIP)
29595a916be1STheodore Ts'o 			continue;
2960e3645d72SZhang Yi 
2961e3645d72SZhang Yi 		if (opt_2) {
2962e3645d72SZhang Yi 			mount_opt = sbi->s_mount_opt2;
2963e3645d72SZhang Yi 			def_mount_opt = sbi->s_def_mount_opt2;
2964e3645d72SZhang Yi 		} else {
2965e3645d72SZhang Yi 			mount_opt = sbi->s_mount_opt;
2966e3645d72SZhang Yi 			def_mount_opt = sbi->s_def_mount_opt;
2967e3645d72SZhang Yi 		}
2968e3645d72SZhang Yi 		/* skip if same as the default */
2969e3645d72SZhang Yi 		if (!nodefs && !(m->mount_opt & (mount_opt ^ def_mount_opt)))
2970e3645d72SZhang Yi 			continue;
2971e3645d72SZhang Yi 		/* select Opt_noFoo vs Opt_Foo */
29725a916be1STheodore Ts'o 		if ((want_set &&
2973e3645d72SZhang Yi 		     (mount_opt & m->mount_opt) != m->mount_opt) ||
2974e3645d72SZhang Yi 		    (!want_set && (mount_opt & m->mount_opt)))
2975e3645d72SZhang Yi 			continue;
29765a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
29775a916be1STheodore Ts'o 	}
29785a916be1STheodore Ts'o 
297908cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
29805a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
298108cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
298208cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
298308cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
29845a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
298508cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
298608cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
298766acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
29885a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
29895a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
29902adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
29915a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
29922adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
29935a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
299466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
29955a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
299666acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
29975a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
299866acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
29995a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
300066acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
30015a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
300268afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
3003e3645d72SZhang Yi 			(sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
30042adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
30055a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
30062adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
30075a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
30082adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
30095a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
30105a916be1STheodore Ts'o 	}
301166acdcf4STheodore Ts'o 	if (nodefs ||
301266acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
30135a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
30142adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
30152adf6da8STheodore Ts'o 
3016ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
301766acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
30185a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
3019df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
3020df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
30217915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
30227915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
3023ed318a6cSEric Biggers 
3024ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
30252adf6da8STheodore Ts'o 
30264f74d15fSEric Biggers 	if (sb->s_flags & SB_INLINECRYPT)
30274f74d15fSEric Biggers 		SEQ_OPTS_PUTS("inlinecrypt");
30284f74d15fSEric Biggers 
30299cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
30309cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
30319cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
30329cb20f94SIra Weiny 		else
30339cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
30349cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
30359cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
30369cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
30379cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
30389cb20f94SIra Weiny 	}
30393fa5d23eSOjaswin Mujoo 
30403fa5d23eSOjaswin Mujoo 	if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
30413fa5d23eSOjaswin Mujoo 			!test_opt2(sb, MB_OPTIMIZE_SCAN)) {
30423fa5d23eSOjaswin Mujoo 		SEQ_OPTS_PUTS("mb_optimize_scan=0");
30433fa5d23eSOjaswin Mujoo 	} else if (sbi->s_groups_count < MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
30443fa5d23eSOjaswin Mujoo 			test_opt2(sb, MB_OPTIMIZE_SCAN)) {
30453fa5d23eSOjaswin Mujoo 		SEQ_OPTS_PUTS("mb_optimize_scan=1");
30463fa5d23eSOjaswin Mujoo 	}
30473fa5d23eSOjaswin Mujoo 
30482adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
30492adf6da8STheodore Ts'o 	return 0;
30502adf6da8STheodore Ts'o }
30512adf6da8STheodore Ts'o 
305266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
305366acdcf4STheodore Ts'o {
305466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
305566acdcf4STheodore Ts'o }
305666acdcf4STheodore Ts'o 
3057ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
305866acdcf4STheodore Ts'o {
305966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
306066acdcf4STheodore Ts'o 	int rc;
306166acdcf4STheodore Ts'o 
3062bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
306366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
306466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
306566acdcf4STheodore Ts'o 	return rc;
306666acdcf4STheodore Ts'o }
306766acdcf4STheodore Ts'o 
3068617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
3069ac27a0ecSDave Kleikamp 			    int read_only)
3070ac27a0ecSDave Kleikamp {
3071617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3072c89128a0SJaegeuk Kim 	int err = 0;
3073ac27a0ecSDave Kleikamp 
3074617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
3075b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
3076b31e1552SEric Sandeen 			 "forcing read-only mode");
3077c89128a0SJaegeuk Kim 		err = -EROFS;
30785adaccacSyangerkun 		goto done;
3079ac27a0ecSDave Kleikamp 	}
3080ac27a0ecSDave Kleikamp 	if (read_only)
3081281b5995STheodore Ts'o 		goto done;
3082617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
3083b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
3084b31e1552SEric Sandeen 			 "running e2fsck is recommended");
3085c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
3086b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3087b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
3088b31e1552SEric Sandeen 			 "running e2fsck is recommended");
3089ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
3090ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
3091ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
3092b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3093b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
3094b31e1552SEric Sandeen 			 "running e2fsck is recommended");
3095ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
30966a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
30976a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
3098b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3099b31e1552SEric Sandeen 			 "warning: checktime reached, "
3100b31e1552SEric Sandeen 			 "running e2fsck is recommended");
31010390131bSFrank Mayhar 	if (!sbi->s_journal)
3102216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
3103ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
3104617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
3105e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
31066a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
310702f310fcSJan Kara 	if (sbi->s_journal) {
3108e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
310902f310fcSJan Kara 		if (ext4_has_feature_orphan_file(sb))
311002f310fcSJan Kara 			ext4_set_feature_orphan_present(sb);
311102f310fcSJan Kara 	}
3112ac27a0ecSDave Kleikamp 
31134392fbc4SJan Kara 	err = ext4_commit_super(sb);
3114281b5995STheodore Ts'o done:
3115ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
3116a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
3117a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
3118ac27a0ecSDave Kleikamp 			sb->s_blocksize,
3119ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
3120617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
3121617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
3122a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
3123c89128a0SJaegeuk Kim 	return err;
3124ac27a0ecSDave Kleikamp }
3125ac27a0ecSDave Kleikamp 
3126117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
3127117fff10STheodore Ts'o {
3128117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
31297c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
313037b0b6b8SDan Carpenter 	int size, i, j;
3131117fff10STheodore Ts'o 
3132117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
3133117fff10STheodore Ts'o 		return 0;
3134117fff10STheodore Ts'o 
3135117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
3136117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
3137117fff10STheodore Ts'o 		return 0;
3138117fff10STheodore Ts'o 
31397c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
31407c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
3141117fff10STheodore Ts'o 	if (!new_groups) {
31427c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
31437c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
3144117fff10STheodore Ts'o 		return -ENOMEM;
3145117fff10STheodore Ts'o 	}
31467c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
31477c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
31487c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
31497c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
31507c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
315137b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
315237b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
31537c990728SSuraj Jitindar Singh 			kvfree(new_groups);
31547c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
31557c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
31567c990728SSuraj Jitindar Singh 			return -ENOMEM;
3157117fff10STheodore Ts'o 		}
31587c990728SSuraj Jitindar Singh 	}
31597c990728SSuraj Jitindar Singh 	rcu_read_lock();
31607c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
31617c990728SSuraj Jitindar Singh 	if (old_groups)
31627c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
31637c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
31647c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
31657c990728SSuraj Jitindar Singh 	rcu_read_unlock();
31667c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
31677c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
31687c990728SSuraj Jitindar Singh 	if (old_groups)
31697c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
3170117fff10STheodore Ts'o 	return 0;
3171117fff10STheodore Ts'o }
3172117fff10STheodore Ts'o 
3173772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
3174772cb7c8SJose R. Santos {
3175772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3176772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
31777c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
3178772cb7c8SJose R. Santos 	ext4_group_t flex_group;
3179117fff10STheodore Ts'o 	int i, err;
3180772cb7c8SJose R. Santos 
3181503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
3182d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
3183772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
3184772cb7c8SJose R. Santos 		return 1;
3185772cb7c8SJose R. Santos 	}
3186772cb7c8SJose R. Santos 
3187117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
3188117fff10STheodore Ts'o 	if (err)
3189772cb7c8SJose R. Santos 		goto failed;
3190772cb7c8SJose R. Santos 
3191772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
319288b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3193772cb7c8SJose R. Santos 
3194772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
31957c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
31967c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
319790ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
31987c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
31997c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
3200772cb7c8SJose R. Santos 	}
3201772cb7c8SJose R. Santos 
3202772cb7c8SJose R. Santos 	return 1;
3203772cb7c8SJose R. Santos failed:
3204772cb7c8SJose R. Santos 	return 0;
3205772cb7c8SJose R. Santos }
3206772cb7c8SJose R. Santos 
3207e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
3208717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
3209717d50e4SAndreas Dilger {
3210b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
3211717d50e4SAndreas Dilger 	__u16 crc = 0;
3212717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
3213e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3214717d50e4SAndreas Dilger 
32159aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
3216feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
3217feb0ab32SDarrick J. Wong 		__u32 csum32;
3218b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
3219feb0ab32SDarrick J. Wong 
3220feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
3221feb0ab32SDarrick J. Wong 				     sizeof(le_group));
3222b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
3223b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
3224b47820edSDaeho Jeong 				     sizeof(dummy_csum));
3225b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
3226b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
3227b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
3228b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
3229feb0ab32SDarrick J. Wong 
3230feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
3231feb0ab32SDarrick J. Wong 		goto out;
3232feb0ab32SDarrick J. Wong 	}
3233feb0ab32SDarrick J. Wong 
3234feb0ab32SDarrick J. Wong 	/* old crc16 code */
3235e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
3236813d32f9SDarrick J. Wong 		return 0;
3237813d32f9SDarrick J. Wong 
3238717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
3239717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
3240717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
3241717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
3242717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
32434f043518STudor Ambarus 	if (ext4_has_feature_64bit(sb) && offset < sbi->s_desc_size)
3244717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
32454f043518STudor Ambarus 			    sbi->s_desc_size - offset);
3246717d50e4SAndreas Dilger 
3247feb0ab32SDarrick J. Wong out:
3248717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
3249717d50e4SAndreas Dilger }
3250717d50e4SAndreas Dilger 
3251feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
3252717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
3253717d50e4SAndreas Dilger {
3254feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
3255e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
3256717d50e4SAndreas Dilger 		return 0;
3257717d50e4SAndreas Dilger 
3258717d50e4SAndreas Dilger 	return 1;
3259717d50e4SAndreas Dilger }
3260717d50e4SAndreas Dilger 
3261feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
3262feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
3263feb0ab32SDarrick J. Wong {
3264feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
3265feb0ab32SDarrick J. Wong 		return;
3266e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
3267feb0ab32SDarrick J. Wong }
3268feb0ab32SDarrick J. Wong 
3269ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
3270bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
3271829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
3272bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
3273ac27a0ecSDave Kleikamp {
3274617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3275617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
3276617ba13bSMingming Cao 	ext4_fsblk_t last_block;
327744de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
3278bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
3279bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
3280bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
3281ce421581SJose R. Santos 	int flexbg_flag = 0;
3282bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
3283ac27a0ecSDave Kleikamp 
3284e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
3285ce421581SJose R. Santos 		flexbg_flag = 1;
3286ce421581SJose R. Santos 
3287617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
3288ac27a0ecSDave Kleikamp 
3289197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
3290197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
3291197cd65aSAkinobu Mita 
3292ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
3293bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
3294ac27a0ecSDave Kleikamp 		else
3295ac27a0ecSDave Kleikamp 			last_block = first_block +
3296617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
3297ac27a0ecSDave Kleikamp 
3298bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
3299bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3300bfff6873SLukas Czerner 			grp = i;
3301bfff6873SLukas Czerner 
33028fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
3303829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
3304829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3305829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
3306829fa70dSTheodore Ts'o 				 "superblock", i);
330718db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
330818db4b4eSTheodore Ts'o 				return 0;
3309829fa70dSTheodore Ts'o 		}
331077260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
331177260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
331277260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
331377260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
331477260807STheodore Ts'o 				 "block group descriptors", i);
331577260807STheodore Ts'o 			if (!sb_rdonly(sb))
331677260807STheodore Ts'o 				return 0;
331777260807STheodore Ts'o 		}
33182b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
3319b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3320a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
3321b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
3322ac27a0ecSDave Kleikamp 			return 0;
3323ac27a0ecSDave Kleikamp 		}
33248fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
3325829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
3326829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3327829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
3328829fa70dSTheodore Ts'o 				 "superblock", i);
332918db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
333018db4b4eSTheodore Ts'o 				return 0;
3331829fa70dSTheodore Ts'o 		}
333277260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
333377260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
333477260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
333577260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
333677260807STheodore Ts'o 				 "block group descriptors", i);
333777260807STheodore Ts'o 			if (!sb_rdonly(sb))
333877260807STheodore Ts'o 				return 0;
333977260807STheodore Ts'o 		}
33402b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
3341b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3342a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
3343b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
3344ac27a0ecSDave Kleikamp 			return 0;
3345ac27a0ecSDave Kleikamp 		}
33468fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
3347829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
3348829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3349829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
3350829fa70dSTheodore Ts'o 				 "superblock", i);
335118db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
335218db4b4eSTheodore Ts'o 				return 0;
3353829fa70dSTheodore Ts'o 		}
335477260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
335577260807STheodore Ts'o 		    inode_table <= last_bg_block) {
335677260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
335777260807STheodore Ts'o 				 "Inode table for group %u overlaps "
335877260807STheodore Ts'o 				 "block group descriptors", i);
335977260807STheodore Ts'o 			if (!sb_rdonly(sb))
336077260807STheodore Ts'o 				return 0;
336177260807STheodore Ts'o 		}
3362bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
33632b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
3364b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3365a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
3366b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
3367ac27a0ecSDave Kleikamp 			return 0;
3368ac27a0ecSDave Kleikamp 		}
3369955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
3370feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
3371b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3372b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
3373e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
3374fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
3375bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
3376955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
3377717d50e4SAndreas Dilger 				return 0;
3378717d50e4SAndreas Dilger 			}
33797ee1ec4cSLi Zefan 		}
3380955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
3381ce421581SJose R. Santos 		if (!flexbg_flag)
3382617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
3383ac27a0ecSDave Kleikamp 	}
3384bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
3385bfff6873SLukas Czerner 		*first_not_zeroed = grp;
3386ac27a0ecSDave Kleikamp 	return 1;
3387ac27a0ecSDave Kleikamp }
3388ac27a0ecSDave Kleikamp 
3389cd2291a4SEric Sandeen /*
3390cd2291a4SEric Sandeen  * Maximal extent format file size.
3391cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
3392cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
3393cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
3394cd2291a4SEric Sandeen  * so that won't be a limiting factor.
3395cd2291a4SEric Sandeen  *
3396f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
3397f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
3398f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
3399f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
3400f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3401f17722f9SLukas Czerner  *
3402cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3403cd2291a4SEric Sandeen  */
3404f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
3405cd2291a4SEric Sandeen {
3406cd2291a4SEric Sandeen 	loff_t res;
3407cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
3408cd2291a4SEric Sandeen 
340972deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
341072deb455SChristoph Hellwig 
341172deb455SChristoph Hellwig 	if (!has_huge_files) {
3412cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
3413cd2291a4SEric Sandeen 
3414cd2291a4SEric Sandeen 		/* total blocks in file system block size */
3415cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
3416cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
3417cd2291a4SEric Sandeen 	}
3418cd2291a4SEric Sandeen 
3419f17722f9SLukas Czerner 	/*
3420f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
3421f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
3422f17722f9SLukas Czerner 	 * size
3423f17722f9SLukas Czerner 	 */
3424f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
3425cd2291a4SEric Sandeen 	res <<= blkbits;
3426cd2291a4SEric Sandeen 
3427cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
3428cd2291a4SEric Sandeen 	if (res > upper_limit)
3429cd2291a4SEric Sandeen 		res = upper_limit;
3430cd2291a4SEric Sandeen 
3431cd2291a4SEric Sandeen 	return res;
3432cd2291a4SEric Sandeen }
3433ac27a0ecSDave Kleikamp 
3434ac27a0ecSDave Kleikamp /*
3435cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
34360fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
34370fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
3438ac27a0ecSDave Kleikamp  */
3439f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3440ac27a0ecSDave Kleikamp {
34415c93e8ecSZhang Yi 	loff_t upper_limit, res = EXT4_NDIR_BLOCKS;
34420fc1b451SAneesh Kumar K.V 	int meta_blocks;
34435c93e8ecSZhang Yi 	unsigned int ppb = 1 << (bits - 2);
344475ca6ad4SRitesh Harjani 
344575ca6ad4SRitesh Harjani 	/*
344675ca6ad4SRitesh Harjani 	 * This is calculated to be the largest file size for a dense, block
34470b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
34480b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
34490b8e58a1SAndreas Dilger 	 *
34500b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
34510b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
34520fc1b451SAneesh Kumar K.V 	 */
345372deb455SChristoph Hellwig 	if (!has_huge_files) {
34540fc1b451SAneesh Kumar K.V 		/*
345572deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
345672deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
345772deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
34580fc1b451SAneesh Kumar K.V 		 */
34590fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
34600fc1b451SAneesh Kumar K.V 
34610fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
34620fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
34630fc1b451SAneesh Kumar K.V 
34640fc1b451SAneesh Kumar K.V 	} else {
34658180a562SAneesh Kumar K.V 		/*
34668180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
34678180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
34688180a562SAneesh Kumar K.V 		 * represent total number of blocks in
34698180a562SAneesh Kumar K.V 		 * file system block size
34708180a562SAneesh Kumar K.V 		 */
34710fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
34720fc1b451SAneesh Kumar K.V 
34730fc1b451SAneesh Kumar K.V 	}
34740fc1b451SAneesh Kumar K.V 
34755c93e8ecSZhang Yi 	/* Compute how many blocks we can address by block tree */
34765c93e8ecSZhang Yi 	res += ppb;
34775c93e8ecSZhang Yi 	res += ppb * ppb;
34785c93e8ecSZhang Yi 	res += ((loff_t)ppb) * ppb * ppb;
34795c93e8ecSZhang Yi 	/* Compute how many metadata blocks are needed */
34805c93e8ecSZhang Yi 	meta_blocks = 1;
34815c93e8ecSZhang Yi 	meta_blocks += 1 + ppb;
34825c93e8ecSZhang Yi 	meta_blocks += 1 + ppb + ppb * ppb;
34835c93e8ecSZhang Yi 	/* Does block tree limit file size? */
34845c93e8ecSZhang Yi 	if (res + meta_blocks <= upper_limit)
34855c93e8ecSZhang Yi 		goto check_lfs;
34865c93e8ecSZhang Yi 
34875c93e8ecSZhang Yi 	res = upper_limit;
34885c93e8ecSZhang Yi 	/* How many metadata blocks are needed for addressing upper_limit? */
34895c93e8ecSZhang Yi 	upper_limit -= EXT4_NDIR_BLOCKS;
34900fc1b451SAneesh Kumar K.V 	/* indirect blocks */
34910fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
34925c93e8ecSZhang Yi 	upper_limit -= ppb;
34930fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
34945c93e8ecSZhang Yi 	if (upper_limit < ppb * ppb) {
34955c93e8ecSZhang Yi 		meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb);
34965c93e8ecSZhang Yi 		res -= meta_blocks;
34975c93e8ecSZhang Yi 		goto check_lfs;
34985c93e8ecSZhang Yi 	}
34995c93e8ecSZhang Yi 	meta_blocks += 1 + ppb;
35005c93e8ecSZhang Yi 	upper_limit -= ppb * ppb;
35015c93e8ecSZhang Yi 	/* tripple indirect blocks for the rest */
35025c93e8ecSZhang Yi 	meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) +
35035c93e8ecSZhang Yi 		DIV_ROUND_UP_ULL(upper_limit, ppb*ppb);
35045c93e8ecSZhang Yi 	res -= meta_blocks;
35055c93e8ecSZhang Yi check_lfs:
3506ac27a0ecSDave Kleikamp 	res <<= bits;
35070fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
35080fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
35090fc1b451SAneesh Kumar K.V 
35105c93e8ecSZhang Yi 	return res;
3511ac27a0ecSDave Kleikamp }
3512ac27a0ecSDave Kleikamp 
3513617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
351470bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3515ac27a0ecSDave Kleikamp {
3516617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3517fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3518ac27a0ecSDave Kleikamp 	int has_super = 0;
3519ac27a0ecSDave Kleikamp 
3520ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3521ac27a0ecSDave Kleikamp 
3522e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
352370bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3524ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3525617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3526ac27a0ecSDave Kleikamp 		has_super = 1;
35270b8e58a1SAndreas Dilger 
3528bd63f6b0SDarrick J. Wong 	/*
3529bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3530bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3531bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3532bd63f6b0SDarrick J. Wong 	 * compensate.
3533bd63f6b0SDarrick J. Wong 	 */
3534bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
353549598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3536bd63f6b0SDarrick J. Wong 		has_super++;
3537bd63f6b0SDarrick J. Wong 
3538617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3539ac27a0ecSDave Kleikamp }
3540ac27a0ecSDave Kleikamp 
3541c9de560dSAlex Tomas /**
3542c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3543c9de560dSAlex Tomas  * @sbi: In memory super block info
3544c9de560dSAlex Tomas  *
3545c9de560dSAlex Tomas  * If we have specified it via mount option, then
3546c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3547c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3548c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3549c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3550c9de560dSAlex Tomas  *
3551c9de560dSAlex Tomas  */
3552c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3553c9de560dSAlex Tomas {
3554c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3555c9de560dSAlex Tomas 	unsigned long stripe_width =
3556c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
35573eb08658SDan Ehrenberg 	int ret;
3558c9de560dSAlex Tomas 
3559c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
35603eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
35615469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
35623eb08658SDan Ehrenberg 		ret = stripe_width;
35635469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
35643eb08658SDan Ehrenberg 		ret = stride;
35653eb08658SDan Ehrenberg 	else
35663eb08658SDan Ehrenberg 		ret = 0;
3567c9de560dSAlex Tomas 
35683eb08658SDan Ehrenberg 	/*
35693eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
35703eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
35713eb08658SDan Ehrenberg 	 */
35723eb08658SDan Ehrenberg 	if (ret <= 1)
35733eb08658SDan Ehrenberg 		ret = 0;
3574c9de560dSAlex Tomas 
35753eb08658SDan Ehrenberg 	return ret;
3576c9de560dSAlex Tomas }
3577ac27a0ecSDave Kleikamp 
3578a13fb1a4SEric Sandeen /*
3579a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3580a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3581a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3582a13fb1a4SEric Sandeen  * 0 if it cannot be.
3583a13fb1a4SEric Sandeen  */
358425c6d98fSJan Kara int ext4_feature_set_ok(struct super_block *sb, int readonly)
3585a13fb1a4SEric Sandeen {
3586e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3587a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3588a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3589a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3590a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3591a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3592a13fb1a4SEric Sandeen 		return 0;
3593a13fb1a4SEric Sandeen 	}
3594a13fb1a4SEric Sandeen 
35955298d4bfSChristoph Hellwig #if !IS_ENABLED(CONFIG_UNICODE)
3596c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3597c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3598c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3599c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3600c83ad55eSGabriel Krisman Bertazi 		return 0;
3601c83ad55eSGabriel Krisman Bertazi 	}
3602c83ad55eSGabriel Krisman Bertazi #endif
3603c83ad55eSGabriel Krisman Bertazi 
3604a13fb1a4SEric Sandeen 	if (readonly)
3605a13fb1a4SEric Sandeen 		return 1;
3606a13fb1a4SEric Sandeen 
3607e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
36082cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
36091751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
36102cb5cc8bSDarrick J. Wong 		return 1;
36112cb5cc8bSDarrick J. Wong 	}
36122cb5cc8bSDarrick J. Wong 
3613a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3614e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3615a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3616a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3617a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3618a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3619a13fb1a4SEric Sandeen 		return 0;
3620a13fb1a4SEric Sandeen 	}
3621e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3622bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3623bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3624bab08ab9STheodore Ts'o 			 "extents feature\n");
3625bab08ab9STheodore Ts'o 		return 0;
3626bab08ab9STheodore Ts'o 	}
36277c319d32SAditya Kali 
36289db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3629d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3630d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
36317c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3632d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3633689c958cSLi Xi 		return 0;
3634689c958cSLi Xi 	}
36357c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3636a13fb1a4SEric Sandeen 	return 1;
3637a13fb1a4SEric Sandeen }
3638a13fb1a4SEric Sandeen 
363966e61a9eSTheodore Ts'o /*
364066e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
364166e61a9eSTheodore Ts'o  * on the file system
364266e61a9eSTheodore Ts'o  */
3643235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
364466e61a9eSTheodore Ts'o {
3645235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3646235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3647235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
364866e61a9eSTheodore Ts'o 
364966e61a9eSTheodore Ts'o 	if (es->s_error_count)
3650ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3651ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
365266e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
365366e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
36546a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
36556a0678a7SArnd Bergmann 		       sb->s_id,
36566a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
365766e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
365866e61a9eSTheodore Ts'o 		       es->s_first_error_func,
365966e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
366066e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3661651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
366266e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
366366e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3664651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
366566e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3666651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
366766e61a9eSTheodore Ts'o 	}
366866e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
36696a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
36706a0678a7SArnd Bergmann 		       sb->s_id,
36716a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
367266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
367366e61a9eSTheodore Ts'o 		       es->s_last_error_func,
367466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
367566e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3676651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
367766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
367866e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3679651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
368066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3681651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
368266e61a9eSTheodore Ts'o 	}
368366e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
368466e61a9eSTheodore Ts'o }
368566e61a9eSTheodore Ts'o 
3686bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3687bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3688bfff6873SLukas Czerner {
3689bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
36903d392b26STheodore Ts'o 	struct super_block *sb = elr->lr_super;
36913d392b26STheodore Ts'o 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
36923d392b26STheodore Ts'o 	ext4_group_t group = elr->lr_next_group;
36933d392b26STheodore Ts'o 	unsigned int prefetch_ios = 0;
3694bfff6873SLukas Czerner 	int ret = 0;
36954f3d1e45SOjaswin Mujoo 	int nr = EXT4_SB(sb)->s_mb_prefetch;
369639fec688SShaoying Xu 	u64 start_time;
3697bfff6873SLukas Czerner 
36983d392b26STheodore Ts'o 	if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
36994f3d1e45SOjaswin Mujoo 		elr->lr_next_group = ext4_mb_prefetch(sb, group, nr, &prefetch_ios);
37004f3d1e45SOjaswin Mujoo 		ext4_mb_prefetch_fini(sb, elr->lr_next_group, nr);
37014f3d1e45SOjaswin Mujoo 		trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group, nr);
37023d392b26STheodore Ts'o 		if (group >= elr->lr_next_group) {
37033d392b26STheodore Ts'o 			ret = 1;
37043d392b26STheodore Ts'o 			if (elr->lr_first_not_zeroed != ngroups &&
37053d392b26STheodore Ts'o 			    !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
37063d392b26STheodore Ts'o 				elr->lr_next_group = elr->lr_first_not_zeroed;
37073d392b26STheodore Ts'o 				elr->lr_mode = EXT4_LI_MODE_ITABLE;
37083d392b26STheodore Ts'o 				ret = 0;
37093d392b26STheodore Ts'o 			}
37103d392b26STheodore Ts'o 		}
37113d392b26STheodore Ts'o 		return ret;
37123d392b26STheodore Ts'o 	}
3713bfff6873SLukas Czerner 
37143d392b26STheodore Ts'o 	for (; group < ngroups; group++) {
3715bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3716bfff6873SLukas Czerner 		if (!gdp) {
3717bfff6873SLukas Czerner 			ret = 1;
3718bfff6873SLukas Czerner 			break;
3719bfff6873SLukas Czerner 		}
3720bfff6873SLukas Czerner 
3721bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3722bfff6873SLukas Czerner 			break;
3723bfff6873SLukas Czerner 	}
3724bfff6873SLukas Czerner 
37257f511862STheodore Ts'o 	if (group >= ngroups)
3726bfff6873SLukas Czerner 		ret = 1;
3727bfff6873SLukas Czerner 
3728bfff6873SLukas Czerner 	if (!ret) {
372939fec688SShaoying Xu 		start_time = ktime_get_real_ns();
3730bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3731bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
37323d392b26STheodore Ts'o 		trace_ext4_lazy_itable_init(sb, group);
3733bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
373439fec688SShaoying Xu 			elr->lr_timeout = nsecs_to_jiffies((ktime_get_real_ns() - start_time) *
373539fec688SShaoying Xu 				EXT4_SB(elr->lr_super)->s_li_wait_mult);
3736bfff6873SLukas Czerner 		}
3737bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3738bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3739bfff6873SLukas Czerner 	}
3740bfff6873SLukas Czerner 	return ret;
3741bfff6873SLukas Czerner }
3742bfff6873SLukas Czerner 
3743bfff6873SLukas Czerner /*
3744bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
37454ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3746bfff6873SLukas Czerner  */
3747bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3748bfff6873SLukas Czerner {
3749bfff6873SLukas Czerner 	if (!elr)
3750bfff6873SLukas Czerner 		return;
3751bfff6873SLukas Czerner 
3752bfff6873SLukas Czerner 	list_del(&elr->lr_request);
37533d392b26STheodore Ts'o 	EXT4_SB(elr->lr_super)->s_li_request = NULL;
3754bfff6873SLukas Czerner 	kfree(elr);
3755bfff6873SLukas Czerner }
3756bfff6873SLukas Czerner 
3757bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3758bfff6873SLukas Czerner {
37591bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
37601bb933fbSLukas Czerner 	if (!ext4_li_info) {
37611bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3762bfff6873SLukas Czerner 		return;
37631bb933fbSLukas Czerner 	}
3764bfff6873SLukas Czerner 
3765bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
37661bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3767bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
37681bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3769bfff6873SLukas Czerner }
3770bfff6873SLukas Czerner 
37718f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
37728f1f7453SEric Sandeen 
3773bfff6873SLukas Czerner /*
3774bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3775bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3776bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3777bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3778bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3779bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3780bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3781bfff6873SLukas Czerner  */
3782bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3783bfff6873SLukas Czerner {
3784c30365b9SYu Zhe 	struct ext4_lazy_init *eli = arg;
3785bfff6873SLukas Czerner 	struct list_head *pos, *n;
3786bfff6873SLukas Czerner 	struct ext4_li_request *elr;
37874ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3788bfff6873SLukas Czerner 
3789bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
37903b575495SLalith Rajendran 	set_freezable();
3791bfff6873SLukas Czerner 
3792bfff6873SLukas Czerner cont_thread:
3793bfff6873SLukas Czerner 	while (true) {
3794bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3795bfff6873SLukas Czerner 
3796bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3797bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3798bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3799bfff6873SLukas Czerner 			goto exit_thread;
3800bfff6873SLukas Czerner 		}
3801bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3802e22834f0SDmitry Monakhov 			int err = 0;
3803e22834f0SDmitry Monakhov 			int progress = 0;
3804bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3805bfff6873SLukas Czerner 					 lr_request);
3806bfff6873SLukas Czerner 
3807e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3808e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3809e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3810e22834f0SDmitry Monakhov 				continue;
3811e22834f0SDmitry Monakhov 			}
3812e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3813e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3814e22834f0SDmitry Monakhov 					progress = 1;
3815e22834f0SDmitry Monakhov 					/*
3816e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3817e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3818e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3819e22834f0SDmitry Monakhov 					 */
3820e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3821e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3822e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3823e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3824e22834f0SDmitry Monakhov 					n = pos->next;
3825e22834f0SDmitry Monakhov 				}
3826e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3827e22834f0SDmitry Monakhov 			}
3828b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3829e22834f0SDmitry Monakhov 			if (err) {
3830bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3831bfff6873SLukas Czerner 				continue;
3832bfff6873SLukas Czerner 			}
3833e22834f0SDmitry Monakhov 			if (!progress) {
3834e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
38358032bf12SJason A. Donenfeld 					get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
3836b2c78cd0STheodore Ts'o 			}
3837bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3838bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3839bfff6873SLukas Czerner 		}
3840bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3841bfff6873SLukas Czerner 
3842a0acae0eSTejun Heo 		try_to_freeze();
3843bfff6873SLukas Czerner 
38444ed5c033SLukas Czerner 		cur = jiffies;
38454ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3846f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3847bfff6873SLukas Czerner 			cond_resched();
3848bfff6873SLukas Czerner 			continue;
3849bfff6873SLukas Czerner 		}
3850bfff6873SLukas Czerner 
38514ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
38524ed5c033SLukas Czerner 
38538f1f7453SEric Sandeen 		if (kthread_should_stop()) {
38548f1f7453SEric Sandeen 			ext4_clear_request_list();
38558f1f7453SEric Sandeen 			goto exit_thread;
38568f1f7453SEric Sandeen 		}
3857bfff6873SLukas Czerner 	}
3858bfff6873SLukas Czerner 
3859bfff6873SLukas Czerner exit_thread:
3860bfff6873SLukas Czerner 	/*
3861bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3862bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3863bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3864bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3865bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3866bfff6873SLukas Czerner 	 * new one.
3867bfff6873SLukas Czerner 	 */
3868bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3869bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3870bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3871bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3872bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3873bfff6873SLukas Czerner 		goto cont_thread;
3874bfff6873SLukas Czerner 	}
3875bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3876bfff6873SLukas Czerner 	kfree(ext4_li_info);
3877bfff6873SLukas Czerner 	ext4_li_info = NULL;
3878bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3879bfff6873SLukas Czerner 
3880bfff6873SLukas Czerner 	return 0;
3881bfff6873SLukas Czerner }
3882bfff6873SLukas Czerner 
3883bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3884bfff6873SLukas Czerner {
3885bfff6873SLukas Czerner 	struct list_head *pos, *n;
3886bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3887bfff6873SLukas Czerner 
3888bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3889bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3890bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3891bfff6873SLukas Czerner 				 lr_request);
3892bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3893bfff6873SLukas Czerner 	}
3894bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3895bfff6873SLukas Czerner }
3896bfff6873SLukas Czerner 
3897bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3898bfff6873SLukas Czerner {
38998f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
39008f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
39018f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
39028f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3903bfff6873SLukas Czerner 		ext4_clear_request_list();
3904bfff6873SLukas Czerner 		kfree(ext4_li_info);
3905bfff6873SLukas Czerner 		ext4_li_info = NULL;
390692b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3907bfff6873SLukas Czerner 				 "initialization thread\n",
3908bfff6873SLukas Czerner 				 err);
3909bfff6873SLukas Czerner 		return err;
3910bfff6873SLukas Czerner 	}
3911bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3912bfff6873SLukas Czerner 	return 0;
3913bfff6873SLukas Czerner }
3914bfff6873SLukas Czerner 
3915bfff6873SLukas Czerner /*
3916bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3917bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3918bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3919bfff6873SLukas Czerner  * groups and return total number of groups.
3920bfff6873SLukas Czerner  */
3921bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3922bfff6873SLukas Czerner {
3923bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3924bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3925bfff6873SLukas Czerner 
39268844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
39278844618dSTheodore Ts'o 		return ngroups;
39288844618dSTheodore Ts'o 
3929bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3930bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3931bfff6873SLukas Czerner 		if (!gdp)
3932bfff6873SLukas Czerner 			continue;
3933bfff6873SLukas Czerner 
393450122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3935bfff6873SLukas Czerner 			break;
3936bfff6873SLukas Czerner 	}
3937bfff6873SLukas Czerner 
3938bfff6873SLukas Czerner 	return group;
3939bfff6873SLukas Czerner }
3940bfff6873SLukas Czerner 
3941bfff6873SLukas Czerner static int ext4_li_info_new(void)
3942bfff6873SLukas Czerner {
3943bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3944bfff6873SLukas Czerner 
3945bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3946bfff6873SLukas Czerner 	if (!eli)
3947bfff6873SLukas Czerner 		return -ENOMEM;
3948bfff6873SLukas Czerner 
3949bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3950bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3951bfff6873SLukas Czerner 
3952bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3953bfff6873SLukas Czerner 
3954bfff6873SLukas Czerner 	ext4_li_info = eli;
3955bfff6873SLukas Czerner 
3956bfff6873SLukas Czerner 	return 0;
3957bfff6873SLukas Czerner }
3958bfff6873SLukas Czerner 
3959bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3960bfff6873SLukas Czerner 					    ext4_group_t start)
3961bfff6873SLukas Czerner {
3962bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3963bfff6873SLukas Czerner 
3964bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3965bfff6873SLukas Czerner 	if (!elr)
3966bfff6873SLukas Czerner 		return NULL;
3967bfff6873SLukas Czerner 
3968bfff6873SLukas Czerner 	elr->lr_super = sb;
39693d392b26STheodore Ts'o 	elr->lr_first_not_zeroed = start;
397021175ca4SHarshad Shirwadkar 	if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
39713d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_ITABLE;
3972bfff6873SLukas Czerner 		elr->lr_next_group = start;
397321175ca4SHarshad Shirwadkar 	} else {
397421175ca4SHarshad Shirwadkar 		elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
39753d392b26STheodore Ts'o 	}
3976bfff6873SLukas Czerner 
3977bfff6873SLukas Czerner 	/*
3978bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3979bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3980bfff6873SLukas Czerner 	 * better.
3981bfff6873SLukas Czerner 	 */
39828032bf12SJason A. Donenfeld 	elr->lr_next_sched = jiffies + get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
3983bfff6873SLukas Czerner 	return elr;
3984bfff6873SLukas Czerner }
3985bfff6873SLukas Czerner 
39867f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3987bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3988bfff6873SLukas Czerner {
3989bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
39907f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
399149598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
39926c5a6cb9SAndrew Morton 	int ret = 0;
3993bfff6873SLukas Czerner 
39947f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
399551ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
399651ce6511SLukas Czerner 		/*
399751ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
399851ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
399951ce6511SLukas Czerner 		 */
400051ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
40017f511862STheodore Ts'o 		goto out;
400251ce6511SLukas Czerner 	}
4003bfff6873SLukas Czerner 
4004426d15adSJosh Triplett 	if (sb_rdonly(sb) ||
4005426d15adSJosh Triplett 	    (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
4006426d15adSJosh Triplett 	     (first_not_zeroed == ngroups || !test_opt(sb, INIT_INODE_TABLE))))
40077f511862STheodore Ts'o 		goto out;
4008bfff6873SLukas Czerner 
4009bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
40107f511862STheodore Ts'o 	if (!elr) {
40117f511862STheodore Ts'o 		ret = -ENOMEM;
40127f511862STheodore Ts'o 		goto out;
40137f511862STheodore Ts'o 	}
4014bfff6873SLukas Czerner 
4015bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
4016bfff6873SLukas Czerner 		ret = ext4_li_info_new();
4017bfff6873SLukas Czerner 		if (ret)
4018bfff6873SLukas Czerner 			goto out;
4019bfff6873SLukas Czerner 	}
4020bfff6873SLukas Czerner 
4021bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
4022bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
4023bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
4024bfff6873SLukas Czerner 
4025bfff6873SLukas Czerner 	sbi->s_li_request = elr;
402646e4690bSTao Ma 	/*
402746e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
402846e4690bSTao Ma 	 * the request_list and the removal and free of it is
402946e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
403046e4690bSTao Ma 	 */
403146e4690bSTao Ma 	elr = NULL;
4032bfff6873SLukas Czerner 
4033bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
4034bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
4035bfff6873SLukas Czerner 		if (ret)
4036bfff6873SLukas Czerner 			goto out;
4037bfff6873SLukas Czerner 	}
4038bfff6873SLukas Czerner out:
4039bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
4040beed5ecbSNicolas Kaiser 	if (ret)
4041bfff6873SLukas Czerner 		kfree(elr);
4042bfff6873SLukas Czerner 	return ret;
4043bfff6873SLukas Czerner }
4044bfff6873SLukas Czerner 
4045bfff6873SLukas Czerner /*
4046bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
4047bfff6873SLukas Czerner  * module unload.
4048bfff6873SLukas Czerner  */
4049bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
4050bfff6873SLukas Czerner {
4051bfff6873SLukas Czerner 	/*
4052bfff6873SLukas Czerner 	 * If thread exited earlier
4053bfff6873SLukas Czerner 	 * there's nothing to be done.
4054bfff6873SLukas Czerner 	 */
40558f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
4056bfff6873SLukas Czerner 		return;
4057bfff6873SLukas Czerner 
40588f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
4059bfff6873SLukas Czerner }
4060bfff6873SLukas Czerner 
406125ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
406225ed6e8aSDarrick J. Wong {
406325ed6e8aSDarrick J. Wong 	int ret = 1;
406425ed6e8aSDarrick J. Wong 	int compat, incompat;
406525ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
406625ed6e8aSDarrick J. Wong 
40679aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
4068db9ee220SDarrick J. Wong 		/* journal checksum v3 */
406925ed6e8aSDarrick J. Wong 		compat = 0;
4070db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
407125ed6e8aSDarrick J. Wong 	} else {
407225ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
407325ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
407425ed6e8aSDarrick J. Wong 		incompat = 0;
407525ed6e8aSDarrick J. Wong 	}
407625ed6e8aSDarrick J. Wong 
4077feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
4078feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
4079feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
4080feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
408125ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
408225ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
408325ed6e8aSDarrick J. Wong 				compat, 0,
408425ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
408525ed6e8aSDarrick J. Wong 				incompat);
408625ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
408725ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
408825ed6e8aSDarrick J. Wong 				compat, 0,
408925ed6e8aSDarrick J. Wong 				incompat);
409025ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
409125ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
409225ed6e8aSDarrick J. Wong 	} else {
4093feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
4094feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
409525ed6e8aSDarrick J. Wong 	}
409625ed6e8aSDarrick J. Wong 
409725ed6e8aSDarrick J. Wong 	return ret;
409825ed6e8aSDarrick J. Wong }
409925ed6e8aSDarrick J. Wong 
4100952fc18eSTheodore Ts'o /*
4101952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
4102952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
4103952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
4104952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
4105952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
4106952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
4107952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
4108952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
4109952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
4110952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
4111952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
4112952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
4113952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
4114952fc18eSTheodore Ts'o  */
4115952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
4116952fc18eSTheodore Ts'o 			  char *buf)
4117952fc18eSTheodore Ts'o {
4118952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
4119952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
4120952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
4121952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
4122952fc18eSTheodore Ts'o 	int			s, j, count = 0;
412310b01ee9STheodore Ts'o 	int			has_super = ext4_bg_has_super(sb, grp);
4124952fc18eSTheodore Ts'o 
4125e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
412610b01ee9STheodore Ts'o 		return (has_super + ext4_bg_num_gdb(sb, grp) +
412710b01ee9STheodore Ts'o 			(has_super ? le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0) +
41280548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
41290548bbb8STheodore Ts'o 
4130952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
4131952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
4132952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
4133952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
4134952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
4135952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
4136952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
4137952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4138952fc18eSTheodore Ts'o 			count++;
4139952fc18eSTheodore Ts'o 		}
4140952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
4141952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
4142952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4143952fc18eSTheodore Ts'o 			count++;
4144952fc18eSTheodore Ts'o 		}
4145952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
4146952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
4147952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
4148952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
4149952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
4150952fc18eSTheodore Ts'o 				count++;
4151952fc18eSTheodore Ts'o 			}
4152952fc18eSTheodore Ts'o 		if (i != grp)
4153952fc18eSTheodore Ts'o 			continue;
4154952fc18eSTheodore Ts'o 		s = 0;
4155952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
4156952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
4157952fc18eSTheodore Ts'o 			count++;
4158952fc18eSTheodore Ts'o 		}
4159c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
4160c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
4161c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
4162c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
4163c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
4164952fc18eSTheodore Ts'o 		}
4165c48ae41bSTheodore Ts'o 		count += j;
4166c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
4167c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
4168952fc18eSTheodore Ts'o 	}
4169952fc18eSTheodore Ts'o 	if (!count)
4170952fc18eSTheodore Ts'o 		return 0;
4171952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
4172952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
4173952fc18eSTheodore Ts'o }
4174952fc18eSTheodore Ts'o 
4175952fc18eSTheodore Ts'o /*
4176952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
4177952fc18eSTheodore Ts'o  */
4178952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
4179952fc18eSTheodore Ts'o {
4180952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4181952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
41823c816dedSEric Whitney 	struct inode *j_inode;
41833c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
4184952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4185952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
41864fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
4187952fc18eSTheodore Ts'o 
4188952fc18eSTheodore Ts'o 	if (!buf)
4189952fc18eSTheodore Ts'o 		return -ENOMEM;
4190952fc18eSTheodore Ts'o 
4191952fc18eSTheodore Ts'o 	/*
4192952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
4193952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
4194952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
4195952fc18eSTheodore Ts'o 	 */
4196952fc18eSTheodore Ts'o 
4197952fc18eSTheodore Ts'o 	/*
4198952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
4199952fc18eSTheodore Ts'o 	 */
4200952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
4201952fc18eSTheodore Ts'o 
4202952fc18eSTheodore Ts'o 	/*
4203952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
4204952fc18eSTheodore Ts'o 	 */
4205952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
4206952fc18eSTheodore Ts'o 		int blks;
4207952fc18eSTheodore Ts'o 
4208952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
4209952fc18eSTheodore Ts'o 		overhead += blks;
4210952fc18eSTheodore Ts'o 		if (blks)
4211952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
4212952fc18eSTheodore Ts'o 		cond_resched();
4213952fc18eSTheodore Ts'o 	}
42143c816dedSEric Whitney 
42153c816dedSEric Whitney 	/*
42163c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
42173c816dedSEric Whitney 	 * loaded or not
42183c816dedSEric Whitney 	 */
4219ee7ed3aaSChunguang Xu 	if (sbi->s_journal && !sbi->s_journal_bdev)
4220ede7dc7fSHarshad Shirwadkar 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
4221f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
4222f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
42233c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
42243c816dedSEric Whitney 		if (j_inode) {
42253c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
42263c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
42273c816dedSEric Whitney 			iput(j_inode);
42283c816dedSEric Whitney 		} else {
42293c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
42303c816dedSEric Whitney 		}
42313c816dedSEric Whitney 	}
4232952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
4233952fc18eSTheodore Ts'o 	smp_wmb();
4234952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
4235952fc18eSTheodore Ts'o 	return 0;
4236952fc18eSTheodore Ts'o }
4237952fc18eSTheodore Ts'o 
4238b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
423927dd4385SLukas Czerner {
424027dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
4241b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
424227dd4385SLukas Czerner 
424327dd4385SLukas Czerner 	/*
424430fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
424530fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
424630fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
424730fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
424830fac0f7SJan Kara 	 */
4249e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
4250b5799018STheodore Ts'o 		return;
425130fac0f7SJan Kara 	/*
425227dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
425327dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
425427dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
4255556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
425627dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
425727dd4385SLukas Czerner 	 * very rare.
425827dd4385SLukas Czerner 	 */
4259b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
4260b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
426127dd4385SLukas Czerner 
426227dd4385SLukas Czerner 	do_div(resv_clusters, 50);
426327dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
426427dd4385SLukas Czerner 
4265b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
426627dd4385SLukas Czerner }
426727dd4385SLukas Czerner 
4268ca9b404fSRoman Anufriev static const char *ext4_quota_mode(struct super_block *sb)
4269ca9b404fSRoman Anufriev {
4270ca9b404fSRoman Anufriev #ifdef CONFIG_QUOTA
4271ca9b404fSRoman Anufriev 	if (!ext4_quota_capable(sb))
4272ca9b404fSRoman Anufriev 		return "none";
4273ca9b404fSRoman Anufriev 
4274ca9b404fSRoman Anufriev 	if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
4275ca9b404fSRoman Anufriev 		return "journalled";
4276ca9b404fSRoman Anufriev 	else
4277ca9b404fSRoman Anufriev 		return "writeback";
4278ca9b404fSRoman Anufriev #else
4279ca9b404fSRoman Anufriev 	return "disabled";
4280ca9b404fSRoman Anufriev #endif
4281ca9b404fSRoman Anufriev }
4282ca9b404fSRoman Anufriev 
4283188c299eSJan Kara static void ext4_setup_csum_trigger(struct super_block *sb,
4284188c299eSJan Kara 				    enum ext4_journal_trigger_type type,
4285188c299eSJan Kara 				    void (*trigger)(
4286188c299eSJan Kara 					struct jbd2_buffer_trigger_type *type,
4287188c299eSJan Kara 					struct buffer_head *bh,
4288188c299eSJan Kara 					void *mapped_data,
4289188c299eSJan Kara 					size_t size))
4290188c299eSJan Kara {
4291188c299eSJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4292188c299eSJan Kara 
4293188c299eSJan Kara 	sbi->s_journal_triggers[type].sb = sb;
4294188c299eSJan Kara 	sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
4295188c299eSJan Kara }
4296188c299eSJan Kara 
42977edfd85bSLukas Czerner static void ext4_free_sbi(struct ext4_sb_info *sbi)
4298ac27a0ecSDave Kleikamp {
42997edfd85bSLukas Czerner 	if (!sbi)
43007edfd85bSLukas Czerner 		return;
43017edfd85bSLukas Czerner 
43027edfd85bSLukas Czerner 	kfree(sbi->s_blockgroup_lock);
43038012b866SShiyang Ruan 	fs_put_dax(sbi->s_daxdev, NULL);
43047edfd85bSLukas Czerner 	kfree(sbi);
43057edfd85bSLukas Czerner }
43067edfd85bSLukas Czerner 
43077edfd85bSLukas Czerner static struct ext4_sb_info *ext4_alloc_sbi(struct super_block *sb)
43087edfd85bSLukas Czerner {
43097edfd85bSLukas Czerner 	struct ext4_sb_info *sbi;
43107edfd85bSLukas Czerner 
43117edfd85bSLukas Czerner 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
43127edfd85bSLukas Czerner 	if (!sbi)
43137edfd85bSLukas Czerner 		return NULL;
43147edfd85bSLukas Czerner 
43158012b866SShiyang Ruan 	sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->s_dax_part_off,
43168012b866SShiyang Ruan 					   NULL, NULL);
43177edfd85bSLukas Czerner 
43187edfd85bSLukas Czerner 	sbi->s_blockgroup_lock =
43197edfd85bSLukas Czerner 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
43207edfd85bSLukas Czerner 
43217edfd85bSLukas Czerner 	if (!sbi->s_blockgroup_lock)
43227edfd85bSLukas Czerner 		goto err_out;
43237edfd85bSLukas Czerner 
43247edfd85bSLukas Czerner 	sb->s_fs_info = sbi;
43257edfd85bSLukas Czerner 	sbi->s_sb = sb;
43267edfd85bSLukas Czerner 	return sbi;
43277edfd85bSLukas Czerner err_out:
43288012b866SShiyang Ruan 	fs_put_dax(sbi->s_daxdev, NULL);
43297edfd85bSLukas Czerner 	kfree(sbi);
43307edfd85bSLukas Czerner 	return NULL;
43317edfd85bSLukas Czerner }
43327edfd85bSLukas Czerner 
43335f6d662dSJason Yan static void ext4_set_def_opts(struct super_block *sb,
43345f6d662dSJason Yan 			      struct ext4_super_block *es)
43355f6d662dSJason Yan {
43365f6d662dSJason Yan 	unsigned long def_mount_opts;
43375f6d662dSJason Yan 
43385f6d662dSJason Yan 	/* Set defaults before we parse the mount options */
43395f6d662dSJason Yan 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
43405f6d662dSJason Yan 	set_opt(sb, INIT_INODE_TABLE);
43415f6d662dSJason Yan 	if (def_mount_opts & EXT4_DEFM_DEBUG)
43425f6d662dSJason Yan 		set_opt(sb, DEBUG);
43435f6d662dSJason Yan 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
43445f6d662dSJason Yan 		set_opt(sb, GRPID);
43455f6d662dSJason Yan 	if (def_mount_opts & EXT4_DEFM_UID16)
43465f6d662dSJason Yan 		set_opt(sb, NO_UID32);
43475f6d662dSJason Yan 	/* xattr user namespace & acls are now defaulted on */
43485f6d662dSJason Yan 	set_opt(sb, XATTR_USER);
43495f6d662dSJason Yan #ifdef CONFIG_EXT4_FS_POSIX_ACL
43505f6d662dSJason Yan 	set_opt(sb, POSIX_ACL);
43515f6d662dSJason Yan #endif
43525f6d662dSJason Yan 	if (ext4_has_feature_fast_commit(sb))
43535f6d662dSJason Yan 		set_opt2(sb, JOURNAL_FAST_COMMIT);
43545f6d662dSJason Yan 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
43555f6d662dSJason Yan 	if (ext4_has_metadata_csum(sb))
43565f6d662dSJason Yan 		set_opt(sb, JOURNAL_CHECKSUM);
43575f6d662dSJason Yan 
43585f6d662dSJason Yan 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
43595f6d662dSJason Yan 		set_opt(sb, JOURNAL_DATA);
43605f6d662dSJason Yan 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
43615f6d662dSJason Yan 		set_opt(sb, ORDERED_DATA);
43625f6d662dSJason Yan 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
43635f6d662dSJason Yan 		set_opt(sb, WRITEBACK_DATA);
43645f6d662dSJason Yan 
43655f6d662dSJason Yan 	if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_PANIC)
43665f6d662dSJason Yan 		set_opt(sb, ERRORS_PANIC);
43675f6d662dSJason Yan 	else if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_CONTINUE)
43685f6d662dSJason Yan 		set_opt(sb, ERRORS_CONT);
43695f6d662dSJason Yan 	else
43705f6d662dSJason Yan 		set_opt(sb, ERRORS_RO);
43715f6d662dSJason Yan 	/* block_validity enabled by default; disable with noblock_validity */
43725f6d662dSJason Yan 	set_opt(sb, BLOCK_VALIDITY);
43735f6d662dSJason Yan 	if (def_mount_opts & EXT4_DEFM_DISCARD)
43745f6d662dSJason Yan 		set_opt(sb, DISCARD);
43755f6d662dSJason Yan 
43765f6d662dSJason Yan 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
43775f6d662dSJason Yan 		set_opt(sb, BARRIER);
43785f6d662dSJason Yan 
43795f6d662dSJason Yan 	/*
43805f6d662dSJason Yan 	 * enable delayed allocation by default
43815f6d662dSJason Yan 	 * Use -o nodelalloc to turn it off
43825f6d662dSJason Yan 	 */
43835f6d662dSJason Yan 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
43845f6d662dSJason Yan 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
43855f6d662dSJason Yan 		set_opt(sb, DELALLOC);
43863df11e27SJason Yan 
43873df11e27SJason Yan 	if (sb->s_blocksize == PAGE_SIZE)
43883df11e27SJason Yan 		set_opt(sb, DIOREAD_NOLOCK);
43895f6d662dSJason Yan }
43905f6d662dSJason Yan 
4391c8267c51SJason Yan static int ext4_handle_clustersize(struct super_block *sb)
43924a8557b0SJason Yan {
43934a8557b0SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
43944a8557b0SJason Yan 	struct ext4_super_block *es = sbi->s_es;
43954a8557b0SJason Yan 	int clustersize;
43964a8557b0SJason Yan 
43974a8557b0SJason Yan 	/* Handle clustersize */
43984a8557b0SJason Yan 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
43994a8557b0SJason Yan 	if (ext4_has_feature_bigalloc(sb)) {
4400c8267c51SJason Yan 		if (clustersize < sb->s_blocksize) {
44014a8557b0SJason Yan 			ext4_msg(sb, KERN_ERR,
44024a8557b0SJason Yan 				 "cluster size (%d) smaller than "
4403c8267c51SJason Yan 				 "block size (%lu)", clustersize, sb->s_blocksize);
44044a8557b0SJason Yan 			return -EINVAL;
44054a8557b0SJason Yan 		}
44064a8557b0SJason Yan 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
44074a8557b0SJason Yan 			le32_to_cpu(es->s_log_block_size);
44084a8557b0SJason Yan 		sbi->s_clusters_per_group =
44094a8557b0SJason Yan 			le32_to_cpu(es->s_clusters_per_group);
4410c8267c51SJason Yan 		if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
44114a8557b0SJason Yan 			ext4_msg(sb, KERN_ERR,
44124a8557b0SJason Yan 				 "#clusters per group too big: %lu",
44134a8557b0SJason Yan 				 sbi->s_clusters_per_group);
44144a8557b0SJason Yan 			return -EINVAL;
44154a8557b0SJason Yan 		}
44164a8557b0SJason Yan 		if (sbi->s_blocks_per_group !=
4417c8267c51SJason Yan 		    (sbi->s_clusters_per_group * (clustersize / sb->s_blocksize))) {
44184a8557b0SJason Yan 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
44194a8557b0SJason Yan 				 "clusters per group (%lu) inconsistent",
44204a8557b0SJason Yan 				 sbi->s_blocks_per_group,
44214a8557b0SJason Yan 				 sbi->s_clusters_per_group);
44224a8557b0SJason Yan 			return -EINVAL;
44234a8557b0SJason Yan 		}
44244a8557b0SJason Yan 	} else {
4425c8267c51SJason Yan 		if (clustersize != sb->s_blocksize) {
44264a8557b0SJason Yan 			ext4_msg(sb, KERN_ERR,
44274a8557b0SJason Yan 				 "fragment/cluster size (%d) != "
4428c8267c51SJason Yan 				 "block size (%lu)", clustersize, sb->s_blocksize);
44294a8557b0SJason Yan 			return -EINVAL;
44304a8557b0SJason Yan 		}
4431c8267c51SJason Yan 		if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
44324a8557b0SJason Yan 			ext4_msg(sb, KERN_ERR,
44334a8557b0SJason Yan 				 "#blocks per group too big: %lu",
44344a8557b0SJason Yan 				 sbi->s_blocks_per_group);
44354a8557b0SJason Yan 			return -EINVAL;
44364a8557b0SJason Yan 		}
44374a8557b0SJason Yan 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
44384a8557b0SJason Yan 		sbi->s_cluster_bits = 0;
44394a8557b0SJason Yan 	}
4440c8267c51SJason Yan 	sbi->s_cluster_ratio = clustersize / sb->s_blocksize;
44414a8557b0SJason Yan 
44424a8557b0SJason Yan 	/* Do we have standard group size of clustersize * 8 blocks ? */
44434a8557b0SJason Yan 	if (sbi->s_blocks_per_group == clustersize << 3)
44444a8557b0SJason Yan 		set_opt2(sb, STD_GROUP_SIZE);
44454a8557b0SJason Yan 
44464a8557b0SJason Yan 	return 0;
44474a8557b0SJason Yan }
44484a8557b0SJason Yan 
4449f7314a67SJason Yan static void ext4_fast_commit_init(struct super_block *sb)
4450f7314a67SJason Yan {
4451f7314a67SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4452f7314a67SJason Yan 
4453f7314a67SJason Yan 	/* Initialize fast commit stuff */
4454f7314a67SJason Yan 	atomic_set(&sbi->s_fc_subtid, 0);
4455f7314a67SJason Yan 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4456f7314a67SJason Yan 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4457f7314a67SJason Yan 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4458f7314a67SJason Yan 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4459f7314a67SJason Yan 	sbi->s_fc_bytes = 0;
4460f7314a67SJason Yan 	ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
4461f7314a67SJason Yan 	sbi->s_fc_ineligible_tid = 0;
4462f7314a67SJason Yan 	spin_lock_init(&sbi->s_fc_lock);
4463f7314a67SJason Yan 	memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
4464f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_regions = NULL;
4465f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_regions_size = 0;
4466f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_regions_used = 0;
4467f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_regions_valid = 0;
4468f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_modified_inodes = NULL;
4469f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
4470f7314a67SJason Yan 	sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4471f7314a67SJason Yan }
4472f7314a67SJason Yan 
44730e495f7cSJason Yan static int ext4_inode_info_init(struct super_block *sb,
4474c8267c51SJason Yan 				struct ext4_super_block *es)
44750e495f7cSJason Yan {
44760e495f7cSJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
44770e495f7cSJason Yan 
44780e495f7cSJason Yan 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
44790e495f7cSJason Yan 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
44800e495f7cSJason Yan 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
44810e495f7cSJason Yan 	} else {
44820e495f7cSJason Yan 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
44830e495f7cSJason Yan 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
44840e495f7cSJason Yan 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
44850e495f7cSJason Yan 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
44860e495f7cSJason Yan 				 sbi->s_first_ino);
44870e495f7cSJason Yan 			return -EINVAL;
44880e495f7cSJason Yan 		}
44890e495f7cSJason Yan 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
44900e495f7cSJason Yan 		    (!is_power_of_2(sbi->s_inode_size)) ||
4491c8267c51SJason Yan 		    (sbi->s_inode_size > sb->s_blocksize)) {
44920e495f7cSJason Yan 			ext4_msg(sb, KERN_ERR,
44930e495f7cSJason Yan 			       "unsupported inode size: %d",
44940e495f7cSJason Yan 			       sbi->s_inode_size);
4495c8267c51SJason Yan 			ext4_msg(sb, KERN_ERR, "blocksize: %lu", sb->s_blocksize);
44960e495f7cSJason Yan 			return -EINVAL;
44970e495f7cSJason Yan 		}
44980e495f7cSJason Yan 		/*
44990e495f7cSJason Yan 		 * i_atime_extra is the last extra field available for
45000e495f7cSJason Yan 		 * [acm]times in struct ext4_inode. Checking for that
45010e495f7cSJason Yan 		 * field should suffice to ensure we have extra space
45020e495f7cSJason Yan 		 * for all three.
45030e495f7cSJason Yan 		 */
45040e495f7cSJason Yan 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
45050e495f7cSJason Yan 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
45060e495f7cSJason Yan 			sb->s_time_gran = 1;
45070e495f7cSJason Yan 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
45080e495f7cSJason Yan 		} else {
45090e495f7cSJason Yan 			sb->s_time_gran = NSEC_PER_SEC;
45100e495f7cSJason Yan 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
45110e495f7cSJason Yan 		}
45120e495f7cSJason Yan 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
45130e495f7cSJason Yan 	}
45140e495f7cSJason Yan 
45150e495f7cSJason Yan 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
45160e495f7cSJason Yan 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
45170e495f7cSJason Yan 			EXT4_GOOD_OLD_INODE_SIZE;
45180e495f7cSJason Yan 		if (ext4_has_feature_extra_isize(sb)) {
45190e495f7cSJason Yan 			unsigned v, max = (sbi->s_inode_size -
45200e495f7cSJason Yan 					   EXT4_GOOD_OLD_INODE_SIZE);
45210e495f7cSJason Yan 
45220e495f7cSJason Yan 			v = le16_to_cpu(es->s_want_extra_isize);
45230e495f7cSJason Yan 			if (v > max) {
45240e495f7cSJason Yan 				ext4_msg(sb, KERN_ERR,
45250e495f7cSJason Yan 					 "bad s_want_extra_isize: %d", v);
45260e495f7cSJason Yan 				return -EINVAL;
45270e495f7cSJason Yan 			}
45280e495f7cSJason Yan 			if (sbi->s_want_extra_isize < v)
45290e495f7cSJason Yan 				sbi->s_want_extra_isize = v;
45300e495f7cSJason Yan 
45310e495f7cSJason Yan 			v = le16_to_cpu(es->s_min_extra_isize);
45320e495f7cSJason Yan 			if (v > max) {
45330e495f7cSJason Yan 				ext4_msg(sb, KERN_ERR,
45340e495f7cSJason Yan 					 "bad s_min_extra_isize: %d", v);
45350e495f7cSJason Yan 				return -EINVAL;
45360e495f7cSJason Yan 			}
45370e495f7cSJason Yan 			if (sbi->s_want_extra_isize < v)
45380e495f7cSJason Yan 				sbi->s_want_extra_isize = v;
45390e495f7cSJason Yan 		}
45400e495f7cSJason Yan 	}
45410e495f7cSJason Yan 
45420e495f7cSJason Yan 	return 0;
45430e495f7cSJason Yan }
45440e495f7cSJason Yan 
454539c135b0SJason Yan #if IS_ENABLED(CONFIG_UNICODE)
454639c135b0SJason Yan static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
454739c135b0SJason Yan {
454839c135b0SJason Yan 	const struct ext4_sb_encodings *encoding_info;
454939c135b0SJason Yan 	struct unicode_map *encoding;
455039c135b0SJason Yan 	__u16 encoding_flags = le16_to_cpu(es->s_encoding_flags);
455139c135b0SJason Yan 
455239c135b0SJason Yan 	if (!ext4_has_feature_casefold(sb) || sb->s_encoding)
455339c135b0SJason Yan 		return 0;
455439c135b0SJason Yan 
455539c135b0SJason Yan 	encoding_info = ext4_sb_read_encoding(es);
455639c135b0SJason Yan 	if (!encoding_info) {
455739c135b0SJason Yan 		ext4_msg(sb, KERN_ERR,
455839c135b0SJason Yan 			"Encoding requested by superblock is unknown");
455939c135b0SJason Yan 		return -EINVAL;
456039c135b0SJason Yan 	}
456139c135b0SJason Yan 
456239c135b0SJason Yan 	encoding = utf8_load(encoding_info->version);
456339c135b0SJason Yan 	if (IS_ERR(encoding)) {
456439c135b0SJason Yan 		ext4_msg(sb, KERN_ERR,
456539c135b0SJason Yan 			"can't mount with superblock charset: %s-%u.%u.%u "
456639c135b0SJason Yan 			"not supported by the kernel. flags: 0x%x.",
456739c135b0SJason Yan 			encoding_info->name,
456839c135b0SJason Yan 			unicode_major(encoding_info->version),
456939c135b0SJason Yan 			unicode_minor(encoding_info->version),
457039c135b0SJason Yan 			unicode_rev(encoding_info->version),
457139c135b0SJason Yan 			encoding_flags);
457239c135b0SJason Yan 		return -EINVAL;
457339c135b0SJason Yan 	}
457439c135b0SJason Yan 	ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
457539c135b0SJason Yan 		"%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
457639c135b0SJason Yan 		unicode_major(encoding_info->version),
457739c135b0SJason Yan 		unicode_minor(encoding_info->version),
457839c135b0SJason Yan 		unicode_rev(encoding_info->version),
457939c135b0SJason Yan 		encoding_flags);
458039c135b0SJason Yan 
458139c135b0SJason Yan 	sb->s_encoding = encoding;
458239c135b0SJason Yan 	sb->s_encoding_flags = encoding_flags;
458339c135b0SJason Yan 
458439c135b0SJason Yan 	return 0;
458539c135b0SJason Yan }
458639c135b0SJason Yan #else
458739c135b0SJason Yan static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
458839c135b0SJason Yan {
458939c135b0SJason Yan 	return 0;
459039c135b0SJason Yan }
459139c135b0SJason Yan #endif
459239c135b0SJason Yan 
4593b26458d1SJason Yan static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_block *es)
4594b26458d1SJason Yan {
4595b26458d1SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4596b26458d1SJason Yan 
4597b26458d1SJason Yan 	/* Warn if metadata_csum and gdt_csum are both set. */
4598b26458d1SJason Yan 	if (ext4_has_feature_metadata_csum(sb) &&
4599b26458d1SJason Yan 	    ext4_has_feature_gdt_csum(sb))
4600b26458d1SJason Yan 		ext4_warning(sb, "metadata_csum and uninit_bg are "
4601b26458d1SJason Yan 			     "redundant flags; please run fsck.");
4602b26458d1SJason Yan 
4603b26458d1SJason Yan 	/* Check for a known checksum algorithm */
4604b26458d1SJason Yan 	if (!ext4_verify_csum_type(sb, es)) {
4605b26458d1SJason Yan 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4606b26458d1SJason Yan 			 "unknown checksum algorithm.");
4607b26458d1SJason Yan 		return -EINVAL;
4608b26458d1SJason Yan 	}
4609b26458d1SJason Yan 	ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
4610b26458d1SJason Yan 				ext4_orphan_file_block_trigger);
4611b26458d1SJason Yan 
4612b26458d1SJason Yan 	/* Load the checksum driver */
4613b26458d1SJason Yan 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
4614b26458d1SJason Yan 	if (IS_ERR(sbi->s_chksum_driver)) {
4615b26458d1SJason Yan 		int ret = PTR_ERR(sbi->s_chksum_driver);
4616b26458d1SJason Yan 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
4617b26458d1SJason Yan 		sbi->s_chksum_driver = NULL;
4618b26458d1SJason Yan 		return ret;
4619b26458d1SJason Yan 	}
4620b26458d1SJason Yan 
4621b26458d1SJason Yan 	/* Check superblock checksum */
4622b26458d1SJason Yan 	if (!ext4_superblock_csum_verify(sb, es)) {
4623b26458d1SJason Yan 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4624b26458d1SJason Yan 			 "invalid superblock checksum.  Run e2fsck?");
4625b26458d1SJason Yan 		return -EFSBADCRC;
4626b26458d1SJason Yan 	}
4627b26458d1SJason Yan 
4628b26458d1SJason Yan 	/* Precompute checksum seed for all metadata */
4629b26458d1SJason Yan 	if (ext4_has_feature_csum_seed(sb))
4630b26458d1SJason Yan 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4631b26458d1SJason Yan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4632b26458d1SJason Yan 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4633b26458d1SJason Yan 					       sizeof(es->s_uuid));
4634b26458d1SJason Yan 	return 0;
4635b26458d1SJason Yan }
4636b26458d1SJason Yan 
4637d7f3542bSJason Yan static int ext4_check_feature_compatibility(struct super_block *sb,
4638d7f3542bSJason Yan 					    struct ext4_super_block *es,
4639d7f3542bSJason Yan 					    int silent)
4640d7f3542bSJason Yan {
464154902099SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
464254902099SJason Yan 
4643d7f3542bSJason Yan 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4644d7f3542bSJason Yan 	    (ext4_has_compat_features(sb) ||
4645d7f3542bSJason Yan 	     ext4_has_ro_compat_features(sb) ||
4646d7f3542bSJason Yan 	     ext4_has_incompat_features(sb)))
4647d7f3542bSJason Yan 		ext4_msg(sb, KERN_WARNING,
4648d7f3542bSJason Yan 		       "feature flags set on rev 0 fs, "
4649d7f3542bSJason Yan 		       "running e2fsck is recommended");
4650d7f3542bSJason Yan 
4651d7f3542bSJason Yan 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4652d7f3542bSJason Yan 		set_opt2(sb, HURD_COMPAT);
4653d7f3542bSJason Yan 		if (ext4_has_feature_64bit(sb)) {
4654d7f3542bSJason Yan 			ext4_msg(sb, KERN_ERR,
4655d7f3542bSJason Yan 				 "The Hurd can't support 64-bit file systems");
4656d7f3542bSJason Yan 			return -EINVAL;
4657d7f3542bSJason Yan 		}
4658d7f3542bSJason Yan 
4659d7f3542bSJason Yan 		/*
4660d7f3542bSJason Yan 		 * ea_inode feature uses l_i_version field which is not
4661d7f3542bSJason Yan 		 * available in HURD_COMPAT mode.
4662d7f3542bSJason Yan 		 */
4663d7f3542bSJason Yan 		if (ext4_has_feature_ea_inode(sb)) {
4664d7f3542bSJason Yan 			ext4_msg(sb, KERN_ERR,
4665d7f3542bSJason Yan 				 "ea_inode feature is not supported for Hurd");
4666d7f3542bSJason Yan 			return -EINVAL;
4667d7f3542bSJason Yan 		}
4668d7f3542bSJason Yan 	}
4669d7f3542bSJason Yan 
4670d7f3542bSJason Yan 	if (IS_EXT2_SB(sb)) {
4671d7f3542bSJason Yan 		if (ext2_feature_set_ok(sb))
4672d7f3542bSJason Yan 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4673d7f3542bSJason Yan 				 "using the ext4 subsystem");
4674d7f3542bSJason Yan 		else {
4675d7f3542bSJason Yan 			/*
4676d7f3542bSJason Yan 			 * If we're probing be silent, if this looks like
4677d7f3542bSJason Yan 			 * it's actually an ext[34] filesystem.
4678d7f3542bSJason Yan 			 */
4679d7f3542bSJason Yan 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4680d7f3542bSJason Yan 				return -EINVAL;
4681d7f3542bSJason Yan 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4682d7f3542bSJason Yan 				 "to feature incompatibilities");
4683d7f3542bSJason Yan 			return -EINVAL;
4684d7f3542bSJason Yan 		}
4685d7f3542bSJason Yan 	}
4686d7f3542bSJason Yan 
4687d7f3542bSJason Yan 	if (IS_EXT3_SB(sb)) {
4688d7f3542bSJason Yan 		if (ext3_feature_set_ok(sb))
4689d7f3542bSJason Yan 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4690d7f3542bSJason Yan 				 "using the ext4 subsystem");
4691d7f3542bSJason Yan 		else {
4692d7f3542bSJason Yan 			/*
4693d7f3542bSJason Yan 			 * If we're probing be silent, if this looks like
4694d7f3542bSJason Yan 			 * it's actually an ext4 filesystem.
4695d7f3542bSJason Yan 			 */
4696d7f3542bSJason Yan 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4697d7f3542bSJason Yan 				return -EINVAL;
4698d7f3542bSJason Yan 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4699d7f3542bSJason Yan 				 "to feature incompatibilities");
4700d7f3542bSJason Yan 			return -EINVAL;
4701d7f3542bSJason Yan 		}
4702d7f3542bSJason Yan 	}
4703d7f3542bSJason Yan 
4704d7f3542bSJason Yan 	/*
4705d7f3542bSJason Yan 	 * Check feature flags regardless of the revision level, since we
4706d7f3542bSJason Yan 	 * previously didn't change the revision level when setting the flags,
4707d7f3542bSJason Yan 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4708d7f3542bSJason Yan 	 */
4709d7f3542bSJason Yan 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4710d7f3542bSJason Yan 		return -EINVAL;
4711d7f3542bSJason Yan 
471254902099SJason Yan 	if (sbi->s_daxdev) {
471354902099SJason Yan 		if (sb->s_blocksize == PAGE_SIZE)
471454902099SJason Yan 			set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
471554902099SJason Yan 		else
471654902099SJason Yan 			ext4_msg(sb, KERN_ERR, "unsupported blocksize for DAX\n");
471754902099SJason Yan 	}
471854902099SJason Yan 
471954902099SJason Yan 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
472054902099SJason Yan 		if (ext4_has_feature_inline_data(sb)) {
472154902099SJason Yan 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
472254902099SJason Yan 					" that may contain inline data");
472354902099SJason Yan 			return -EINVAL;
472454902099SJason Yan 		}
472554902099SJason Yan 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
472654902099SJason Yan 			ext4_msg(sb, KERN_ERR,
472754902099SJason Yan 				"DAX unsupported by block device.");
472854902099SJason Yan 			return -EINVAL;
472954902099SJason Yan 		}
473054902099SJason Yan 	}
473154902099SJason Yan 
473254902099SJason Yan 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
473354902099SJason Yan 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
473454902099SJason Yan 			 es->s_encryption_level);
473554902099SJason Yan 		return -EINVAL;
473654902099SJason Yan 	}
473754902099SJason Yan 
4738d7f3542bSJason Yan 	return 0;
4739d7f3542bSJason Yan }
4740d7f3542bSJason Yan 
474168e62439SJason Yan static int ext4_check_geometry(struct super_block *sb,
4742bc62dbf9SJason Yan 			       struct ext4_super_block *es)
4743bc62dbf9SJason Yan {
4744bc62dbf9SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4745bc62dbf9SJason Yan 	__u64 blocks_count;
4746269e9226SJason Yan 	int err;
4747269e9226SJason Yan 
4748269e9226SJason Yan 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (sb->s_blocksize / 4)) {
4749269e9226SJason Yan 		ext4_msg(sb, KERN_ERR,
4750269e9226SJason Yan 			 "Number of reserved GDT blocks insanely large: %d",
4751269e9226SJason Yan 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4752269e9226SJason Yan 		return -EINVAL;
4753269e9226SJason Yan 	}
4754269e9226SJason Yan 	/*
4755269e9226SJason Yan 	 * Test whether we have more sectors than will fit in sector_t,
4756269e9226SJason Yan 	 * and whether the max offset is addressable by the page cache.
4757269e9226SJason Yan 	 */
4758269e9226SJason Yan 	err = generic_check_addressable(sb->s_blocksize_bits,
4759269e9226SJason Yan 					ext4_blocks_count(es));
4760269e9226SJason Yan 	if (err) {
4761269e9226SJason Yan 		ext4_msg(sb, KERN_ERR, "filesystem"
4762269e9226SJason Yan 			 " too large to mount safely on this system");
4763269e9226SJason Yan 		return err;
4764269e9226SJason Yan 	}
4765bc62dbf9SJason Yan 
4766bc62dbf9SJason Yan 	/* check blocks count against device size */
4767bc62dbf9SJason Yan 	blocks_count = sb_bdev_nr_blocks(sb);
4768bc62dbf9SJason Yan 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4769bc62dbf9SJason Yan 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4770bc62dbf9SJason Yan 		       "exceeds size of device (%llu blocks)",
4771bc62dbf9SJason Yan 		       ext4_blocks_count(es), blocks_count);
4772bc62dbf9SJason Yan 		return -EINVAL;
4773bc62dbf9SJason Yan 	}
4774bc62dbf9SJason Yan 
4775bc62dbf9SJason Yan 	/*
4776bc62dbf9SJason Yan 	 * It makes no sense for the first data block to be beyond the end
4777bc62dbf9SJason Yan 	 * of the filesystem.
4778bc62dbf9SJason Yan 	 */
4779bc62dbf9SJason Yan 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4780bc62dbf9SJason Yan 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4781bc62dbf9SJason Yan 			 "block %u is beyond end of filesystem (%llu)",
4782bc62dbf9SJason Yan 			 le32_to_cpu(es->s_first_data_block),
4783bc62dbf9SJason Yan 			 ext4_blocks_count(es));
4784bc62dbf9SJason Yan 		return -EINVAL;
4785bc62dbf9SJason Yan 	}
4786bc62dbf9SJason Yan 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4787bc62dbf9SJason Yan 	    (sbi->s_cluster_ratio == 1)) {
4788bc62dbf9SJason Yan 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4789bc62dbf9SJason Yan 			 "block is 0 with a 1k block and cluster size");
4790bc62dbf9SJason Yan 		return -EINVAL;
4791bc62dbf9SJason Yan 	}
4792bc62dbf9SJason Yan 
4793bc62dbf9SJason Yan 	blocks_count = (ext4_blocks_count(es) -
4794bc62dbf9SJason Yan 			le32_to_cpu(es->s_first_data_block) +
4795bc62dbf9SJason Yan 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4796bc62dbf9SJason Yan 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4797bc62dbf9SJason Yan 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4798bc62dbf9SJason Yan 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
4799bc62dbf9SJason Yan 		       "(block count %llu, first data block %u, "
4800bc62dbf9SJason Yan 		       "blocks per group %lu)", blocks_count,
4801bc62dbf9SJason Yan 		       ext4_blocks_count(es),
4802bc62dbf9SJason Yan 		       le32_to_cpu(es->s_first_data_block),
4803bc62dbf9SJason Yan 		       EXT4_BLOCKS_PER_GROUP(sb));
4804bc62dbf9SJason Yan 		return -EINVAL;
4805bc62dbf9SJason Yan 	}
4806bc62dbf9SJason Yan 	sbi->s_groups_count = blocks_count;
4807bc62dbf9SJason Yan 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4808bc62dbf9SJason Yan 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
4809bc62dbf9SJason Yan 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4810bc62dbf9SJason Yan 	    le32_to_cpu(es->s_inodes_count)) {
4811bc62dbf9SJason Yan 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4812bc62dbf9SJason Yan 			 le32_to_cpu(es->s_inodes_count),
4813bc62dbf9SJason Yan 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4814bc62dbf9SJason Yan 		return -EINVAL;
4815bc62dbf9SJason Yan 	}
4816bc62dbf9SJason Yan 
4817bc62dbf9SJason Yan 	return 0;
4818bc62dbf9SJason Yan }
4819bc62dbf9SJason Yan 
4820a4e6a511SJason Yan static int ext4_group_desc_init(struct super_block *sb,
4821a4e6a511SJason Yan 				struct ext4_super_block *es,
4822a4e6a511SJason Yan 				ext4_fsblk_t logical_sb_block,
4823a4e6a511SJason Yan 				ext4_group_t *first_not_zeroed)
4824a4e6a511SJason Yan {
4825a4e6a511SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4826a4e6a511SJason Yan 	unsigned int db_count;
4827a4e6a511SJason Yan 	ext4_fsblk_t block;
4828a4e6a511SJason Yan 	int i;
4829a4e6a511SJason Yan 
4830a4e6a511SJason Yan 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4831a4e6a511SJason Yan 		   EXT4_DESC_PER_BLOCK(sb);
4832a4e6a511SJason Yan 	if (ext4_has_feature_meta_bg(sb)) {
4833a4e6a511SJason Yan 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
4834a4e6a511SJason Yan 			ext4_msg(sb, KERN_WARNING,
4835a4e6a511SJason Yan 				 "first meta block group too large: %u "
4836a4e6a511SJason Yan 				 "(group descriptor block count %u)",
4837a4e6a511SJason Yan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
4838a4e6a511SJason Yan 			return -EINVAL;
4839a4e6a511SJason Yan 		}
4840a4e6a511SJason Yan 	}
4841a4e6a511SJason Yan 	rcu_assign_pointer(sbi->s_group_desc,
4842a4e6a511SJason Yan 			   kvmalloc_array(db_count,
4843a4e6a511SJason Yan 					  sizeof(struct buffer_head *),
4844a4e6a511SJason Yan 					  GFP_KERNEL));
4845a4e6a511SJason Yan 	if (sbi->s_group_desc == NULL) {
4846a4e6a511SJason Yan 		ext4_msg(sb, KERN_ERR, "not enough memory");
4847a4e6a511SJason Yan 		return -ENOMEM;
4848a4e6a511SJason Yan 	}
4849a4e6a511SJason Yan 
4850a4e6a511SJason Yan 	bgl_lock_init(sbi->s_blockgroup_lock);
4851a4e6a511SJason Yan 
4852a4e6a511SJason Yan 	/* Pre-read the descriptors into the buffer cache */
4853a4e6a511SJason Yan 	for (i = 0; i < db_count; i++) {
4854a4e6a511SJason Yan 		block = descriptor_loc(sb, logical_sb_block, i);
4855a4e6a511SJason Yan 		ext4_sb_breadahead_unmovable(sb, block);
4856a4e6a511SJason Yan 	}
4857a4e6a511SJason Yan 
4858a4e6a511SJason Yan 	for (i = 0; i < db_count; i++) {
4859a4e6a511SJason Yan 		struct buffer_head *bh;
4860a4e6a511SJason Yan 
4861a4e6a511SJason Yan 		block = descriptor_loc(sb, logical_sb_block, i);
4862a4e6a511SJason Yan 		bh = ext4_sb_bread_unmovable(sb, block);
4863a4e6a511SJason Yan 		if (IS_ERR(bh)) {
4864a4e6a511SJason Yan 			ext4_msg(sb, KERN_ERR,
4865a4e6a511SJason Yan 			       "can't read group descriptor %d", i);
4866a4e6a511SJason Yan 			sbi->s_gdb_count = i;
4867172e344eSYe Bin 			return PTR_ERR(bh);
4868a4e6a511SJason Yan 		}
4869a4e6a511SJason Yan 		rcu_read_lock();
4870a4e6a511SJason Yan 		rcu_dereference(sbi->s_group_desc)[i] = bh;
4871a4e6a511SJason Yan 		rcu_read_unlock();
4872a4e6a511SJason Yan 	}
4873a4e6a511SJason Yan 	sbi->s_gdb_count = db_count;
4874a4e6a511SJason Yan 	if (!ext4_check_descriptors(sb, logical_sb_block, first_not_zeroed)) {
4875a4e6a511SJason Yan 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
4876172e344eSYe Bin 		return -EFSCORRUPTED;
4877a4e6a511SJason Yan 	}
4878172e344eSYe Bin 
4879a4e6a511SJason Yan 	return 0;
4880a4e6a511SJason Yan }
4881a4e6a511SJason Yan 
48829c1dd22dSJason Yan static int ext4_load_and_init_journal(struct super_block *sb,
48839c1dd22dSJason Yan 				      struct ext4_super_block *es,
48849c1dd22dSJason Yan 				      struct ext4_fs_context *ctx)
48859c1dd22dSJason Yan {
48869c1dd22dSJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
48879c1dd22dSJason Yan 	int err;
48889c1dd22dSJason Yan 
48899c1dd22dSJason Yan 	err = ext4_load_journal(sb, es, ctx->journal_devnum);
48909c1dd22dSJason Yan 	if (err)
48919c1dd22dSJason Yan 		return err;
48929c1dd22dSJason Yan 
48939c1dd22dSJason Yan 	if (ext4_has_feature_64bit(sb) &&
48949c1dd22dSJason Yan 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
48959c1dd22dSJason Yan 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
48969c1dd22dSJason Yan 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
48979c1dd22dSJason Yan 		goto out;
48989c1dd22dSJason Yan 	}
48999c1dd22dSJason Yan 
49009c1dd22dSJason Yan 	if (!set_journal_csum_feature_set(sb)) {
49019c1dd22dSJason Yan 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
49029c1dd22dSJason Yan 			 "feature set");
49039c1dd22dSJason Yan 		goto out;
49049c1dd22dSJason Yan 	}
49059c1dd22dSJason Yan 
49069c1dd22dSJason Yan 	if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
49079c1dd22dSJason Yan 		!jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
49089c1dd22dSJason Yan 					  JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
49099c1dd22dSJason Yan 		ext4_msg(sb, KERN_ERR,
49109c1dd22dSJason Yan 			"Failed to set fast commit journal feature");
49119c1dd22dSJason Yan 		goto out;
49129c1dd22dSJason Yan 	}
49139c1dd22dSJason Yan 
49149c1dd22dSJason Yan 	/* We have now updated the journal if required, so we can
49159c1dd22dSJason Yan 	 * validate the data journaling mode. */
49169c1dd22dSJason Yan 	switch (test_opt(sb, DATA_FLAGS)) {
49179c1dd22dSJason Yan 	case 0:
49189c1dd22dSJason Yan 		/* No mode set, assume a default based on the journal
49199c1dd22dSJason Yan 		 * capabilities: ORDERED_DATA if the journal can
49209c1dd22dSJason Yan 		 * cope, else JOURNAL_DATA
49219c1dd22dSJason Yan 		 */
49229c1dd22dSJason Yan 		if (jbd2_journal_check_available_features
49239c1dd22dSJason Yan 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
49249c1dd22dSJason Yan 			set_opt(sb, ORDERED_DATA);
49259c1dd22dSJason Yan 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
49269c1dd22dSJason Yan 		} else {
49279c1dd22dSJason Yan 			set_opt(sb, JOURNAL_DATA);
49289c1dd22dSJason Yan 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
49299c1dd22dSJason Yan 		}
49309c1dd22dSJason Yan 		break;
49319c1dd22dSJason Yan 
49329c1dd22dSJason Yan 	case EXT4_MOUNT_ORDERED_DATA:
49339c1dd22dSJason Yan 	case EXT4_MOUNT_WRITEBACK_DATA:
49349c1dd22dSJason Yan 		if (!jbd2_journal_check_available_features
49359c1dd22dSJason Yan 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
49369c1dd22dSJason Yan 			ext4_msg(sb, KERN_ERR, "Journal does not support "
49379c1dd22dSJason Yan 			       "requested data journaling mode");
49389c1dd22dSJason Yan 			goto out;
49399c1dd22dSJason Yan 		}
49409c1dd22dSJason Yan 		break;
49419c1dd22dSJason Yan 	default:
49429c1dd22dSJason Yan 		break;
49439c1dd22dSJason Yan 	}
49449c1dd22dSJason Yan 
49459c1dd22dSJason Yan 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
49469c1dd22dSJason Yan 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
49479c1dd22dSJason Yan 		ext4_msg(sb, KERN_ERR, "can't mount with "
49489c1dd22dSJason Yan 			"journal_async_commit in data=ordered mode");
49499c1dd22dSJason Yan 		goto out;
49509c1dd22dSJason Yan 	}
49519c1dd22dSJason Yan 
49529c1dd22dSJason Yan 	set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
49539c1dd22dSJason Yan 
49549c1dd22dSJason Yan 	sbi->s_journal->j_submit_inode_data_buffers =
49559c1dd22dSJason Yan 		ext4_journal_submit_inode_data_buffers;
49569c1dd22dSJason Yan 	sbi->s_journal->j_finish_inode_data_buffers =
49579c1dd22dSJason Yan 		ext4_journal_finish_inode_data_buffers;
49589c1dd22dSJason Yan 
49599c1dd22dSJason Yan 	return 0;
49609c1dd22dSJason Yan 
49619c1dd22dSJason Yan out:
49629c1dd22dSJason Yan 	/* flush s_error_work before journal destroy. */
49639c1dd22dSJason Yan 	flush_work(&sbi->s_error_work);
49649c1dd22dSJason Yan 	jbd2_journal_destroy(sbi->s_journal);
49659c1dd22dSJason Yan 	sbi->s_journal = NULL;
49669f2a1d9fSJason Yan 	return -EINVAL;
49679c1dd22dSJason Yan }
49689c1dd22dSJason Yan 
496968e62439SJason Yan static int ext4_check_journal_data_mode(struct super_block *sb)
4970a5991e53SJason Yan {
4971a5991e53SJason Yan 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4972a5991e53SJason Yan 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with "
4973a5991e53SJason Yan 			    "data=journal disables delayed allocation, "
4974a5991e53SJason Yan 			    "dioread_nolock, O_DIRECT and fast_commit support!\n");
4975a5991e53SJason Yan 		/* can't mount with both data=journal and dioread_nolock. */
4976a5991e53SJason Yan 		clear_opt(sb, DIOREAD_NOLOCK);
4977a5991e53SJason Yan 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
4978a5991e53SJason Yan 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4979a5991e53SJason Yan 			ext4_msg(sb, KERN_ERR, "can't mount with "
4980a5991e53SJason Yan 				 "both data=journal and delalloc");
4981a5991e53SJason Yan 			return -EINVAL;
4982a5991e53SJason Yan 		}
4983a5991e53SJason Yan 		if (test_opt(sb, DAX_ALWAYS)) {
4984a5991e53SJason Yan 			ext4_msg(sb, KERN_ERR, "can't mount with "
4985a5991e53SJason Yan 				 "both data=journal and dax");
4986a5991e53SJason Yan 			return -EINVAL;
4987a5991e53SJason Yan 		}
4988a5991e53SJason Yan 		if (ext4_has_feature_encrypt(sb)) {
4989a5991e53SJason Yan 			ext4_msg(sb, KERN_WARNING,
4990a5991e53SJason Yan 				 "encrypted files will use data=ordered "
4991a5991e53SJason Yan 				 "instead of data journaling mode");
4992a5991e53SJason Yan 		}
4993a5991e53SJason Yan 		if (test_opt(sb, DELALLOC))
4994a5991e53SJason Yan 			clear_opt(sb, DELALLOC);
4995a5991e53SJason Yan 	} else {
4996a5991e53SJason Yan 		sb->s_iflags |= SB_I_CGROUPWB;
4997a5991e53SJason Yan 	}
4998a5991e53SJason Yan 
4999a5991e53SJason Yan 	return 0;
5000a5991e53SJason Yan }
5001a5991e53SJason Yan 
5002a7a79c29SJason Yan static int ext4_load_super(struct super_block *sb, ext4_fsblk_t *lsb,
5003a7a79c29SJason Yan 			   int silent)
5004a7a79c29SJason Yan {
5005a7a79c29SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5006a7a79c29SJason Yan 	struct ext4_super_block *es;
5007a7a79c29SJason Yan 	ext4_fsblk_t logical_sb_block;
5008a7a79c29SJason Yan 	unsigned long offset = 0;
5009a7a79c29SJason Yan 	struct buffer_head *bh;
5010a7a79c29SJason Yan 	int ret = -EINVAL;
5011a7a79c29SJason Yan 	int blocksize;
5012a7a79c29SJason Yan 
5013a7a79c29SJason Yan 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
5014a7a79c29SJason Yan 	if (!blocksize) {
5015a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
5016a7a79c29SJason Yan 		return -EINVAL;
5017a7a79c29SJason Yan 	}
5018a7a79c29SJason Yan 
5019a7a79c29SJason Yan 	/*
5020a7a79c29SJason Yan 	 * The ext4 superblock will not be buffer aligned for other than 1kB
5021a7a79c29SJason Yan 	 * block sizes.  We need to calculate the offset from buffer start.
5022a7a79c29SJason Yan 	 */
5023a7a79c29SJason Yan 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
5024a7a79c29SJason Yan 		logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
5025a7a79c29SJason Yan 		offset = do_div(logical_sb_block, blocksize);
5026a7a79c29SJason Yan 	} else {
5027a7a79c29SJason Yan 		logical_sb_block = sbi->s_sb_block;
5028a7a79c29SJason Yan 	}
5029a7a79c29SJason Yan 
5030a7a79c29SJason Yan 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
5031a7a79c29SJason Yan 	if (IS_ERR(bh)) {
5032a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
5033a7a79c29SJason Yan 		return PTR_ERR(bh);
5034a7a79c29SJason Yan 	}
5035a7a79c29SJason Yan 	/*
5036a7a79c29SJason Yan 	 * Note: s_es must be initialized as soon as possible because
5037a7a79c29SJason Yan 	 *       some ext4 macro-instructions depend on its value
5038a7a79c29SJason Yan 	 */
5039a7a79c29SJason Yan 	es = (struct ext4_super_block *) (bh->b_data + offset);
5040a7a79c29SJason Yan 	sbi->s_es = es;
5041a7a79c29SJason Yan 	sb->s_magic = le16_to_cpu(es->s_magic);
5042a7a79c29SJason Yan 	if (sb->s_magic != EXT4_SUPER_MAGIC) {
5043a7a79c29SJason Yan 		if (!silent)
5044a7a79c29SJason Yan 			ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5045a7a79c29SJason Yan 		goto out;
5046a7a79c29SJason Yan 	}
5047a7a79c29SJason Yan 
5048a7a79c29SJason Yan 	if (le32_to_cpu(es->s_log_block_size) >
5049a7a79c29SJason Yan 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
5050a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR,
5051a7a79c29SJason Yan 			 "Invalid log block size: %u",
5052a7a79c29SJason Yan 			 le32_to_cpu(es->s_log_block_size));
5053a7a79c29SJason Yan 		goto out;
5054a7a79c29SJason Yan 	}
5055a7a79c29SJason Yan 	if (le32_to_cpu(es->s_log_cluster_size) >
5056a7a79c29SJason Yan 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
5057a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR,
5058a7a79c29SJason Yan 			 "Invalid log cluster size: %u",
5059a7a79c29SJason Yan 			 le32_to_cpu(es->s_log_cluster_size));
5060a7a79c29SJason Yan 		goto out;
5061a7a79c29SJason Yan 	}
5062a7a79c29SJason Yan 
5063a7a79c29SJason Yan 	blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
5064a7a79c29SJason Yan 
5065a7a79c29SJason Yan 	/*
5066a7a79c29SJason Yan 	 * If the default block size is not the same as the real block size,
5067a7a79c29SJason Yan 	 * we need to reload it.
5068a7a79c29SJason Yan 	 */
5069a7a79c29SJason Yan 	if (sb->s_blocksize == blocksize) {
5070a7a79c29SJason Yan 		*lsb = logical_sb_block;
5071a7a79c29SJason Yan 		sbi->s_sbh = bh;
5072a7a79c29SJason Yan 		return 0;
5073a7a79c29SJason Yan 	}
5074a7a79c29SJason Yan 
5075a7a79c29SJason Yan 	/*
5076a7a79c29SJason Yan 	 * bh must be released before kill_bdev(), otherwise
5077a7a79c29SJason Yan 	 * it won't be freed and its page also. kill_bdev()
5078a7a79c29SJason Yan 	 * is called by sb_set_blocksize().
5079a7a79c29SJason Yan 	 */
5080a7a79c29SJason Yan 	brelse(bh);
5081a7a79c29SJason Yan 	/* Validate the filesystem blocksize */
5082a7a79c29SJason Yan 	if (!sb_set_blocksize(sb, blocksize)) {
5083a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR, "bad block size %d",
5084a7a79c29SJason Yan 				blocksize);
5085a7a79c29SJason Yan 		bh = NULL;
5086a7a79c29SJason Yan 		goto out;
5087a7a79c29SJason Yan 	}
5088a7a79c29SJason Yan 
5089a7a79c29SJason Yan 	logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
5090a7a79c29SJason Yan 	offset = do_div(logical_sb_block, blocksize);
5091a7a79c29SJason Yan 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
5092a7a79c29SJason Yan 	if (IS_ERR(bh)) {
5093a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR, "Can't read superblock on 2nd try");
5094a7a79c29SJason Yan 		ret = PTR_ERR(bh);
5095a7a79c29SJason Yan 		bh = NULL;
5096a7a79c29SJason Yan 		goto out;
5097a7a79c29SJason Yan 	}
5098a7a79c29SJason Yan 	es = (struct ext4_super_block *)(bh->b_data + offset);
5099a7a79c29SJason Yan 	sbi->s_es = es;
5100a7a79c29SJason Yan 	if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
5101a7a79c29SJason Yan 		ext4_msg(sb, KERN_ERR, "Magic mismatch, very weird!");
5102a7a79c29SJason Yan 		goto out;
5103a7a79c29SJason Yan 	}
5104a7a79c29SJason Yan 	*lsb = logical_sb_block;
5105a7a79c29SJason Yan 	sbi->s_sbh = bh;
5106a7a79c29SJason Yan 	return 0;
5107a7a79c29SJason Yan out:
5108a7a79c29SJason Yan 	brelse(bh);
5109a7a79c29SJason Yan 	return ret;
5110a7a79c29SJason Yan }
5111a7a79c29SJason Yan 
5112db9345d9SJason Yan static void ext4_hash_info_init(struct super_block *sb)
5113db9345d9SJason Yan {
5114db9345d9SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5115db9345d9SJason Yan 	struct ext4_super_block *es = sbi->s_es;
5116db9345d9SJason Yan 	unsigned int i;
5117db9345d9SJason Yan 
5118db9345d9SJason Yan 	for (i = 0; i < 4; i++)
5119db9345d9SJason Yan 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
5120db9345d9SJason Yan 
5121db9345d9SJason Yan 	sbi->s_def_hash_version = es->s_def_hash_version;
5122db9345d9SJason Yan 	if (ext4_has_feature_dir_index(sb)) {
5123db9345d9SJason Yan 		i = le32_to_cpu(es->s_flags);
5124db9345d9SJason Yan 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
5125db9345d9SJason Yan 			sbi->s_hash_unsigned = 3;
5126db9345d9SJason Yan 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
5127db9345d9SJason Yan #ifdef __CHAR_UNSIGNED__
5128db9345d9SJason Yan 			if (!sb_rdonly(sb))
5129db9345d9SJason Yan 				es->s_flags |=
5130db9345d9SJason Yan 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
5131db9345d9SJason Yan 			sbi->s_hash_unsigned = 3;
5132db9345d9SJason Yan #else
5133db9345d9SJason Yan 			if (!sb_rdonly(sb))
5134db9345d9SJason Yan 				es->s_flags |=
5135db9345d9SJason Yan 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
5136db9345d9SJason Yan #endif
5137db9345d9SJason Yan 		}
5138db9345d9SJason Yan 	}
5139db9345d9SJason Yan }
5140db9345d9SJason Yan 
5141107d2be9SJason Yan static int ext4_block_group_meta_init(struct super_block *sb, int silent)
5142107d2be9SJason Yan {
5143107d2be9SJason Yan 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5144107d2be9SJason Yan 	struct ext4_super_block *es = sbi->s_es;
5145107d2be9SJason Yan 	int has_huge_files;
5146107d2be9SJason Yan 
5147107d2be9SJason Yan 	has_huge_files = ext4_has_feature_huge_file(sb);
5148107d2be9SJason Yan 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
5149107d2be9SJason Yan 						      has_huge_files);
5150107d2be9SJason Yan 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
5151107d2be9SJason Yan 
5152107d2be9SJason Yan 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
5153107d2be9SJason Yan 	if (ext4_has_feature_64bit(sb)) {
5154107d2be9SJason Yan 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
5155107d2be9SJason Yan 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
5156107d2be9SJason Yan 		    !is_power_of_2(sbi->s_desc_size)) {
5157107d2be9SJason Yan 			ext4_msg(sb, KERN_ERR,
5158107d2be9SJason Yan 			       "unsupported descriptor size %lu",
5159107d2be9SJason Yan 			       sbi->s_desc_size);
5160107d2be9SJason Yan 			return -EINVAL;
5161107d2be9SJason Yan 		}
5162107d2be9SJason Yan 	} else
5163107d2be9SJason Yan 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
5164107d2be9SJason Yan 
5165107d2be9SJason Yan 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
5166107d2be9SJason Yan 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
5167107d2be9SJason Yan 
5168107d2be9SJason Yan 	sbi->s_inodes_per_block = sb->s_blocksize / EXT4_INODE_SIZE(sb);
5169107d2be9SJason Yan 	if (sbi->s_inodes_per_block == 0 || sbi->s_blocks_per_group == 0) {
5170107d2be9SJason Yan 		if (!silent)
5171107d2be9SJason Yan 			ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5172107d2be9SJason Yan 		return -EINVAL;
5173107d2be9SJason Yan 	}
5174107d2be9SJason Yan 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
5175107d2be9SJason Yan 	    sbi->s_inodes_per_group > sb->s_blocksize * 8) {
5176107d2be9SJason Yan 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
5177107d2be9SJason Yan 			 sbi->s_inodes_per_group);
5178107d2be9SJason Yan 		return -EINVAL;
5179107d2be9SJason Yan 	}
5180107d2be9SJason Yan 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
5181107d2be9SJason Yan 					sbi->s_inodes_per_block;
5182107d2be9SJason Yan 	sbi->s_desc_per_block = sb->s_blocksize / EXT4_DESC_SIZE(sb);
5183107d2be9SJason Yan 	sbi->s_mount_state = le16_to_cpu(es->s_state) & ~EXT4_FC_REPLAY;
5184107d2be9SJason Yan 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
5185107d2be9SJason Yan 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
5186107d2be9SJason Yan 
5187107d2be9SJason Yan 	return 0;
5188107d2be9SJason Yan }
5189107d2be9SJason Yan 
5190960e0ab6SLukas Czerner static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
51917edfd85bSLukas Czerner {
5192617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
51937edfd85bSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
519470bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
5195ac27a0ecSDave Kleikamp 	struct inode *root;
5196107d2be9SJason Yan 	int needs_recovery;
5197d4fab7b2STheodore Ts'o 	int err;
5198bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
51997edfd85bSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
5200960e0ab6SLukas Czerner 	int silent = fc->sb_flags & SB_SILENT;
5201b237e304SHarshad Shirwadkar 
5202b237e304SHarshad Shirwadkar 	/* Set defaults for the variables that will be set during parsing */
5203e4e58e5dSOjaswin Mujoo 	if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO))
52047edfd85bSLukas Czerner 		ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5205ac27a0ecSDave Kleikamp 
5206240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
5207f613dfcbSTheodore Ts'o 	sbi->s_sectors_written_start =
52088446fe92SChristoph Hellwig 		part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
5209ac27a0ecSDave Kleikamp 
5210a7a79c29SJason Yan 	err = ext4_load_super(sb, &logical_sb_block, silent);
5211a7a79c29SJason Yan 	if (err)
5212ac27a0ecSDave Kleikamp 		goto out_fail;
5213ac27a0ecSDave Kleikamp 
5214a7a79c29SJason Yan 	es = sbi->s_es;
5215afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
5216ac27a0ecSDave Kleikamp 
5217b26458d1SJason Yan 	err = ext4_init_metadata_csum(sb, es);
5218b26458d1SJason Yan 	if (err)
5219a5fc5119SJason Yan 		goto failed_mount;
5220a9c47317SDarrick J. Wong 
52215f6d662dSJason Yan 	ext4_set_def_opts(sb, es);
5222ac27a0ecSDave Kleikamp 
522308cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
522408cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
522530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
522630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
522730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
5228ac27a0ecSDave Kleikamp 
522951ce6511SLukas Czerner 	/*
523051ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
523151ce6511SLukas Czerner 	 * no mount option specified.
523251ce6511SLukas Czerner 	 */
523351ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
523451ce6511SLukas Czerner 
5235d4fab7b2STheodore Ts'o 	err = ext4_inode_info_init(sb, es);
5236d4fab7b2STheodore Ts'o 	if (err)
52379803387cSTheodore Ts'o 		goto failed_mount;
52389803387cSTheodore Ts'o 
52397edfd85bSLukas Czerner 	err = parse_apply_sb_mount_options(sb, ctx);
52407edfd85bSLukas Czerner 	if (err < 0)
52415aee0f8aSTheodore Ts'o 		goto failed_mount;
52427edfd85bSLukas Czerner 
52435a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
5244e3645d72SZhang Yi 	sbi->s_def_mount_opt2 = sbi->s_mount_opt2;
52457edfd85bSLukas Czerner 
52467edfd85bSLukas Czerner 	err = ext4_check_opt_consistency(fc, sb);
52477edfd85bSLukas Czerner 	if (err < 0)
52487edfd85bSLukas Czerner 		goto failed_mount;
52497edfd85bSLukas Czerner 
525085456054SEric Biggers 	ext4_apply_options(fc, sb);
5251ac27a0ecSDave Kleikamp 
5252d4fab7b2STheodore Ts'o 	err = ext4_encoding_init(sb, es);
5253d4fab7b2STheodore Ts'o 	if (err)
5254c83ad55eSGabriel Krisman Bertazi 		goto failed_mount;
5255c83ad55eSGabriel Krisman Bertazi 
5256d4fab7b2STheodore Ts'o 	err = ext4_check_journal_data_mode(sb);
5257d4fab7b2STheodore Ts'o 	if (err)
525856889787STheodore Ts'o 		goto failed_mount;
525956889787STheodore Ts'o 
52601751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
52611751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5262ac27a0ecSDave Kleikamp 
52631ff20307SJeff Layton 	/* i_version is always enabled now */
52641ff20307SJeff Layton 	sb->s_flags |= SB_I_VERSION;
52651ff20307SJeff Layton 
5266d4fab7b2STheodore Ts'o 	err = ext4_check_feature_compatibility(sb, es, silent);
5267d4fab7b2STheodore Ts'o 	if (err)
5268ac27a0ecSDave Kleikamp 		goto failed_mount;
5269a13fb1a4SEric Sandeen 
5270d4fab7b2STheodore Ts'o 	err = ext4_block_group_meta_init(sb, silent);
5271d4fab7b2STheodore Ts'o 	if (err)
52725b9554dcSTheodore Ts'o 		goto failed_mount;
52735b9554dcSTheodore Ts'o 
5274db9345d9SJason Yan 	ext4_hash_info_init(sb);
5275ac27a0ecSDave Kleikamp 
5276d4fab7b2STheodore Ts'o 	err = ext4_handle_clustersize(sb);
5277d4fab7b2STheodore Ts'o 	if (err)
5278281b5995STheodore Ts'o 		goto failed_mount;
5279960fd856STheodore Ts'o 
5280d4fab7b2STheodore Ts'o 	err = ext4_check_geometry(sb, es);
5281d4fab7b2STheodore Ts'o 	if (err)
52820f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
52830f2ddca6SFrom: Thiemo Nagel 
5284235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
5285c92dc856SJan Kara 	spin_lock_init(&sbi->s_error_lock);
5286c92dc856SJan Kara 	INIT_WORK(&sbi->s_error_work, flush_stashed_error_work);
528704496411STao Ma 
5288172e344eSYe Bin 	err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed);
5289172e344eSYe Bin 	if (err)
5290172e344eSYe Bin 		goto failed_mount3;
5291172e344eSYe Bin 
5292d4fab7b2STheodore Ts'o 	err = ext4_es_register_shrinker(sbi);
5293d4fab7b2STheodore Ts'o 	if (err)
5294ce7e010aSTheodore Ts'o 		goto failed_mount3;
5295ce7e010aSTheodore Ts'o 
5296c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
5297c3defd99SKemeng Shi 	/*
5298c3defd99SKemeng Shi 	 * It's hard to get stripe aligned blocks if stripe is not aligned with
5299c3defd99SKemeng Shi 	 * cluster, just disable stripe and alert user to simpfy code and avoid
5300c3defd99SKemeng Shi 	 * stripe aligned allocation which will rarely successes.
5301c3defd99SKemeng Shi 	 */
5302c3defd99SKemeng Shi 	if (sbi->s_stripe > 0 && sbi->s_cluster_ratio > 1 &&
5303c3defd99SKemeng Shi 	    sbi->s_stripe % sbi->s_cluster_ratio != 0) {
5304c3defd99SKemeng Shi 		ext4_msg(sb, KERN_WARNING,
5305c3defd99SKemeng Shi 			 "stripe (%lu) is not aligned with cluster size (%u), "
5306c3defd99SKemeng Shi 			 "stripe is disabled",
5307c3defd99SKemeng Shi 			 sbi->s_stripe, sbi->s_cluster_ratio);
5308c3defd99SKemeng Shi 		sbi->s_stripe = 0;
5309c3defd99SKemeng Shi 	}
531067a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
5311c9de560dSAlex Tomas 
5312f9ae9cf5STheodore Ts'o 	/*
5313f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
5314f9ae9cf5STheodore Ts'o 	 */
5315f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
5316617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
5317617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
5318643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
5319a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
5320ffcc4182SEric Biggers #endif
5321c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
5322c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
5323c93d8f88SEric Biggers #endif
5324ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5325617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
5326e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
53271fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
5328262b4662SJan Kara 	else
5329262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
5330689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
5331ac27a0ecSDave Kleikamp #endif
533285787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
5333f2fa2ffcSAneesh Kumar K.V 
5334ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
53353b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
5336ac27a0ecSDave Kleikamp 
5337f7314a67SJason Yan 	ext4_fast_commit_init(sb);
5338aa75f4d3SHarshad Shirwadkar 
5339ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
5340ac27a0ecSDave Kleikamp 
5341ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
534202f310fcSJan Kara 			  ext4_has_feature_orphan_present(sb) ||
5343e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
5344ac27a0ecSDave Kleikamp 
53453b50d501STheodore Ts'o 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) {
53463b50d501STheodore Ts'o 		err = ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block));
53473b50d501STheodore Ts'o 		if (err)
534850460fe8SDarrick J. Wong 			goto failed_mount3a;
53493b50d501STheodore Ts'o 	}
5350c5e06d10SJohann Lombardi 
5351d4fab7b2STheodore Ts'o 	err = -EINVAL;
5352ac27a0ecSDave Kleikamp 	/*
5353ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
5354ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
5355ac27a0ecSDave Kleikamp 	 */
5356e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
53579c1dd22dSJason Yan 		err = ext4_load_and_init_journal(sb, es, ctx);
53584753d8a2STheodore Ts'o 		if (err)
535950460fe8SDarrick J. Wong 			goto failed_mount3a;
5360bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
5361e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
5362b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
5363b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
536443bd6f1bSJason Yan 		goto failed_mount3a;
5365ac27a0ecSDave Kleikamp 	} else {
53661e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
53671e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
53681e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
53691e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
537043bd6f1bSJason Yan 			goto failed_mount3a;
53711e381f60SDmitry Monakhov 		}
537289481b5fSBaokun Li 
537389481b5fSBaokun Li 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
537489481b5fSBaokun Li 			ext4_msg(sb, KERN_ERR, "can't mount with "
537589481b5fSBaokun Li 				 "journal_checksum, fs mounted w/o journal");
537689481b5fSBaokun Li 			goto failed_mount3a;
537789481b5fSBaokun Li 		}
53781e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
53791e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
53801e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
53811e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
538243bd6f1bSJason Yan 			goto failed_mount3a;
53831e381f60SDmitry Monakhov 		}
53841e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
53851e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
53861e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
53871e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
538843bd6f1bSJason Yan 			goto failed_mount3a;
53891e381f60SDmitry Monakhov 		}
539050b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
53911e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
5392fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
5393995a3ed6SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
53940390131bSFrank Mayhar 		sbi->s_journal = NULL;
53950390131bSFrank Mayhar 		needs_recovery = 0;
5396ac27a0ecSDave Kleikamp 	}
5397ac27a0ecSDave Kleikamp 
5398cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
539947387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
540047387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
5401cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
5402cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
5403d4fab7b2STheodore Ts'o 			err = -EINVAL;
54049c191f70ST Makphaibulchoke 			goto failed_mount_wq;
54059c191f70ST Makphaibulchoke 		}
54069c191f70ST Makphaibulchoke 
5407dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
5408dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
5409dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
5410dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
5411dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
5412d4fab7b2STheodore Ts'o 				err = -EINVAL;
5413dec214d0STahsin Erdogan 				goto failed_mount_wq;
5414dec214d0STahsin Erdogan 			}
5415dec214d0STahsin Erdogan 		}
5416cdb7ee4cSTahsin Erdogan 	}
5417dec214d0STahsin Erdogan 
5418fd89d5f2STejun Heo 	/*
5419952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
5420952fc18eSTheodore Ts'o 	 * superblock if present.
5421952fc18eSTheodore Ts'o 	 */
5422952fc18eSTheodore Ts'o 	sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
542385d825dbSTheodore Ts'o 	/* ignore the precalculated value if it is ridiculous */
542485d825dbSTheodore Ts'o 	if (sbi->s_overhead > ext4_blocks_count(es))
542585d825dbSTheodore Ts'o 		sbi->s_overhead = 0;
542685d825dbSTheodore Ts'o 	/*
542785d825dbSTheodore Ts'o 	 * If the bigalloc feature is not enabled recalculating the
542885d825dbSTheodore Ts'o 	 * overhead doesn't take long, so we might as well just redo
542985d825dbSTheodore Ts'o 	 * it to make sure we are using the correct value.
543085d825dbSTheodore Ts'o 	 */
543185d825dbSTheodore Ts'o 	if (!ext4_has_feature_bigalloc(sb))
543285d825dbSTheodore Ts'o 		sbi->s_overhead = 0;
543385d825dbSTheodore Ts'o 	if (sbi->s_overhead == 0) {
543407aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
543507aa2ea1SLukas Czerner 		if (err)
5436952fc18eSTheodore Ts'o 			goto failed_mount_wq;
5437952fc18eSTheodore Ts'o 	}
5438952fc18eSTheodore Ts'o 
5439952fc18eSTheodore Ts'o 	/*
5440fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
5441fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
5442fd89d5f2STejun Heo 	 */
54432e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
54442e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
54452e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
54462e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
5447d4fab7b2STheodore Ts'o 		err = -ENOMEM;
54482e8fa54eSJan Kara 		goto failed_mount4;
54492e8fa54eSJan Kara 	}
54502e8fa54eSJan Kara 
5451ac27a0ecSDave Kleikamp 	/*
5452dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
5453ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
5454ac27a0ecSDave Kleikamp 	 */
5455ac27a0ecSDave Kleikamp 
54568a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
54571d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
5458b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
5459d4fab7b2STheodore Ts'o 		err = PTR_ERR(root);
546032a9bb57SManish Katiyar 		root = NULL;
5461ac27a0ecSDave Kleikamp 		goto failed_mount4;
5462ac27a0ecSDave Kleikamp 	}
5463ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
5464b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
546594bf608aSAl Viro 		iput(root);
5466d4fab7b2STheodore Ts'o 		err = -EFSCORRUPTED;
5467ac27a0ecSDave Kleikamp 		goto failed_mount4;
5468ac27a0ecSDave Kleikamp 	}
5469b886ee3eSGabriel Krisman Bertazi 
547048fde701SAl Viro 	sb->s_root = d_make_root(root);
54711d1fe1eeSDavid Howells 	if (!sb->s_root) {
5472b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
5473d4fab7b2STheodore Ts'o 		err = -ENOMEM;
54741d1fe1eeSDavid Howells 		goto failed_mount4;
54751d1fe1eeSDavid Howells 	}
5476ac27a0ecSDave Kleikamp 
5477d4fab7b2STheodore Ts'o 	err = ext4_setup_super(sb, es, sb_rdonly(sb));
5478d4fab7b2STheodore Ts'o 	if (err == -EROFS) {
54791751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
5480d4fab7b2STheodore Ts'o 	} else if (err)
5481c89128a0SJaegeuk Kim 		goto failed_mount4a;
5482ef7f3835SKalpak Shah 
5483b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
548427dd4385SLukas Czerner 
54850f5bde1dSJan Kara 	if (test_opt(sb, BLOCK_VALIDITY)) {
54866fd058f7STheodore Ts'o 		err = ext4_setup_system_zone(sb);
54876fd058f7STheodore Ts'o 		if (err) {
5488b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
5489fbe845ddSCurt Wohlgemuth 				 "zone (%d)", err);
5490f9ae9cf5STheodore Ts'o 			goto failed_mount4a;
5491f9ae9cf5STheodore Ts'o 		}
54920f5bde1dSJan Kara 	}
54938016e29fSHarshad Shirwadkar 	ext4_fc_replay_cleanup(sb);
5494f9ae9cf5STheodore Ts'o 
5495f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
5496196e402aSHarshad Shirwadkar 
5497196e402aSHarshad Shirwadkar 	/*
5498196e402aSHarshad Shirwadkar 	 * Enable optimize_scan if number of groups is > threshold. This can be
5499196e402aSHarshad Shirwadkar 	 * turned off by passing "mb_optimize_scan=0". This can also be
5500196e402aSHarshad Shirwadkar 	 * turned on forcefully by passing "mb_optimize_scan=1".
5501196e402aSHarshad Shirwadkar 	 */
550227b38686SOjaswin Mujoo 	if (!(ctx->spec & EXT4_SPEC_mb_optimize_scan)) {
550327b38686SOjaswin Mujoo 		if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
5504196e402aSHarshad Shirwadkar 			set_opt2(sb, MB_OPTIMIZE_SCAN);
550527b38686SOjaswin Mujoo 		else
5506196e402aSHarshad Shirwadkar 			clear_opt2(sb, MB_OPTIMIZE_SCAN);
550727b38686SOjaswin Mujoo 	}
5508196e402aSHarshad Shirwadkar 
5509f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
5510f9ae9cf5STheodore Ts'o 	if (err) {
5511f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5512f9ae9cf5STheodore Ts'o 			 err);
5513dcf2d804STao Ma 		goto failed_mount5;
5514c2774d84SAneesh Kumar K.V 	}
5515c2774d84SAneesh Kumar K.V 
5516027f14f5STheodore Ts'o 	/*
5517027f14f5STheodore Ts'o 	 * We can only set up the journal commit callback once
5518027f14f5STheodore Ts'o 	 * mballoc is initialized
5519027f14f5STheodore Ts'o 	 */
5520027f14f5STheodore Ts'o 	if (sbi->s_journal)
5521027f14f5STheodore Ts'o 		sbi->s_journal->j_commit_callback =
5522027f14f5STheodore Ts'o 			ext4_journal_commit_callback;
5523027f14f5STheodore Ts'o 
5524d5e72c4eSTheodore Ts'o 	err = ext4_percpu_param_init(sbi);
5525d5e72c4eSTheodore Ts'o 	if (err)
5526d5e03cbbSTheodore Ts'o 		goto failed_mount6;
5527d5e03cbbSTheodore Ts'o 
5528e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
5529d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
5530d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
5531d5e03cbbSTheodore Ts'o 			       "unable to initialize "
5532d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
5533d4fab7b2STheodore Ts'o 			err = -ENOMEM;
5534d5e03cbbSTheodore Ts'o 			goto failed_mount6;
5535d5e03cbbSTheodore Ts'o 		}
5536d5e03cbbSTheodore Ts'o 
5537bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
5538bfff6873SLukas Czerner 	if (err)
5539dcf2d804STao Ma 		goto failed_mount6;
5540bfff6873SLukas Czerner 
5541b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
5542dcf2d804STao Ma 	if (err)
5543dcf2d804STao Ma 		goto failed_mount7;
55443197ebdbSTheodore Ts'o 
554502f310fcSJan Kara 	err = ext4_init_orphan_info(sb);
554602f310fcSJan Kara 	if (err)
554702f310fcSJan Kara 		goto failed_mount8;
55489b2ff357SJan Kara #ifdef CONFIG_QUOTA
55499b2ff357SJan Kara 	/* Enable quota usage during mount. */
5550bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
55519b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
55529b2ff357SJan Kara 		if (err)
555302f310fcSJan Kara 			goto failed_mount9;
55549b2ff357SJan Kara 	}
55559b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
55569b2ff357SJan Kara 
5557bc71726cSzhangyi (F) 	/*
5558bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
5559bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
5560bc71726cSzhangyi (F) 	 */
5561bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
5562bc71726cSzhangyi (F) 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5563bc71726cSzhangyi (F) 				 &sbi->s_bdev_wb_err);
5564bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
5565617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5566617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
5567617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
55689b6641ddSYe Bin 	/*
55699b6641ddSYe Bin 	 * Update the checksum after updating free space/inode counters and
55709b6641ddSYe Bin 	 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
55719b6641ddSYe Bin 	 * checksum in the buffer cache until it is written out and
55729b6641ddSYe Bin 	 * e2fsprogs programs trying to open a file system immediately
55739b6641ddSYe Bin 	 * after it is mounted can fail.
55749b6641ddSYe Bin 	 */
55759b6641ddSYe Bin 	ext4_superblock_csum_set(sb);
55760390131bSFrank Mayhar 	if (needs_recovery) {
5577b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
557811215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
557911215630SJan Kara 		if (err)
5580d13f9963SBaokun Li 			goto failed_mount10;
55810390131bSFrank Mayhar 	}
55820390131bSFrank Mayhar 
558370200574SChristoph Hellwig 	if (test_opt(sb, DISCARD) && !bdev_max_discard_sectors(sb->s_bdev))
558479add3a3SLukas Czerner 		ext4_msg(sb, KERN_WARNING,
558570200574SChristoph Hellwig 			 "mounting with \"discard\" option, but the device does not support discard");
558679add3a3SLukas Czerner 
558766e61a9eSTheodore Ts'o 	if (es->s_error_count)
558866e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5589ac27a0ecSDave Kleikamp 
5590efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5591efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5592efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5593efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
55941cf006edSDmitry Monakhov 	atomic_set(&sbi->s_warning_count, 0);
55951cf006edSDmitry Monakhov 	atomic_set(&sbi->s_msg_count, 0);
5596efbed4dcSTheodore Ts'o 
5597ac27a0ecSDave Kleikamp 	return 0;
5598ac27a0ecSDave Kleikamp 
5599d13f9963SBaokun Li failed_mount10:
5600f3c1c42eSBaokun Li 	ext4_quotas_off(sb, EXT4_MAXQUOTAS);
5601d13f9963SBaokun Li failed_mount9: __maybe_unused
560202f310fcSJan Kara 	ext4_release_orphan_info(sb);
560372ba7450STheodore Ts'o failed_mount8:
5604ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
5605cb8d53d2SEric Biggers 	kobject_put(&sbi->s_kobj);
5606dcf2d804STao Ma failed_mount7:
5607dcf2d804STao Ma 	ext4_unregister_li_request(sb);
5608dcf2d804STao Ma failed_mount6:
5609f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
5610dcbf8758SJason Yan 	ext4_flex_groups_free(sbi);
56111f79467cSJason Yan 	ext4_percpu_param_destroy(sbi);
561200764937SAzat Khuzhin failed_mount5:
5613f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
5614f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
5615f9ae9cf5STheodore Ts'o failed_mount4a:
561694bf608aSAl Viro 	dput(sb->s_root);
561732a9bb57SManish Katiyar 	sb->s_root = NULL;
561894bf608aSAl Viro failed_mount4:
5619b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
56202e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
56212e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
56224c0425ffSMingming Cao failed_mount_wq:
5623dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5624dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
562550c15df6SChengguang Xu 
562647387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
562747387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
562850c15df6SChengguang Xu 
56290390131bSFrank Mayhar 	if (sbi->s_journal) {
5630bb9464e0Syangerkun 		/* flush s_error_work before journal destroy. */
5631bb9464e0Syangerkun 		flush_work(&sbi->s_error_work);
5632dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
563347b4a50bSJan Kara 		sbi->s_journal = NULL;
56340390131bSFrank Mayhar 	}
563550460fe8SDarrick J. Wong failed_mount3a:
5636d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
5637eb68d0e2SZheng Liu failed_mount3:
5638bb9464e0Syangerkun 	/* flush s_error_work before sbi destroy */
5639c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
56402a4ae3bcSJan Kara 	del_timer_sync(&sbi->s_err_report);
5641618f0031SPavel Skripkin 	ext4_stop_mmpd(sbi);
5642a4e6a511SJason Yan 	ext4_group_desc_free(sbi);
5643ac27a0ecSDave Kleikamp failed_mount:
56440441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
56450441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
5646c83ad55eSGabriel Krisman Bertazi 
56475298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
5648f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
5649c83ad55eSGabriel Krisman Bertazi #endif
5650c83ad55eSGabriel Krisman Bertazi 
5651ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5652856dd6c5SNathan Chancellor 	for (unsigned int i = 0; i < EXT4_MAXQUOTAS; i++)
56530ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
5654ac27a0ecSDave Kleikamp #endif
5655ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5656afd09b61SAlexey Makhalov 	/* ext4_blkdev_remove() calls kill_bdev(), release bh before it. */
5657a7a79c29SJason Yan 	brelse(sbi->s_sbh);
5658afd09b61SAlexey Makhalov 	ext4_blkdev_remove(sbi);
5659ac27a0ecSDave Kleikamp out_fail:
566026fb5290SZhihao Cheng 	invalidate_bdev(sb->s_bdev);
5661ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
5662d4fab7b2STheodore Ts'o 	return err;
5663ac27a0ecSDave Kleikamp }
5664ac27a0ecSDave Kleikamp 
5665cebe85d5SLukas Czerner static int ext4_fill_super(struct super_block *sb, struct fs_context *fc)
56667edfd85bSLukas Czerner {
5667cebe85d5SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
56687edfd85bSLukas Czerner 	struct ext4_sb_info *sbi;
56697edfd85bSLukas Czerner 	const char *descr;
5670cebe85d5SLukas Czerner 	int ret;
56717edfd85bSLukas Czerner 
5672cebe85d5SLukas Czerner 	sbi = ext4_alloc_sbi(sb);
5673cebe85d5SLukas Czerner 	if (!sbi)
56747c268d4cSLukas Czerner 		return -ENOMEM;
5675cebe85d5SLukas Czerner 
5676cebe85d5SLukas Czerner 	fc->s_fs_info = sbi;
56777edfd85bSLukas Czerner 
56787edfd85bSLukas Czerner 	/* Cleanup superblock name */
56797edfd85bSLukas Czerner 	strreplace(sb->s_id, '/', '!');
56807edfd85bSLukas Czerner 
56817edfd85bSLukas Czerner 	sbi->s_sb_block = 1;	/* Default super block location */
5682cebe85d5SLukas Czerner 	if (ctx->spec & EXT4_SPEC_s_sb_block)
5683cebe85d5SLukas Czerner 		sbi->s_sb_block = ctx->s_sb_block;
56847edfd85bSLukas Czerner 
5685960e0ab6SLukas Czerner 	ret = __ext4_fill_super(fc, sb);
56867edfd85bSLukas Czerner 	if (ret < 0)
56877edfd85bSLukas Czerner 		goto free_sbi;
56887edfd85bSLukas Czerner 
5689cebe85d5SLukas Czerner 	if (sbi->s_journal) {
56907edfd85bSLukas Czerner 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
56917edfd85bSLukas Czerner 			descr = " journalled data mode";
56927edfd85bSLukas Czerner 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
56937edfd85bSLukas Czerner 			descr = " ordered data mode";
56947edfd85bSLukas Czerner 		else
56957edfd85bSLukas Czerner 			descr = " writeback data mode";
56967edfd85bSLukas Czerner 	} else
56977edfd85bSLukas Czerner 		descr = "out journal";
56987edfd85bSLukas Czerner 
56997edfd85bSLukas Czerner 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
57006dcc98fbSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "mounted filesystem %pU %s with%s. "
57016dcc98fbSTheodore Ts'o 			 "Quota mode: %s.", &sb->s_uuid,
57026dcc98fbSTheodore Ts'o 			 sb_rdonly(sb) ? "ro" : "r/w", descr,
5703bb0fbc78SLukas Czerner 			 ext4_quota_mode(sb));
57047edfd85bSLukas Czerner 
5705eb705421STheodore Ts'o 	/* Update the s_overhead_clusters if necessary */
5706827891a3STheodore Ts'o 	ext4_update_overhead(sb, false);
57077edfd85bSLukas Czerner 	return 0;
5708cebe85d5SLukas Czerner 
57097edfd85bSLukas Czerner free_sbi:
57107edfd85bSLukas Czerner 	ext4_free_sbi(sbi);
5711cebe85d5SLukas Czerner 	fc->s_fs_info = NULL;
57127edfd85bSLukas Czerner 	return ret;
57137edfd85bSLukas Czerner }
57147edfd85bSLukas Czerner 
5715cebe85d5SLukas Czerner static int ext4_get_tree(struct fs_context *fc)
5716cebe85d5SLukas Czerner {
5717cebe85d5SLukas Czerner 	return get_tree_bdev(fc, ext4_fill_super);
5718cebe85d5SLukas Czerner }
5719cebe85d5SLukas Czerner 
5720ac27a0ecSDave Kleikamp /*
5721ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
5722ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
5723ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
5724ac27a0ecSDave Kleikamp  */
5725617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5726ac27a0ecSDave Kleikamp {
5727617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5728ac27a0ecSDave Kleikamp 
5729ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
573030773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
573130773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
57326866d7b3SHarshad Shirwadkar 	ext4_fc_init(sb, journal);
5733ac27a0ecSDave Kleikamp 
5734a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
5735ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
5736dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
5737ac27a0ecSDave Kleikamp 	else
5738dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
57395bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
57405bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
57415bf5683aSHidehiro Kawai 	else
57425bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
574372945058SZhang Yi 	/*
574472945058SZhang Yi 	 * Always enable journal cycle record option, letting the journal
574572945058SZhang Yi 	 * records log transactions continuously between each mount.
574672945058SZhang Yi 	 */
574772945058SZhang Yi 	journal->j_flags |= JBD2_CYCLE_RECORD;
5748a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
5749ac27a0ecSDave Kleikamp }
5750ac27a0ecSDave Kleikamp 
5751c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
5752ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
5753ac27a0ecSDave Kleikamp {
5754ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
5755ac27a0ecSDave Kleikamp 
5756c6cb7e77SEric Whitney 	/*
5757c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
5758c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
5759c6cb7e77SEric Whitney 	 * will try to delete it.
5760c6cb7e77SEric Whitney 	 */
57618a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
57621d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
5763b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
5764ac27a0ecSDave Kleikamp 		return NULL;
5765ac27a0ecSDave Kleikamp 	}
5766ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
5767ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
5768ac27a0ecSDave Kleikamp 		iput(journal_inode);
5769b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5770ac27a0ecSDave Kleikamp 		return NULL;
5771ac27a0ecSDave Kleikamp 	}
5772ac27a0ecSDave Kleikamp 
57734978c659SJan Kara 	ext4_debug("Journal inode found at %p: %lld bytes\n",
5774ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
5775105c78e1SEric Biggers 	if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
5776b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5777ac27a0ecSDave Kleikamp 		iput(journal_inode);
5778ac27a0ecSDave Kleikamp 		return NULL;
5779ac27a0ecSDave Kleikamp 	}
5780c6cb7e77SEric Whitney 	return journal_inode;
5781c6cb7e77SEric Whitney }
5782c6cb7e77SEric Whitney 
578362913ae9STheodore Ts'o static int ext4_journal_bmap(journal_t *journal, sector_t *block)
578462913ae9STheodore Ts'o {
578562913ae9STheodore Ts'o 	struct ext4_map_blocks map;
578662913ae9STheodore Ts'o 	int ret;
578762913ae9STheodore Ts'o 
578862913ae9STheodore Ts'o 	if (journal->j_inode == NULL)
578962913ae9STheodore Ts'o 		return 0;
579062913ae9STheodore Ts'o 
579162913ae9STheodore Ts'o 	map.m_lblk = *block;
579262913ae9STheodore Ts'o 	map.m_len = 1;
579362913ae9STheodore Ts'o 	ret = ext4_map_blocks(NULL, journal->j_inode, &map, 0);
579462913ae9STheodore Ts'o 	if (ret <= 0) {
579562913ae9STheodore Ts'o 		ext4_msg(journal->j_inode->i_sb, KERN_CRIT,
579662913ae9STheodore Ts'o 			 "journal bmap failed: block %llu ret %d\n",
579762913ae9STheodore Ts'o 			 *block, ret);
579862913ae9STheodore Ts'o 		jbd2_journal_abort(journal, ret ? ret : -EIO);
579962913ae9STheodore Ts'o 		return ret;
580062913ae9STheodore Ts'o 	}
580162913ae9STheodore Ts'o 	*block = map.m_pblk;
580262913ae9STheodore Ts'o 	return 0;
580362913ae9STheodore Ts'o }
580462913ae9STheodore Ts'o 
5805c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
5806c6cb7e77SEric Whitney 				   unsigned int journal_inum)
5807c6cb7e77SEric Whitney {
5808c6cb7e77SEric Whitney 	struct inode *journal_inode;
5809c6cb7e77SEric Whitney 	journal_t *journal;
5810c6cb7e77SEric Whitney 
581111215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
581211215630SJan Kara 		return NULL;
5813c6cb7e77SEric Whitney 
5814c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5815c6cb7e77SEric Whitney 	if (!journal_inode)
5816c6cb7e77SEric Whitney 		return NULL;
5817ac27a0ecSDave Kleikamp 
5818dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
5819ac27a0ecSDave Kleikamp 	if (!journal) {
5820b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5821ac27a0ecSDave Kleikamp 		iput(journal_inode);
5822ac27a0ecSDave Kleikamp 		return NULL;
5823ac27a0ecSDave Kleikamp 	}
5824ac27a0ecSDave Kleikamp 	journal->j_private = sb;
582562913ae9STheodore Ts'o 	journal->j_bmap = ext4_journal_bmap;
5826617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5827ac27a0ecSDave Kleikamp 	return journal;
5828ac27a0ecSDave Kleikamp }
5829ac27a0ecSDave Kleikamp 
5830617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
5831ac27a0ecSDave Kleikamp 				       dev_t j_dev)
5832ac27a0ecSDave Kleikamp {
5833ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5834ac27a0ecSDave Kleikamp 	journal_t *journal;
5835617ba13bSMingming Cao 	ext4_fsblk_t start;
5836617ba13bSMingming Cao 	ext4_fsblk_t len;
5837ac27a0ecSDave Kleikamp 	int hblock, blocksize;
5838617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
5839ac27a0ecSDave Kleikamp 	unsigned long offset;
5840617ba13bSMingming Cao 	struct ext4_super_block *es;
5841ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5842ac27a0ecSDave Kleikamp 
584311215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
584411215630SJan Kara 		return NULL;
58450390131bSFrank Mayhar 
5846b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5847ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5848ac27a0ecSDave Kleikamp 		return NULL;
5849ac27a0ecSDave Kleikamp 
5850ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5851e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5852ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5853b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5854b31e1552SEric Sandeen 			"blocksize too small for journal device");
5855ac27a0ecSDave Kleikamp 		goto out_bdev;
5856ac27a0ecSDave Kleikamp 	}
5857ac27a0ecSDave Kleikamp 
5858617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5859617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5860ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5861ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5862b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5863b31e1552SEric Sandeen 		       "external journal");
5864ac27a0ecSDave Kleikamp 		goto out_bdev;
5865ac27a0ecSDave Kleikamp 	}
5866ac27a0ecSDave Kleikamp 
58672716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5868617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5869ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5870617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5871b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5872b31e1552SEric Sandeen 					"bad superblock");
5873ac27a0ecSDave Kleikamp 		brelse(bh);
5874ac27a0ecSDave Kleikamp 		goto out_bdev;
5875ac27a0ecSDave Kleikamp 	}
5876ac27a0ecSDave Kleikamp 
5877df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5878df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5879df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5880df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5881df4763beSDarrick J. Wong 				       "corrupt superblock");
5882df4763beSDarrick J. Wong 		brelse(bh);
5883df4763beSDarrick J. Wong 		goto out_bdev;
5884df4763beSDarrick J. Wong 	}
5885df4763beSDarrick J. Wong 
5886617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5887b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5888ac27a0ecSDave Kleikamp 		brelse(bh);
5889ac27a0ecSDave Kleikamp 		goto out_bdev;
5890ac27a0ecSDave Kleikamp 	}
5891ac27a0ecSDave Kleikamp 
5892bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5893ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5894ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5895ac27a0ecSDave Kleikamp 
5896dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5897ac27a0ecSDave Kleikamp 					start, len, blocksize);
5898ac27a0ecSDave Kleikamp 	if (!journal) {
5899b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5900ac27a0ecSDave Kleikamp 		goto out_bdev;
5901ac27a0ecSDave Kleikamp 	}
5902ac27a0ecSDave Kleikamp 	journal->j_private = sb;
59032d069c08Szhangyi (F) 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5904b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5905ac27a0ecSDave Kleikamp 		goto out_journal;
5906ac27a0ecSDave Kleikamp 	}
5907ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5908b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5909b31e1552SEric Sandeen 					"user (unsupported) - %d",
5910ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5911ac27a0ecSDave Kleikamp 		goto out_journal;
5912ac27a0ecSDave Kleikamp 	}
5913ee7ed3aaSChunguang Xu 	EXT4_SB(sb)->s_journal_bdev = bdev;
5914617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5915ac27a0ecSDave Kleikamp 	return journal;
59160b8e58a1SAndreas Dilger 
5917ac27a0ecSDave Kleikamp out_journal:
5918dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5919ac27a0ecSDave Kleikamp out_bdev:
5920617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5921ac27a0ecSDave Kleikamp 	return NULL;
5922ac27a0ecSDave Kleikamp }
5923ac27a0ecSDave Kleikamp 
5924617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5925617ba13bSMingming Cao 			     struct ext4_super_block *es,
5926ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5927ac27a0ecSDave Kleikamp {
5928ac27a0ecSDave Kleikamp 	journal_t *journal;
5929ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5930ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5931ac27a0ecSDave Kleikamp 	int err = 0;
5932ac27a0ecSDave Kleikamp 	int really_read_only;
5933273108faSLukas Czerner 	int journal_dev_ro;
5934ac27a0ecSDave Kleikamp 
593511215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
593611215630SJan Kara 		return -EFSCORRUPTED;
59370390131bSFrank Mayhar 
5938ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5939ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5940b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5941b31e1552SEric Sandeen 			"numbers have changed");
5942ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5943ac27a0ecSDave Kleikamp 	} else
5944ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5945ac27a0ecSDave Kleikamp 
5946273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5947273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5948273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5949273108faSLukas Czerner 		return -EINVAL;
5950273108faSLukas Czerner 	}
5951273108faSLukas Czerner 
5952273108faSLukas Czerner 	if (journal_inum) {
5953273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5954273108faSLukas Czerner 		if (!journal)
5955273108faSLukas Czerner 			return -EINVAL;
5956273108faSLukas Czerner 	} else {
5957273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5958273108faSLukas Czerner 		if (!journal)
5959273108faSLukas Czerner 			return -EINVAL;
5960273108faSLukas Czerner 	}
5961273108faSLukas Czerner 
5962273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5963273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5964273108faSLukas Czerner 
5965273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5966273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5967273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5968273108faSLukas Czerner 		err = -EROFS;
5969273108faSLukas Czerner 		goto err_out;
5970273108faSLukas Czerner 	}
5971ac27a0ecSDave Kleikamp 
5972ac27a0ecSDave Kleikamp 	/*
5973ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5974ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5975ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5976ac27a0ecSDave Kleikamp 	 */
5977e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5978bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5979b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5980b31e1552SEric Sandeen 					"required on readonly filesystem");
5981ac27a0ecSDave Kleikamp 			if (really_read_only) {
5982b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5983d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5984d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5985273108faSLukas Czerner 				err = -EROFS;
5986273108faSLukas Czerner 				goto err_out;
5987ac27a0ecSDave Kleikamp 			}
5988b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5989b31e1552SEric Sandeen 			       "be enabled during recovery");
5990ac27a0ecSDave Kleikamp 		}
5991ac27a0ecSDave Kleikamp 	}
5992ac27a0ecSDave Kleikamp 
599390576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5994b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
59954776004fSTheodore Ts'o 
5996e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5997dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
59981c13d5c0STheodore Ts'o 	if (!err) {
59991c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
6000*2ef6c32aSTheodore Ts'o 		__le16 orig_state;
6001*2ef6c32aSTheodore Ts'o 		bool changed = false;
6002eee00237SYe Bin 
60031c13d5c0STheodore Ts'o 		if (save)
60041c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
60051c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
6006dab291afSMingming Cao 		err = jbd2_journal_load(journal);
6007*2ef6c32aSTheodore Ts'o 		if (save && memcmp(((char *) es) + EXT4_S_ERR_START,
6008*2ef6c32aSTheodore Ts'o 				   save, EXT4_S_ERR_LEN)) {
60091c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
60101c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
6011*2ef6c32aSTheodore Ts'o 			changed = true;
6012*2ef6c32aSTheodore Ts'o 		}
60131c13d5c0STheodore Ts'o 		kfree(save);
6014*2ef6c32aSTheodore Ts'o 		orig_state = es->s_state;
6015eee00237SYe Bin 		es->s_state |= cpu_to_le16(EXT4_SB(sb)->s_mount_state &
6016eee00237SYe Bin 					   EXT4_ERROR_FS);
6017*2ef6c32aSTheodore Ts'o 		if (orig_state != es->s_state)
6018*2ef6c32aSTheodore Ts'o 			changed = true;
6019eee00237SYe Bin 		/* Write out restored error information to the superblock */
6020*2ef6c32aSTheodore Ts'o 		if (changed && !really_read_only) {
6021eee00237SYe Bin 			int err2;
6022eee00237SYe Bin 			err2 = ext4_commit_super(sb);
6023eee00237SYe Bin 			err = err ? : err2;
6024eee00237SYe Bin 		}
60251c13d5c0STheodore Ts'o 	}
6026ac27a0ecSDave Kleikamp 
6027ac27a0ecSDave Kleikamp 	if (err) {
6028b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
6029273108faSLukas Czerner 		goto err_out;
6030ac27a0ecSDave Kleikamp 	}
6031ac27a0ecSDave Kleikamp 
6032617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
603311215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
603411215630SJan Kara 	if (err) {
603511215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
603611215630SJan Kara 		jbd2_journal_destroy(journal);
603711215630SJan Kara 		return err;
603811215630SJan Kara 	}
6039ac27a0ecSDave Kleikamp 
6040c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
6041ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
6042ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
60433039d8b8SBaokun Li 		ext4_commit_super(sb);
60443039d8b8SBaokun Li 	}
60453039d8b8SBaokun Li 	if (!really_read_only && journal_inum &&
60463039d8b8SBaokun Li 	    journal_inum != le32_to_cpu(es->s_journal_inum)) {
60473039d8b8SBaokun Li 		es->s_journal_inum = cpu_to_le32(journal_inum);
60484392fbc4SJan Kara 		ext4_commit_super(sb);
6049ac27a0ecSDave Kleikamp 	}
6050ac27a0ecSDave Kleikamp 
6051ac27a0ecSDave Kleikamp 	return 0;
6052273108faSLukas Czerner 
6053273108faSLukas Czerner err_out:
6054273108faSLukas Czerner 	jbd2_journal_destroy(journal);
6055273108faSLukas Czerner 	return err;
6056ac27a0ecSDave Kleikamp }
6057ac27a0ecSDave Kleikamp 
60582d01ddc8SJan Kara /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
60592d01ddc8SJan Kara static void ext4_update_super(struct super_block *sb)
6060ac27a0ecSDave Kleikamp {
6061c92dc856SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6062e92ad03fSJan Kara 	struct ext4_super_block *es = sbi->s_es;
6063e92ad03fSJan Kara 	struct buffer_head *sbh = sbi->s_sbh;
6064ac27a0ecSDave Kleikamp 
606505c2c00fSJan Kara 	lock_buffer(sbh);
6066a17712c8SJon Derrick 	/*
606771290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
606871290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
606971290b36STheodore Ts'o 	 * write time when we are mounting the root file system
607071290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
607171290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
607271290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
607371290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
607471290b36STheodore Ts'o 	 * to complain and force a full file system check.
607571290b36STheodore Ts'o 	 */
60761751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
60776a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
6078afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
6079e92ad03fSJan Kara 		cpu_to_le64(sbi->s_kbytes_written +
60808446fe92SChristoph Hellwig 		    ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
6081e92ad03fSJan Kara 		      sbi->s_sectors_written_start) >> 1));
6082e92ad03fSJan Kara 	if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
608357042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
6084e92ad03fSJan Kara 			EXT4_C2B(sbi, percpu_counter_sum_positive(
6085e92ad03fSJan Kara 				&sbi->s_freeclusters_counter)));
6086e92ad03fSJan Kara 	if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
60877f93cff9STheodore Ts'o 		es->s_free_inodes_count =
60887f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
6089e92ad03fSJan Kara 				&sbi->s_freeinodes_counter));
6090c92dc856SJan Kara 	/* Copy error information to the on-disk superblock */
6091c92dc856SJan Kara 	spin_lock(&sbi->s_error_lock);
6092c92dc856SJan Kara 	if (sbi->s_add_error_count > 0) {
6093c92dc856SJan Kara 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
6094c92dc856SJan Kara 		if (!es->s_first_error_time && !es->s_first_error_time_hi) {
6095c92dc856SJan Kara 			__ext4_update_tstamp(&es->s_first_error_time,
6096c92dc856SJan Kara 					     &es->s_first_error_time_hi,
6097c92dc856SJan Kara 					     sbi->s_first_error_time);
6098c92dc856SJan Kara 			strncpy(es->s_first_error_func, sbi->s_first_error_func,
6099c92dc856SJan Kara 				sizeof(es->s_first_error_func));
6100c92dc856SJan Kara 			es->s_first_error_line =
6101c92dc856SJan Kara 				cpu_to_le32(sbi->s_first_error_line);
6102c92dc856SJan Kara 			es->s_first_error_ino =
6103c92dc856SJan Kara 				cpu_to_le32(sbi->s_first_error_ino);
6104c92dc856SJan Kara 			es->s_first_error_block =
6105c92dc856SJan Kara 				cpu_to_le64(sbi->s_first_error_block);
6106c92dc856SJan Kara 			es->s_first_error_errcode =
6107c92dc856SJan Kara 				ext4_errno_to_code(sbi->s_first_error_code);
6108c92dc856SJan Kara 		}
6109c92dc856SJan Kara 		__ext4_update_tstamp(&es->s_last_error_time,
6110c92dc856SJan Kara 				     &es->s_last_error_time_hi,
6111c92dc856SJan Kara 				     sbi->s_last_error_time);
6112c92dc856SJan Kara 		strncpy(es->s_last_error_func, sbi->s_last_error_func,
6113c92dc856SJan Kara 			sizeof(es->s_last_error_func));
6114c92dc856SJan Kara 		es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
6115c92dc856SJan Kara 		es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
6116c92dc856SJan Kara 		es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
6117c92dc856SJan Kara 		es->s_last_error_errcode =
6118c92dc856SJan Kara 				ext4_errno_to_code(sbi->s_last_error_code);
6119c92dc856SJan Kara 		/*
6120c92dc856SJan Kara 		 * Start the daily error reporting function if it hasn't been
6121c92dc856SJan Kara 		 * started already
6122c92dc856SJan Kara 		 */
6123c92dc856SJan Kara 		if (!es->s_error_count)
6124c92dc856SJan Kara 			mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
6125c92dc856SJan Kara 		le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
6126c92dc856SJan Kara 		sbi->s_add_error_count = 0;
6127c92dc856SJan Kara 	}
6128c92dc856SJan Kara 	spin_unlock(&sbi->s_error_lock);
6129c92dc856SJan Kara 
613006db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
61312d01ddc8SJan Kara 	unlock_buffer(sbh);
61322d01ddc8SJan Kara }
61332d01ddc8SJan Kara 
61342d01ddc8SJan Kara static int ext4_commit_super(struct super_block *sb)
61352d01ddc8SJan Kara {
61362d01ddc8SJan Kara 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
61372d01ddc8SJan Kara 
6138f88f1466SFengnan Chang 	if (!sbh)
6139f88f1466SFengnan Chang 		return -EINVAL;
6140f88f1466SFengnan Chang 	if (block_device_ejected(sb))
6141f88f1466SFengnan Chang 		return -ENODEV;
61422d01ddc8SJan Kara 
61432d01ddc8SJan Kara 	ext4_update_super(sb);
61442d01ddc8SJan Kara 
614515baa7dcSZhang Yi 	lock_buffer(sbh);
614615baa7dcSZhang Yi 	/* Buffer got discarded which means block device got invalidated */
614715baa7dcSZhang Yi 	if (!buffer_mapped(sbh)) {
614815baa7dcSZhang Yi 		unlock_buffer(sbh);
614915baa7dcSZhang Yi 		return -EIO;
615015baa7dcSZhang Yi 	}
615115baa7dcSZhang Yi 
6152e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
61534743f839SPranay Kr. Srivastava 		/*
61544743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
61554743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
61564743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
61574743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
61584743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
61594743f839SPranay Kr. Srivastava 		 * write and hope for the best.
61604743f839SPranay Kr. Srivastava 		 */
61614743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
61624743f839SPranay Kr. Srivastava 		       "superblock detected");
61634743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
61644743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
61654743f839SPranay Kr. Srivastava 	}
616615baa7dcSZhang Yi 	get_bh(sbh);
616715baa7dcSZhang Yi 	/* Clear potential dirty bit if it was journalled update */
616815baa7dcSZhang Yi 	clear_buffer_dirty(sbh);
616915baa7dcSZhang Yi 	sbh->b_end_io = end_buffer_write_sync;
61701420c4a5SBart Van Assche 	submit_bh(REQ_OP_WRITE | REQ_SYNC |
61711420c4a5SBart Van Assche 		  (test_opt(sb, BARRIER) ? REQ_FUA : 0), sbh);
617215baa7dcSZhang Yi 	wait_on_buffer(sbh);
6173c89128a0SJaegeuk Kim 	if (buffer_write_io_error(sbh)) {
6174b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error while writing "
6175b31e1552SEric Sandeen 		       "superblock");
6176914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
6177914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
617815baa7dcSZhang Yi 		return -EIO;
6179914258bfSTheodore Ts'o 	}
618015baa7dcSZhang Yi 	return 0;
6181ac27a0ecSDave Kleikamp }
6182ac27a0ecSDave Kleikamp 
6183ac27a0ecSDave Kleikamp /*
6184ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
6185ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
6186ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
6187ac27a0ecSDave Kleikamp  */
618811215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
6189617ba13bSMingming Cao 				       struct ext4_super_block *es)
6190ac27a0ecSDave Kleikamp {
619111215630SJan Kara 	int err;
6192617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
6193ac27a0ecSDave Kleikamp 
6194e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
619511215630SJan Kara 		if (journal != NULL) {
619611215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
619711215630SJan Kara 				   "mounted!");
619811215630SJan Kara 			return -EFSCORRUPTED;
619911215630SJan Kara 		}
620011215630SJan Kara 		return 0;
62010390131bSFrank Mayhar 	}
6202dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
620301d5d965SLeah Rumancik 	err = jbd2_journal_flush(journal, 0);
620411215630SJan Kara 	if (err < 0)
62057ffe1ea8SHidehiro Kawai 		goto out;
62067ffe1ea8SHidehiro Kawai 
620702f310fcSJan Kara 	if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
620802f310fcSJan Kara 	    ext4_has_feature_orphan_present(sb))) {
620902f310fcSJan Kara 		if (!ext4_orphan_file_empty(sb)) {
621002f310fcSJan Kara 			ext4_error(sb, "Orphan file not empty on read-only fs.");
621102f310fcSJan Kara 			err = -EFSCORRUPTED;
621202f310fcSJan Kara 			goto out;
621302f310fcSJan Kara 		}
6214e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
621502f310fcSJan Kara 		ext4_clear_feature_orphan_present(sb);
62164392fbc4SJan Kara 		ext4_commit_super(sb);
6217ac27a0ecSDave Kleikamp 	}
62187ffe1ea8SHidehiro Kawai out:
6219dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
622011215630SJan Kara 	return err;
6221ac27a0ecSDave Kleikamp }
6222ac27a0ecSDave Kleikamp 
6223ac27a0ecSDave Kleikamp /*
6224ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
6225ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
6226ac27a0ecSDave Kleikamp  * main filesystem now.
6227ac27a0ecSDave Kleikamp  */
622811215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
6229617ba13bSMingming Cao 				   struct ext4_super_block *es)
6230ac27a0ecSDave Kleikamp {
6231ac27a0ecSDave Kleikamp 	journal_t *journal;
6232ac27a0ecSDave Kleikamp 	int j_errno;
6233ac27a0ecSDave Kleikamp 	const char *errstr;
6234ac27a0ecSDave Kleikamp 
623511215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
623611215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
623711215630SJan Kara 		return -EFSCORRUPTED;
623811215630SJan Kara 	}
62390390131bSFrank Mayhar 
6240617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
6241ac27a0ecSDave Kleikamp 
6242ac27a0ecSDave Kleikamp 	/*
6243ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
6244617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
6245ac27a0ecSDave Kleikamp 	 */
6246ac27a0ecSDave Kleikamp 
6247dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
6248ac27a0ecSDave Kleikamp 	if (j_errno) {
6249ac27a0ecSDave Kleikamp 		char nbuf[16];
6250ac27a0ecSDave Kleikamp 
6251617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
625212062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
6253ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
6254ac27a0ecSDave Kleikamp 
6255617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
6256617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
6257f57886caSYe Bin 		j_errno = ext4_commit_super(sb);
6258f57886caSYe Bin 		if (j_errno)
6259f57886caSYe Bin 			return j_errno;
6260f57886caSYe Bin 		ext4_warning(sb, "Marked fs in need of filesystem check.");
6261ac27a0ecSDave Kleikamp 
6262dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
6263d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
6264ac27a0ecSDave Kleikamp 	}
626511215630SJan Kara 	return 0;
6266ac27a0ecSDave Kleikamp }
6267ac27a0ecSDave Kleikamp 
6268ac27a0ecSDave Kleikamp /*
6269ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
6270ac27a0ecSDave Kleikamp  * and wait on the commit.
6271ac27a0ecSDave Kleikamp  */
6272617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
6273ac27a0ecSDave Kleikamp {
6274ac27a0ecSDave Kleikamp 	journal_t *journal;
6275ac27a0ecSDave Kleikamp 
6276bc98a42cSDavid Howells 	if (sb_rdonly(sb))
6277ac27a0ecSDave Kleikamp 		return 0;
6278ac27a0ecSDave Kleikamp 
6279617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
6280b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
6281ac27a0ecSDave Kleikamp }
6282ac27a0ecSDave Kleikamp 
6283617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
6284ac27a0ecSDave Kleikamp {
628514ce0cb4STheodore Ts'o 	int ret = 0;
62869eddacf9SJan Kara 	tid_t target;
628706a407f1SDmitry Monakhov 	bool needs_barrier = false;
62888d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6289ac27a0ecSDave Kleikamp 
629049598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
62910db1ff22STheodore Ts'o 		return 0;
62920db1ff22STheodore Ts'o 
62939bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
62942e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
6295a1177825SJan Kara 	/*
6296a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
6297a1177825SJan Kara 	 * no dirty dquots
6298a1177825SJan Kara 	 */
6299a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
630006a407f1SDmitry Monakhov 	/*
630106a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
630206a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
630306a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
630406a407f1SDmitry Monakhov 	 */
6305bda32530STheodore Ts'o 	if (sbi->s_journal) {
630606a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
630706a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
630806a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
630906a407f1SDmitry Monakhov 			needs_barrier = true;
631006a407f1SDmitry Monakhov 
63118d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
6312ac27a0ecSDave Kleikamp 			if (wait)
6313bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
6314bda32530STheodore Ts'o 							   target);
63150390131bSFrank Mayhar 		}
6316bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
6317bda32530STheodore Ts'o 		needs_barrier = true;
631806a407f1SDmitry Monakhov 	if (needs_barrier) {
631906a407f1SDmitry Monakhov 		int err;
6320c6bf3f0eSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev);
632106a407f1SDmitry Monakhov 		if (!ret)
632206a407f1SDmitry Monakhov 			ret = err;
632306a407f1SDmitry Monakhov 	}
632406a407f1SDmitry Monakhov 
632506a407f1SDmitry Monakhov 	return ret;
632606a407f1SDmitry Monakhov }
632706a407f1SDmitry Monakhov 
6328ac27a0ecSDave Kleikamp /*
6329ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
6330ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
6331be4f27d3SYongqiang Yang  *
6332be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
63338e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
63348e8ad8a5SJan Kara  * modifications.
6335ac27a0ecSDave Kleikamp  */
6336c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
6337ac27a0ecSDave Kleikamp {
6338c4be0c1dSTakashi Sato 	int error = 0;
6339c4be0c1dSTakashi Sato 	journal_t *journal;
6340ac27a0ecSDave Kleikamp 
6341bc98a42cSDavid Howells 	if (sb_rdonly(sb))
63429ca92389STheodore Ts'o 		return 0;
63439ca92389STheodore Ts'o 
6344c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
6345ac27a0ecSDave Kleikamp 
6346bb044576STheodore Ts'o 	if (journal) {
6347ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
6348dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
63497ffe1ea8SHidehiro Kawai 
63507ffe1ea8SHidehiro Kawai 		/*
6351bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
6352bb044576STheodore Ts'o 		 * flush the journal.
63537ffe1ea8SHidehiro Kawai 		 */
635401d5d965SLeah Rumancik 		error = jbd2_journal_flush(journal, 0);
63556b0310fbSEric Sandeen 		if (error < 0)
63566b0310fbSEric Sandeen 			goto out;
6357ac27a0ecSDave Kleikamp 
6358ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
6359e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
636002f310fcSJan Kara 		if (ext4_orphan_file_empty(sb))
636102f310fcSJan Kara 			ext4_clear_feature_orphan_present(sb);
6362c642dc9eSEric Sandeen 	}
6363c642dc9eSEric Sandeen 
63644392fbc4SJan Kara 	error = ext4_commit_super(sb);
63656b0310fbSEric Sandeen out:
6366bb044576STheodore Ts'o 	if (journal)
63678e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
6368bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
63696b0310fbSEric Sandeen 	return error;
6370ac27a0ecSDave Kleikamp }
6371ac27a0ecSDave Kleikamp 
6372ac27a0ecSDave Kleikamp /*
6373ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
6374ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
6375ac27a0ecSDave Kleikamp  */
6376c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
6377ac27a0ecSDave Kleikamp {
6378bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
63799ca92389STheodore Ts'o 		return 0;
63809ca92389STheodore Ts'o 
6381c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
63829ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
6383e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
638402f310fcSJan Kara 		if (ext4_has_feature_orphan_file(sb))
638502f310fcSJan Kara 			ext4_set_feature_orphan_present(sb);
6386c642dc9eSEric Sandeen 	}
6387c642dc9eSEric Sandeen 
63884392fbc4SJan Kara 	ext4_commit_super(sb);
6389c4be0c1dSTakashi Sato 	return 0;
6390ac27a0ecSDave Kleikamp }
6391ac27a0ecSDave Kleikamp 
6392673c6100STheodore Ts'o /*
6393673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
6394673c6100STheodore Ts'o  */
6395673c6100STheodore Ts'o struct ext4_mount_options {
6396673c6100STheodore Ts'o 	unsigned long s_mount_opt;
6397a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
639808cefc7aSEric W. Biederman 	kuid_t s_resuid;
639908cefc7aSEric W. Biederman 	kgid_t s_resgid;
6400673c6100STheodore Ts'o 	unsigned long s_commit_interval;
6401673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
6402673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
6403673c6100STheodore Ts'o 	int s_jquota_fmt;
6404a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
6405673c6100STheodore Ts'o #endif
6406673c6100STheodore Ts'o };
6407673c6100STheodore Ts'o 
6408960e0ab6SLukas Czerner static int __ext4_remount(struct fs_context *fc, struct super_block *sb)
6409ac27a0ecSDave Kleikamp {
64107edfd85bSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
6411617ba13bSMingming Cao 	struct ext4_super_block *es;
6412617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6413960e0ab6SLukas Czerner 	unsigned long old_sb_flags;
6414617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
64158a266467STheodore Ts'o 	ext4_group_t g;
6416c5e06d10SJohann Lombardi 	int err = 0;
6417ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
64183bbef91bSAustin Kim 	int enable_quota = 0;
641903dafb5fSChen Gang 	int i, j;
642033458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
6421ac27a0ecSDave Kleikamp #endif
6422b237e304SHarshad Shirwadkar 
642321ac738eSChengguang Xu 
6424ac27a0ecSDave Kleikamp 	/* Store the original options */
6425ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
6426ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
6427a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
6428ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
6429ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
6430ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
643130773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
643230773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
6433ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6434ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
6435a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
643603dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
643733458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
643833458eabSTheodore Ts'o 
643933458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
644003dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
644103dafb5fSChen Gang 				for (j = 0; j < i; j++)
644203dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
644303dafb5fSChen Gang 				return -ENOMEM;
644403dafb5fSChen Gang 			}
644503dafb5fSChen Gang 		} else
644603dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
6447ac27a0ecSDave Kleikamp #endif
6448e4e58e5dSOjaswin Mujoo 	if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)) {
6449b3881f74STheodore Ts'o 		if (sbi->s_journal && sbi->s_journal->j_task->io_context)
64507edfd85bSLukas Czerner 			ctx->journal_ioprio =
6451b237e304SHarshad Shirwadkar 				sbi->s_journal->j_task->io_context->ioprio;
6452e4e58e5dSOjaswin Mujoo 		else
6453e4e58e5dSOjaswin Mujoo 			ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
6454e4e58e5dSOjaswin Mujoo 
6455e4e58e5dSOjaswin Mujoo 	}
6456ac27a0ecSDave Kleikamp 
64577edfd85bSLukas Czerner 	ext4_apply_options(fc, sb);
6458ac27a0ecSDave Kleikamp 
64596b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
64606b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
64616b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
64622d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
64632d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
6464c6d3d56dSDarrick J. Wong 	}
6465c6d3d56dSDarrick J. Wong 
64666ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
64676ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
64686ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
64696ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
64706ae6514bSPiotr Sarna 			err = -EINVAL;
64716ae6514bSPiotr Sarna 			goto restore_opts;
64726ae6514bSPiotr Sarna 		}
64736ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
64746ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
64756ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
64766ae6514bSPiotr Sarna 			err = -EINVAL;
64776ae6514bSPiotr Sarna 			goto restore_opts;
64786ae6514bSPiotr Sarna 		}
6479ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
6480ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
6481ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
6482ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
6483ab04df78SJan Kara 			err = -EINVAL;
6484ab04df78SJan Kara 			goto restore_opts;
6485ab04df78SJan Kara 		}
6486923ae0ffSRoss Zwisler 	}
6487923ae0ffSRoss Zwisler 
6488cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
6489cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
6490cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
6491cdb7ee4cSTahsin Erdogan 		goto restore_opts;
6492cdb7ee4cSTahsin Erdogan 	}
6493cdb7ee4cSTahsin Erdogan 
64949b5f6c9bSHarshad Shirwadkar 	if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
6495124e7c61SGabriel Krisman Bertazi 		ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
6496ac27a0ecSDave Kleikamp 
64971751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
64981751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
6499ac27a0ecSDave Kleikamp 
6500ac27a0ecSDave Kleikamp 	es = sbi->s_es;
6501ac27a0ecSDave Kleikamp 
6502b3881f74STheodore Ts'o 	if (sbi->s_journal) {
6503617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
65047edfd85bSLukas Czerner 		set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
6505b3881f74STheodore Ts'o 	}
6506ac27a0ecSDave Kleikamp 
6507c92dc856SJan Kara 	/* Flush outstanding errors before changing fs state */
6508c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
6509c92dc856SJan Kara 
6510960e0ab6SLukas Czerner 	if ((bool)(fc->sb_flags & SB_RDONLY) != sb_rdonly(sb)) {
65119b5f6c9bSHarshad Shirwadkar 		if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) {
6512ac27a0ecSDave Kleikamp 			err = -EROFS;
6513ac27a0ecSDave Kleikamp 			goto restore_opts;
6514ac27a0ecSDave Kleikamp 		}
6515ac27a0ecSDave Kleikamp 
6516960e0ab6SLukas Czerner 		if (fc->sb_flags & SB_RDONLY) {
651738c03b34STheodore Ts'o 			err = sync_filesystem(sb);
651838c03b34STheodore Ts'o 			if (err < 0)
651938c03b34STheodore Ts'o 				goto restore_opts;
65200f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
65210f0dd62fSChristoph Hellwig 			if (err < 0)
6522c79d967dSChristoph Hellwig 				goto restore_opts;
6523c79d967dSChristoph Hellwig 
6524ac27a0ecSDave Kleikamp 			/*
6525ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
6526ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
6527ac27a0ecSDave Kleikamp 			 */
65281751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
6529ac27a0ecSDave Kleikamp 
6530ac27a0ecSDave Kleikamp 			/*
6531ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
6532ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
6533ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
6534ac27a0ecSDave Kleikamp 			 */
6535617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
6536617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
6537ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
6538ac27a0ecSDave Kleikamp 
653911215630SJan Kara 			if (sbi->s_journal) {
654011215630SJan Kara 				/*
654111215630SJan Kara 				 * We let remount-ro finish even if marking fs
654211215630SJan Kara 				 * as clean failed...
654311215630SJan Kara 				 */
6544617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
654511215630SJan Kara 			}
6546ac27a0ecSDave Kleikamp 		} else {
6547a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
6548e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
65492cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
6550ac27a0ecSDave Kleikamp 				err = -EROFS;
6551ac27a0ecSDave Kleikamp 				goto restore_opts;
6552ac27a0ecSDave Kleikamp 			}
6553ead6596bSEric Sandeen 			/*
65548a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
65550b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
65568a266467STheodore Ts'o 			 */
65578a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
65588a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
65598a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
65608a266467STheodore Ts'o 
6561feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
6562b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
6563b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
6564e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
65658a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
65666a797d27SDarrick J. Wong 					err = -EFSBADCRC;
65678a266467STheodore Ts'o 					goto restore_opts;
65688a266467STheodore Ts'o 				}
65698a266467STheodore Ts'o 			}
65708a266467STheodore Ts'o 
65718a266467STheodore Ts'o 			/*
6572ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
6573ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
6574ead6596bSEric Sandeen 			 * require a full umount/remount for now.
6575ead6596bSEric Sandeen 			 */
657602f310fcSJan Kara 			if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
6577b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
6578ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
6579ead6596bSEric Sandeen 				       "orphan inode list.  Please "
6580b31e1552SEric Sandeen 				       "umount/remount instead");
6581ead6596bSEric Sandeen 				err = -EINVAL;
6582ead6596bSEric Sandeen 				goto restore_opts;
6583ead6596bSEric Sandeen 			}
6584ead6596bSEric Sandeen 
6585ac27a0ecSDave Kleikamp 			/*
6586ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
6587ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
6588ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
6589ac27a0ecSDave Kleikamp 			 * the partition.)
6590ac27a0ecSDave Kleikamp 			 */
659111215630SJan Kara 			if (sbi->s_journal) {
659211215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
659311215630SJan Kara 				if (err)
659411215630SJan Kara 					goto restore_opts;
659511215630SJan Kara 			}
6596c878bea3STheodore Ts'o 			sbi->s_mount_state = (le16_to_cpu(es->s_state) &
6597c878bea3STheodore Ts'o 					      ~EXT4_FC_REPLAY);
6598c89128a0SJaegeuk Kim 
6599c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
6600c89128a0SJaegeuk Kim 			if (err)
6601c89128a0SJaegeuk Kim 				goto restore_opts;
6602c89128a0SJaegeuk Kim 
66031b292439STheodore Ts'o 			sb->s_flags &= ~SB_RDONLY;
66043b50d501STheodore Ts'o 			if (ext4_has_feature_mmp(sb)) {
66053b50d501STheodore Ts'o 				err = ext4_multi_mount_protect(sb,
66063b50d501STheodore Ts'o 						le64_to_cpu(es->s_mmp_block));
66073b50d501STheodore Ts'o 				if (err)
6608c5e06d10SJohann Lombardi 					goto restore_opts;
6609c5e06d10SJohann Lombardi 			}
66103bbef91bSAustin Kim #ifdef CONFIG_QUOTA
6611c79d967dSChristoph Hellwig 			enable_quota = 1;
66123bbef91bSAustin Kim #endif
6613ac27a0ecSDave Kleikamp 		}
6614ac27a0ecSDave Kleikamp 	}
6615bfff6873SLukas Czerner 
6616bfff6873SLukas Czerner 	/*
66170f5bde1dSJan Kara 	 * Handle creation of system zone data early because it can fail.
66180f5bde1dSJan Kara 	 * Releasing of existing data is done when we are sure remount will
66190f5bde1dSJan Kara 	 * succeed.
66200f5bde1dSJan Kara 	 */
6621dd0db94fSChunguang Xu 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
6622d176b1f6SJan Kara 		err = ext4_setup_system_zone(sb);
6623d176b1f6SJan Kara 		if (err)
6624d176b1f6SJan Kara 			goto restore_opts;
66250f5bde1dSJan Kara 	}
6626d176b1f6SJan Kara 
6627c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
66284392fbc4SJan Kara 		err = ext4_commit_super(sb);
6629c89128a0SJaegeuk Kim 		if (err)
6630c89128a0SJaegeuk Kim 			goto restore_opts;
6631c89128a0SJaegeuk Kim 	}
66320390131bSFrank Mayhar 
6633ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
66347c319d32SAditya Kali 	if (enable_quota) {
66357c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
66360f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
6637e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
66387c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
663907724f98STheodore Ts'o 			if (err)
66407c319d32SAditya Kali 				goto restore_opts;
66417c319d32SAditya Kali 		}
66427c319d32SAditya Kali 	}
66434c0b4818STheodore Ts'o 	/* Release old quota file names */
66444c0b4818STheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
66454c0b4818STheodore Ts'o 		kfree(old_opts.s_qf_names[i]);
66467c319d32SAditya Kali #endif
6647dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
66480f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6649d4c402d9SCurt Wohlgemuth 
6650eb1f822cSTheodore Ts'o 	/*
6651eb1f822cSTheodore Ts'o 	 * Reinitialize lazy itable initialization thread based on
6652eb1f822cSTheodore Ts'o 	 * current settings
6653eb1f822cSTheodore Ts'o 	 */
6654eb1f822cSTheodore Ts'o 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
6655eb1f822cSTheodore Ts'o 		ext4_unregister_li_request(sb);
6656eb1f822cSTheodore Ts'o 	else {
6657eb1f822cSTheodore Ts'o 		ext4_group_t first_not_zeroed;
6658eb1f822cSTheodore Ts'o 		first_not_zeroed = ext4_has_uninit_itable(sb);
6659eb1f822cSTheodore Ts'o 		ext4_register_li_request(sb, first_not_zeroed);
6660eb1f822cSTheodore Ts'o 	}
6661eb1f822cSTheodore Ts'o 
666261bb4a1cSTheodore Ts'o 	if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
666361bb4a1cSTheodore Ts'o 		ext4_stop_mmpd(sbi);
666461bb4a1cSTheodore Ts'o 
6665ac27a0ecSDave Kleikamp 	return 0;
66660b8e58a1SAndreas Dilger 
6667ac27a0ecSDave Kleikamp restore_opts:
66684c0b4818STheodore Ts'o 	/*
66694c0b4818STheodore Ts'o 	 * If there was a failing r/w to ro transition, we may need to
66704c0b4818STheodore Ts'o 	 * re-enable quota
66714c0b4818STheodore Ts'o 	 */
66724c0b4818STheodore Ts'o 	if ((sb->s_flags & SB_RDONLY) && !(old_sb_flags & SB_RDONLY) &&
66734c0b4818STheodore Ts'o 	    sb_any_quota_suspended(sb))
66744c0b4818STheodore Ts'o 		dquot_resume(sb, -1);
6675ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
6676ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
6677a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6678ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
6679ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
6680ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
668130773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
668230773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
6683dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
66840f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6685ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6686ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6687a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
668833458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
668933458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6690ac27a0ecSDave Kleikamp 	}
669133458eabSTheodore Ts'o 	synchronize_rcu();
669233458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
669333458eabSTheodore Ts'o 		kfree(to_free[i]);
6694ac27a0ecSDave Kleikamp #endif
669561bb4a1cSTheodore Ts'o 	if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
669661bb4a1cSTheodore Ts'o 		ext4_stop_mmpd(sbi);
6697ac27a0ecSDave Kleikamp 	return err;
6698ac27a0ecSDave Kleikamp }
6699ac27a0ecSDave Kleikamp 
6700cebe85d5SLukas Czerner static int ext4_reconfigure(struct fs_context *fc)
67017edfd85bSLukas Czerner {
6702cebe85d5SLukas Czerner 	struct super_block *sb = fc->root->d_sb;
67037edfd85bSLukas Czerner 	int ret;
67047edfd85bSLukas Czerner 
6705cebe85d5SLukas Czerner 	fc->s_fs_info = EXT4_SB(sb);
67067edfd85bSLukas Czerner 
6707cebe85d5SLukas Czerner 	ret = ext4_check_opt_consistency(fc, sb);
67087edfd85bSLukas Czerner 	if (ret < 0)
67097edfd85bSLukas Czerner 		return ret;
6710cebe85d5SLukas Czerner 
6711960e0ab6SLukas Czerner 	ret = __ext4_remount(fc, sb);
6712cebe85d5SLukas Czerner 	if (ret < 0)
6713cebe85d5SLukas Czerner 		return ret;
6714cebe85d5SLukas Czerner 
67156dcc98fbSTheodore Ts'o 	ext4_msg(sb, KERN_INFO, "re-mounted %pU %s. Quota mode: %s.",
67166dcc98fbSTheodore Ts'o 		 &sb->s_uuid, sb_rdonly(sb) ? "ro" : "r/w",
67176dcc98fbSTheodore Ts'o 		 ext4_quota_mode(sb));
6718cebe85d5SLukas Czerner 
6719cebe85d5SLukas Czerner 	return 0;
67207edfd85bSLukas Czerner }
67217edfd85bSLukas Czerner 
6722689c958cSLi Xi #ifdef CONFIG_QUOTA
6723689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
6724689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
6725689c958cSLi Xi {
6726689c958cSLi Xi 	struct kqid qid;
6727689c958cSLi Xi 	struct dquot *dquot;
6728689c958cSLi Xi 	u64 limit;
6729689c958cSLi Xi 	u64 curblock;
6730689c958cSLi Xi 
6731689c958cSLi Xi 	qid = make_kqid_projid(projid);
6732689c958cSLi Xi 	dquot = dqget(sb, qid);
6733689c958cSLi Xi 	if (IS_ERR(dquot))
6734689c958cSLi Xi 		return PTR_ERR(dquot);
67357b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
6736689c958cSLi Xi 
6737a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6738a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
673957c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
674057c32ea4SChengguang Xu 
6741689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
6742f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
6743f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6744689c958cSLi Xi 		buf->f_blocks = limit;
6745689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
6746689c958cSLi Xi 			(buf->f_blocks > curblock) ?
6747689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
6748689c958cSLi Xi 	}
6749689c958cSLi Xi 
6750a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6751a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
6752689c958cSLi Xi 	if (limit && buf->f_files > limit) {
6753689c958cSLi Xi 		buf->f_files = limit;
6754689c958cSLi Xi 		buf->f_ffree =
6755689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6756689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6757689c958cSLi Xi 	}
6758689c958cSLi Xi 
67597b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
6760689c958cSLi Xi 	dqput(dquot);
6761689c958cSLi Xi 	return 0;
6762689c958cSLi Xi }
6763689c958cSLi Xi #endif
6764689c958cSLi Xi 
6765617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6766ac27a0ecSDave Kleikamp {
6767ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
6768617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6769617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
677027dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
6771d02a9391SKazuya Mio 	s64 bfree;
677227dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6773ac27a0ecSDave Kleikamp 
6774952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
6775952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
6776ac27a0ecSDave Kleikamp 
6777617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
6778ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
6779b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
678057042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
678157042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6782d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
678357042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
678427dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
678527dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
678627dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6787ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
6788ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
678952d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6790617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
67919591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(es->s_uuid);
67920b8e58a1SAndreas Dilger 
6793689c958cSLi Xi #ifdef CONFIG_QUOTA
6794689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6795689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6796689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6797689c958cSLi Xi #endif
6798ac27a0ecSDave Kleikamp 	return 0;
6799ac27a0ecSDave Kleikamp }
6800ac27a0ecSDave Kleikamp 
6801ac27a0ecSDave Kleikamp 
6802ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6803ac27a0ecSDave Kleikamp 
6804bc8230eeSJan Kara /*
6805bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
6806bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
6807bc8230eeSJan Kara  */
6808ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
6809ac27a0ecSDave Kleikamp {
68104c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6811ac27a0ecSDave Kleikamp }
6812ac27a0ecSDave Kleikamp 
6813617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
6814ac27a0ecSDave Kleikamp {
6815ac27a0ecSDave Kleikamp 	int ret, err;
6816ac27a0ecSDave Kleikamp 	handle_t *handle;
6817ac27a0ecSDave Kleikamp 	struct inode *inode;
6818ac27a0ecSDave Kleikamp 
6819ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
68209924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6821617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6822ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6823ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6824ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
6825617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6826ac27a0ecSDave Kleikamp 	if (!ret)
6827ac27a0ecSDave Kleikamp 		ret = err;
6828ac27a0ecSDave Kleikamp 	return ret;
6829ac27a0ecSDave Kleikamp }
6830ac27a0ecSDave Kleikamp 
6831617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
6832ac27a0ecSDave Kleikamp {
6833ac27a0ecSDave Kleikamp 	int ret, err;
6834ac27a0ecSDave Kleikamp 	handle_t *handle;
6835ac27a0ecSDave Kleikamp 
68369924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6837617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6838ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6839ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6840ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
6841617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6842ac27a0ecSDave Kleikamp 	if (!ret)
6843ac27a0ecSDave Kleikamp 		ret = err;
6844ac27a0ecSDave Kleikamp 	return ret;
6845ac27a0ecSDave Kleikamp }
6846ac27a0ecSDave Kleikamp 
6847617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
6848ac27a0ecSDave Kleikamp {
6849ac27a0ecSDave Kleikamp 	int ret, err;
6850ac27a0ecSDave Kleikamp 	handle_t *handle;
6851ac27a0ecSDave Kleikamp 
68529924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6853617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
68549c3013e9SJan Kara 	if (IS_ERR(handle)) {
68559c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
68569c3013e9SJan Kara 		dquot_release(dquot);
6857ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
68589c3013e9SJan Kara 	}
6859ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
6860617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6861ac27a0ecSDave Kleikamp 	if (!ret)
6862ac27a0ecSDave Kleikamp 		ret = err;
6863ac27a0ecSDave Kleikamp 	return ret;
6864ac27a0ecSDave Kleikamp }
6865ac27a0ecSDave Kleikamp 
6866617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
6867ac27a0ecSDave Kleikamp {
6868262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
6869262b4662SJan Kara 
6870f177ee08SRoman Anufriev 	if (ext4_is_quota_journalled(sb)) {
6871ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
6872617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
6873ac27a0ecSDave Kleikamp 	} else {
6874ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
6875ac27a0ecSDave Kleikamp 	}
6876ac27a0ecSDave Kleikamp }
6877ac27a0ecSDave Kleikamp 
6878617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
6879ac27a0ecSDave Kleikamp {
6880ac27a0ecSDave Kleikamp 	int ret, err;
6881ac27a0ecSDave Kleikamp 	handle_t *handle;
6882ac27a0ecSDave Kleikamp 
6883ac27a0ecSDave Kleikamp 	/* Data block + inode block */
6884f9c1f248SBaokun Li 	handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
6885ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6886ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6887ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
6888617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6889ac27a0ecSDave Kleikamp 	if (!ret)
6890ac27a0ecSDave Kleikamp 		ret = err;
6891ac27a0ecSDave Kleikamp 	return ret;
6892ac27a0ecSDave Kleikamp }
6893ac27a0ecSDave Kleikamp 
6894daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6895daf647d2STheodore Ts'o {
6896daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
6897daf647d2STheodore Ts'o 
6898daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
6899daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
6900daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
6901daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
6902daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
6903daf647d2STheodore Ts'o 	 */
6904daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6905daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6906daf647d2STheodore Ts'o }
6907daf647d2STheodore Ts'o 
6908ac27a0ecSDave Kleikamp /*
6909ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
6910ac27a0ecSDave Kleikamp  */
6911617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
69128c54ca9cSAl Viro 			 const struct path *path)
6913ac27a0ecSDave Kleikamp {
6914ac27a0ecSDave Kleikamp 	int err;
6915ac27a0ecSDave Kleikamp 
6916ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
6917ac27a0ecSDave Kleikamp 		return -EINVAL;
69180623543bSJan Kara 
6919ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
6920d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
6921ac27a0ecSDave Kleikamp 		return -EXDEV;
6922e0770e91SJan Kara 
6923e0770e91SJan Kara 	/* Quota already enabled for this file? */
6924e0770e91SJan Kara 	if (IS_NOQUOTA(d_inode(path->dentry)))
6925e0770e91SJan Kara 		return -EBUSY;
6926e0770e91SJan Kara 
69270623543bSJan Kara 	/* Journaling quota? */
69280623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
69292b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
6930f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
6931b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
6932b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
6933b31e1552SEric Sandeen 				"Journaled quota will not work");
693491389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
693591389240SJan Kara 	} else {
693691389240SJan Kara 		/*
693791389240SJan Kara 		 * Clear the flag just in case mount options changed since
693891389240SJan Kara 		 * last time.
693991389240SJan Kara 		 */
694091389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
69410623543bSJan Kara 	}
69420623543bSJan Kara 
6943daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6944daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
694515fc69bbSJan Kara 	if (!err) {
6946957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6947957153fcSJan Kara 		handle_t *handle;
6948957153fcSJan Kara 
694961a92987SJan Kara 		/*
695061a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
695161a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
695261a92987SJan Kara 		 * are already enabled and this is not a hard failure.
695361a92987SJan Kara 		 */
6954957153fcSJan Kara 		inode_lock(inode);
6955957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6956957153fcSJan Kara 		if (IS_ERR(handle))
6957957153fcSJan Kara 			goto unlock_inode;
6958957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6959957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6960957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
69614209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6962957153fcSJan Kara 		ext4_journal_stop(handle);
6963957153fcSJan Kara 	unlock_inode:
6964957153fcSJan Kara 		inode_unlock(inode);
696515fc69bbSJan Kara 		if (err)
696615fc69bbSJan Kara 			dquot_quota_off(sb, type);
6967957153fcSJan Kara 	}
696815fc69bbSJan Kara 	if (err)
696915fc69bbSJan Kara 		lockdep_set_quota_inode(path->dentry->d_inode,
697015fc69bbSJan Kara 					     I_DATA_SEM_NORMAL);
6971daf647d2STheodore Ts'o 	return err;
6972ac27a0ecSDave Kleikamp }
6973ac27a0ecSDave Kleikamp 
697407342ec2SBaokun Li static inline bool ext4_check_quota_inum(int type, unsigned long qf_inum)
697507342ec2SBaokun Li {
697607342ec2SBaokun Li 	switch (type) {
697707342ec2SBaokun Li 	case USRQUOTA:
697807342ec2SBaokun Li 		return qf_inum == EXT4_USR_QUOTA_INO;
697907342ec2SBaokun Li 	case GRPQUOTA:
698007342ec2SBaokun Li 		return qf_inum == EXT4_GRP_QUOTA_INO;
698107342ec2SBaokun Li 	case PRJQUOTA:
698207342ec2SBaokun Li 		return qf_inum >= EXT4_GOOD_OLD_FIRST_INO;
698307342ec2SBaokun Li 	default:
698407342ec2SBaokun Li 		BUG();
698507342ec2SBaokun Li 	}
698607342ec2SBaokun Li }
698707342ec2SBaokun Li 
69887c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
69897c319d32SAditya Kali 			     unsigned int flags)
69907c319d32SAditya Kali {
69917c319d32SAditya Kali 	int err;
69927c319d32SAditya Kali 	struct inode *qf_inode;
6993a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
69947c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6995689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6996689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
69977c319d32SAditya Kali 	};
69987c319d32SAditya Kali 
6999e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
70007c319d32SAditya Kali 
70017c319d32SAditya Kali 	if (!qf_inums[type])
70027c319d32SAditya Kali 		return -EPERM;
70037c319d32SAditya Kali 
700407342ec2SBaokun Li 	if (!ext4_check_quota_inum(type, qf_inums[type])) {
700507342ec2SBaokun Li 		ext4_error(sb, "Bad quota inum: %lu, type: %d",
700607342ec2SBaokun Li 				qf_inums[type], type);
700707342ec2SBaokun Li 		return -EUCLEAN;
700807342ec2SBaokun Li 	}
700907342ec2SBaokun Li 
70108a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
70117c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
701207342ec2SBaokun Li 		ext4_error(sb, "Bad quota inode: %lu, type: %d",
701307342ec2SBaokun Li 				qf_inums[type], type);
70147c319d32SAditya Kali 		return PTR_ERR(qf_inode);
70157c319d32SAditya Kali 	}
70167c319d32SAditya Kali 
7017bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
7018bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
7019daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
70207212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
7021daf647d2STheodore Ts'o 	if (err)
7022daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
702361157b24SPan Bian 	iput(qf_inode);
70247c319d32SAditya Kali 
70257c319d32SAditya Kali 	return err;
70267c319d32SAditya Kali }
70277c319d32SAditya Kali 
70287c319d32SAditya Kali /* Enable usage tracking for all quota types. */
702925c6d98fSJan Kara int ext4_enable_quotas(struct super_block *sb)
70307c319d32SAditya Kali {
70317c319d32SAditya Kali 	int type, err = 0;
7032a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
70337c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
7034689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
7035689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
70367c319d32SAditya Kali 	};
703749da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
703849da9392SJan Kara 		test_opt(sb, USRQUOTA),
703949da9392SJan Kara 		test_opt(sb, GRPQUOTA),
704049da9392SJan Kara 		test_opt(sb, PRJQUOTA),
704149da9392SJan Kara 	};
70427c319d32SAditya Kali 
704391389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
7044a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
70457c319d32SAditya Kali 		if (qf_inums[type]) {
70467c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
704749da9392SJan Kara 				DQUOT_USAGE_ENABLED |
704849da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
70497c319d32SAditya Kali 			if (err) {
70507c319d32SAditya Kali 				ext4_warning(sb,
705172ba7450STheodore Ts'o 					"Failed to enable quota tracking "
705207342ec2SBaokun Li 					"(type=%d, err=%d, ino=%lu). "
705307342ec2SBaokun Li 					"Please run e2fsck to fix.", type,
705407342ec2SBaokun Li 					err, qf_inums[type]);
70554013d47aSJan Kara 
7056f3c1c42eSBaokun Li 				ext4_quotas_off(sb, type);
70577c319d32SAditya Kali 				return err;
70587c319d32SAditya Kali 			}
70597c319d32SAditya Kali 		}
70607c319d32SAditya Kali 	}
70617c319d32SAditya Kali 	return 0;
70627c319d32SAditya Kali }
70637c319d32SAditya Kali 
7064ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
7065ca0e05e4SDmitry Monakhov {
706621f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
706721f97697SJan Kara 	handle_t *handle;
7068957153fcSJan Kara 	int err;
706921f97697SJan Kara 
707087009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
707187009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
707287009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
7073ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
7074ca0e05e4SDmitry Monakhov 
7075957153fcSJan Kara 	if (!inode || !igrab(inode))
70760b268590SAmir Goldstein 		goto out;
70770b268590SAmir Goldstein 
7078957153fcSJan Kara 	err = dquot_quota_off(sb, type);
7079964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
7080957153fcSJan Kara 		goto out_put;
7081957153fcSJan Kara 
7082957153fcSJan Kara 	inode_lock(inode);
708361a92987SJan Kara 	/*
708461a92987SJan Kara 	 * Update modification times of quota files when userspace can
708561a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
708661a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
708761a92987SJan Kara 	 */
70889924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
70894209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
70904209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
7091957153fcSJan Kara 		goto out_unlock;
70924209ae12SHarshad Shirwadkar 	}
7093957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
7094957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
7095eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
70964209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
709721f97697SJan Kara 	ext4_journal_stop(handle);
7098957153fcSJan Kara out_unlock:
7099957153fcSJan Kara 	inode_unlock(inode);
7100957153fcSJan Kara out_put:
7101964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
7102957153fcSJan Kara 	iput(inode);
7103957153fcSJan Kara 	return err;
710421f97697SJan Kara out:
7105ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
7106ca0e05e4SDmitry Monakhov }
7107ca0e05e4SDmitry Monakhov 
7108ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
7109ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
7110ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
7111ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
7112617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
7113ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
7114ac27a0ecSDave Kleikamp {
7115ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
7116725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
7117ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
7118ac27a0ecSDave Kleikamp 	int tocopy;
7119ac27a0ecSDave Kleikamp 	size_t toread;
7120ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
7121ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
7122ac27a0ecSDave Kleikamp 
7123ac27a0ecSDave Kleikamp 	if (off > i_size)
7124ac27a0ecSDave Kleikamp 		return 0;
7125ac27a0ecSDave Kleikamp 	if (off+len > i_size)
7126ac27a0ecSDave Kleikamp 		len = i_size-off;
7127ac27a0ecSDave Kleikamp 	toread = len;
7128ac27a0ecSDave Kleikamp 	while (toread > 0) {
712966267814SJiangshan Yi 		tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
71301c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
71311c215028STheodore Ts'o 		if (IS_ERR(bh))
71321c215028STheodore Ts'o 			return PTR_ERR(bh);
7133ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
7134ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
7135ac27a0ecSDave Kleikamp 		else
7136ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
7137ac27a0ecSDave Kleikamp 		brelse(bh);
7138ac27a0ecSDave Kleikamp 		offset = 0;
7139ac27a0ecSDave Kleikamp 		toread -= tocopy;
7140ac27a0ecSDave Kleikamp 		data += tocopy;
7141ac27a0ecSDave Kleikamp 		blk++;
7142ac27a0ecSDave Kleikamp 	}
7143ac27a0ecSDave Kleikamp 	return len;
7144ac27a0ecSDave Kleikamp }
7145ac27a0ecSDave Kleikamp 
7146ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
7147ac27a0ecSDave Kleikamp  * enough credits) */
7148617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
7149ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
7150ac27a0ecSDave Kleikamp {
7151ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
7152725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
71534209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
7154c5e298aeSTheodore Ts'o 	int retries = 0;
7155ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
7156ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
7157ac27a0ecSDave Kleikamp 
7158380a0091SYe Bin 	if (!handle) {
7159b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
7160b31e1552SEric Sandeen 			" cancelled because transaction is not started",
71619c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
71629c3013e9SJan Kara 		return -EIO;
71639c3013e9SJan Kara 	}
716467eeb568SDmitry Monakhov 	/*
716567eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
716667eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
716767eeb568SDmitry Monakhov 	 */
716867eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
716967eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
717067eeb568SDmitry Monakhov 			" cancelled because not block aligned",
717167eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
717267eeb568SDmitry Monakhov 		return -EIO;
717367eeb568SDmitry Monakhov 	}
717467eeb568SDmitry Monakhov 
7175c5e298aeSTheodore Ts'o 	do {
7176c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
7177c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
7178c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
717945586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
7180c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
71811c215028STheodore Ts'o 	if (IS_ERR(bh))
71821c215028STheodore Ts'o 		return PTR_ERR(bh);
7183ac27a0ecSDave Kleikamp 	if (!bh)
7184ac27a0ecSDave Kleikamp 		goto out;
71855d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
7186188c299eSJan Kara 	err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
7187ac27a0ecSDave Kleikamp 	if (err) {
7188ac27a0ecSDave Kleikamp 		brelse(bh);
71891c215028STheodore Ts'o 		return err;
7190ac27a0ecSDave Kleikamp 	}
7191ac27a0ecSDave Kleikamp 	lock_buffer(bh);
719267eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
7193ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
7194ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
71950390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
7196ac27a0ecSDave Kleikamp 	brelse(bh);
7197ac27a0ecSDave Kleikamp out:
719867eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
719967eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
7200617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
72014209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
72024209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
72034209ae12SHarshad Shirwadkar 			err = err2;
720421f97697SJan Kara 	}
72054209ae12SHarshad Shirwadkar 	return err ? err : len;
7206ac27a0ecSDave Kleikamp }
7207ac27a0ecSDave Kleikamp #endif
7208ac27a0ecSDave Kleikamp 
7209c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
721024b58424STheodore Ts'o static inline void register_as_ext2(void)
721124b58424STheodore Ts'o {
721224b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
721324b58424STheodore Ts'o 	if (err)
721424b58424STheodore Ts'o 		printk(KERN_WARNING
721524b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
721624b58424STheodore Ts'o }
721724b58424STheodore Ts'o 
721824b58424STheodore Ts'o static inline void unregister_as_ext2(void)
721924b58424STheodore Ts'o {
722024b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
722124b58424STheodore Ts'o }
72222035e776STheodore Ts'o 
72232035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
72242035e776STheodore Ts'o {
7225e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
72262035e776STheodore Ts'o 		return 0;
7227bc98a42cSDavid Howells 	if (sb_rdonly(sb))
72282035e776STheodore Ts'o 		return 1;
7229e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
72302035e776STheodore Ts'o 		return 0;
72312035e776STheodore Ts'o 	return 1;
72322035e776STheodore Ts'o }
723324b58424STheodore Ts'o #else
723424b58424STheodore Ts'o static inline void register_as_ext2(void) { }
723524b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
72362035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
723724b58424STheodore Ts'o #endif
723824b58424STheodore Ts'o 
723924b58424STheodore Ts'o static inline void register_as_ext3(void)
724024b58424STheodore Ts'o {
724124b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
724224b58424STheodore Ts'o 	if (err)
724324b58424STheodore Ts'o 		printk(KERN_WARNING
724424b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
724524b58424STheodore Ts'o }
724624b58424STheodore Ts'o 
724724b58424STheodore Ts'o static inline void unregister_as_ext3(void)
724824b58424STheodore Ts'o {
724924b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
725024b58424STheodore Ts'o }
72512035e776STheodore Ts'o 
72522035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
72532035e776STheodore Ts'o {
7254e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
72552035e776STheodore Ts'o 		return 0;
7256e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
72572035e776STheodore Ts'o 		return 0;
7258bc98a42cSDavid Howells 	if (sb_rdonly(sb))
72592035e776STheodore Ts'o 		return 1;
7260e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
72612035e776STheodore Ts'o 		return 0;
72622035e776STheodore Ts'o 	return 1;
72632035e776STheodore Ts'o }
726424b58424STheodore Ts'o 
726503010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
7266ac27a0ecSDave Kleikamp 	.owner			= THIS_MODULE,
726703010a33STheodore Ts'o 	.name			= "ext4",
7268cebe85d5SLukas Czerner 	.init_fs_context	= ext4_init_fs_context,
7269cebe85d5SLukas Czerner 	.parameters		= ext4_param_specs,
7270ac27a0ecSDave Kleikamp 	.kill_sb		= kill_block_super,
727114f3db55SChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
7272ac27a0ecSDave Kleikamp };
72737f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
7274ac27a0ecSDave Kleikamp 
7275e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
7276e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
7277e9e3bcecSEric Sandeen 
72785dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
7279ac27a0ecSDave Kleikamp {
7280e9e3bcecSEric Sandeen 	int i, err;
7281c9de560dSAlex Tomas 
7282e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
728307c0c5d8SAl Viro 	ext4_li_info = NULL;
728407c0c5d8SAl Viro 
72859a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
728612e9b892SDmitry Monakhov 	ext4_check_flag_values();
7287e9e3bcecSEric Sandeen 
7288e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
7289e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
7290e9e3bcecSEric Sandeen 
729151865fdaSZheng Liu 	err = ext4_init_es();
72926fd058f7STheodore Ts'o 	if (err)
72936fd058f7STheodore Ts'o 		return err;
729451865fdaSZheng Liu 
72951dc0aa46SEric Whitney 	err = ext4_init_pending();
72961dc0aa46SEric Whitney 	if (err)
729722cfe4b4SEric Biggers 		goto out7;
729822cfe4b4SEric Biggers 
729922cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
730022cfe4b4SEric Biggers 	if (err)
73011dc0aa46SEric Whitney 		goto out6;
73021dc0aa46SEric Whitney 
730351865fdaSZheng Liu 	err = ext4_init_pageio();
730451865fdaSZheng Liu 	if (err)
7305b5799018STheodore Ts'o 		goto out5;
730651865fdaSZheng Liu 
73075dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
7308bd2d0210STheodore Ts'o 	if (err)
7309b5799018STheodore Ts'o 		goto out4;
7310857ac889SLukas Czerner 
7311b5799018STheodore Ts'o 	err = ext4_init_sysfs();
7312dd68314cSTheodore Ts'o 	if (err)
7313b5799018STheodore Ts'o 		goto out3;
7314857ac889SLukas Czerner 
73155dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
7316ac27a0ecSDave Kleikamp 	if (err)
7317c9de560dSAlex Tomas 		goto out2;
7318ac27a0ecSDave Kleikamp 	err = init_inodecache();
7319ac27a0ecSDave Kleikamp 	if (err)
7320ac27a0ecSDave Kleikamp 		goto out1;
7321aa75f4d3SHarshad Shirwadkar 
7322aa75f4d3SHarshad Shirwadkar 	err = ext4_fc_init_dentry_cache();
7323aa75f4d3SHarshad Shirwadkar 	if (err)
7324aa75f4d3SHarshad Shirwadkar 		goto out05;
7325aa75f4d3SHarshad Shirwadkar 
732624b58424STheodore Ts'o 	register_as_ext3();
73272035e776STheodore Ts'o 	register_as_ext2();
732803010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
7329ac27a0ecSDave Kleikamp 	if (err)
7330ac27a0ecSDave Kleikamp 		goto out;
7331bfff6873SLukas Czerner 
7332ac27a0ecSDave Kleikamp 	return 0;
7333ac27a0ecSDave Kleikamp out:
733424b58424STheodore Ts'o 	unregister_as_ext2();
733524b58424STheodore Ts'o 	unregister_as_ext3();
7336ab047d51SSebastian Andrzej Siewior 	ext4_fc_destroy_dentry_cache();
7337aa75f4d3SHarshad Shirwadkar out05:
7338ac27a0ecSDave Kleikamp 	destroy_inodecache();
7339ac27a0ecSDave Kleikamp out1:
73405dabfc78STheodore Ts'o 	ext4_exit_mballoc();
73419c191f70ST Makphaibulchoke out2:
7342b5799018STheodore Ts'o 	ext4_exit_sysfs();
7343b5799018STheodore Ts'o out3:
7344dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
7345b5799018STheodore Ts'o out4:
73465dabfc78STheodore Ts'o 	ext4_exit_pageio();
7347b5799018STheodore Ts'o out5:
734822cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
73491dc0aa46SEric Whitney out6:
735022cfe4b4SEric Biggers 	ext4_exit_pending();
735122cfe4b4SEric Biggers out7:
735251865fdaSZheng Liu 	ext4_exit_es();
735351865fdaSZheng Liu 
7354ac27a0ecSDave Kleikamp 	return err;
7355ac27a0ecSDave Kleikamp }
7356ac27a0ecSDave Kleikamp 
73575dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
7358ac27a0ecSDave Kleikamp {
7359bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
736024b58424STheodore Ts'o 	unregister_as_ext2();
736124b58424STheodore Ts'o 	unregister_as_ext3();
736203010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
7363ab047d51SSebastian Andrzej Siewior 	ext4_fc_destroy_dentry_cache();
7364ac27a0ecSDave Kleikamp 	destroy_inodecache();
73655dabfc78STheodore Ts'o 	ext4_exit_mballoc();
7366b5799018STheodore Ts'o 	ext4_exit_sysfs();
73675dabfc78STheodore Ts'o 	ext4_exit_system_zone();
73685dabfc78STheodore Ts'o 	ext4_exit_pageio();
736922cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
7370dd12ed14SEric Sandeen 	ext4_exit_es();
73711dc0aa46SEric Whitney 	ext4_exit_pending();
7372ac27a0ecSDave Kleikamp }
7373ac27a0ecSDave Kleikamp 
7374ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
737583982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
7376ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
73777ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
73785dabfc78STheodore Ts'o module_init(ext4_init_fs)
73795dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
7380