xref: /linux/fs/ext4/super.c (revision 342af94ec6c02aa478fe2adcd41b950e154b03ba)
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>
427abc52c2SDan Magenheimer #include <linux/cleancache.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
44ee73f9a5SJeff Layton #include <linux/iversion.h>
45c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h>
46c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
47bfff6873SLukas Czerner #include <linux/kthread.h>
48bfff6873SLukas Czerner #include <linux/freezer.h>
49bfff6873SLukas Czerner 
503dcf5451SChristoph Hellwig #include "ext4.h"
514a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
523dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
53ac27a0ecSDave Kleikamp #include "xattr.h"
54ac27a0ecSDave Kleikamp #include "acl.h"
553661d286STheodore Ts'o #include "mballoc.h"
560c9ec4beSDarrick J. Wong #include "fsmap.h"
57ac27a0ecSDave Kleikamp 
589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
599bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
609bffad1eSTheodore Ts'o 
610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
620b75a840SLukas Czerner static struct mutex ext4_li_mtx;
63e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
649f6200bbSTheodore Ts'o 
65617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
66ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
672adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
68e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
6911215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
70617ba13bSMingming Cao 					struct ext4_super_block *es);
7111215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
72617ba13bSMingming Cao 				  struct ext4_super_block *es);
73617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
74617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
75617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
76c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79152a0836SAl Viro 		       const char *dev_name, void *data);
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);
82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
858f1f7453SEric Sandeen static void ext4_clear_request_list(void);
86c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
87c6cb7e77SEric Whitney 					    unsigned int journal_inum);
88ac27a0ecSDave Kleikamp 
89e74031fdSJan Kara /*
90e74031fdSJan Kara  * Lock ordering
91e74031fdSJan Kara  *
92e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
93e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
94e74031fdSJan Kara  *
95e74031fdSJan Kara  * page fault path:
96c1e8d7c6SMichel Lespinasse  * mmap_lock -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
97e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
98e74031fdSJan Kara  *
99e74031fdSJan Kara  * buffered write path:
100c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
101e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
102e74031fdSJan Kara  *   i_data_sem (rw)
103e74031fdSJan Kara  *
104e74031fdSJan Kara  * truncate:
1051d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock
1061d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start ->
1071d39834fSNikolay Borisov  *   i_data_sem (rw)
108e74031fdSJan Kara  *
109e74031fdSJan Kara  * direct IO:
110c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
1111d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
112e74031fdSJan Kara  *
113e74031fdSJan Kara  * writepages:
114e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
115e74031fdSJan Kara  */
116e74031fdSJan Kara 
117c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1182035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1192035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1202035e776STheodore Ts'o 	.name		= "ext2",
1212035e776STheodore Ts'o 	.mount		= ext4_mount,
1222035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1232035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1242035e776STheodore Ts'o };
1257f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
126fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1272035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1282035e776STheodore Ts'o #else
1292035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1302035e776STheodore Ts'o #endif
1312035e776STheodore Ts'o 
1322035e776STheodore Ts'o 
133ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
134ba69f9abSJan Kara 	.owner		= THIS_MODULE,
135ba69f9abSJan Kara 	.name		= "ext3",
136152a0836SAl Viro 	.mount		= ext4_mount,
137ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
138ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
139ba69f9abSJan Kara };
1407f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
141fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
142ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
143bd81d8eeSLaurent Vivier 
144fa491b14Szhangyi (F) 
145fa491b14Szhangyi (F) static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
146fa491b14Szhangyi (F) 				  bh_end_io_t *end_io)
147fa491b14Szhangyi (F) {
148fa491b14Szhangyi (F) 	/*
149fa491b14Szhangyi (F) 	 * buffer's verified bit is no longer valid after reading from
150fa491b14Szhangyi (F) 	 * disk again due to write out error, clear it to make sure we
151fa491b14Szhangyi (F) 	 * recheck the buffer contents.
152fa491b14Szhangyi (F) 	 */
153fa491b14Szhangyi (F) 	clear_buffer_verified(bh);
154fa491b14Szhangyi (F) 
155fa491b14Szhangyi (F) 	bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
156fa491b14Szhangyi (F) 	get_bh(bh);
157fa491b14Szhangyi (F) 	submit_bh(REQ_OP_READ, op_flags, bh);
158fa491b14Szhangyi (F) }
159fa491b14Szhangyi (F) 
160fa491b14Szhangyi (F) void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
161fa491b14Szhangyi (F) 			 bh_end_io_t *end_io)
162fa491b14Szhangyi (F) {
163fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
164fa491b14Szhangyi (F) 
165fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
166fa491b14Szhangyi (F) 		unlock_buffer(bh);
167fa491b14Szhangyi (F) 		return;
168fa491b14Szhangyi (F) 	}
169fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
170fa491b14Szhangyi (F) }
171fa491b14Szhangyi (F) 
172fa491b14Szhangyi (F) int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
173fa491b14Szhangyi (F) {
174fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
175fa491b14Szhangyi (F) 
176fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
177fa491b14Szhangyi (F) 		unlock_buffer(bh);
178fa491b14Szhangyi (F) 		return 0;
179fa491b14Szhangyi (F) 	}
180fa491b14Szhangyi (F) 
181fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
182fa491b14Szhangyi (F) 
183fa491b14Szhangyi (F) 	wait_on_buffer(bh);
184fa491b14Szhangyi (F) 	if (buffer_uptodate(bh))
185fa491b14Szhangyi (F) 		return 0;
186fa491b14Szhangyi (F) 	return -EIO;
187fa491b14Szhangyi (F) }
188fa491b14Szhangyi (F) 
189fa491b14Szhangyi (F) int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
190fa491b14Szhangyi (F) {
191fa491b14Szhangyi (F) 	if (trylock_buffer(bh)) {
192fa491b14Szhangyi (F) 		if (wait)
193fa491b14Szhangyi (F) 			return ext4_read_bh(bh, op_flags, NULL);
194fa491b14Szhangyi (F) 		ext4_read_bh_nowait(bh, op_flags, NULL);
195fa491b14Szhangyi (F) 		return 0;
196fa491b14Szhangyi (F) 	}
197fa491b14Szhangyi (F) 	if (wait) {
198fa491b14Szhangyi (F) 		wait_on_buffer(bh);
199fa491b14Szhangyi (F) 		if (buffer_uptodate(bh))
200fa491b14Szhangyi (F) 			return 0;
201fa491b14Szhangyi (F) 		return -EIO;
202fa491b14Szhangyi (F) 	}
203fa491b14Szhangyi (F) 	return 0;
204fa491b14Szhangyi (F) }
205fa491b14Szhangyi (F) 
206fb265c9cSTheodore Ts'o /*
2078394a6abSzhangyi (F)  * This works like __bread_gfp() except it uses ERR_PTR for error
208fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
209fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
210fb265c9cSTheodore Ts'o  * return.
211fb265c9cSTheodore Ts'o  */
2128394a6abSzhangyi (F) static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
2138394a6abSzhangyi (F) 					       sector_t block, int op_flags,
2148394a6abSzhangyi (F) 					       gfp_t gfp)
215fb265c9cSTheodore Ts'o {
2162d069c08Szhangyi (F) 	struct buffer_head *bh;
2172d069c08Szhangyi (F) 	int ret;
218fb265c9cSTheodore Ts'o 
2198394a6abSzhangyi (F) 	bh = sb_getblk_gfp(sb, block, gfp);
220fb265c9cSTheodore Ts'o 	if (bh == NULL)
221fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
222cf2834a5STheodore Ts'o 	if (ext4_buffer_uptodate(bh))
223fb265c9cSTheodore Ts'o 		return bh;
2242d069c08Szhangyi (F) 
2252d069c08Szhangyi (F) 	ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
2262d069c08Szhangyi (F) 	if (ret) {
227fb265c9cSTheodore Ts'o 		put_bh(bh);
2282d069c08Szhangyi (F) 		return ERR_PTR(ret);
2292d069c08Szhangyi (F) 	}
2302d069c08Szhangyi (F) 	return bh;
231fb265c9cSTheodore Ts'o }
232fb265c9cSTheodore Ts'o 
2338394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
2348394a6abSzhangyi (F) 				   int op_flags)
2358394a6abSzhangyi (F) {
2368394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
2378394a6abSzhangyi (F) }
2388394a6abSzhangyi (F) 
2398394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
2408394a6abSzhangyi (F) 					    sector_t block)
2418394a6abSzhangyi (F) {
2428394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, 0, 0);
2438394a6abSzhangyi (F) }
2448394a6abSzhangyi (F) 
2455df1d412Szhangyi (F) void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
2465df1d412Szhangyi (F) {
2475df1d412Szhangyi (F) 	struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
2485df1d412Szhangyi (F) 
2495df1d412Szhangyi (F) 	if (likely(bh)) {
2505df1d412Szhangyi (F) 		ext4_read_bh_lock(bh, REQ_RAHEAD, false);
2515df1d412Szhangyi (F) 		brelse(bh);
2525df1d412Szhangyi (F) 	}
2535df1d412Szhangyi (F) }
2545df1d412Szhangyi (F) 
255d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
256d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
257d25425f8SDarrick J. Wong {
258e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
259d25425f8SDarrick J. Wong 		return 1;
260d25425f8SDarrick J. Wong 
261d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
262d25425f8SDarrick J. Wong }
263d25425f8SDarrick J. Wong 
264a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
265a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
266a9c47317SDarrick J. Wong {
267a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
268a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
269a9c47317SDarrick J. Wong 	__u32 csum;
270a9c47317SDarrick J. Wong 
271a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
272a9c47317SDarrick J. Wong 
273a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
274a9c47317SDarrick J. Wong }
275a9c47317SDarrick J. Wong 
276c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb,
277a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
278a9c47317SDarrick J. Wong {
2799aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
280a9c47317SDarrick J. Wong 		return 1;
281a9c47317SDarrick J. Wong 
282a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
283a9c47317SDarrick J. Wong }
284a9c47317SDarrick J. Wong 
28506db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
286a9c47317SDarrick J. Wong {
28706db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
28806db49e6STheodore Ts'o 
2899aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
290a9c47317SDarrick J. Wong 		return;
291a9c47317SDarrick J. Wong 
292acaa5326SConstantine Sapuntzakis 	/*
293acaa5326SConstantine Sapuntzakis 	 * Locking the superblock prevents the scenario
294acaa5326SConstantine Sapuntzakis 	 * where:
295acaa5326SConstantine Sapuntzakis 	 *  1) a first thread pauses during checksum calculation.
296acaa5326SConstantine Sapuntzakis 	 *  2) a second thread updates the superblock, recalculates
297acaa5326SConstantine Sapuntzakis 	 *     the checksum, and updates s_checksum
298acaa5326SConstantine Sapuntzakis 	 *  3) the first thread resumes and finishes its checksum calculation
299acaa5326SConstantine Sapuntzakis 	 *     and updates s_checksum with a potentially stale or torn value.
300acaa5326SConstantine Sapuntzakis 	 */
301acaa5326SConstantine Sapuntzakis 	lock_buffer(EXT4_SB(sb)->s_sbh);
302a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
303acaa5326SConstantine Sapuntzakis 	unlock_buffer(EXT4_SB(sb)->s_sbh);
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 
4186a0678a7SArnd Bergmann static void __ext4_update_tstamp(__le32 *lo, __u8 *hi)
4196a0678a7SArnd Bergmann {
4206a0678a7SArnd Bergmann 	time64_t now = ktime_get_real_seconds();
4216a0678a7SArnd Bergmann 
4226a0678a7SArnd Bergmann 	now = clamp_val(now, 0, (1ull << 40) - 1);
4236a0678a7SArnd Bergmann 
4246a0678a7SArnd Bergmann 	*lo = cpu_to_le32(lower_32_bits(now));
4256a0678a7SArnd Bergmann 	*hi = upper_32_bits(now);
4266a0678a7SArnd Bergmann }
4276a0678a7SArnd Bergmann 
4286a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
4296a0678a7SArnd Bergmann {
4306a0678a7SArnd Bergmann 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
4316a0678a7SArnd Bergmann }
4326a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \
4336a0678a7SArnd Bergmann 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
4346a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \
4356a0678a7SArnd Bergmann 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
436d3d1faf6SCurt Wohlgemuth 
43754d3adbcSTheodore Ts'o static void __save_error_info(struct super_block *sb, int error,
43854d3adbcSTheodore Ts'o 			      __u32 ino, __u64 block,
43954d3adbcSTheodore Ts'o 			      const char *func, unsigned int line)
4401c13d5c0STheodore Ts'o {
4411c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
44254d3adbcSTheodore Ts'o 	int err;
4431c13d5c0STheodore Ts'o 
4441c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4451b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
4461b46617bSTheodore Ts'o 		return;
4471c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4486a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_last_error_time);
4491c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
4501c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
45154d3adbcSTheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
45254d3adbcSTheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
45354d3adbcSTheodore Ts'o 	switch (error) {
45454d3adbcSTheodore Ts'o 	case EIO:
45554d3adbcSTheodore Ts'o 		err = EXT4_ERR_EIO;
45654d3adbcSTheodore Ts'o 		break;
45754d3adbcSTheodore Ts'o 	case ENOMEM:
45854d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOMEM;
45954d3adbcSTheodore Ts'o 		break;
46054d3adbcSTheodore Ts'o 	case EFSBADCRC:
46154d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFSBADCRC;
46254d3adbcSTheodore Ts'o 		break;
46354d3adbcSTheodore Ts'o 	case 0:
46454d3adbcSTheodore Ts'o 	case EFSCORRUPTED:
46554d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFSCORRUPTED;
46654d3adbcSTheodore Ts'o 		break;
46754d3adbcSTheodore Ts'o 	case ENOSPC:
46854d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOSPC;
46954d3adbcSTheodore Ts'o 		break;
47054d3adbcSTheodore Ts'o 	case ENOKEY:
47154d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOKEY;
47254d3adbcSTheodore Ts'o 		break;
47354d3adbcSTheodore Ts'o 	case EROFS:
47454d3adbcSTheodore Ts'o 		err = EXT4_ERR_EROFS;
47554d3adbcSTheodore Ts'o 		break;
47654d3adbcSTheodore Ts'o 	case EFBIG:
47754d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFBIG;
47854d3adbcSTheodore Ts'o 		break;
47954d3adbcSTheodore Ts'o 	case EEXIST:
48054d3adbcSTheodore Ts'o 		err = EXT4_ERR_EEXIST;
48154d3adbcSTheodore Ts'o 		break;
48254d3adbcSTheodore Ts'o 	case ERANGE:
48354d3adbcSTheodore Ts'o 		err = EXT4_ERR_ERANGE;
48454d3adbcSTheodore Ts'o 		break;
48554d3adbcSTheodore Ts'o 	case EOVERFLOW:
48654d3adbcSTheodore Ts'o 		err = EXT4_ERR_EOVERFLOW;
48754d3adbcSTheodore Ts'o 		break;
48854d3adbcSTheodore Ts'o 	case EBUSY:
48954d3adbcSTheodore Ts'o 		err = EXT4_ERR_EBUSY;
49054d3adbcSTheodore Ts'o 		break;
49154d3adbcSTheodore Ts'o 	case ENOTDIR:
49254d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOTDIR;
49354d3adbcSTheodore Ts'o 		break;
49454d3adbcSTheodore Ts'o 	case ENOTEMPTY:
49554d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOTEMPTY;
49654d3adbcSTheodore Ts'o 		break;
49754d3adbcSTheodore Ts'o 	case ESHUTDOWN:
49854d3adbcSTheodore Ts'o 		err = EXT4_ERR_ESHUTDOWN;
49954d3adbcSTheodore Ts'o 		break;
50054d3adbcSTheodore Ts'o 	case EFAULT:
50154d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFAULT;
50254d3adbcSTheodore Ts'o 		break;
50354d3adbcSTheodore Ts'o 	default:
50454d3adbcSTheodore Ts'o 		err = EXT4_ERR_UNKNOWN;
50554d3adbcSTheodore Ts'o 	}
50654d3adbcSTheodore Ts'o 	es->s_last_error_errcode = err;
5071c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
5081c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
5096a0678a7SArnd Bergmann 		es->s_first_error_time_hi = es->s_last_error_time_hi;
5101c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
5111c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
5121c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
5131c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
5141c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
515878520acSTheodore Ts'o 		es->s_first_error_errcode = es->s_last_error_errcode;
5161c13d5c0STheodore Ts'o 	}
51766e61a9eSTheodore Ts'o 	/*
51866e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
51966e61a9eSTheodore Ts'o 	 * started already
52066e61a9eSTheodore Ts'o 	 */
52166e61a9eSTheodore Ts'o 	if (!es->s_error_count)
52266e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
523ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
5241c13d5c0STheodore Ts'o }
5251c13d5c0STheodore Ts'o 
52654d3adbcSTheodore Ts'o static void save_error_info(struct super_block *sb, int error,
52754d3adbcSTheodore Ts'o 			    __u32 ino, __u64 block,
52854d3adbcSTheodore Ts'o 			    const char *func, unsigned int line)
5291c13d5c0STheodore Ts'o {
53054d3adbcSTheodore Ts'o 	__save_error_info(sb, error, ino, block, func, line);
531c96e2b85SEric Sandeen 	if (!bdev_read_only(sb->s_bdev))
5321c13d5c0STheodore Ts'o 		ext4_commit_super(sb, 1);
5331c13d5c0STheodore Ts'o }
5341c13d5c0STheodore Ts'o 
535bdfe0cbdSTheodore Ts'o /*
536bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
537bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
538bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
539bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
540bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
541bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
542bdfe0cbdSTheodore Ts'o  */
543bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
544bdfe0cbdSTheodore Ts'o {
545bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
546bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
547bdfe0cbdSTheodore Ts'o 
548bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
549bdfe0cbdSTheodore Ts'o }
550bdfe0cbdSTheodore Ts'o 
55118aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
55218aadd47SBobi Jam {
55318aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
55418aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
55518aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
5565d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
55718aadd47SBobi Jam 
5585d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
559a0154344SDaeho Jeong 
560a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
561a0154344SDaeho Jeong 
56218aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
5635d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
5645d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
5655d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
56618aadd47SBobi Jam 		list_del_init(&jce->jce_list);
56718aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
56818aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
56918aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
57018aadd47SBobi Jam 	}
57118aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
57218aadd47SBobi Jam }
5731c13d5c0STheodore Ts'o 
5741dc1097fSJan Kara static bool system_going_down(void)
5751dc1097fSJan Kara {
5761dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
5771dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
5781dc1097fSJan Kara }
5791dc1097fSJan Kara 
580ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
581ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
582ac27a0ecSDave Kleikamp  *
583ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
584617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
585ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
586ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
587ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
588ac27a0ecSDave Kleikamp  * write out the superblock safely.
589ac27a0ecSDave Kleikamp  *
590dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
591d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
592ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
593ac27a0ecSDave Kleikamp  */
594ac27a0ecSDave Kleikamp 
595617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
596ac27a0ecSDave Kleikamp {
597327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
598327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
599327eaf73STheodore Ts'o 
600bc98a42cSDavid Howells 	if (sb_rdonly(sb))
601ac27a0ecSDave Kleikamp 		return;
602ac27a0ecSDave Kleikamp 
603ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
604617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
605ac27a0ecSDave Kleikamp 
6064ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
607ac27a0ecSDave Kleikamp 		if (journal)
608dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
609ac27a0ecSDave Kleikamp 	}
6101dc1097fSJan Kara 	/*
6111dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
6121dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
6131dc1097fSJan Kara 	 * disabled.
6141dc1097fSJan Kara 	 */
6151dc1097fSJan Kara 	if (test_opt(sb, ERRORS_RO) || system_going_down()) {
616b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
6174418e141SDmitry Monakhov 		/*
6184418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
6194418e141SDmitry Monakhov 		 * before ->s_flags update
6204418e141SDmitry Monakhov 		 */
6214418e141SDmitry Monakhov 		smp_wmb();
6221751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
6231dc1097fSJan Kara 	} else if (test_opt(sb, ERRORS_PANIC)) {
624617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
625ac27a0ecSDave Kleikamp 			sb->s_id);
626ac27a0ecSDave Kleikamp 	}
6274327ba52SDaeho Jeong }
628ac27a0ecSDave Kleikamp 
629efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
630efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
631efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
632efbed4dcSTheodore Ts'o 
63312062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
63454d3adbcSTheodore Ts'o 		  unsigned int line, int error, __u64 block,
63554d3adbcSTheodore Ts'o 		  const char *fmt, ...)
636ac27a0ecSDave Kleikamp {
6370ff2ea7dSJoe Perches 	struct va_format vaf;
638ac27a0ecSDave Kleikamp 	va_list args;
639ac27a0ecSDave Kleikamp 
6400db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6410db1ff22STheodore Ts'o 		return;
6420db1ff22STheodore Ts'o 
643ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
644efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
645ac27a0ecSDave Kleikamp 		va_start(args, fmt);
6460ff2ea7dSJoe Perches 		vaf.fmt = fmt;
6470ff2ea7dSJoe Perches 		vaf.va = &args;
648efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
649efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
6500ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
651ac27a0ecSDave Kleikamp 		va_end(args);
652efbed4dcSTheodore Ts'o 	}
65354d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, block, function, line);
654617ba13bSMingming Cao 	ext4_handle_error(sb);
655ac27a0ecSDave Kleikamp }
656ac27a0ecSDave Kleikamp 
657e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
65854d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
659273df556SFrank Mayhar 			const char *fmt, ...)
660273df556SFrank Mayhar {
661273df556SFrank Mayhar 	va_list args;
662f7c21177STheodore Ts'o 	struct va_format vaf;
663273df556SFrank Mayhar 
6640db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
6650db1ff22STheodore Ts'o 		return;
6660db1ff22STheodore Ts'o 
667ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
668efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
669273df556SFrank Mayhar 		va_start(args, fmt);
670f7c21177STheodore Ts'o 		vaf.fmt = fmt;
671f7c21177STheodore Ts'o 		vaf.va = &args;
672c398eda0STheodore Ts'o 		if (block)
673d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
674d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
675d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
676d9ee81daSJoe Perches 			       block, current->comm, &vaf);
677d9ee81daSJoe Perches 		else
678d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
679d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
680d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
681d9ee81daSJoe Perches 			       current->comm, &vaf);
682273df556SFrank Mayhar 		va_end(args);
683efbed4dcSTheodore Ts'o 	}
68454d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, error, inode->i_ino, block,
68554d3adbcSTheodore Ts'o 			function, line);
686273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
687273df556SFrank Mayhar }
688273df556SFrank Mayhar 
689e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
690f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
691f7c21177STheodore Ts'o 		       const char *fmt, ...)
692273df556SFrank Mayhar {
693273df556SFrank Mayhar 	va_list args;
694f7c21177STheodore Ts'o 	struct va_format vaf;
695496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
696273df556SFrank Mayhar 	char pathname[80], *path;
697273df556SFrank Mayhar 
6980db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
6990db1ff22STheodore Ts'o 		return;
7000db1ff22STheodore Ts'o 
701ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
702efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
7039bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
704f9a62d09SDan Carpenter 		if (IS_ERR(path))
705273df556SFrank Mayhar 			path = "(unknown)";
706f7c21177STheodore Ts'o 		va_start(args, fmt);
707f7c21177STheodore Ts'o 		vaf.fmt = fmt;
708f7c21177STheodore Ts'o 		vaf.va = &args;
709d9ee81daSJoe Perches 		if (block)
710d9ee81daSJoe Perches 			printk(KERN_CRIT
711d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
712d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
713d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
714d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
715d9ee81daSJoe Perches 		else
716d9ee81daSJoe Perches 			printk(KERN_CRIT
717d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
718d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
719d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
720d9ee81daSJoe Perches 			       current->comm, path, &vaf);
721273df556SFrank Mayhar 		va_end(args);
722efbed4dcSTheodore Ts'o 	}
72354d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block,
72454d3adbcSTheodore Ts'o 			function, line);
725273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
726273df556SFrank Mayhar }
727273df556SFrank Mayhar 
728722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
729ac27a0ecSDave Kleikamp 			      char nbuf[16])
730ac27a0ecSDave Kleikamp {
731ac27a0ecSDave Kleikamp 	char *errstr = NULL;
732ac27a0ecSDave Kleikamp 
733ac27a0ecSDave Kleikamp 	switch (errno) {
7346a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
7356a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
7366a797d27SDarrick J. Wong 		break;
7376a797d27SDarrick J. Wong 	case -EFSBADCRC:
7386a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
7396a797d27SDarrick J. Wong 		break;
740ac27a0ecSDave Kleikamp 	case -EIO:
741ac27a0ecSDave Kleikamp 		errstr = "IO failure";
742ac27a0ecSDave Kleikamp 		break;
743ac27a0ecSDave Kleikamp 	case -ENOMEM:
744ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
745ac27a0ecSDave Kleikamp 		break;
746ac27a0ecSDave Kleikamp 	case -EROFS:
74778f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
74878f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
749ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
750ac27a0ecSDave Kleikamp 		else
751ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
752ac27a0ecSDave Kleikamp 		break;
753ac27a0ecSDave Kleikamp 	default:
754ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
755ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
756ac27a0ecSDave Kleikamp 		 * NULL. */
757ac27a0ecSDave Kleikamp 		if (nbuf) {
758ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
759ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
760ac27a0ecSDave Kleikamp 				errstr = nbuf;
761ac27a0ecSDave Kleikamp 		}
762ac27a0ecSDave Kleikamp 		break;
763ac27a0ecSDave Kleikamp 	}
764ac27a0ecSDave Kleikamp 
765ac27a0ecSDave Kleikamp 	return errstr;
766ac27a0ecSDave Kleikamp }
767ac27a0ecSDave Kleikamp 
768617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
769ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
770ac27a0ecSDave Kleikamp 
771c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
772c398eda0STheodore Ts'o 		      unsigned int line, int errno)
773ac27a0ecSDave Kleikamp {
774ac27a0ecSDave Kleikamp 	char nbuf[16];
775ac27a0ecSDave Kleikamp 	const char *errstr;
776ac27a0ecSDave Kleikamp 
7770db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7780db1ff22STheodore Ts'o 		return;
7790db1ff22STheodore Ts'o 
780ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
781ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
782ac27a0ecSDave Kleikamp 	 * an error. */
783bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
784ac27a0ecSDave Kleikamp 		return;
785ac27a0ecSDave Kleikamp 
786efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
787617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
788c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
789c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
790efbed4dcSTheodore Ts'o 	}
791ac27a0ecSDave Kleikamp 
79254d3adbcSTheodore Ts'o 	save_error_info(sb, -errno, 0, 0, function, line);
793617ba13bSMingming Cao 	ext4_handle_error(sb);
794ac27a0ecSDave Kleikamp }
795ac27a0ecSDave Kleikamp 
796ac27a0ecSDave Kleikamp /*
797617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
798ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
799ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
800ac27a0ecSDave Kleikamp  *
801ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
802ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
803ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
804ac27a0ecSDave Kleikamp  */
805ac27a0ecSDave Kleikamp 
806c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
80754d3adbcSTheodore Ts'o 		  unsigned int line, int error, const char *fmt, ...)
808ac27a0ecSDave Kleikamp {
809651e1c3bSJoe Perches 	struct va_format vaf;
810ac27a0ecSDave Kleikamp 	va_list args;
811ac27a0ecSDave Kleikamp 
8120db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8130db1ff22STheodore Ts'o 		return;
8140db1ff22STheodore Ts'o 
81554d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, 0, function, line);
816ac27a0ecSDave Kleikamp 	va_start(args, fmt);
817651e1c3bSJoe Perches 	vaf.fmt = fmt;
818651e1c3bSJoe Perches 	vaf.va = &args;
819651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
820651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
821ac27a0ecSDave Kleikamp 	va_end(args);
822ac27a0ecSDave Kleikamp 
823bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
8244ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
8257b97d868Szhangyi (F) 		if (EXT4_SB(sb)->s_journal)
8267b97d868Szhangyi (F) 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
8277b97d868Szhangyi (F) 
8287b97d868Szhangyi (F) 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
8294418e141SDmitry Monakhov 		/*
8304418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
8314418e141SDmitry Monakhov 		 * before ->s_flags update
8324418e141SDmitry Monakhov 		 */
8334418e141SDmitry Monakhov 		smp_wmb();
8341751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
8351c13d5c0STheodore Ts'o 	}
8367b97d868Szhangyi (F) 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down())
8371c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
838ac27a0ecSDave Kleikamp }
839ac27a0ecSDave Kleikamp 
840e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
841e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
842b31e1552SEric Sandeen {
8430ff2ea7dSJoe Perches 	struct va_format vaf;
844b31e1552SEric Sandeen 	va_list args;
845b31e1552SEric Sandeen 
8461cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_msg_count);
847efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
848efbed4dcSTheodore Ts'o 		return;
849efbed4dcSTheodore Ts'o 
850b31e1552SEric Sandeen 	va_start(args, fmt);
8510ff2ea7dSJoe Perches 	vaf.fmt = fmt;
8520ff2ea7dSJoe Perches 	vaf.va = &args;
8530ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
854b31e1552SEric Sandeen 	va_end(args);
855b31e1552SEric Sandeen }
856b31e1552SEric Sandeen 
8571cf006edSDmitry Monakhov static int ext4_warning_ratelimit(struct super_block *sb)
8581cf006edSDmitry Monakhov {
8591cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_warning_count);
8601cf006edSDmitry Monakhov 	return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
8611cf006edSDmitry Monakhov 			    "EXT4-fs warning");
8621cf006edSDmitry Monakhov }
863b03a2f7eSAndreas Dilger 
86412062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
865c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
866ac27a0ecSDave Kleikamp {
8670ff2ea7dSJoe Perches 	struct va_format vaf;
868ac27a0ecSDave Kleikamp 	va_list args;
869ac27a0ecSDave Kleikamp 
870b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
871efbed4dcSTheodore Ts'o 		return;
872efbed4dcSTheodore Ts'o 
873ac27a0ecSDave Kleikamp 	va_start(args, fmt);
8740ff2ea7dSJoe Perches 	vaf.fmt = fmt;
8750ff2ea7dSJoe Perches 	vaf.va = &args;
8760ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
8770ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
878ac27a0ecSDave Kleikamp 	va_end(args);
879ac27a0ecSDave Kleikamp }
880ac27a0ecSDave Kleikamp 
881b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
882b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
883b03a2f7eSAndreas Dilger {
884b03a2f7eSAndreas Dilger 	struct va_format vaf;
885b03a2f7eSAndreas Dilger 	va_list args;
886b03a2f7eSAndreas Dilger 
887b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
888b03a2f7eSAndreas Dilger 		return;
889b03a2f7eSAndreas Dilger 
890b03a2f7eSAndreas Dilger 	va_start(args, fmt);
891b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
892b03a2f7eSAndreas Dilger 	vaf.va = &args;
893b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
894b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
895b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
896b03a2f7eSAndreas Dilger 	va_end(args);
897b03a2f7eSAndreas Dilger }
898b03a2f7eSAndreas Dilger 
899e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
900e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
901e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
902e29136f8STheodore Ts'o 			     const char *fmt, ...)
9035d1b1b3fSAneesh Kumar K.V __releases(bitlock)
9045d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
9055d1b1b3fSAneesh Kumar K.V {
9060ff2ea7dSJoe Perches 	struct va_format vaf;
9075d1b1b3fSAneesh Kumar K.V 	va_list args;
9085d1b1b3fSAneesh Kumar K.V 
9090db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
9100db1ff22STheodore Ts'o 		return;
9110db1ff22STheodore Ts'o 
912ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
91354d3adbcSTheodore Ts'o 	__save_error_info(sb, EFSCORRUPTED, ino, block, function, line);
9140ff2ea7dSJoe Perches 
915efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
9165d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
9170ff2ea7dSJoe Perches 		vaf.fmt = fmt;
9180ff2ea7dSJoe Perches 		vaf.va = &args;
91921149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
920e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
921e29136f8STheodore Ts'o 		if (ino)
9220ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
923e29136f8STheodore Ts'o 		if (block)
924efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
925efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
9260ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
9275d1b1b3fSAneesh Kumar K.V 		va_end(args);
928efbed4dcSTheodore Ts'o 	}
9295d1b1b3fSAneesh Kumar K.V 
930327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
931327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
932327eaf73STheodore Ts'o 
9335d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
934e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
9355d1b1b3fSAneesh Kumar K.V 		return;
9365d1b1b3fSAneesh Kumar K.V 	}
9371c13d5c0STheodore Ts'o 
9385d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
93906f29cc8SZhouyi Zhou 	ext4_commit_super(sb, 1);
9405d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
9415d1b1b3fSAneesh Kumar K.V 	/*
9425d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
9435d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
9445d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
9455d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
9465d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
94725985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
9485d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
9495d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
9505d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
9515d1b1b3fSAneesh Kumar K.V 	 */
9525d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
9535d1b1b3fSAneesh Kumar K.V 	return;
9545d1b1b3fSAneesh Kumar K.V }
9555d1b1b3fSAneesh Kumar K.V 
956db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
957db79e6d1SWang Shilong 				     ext4_group_t group,
958db79e6d1SWang Shilong 				     unsigned int flags)
959db79e6d1SWang Shilong {
960db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
961db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
962db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
9639af0b3d1SWang Shilong 	int ret;
964db79e6d1SWang Shilong 
9659af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
9669af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
9679af0b3d1SWang Shilong 					    &grp->bb_state);
9689af0b3d1SWang Shilong 		if (!ret)
969db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
970db79e6d1SWang Shilong 					   grp->bb_free);
971db79e6d1SWang Shilong 	}
972db79e6d1SWang Shilong 
9739af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
9749af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
9759af0b3d1SWang Shilong 					    &grp->bb_state);
9769af0b3d1SWang Shilong 		if (!ret && gdp) {
977db79e6d1SWang Shilong 			int count;
978db79e6d1SWang Shilong 
979db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
980db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
981db79e6d1SWang Shilong 					   count);
982db79e6d1SWang Shilong 		}
983db79e6d1SWang Shilong 	}
984db79e6d1SWang Shilong }
985db79e6d1SWang Shilong 
986617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
987ac27a0ecSDave Kleikamp {
988617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
989ac27a0ecSDave Kleikamp 
990617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
991ac27a0ecSDave Kleikamp 		return;
992ac27a0ecSDave Kleikamp 
99312062dddSEric Sandeen 	ext4_warning(sb,
994ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
995ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
996617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
997ac27a0ecSDave Kleikamp 
998617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
999617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1000617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1001ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
1002ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
1003ac27a0ecSDave Kleikamp 
1004ac27a0ecSDave Kleikamp 	/*
1005ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
1006ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
1007ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
1008ac27a0ecSDave Kleikamp 	 */
1009ac27a0ecSDave Kleikamp }
1010ac27a0ecSDave Kleikamp 
1011ac27a0ecSDave Kleikamp /*
1012ac27a0ecSDave Kleikamp  * Open the external journal device
1013ac27a0ecSDave Kleikamp  */
1014b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1015ac27a0ecSDave Kleikamp {
1016ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1017ac27a0ecSDave Kleikamp 
1018d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1019ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
1020ac27a0ecSDave Kleikamp 		goto fail;
1021ac27a0ecSDave Kleikamp 	return bdev;
1022ac27a0ecSDave Kleikamp 
1023ac27a0ecSDave Kleikamp fail:
1024ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
1025ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
1026ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1027ac27a0ecSDave Kleikamp 	return NULL;
1028ac27a0ecSDave Kleikamp }
1029ac27a0ecSDave Kleikamp 
1030ac27a0ecSDave Kleikamp /*
1031ac27a0ecSDave Kleikamp  * Release the journal device
1032ac27a0ecSDave Kleikamp  */
10334385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
1034ac27a0ecSDave Kleikamp {
10354385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1036ac27a0ecSDave Kleikamp }
1037ac27a0ecSDave Kleikamp 
10384385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1039ac27a0ecSDave Kleikamp {
1040ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1041ee7ed3aaSChunguang Xu 	bdev = sbi->s_journal_bdev;
1042ac27a0ecSDave Kleikamp 	if (bdev) {
10434385bab1SAl Viro 		ext4_blkdev_put(bdev);
1044ee7ed3aaSChunguang Xu 		sbi->s_journal_bdev = NULL;
1045ac27a0ecSDave Kleikamp 	}
1046ac27a0ecSDave Kleikamp }
1047ac27a0ecSDave Kleikamp 
1048ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
1049ac27a0ecSDave Kleikamp {
1050617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1051ac27a0ecSDave Kleikamp }
1052ac27a0ecSDave Kleikamp 
1053617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1054ac27a0ecSDave Kleikamp {
1055ac27a0ecSDave Kleikamp 	struct list_head *l;
1056ac27a0ecSDave Kleikamp 
1057b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1058ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
1059ac27a0ecSDave Kleikamp 
1060ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
1061ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
1062ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
1063ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
1064ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1065ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
1066ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
1067ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1068ac27a0ecSDave Kleikamp 	}
1069ac27a0ecSDave Kleikamp }
1070ac27a0ecSDave Kleikamp 
1071957153fcSJan Kara #ifdef CONFIG_QUOTA
1072957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
1073957153fcSJan Kara 
1074957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1075957153fcSJan Kara {
1076957153fcSJan Kara 	int type;
1077957153fcSJan Kara 
1078957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
1079957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
1080957153fcSJan Kara 		ext4_quota_off(sb, type);
1081957153fcSJan Kara }
108233458eabSTheodore Ts'o 
108333458eabSTheodore Ts'o /*
108433458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
108533458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
108633458eabSTheodore Ts'o  */
108733458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
108833458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
108933458eabSTheodore Ts'o 				int type)
109033458eabSTheodore Ts'o {
109133458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
109233458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
109333458eabSTheodore Ts'o }
1094957153fcSJan Kara #else
1095957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1096957153fcSJan Kara {
1097957153fcSJan Kara }
1098957153fcSJan Kara #endif
1099957153fcSJan Kara 
1100617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1101ac27a0ecSDave Kleikamp {
1102617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1103617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
11041d0c3924STheodore Ts'o 	struct buffer_head **group_desc;
11057c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
110697abd7d4STheodore Ts'o 	int aborted = 0;
1107ef2cabf7SHidehiro Kawai 	int i, err;
1108ac27a0ecSDave Kleikamp 
1109857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1110957153fcSJan Kara 	ext4_quota_off_umount(sb);
1111e0ccfd95SChristoph Hellwig 
11122e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
11134c0425ffSMingming Cao 
11145e47868fSRitesh Harjani 	/*
11155e47868fSRitesh Harjani 	 * Unregister sysfs before destroying jbd2 journal.
11165e47868fSRitesh Harjani 	 * Since we could still access attr_journal_task attribute via sysfs
11175e47868fSRitesh Harjani 	 * path which could have sbi->s_journal->j_task as NULL
11185e47868fSRitesh Harjani 	 */
11195e47868fSRitesh Harjani 	ext4_unregister_sysfs(sb);
11205e47868fSRitesh Harjani 
11210390131bSFrank Mayhar 	if (sbi->s_journal) {
112297abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1123ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
112447b4a50bSJan Kara 		sbi->s_journal = NULL;
1125878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
112654d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
11270390131bSFrank Mayhar 		}
1128878520acSTheodore Ts'o 	}
1129d4edac31SJosef Bacik 
1130d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
11319105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1132d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1133d4edac31SJosef Bacik 	ext4_mb_release(sb);
1134d4edac31SJosef Bacik 	ext4_ext_release(sb);
1135d4edac31SJosef Bacik 
1136bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1137e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
1138ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1139ac27a0ecSDave Kleikamp 	}
1140bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
1141a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
1142a8e25a83SArtem Bityutskiy 
11431d0c3924STheodore Ts'o 	rcu_read_lock();
11441d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
1145ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
11461d0c3924STheodore Ts'o 		brelse(group_desc[i]);
11471d0c3924STheodore Ts'o 	kvfree(group_desc);
11487c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
11497c990728SSuraj Jitindar Singh 	if (flex_groups) {
11507c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
11517c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
11527c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
11537c990728SSuraj Jitindar Singh 	}
11541d0c3924STheodore Ts'o 	rcu_read_unlock();
115557042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1156ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1157ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
115857042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1159bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1160ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1161a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
116233458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1163ac27a0ecSDave Kleikamp #endif
1164ac27a0ecSDave Kleikamp 
1165ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1166ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1167ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1168ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1169ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1170ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1171ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
1172ac27a0ecSDave Kleikamp 
117389d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1174f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1175ee7ed3aaSChunguang Xu 	if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
1176ac27a0ecSDave Kleikamp 		/*
1177ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1178ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1179ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1180ac27a0ecSDave Kleikamp 		 */
1181ee7ed3aaSChunguang Xu 		sync_blockdev(sbi->s_journal_bdev);
1182ee7ed3aaSChunguang Xu 		invalidate_bdev(sbi->s_journal_bdev);
1183617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1184ac27a0ecSDave Kleikamp 	}
118550c15df6SChengguang Xu 
1186dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1187dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
118850c15df6SChengguang Xu 
118947387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
119047387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
119150c15df6SChengguang Xu 
1192c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1193c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
11949060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1195ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
11963197ebdbSTheodore Ts'o 	/*
11973197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
11983197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
11993197ebdbSTheodore Ts'o 	 */
12003197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
12013197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
12020441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
12030441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1204705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
12055e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1206ed318a6cSEric Biggers 	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
1207c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1208c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
1209c83ad55eSGabriel Krisman Bertazi #endif
1210ac27a0ecSDave Kleikamp 	kfree(sbi);
1211ac27a0ecSDave Kleikamp }
1212ac27a0ecSDave Kleikamp 
1213e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1214ac27a0ecSDave Kleikamp 
1215ac27a0ecSDave Kleikamp /*
1216ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1217ac27a0ecSDave Kleikamp  */
1218617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1219ac27a0ecSDave Kleikamp {
1220617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1221ac27a0ecSDave Kleikamp 
1222e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1223ac27a0ecSDave Kleikamp 	if (!ei)
1224ac27a0ecSDave Kleikamp 		return NULL;
12250b8e58a1SAndreas Dilger 
1226ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1227202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1228c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
122927bc446eSbrookxu 	atomic_set(&ei->i_prealloc_active, 0);
1230c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
12319a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
12329a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1233edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1234eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1235edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1236dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1237d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1238d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
12391dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1240a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1241a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
124296c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1243a9e7f447SDmitry Monakhov #endif
12448aefcd55STheodore Ts'o 	ei->jinode = NULL;
12452e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1246744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1247b436b9beSJan Kara 	ei->i_sync_tid = 0;
1248b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1249e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
12502e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1251ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1252ac27a0ecSDave Kleikamp }
1253ac27a0ecSDave Kleikamp 
12547ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
12557ff9c073STheodore Ts'o {
12567ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
12577ff9c073STheodore Ts'o 
125829b3692eSEric Biggers 	if (!drop)
125929b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
126029b3692eSEric Biggers 
12617ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
12627ff9c073STheodore Ts'o 	return drop;
12637ff9c073STheodore Ts'o }
12647ff9c073STheodore Ts'o 
126594053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1266fa0d7e3dSNick Piggin {
12672c58d548SEric Biggers 	fscrypt_free_inode(inode);
1268fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1269fa0d7e3dSNick Piggin }
1270fa0d7e3dSNick Piggin 
1271617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1272ac27a0ecSDave Kleikamp {
12739f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1274b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1275b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1276b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
12779f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
12789f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
12799f7dd93dSVasily Averin 				true);
12809f7dd93dSVasily Averin 		dump_stack();
12819f7dd93dSVasily Averin 	}
1282ac27a0ecSDave Kleikamp }
1283ac27a0ecSDave Kleikamp 
128451cc5068SAlexey Dobriyan static void init_once(void *foo)
1285ac27a0ecSDave Kleikamp {
1286617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1287ac27a0ecSDave Kleikamp 
1288ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1289ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
12900e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1291ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1292ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1293ac27a0ecSDave Kleikamp }
1294ac27a0ecSDave Kleikamp 
1295e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1296ac27a0ecSDave Kleikamp {
1297f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1298f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1299f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1300f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1301f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1302f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
130320c2df83SPaul Mundt 				init_once);
1304617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1305ac27a0ecSDave Kleikamp 		return -ENOMEM;
1306ac27a0ecSDave Kleikamp 	return 0;
1307ac27a0ecSDave Kleikamp }
1308ac27a0ecSDave Kleikamp 
1309ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1310ac27a0ecSDave Kleikamp {
13118c0a8537SKirill A. Shutemov 	/*
13128c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
13138c0a8537SKirill A. Shutemov 	 * destroy cache.
13148c0a8537SKirill A. Shutemov 	 */
13158c0a8537SKirill A. Shutemov 	rcu_barrier();
1316617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1317ac27a0ecSDave Kleikamp }
1318ac27a0ecSDave Kleikamp 
13190930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1320ac27a0ecSDave Kleikamp {
13210930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1322dbd5768fSJan Kara 	clear_inode(inode);
132327bc446eSbrookxu 	ext4_discard_preallocations(inode, 0);
132451865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1325f4c2d372SJan Kara 	dquot_drop(inode);
13268aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
13278aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
13288aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
13298aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
13308aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
13318aefcd55STheodore Ts'o 	}
13323d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1333c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1334ac27a0ecSDave Kleikamp }
1335ac27a0ecSDave Kleikamp 
13361b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
13371b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1338ac27a0ecSDave Kleikamp {
1339ac27a0ecSDave Kleikamp 	struct inode *inode;
1340ac27a0ecSDave Kleikamp 
13418a363970STheodore Ts'o 	/*
1342ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1343ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1344ac27a0ecSDave Kleikamp 	 */
13458a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
13461d1fe1eeSDavid Howells 	if (IS_ERR(inode))
13471d1fe1eeSDavid Howells 		return ERR_CAST(inode);
13481d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1349ac27a0ecSDave Kleikamp 		iput(inode);
1350ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1351ac27a0ecSDave Kleikamp 	}
13521b961ac0SChristoph Hellwig 
13531b961ac0SChristoph Hellwig 	return inode;
1354ac27a0ecSDave Kleikamp }
13551b961ac0SChristoph Hellwig 
13561b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
13571b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
13581b961ac0SChristoph Hellwig {
13591b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
13601b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
13611b961ac0SChristoph Hellwig }
13621b961ac0SChristoph Hellwig 
13631b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
13641b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
13651b961ac0SChristoph Hellwig {
13661b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
13671b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1368ac27a0ecSDave Kleikamp }
1369ac27a0ecSDave Kleikamp 
1370fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1371fde87268STheodore Ts'o {
1372fde87268STheodore Ts'o 	struct writeback_control wbc = {
1373fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1374fde87268STheodore Ts'o 	};
1375fde87268STheodore Ts'o 
1376fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1377fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1378fde87268STheodore Ts'o }
1379fde87268STheodore Ts'o 
1380c39a7f84SToshiyuki Okajima /*
1381c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1382c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1383c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1384c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1385c39a7f84SToshiyuki Okajima  */
13860b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
13870b8e58a1SAndreas Dilger 				 gfp_t wait)
1388c39a7f84SToshiyuki Okajima {
1389c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1390c39a7f84SToshiyuki Okajima 
1391c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1392c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1393c39a7f84SToshiyuki Okajima 		return 0;
1394c39a7f84SToshiyuki Okajima 	if (journal)
1395529a781eSzhangyi (F) 		return jbd2_journal_try_to_free_buffers(journal, page);
1396529a781eSzhangyi (F) 
1397c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1398c39a7f84SToshiyuki Okajima }
1399c39a7f84SToshiyuki Okajima 
1400643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1401a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1402a7550b30SJaegeuk Kim {
1403a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1404a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1405a7550b30SJaegeuk Kim }
1406a7550b30SJaegeuk Kim 
1407a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1408a7550b30SJaegeuk Kim 							void *fs_data)
1409a7550b30SJaegeuk Kim {
14102f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1411c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1412a7550b30SJaegeuk Kim 
14139ce0151aSEric Biggers 	/*
14149ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
14159ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
14169ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
14179ce0151aSEric Biggers 	 * the filename "lost+found" itself.
14189ce0151aSEric Biggers 	 */
14199ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
14209ce0151aSEric Biggers 		return -EPERM;
1421a7550b30SJaegeuk Kim 
14227d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
14237d3e06a8SRoss Zwisler 		return -EINVAL;
14247d3e06a8SRoss Zwisler 
1425b383a73fSIra Weiny 	if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1426b383a73fSIra Weiny 		return -EOPNOTSUPP;
1427b383a73fSIra Weiny 
142894840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
142994840e3cSEric Biggers 	if (res)
143094840e3cSEric Biggers 		return res;
143194840e3cSEric Biggers 
14322f8f5e76SEric Biggers 	/*
14332f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
14342f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
14352f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
14362f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
14372f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
14382f8f5e76SEric Biggers 	 */
14392f8f5e76SEric Biggers 
14402f8f5e76SEric Biggers 	if (handle) {
14412f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
14422f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
14432f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
14442f8f5e76SEric Biggers 					    ctx, len, 0);
1445a7550b30SJaegeuk Kim 		if (!res) {
1446a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1447a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1448a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1449a3caa24bSJan Kara 			/*
14502ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
14512ee6a576SEric Biggers 			 * S_DAX may be disabled
1452a3caa24bSJan Kara 			 */
1453043546e4SIra Weiny 			ext4_set_inode_flags(inode, false);
1454a7550b30SJaegeuk Kim 		}
1455a7550b30SJaegeuk Kim 		return res;
1456a7550b30SJaegeuk Kim 	}
1457a7550b30SJaegeuk Kim 
1458b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1459b8cb5a54STahsin Erdogan 	if (res)
1460b8cb5a54STahsin Erdogan 		return res;
14612f8f5e76SEric Biggers retry:
1462af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1463af65207cSTahsin Erdogan 				     &credits);
1464dec214d0STahsin Erdogan 	if (res)
1465dec214d0STahsin Erdogan 		return res;
1466dec214d0STahsin Erdogan 
1467c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1468a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1469a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1470a7550b30SJaegeuk Kim 
14712f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
14722f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
14732f8f5e76SEric Biggers 				    ctx, len, 0);
1474a7550b30SJaegeuk Kim 	if (!res) {
1475a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
14762ee6a576SEric Biggers 		/*
14772ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
14782ee6a576SEric Biggers 		 * S_DAX may be disabled
14792ee6a576SEric Biggers 		 */
1480043546e4SIra Weiny 		ext4_set_inode_flags(inode, false);
1481a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1482a7550b30SJaegeuk Kim 		if (res)
1483a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1484a7550b30SJaegeuk Kim 	}
1485a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
14862f8f5e76SEric Biggers 
14872f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
14882f8f5e76SEric Biggers 		goto retry;
1489a7550b30SJaegeuk Kim 	if (!res)
1490a7550b30SJaegeuk Kim 		res = res2;
1491a7550b30SJaegeuk Kim 	return res;
1492a7550b30SJaegeuk Kim }
1493a7550b30SJaegeuk Kim 
1494ed318a6cSEric Biggers static const union fscrypt_context *
1495ed318a6cSEric Biggers ext4_get_dummy_context(struct super_block *sb)
1496a7550b30SJaegeuk Kim {
1497ed318a6cSEric Biggers 	return EXT4_SB(sb)->s_dummy_enc_ctx.ctx;
1498a7550b30SJaegeuk Kim }
1499a7550b30SJaegeuk Kim 
1500b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1501b925acb8SEric Biggers {
1502b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1503b925acb8SEric Biggers }
1504b925acb8SEric Biggers 
1505b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1506b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1507b925acb8SEric Biggers {
1508b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1509b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1510b925acb8SEric Biggers }
1511b925acb8SEric Biggers 
15126f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1513a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1514a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1515a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1516ed318a6cSEric Biggers 	.get_dummy_context	= ext4_get_dummy_context,
1517a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1518e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1519b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1520b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1521a7550b30SJaegeuk Kim };
1522a7550b30SJaegeuk Kim #endif
1523a7550b30SJaegeuk Kim 
1524ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1525d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1526689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1527ac27a0ecSDave Kleikamp 
1528617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1529617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1530617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1531617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1532617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
15336f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
15348c54ca9cSAl Viro 			 const struct path *path);
1535617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1536617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1537ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1538617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1539ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
15407c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
15417c319d32SAditya Kali 			     unsigned int flags);
15427c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1543ac27a0ecSDave Kleikamp 
154496c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
154596c7e0d9SJan Kara {
154696c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
154796c7e0d9SJan Kara }
154896c7e0d9SJan Kara 
154961e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
155060e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1551617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1552617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1553617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1554617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1555a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1556a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1557a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1558040cb378SLi Xi 	.get_projid		= ext4_get_projid,
15597a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1560ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1561ac27a0ecSDave Kleikamp };
1562ac27a0ecSDave Kleikamp 
15630d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1564617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1565ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1566287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
15670a240339SJan Kara 	.get_state	= dquot_get_state,
1568287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1569287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
15706332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
15716332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1572ac27a0ecSDave Kleikamp };
1573ac27a0ecSDave Kleikamp #endif
1574ac27a0ecSDave Kleikamp 
1575ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1576617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
157794053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1578617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1579617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1580617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
15817ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
15820930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1583617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1584617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1585c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1586c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1587617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1588617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1589617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1590ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1591617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1592617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
159396c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1594ac27a0ecSDave Kleikamp #endif
1595c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1596ac27a0ecSDave Kleikamp };
1597ac27a0ecSDave Kleikamp 
159839655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
15991b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
16001b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1601617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1602fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1603ac27a0ecSDave Kleikamp };
1604ac27a0ecSDave Kleikamp 
1605ac27a0ecSDave Kleikamp enum {
1606ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1607ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
160872578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1609ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
161072578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1611ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1612ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1613ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
16146ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
16154f74d15fSEric Biggers 	Opt_inlinecrypt,
1616ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
16175a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1618ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
16199cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
16209cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1621327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1622327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1623670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
16241449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
16255328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1626744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1627fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1628cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
16293d392b26STheodore Ts'o 	Opt_prefetch_block_bitmaps,
1630ac27a0ecSDave Kleikamp };
1631ac27a0ecSDave Kleikamp 
1632a447c093SSteven Whitehouse static const match_table_t tokens = {
1633ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1634ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1635ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1636ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1637ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1638ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1639ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1640ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1641ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1642ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1643ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1644ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1645ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1646ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
164772578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
164872578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1649ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1650ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1651ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1652ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1653e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
16545a916be1STheodore Ts'o 	{Opt_noload, "noload"},
165572578c33STheodore Ts'o 	{Opt_removed, "nobh"},
165672578c33STheodore Ts'o 	{Opt_removed, "bh"},
1657ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
165830773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
165930773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1660ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1661ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1662818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1663c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1664818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1665ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1666ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1667ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1668ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
16695bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
16705bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1671ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1672ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1673ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1674ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1675ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1676ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
16775a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1678ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1679ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1680ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1681ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
168249da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1683ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
168406705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
168506705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
168625ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1687923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
16889cb20f94SIra Weiny 	{Opt_dax_always, "dax=always"},
16899cb20f94SIra Weiny 	{Opt_dax_inode, "dax=inode"},
16909cb20f94SIra Weiny 	{Opt_dax_never, "dax=never"},
1691c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
169264769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1693327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1694327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1695a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1696a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1697670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1698dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
169936ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
170036ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
17016fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
17026fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1703240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1704b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1705afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
170606705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
170706705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1708744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1709244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1710744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
17115328e635SEric Sandeen 	{Opt_discard, "discard"},
17125328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1713fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1714fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1715fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1716df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1717ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
17186ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
17194f74d15fSEric Biggers 	{Opt_inlinecrypt, "inlinecrypt"},
1720cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1721cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
17223d392b26STheodore Ts'o 	{Opt_prefetch_block_bitmaps, "prefetch_block_bitmaps"},
1723c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1724c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1725c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1726c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1727c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1728f3f12faaSJosef Bacik 	{Opt_err, NULL},
1729ac27a0ecSDave Kleikamp };
1730ac27a0ecSDave Kleikamp 
1731617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1732ac27a0ecSDave Kleikamp {
1733617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1734ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1735ac27a0ecSDave Kleikamp 
1736ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1737ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
17380b8e58a1SAndreas Dilger 
1739ac27a0ecSDave Kleikamp 	options += 3;
17400b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1741ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1742ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
17434776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1744ac27a0ecSDave Kleikamp 		       (char *) *data);
1745ac27a0ecSDave Kleikamp 		return 1;
1746ac27a0ecSDave Kleikamp 	}
1747ac27a0ecSDave Kleikamp 	if (*options == ',')
1748ac27a0ecSDave Kleikamp 		options++;
1749ac27a0ecSDave Kleikamp 	*data = (void *) options;
17500b8e58a1SAndreas Dilger 
1751ac27a0ecSDave Kleikamp 	return sb_block;
1752ac27a0ecSDave Kleikamp }
1753ac27a0ecSDave Kleikamp 
1754b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1755d6006186SEric Biggers static const char deprecated_msg[] =
1756d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1757437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1758b3881f74STheodore Ts'o 
175956c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
176056c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
176156c50f11SDmitry Monakhov {
176256c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
176333458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
176403dafb5fSChen Gang 	int ret = -1;
176556c50f11SDmitry Monakhov 
176633458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
176756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
176856c50f11SDmitry Monakhov 			"Cannot change journaled "
176956c50f11SDmitry Monakhov 			"quota options when quota turned on");
177057f73c2cSTheodore Ts'o 		return -1;
177156c50f11SDmitry Monakhov 	}
1772e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1773c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1774c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1775c325a67cSTheodore Ts'o 		return 1;
1776262b4662SJan Kara 	}
177756c50f11SDmitry Monakhov 	qname = match_strdup(args);
177856c50f11SDmitry Monakhov 	if (!qname) {
177956c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
178056c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
178157f73c2cSTheodore Ts'o 		return -1;
178256c50f11SDmitry Monakhov 	}
178333458eabSTheodore Ts'o 	if (old_qname) {
178433458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
178503dafb5fSChen Gang 			ret = 1;
178603dafb5fSChen Gang 		else
178756c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
178803dafb5fSChen Gang 				 "%s quota file already specified",
178903dafb5fSChen Gang 				 QTYPE2NAME(qtype));
179003dafb5fSChen Gang 		goto errout;
179156c50f11SDmitry Monakhov 	}
179203dafb5fSChen Gang 	if (strchr(qname, '/')) {
179356c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
179456c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
179503dafb5fSChen Gang 		goto errout;
179656c50f11SDmitry Monakhov 	}
179733458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1798fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
179956c50f11SDmitry Monakhov 	return 1;
180003dafb5fSChen Gang errout:
180103dafb5fSChen Gang 	kfree(qname);
180203dafb5fSChen Gang 	return ret;
180356c50f11SDmitry Monakhov }
180456c50f11SDmitry Monakhov 
180556c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
180656c50f11SDmitry Monakhov {
180756c50f11SDmitry Monakhov 
180856c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
180933458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
181056c50f11SDmitry Monakhov 
181133458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
181256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
181356c50f11SDmitry Monakhov 			" when quota turned on");
181457f73c2cSTheodore Ts'o 		return -1;
181556c50f11SDmitry Monakhov 	}
181633458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
181733458eabSTheodore Ts'o 	synchronize_rcu();
181833458eabSTheodore Ts'o 	kfree(old_qname);
181956c50f11SDmitry Monakhov 	return 1;
182056c50f11SDmitry Monakhov }
182156c50f11SDmitry Monakhov #endif
182256c50f11SDmitry Monakhov 
182326092bf5STheodore Ts'o #define MOPT_SET	0x0001
182426092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
182526092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
182626092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
182726092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
182826092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
182926092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
183026092bf5STheodore Ts'o #define MOPT_Q		0
183126092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
183226092bf5STheodore Ts'o #else
183326092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
183426092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
183526092bf5STheodore Ts'o #endif
183626092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
18378dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
18388dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
18398dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1840ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
18419cb20f94SIra Weiny #define MOPT_SKIP	0x0800
184226092bf5STheodore Ts'o 
184326092bf5STheodore Ts'o static const struct mount_opts {
184426092bf5STheodore Ts'o 	int	token;
184526092bf5STheodore Ts'o 	int	mount_opt;
184626092bf5STheodore Ts'o 	int	flags;
184726092bf5STheodore Ts'o } ext4_mount_opts[] = {
184826092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
184926092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
185026092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
185126092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
185226092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
185326092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
18548dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
18558dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
18568dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
18578dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
185826092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
185926092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
18608dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
18618dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
18628dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
186359d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1864327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1865327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1866c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1867c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
18688dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
18691e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
187026092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
18718dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
18721e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
18738dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
187426092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
187526092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
187626092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
18778dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
18787915a861SAles Novak 	 MOPT_NO_EXT2},
18798dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
18807915a861SAles Novak 	 MOPT_NO_EXT2},
188126092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
188226092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
188326092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
188426092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
188526092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
188626092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
188726092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
188826092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
188926092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
189026092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
18919cb20f94SIra Weiny 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
18929cb20f94SIra Weiny 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
18939cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
18949cb20f94SIra Weiny 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
18959cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
18969cb20f94SIra Weiny 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
18979cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
189826092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
18990efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
19000efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
19015ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
19025ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
19035ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
19048dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
19058dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
19068dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
19078dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
190826092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
190926092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
191026092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
191126092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
191226092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
191326092bf5STheodore Ts'o #else
191426092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
191526092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
191626092bf5STheodore Ts'o #endif
191726092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
191826092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1919670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
192026092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
192126092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
192226092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
192326092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
192426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
192549da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
192649da9392SJan Kara 							MOPT_SET | MOPT_Q},
192726092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
192849da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
192949da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
193026092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
193126092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
193226092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
193326092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
193426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
193526092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
193626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1937df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
1938ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, 0, MOPT_STRING},
1939cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
19403d392b26STheodore Ts'o 	{Opt_prefetch_block_bitmaps, EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS,
19413d392b26STheodore Ts'o 	 MOPT_SET},
194226092bf5STheodore Ts'o 	{Opt_err, 0, 0}
194326092bf5STheodore Ts'o };
194426092bf5STheodore Ts'o 
1945c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1946c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1947c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1948c83ad55eSGabriel Krisman Bertazi 	char *name;
1949c83ad55eSGabriel Krisman Bertazi 	char *version;
1950c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1951c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1952c83ad55eSGabriel Krisman Bertazi };
1953c83ad55eSGabriel Krisman Bertazi 
1954c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1955c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1956c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1957c83ad55eSGabriel Krisman Bertazi {
1958c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1959c83ad55eSGabriel Krisman Bertazi 	int i;
1960c83ad55eSGabriel Krisman Bertazi 
1961c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1962c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1963c83ad55eSGabriel Krisman Bertazi 			break;
1964c83ad55eSGabriel Krisman Bertazi 
1965c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1966c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1967c83ad55eSGabriel Krisman Bertazi 
1968c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1969c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1970c83ad55eSGabriel Krisman Bertazi 
1971c83ad55eSGabriel Krisman Bertazi 	return 0;
1972c83ad55eSGabriel Krisman Bertazi }
1973c83ad55eSGabriel Krisman Bertazi #endif
1974c83ad55eSGabriel Krisman Bertazi 
1975ed318a6cSEric Biggers static int ext4_set_test_dummy_encryption(struct super_block *sb,
1976ed318a6cSEric Biggers 					  const char *opt,
1977ed318a6cSEric Biggers 					  const substring_t *arg,
1978ed318a6cSEric Biggers 					  bool is_remount)
1979ed318a6cSEric Biggers {
1980ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
1981ed318a6cSEric Biggers 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1982ed318a6cSEric Biggers 	int err;
1983ed318a6cSEric Biggers 
1984ed318a6cSEric Biggers 	/*
1985ed318a6cSEric Biggers 	 * This mount option is just for testing, and it's not worthwhile to
1986ed318a6cSEric Biggers 	 * implement the extra complexity (e.g. RCU protection) that would be
1987ed318a6cSEric Biggers 	 * needed to allow it to be set or changed during remount.  We do allow
1988ed318a6cSEric Biggers 	 * it to be specified during remount, but only if there is no change.
1989ed318a6cSEric Biggers 	 */
1990ed318a6cSEric Biggers 	if (is_remount && !sbi->s_dummy_enc_ctx.ctx) {
1991ed318a6cSEric Biggers 		ext4_msg(sb, KERN_WARNING,
1992ed318a6cSEric Biggers 			 "Can't set test_dummy_encryption on remount");
1993ed318a6cSEric Biggers 		return -1;
1994ed318a6cSEric Biggers 	}
1995ed318a6cSEric Biggers 	err = fscrypt_set_test_dummy_encryption(sb, arg, &sbi->s_dummy_enc_ctx);
1996ed318a6cSEric Biggers 	if (err) {
1997ed318a6cSEric Biggers 		if (err == -EEXIST)
1998ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1999ed318a6cSEric Biggers 				 "Can't change test_dummy_encryption on remount");
2000ed318a6cSEric Biggers 		else if (err == -EINVAL)
2001ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2002ed318a6cSEric Biggers 				 "Value of option \"%s\" is unrecognized", opt);
2003ed318a6cSEric Biggers 		else
2004ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2005ed318a6cSEric Biggers 				 "Error processing option \"%s\" [%d]",
2006ed318a6cSEric Biggers 				 opt, err);
2007ed318a6cSEric Biggers 		return -1;
2008ed318a6cSEric Biggers 	}
2009ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2010ed318a6cSEric Biggers #else
2011ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING,
2012ed318a6cSEric Biggers 		 "Test dummy encryption mount option ignored");
2013ed318a6cSEric Biggers #endif
2014ed318a6cSEric Biggers 	return 1;
2015ed318a6cSEric Biggers }
2016ed318a6cSEric Biggers 
201726092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
201826092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
201926092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
202026092bf5STheodore Ts'o {
202126092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
202226092bf5STheodore Ts'o 	const struct mount_opts *m;
202308cefc7aSEric W. Biederman 	kuid_t uid;
202408cefc7aSEric W. Biederman 	kgid_t gid;
202526092bf5STheodore Ts'o 	int arg = 0;
202626092bf5STheodore Ts'o 
202757f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
202857f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
202957f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
203057f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
203157f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
203257f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
203357f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
203457f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
203557f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
203657f73c2cSTheodore Ts'o #endif
203726092bf5STheodore Ts'o 	switch (token) {
2038f7048605STheodore Ts'o 	case Opt_noacl:
2039f7048605STheodore Ts'o 	case Opt_nouser_xattr:
2040f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
2041f7048605STheodore Ts'o 		break;
204226092bf5STheodore Ts'o 	case Opt_sb:
204326092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
204426092bf5STheodore Ts'o 	case Opt_removed:
20455f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
204626092bf5STheodore Ts'o 		return 1;
204726092bf5STheodore Ts'o 	case Opt_abort:
204826092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
204926092bf5STheodore Ts'o 		return 1;
205026092bf5STheodore Ts'o 	case Opt_i_version:
2051357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
205226092bf5STheodore Ts'o 		return 1;
2053a26f4992STheodore Ts'o 	case Opt_lazytime:
20541751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
2055a26f4992STheodore Ts'o 		return 1;
2056a26f4992STheodore Ts'o 	case Opt_nolazytime:
20571751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
2058a26f4992STheodore Ts'o 		return 1;
20594f74d15fSEric Biggers 	case Opt_inlinecrypt:
20604f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
20614f74d15fSEric Biggers 		sb->s_flags |= SB_INLINECRYPT;
20624f74d15fSEric Biggers #else
20634f74d15fSEric Biggers 		ext4_msg(sb, KERN_ERR, "inline encryption not supported");
20644f74d15fSEric Biggers #endif
20654f74d15fSEric Biggers 		return 1;
206626092bf5STheodore Ts'o 	}
206726092bf5STheodore Ts'o 
20685f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
20695f3633e3SJan Kara 		if (token == m->token)
20705f3633e3SJan Kara 			break;
20715f3633e3SJan Kara 
20725f3633e3SJan Kara 	if (m->token == Opt_err) {
20735f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
20745f3633e3SJan Kara 			 "or missing value", opt);
20755f3633e3SJan Kara 		return -1;
20765f3633e3SJan Kara 	}
20775f3633e3SJan Kara 
20788dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
20798dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
20808dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
20818dc0aa8cSTheodore Ts'o 		return -1;
20828dc0aa8cSTheodore Ts'o 	}
20838dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
20848dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
20858dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
20868dc0aa8cSTheodore Ts'o 		return -1;
20878dc0aa8cSTheodore Ts'o 	}
20888dc0aa8cSTheodore Ts'o 
2089ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
20900efb3b23SJan Kara 		return -1;
209126092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
209226092bf5STheodore Ts'o 		return -1;
2093c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
2094c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
209526092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
20961e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
20971e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
2098c93cf2d7SDmitry Monakhov 		} else
2099c93cf2d7SDmitry Monakhov 			return -1;
2100c93cf2d7SDmitry Monakhov 	}
210126092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
210226092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
210326092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
210426092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
210526092bf5STheodore Ts'o 			 "options when quota turned on");
210626092bf5STheodore Ts'o 		return -1;
210726092bf5STheodore Ts'o 	}
210826092bf5STheodore Ts'o 
210926092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
211026092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
211126092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
211226092bf5STheodore Ts'o 		if (arg == 0)
211326092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
21149ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
21159ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
21169ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
21179ba55543Szhangyi (F) 				 "must be smaller than %d",
21189ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
21199ba55543Szhangyi (F) 			return -1;
21209ba55543Szhangyi (F) 		}
212126092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
2122670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
21239803387cSTheodore Ts'o 		if ((arg & 1) ||
21249803387cSTheodore Ts'o 		    (arg < 4) ||
21259803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
21269803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
21279803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
21289803387cSTheodore Ts'o 			return -1;
21299803387cSTheodore Ts'o 		}
2130670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
213126092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
213226092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
213326092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
213426092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
213526092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
2136e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2137e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
2138e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2139e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
214026092bf5STheodore Ts'o 			return -1;
214126092bf5STheodore Ts'o 		}
214226092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
214326092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
214426092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
214526092bf5STheodore Ts'o 		if (!args->from)
214626092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
214726092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
2148df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
2149df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
215026092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
215126092bf5STheodore Ts'o 		sbi->s_stripe = arg;
21520efb3b23SJan Kara 	} else if (token == Opt_resuid) {
21530efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
21540efb3b23SJan Kara 		if (!uid_valid(uid)) {
21555f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
21560efb3b23SJan Kara 			return -1;
21570efb3b23SJan Kara 		}
21580efb3b23SJan Kara 		sbi->s_resuid = uid;
21590efb3b23SJan Kara 	} else if (token == Opt_resgid) {
21600efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
21610efb3b23SJan Kara 		if (!gid_valid(gid)) {
21625f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
21630efb3b23SJan Kara 			return -1;
21640efb3b23SJan Kara 		}
21650efb3b23SJan Kara 		sbi->s_resgid = gid;
21660efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
21670efb3b23SJan Kara 		if (is_remount) {
21680efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
21690efb3b23SJan Kara 				 "Cannot specify journal on remount");
21700efb3b23SJan Kara 			return -1;
21710efb3b23SJan Kara 		}
21720efb3b23SJan Kara 		*journal_devnum = arg;
2173ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2174ad4eec61SEric Sandeen 		char *journal_path;
2175ad4eec61SEric Sandeen 		struct inode *journal_inode;
2176ad4eec61SEric Sandeen 		struct path path;
2177ad4eec61SEric Sandeen 		int error;
2178ad4eec61SEric Sandeen 
2179ad4eec61SEric Sandeen 		if (is_remount) {
2180ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2181ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2182ad4eec61SEric Sandeen 			return -1;
2183ad4eec61SEric Sandeen 		}
2184ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2185ad4eec61SEric Sandeen 		if (!journal_path) {
2186ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2187ad4eec61SEric Sandeen 				"journal device string");
2188ad4eec61SEric Sandeen 			return -1;
2189ad4eec61SEric Sandeen 		}
2190ad4eec61SEric Sandeen 
2191ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2192ad4eec61SEric Sandeen 		if (error) {
2193ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2194ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2195ad4eec61SEric Sandeen 			kfree(journal_path);
2196ad4eec61SEric Sandeen 			return -1;
2197ad4eec61SEric Sandeen 		}
2198ad4eec61SEric Sandeen 
21992b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2200ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2201ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2202ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2203ad4eec61SEric Sandeen 			path_put(&path);
2204ad4eec61SEric Sandeen 			kfree(journal_path);
2205ad4eec61SEric Sandeen 			return -1;
2206ad4eec61SEric Sandeen 		}
2207ad4eec61SEric Sandeen 
2208ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2209ad4eec61SEric Sandeen 		path_put(&path);
2210ad4eec61SEric Sandeen 		kfree(journal_path);
22110efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
22120efb3b23SJan Kara 		if (arg > 7) {
22135f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
22140efb3b23SJan Kara 				 " (must be 0-7)");
22150efb3b23SJan Kara 			return -1;
22160efb3b23SJan Kara 		}
22170efb3b23SJan Kara 		*journal_ioprio =
22180efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
22196ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2220ed318a6cSEric Biggers 		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2221ed318a6cSEric Biggers 						      is_remount);
222226092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
222326092bf5STheodore Ts'o 		if (is_remount) {
222426092bf5STheodore Ts'o 			if (!sbi->s_journal)
222526092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
22265f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
222726092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
222826092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
222926092bf5STheodore Ts'o 				return -1;
223026092bf5STheodore Ts'o 			}
223126092bf5STheodore Ts'o 		} else {
223226092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
223326092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
223426092bf5STheodore Ts'o 		}
223526092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
223626092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
223726092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
223826092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
22395f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
22405f3633e3SJan Kara 				 "quota options when quota turned on");
224126092bf5STheodore Ts'o 			return -1;
224226092bf5STheodore Ts'o 		}
2243e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2244c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2245c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2246262b4662SJan Kara 				 "when QUOTA feature is enabled");
2247c325a67cSTheodore Ts'o 			return 1;
2248262b4662SJan Kara 		}
224926092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
225026092bf5STheodore Ts'o #endif
22519cb20f94SIra Weiny 	} else if (token == Opt_dax || token == Opt_dax_always ||
22529cb20f94SIra Weiny 		   token == Opt_dax_inode || token == Opt_dax_never) {
2253ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
22549cb20f94SIra Weiny 		switch (token) {
22559cb20f94SIra Weiny 		case Opt_dax:
22569cb20f94SIra Weiny 		case Opt_dax_always:
2257829b37b8STheodore Ts'o 			if (is_remount &&
2258829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2259829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2260829b37b8STheodore Ts'o 			fail_dax_change_remount:
2261829b37b8STheodore Ts'o 				ext4_msg(sb, KERN_ERR, "can't change "
2262829b37b8STheodore Ts'o 					 "dax mount option while remounting");
2263829b37b8STheodore Ts'o 				return -1;
2264829b37b8STheodore Ts'o 			}
2265829b37b8STheodore Ts'o 			if (is_remount &&
2266829b37b8STheodore Ts'o 			    (test_opt(sb, DATA_FLAGS) ==
2267829b37b8STheodore Ts'o 			     EXT4_MOUNT_JOURNAL_DATA)) {
2268829b37b8STheodore Ts'o 				    ext4_msg(sb, KERN_ERR, "can't mount with "
2269829b37b8STheodore Ts'o 					     "both data=journal and dax");
2270829b37b8STheodore Ts'o 				    return -1;
2271829b37b8STheodore Ts'o 			}
2272ef83b6e8SDan Williams 			ext4_msg(sb, KERN_WARNING,
2273ef83b6e8SDan Williams 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
22749cb20f94SIra Weiny 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
22759cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
22769cb20f94SIra Weiny 			break;
22779cb20f94SIra Weiny 		case Opt_dax_never:
2278829b37b8STheodore Ts'o 			if (is_remount &&
2279829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2280829b37b8STheodore Ts'o 			     (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2281829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
22829cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
22839cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
22849cb20f94SIra Weiny 			break;
22859cb20f94SIra Weiny 		case Opt_dax_inode:
2286829b37b8STheodore Ts'o 			if (is_remount &&
2287829b37b8STheodore Ts'o 			    ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2288829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2289829b37b8STheodore Ts'o 			     !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2290829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
22919cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
22929cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
22939cb20f94SIra Weiny 			/* Strictly for printing options */
22949cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
22959cb20f94SIra Weiny 			break;
22969cb20f94SIra Weiny 		}
2297ef83b6e8SDan Williams #else
2298923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
22999cb20f94SIra Weiny 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
23009cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2301923ae0ffSRoss Zwisler 		return -1;
2302923ae0ffSRoss Zwisler #endif
23037915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
23047915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
23057915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
23067915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
230726092bf5STheodore Ts'o 	} else {
230826092bf5STheodore Ts'o 		if (!args->from)
230926092bf5STheodore Ts'o 			arg = 1;
231026092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
231126092bf5STheodore Ts'o 			arg = !arg;
231226092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
231326092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
231426092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
231526092bf5STheodore Ts'o 			WARN_ON(1);
231626092bf5STheodore Ts'o 			return -1;
231726092bf5STheodore Ts'o 		}
231826092bf5STheodore Ts'o 		if (arg != 0)
231926092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
232026092bf5STheodore Ts'o 		else
232126092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
232226092bf5STheodore Ts'o 	}
232326092bf5STheodore Ts'o 	return 1;
232426092bf5STheodore Ts'o }
232526092bf5STheodore Ts'o 
2326ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2327c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2328b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2329661aa520SEric Sandeen 			 int is_remount)
2330ac27a0ecSDave Kleikamp {
23311e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
233233458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2333ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
233426092bf5STheodore Ts'o 	int token;
2335ac27a0ecSDave Kleikamp 
2336ac27a0ecSDave Kleikamp 	if (!options)
2337ac27a0ecSDave Kleikamp 		return 1;
2338ac27a0ecSDave Kleikamp 
2339ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2340ac27a0ecSDave Kleikamp 		if (!*p)
2341ac27a0ecSDave Kleikamp 			continue;
234215121c18SEric Sandeen 		/*
234315121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
234415121c18SEric Sandeen 		 * found; some options take optional arguments.
234515121c18SEric Sandeen 		 */
2346caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2347ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
234826092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
234926092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2350ac27a0ecSDave Kleikamp 			return 0;
2351ac27a0ecSDave Kleikamp 	}
2352ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
235349da9392SJan Kara 	/*
235449da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
235549da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
235649da9392SJan Kara 	 * to support legacy quotas in quota files.
235749da9392SJan Kara 	 */
235849da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
235949da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
236049da9392SJan Kara 			 "Cannot enable project quota enforcement.");
236149da9392SJan Kara 		return 0;
236249da9392SJan Kara 	}
236333458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
236433458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
236533458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
236633458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2367fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2368ac27a0ecSDave Kleikamp 
236933458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2370fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2371ac27a0ecSDave Kleikamp 
237256c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2373b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2374b31e1552SEric Sandeen 					"format mixing");
2375ac27a0ecSDave Kleikamp 			return 0;
2376ac27a0ecSDave Kleikamp 		}
2377ac27a0ecSDave Kleikamp 
2378ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2379b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2380b31e1552SEric Sandeen 					"not specified");
2381ac27a0ecSDave Kleikamp 			return 0;
2382ac27a0ecSDave Kleikamp 		}
2383ac27a0ecSDave Kleikamp 	}
2384ac27a0ecSDave Kleikamp #endif
2385626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2386626b035bSRitesh Harjani 		int blocksize =
2387626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2388626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2389626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2390626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2391626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2392626b035bSRitesh Harjani 	}
2393ac27a0ecSDave Kleikamp 	return 1;
2394ac27a0ecSDave Kleikamp }
2395ac27a0ecSDave Kleikamp 
23962adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
23972adf6da8STheodore Ts'o 					   struct super_block *sb)
23982adf6da8STheodore Ts'o {
23992adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
24002adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
240133458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
24022adf6da8STheodore Ts'o 
24032adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
24042adf6da8STheodore Ts'o 		char *fmtname = "";
24052adf6da8STheodore Ts'o 
24062adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
24072adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
24082adf6da8STheodore Ts'o 			fmtname = "vfsold";
24092adf6da8STheodore Ts'o 			break;
24102adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
24112adf6da8STheodore Ts'o 			fmtname = "vfsv0";
24122adf6da8STheodore Ts'o 			break;
24132adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
24142adf6da8STheodore Ts'o 			fmtname = "vfsv1";
24152adf6da8STheodore Ts'o 			break;
24162adf6da8STheodore Ts'o 		}
24172adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
24182adf6da8STheodore Ts'o 	}
24192adf6da8STheodore Ts'o 
242033458eabSTheodore Ts'o 	rcu_read_lock();
242133458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
242233458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
242333458eabSTheodore Ts'o 	if (usr_qf_name)
242433458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
242533458eabSTheodore Ts'o 	if (grp_qf_name)
242633458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
242733458eabSTheodore Ts'o 	rcu_read_unlock();
24282adf6da8STheodore Ts'o #endif
24292adf6da8STheodore Ts'o }
24302adf6da8STheodore Ts'o 
24315a916be1STheodore Ts'o static const char *token2str(int token)
24325a916be1STheodore Ts'o {
243350df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
24345a916be1STheodore Ts'o 
24355a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
24365a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
24375a916be1STheodore Ts'o 			break;
24385a916be1STheodore Ts'o 	return t->pattern;
24395a916be1STheodore Ts'o }
24405a916be1STheodore Ts'o 
24412adf6da8STheodore Ts'o /*
24422adf6da8STheodore Ts'o  * Show an option if
24432adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
24442adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
24452adf6da8STheodore Ts'o  */
244666acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
244766acdcf4STheodore Ts'o 			      int nodefs)
24482adf6da8STheodore Ts'o {
24492adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
24502adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
245168afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
24525a916be1STheodore Ts'o 	const struct mount_opts *m;
245366acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
24542adf6da8STheodore Ts'o 
245566acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
245666acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
24572adf6da8STheodore Ts'o 
24582adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
24595a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
24605a916be1STheodore Ts'o 
24615a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
24625a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
24635a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
24649cb20f94SIra Weiny 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
24655a916be1STheodore Ts'o 			continue;
246668afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
24675a916be1STheodore Ts'o 			continue; /* skip if same as the default */
24685a916be1STheodore Ts'o 		if ((want_set &&
24695a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
24705a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
24715a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
24725a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
24735a916be1STheodore Ts'o 	}
24745a916be1STheodore Ts'o 
247508cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
24765a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
247708cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
247808cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
247908cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
24805a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
248108cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
248208cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
248366acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
24845a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
24855a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
24862adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
24875a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
24882adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
24895a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
249066acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
24915a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
249266acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
24935a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
249466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
24955a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2496357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
24975a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
249866acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
24995a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
250068afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
250168afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
25022adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
25035a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
25042adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
25055a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
25062adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
25075a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
25085a916be1STheodore Ts'o 	}
250966acdcf4STheodore Ts'o 	if (nodefs ||
251066acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
25115a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
25122adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
25132adf6da8STheodore Ts'o 
2514ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
251566acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
25165a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2517df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2518df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
25197915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
25207915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2521ed318a6cSEric Biggers 
2522ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
25232adf6da8STheodore Ts'o 
25244f74d15fSEric Biggers 	if (sb->s_flags & SB_INLINECRYPT)
25254f74d15fSEric Biggers 		SEQ_OPTS_PUTS("inlinecrypt");
25264f74d15fSEric Biggers 
25279cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
25289cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
25299cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
25309cb20f94SIra Weiny 		else
25319cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
25329cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
25339cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
25349cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
25359cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
25369cb20f94SIra Weiny 	}
25379cb20f94SIra Weiny 
25382adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
25392adf6da8STheodore Ts'o 	return 0;
25402adf6da8STheodore Ts'o }
25412adf6da8STheodore Ts'o 
254266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
254366acdcf4STheodore Ts'o {
254466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
254566acdcf4STheodore Ts'o }
254666acdcf4STheodore Ts'o 
2547ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
254866acdcf4STheodore Ts'o {
254966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
255066acdcf4STheodore Ts'o 	int rc;
255166acdcf4STheodore Ts'o 
2552bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
255366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
255466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
255566acdcf4STheodore Ts'o 	return rc;
255666acdcf4STheodore Ts'o }
255766acdcf4STheodore Ts'o 
2558617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2559ac27a0ecSDave Kleikamp 			    int read_only)
2560ac27a0ecSDave Kleikamp {
2561617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2562c89128a0SJaegeuk Kim 	int err = 0;
2563ac27a0ecSDave Kleikamp 
2564617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2565b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2566b31e1552SEric Sandeen 			 "forcing read-only mode");
2567c89128a0SJaegeuk Kim 		err = -EROFS;
25685adaccacSyangerkun 		goto done;
2569ac27a0ecSDave Kleikamp 	}
2570ac27a0ecSDave Kleikamp 	if (read_only)
2571281b5995STheodore Ts'o 		goto done;
2572617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2573b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2574b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2575c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2576b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2577b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2578b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2579ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2580ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2581ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2582b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2583b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2584b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2585ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
25866a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
25876a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2588b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2589b31e1552SEric Sandeen 			 "warning: checktime reached, "
2590b31e1552SEric Sandeen 			 "running e2fsck is recommended");
25910390131bSFrank Mayhar 	if (!sbi->s_journal)
2592216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2593ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2594617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2595e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
25966a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
25970390131bSFrank Mayhar 	if (sbi->s_journal)
2598e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2599ac27a0ecSDave Kleikamp 
2600c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2601281b5995STheodore Ts'o done:
2602ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2603a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2604a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2605ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2606ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2607617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2608617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2609a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2610ac27a0ecSDave Kleikamp 
26117abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2612c89128a0SJaegeuk Kim 	return err;
2613ac27a0ecSDave Kleikamp }
2614ac27a0ecSDave Kleikamp 
2615117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2616117fff10STheodore Ts'o {
2617117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
26187c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
261937b0b6b8SDan Carpenter 	int size, i, j;
2620117fff10STheodore Ts'o 
2621117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2622117fff10STheodore Ts'o 		return 0;
2623117fff10STheodore Ts'o 
2624117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2625117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2626117fff10STheodore Ts'o 		return 0;
2627117fff10STheodore Ts'o 
26287c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
26297c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2630117fff10STheodore Ts'o 	if (!new_groups) {
26317c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
26327c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2633117fff10STheodore Ts'o 		return -ENOMEM;
2634117fff10STheodore Ts'o 	}
26357c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
26367c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
26377c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
26387c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
26397c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
264037b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
264137b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
26427c990728SSuraj Jitindar Singh 			kvfree(new_groups);
26437c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
26447c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
26457c990728SSuraj Jitindar Singh 			return -ENOMEM;
2646117fff10STheodore Ts'o 		}
26477c990728SSuraj Jitindar Singh 	}
26487c990728SSuraj Jitindar Singh 	rcu_read_lock();
26497c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
26507c990728SSuraj Jitindar Singh 	if (old_groups)
26517c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
26527c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
26537c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
26547c990728SSuraj Jitindar Singh 	rcu_read_unlock();
26557c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
26567c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
26577c990728SSuraj Jitindar Singh 	if (old_groups)
26587c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2659117fff10STheodore Ts'o 	return 0;
2660117fff10STheodore Ts'o }
2661117fff10STheodore Ts'o 
2662772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2663772cb7c8SJose R. Santos {
2664772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2665772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
26667c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2667772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2668117fff10STheodore Ts'o 	int i, err;
2669772cb7c8SJose R. Santos 
2670503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2671d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2672772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2673772cb7c8SJose R. Santos 		return 1;
2674772cb7c8SJose R. Santos 	}
2675772cb7c8SJose R. Santos 
2676117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2677117fff10STheodore Ts'o 	if (err)
2678772cb7c8SJose R. Santos 		goto failed;
2679772cb7c8SJose R. Santos 
2680772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
268188b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2682772cb7c8SJose R. Santos 
2683772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
26847c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
26857c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
268690ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
26877c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
26887c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2689772cb7c8SJose R. Santos 	}
2690772cb7c8SJose R. Santos 
2691772cb7c8SJose R. Santos 	return 1;
2692772cb7c8SJose R. Santos failed:
2693772cb7c8SJose R. Santos 	return 0;
2694772cb7c8SJose R. Santos }
2695772cb7c8SJose R. Santos 
2696e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2697717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2698717d50e4SAndreas Dilger {
2699b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2700717d50e4SAndreas Dilger 	__u16 crc = 0;
2701717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2702e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2703717d50e4SAndreas Dilger 
27049aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2705feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2706feb0ab32SDarrick J. Wong 		__u32 csum32;
2707b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2708feb0ab32SDarrick J. Wong 
2709feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2710feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2711b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2712b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2713b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2714b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2715b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2716b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2717b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2718feb0ab32SDarrick J. Wong 
2719feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2720feb0ab32SDarrick J. Wong 		goto out;
2721feb0ab32SDarrick J. Wong 	}
2722feb0ab32SDarrick J. Wong 
2723feb0ab32SDarrick J. Wong 	/* old crc16 code */
2724e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2725813d32f9SDarrick J. Wong 		return 0;
2726813d32f9SDarrick J. Wong 
2727717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2728717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2729717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2730717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2731717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2732e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2733717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2734717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2735717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2736717d50e4SAndreas Dilger 				offset);
2737717d50e4SAndreas Dilger 
2738feb0ab32SDarrick J. Wong out:
2739717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2740717d50e4SAndreas Dilger }
2741717d50e4SAndreas Dilger 
2742feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2743717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2744717d50e4SAndreas Dilger {
2745feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2746e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2747717d50e4SAndreas Dilger 		return 0;
2748717d50e4SAndreas Dilger 
2749717d50e4SAndreas Dilger 	return 1;
2750717d50e4SAndreas Dilger }
2751717d50e4SAndreas Dilger 
2752feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2753feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2754feb0ab32SDarrick J. Wong {
2755feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2756feb0ab32SDarrick J. Wong 		return;
2757e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2758feb0ab32SDarrick J. Wong }
2759feb0ab32SDarrick J. Wong 
2760ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2761bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2762829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2763bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2764ac27a0ecSDave Kleikamp {
2765617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2766617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2767617ba13bSMingming Cao 	ext4_fsblk_t last_block;
276844de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2769bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2770bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2771bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2772ce421581SJose R. Santos 	int flexbg_flag = 0;
2773bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2774ac27a0ecSDave Kleikamp 
2775e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2776ce421581SJose R. Santos 		flexbg_flag = 1;
2777ce421581SJose R. Santos 
2778617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2779ac27a0ecSDave Kleikamp 
2780197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2781197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2782197cd65aSAkinobu Mita 
2783ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2784bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2785ac27a0ecSDave Kleikamp 		else
2786ac27a0ecSDave Kleikamp 			last_block = first_block +
2787617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2788ac27a0ecSDave Kleikamp 
2789bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2790bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2791bfff6873SLukas Czerner 			grp = i;
2792bfff6873SLukas Czerner 
27938fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2794829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2795829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2796829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2797829fa70dSTheodore Ts'o 				 "superblock", i);
279818db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
279918db4b4eSTheodore Ts'o 				return 0;
2800829fa70dSTheodore Ts'o 		}
280177260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
280277260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
280377260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
280477260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
280577260807STheodore Ts'o 				 "block group descriptors", i);
280677260807STheodore Ts'o 			if (!sb_rdonly(sb))
280777260807STheodore Ts'o 				return 0;
280877260807STheodore Ts'o 		}
28092b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2810b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2811a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2812b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2813ac27a0ecSDave Kleikamp 			return 0;
2814ac27a0ecSDave Kleikamp 		}
28158fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2816829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2817829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2818829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2819829fa70dSTheodore Ts'o 				 "superblock", i);
282018db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
282118db4b4eSTheodore Ts'o 				return 0;
2822829fa70dSTheodore Ts'o 		}
282377260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
282477260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
282577260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
282677260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
282777260807STheodore Ts'o 				 "block group descriptors", i);
282877260807STheodore Ts'o 			if (!sb_rdonly(sb))
282977260807STheodore Ts'o 				return 0;
283077260807STheodore Ts'o 		}
28312b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2832b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2833a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2834b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2835ac27a0ecSDave Kleikamp 			return 0;
2836ac27a0ecSDave Kleikamp 		}
28378fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2838829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2839829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2840829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2841829fa70dSTheodore Ts'o 				 "superblock", i);
284218db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
284318db4b4eSTheodore Ts'o 				return 0;
2844829fa70dSTheodore Ts'o 		}
284577260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
284677260807STheodore Ts'o 		    inode_table <= last_bg_block) {
284777260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
284877260807STheodore Ts'o 				 "Inode table for group %u overlaps "
284977260807STheodore Ts'o 				 "block group descriptors", i);
285077260807STheodore Ts'o 			if (!sb_rdonly(sb))
285177260807STheodore Ts'o 				return 0;
285277260807STheodore Ts'o 		}
2853bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
28542b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2855b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2856a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2857b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2858ac27a0ecSDave Kleikamp 			return 0;
2859ac27a0ecSDave Kleikamp 		}
2860955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2861feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2862b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2863b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2864e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2865fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2866bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2867955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2868717d50e4SAndreas Dilger 				return 0;
2869717d50e4SAndreas Dilger 			}
28707ee1ec4cSLi Zefan 		}
2871955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2872ce421581SJose R. Santos 		if (!flexbg_flag)
2873617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2874ac27a0ecSDave Kleikamp 	}
2875bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2876bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2877ac27a0ecSDave Kleikamp 	return 1;
2878ac27a0ecSDave Kleikamp }
2879ac27a0ecSDave Kleikamp 
2880617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2881ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2882ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2883ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2884ac27a0ecSDave Kleikamp  *
2885ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2886ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2887ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2888ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2889ac27a0ecSDave Kleikamp  *
2890ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2891ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2892ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2893617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2894ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2895ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2896ac27a0ecSDave Kleikamp  */
2897617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2898617ba13bSMingming Cao 				struct ext4_super_block *es)
2899ac27a0ecSDave Kleikamp {
2900ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
29012c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2902ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
290395f1fda4Szhangyi (F) 	int quota_update = 0;
2904ac27a0ecSDave Kleikamp 	int i;
2905ac27a0ecSDave Kleikamp #endif
2906ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2907ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2908ac27a0ecSDave Kleikamp 		return;
2909ac27a0ecSDave Kleikamp 	}
2910ac27a0ecSDave Kleikamp 
2911a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2912b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2913b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2914a8f48a95SEric Sandeen 		return;
2915a8f48a95SEric Sandeen 	}
2916a8f48a95SEric Sandeen 
2917d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2918d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2919d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2920d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2921d39195c3SAmir Goldstein 		return;
2922d39195c3SAmir Goldstein 	}
2923d39195c3SAmir Goldstein 
2924617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2925c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
29261751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
292784474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2928ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2929ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2930c25f9bc6SEric Sandeen 		}
2931ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2932ac27a0ecSDave Kleikamp 		return;
2933ac27a0ecSDave Kleikamp 	}
2934ac27a0ecSDave Kleikamp 
29351751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2936b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
29371751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2938ac27a0ecSDave Kleikamp 	}
2939ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2940ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
29411751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
294295f1fda4Szhangyi (F) 
294395f1fda4Szhangyi (F) 	/*
294495f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
294595f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
294695f1fda4Szhangyi (F) 	 */
29471751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
294895f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
294995f1fda4Szhangyi (F) 
295095f1fda4Szhangyi (F) 		if (!ret)
295195f1fda4Szhangyi (F) 			quota_update = 1;
295295f1fda4Szhangyi (F) 		else
295395f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
295495f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
295595f1fda4Szhangyi (F) 	}
295695f1fda4Szhangyi (F) 
295795f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2958a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2959617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2960617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
296195f1fda4Szhangyi (F) 
296295f1fda4Szhangyi (F) 			if (!ret)
296395f1fda4Szhangyi (F) 				quota_update = 1;
296495f1fda4Szhangyi (F) 			else
2965b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2966b31e1552SEric Sandeen 					"Cannot turn on journaled "
296795f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2968ac27a0ecSDave Kleikamp 		}
2969ac27a0ecSDave Kleikamp 	}
2970ac27a0ecSDave Kleikamp #endif
2971ac27a0ecSDave Kleikamp 
2972ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2973ac27a0ecSDave Kleikamp 		struct inode *inode;
2974ac27a0ecSDave Kleikamp 
2975c65d5c6cSVegard Nossum 		/*
2976c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2977c65d5c6cSVegard Nossum 		 * so, skip the rest.
2978c65d5c6cSVegard Nossum 		 */
2979c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2980c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2981c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2982c65d5c6cSVegard Nossum 			break;
2983c65d5c6cSVegard Nossum 		}
2984c65d5c6cSVegard Nossum 
298597bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
298697bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2987ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2988ac27a0ecSDave Kleikamp 			break;
2989ac27a0ecSDave Kleikamp 		}
2990ac27a0ecSDave Kleikamp 
2991617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2992871a2931SChristoph Hellwig 		dquot_initialize(inode);
2993ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2994566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2995b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2996b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
299746e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2998e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2999ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
30005955102cSAl Viro 			inode_lock(inode);
300155f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
30022c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
30032c98eb5eSTheodore Ts'o 			if (ret)
30042c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
30055955102cSAl Viro 			inode_unlock(inode);
3006ac27a0ecSDave Kleikamp 			nr_truncates++;
3007ac27a0ecSDave Kleikamp 		} else {
3008566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
3009b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
3010b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
301146e665e9SHarvey Harrison 					__func__, inode->i_ino);
3012ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
3013ac27a0ecSDave Kleikamp 				  inode->i_ino);
3014ac27a0ecSDave Kleikamp 			nr_orphans++;
3015ac27a0ecSDave Kleikamp 		}
3016ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
3017ac27a0ecSDave Kleikamp 	}
3018ac27a0ecSDave Kleikamp 
3019ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
3020ac27a0ecSDave Kleikamp 
3021ac27a0ecSDave Kleikamp 	if (nr_orphans)
3022b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
3023b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
3024ac27a0ecSDave Kleikamp 	if (nr_truncates)
3025b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
3026b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
3027ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
302895f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
302995f1fda4Szhangyi (F) 	if (quota_update) {
3030a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
303195f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
3032287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
3033ac27a0ecSDave Kleikamp 		}
303495f1fda4Szhangyi (F) 	}
3035ac27a0ecSDave Kleikamp #endif
30361751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
3037ac27a0ecSDave Kleikamp }
30380b8e58a1SAndreas Dilger 
3039cd2291a4SEric Sandeen /*
3040cd2291a4SEric Sandeen  * Maximal extent format file size.
3041cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
3042cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
3043cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
3044cd2291a4SEric Sandeen  * so that won't be a limiting factor.
3045cd2291a4SEric Sandeen  *
3046f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
3047f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
3048f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
3049f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
3050f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3051f17722f9SLukas Czerner  *
3052cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3053cd2291a4SEric Sandeen  */
3054f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
3055cd2291a4SEric Sandeen {
3056cd2291a4SEric Sandeen 	loff_t res;
3057cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
3058cd2291a4SEric Sandeen 
305972deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
306072deb455SChristoph Hellwig 
306172deb455SChristoph Hellwig 	if (!has_huge_files) {
3062cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
3063cd2291a4SEric Sandeen 
3064cd2291a4SEric Sandeen 		/* total blocks in file system block size */
3065cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
3066cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
3067cd2291a4SEric Sandeen 	}
3068cd2291a4SEric Sandeen 
3069f17722f9SLukas Czerner 	/*
3070f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
3071f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
3072f17722f9SLukas Czerner 	 * size
3073f17722f9SLukas Czerner 	 */
3074f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
3075cd2291a4SEric Sandeen 	res <<= blkbits;
3076cd2291a4SEric Sandeen 
3077cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
3078cd2291a4SEric Sandeen 	if (res > upper_limit)
3079cd2291a4SEric Sandeen 		res = upper_limit;
3080cd2291a4SEric Sandeen 
3081cd2291a4SEric Sandeen 	return res;
3082cd2291a4SEric Sandeen }
3083ac27a0ecSDave Kleikamp 
3084ac27a0ecSDave Kleikamp /*
3085cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
30860fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
30870fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
3088ac27a0ecSDave Kleikamp  */
3089f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3090ac27a0ecSDave Kleikamp {
3091617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
30920fc1b451SAneesh Kumar K.V 	int meta_blocks;
30930fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
30940b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
30950b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
30960b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
30970b8e58a1SAndreas Dilger 	 *
30980b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
30990b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
31000fc1b451SAneesh Kumar K.V 	 */
31010fc1b451SAneesh Kumar K.V 
310272deb455SChristoph Hellwig 	if (!has_huge_files) {
31030fc1b451SAneesh Kumar K.V 		/*
310472deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
310572deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
310672deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
31070fc1b451SAneesh Kumar K.V 		 */
31080fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
31090fc1b451SAneesh Kumar K.V 
31100fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
31110fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
31120fc1b451SAneesh Kumar K.V 
31130fc1b451SAneesh Kumar K.V 	} else {
31148180a562SAneesh Kumar K.V 		/*
31158180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
31168180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
31178180a562SAneesh Kumar K.V 		 * represent total number of blocks in
31188180a562SAneesh Kumar K.V 		 * file system block size
31198180a562SAneesh Kumar K.V 		 */
31200fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
31210fc1b451SAneesh Kumar K.V 
31220fc1b451SAneesh Kumar K.V 	}
31230fc1b451SAneesh Kumar K.V 
31240fc1b451SAneesh Kumar K.V 	/* indirect blocks */
31250fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
31260fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
31270fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
31280fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
31290fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
31300fc1b451SAneesh Kumar K.V 
31310fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
31320fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
3133ac27a0ecSDave Kleikamp 
3134ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
3135ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
3136ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
3137ac27a0ecSDave Kleikamp 	res <<= bits;
3138ac27a0ecSDave Kleikamp 	if (res > upper_limit)
3139ac27a0ecSDave Kleikamp 		res = upper_limit;
31400fc1b451SAneesh Kumar K.V 
31410fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
31420fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
31430fc1b451SAneesh Kumar K.V 
3144ac27a0ecSDave Kleikamp 	return res;
3145ac27a0ecSDave Kleikamp }
3146ac27a0ecSDave Kleikamp 
3147617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
314870bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3149ac27a0ecSDave Kleikamp {
3150617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3151fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3152ac27a0ecSDave Kleikamp 	int has_super = 0;
3153ac27a0ecSDave Kleikamp 
3154ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3155ac27a0ecSDave Kleikamp 
3156e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
315770bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3158ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3159617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3160ac27a0ecSDave Kleikamp 		has_super = 1;
31610b8e58a1SAndreas Dilger 
3162bd63f6b0SDarrick J. Wong 	/*
3163bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3164bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3165bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3166bd63f6b0SDarrick J. Wong 	 * compensate.
3167bd63f6b0SDarrick J. Wong 	 */
3168bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
316949598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3170bd63f6b0SDarrick J. Wong 		has_super++;
3171bd63f6b0SDarrick J. Wong 
3172617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3173ac27a0ecSDave Kleikamp }
3174ac27a0ecSDave Kleikamp 
3175c9de560dSAlex Tomas /**
3176c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3177c9de560dSAlex Tomas  * @sbi: In memory super block info
3178c9de560dSAlex Tomas  *
3179c9de560dSAlex Tomas  * If we have specified it via mount option, then
3180c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3181c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3182c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3183c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3184c9de560dSAlex Tomas  *
3185c9de560dSAlex Tomas  */
3186c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3187c9de560dSAlex Tomas {
3188c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3189c9de560dSAlex Tomas 	unsigned long stripe_width =
3190c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
31913eb08658SDan Ehrenberg 	int ret;
3192c9de560dSAlex Tomas 
3193c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
31943eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
31955469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
31963eb08658SDan Ehrenberg 		ret = stripe_width;
31975469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
31983eb08658SDan Ehrenberg 		ret = stride;
31993eb08658SDan Ehrenberg 	else
32003eb08658SDan Ehrenberg 		ret = 0;
3201c9de560dSAlex Tomas 
32023eb08658SDan Ehrenberg 	/*
32033eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
32043eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
32053eb08658SDan Ehrenberg 	 */
32063eb08658SDan Ehrenberg 	if (ret <= 1)
32073eb08658SDan Ehrenberg 		ret = 0;
3208c9de560dSAlex Tomas 
32093eb08658SDan Ehrenberg 	return ret;
3210c9de560dSAlex Tomas }
3211ac27a0ecSDave Kleikamp 
3212a13fb1a4SEric Sandeen /*
3213a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3214a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3215a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3216a13fb1a4SEric Sandeen  * 0 if it cannot be.
3217a13fb1a4SEric Sandeen  */
3218a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3219a13fb1a4SEric Sandeen {
3220e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3221a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3222a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3223a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3224a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3225a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3226a13fb1a4SEric Sandeen 		return 0;
3227a13fb1a4SEric Sandeen 	}
3228a13fb1a4SEric Sandeen 
3229c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3230c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3231c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3232c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3233c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3234c83ad55eSGabriel Krisman Bertazi 		return 0;
3235c83ad55eSGabriel Krisman Bertazi 	}
3236c83ad55eSGabriel Krisman Bertazi #endif
3237c83ad55eSGabriel Krisman Bertazi 
3238a13fb1a4SEric Sandeen 	if (readonly)
3239a13fb1a4SEric Sandeen 		return 1;
3240a13fb1a4SEric Sandeen 
3241e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
32422cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
32431751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
32442cb5cc8bSDarrick J. Wong 		return 1;
32452cb5cc8bSDarrick J. Wong 	}
32462cb5cc8bSDarrick J. Wong 
3247a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3248e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3249a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3250a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3251a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3252a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3253a13fb1a4SEric Sandeen 		return 0;
3254a13fb1a4SEric Sandeen 	}
3255e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3256bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3257bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3258bab08ab9STheodore Ts'o 			 "extents feature\n");
3259bab08ab9STheodore Ts'o 		return 0;
3260bab08ab9STheodore Ts'o 	}
32617c319d32SAditya Kali 
32629db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3263d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3264d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
32657c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3266d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3267689c958cSLi Xi 		return 0;
3268689c958cSLi Xi 	}
32697c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3270a13fb1a4SEric Sandeen 	return 1;
3271a13fb1a4SEric Sandeen }
3272a13fb1a4SEric Sandeen 
327366e61a9eSTheodore Ts'o /*
327466e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
327566e61a9eSTheodore Ts'o  * on the file system
327666e61a9eSTheodore Ts'o  */
3277235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
327866e61a9eSTheodore Ts'o {
3279235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3280235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3281235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
328266e61a9eSTheodore Ts'o 
328366e61a9eSTheodore Ts'o 	if (es->s_error_count)
3284ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3285ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
328666e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
328766e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
32886a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
32896a0678a7SArnd Bergmann 		       sb->s_id,
32906a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
329166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
329266e61a9eSTheodore Ts'o 		       es->s_first_error_func,
329366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
329466e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3295651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
329666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
329766e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3298651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
329966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3300651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
330166e61a9eSTheodore Ts'o 	}
330266e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
33036a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
33046a0678a7SArnd Bergmann 		       sb->s_id,
33056a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
330666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
330766e61a9eSTheodore Ts'o 		       es->s_last_error_func,
330866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
330966e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3310651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
331166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
331266e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3313651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
331466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3315651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
331666e61a9eSTheodore Ts'o 	}
331766e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
331866e61a9eSTheodore Ts'o }
331966e61a9eSTheodore Ts'o 
3320bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3321bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3322bfff6873SLukas Czerner {
3323bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
33243d392b26STheodore Ts'o 	struct super_block *sb = elr->lr_super;
33253d392b26STheodore Ts'o 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
33263d392b26STheodore Ts'o 	ext4_group_t group = elr->lr_next_group;
3327bfff6873SLukas Czerner 	unsigned long timeout = 0;
33283d392b26STheodore Ts'o 	unsigned int prefetch_ios = 0;
3329bfff6873SLukas Czerner 	int ret = 0;
3330bfff6873SLukas Czerner 
33313d392b26STheodore Ts'o 	if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
33323d392b26STheodore Ts'o 		elr->lr_next_group = ext4_mb_prefetch(sb, group,
33333d392b26STheodore Ts'o 				EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
33343d392b26STheodore Ts'o 		if (prefetch_ios)
33353d392b26STheodore Ts'o 			ext4_mb_prefetch_fini(sb, elr->lr_next_group,
33363d392b26STheodore Ts'o 					      prefetch_ios);
33373d392b26STheodore Ts'o 		trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
33383d392b26STheodore Ts'o 					    prefetch_ios);
33393d392b26STheodore Ts'o 		if (group >= elr->lr_next_group) {
33403d392b26STheodore Ts'o 			ret = 1;
33413d392b26STheodore Ts'o 			if (elr->lr_first_not_zeroed != ngroups &&
33423d392b26STheodore Ts'o 			    !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
33433d392b26STheodore Ts'o 				elr->lr_next_group = elr->lr_first_not_zeroed;
33443d392b26STheodore Ts'o 				elr->lr_mode = EXT4_LI_MODE_ITABLE;
33453d392b26STheodore Ts'o 				ret = 0;
33463d392b26STheodore Ts'o 			}
33473d392b26STheodore Ts'o 		}
33483d392b26STheodore Ts'o 		return ret;
33493d392b26STheodore Ts'o 	}
3350bfff6873SLukas Czerner 
33513d392b26STheodore Ts'o 	for (; group < ngroups; group++) {
3352bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3353bfff6873SLukas Czerner 		if (!gdp) {
3354bfff6873SLukas Czerner 			ret = 1;
3355bfff6873SLukas Czerner 			break;
3356bfff6873SLukas Czerner 		}
3357bfff6873SLukas Czerner 
3358bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3359bfff6873SLukas Czerner 			break;
3360bfff6873SLukas Czerner 	}
3361bfff6873SLukas Czerner 
33627f511862STheodore Ts'o 	if (group >= ngroups)
3363bfff6873SLukas Czerner 		ret = 1;
3364bfff6873SLukas Czerner 
3365bfff6873SLukas Czerner 	if (!ret) {
3366bfff6873SLukas Czerner 		timeout = jiffies;
3367bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3368bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
33693d392b26STheodore Ts'o 		trace_ext4_lazy_itable_init(sb, group);
3370bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
337151ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
33723d392b26STheodore Ts'o 				EXT4_SB(elr->lr_super)->s_li_wait_mult;
3373bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3374bfff6873SLukas Czerner 		}
3375bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3376bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3377bfff6873SLukas Czerner 	}
3378bfff6873SLukas Czerner 	return ret;
3379bfff6873SLukas Czerner }
3380bfff6873SLukas Czerner 
3381bfff6873SLukas Czerner /*
3382bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
33834ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3384bfff6873SLukas Czerner  */
3385bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3386bfff6873SLukas Czerner {
3387bfff6873SLukas Czerner 	if (!elr)
3388bfff6873SLukas Czerner 		return;
3389bfff6873SLukas Czerner 
3390bfff6873SLukas Czerner 	list_del(&elr->lr_request);
33913d392b26STheodore Ts'o 	EXT4_SB(elr->lr_super)->s_li_request = NULL;
3392bfff6873SLukas Czerner 	kfree(elr);
3393bfff6873SLukas Czerner }
3394bfff6873SLukas Czerner 
3395bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3396bfff6873SLukas Czerner {
33971bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
33981bb933fbSLukas Czerner 	if (!ext4_li_info) {
33991bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3400bfff6873SLukas Czerner 		return;
34011bb933fbSLukas Czerner 	}
3402bfff6873SLukas Czerner 
3403bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
34041bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3405bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
34061bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3407bfff6873SLukas Czerner }
3408bfff6873SLukas Czerner 
34098f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
34108f1f7453SEric Sandeen 
3411bfff6873SLukas Czerner /*
3412bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3413bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3414bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3415bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3416bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3417bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3418bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3419bfff6873SLukas Czerner  */
3420bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3421bfff6873SLukas Czerner {
3422bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3423bfff6873SLukas Czerner 	struct list_head *pos, *n;
3424bfff6873SLukas Czerner 	struct ext4_li_request *elr;
34254ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3426bfff6873SLukas Czerner 
3427bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3428bfff6873SLukas Czerner 
3429bfff6873SLukas Czerner cont_thread:
3430bfff6873SLukas Czerner 	while (true) {
3431bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3432bfff6873SLukas Czerner 
3433bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3434bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3435bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3436bfff6873SLukas Czerner 			goto exit_thread;
3437bfff6873SLukas Czerner 		}
3438bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3439e22834f0SDmitry Monakhov 			int err = 0;
3440e22834f0SDmitry Monakhov 			int progress = 0;
3441bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3442bfff6873SLukas Czerner 					 lr_request);
3443bfff6873SLukas Czerner 
3444e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3445e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3446e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3447e22834f0SDmitry Monakhov 				continue;
3448e22834f0SDmitry Monakhov 			}
3449e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3450e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3451e22834f0SDmitry Monakhov 					progress = 1;
3452e22834f0SDmitry Monakhov 					/*
3453e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3454e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3455e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3456e22834f0SDmitry Monakhov 					 */
3457e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3458e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3459e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3460e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3461e22834f0SDmitry Monakhov 					n = pos->next;
3462e22834f0SDmitry Monakhov 				}
3463e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3464e22834f0SDmitry Monakhov 			}
3465b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3466e22834f0SDmitry Monakhov 			if (err) {
3467bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3468bfff6873SLukas Czerner 				continue;
3469bfff6873SLukas Czerner 			}
3470e22834f0SDmitry Monakhov 			if (!progress) {
3471e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3472e22834f0SDmitry Monakhov 					(prandom_u32()
3473e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3474b2c78cd0STheodore Ts'o 			}
3475bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3476bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3477bfff6873SLukas Czerner 		}
3478bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3479bfff6873SLukas Czerner 
3480a0acae0eSTejun Heo 		try_to_freeze();
3481bfff6873SLukas Czerner 
34824ed5c033SLukas Czerner 		cur = jiffies;
34834ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3484f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3485bfff6873SLukas Czerner 			cond_resched();
3486bfff6873SLukas Czerner 			continue;
3487bfff6873SLukas Czerner 		}
3488bfff6873SLukas Czerner 
34894ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
34904ed5c033SLukas Czerner 
34918f1f7453SEric Sandeen 		if (kthread_should_stop()) {
34928f1f7453SEric Sandeen 			ext4_clear_request_list();
34938f1f7453SEric Sandeen 			goto exit_thread;
34948f1f7453SEric Sandeen 		}
3495bfff6873SLukas Czerner 	}
3496bfff6873SLukas Czerner 
3497bfff6873SLukas Czerner exit_thread:
3498bfff6873SLukas Czerner 	/*
3499bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3500bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3501bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3502bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3503bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3504bfff6873SLukas Czerner 	 * new one.
3505bfff6873SLukas Czerner 	 */
3506bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3507bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3508bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3509bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3510bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3511bfff6873SLukas Czerner 		goto cont_thread;
3512bfff6873SLukas Czerner 	}
3513bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3514bfff6873SLukas Czerner 	kfree(ext4_li_info);
3515bfff6873SLukas Czerner 	ext4_li_info = NULL;
3516bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3517bfff6873SLukas Czerner 
3518bfff6873SLukas Czerner 	return 0;
3519bfff6873SLukas Czerner }
3520bfff6873SLukas Czerner 
3521bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3522bfff6873SLukas Czerner {
3523bfff6873SLukas Czerner 	struct list_head *pos, *n;
3524bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3525bfff6873SLukas Czerner 
3526bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3527bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3528bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3529bfff6873SLukas Czerner 				 lr_request);
3530bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3531bfff6873SLukas Czerner 	}
3532bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3533bfff6873SLukas Czerner }
3534bfff6873SLukas Czerner 
3535bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3536bfff6873SLukas Czerner {
35378f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
35388f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
35398f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
35408f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3541bfff6873SLukas Czerner 		ext4_clear_request_list();
3542bfff6873SLukas Czerner 		kfree(ext4_li_info);
3543bfff6873SLukas Czerner 		ext4_li_info = NULL;
354492b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3545bfff6873SLukas Czerner 				 "initialization thread\n",
3546bfff6873SLukas Czerner 				 err);
3547bfff6873SLukas Czerner 		return err;
3548bfff6873SLukas Czerner 	}
3549bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3550bfff6873SLukas Czerner 	return 0;
3551bfff6873SLukas Czerner }
3552bfff6873SLukas Czerner 
3553bfff6873SLukas Czerner /*
3554bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3555bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3556bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3557bfff6873SLukas Czerner  * groups and return total number of groups.
3558bfff6873SLukas Czerner  */
3559bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3560bfff6873SLukas Czerner {
3561bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3562bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3563bfff6873SLukas Czerner 
35648844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
35658844618dSTheodore Ts'o 		return ngroups;
35668844618dSTheodore Ts'o 
3567bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3568bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3569bfff6873SLukas Czerner 		if (!gdp)
3570bfff6873SLukas Czerner 			continue;
3571bfff6873SLukas Czerner 
357250122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3573bfff6873SLukas Czerner 			break;
3574bfff6873SLukas Czerner 	}
3575bfff6873SLukas Czerner 
3576bfff6873SLukas Czerner 	return group;
3577bfff6873SLukas Czerner }
3578bfff6873SLukas Czerner 
3579bfff6873SLukas Czerner static int ext4_li_info_new(void)
3580bfff6873SLukas Czerner {
3581bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3582bfff6873SLukas Czerner 
3583bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3584bfff6873SLukas Czerner 	if (!eli)
3585bfff6873SLukas Czerner 		return -ENOMEM;
3586bfff6873SLukas Czerner 
3587bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3588bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3589bfff6873SLukas Czerner 
3590bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3591bfff6873SLukas Czerner 
3592bfff6873SLukas Czerner 	ext4_li_info = eli;
3593bfff6873SLukas Czerner 
3594bfff6873SLukas Czerner 	return 0;
3595bfff6873SLukas Czerner }
3596bfff6873SLukas Czerner 
3597bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3598bfff6873SLukas Czerner 					    ext4_group_t start)
3599bfff6873SLukas Czerner {
3600bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3601bfff6873SLukas Czerner 
3602bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3603bfff6873SLukas Czerner 	if (!elr)
3604bfff6873SLukas Czerner 		return NULL;
3605bfff6873SLukas Czerner 
3606bfff6873SLukas Czerner 	elr->lr_super = sb;
36073d392b26STheodore Ts'o 	elr->lr_first_not_zeroed = start;
36083d392b26STheodore Ts'o 	if (test_opt(sb, PREFETCH_BLOCK_BITMAPS))
36093d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
36103d392b26STheodore Ts'o 	else {
36113d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_ITABLE;
3612bfff6873SLukas Czerner 		elr->lr_next_group = start;
36133d392b26STheodore Ts'o 	}
3614bfff6873SLukas Czerner 
3615bfff6873SLukas Czerner 	/*
3616bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3617bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3618bfff6873SLukas Czerner 	 * better.
3619bfff6873SLukas Czerner 	 */
3620dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3621dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3622bfff6873SLukas Czerner 	return elr;
3623bfff6873SLukas Czerner }
3624bfff6873SLukas Czerner 
36257f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3626bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3627bfff6873SLukas Czerner {
3628bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
36297f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
363049598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
36316c5a6cb9SAndrew Morton 	int ret = 0;
3632bfff6873SLukas Czerner 
36337f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
363451ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
363551ce6511SLukas Czerner 		/*
363651ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
363751ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
363851ce6511SLukas Czerner 		 */
363951ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
36407f511862STheodore Ts'o 		goto out;
364151ce6511SLukas Czerner 	}
3642bfff6873SLukas Czerner 
36433d392b26STheodore Ts'o 	if (!test_opt(sb, PREFETCH_BLOCK_BITMAPS) &&
36443d392b26STheodore Ts'o 	    (first_not_zeroed == ngroups || sb_rdonly(sb) ||
36453d392b26STheodore Ts'o 	     !test_opt(sb, INIT_INODE_TABLE)))
36467f511862STheodore Ts'o 		goto out;
3647bfff6873SLukas Czerner 
3648bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
36497f511862STheodore Ts'o 	if (!elr) {
36507f511862STheodore Ts'o 		ret = -ENOMEM;
36517f511862STheodore Ts'o 		goto out;
36527f511862STheodore Ts'o 	}
3653bfff6873SLukas Czerner 
3654bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3655bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3656bfff6873SLukas Czerner 		if (ret)
3657bfff6873SLukas Czerner 			goto out;
3658bfff6873SLukas Czerner 	}
3659bfff6873SLukas Czerner 
3660bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3661bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3662bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3663bfff6873SLukas Czerner 
3664bfff6873SLukas Czerner 	sbi->s_li_request = elr;
366546e4690bSTao Ma 	/*
366646e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
366746e4690bSTao Ma 	 * the request_list and the removal and free of it is
366846e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
366946e4690bSTao Ma 	 */
367046e4690bSTao Ma 	elr = NULL;
3671bfff6873SLukas Czerner 
3672bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3673bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3674bfff6873SLukas Czerner 		if (ret)
3675bfff6873SLukas Czerner 			goto out;
3676bfff6873SLukas Czerner 	}
3677bfff6873SLukas Czerner out:
3678bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3679beed5ecbSNicolas Kaiser 	if (ret)
3680bfff6873SLukas Czerner 		kfree(elr);
3681bfff6873SLukas Czerner 	return ret;
3682bfff6873SLukas Czerner }
3683bfff6873SLukas Czerner 
3684bfff6873SLukas Czerner /*
3685bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3686bfff6873SLukas Czerner  * module unload.
3687bfff6873SLukas Czerner  */
3688bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3689bfff6873SLukas Czerner {
3690bfff6873SLukas Czerner 	/*
3691bfff6873SLukas Czerner 	 * If thread exited earlier
3692bfff6873SLukas Czerner 	 * there's nothing to be done.
3693bfff6873SLukas Czerner 	 */
36948f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3695bfff6873SLukas Czerner 		return;
3696bfff6873SLukas Czerner 
36978f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3698bfff6873SLukas Czerner }
3699bfff6873SLukas Czerner 
370025ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
370125ed6e8aSDarrick J. Wong {
370225ed6e8aSDarrick J. Wong 	int ret = 1;
370325ed6e8aSDarrick J. Wong 	int compat, incompat;
370425ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
370525ed6e8aSDarrick J. Wong 
37069aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3707db9ee220SDarrick J. Wong 		/* journal checksum v3 */
370825ed6e8aSDarrick J. Wong 		compat = 0;
3709db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
371025ed6e8aSDarrick J. Wong 	} else {
371125ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
371225ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
371325ed6e8aSDarrick J. Wong 		incompat = 0;
371425ed6e8aSDarrick J. Wong 	}
371525ed6e8aSDarrick J. Wong 
3716feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3717feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3718feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3719feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
372025ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
372125ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
372225ed6e8aSDarrick J. Wong 				compat, 0,
372325ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
372425ed6e8aSDarrick J. Wong 				incompat);
372525ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
372625ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
372725ed6e8aSDarrick J. Wong 				compat, 0,
372825ed6e8aSDarrick J. Wong 				incompat);
372925ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
373025ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
373125ed6e8aSDarrick J. Wong 	} else {
3732feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3733feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
373425ed6e8aSDarrick J. Wong 	}
373525ed6e8aSDarrick J. Wong 
373625ed6e8aSDarrick J. Wong 	return ret;
373725ed6e8aSDarrick J. Wong }
373825ed6e8aSDarrick J. Wong 
3739952fc18eSTheodore Ts'o /*
3740952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3741952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3742952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3743952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3744952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3745952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3746952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3747952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3748952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3749952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3750952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3751952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3752952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3753952fc18eSTheodore Ts'o  */
3754952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3755952fc18eSTheodore Ts'o 			  char *buf)
3756952fc18eSTheodore Ts'o {
3757952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3758952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3759952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3760952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3761952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3762952fc18eSTheodore Ts'o 
3763e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
37640548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
37650548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
37660548bbb8STheodore Ts'o 
3767952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3768952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3769952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3770952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3771952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3772952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3773952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3774952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3775952fc18eSTheodore Ts'o 			count++;
3776952fc18eSTheodore Ts'o 		}
3777952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3778952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3779952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3780952fc18eSTheodore Ts'o 			count++;
3781952fc18eSTheodore Ts'o 		}
3782952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3783952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3784952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3785952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3786952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3787952fc18eSTheodore Ts'o 				count++;
3788952fc18eSTheodore Ts'o 			}
3789952fc18eSTheodore Ts'o 		if (i != grp)
3790952fc18eSTheodore Ts'o 			continue;
3791952fc18eSTheodore Ts'o 		s = 0;
3792952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3793952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3794952fc18eSTheodore Ts'o 			count++;
3795952fc18eSTheodore Ts'o 		}
3796c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3797c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3798c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3799c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3800c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3801952fc18eSTheodore Ts'o 		}
3802c48ae41bSTheodore Ts'o 		count += j;
3803c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3804c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3805952fc18eSTheodore Ts'o 	}
3806952fc18eSTheodore Ts'o 	if (!count)
3807952fc18eSTheodore Ts'o 		return 0;
3808952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3809952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3810952fc18eSTheodore Ts'o }
3811952fc18eSTheodore Ts'o 
3812952fc18eSTheodore Ts'o /*
3813952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3814952fc18eSTheodore Ts'o  */
3815952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3816952fc18eSTheodore Ts'o {
3817952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3818952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
38193c816dedSEric Whitney 	struct inode *j_inode;
38203c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3821952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3822952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
38234fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3824952fc18eSTheodore Ts'o 
3825952fc18eSTheodore Ts'o 	if (!buf)
3826952fc18eSTheodore Ts'o 		return -ENOMEM;
3827952fc18eSTheodore Ts'o 
3828952fc18eSTheodore Ts'o 	/*
3829952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3830952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3831952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3832952fc18eSTheodore Ts'o 	 */
3833952fc18eSTheodore Ts'o 
3834952fc18eSTheodore Ts'o 	/*
3835952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3836952fc18eSTheodore Ts'o 	 */
3837952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3838952fc18eSTheodore Ts'o 
3839952fc18eSTheodore Ts'o 	/*
3840952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3841952fc18eSTheodore Ts'o 	 */
3842952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3843952fc18eSTheodore Ts'o 		int blks;
3844952fc18eSTheodore Ts'o 
3845952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3846952fc18eSTheodore Ts'o 		overhead += blks;
3847952fc18eSTheodore Ts'o 		if (blks)
3848952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3849952fc18eSTheodore Ts'o 		cond_resched();
3850952fc18eSTheodore Ts'o 	}
38513c816dedSEric Whitney 
38523c816dedSEric Whitney 	/*
38533c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
38543c816dedSEric Whitney 	 * loaded or not
38553c816dedSEric Whitney 	 */
3856ee7ed3aaSChunguang Xu 	if (sbi->s_journal && !sbi->s_journal_bdev)
3857810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
3858f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3859f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
38603c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
38613c816dedSEric Whitney 		if (j_inode) {
38623c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
38633c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
38643c816dedSEric Whitney 			iput(j_inode);
38653c816dedSEric Whitney 		} else {
38663c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
38673c816dedSEric Whitney 		}
38683c816dedSEric Whitney 	}
3869952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3870952fc18eSTheodore Ts'o 	smp_wmb();
3871952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3872952fc18eSTheodore Ts'o 	return 0;
3873952fc18eSTheodore Ts'o }
3874952fc18eSTheodore Ts'o 
3875b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
387627dd4385SLukas Czerner {
387727dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3878b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
387927dd4385SLukas Czerner 
388027dd4385SLukas Czerner 	/*
388130fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
388230fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
388330fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
388430fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
388530fac0f7SJan Kara 	 */
3886e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3887b5799018STheodore Ts'o 		return;
388830fac0f7SJan Kara 	/*
388927dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
389027dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
389127dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3892556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
389327dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
389427dd4385SLukas Czerner 	 * very rare.
389527dd4385SLukas Czerner 	 */
3896b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3897b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
389827dd4385SLukas Czerner 
389927dd4385SLukas Czerner 	do_div(resv_clusters, 50);
390027dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
390127dd4385SLukas Czerner 
3902b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
390327dd4385SLukas Czerner }
390427dd4385SLukas Czerner 
3905617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3906ac27a0ecSDave Kleikamp {
39075e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3908d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
39091d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
3910617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
39115aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
39127c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
3913617ba13bSMingming Cao 	ext4_fsblk_t block;
3914617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
391570bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3916ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3917ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3918ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3919ac27a0ecSDave Kleikamp 	struct inode *root;
39200390131bSFrank Mayhar 	const char *descr;
3921dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3922281b5995STheodore Ts'o 	int blocksize, clustersize;
39234ec11028STheodore Ts'o 	unsigned int db_count;
39244ec11028STheodore Ts'o 	unsigned int i;
3925ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
3926bd81d8eeSLaurent Vivier 	__u64 blocks_count;
392707aa2ea1SLukas Czerner 	int err = 0;
3928b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3929bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3930ac27a0ecSDave Kleikamp 
39315aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
39325aee0f8aSTheodore Ts'o 		goto out_free_base;
3933705895b6SPekka Enberg 
3934aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3935705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3936705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
39375aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
39385aee0f8aSTheodore Ts'o 		goto out_free_base;
39395aee0f8aSTheodore Ts'o 
3940ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
39412c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3942240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3943d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3944f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3945f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3946dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3947ac27a0ecSDave Kleikamp 
39489f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3949ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
39509f6200bbSTheodore Ts'o 
395107aa2ea1SLukas Czerner 	/* -EINVAL is default */
3952dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3953617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3954ac27a0ecSDave Kleikamp 	if (!blocksize) {
3955b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3956ac27a0ecSDave Kleikamp 		goto out_fail;
3957ac27a0ecSDave Kleikamp 	}
3958ac27a0ecSDave Kleikamp 
3959ac27a0ecSDave Kleikamp 	/*
3960617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3961ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3962ac27a0ecSDave Kleikamp 	 */
3963617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
396470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
396570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3966ac27a0ecSDave Kleikamp 	} else {
396770bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3968ac27a0ecSDave Kleikamp 	}
3969ac27a0ecSDave Kleikamp 
39708394a6abSzhangyi (F) 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
39718394a6abSzhangyi (F) 	if (IS_ERR(bh)) {
3972b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
39738394a6abSzhangyi (F) 		ret = PTR_ERR(bh);
39748394a6abSzhangyi (F) 		bh = NULL;
3975ac27a0ecSDave Kleikamp 		goto out_fail;
3976ac27a0ecSDave Kleikamp 	}
3977ac27a0ecSDave Kleikamp 	/*
3978ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3979617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3980ac27a0ecSDave Kleikamp 	 */
39812716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3982ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3983ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3984617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3985617ba13bSMingming Cao 		goto cantfind_ext4;
3986afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3987ac27a0ecSDave Kleikamp 
3988feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3989e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3990e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3991363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3992feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3993feb0ab32SDarrick J. Wong 
3994d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3995d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3996d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3997d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3998d25425f8SDarrick J. Wong 		silent = 1;
3999d25425f8SDarrick J. Wong 		goto cantfind_ext4;
4000d25425f8SDarrick J. Wong 	}
4001d25425f8SDarrick J. Wong 
40020441984aSDarrick J. Wong 	/* Load the checksum driver */
40030441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
40040441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
40050441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
40060441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
40070441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
40080441984aSDarrick J. Wong 		goto failed_mount;
40090441984aSDarrick J. Wong 	}
40100441984aSDarrick J. Wong 
4011a9c47317SDarrick J. Wong 	/* Check superblock checksum */
4012a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
4013a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4014a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
4015a9c47317SDarrick J. Wong 		silent = 1;
40166a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
4017a9c47317SDarrick J. Wong 		goto cantfind_ext4;
4018a9c47317SDarrick J. Wong 	}
4019a9c47317SDarrick J. Wong 
4020a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
4021e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
40228c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4023dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4024a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4025a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
4026a9c47317SDarrick J. Wong 
4027ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
4028ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4029fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
4030617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
4031fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
403287f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4033fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
4034617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
4035fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
4036ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
4037fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
403803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
4039fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
40402e7842b8SHugh Dickins #endif
404198c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
404298c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
404398c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
404498c1a759SDarrick J. Wong 
4045617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4046fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
4047617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4048fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
4049617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4050fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
4051ac27a0ecSDave Kleikamp 
4052617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
4053fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
4054bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
4055fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
4056bb4f397aSAneesh Kumar K.V 	else
4057fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
405845f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
4059fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
40608b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
4061fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
4062ac27a0ecSDave Kleikamp 
406308cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
406408cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
406530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
406630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
406730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
4068ac27a0ecSDave Kleikamp 
40698b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4070fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
4071ac27a0ecSDave Kleikamp 
40721e2462f9SMingming Cao 	/*
4073dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
4074dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
4075dd919b98SAneesh Kumar K.V 	 */
4076bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
40778b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4078fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
4079dd919b98SAneesh Kumar K.V 
408051ce6511SLukas Czerner 	/*
408151ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
408251ce6511SLukas Czerner 	 * no mount option specified.
408351ce6511SLukas Czerner 	 */
408451ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
408551ce6511SLukas Czerner 
40864f97a681STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4087626b035bSRitesh Harjani 
4088626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
4089626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
4090626b035bSRitesh Harjani 
40914f97a681STheodore Ts'o 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
40924f97a681STheodore Ts'o 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
40934f97a681STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
40944f97a681STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
40954f97a681STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
40964f97a681STheodore Ts'o 		goto failed_mount;
40974f97a681STheodore Ts'o 	}
40984f97a681STheodore Ts'o 
40999803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
41009803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
41019803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
41029803387cSTheodore Ts'o 	} else {
41039803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
41049803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
41059803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
41069803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
41079803387cSTheodore Ts'o 				 sbi->s_first_ino);
41089803387cSTheodore Ts'o 			goto failed_mount;
41099803387cSTheodore Ts'o 		}
41109803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
41119803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
41129803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
41139803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
41149803387cSTheodore Ts'o 			       "unsupported inode size: %d",
41159803387cSTheodore Ts'o 			       sbi->s_inode_size);
41164f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
41179803387cSTheodore Ts'o 			goto failed_mount;
41189803387cSTheodore Ts'o 		}
41199803387cSTheodore Ts'o 		/*
41209803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
41219803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
41229803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
41239803387cSTheodore Ts'o 		 * for all three.
41249803387cSTheodore Ts'o 		 */
41259803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
41269803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
41279803387cSTheodore Ts'o 			sb->s_time_gran = 1;
41289803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
41299803387cSTheodore Ts'o 		} else {
41309803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
41319803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
41329803387cSTheodore Ts'o 		}
41339803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
41349803387cSTheodore Ts'o 	}
41359803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
41369803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
41379803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
41389803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
41399803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
41409803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
41419803387cSTheodore Ts'o 
41429803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
41439803387cSTheodore Ts'o 			if (v > max) {
41449803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
41459803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
41469803387cSTheodore Ts'o 				goto failed_mount;
41479803387cSTheodore Ts'o 			}
41489803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
41499803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
41509803387cSTheodore Ts'o 
41519803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
41529803387cSTheodore Ts'o 			if (v > max) {
41539803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
41549803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
41559803387cSTheodore Ts'o 				goto failed_mount;
41569803387cSTheodore Ts'o 			}
41579803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
41589803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
41599803387cSTheodore Ts'o 		}
41609803387cSTheodore Ts'o 	}
41619803387cSTheodore Ts'o 
41625aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
41635aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
41645aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
41655aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
41665aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
41675aee0f8aSTheodore Ts'o 			goto failed_mount;
41685aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
41695aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
41708b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
41718b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
41725aee0f8aSTheodore Ts'o 				 s_mount_opts);
41735aee0f8aSTheodore Ts'o 		}
41745aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
41758b67f04aSTheodore Ts'o 	}
41765a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
4177b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
4178661aa520SEric Sandeen 			   &journal_ioprio, 0))
4179ac27a0ecSDave Kleikamp 		goto failed_mount;
4180ac27a0ecSDave Kleikamp 
4181c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4182c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
4183c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
4184c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
4185c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
4186c83ad55eSGabriel Krisman Bertazi 
4187c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
4188c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4189c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
4190c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4191c83ad55eSGabriel Krisman Bertazi 		}
4192c83ad55eSGabriel Krisman Bertazi 
4193c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
4194c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
4195c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4196c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
4197c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4198c83ad55eSGabriel Krisman Bertazi 		}
4199c83ad55eSGabriel Krisman Bertazi 
4200c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
4201c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
4202c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4203c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
4204c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4205c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4206c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4207c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4208c83ad55eSGabriel Krisman Bertazi 		}
4209c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4210c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4211c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4212c83ad55eSGabriel Krisman Bertazi 
4213c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
4214c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
4215c83ad55eSGabriel Krisman Bertazi 	}
4216c83ad55eSGabriel Krisman Bertazi #endif
4217c83ad55eSGabriel Krisman Bertazi 
421856889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4219244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
4220781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4221244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
422256889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
422356889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
422456889787STheodore Ts'o 				 "both data=journal and delalloc");
422556889787STheodore Ts'o 			goto failed_mount;
422656889787STheodore Ts'o 		}
4227fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4228923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4229923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4230923ae0ffSRoss Zwisler 			goto failed_mount;
4231923ae0ffSRoss Zwisler 		}
423273b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
423373b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
423473b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
423573b92a2aSSergey Karamov 				 "instead of data journaling mode");
423673b92a2aSSergey Karamov 		}
423756889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
423856889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4239001e4a87STejun Heo 	} else {
4240001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
424156889787STheodore Ts'o 	}
424256889787STheodore Ts'o 
42431751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
42441751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4245ac27a0ecSDave Kleikamp 
4246617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4247e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4248e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4249e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4250b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4251b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4252b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4253469108ffSTheodore Tso 
4254ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4255ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4256e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4257ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4258ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4259ed3654ebSTheodore Ts'o 			goto failed_mount;
4260ed3654ebSTheodore Ts'o 		}
4261dec214d0STahsin Erdogan 
4262dec214d0STahsin Erdogan 		/*
4263dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4264dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4265dec214d0STahsin Erdogan 		 */
4266dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4267dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4268dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4269dec214d0STahsin Erdogan 			goto failed_mount;
4270dec214d0STahsin Erdogan 		}
4271ed3654ebSTheodore Ts'o 	}
4272ed3654ebSTheodore Ts'o 
42732035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
42742035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
42752035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
42762035e776STheodore Ts'o 				 "using the ext4 subsystem");
42772035e776STheodore Ts'o 		else {
42780d9366d6SEric Sandeen 			/*
42790d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
42800d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
42810d9366d6SEric Sandeen 			 */
42820d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
42830d9366d6SEric Sandeen 				goto failed_mount;
42842035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
42852035e776STheodore Ts'o 				 "to feature incompatibilities");
42862035e776STheodore Ts'o 			goto failed_mount;
42872035e776STheodore Ts'o 		}
42882035e776STheodore Ts'o 	}
42892035e776STheodore Ts'o 
42902035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
42912035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
42922035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
42932035e776STheodore Ts'o 				 "using the ext4 subsystem");
42942035e776STheodore Ts'o 		else {
42950d9366d6SEric Sandeen 			/*
42960d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
42970d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
42980d9366d6SEric Sandeen 			 */
42990d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
43000d9366d6SEric Sandeen 				goto failed_mount;
43012035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
43022035e776STheodore Ts'o 				 "to feature incompatibilities");
43032035e776STheodore Ts'o 			goto failed_mount;
43042035e776STheodore Ts'o 		}
43052035e776STheodore Ts'o 	}
43062035e776STheodore Ts'o 
4307469108ffSTheodore Tso 	/*
4308ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4309ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4310ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4311ac27a0ecSDave Kleikamp 	 */
4312bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4313ac27a0ecSDave Kleikamp 		goto failed_mount;
4314a13fb1a4SEric Sandeen 
43158cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
43168cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
43178cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
43188cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
43198cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4320ac27a0ecSDave Kleikamp 		goto failed_mount;
4321ac27a0ecSDave Kleikamp 	}
4322bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4323bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4324bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4325bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4326bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4327bfe0a5f4STheodore Ts'o 		goto failed_mount;
4328bfe0a5f4STheodore Ts'o 	}
4329ac27a0ecSDave Kleikamp 
43305b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
43315b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
43325b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
43335b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
43345b9554dcSTheodore Ts'o 		goto failed_mount;
43355b9554dcSTheodore Ts'o 	}
43365b9554dcSTheodore Ts'o 
4337a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4338a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4339a8ab6d38SIra Weiny 
4340fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4341559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4342559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4343559db4c6SRoss Zwisler 					" that may contain inline data");
4344361d24d4SEric Sandeen 			goto failed_mount;
4345559db4c6SRoss Zwisler 		}
4346a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
434724f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4348361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4349361d24d4SEric Sandeen 			goto failed_mount;
435024f3478dSDan Williams 		}
4351923ae0ffSRoss Zwisler 	}
4352923ae0ffSRoss Zwisler 
4353e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
43546ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
43556ddb2447STheodore Ts'o 			 es->s_encryption_level);
43566ddb2447STheodore Ts'o 		goto failed_mount;
43576ddb2447STheodore Ts'o 	}
43586ddb2447STheodore Ts'o 
4359ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4360ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4361ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4362b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4363ce40733cSAneesh Kumar K.V 					blocksize);
4364ac27a0ecSDave Kleikamp 			goto failed_mount;
4365ac27a0ecSDave Kleikamp 		}
4366ac27a0ecSDave Kleikamp 
4367ac27a0ecSDave Kleikamp 		brelse(bh);
436870bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
436970bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
43708394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
43718394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4372b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4373b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
43748394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
43758394a6abSzhangyi (F) 			bh = NULL;
4376ac27a0ecSDave Kleikamp 			goto failed_mount;
4377ac27a0ecSDave Kleikamp 		}
43782716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4379ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4380617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4381b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4382b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4383ac27a0ecSDave Kleikamp 			goto failed_mount;
4384ac27a0ecSDave Kleikamp 		}
4385ac27a0ecSDave Kleikamp 	}
4386ac27a0ecSDave Kleikamp 
4387e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4388f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4389f287a1a5STheodore Ts'o 						      has_huge_files);
4390f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4391ac27a0ecSDave Kleikamp 
43920d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4393e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
43948fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
43950d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4396d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4397b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4398b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
43990d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
44000d1ee42fSAlexandre Ratchov 			goto failed_mount;
44010d1ee42fSAlexandre Ratchov 		}
44020d1ee42fSAlexandre Ratchov 	} else
44030d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
44040b8e58a1SAndreas Dilger 
4405ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4406ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
44070b8e58a1SAndreas Dilger 
4408617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4409ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4410617ba13bSMingming Cao 		goto cantfind_ext4;
4411cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4412cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4413cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4414b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4415cd6bb35bSTheodore Ts'o 		goto failed_mount;
4416cd6bb35bSTheodore Ts'o 	}
4417ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4418ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
44190d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4420ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4421ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4422e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4423e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
44240b8e58a1SAndreas Dilger 
4425ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4426ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4427ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4428e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4429f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4430f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4431f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4432f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4433f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4434bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
443523301410STheodore Ts'o 				es->s_flags |=
443623301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4437f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4438f99b2589STheodore Ts'o #else
4439bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
444023301410STheodore Ts'o 				es->s_flags |=
444123301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4442f99b2589STheodore Ts'o #endif
4443f99b2589STheodore Ts'o 		}
444423301410STheodore Ts'o 	}
4445ac27a0ecSDave Kleikamp 
4446281b5995STheodore Ts'o 	/* Handle clustersize */
4447281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4448ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4449281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4450281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4451281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4452281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4453281b5995STheodore Ts'o 			goto failed_mount;
4454281b5995STheodore Ts'o 		}
4455281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4456281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4457281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4458281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4459281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4460281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4461281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4462281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4463281b5995STheodore Ts'o 			goto failed_mount;
4464281b5995STheodore Ts'o 		}
4465281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4466281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4467281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4468281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4469281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4470281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4471281b5995STheodore Ts'o 			goto failed_mount;
4472281b5995STheodore Ts'o 		}
4473281b5995STheodore Ts'o 	} else {
4474281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4475bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4476bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4477bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4478bfe0a5f4STheodore Ts'o 			goto failed_mount;
4479281b5995STheodore Ts'o 		}
4480ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4481b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4482b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4483ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4484ac27a0ecSDave Kleikamp 			goto failed_mount;
4485ac27a0ecSDave Kleikamp 		}
4486281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4487281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4488281b5995STheodore Ts'o 	}
4489281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4490281b5995STheodore Ts'o 
4491960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4492960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4493960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4494960fd856STheodore Ts'o 
4495bf43d84bSEric Sandeen 	/*
4496bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4497bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4498bf43d84bSEric Sandeen 	 */
44995a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
450030ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
45015a9ae68aSDarrick J. Wong 	if (err) {
4502b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4503bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4504ac27a0ecSDave Kleikamp 		goto failed_mount;
4505ac27a0ecSDave Kleikamp 	}
4506ac27a0ecSDave Kleikamp 
4507617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4508617ba13bSMingming Cao 		goto cantfind_ext4;
4509e7c95593SEric Sandeen 
45100f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
45110f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
45120f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4513b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4514b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
45150f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
45160f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
45170f2ddca6SFrom: Thiemo Nagel 	}
45180f2ddca6SFrom: Thiemo Nagel 
45194ec11028STheodore Ts'o 	/*
45204ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
45214ec11028STheodore Ts'o 	 * of the filesystem.
45224ec11028STheodore Ts'o 	 */
45234ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4524b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4525b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4526e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
45274ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4528e7c95593SEric Sandeen 		goto failed_mount;
4529e7c95593SEric Sandeen 	}
4530bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4531bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4532bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4533bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4534bfe0a5f4STheodore Ts'o 		goto failed_mount;
4535bfe0a5f4STheodore Ts'o 	}
4536bfe0a5f4STheodore Ts'o 
4537bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4538bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4539bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4540bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
45414ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4542df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
45434ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4544df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
45454ec11028STheodore Ts'o 		       ext4_blocks_count(es),
45464ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
45474ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
45484ec11028STheodore Ts'o 		goto failed_mount;
45494ec11028STheodore Ts'o 	}
4550bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4551fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4552fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
45539e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
45549e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
45559e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
45569e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
45579e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
45589e463084STheodore Ts'o 		ret = -EINVAL;
45599e463084STheodore Ts'o 		goto failed_mount;
45609e463084STheodore Ts'o 	}
4561617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4562617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
45633a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
45642ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
45653a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
45663a4b77cdSEryu Guan 				 "first meta block group too large: %u "
45673a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
45683a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
45693a4b77cdSEryu Guan 			goto failed_mount;
45703a4b77cdSEryu Guan 		}
45713a4b77cdSEryu Guan 	}
45721d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
45731d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4574f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
45751d0c3924STheodore Ts'o 					  GFP_KERNEL));
4576ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4577b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
45782cde417dSTheodore Ts'o 		ret = -ENOMEM;
4579ac27a0ecSDave Kleikamp 		goto failed_mount;
4580ac27a0ecSDave Kleikamp 	}
4581ac27a0ecSDave Kleikamp 
4582705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4583ac27a0ecSDave Kleikamp 
458485c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
458585c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
458685c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
45875df1d412Szhangyi (F) 		ext4_sb_breadahead_unmovable(sb, block);
458885c8f176SAndrew Perepechko 	}
458985c8f176SAndrew Perepechko 
4590ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
45911d0c3924STheodore Ts'o 		struct buffer_head *bh;
45921d0c3924STheodore Ts'o 
459370bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
45948394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, block);
45958394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4596b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4597b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4598ac27a0ecSDave Kleikamp 			db_count = i;
45998394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
46008394a6abSzhangyi (F) 			bh = NULL;
4601ac27a0ecSDave Kleikamp 			goto failed_mount2;
4602ac27a0ecSDave Kleikamp 		}
46031d0c3924STheodore Ts'o 		rcu_read_lock();
46041d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
46051d0c3924STheodore Ts'o 		rcu_read_unlock();
4606ac27a0ecSDave Kleikamp 	}
460744de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4608829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4609b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
46106a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4611f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4612ac27a0ecSDave Kleikamp 	}
4613772cb7c8SJose R. Santos 
4614235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
461504496411STao Ma 
4616a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4617eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4618ce7e010aSTheodore Ts'o 		goto failed_mount3;
4619ce7e010aSTheodore Ts'o 
4620c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
462167a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4622c9de560dSAlex Tomas 
4623f9ae9cf5STheodore Ts'o 	/*
4624f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4625f9ae9cf5STheodore Ts'o 	 */
4626f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4627617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4628617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4629643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4630a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4631ffcc4182SEric Biggers #endif
4632c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4633c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4634c93d8f88SEric Biggers #endif
4635ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4636617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4637e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
46381fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4639262b4662SJan Kara 	else
4640262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4641689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4642ac27a0ecSDave Kleikamp #endif
464385787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4644f2fa2ffcSAneesh Kumar K.V 
4645ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
46463b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4647ac27a0ecSDave Kleikamp 
4648ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4649ac27a0ecSDave Kleikamp 
4650ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4651e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4652ac27a0ecSDave Kleikamp 
4653bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4654c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
465550460fe8SDarrick J. Wong 			goto failed_mount3a;
4656c5e06d10SJohann Lombardi 
4657ac27a0ecSDave Kleikamp 	/*
4658ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4659ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4660ac27a0ecSDave Kleikamp 	 */
4661e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
46624753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
46634753d8a2STheodore Ts'o 		if (err)
466450460fe8SDarrick J. Wong 			goto failed_mount3a;
4665bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4666e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4667b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4668b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4669744692dcSJiaying Zhang 		goto failed_mount_wq;
4670ac27a0ecSDave Kleikamp 	} else {
46711e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
46721e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
46731e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
46741e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
46751e381f60SDmitry Monakhov 			goto failed_mount_wq;
46761e381f60SDmitry Monakhov 		}
46771e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
46781e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
46791e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
46801e381f60SDmitry Monakhov 			goto failed_mount_wq;
46811e381f60SDmitry Monakhov 		}
46821e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
46831e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
46841e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
46851e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
46861e381f60SDmitry Monakhov 			goto failed_mount_wq;
46871e381f60SDmitry Monakhov 		}
46881e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
46891e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
46901e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
46911e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
46921e381f60SDmitry Monakhov 			goto failed_mount_wq;
46931e381f60SDmitry Monakhov 		}
469450b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
46951e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4696fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
46970390131bSFrank Mayhar 		sbi->s_journal = NULL;
46980390131bSFrank Mayhar 		needs_recovery = 0;
46990390131bSFrank Mayhar 		goto no_journal;
4700ac27a0ecSDave Kleikamp 	}
4701ac27a0ecSDave Kleikamp 
4702e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4703eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4704eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4705b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4706744692dcSJiaying Zhang 		goto failed_mount_wq;
4707eb40a09cSJose R. Santos 	}
4708eb40a09cSJose R. Santos 
470925ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
471025ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
471125ed6e8aSDarrick J. Wong 			 "feature set");
471225ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4713d4da6c9cSLinus Torvalds 	}
4714818d276cSGirish Shilamkar 
4715ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4716ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4717ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4718ac27a0ecSDave Kleikamp 	case 0:
4719ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
472063f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
472163f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
472263f57933SAndrew Morton 		 */
4723dab291afSMingming Cao 		if (jbd2_journal_check_available_features
472427f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4725fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
472627f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
472727f394a7STyson Nottingham 		} else {
4728fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
472927f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
473027f394a7STyson Nottingham 		}
4731ac27a0ecSDave Kleikamp 		break;
4732ac27a0ecSDave Kleikamp 
4733617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4734617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4735dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4736dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4737b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4738b31e1552SEric Sandeen 			       "requested data journaling mode");
4739744692dcSJiaying Zhang 			goto failed_mount_wq;
4740ac27a0ecSDave Kleikamp 		}
4741ac27a0ecSDave Kleikamp 	default:
4742ac27a0ecSDave Kleikamp 		break;
4743ac27a0ecSDave Kleikamp 	}
4744ab04df78SJan Kara 
4745ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4746ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4747ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4748ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4749ab04df78SJan Kara 		goto failed_mount_wq;
4750ab04df78SJan Kara 	}
4751ab04df78SJan Kara 
4752b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4753ac27a0ecSDave Kleikamp 
475418aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
4755*342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_submit_inode_data_buffers =
4756*342af94eSMauricio Faria de Oliveira 		jbd2_journal_submit_inode_data_buffers;
4757*342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_finish_inode_data_buffers =
4758*342af94eSMauricio Faria de Oliveira 		jbd2_journal_finish_inode_data_buffers;
475918aadd47SBobi Jam 
4760ce7e010aSTheodore Ts'o no_journal:
4761cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
476247387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
476347387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4764cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4765cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
47669c191f70ST Makphaibulchoke 			goto failed_mount_wq;
47679c191f70ST Makphaibulchoke 		}
47689c191f70ST Makphaibulchoke 
4769dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4770dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4771dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4772dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4773dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4774dec214d0STahsin Erdogan 				goto failed_mount_wq;
4775dec214d0STahsin Erdogan 			}
4776dec214d0STahsin Erdogan 		}
4777cdb7ee4cSTahsin Erdogan 	}
4778dec214d0STahsin Erdogan 
4779c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4780c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4781c93d8f88SEric Biggers 		goto failed_mount_wq;
4782c93d8f88SEric Biggers 	}
4783c93d8f88SEric Biggers 
4784bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4785e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4786e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
47876ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
47886ddb2447STheodore Ts'o 	}
47896ddb2447STheodore Ts'o 
4790fd89d5f2STejun Heo 	/*
4791952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4792952fc18eSTheodore Ts'o 	 * superblock if present.
4793952fc18eSTheodore Ts'o 	 */
4794952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4795952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4796952fc18eSTheodore Ts'o 	else {
479707aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
479807aa2ea1SLukas Czerner 		if (err)
4799952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4800952fc18eSTheodore Ts'o 	}
4801952fc18eSTheodore Ts'o 
4802952fc18eSTheodore Ts'o 	/*
4803fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4804fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4805fd89d5f2STejun Heo 	 */
48062e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
48072e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
48082e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
48092e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
481007aa2ea1SLukas Czerner 		ret = -ENOMEM;
48112e8fa54eSJan Kara 		goto failed_mount4;
48122e8fa54eSJan Kara 	}
48132e8fa54eSJan Kara 
4814ac27a0ecSDave Kleikamp 	/*
4815dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4816ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4817ac27a0ecSDave Kleikamp 	 */
4818ac27a0ecSDave Kleikamp 
48198a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
48201d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4821b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
48221d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
482332a9bb57SManish Katiyar 		root = NULL;
4824ac27a0ecSDave Kleikamp 		goto failed_mount4;
4825ac27a0ecSDave Kleikamp 	}
4826ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4827b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
482894bf608aSAl Viro 		iput(root);
4829ac27a0ecSDave Kleikamp 		goto failed_mount4;
4830ac27a0ecSDave Kleikamp 	}
4831b886ee3eSGabriel Krisman Bertazi 
4832b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4833b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4834b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4835b886ee3eSGabriel Krisman Bertazi #endif
4836b886ee3eSGabriel Krisman Bertazi 
483748fde701SAl Viro 	sb->s_root = d_make_root(root);
48381d1fe1eeSDavid Howells 	if (!sb->s_root) {
4839b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
48401d1fe1eeSDavid Howells 		ret = -ENOMEM;
48411d1fe1eeSDavid Howells 		goto failed_mount4;
48421d1fe1eeSDavid Howells 	}
4843ac27a0ecSDave Kleikamp 
4844c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4845c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
48461751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4847c89128a0SJaegeuk Kim 		ret = 0;
4848c89128a0SJaegeuk Kim 	} else if (ret)
4849c89128a0SJaegeuk Kim 		goto failed_mount4a;
4850ef7f3835SKalpak Shah 
4851b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
485227dd4385SLukas Czerner 
48530f5bde1dSJan Kara 	if (test_opt(sb, BLOCK_VALIDITY)) {
48546fd058f7STheodore Ts'o 		err = ext4_setup_system_zone(sb);
48556fd058f7STheodore Ts'o 		if (err) {
4856b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
4857fbe845ddSCurt Wohlgemuth 				 "zone (%d)", err);
4858f9ae9cf5STheodore Ts'o 			goto failed_mount4a;
4859f9ae9cf5STheodore Ts'o 		}
48600f5bde1dSJan Kara 	}
4861f9ae9cf5STheodore Ts'o 
4862f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4863f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4864f9ae9cf5STheodore Ts'o 	if (err) {
4865f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4866f9ae9cf5STheodore Ts'o 			 err);
4867dcf2d804STao Ma 		goto failed_mount5;
4868c2774d84SAneesh Kumar K.V 	}
4869c2774d84SAneesh Kumar K.V 
4870d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4871d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4872d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
48734274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4874908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4875908c7f19STejun Heo 				  GFP_KERNEL);
4876d5e03cbbSTheodore Ts'o 	if (!err) {
4877d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4878d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
48794274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4880908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4881908c7f19STejun Heo 					  GFP_KERNEL);
4882d5e03cbbSTheodore Ts'o 	}
4883d5e03cbbSTheodore Ts'o 	if (!err)
4884d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4885908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4886d5e03cbbSTheodore Ts'o 	if (!err)
4887908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4888908c7f19STejun Heo 					  GFP_KERNEL);
4889c8585c6fSDaeho Jeong 	if (!err)
4890bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4891c8585c6fSDaeho Jeong 
4892d5e03cbbSTheodore Ts'o 	if (err) {
4893d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4894d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4895d5e03cbbSTheodore Ts'o 	}
4896d5e03cbbSTheodore Ts'o 
4897e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4898d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4899d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4900d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4901d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4902d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4903d5e03cbbSTheodore Ts'o 		}
4904d5e03cbbSTheodore Ts'o 
4905bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4906bfff6873SLukas Czerner 	if (err)
4907dcf2d804STao Ma 		goto failed_mount6;
4908bfff6873SLukas Czerner 
4909b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4910dcf2d804STao Ma 	if (err)
4911dcf2d804STao Ma 		goto failed_mount7;
49123197ebdbSTheodore Ts'o 
49139b2ff357SJan Kara #ifdef CONFIG_QUOTA
49149b2ff357SJan Kara 	/* Enable quota usage during mount. */
4915bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
49169b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
49179b2ff357SJan Kara 		if (err)
49189b2ff357SJan Kara 			goto failed_mount8;
49199b2ff357SJan Kara 	}
49209b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
49219b2ff357SJan Kara 
4922bc71726cSzhangyi (F) 	/*
4923bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
4924bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
4925bc71726cSzhangyi (F) 	 */
4926bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
4927bc71726cSzhangyi (F) 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
4928bc71726cSzhangyi (F) 				 &sbi->s_bdev_wb_err);
4929bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
4930617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4931617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4932617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
49330390131bSFrank Mayhar 	if (needs_recovery) {
4934b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
493511215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
493611215630SJan Kara 		if (err)
493711215630SJan Kara 			goto failed_mount8;
49380390131bSFrank Mayhar 	}
49390390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
49400390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
49410390131bSFrank Mayhar 			descr = " journalled data mode";
49420390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
49430390131bSFrank Mayhar 			descr = " ordered data mode";
49440390131bSFrank Mayhar 		else
49450390131bSFrank Mayhar 			descr = " writeback data mode";
49460390131bSFrank Mayhar 	} else
49470390131bSFrank Mayhar 		descr = "out journal";
49480390131bSFrank Mayhar 
494979add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
495079add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
495179add3a3SLukas Czerner 		if (!blk_queue_discard(q))
495279add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
495379add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
495479add3a3SLukas Czerner 				 "the device does not support discard");
495579add3a3SLukas Czerner 	}
495679add3a3SLukas Czerner 
4957e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4958d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
49595aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
49605aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
49615aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
49628b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4963ac27a0ecSDave Kleikamp 
496466e61a9eSTheodore Ts'o 	if (es->s_error_count)
496566e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4966ac27a0ecSDave Kleikamp 
4967efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4968efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4969efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4970efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
49711cf006edSDmitry Monakhov 	atomic_set(&sbi->s_warning_count, 0);
49721cf006edSDmitry Monakhov 	atomic_set(&sbi->s_msg_count, 0);
4973efbed4dcSTheodore Ts'o 
4974d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4975ac27a0ecSDave Kleikamp 	return 0;
4976ac27a0ecSDave Kleikamp 
4977617ba13bSMingming Cao cantfind_ext4:
4978ac27a0ecSDave Kleikamp 	if (!silent)
4979b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4980ac27a0ecSDave Kleikamp 	goto failed_mount;
4981ac27a0ecSDave Kleikamp 
498272ba7450STheodore Ts'o failed_mount8:
4983ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
4984cb8d53d2SEric Biggers 	kobject_put(&sbi->s_kobj);
4985dcf2d804STao Ma failed_mount7:
4986dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4987dcf2d804STao Ma failed_mount6:
4988f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
49897c990728SSuraj Jitindar Singh 	rcu_read_lock();
49907c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
49917c990728SSuraj Jitindar Singh 	if (flex_groups) {
49927c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
49937c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
49947c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
49957c990728SSuraj Jitindar Singh 	}
49967c990728SSuraj Jitindar Singh 	rcu_read_unlock();
4997d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4998d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4999d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
5000d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5001bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
500200764937SAzat Khuzhin failed_mount5:
5003f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
5004f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
5005f9ae9cf5STheodore Ts'o failed_mount4a:
500694bf608aSAl Viro 	dput(sb->s_root);
500732a9bb57SManish Katiyar 	sb->s_root = NULL;
500894bf608aSAl Viro failed_mount4:
5009b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
50102e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
50112e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
50124c0425ffSMingming Cao failed_mount_wq:
5013dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5014dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
501550c15df6SChengguang Xu 
501647387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
501747387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
501850c15df6SChengguang Xu 
50190390131bSFrank Mayhar 	if (sbi->s_journal) {
5020dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
502147b4a50bSJan Kara 		sbi->s_journal = NULL;
50220390131bSFrank Mayhar 	}
502350460fe8SDarrick J. Wong failed_mount3a:
5024d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
5025eb68d0e2SZheng Liu failed_mount3:
50269105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
5027c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
5028c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
5029ac27a0ecSDave Kleikamp failed_mount2:
50301d0c3924STheodore Ts'o 	rcu_read_lock();
50311d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
5032ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
50331d0c3924STheodore Ts'o 		brelse(group_desc[i]);
50341d0c3924STheodore Ts'o 	kvfree(group_desc);
50351d0c3924STheodore Ts'o 	rcu_read_unlock();
5036ac27a0ecSDave Kleikamp failed_mount:
50370441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
50380441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
5039c83ad55eSGabriel Krisman Bertazi 
5040c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
5041c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
5042c83ad55eSGabriel Krisman Bertazi #endif
5043c83ad55eSGabriel Krisman Bertazi 
5044ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5045a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
50460ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
5047ac27a0ecSDave Kleikamp #endif
5048ed318a6cSEric Biggers 	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
5049617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
5050ac27a0ecSDave Kleikamp 	brelse(bh);
5051ac27a0ecSDave Kleikamp out_fail:
5052ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
5053f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
50545aee0f8aSTheodore Ts'o out_free_base:
5055ac27a0ecSDave Kleikamp 	kfree(sbi);
5056d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
50575e405595SDan Williams 	fs_put_dax(dax_dev);
505807aa2ea1SLukas Czerner 	return err ? err : ret;
5059ac27a0ecSDave Kleikamp }
5060ac27a0ecSDave Kleikamp 
5061ac27a0ecSDave Kleikamp /*
5062ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
5063ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
5064ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
5065ac27a0ecSDave Kleikamp  */
5066617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5067ac27a0ecSDave Kleikamp {
5068617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5069ac27a0ecSDave Kleikamp 
5070ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
507130773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
507230773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
5073ac27a0ecSDave Kleikamp 
5074a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
5075ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
5076dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
5077ac27a0ecSDave Kleikamp 	else
5078dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
50795bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
50805bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
50815bf5683aSHidehiro Kawai 	else
50825bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5083a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
5084ac27a0ecSDave Kleikamp }
5085ac27a0ecSDave Kleikamp 
5086c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
5087ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
5088ac27a0ecSDave Kleikamp {
5089ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
5090ac27a0ecSDave Kleikamp 
5091c6cb7e77SEric Whitney 	/*
5092c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
5093c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
5094c6cb7e77SEric Whitney 	 * will try to delete it.
5095c6cb7e77SEric Whitney 	 */
50968a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
50971d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
5098b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
5099ac27a0ecSDave Kleikamp 		return NULL;
5100ac27a0ecSDave Kleikamp 	}
5101ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
5102ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
5103ac27a0ecSDave Kleikamp 		iput(journal_inode);
5104b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5105ac27a0ecSDave Kleikamp 		return NULL;
5106ac27a0ecSDave Kleikamp 	}
5107ac27a0ecSDave Kleikamp 
5108e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5109ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
51101d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
5111b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5112ac27a0ecSDave Kleikamp 		iput(journal_inode);
5113ac27a0ecSDave Kleikamp 		return NULL;
5114ac27a0ecSDave Kleikamp 	}
5115c6cb7e77SEric Whitney 	return journal_inode;
5116c6cb7e77SEric Whitney }
5117c6cb7e77SEric Whitney 
5118c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
5119c6cb7e77SEric Whitney 				   unsigned int journal_inum)
5120c6cb7e77SEric Whitney {
5121c6cb7e77SEric Whitney 	struct inode *journal_inode;
5122c6cb7e77SEric Whitney 	journal_t *journal;
5123c6cb7e77SEric Whitney 
512411215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
512511215630SJan Kara 		return NULL;
5126c6cb7e77SEric Whitney 
5127c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5128c6cb7e77SEric Whitney 	if (!journal_inode)
5129c6cb7e77SEric Whitney 		return NULL;
5130ac27a0ecSDave Kleikamp 
5131dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
5132ac27a0ecSDave Kleikamp 	if (!journal) {
5133b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5134ac27a0ecSDave Kleikamp 		iput(journal_inode);
5135ac27a0ecSDave Kleikamp 		return NULL;
5136ac27a0ecSDave Kleikamp 	}
5137ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5138617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5139ac27a0ecSDave Kleikamp 	return journal;
5140ac27a0ecSDave Kleikamp }
5141ac27a0ecSDave Kleikamp 
5142617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
5143ac27a0ecSDave Kleikamp 				       dev_t j_dev)
5144ac27a0ecSDave Kleikamp {
5145ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5146ac27a0ecSDave Kleikamp 	journal_t *journal;
5147617ba13bSMingming Cao 	ext4_fsblk_t start;
5148617ba13bSMingming Cao 	ext4_fsblk_t len;
5149ac27a0ecSDave Kleikamp 	int hblock, blocksize;
5150617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
5151ac27a0ecSDave Kleikamp 	unsigned long offset;
5152617ba13bSMingming Cao 	struct ext4_super_block *es;
5153ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5154ac27a0ecSDave Kleikamp 
515511215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
515611215630SJan Kara 		return NULL;
51570390131bSFrank Mayhar 
5158b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5159ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5160ac27a0ecSDave Kleikamp 		return NULL;
5161ac27a0ecSDave Kleikamp 
5162ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5163e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5164ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5165b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5166b31e1552SEric Sandeen 			"blocksize too small for journal device");
5167ac27a0ecSDave Kleikamp 		goto out_bdev;
5168ac27a0ecSDave Kleikamp 	}
5169ac27a0ecSDave Kleikamp 
5170617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5171617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5172ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5173ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5174b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5175b31e1552SEric Sandeen 		       "external journal");
5176ac27a0ecSDave Kleikamp 		goto out_bdev;
5177ac27a0ecSDave Kleikamp 	}
5178ac27a0ecSDave Kleikamp 
51792716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5180617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5181ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5182617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5183b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5184b31e1552SEric Sandeen 					"bad superblock");
5185ac27a0ecSDave Kleikamp 		brelse(bh);
5186ac27a0ecSDave Kleikamp 		goto out_bdev;
5187ac27a0ecSDave Kleikamp 	}
5188ac27a0ecSDave Kleikamp 
5189df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5190df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5191df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5192df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5193df4763beSDarrick J. Wong 				       "corrupt superblock");
5194df4763beSDarrick J. Wong 		brelse(bh);
5195df4763beSDarrick J. Wong 		goto out_bdev;
5196df4763beSDarrick J. Wong 	}
5197df4763beSDarrick J. Wong 
5198617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5199b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5200ac27a0ecSDave Kleikamp 		brelse(bh);
5201ac27a0ecSDave Kleikamp 		goto out_bdev;
5202ac27a0ecSDave Kleikamp 	}
5203ac27a0ecSDave Kleikamp 
5204bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5205ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5206ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5207ac27a0ecSDave Kleikamp 
5208dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5209ac27a0ecSDave Kleikamp 					start, len, blocksize);
5210ac27a0ecSDave Kleikamp 	if (!journal) {
5211b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5212ac27a0ecSDave Kleikamp 		goto out_bdev;
5213ac27a0ecSDave Kleikamp 	}
5214ac27a0ecSDave Kleikamp 	journal->j_private = sb;
52152d069c08Szhangyi (F) 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5216b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5217ac27a0ecSDave Kleikamp 		goto out_journal;
5218ac27a0ecSDave Kleikamp 	}
5219ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5220b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5221b31e1552SEric Sandeen 					"user (unsupported) - %d",
5222ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5223ac27a0ecSDave Kleikamp 		goto out_journal;
5224ac27a0ecSDave Kleikamp 	}
5225ee7ed3aaSChunguang Xu 	EXT4_SB(sb)->s_journal_bdev = bdev;
5226617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5227ac27a0ecSDave Kleikamp 	return journal;
52280b8e58a1SAndreas Dilger 
5229ac27a0ecSDave Kleikamp out_journal:
5230dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5231ac27a0ecSDave Kleikamp out_bdev:
5232617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5233ac27a0ecSDave Kleikamp 	return NULL;
5234ac27a0ecSDave Kleikamp }
5235ac27a0ecSDave Kleikamp 
5236617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5237617ba13bSMingming Cao 			     struct ext4_super_block *es,
5238ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5239ac27a0ecSDave Kleikamp {
5240ac27a0ecSDave Kleikamp 	journal_t *journal;
5241ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5242ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5243ac27a0ecSDave Kleikamp 	int err = 0;
5244ac27a0ecSDave Kleikamp 	int really_read_only;
5245273108faSLukas Czerner 	int journal_dev_ro;
5246ac27a0ecSDave Kleikamp 
524711215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
524811215630SJan Kara 		return -EFSCORRUPTED;
52490390131bSFrank Mayhar 
5250ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5251ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5252b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5253b31e1552SEric Sandeen 			"numbers have changed");
5254ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5255ac27a0ecSDave Kleikamp 	} else
5256ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5257ac27a0ecSDave Kleikamp 
5258273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5259273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5260273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5261273108faSLukas Czerner 		return -EINVAL;
5262273108faSLukas Czerner 	}
5263273108faSLukas Czerner 
5264273108faSLukas Czerner 	if (journal_inum) {
5265273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5266273108faSLukas Czerner 		if (!journal)
5267273108faSLukas Czerner 			return -EINVAL;
5268273108faSLukas Czerner 	} else {
5269273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5270273108faSLukas Czerner 		if (!journal)
5271273108faSLukas Czerner 			return -EINVAL;
5272273108faSLukas Czerner 	}
5273273108faSLukas Czerner 
5274273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5275273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5276273108faSLukas Czerner 
5277273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5278273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5279273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5280273108faSLukas Czerner 		err = -EROFS;
5281273108faSLukas Czerner 		goto err_out;
5282273108faSLukas Czerner 	}
5283ac27a0ecSDave Kleikamp 
5284ac27a0ecSDave Kleikamp 	/*
5285ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5286ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5287ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5288ac27a0ecSDave Kleikamp 	 */
5289e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5290bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5291b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5292b31e1552SEric Sandeen 					"required on readonly filesystem");
5293ac27a0ecSDave Kleikamp 			if (really_read_only) {
5294b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5295d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5296d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5297273108faSLukas Czerner 				err = -EROFS;
5298273108faSLukas Czerner 				goto err_out;
5299ac27a0ecSDave Kleikamp 			}
5300b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5301b31e1552SEric Sandeen 			       "be enabled during recovery");
5302ac27a0ecSDave Kleikamp 		}
5303ac27a0ecSDave Kleikamp 	}
5304ac27a0ecSDave Kleikamp 
530590576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5306b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
53074776004fSTheodore Ts'o 
5308e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5309dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
53101c13d5c0STheodore Ts'o 	if (!err) {
53111c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
53121c13d5c0STheodore Ts'o 		if (save)
53131c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
53141c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5315dab291afSMingming Cao 		err = jbd2_journal_load(journal);
53161c13d5c0STheodore Ts'o 		if (save)
53171c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
53181c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
53191c13d5c0STheodore Ts'o 		kfree(save);
53201c13d5c0STheodore Ts'o 	}
5321ac27a0ecSDave Kleikamp 
5322ac27a0ecSDave Kleikamp 	if (err) {
5323b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5324273108faSLukas Czerner 		goto err_out;
5325ac27a0ecSDave Kleikamp 	}
5326ac27a0ecSDave Kleikamp 
5327617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
532811215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
532911215630SJan Kara 	if (err) {
533011215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
533111215630SJan Kara 		jbd2_journal_destroy(journal);
533211215630SJan Kara 		return err;
533311215630SJan Kara 	}
5334ac27a0ecSDave Kleikamp 
5335c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5336ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5337ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5338ac27a0ecSDave Kleikamp 
5339ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5340e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5341ac27a0ecSDave Kleikamp 	}
5342ac27a0ecSDave Kleikamp 
5343ac27a0ecSDave Kleikamp 	return 0;
5344273108faSLukas Czerner 
5345273108faSLukas Czerner err_out:
5346273108faSLukas Czerner 	jbd2_journal_destroy(journal);
5347273108faSLukas Czerner 	return err;
5348ac27a0ecSDave Kleikamp }
5349ac27a0ecSDave Kleikamp 
5350e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5351ac27a0ecSDave Kleikamp {
5352e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5353617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5354c4be0c1dSTakashi Sato 	int error = 0;
5355ac27a0ecSDave Kleikamp 
5356bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5357c4be0c1dSTakashi Sato 		return error;
5358a17712c8SJon Derrick 
5359a17712c8SJon Derrick 	/*
536071290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
536171290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
536271290b36STheodore Ts'o 	 * write time when we are mounting the root file system
536371290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
536471290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
536571290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
536671290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
536771290b36STheodore Ts'o 	 * to complain and force a full file system check.
536871290b36STheodore Ts'o 	 */
53691751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
53706a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5371f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5372afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5373afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5374dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5375dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5376afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5377f613dfcbSTheodore Ts'o 	else
5378f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5379f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5380d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
538157042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
538257042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
538357042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5384d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
53857f93cff9STheodore Ts'o 		es->s_free_inodes_count =
53867f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
53875d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5388ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
538906db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
53901566a48aSTheodore Ts'o 	if (sync)
53914743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5392e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
53934743f839SPranay Kr. Srivastava 		/*
53944743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
53954743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
53964743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
53974743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
53984743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
53994743f839SPranay Kr. Srivastava 		 * write and hope for the best.
54004743f839SPranay Kr. Srivastava 		 */
54014743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
54024743f839SPranay Kr. Srivastava 		       "superblock detected");
54034743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
54044743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
54054743f839SPranay Kr. Srivastava 	}
5406ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5407914258bfSTheodore Ts'o 	if (sync) {
54081566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5409564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
541000473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5411c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5412b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5413b31e1552SEric Sandeen 			       "superblock");
5414914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5415914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5416914258bfSTheodore Ts'o 		}
5417914258bfSTheodore Ts'o 	}
5418c4be0c1dSTakashi Sato 	return error;
5419ac27a0ecSDave Kleikamp }
5420ac27a0ecSDave Kleikamp 
5421ac27a0ecSDave Kleikamp /*
5422ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5423ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5424ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5425ac27a0ecSDave Kleikamp  */
542611215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
5427617ba13bSMingming Cao 				       struct ext4_super_block *es)
5428ac27a0ecSDave Kleikamp {
542911215630SJan Kara 	int err;
5430617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5431ac27a0ecSDave Kleikamp 
5432e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
543311215630SJan Kara 		if (journal != NULL) {
543411215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
543511215630SJan Kara 				   "mounted!");
543611215630SJan Kara 			return -EFSCORRUPTED;
543711215630SJan Kara 		}
543811215630SJan Kara 		return 0;
54390390131bSFrank Mayhar 	}
5440dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
544111215630SJan Kara 	err = jbd2_journal_flush(journal);
544211215630SJan Kara 	if (err < 0)
54437ffe1ea8SHidehiro Kawai 		goto out;
54447ffe1ea8SHidehiro Kawai 
5445bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5446e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5447e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5448ac27a0ecSDave Kleikamp 	}
54497ffe1ea8SHidehiro Kawai out:
5450dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
545111215630SJan Kara 	return err;
5452ac27a0ecSDave Kleikamp }
5453ac27a0ecSDave Kleikamp 
5454ac27a0ecSDave Kleikamp /*
5455ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5456ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5457ac27a0ecSDave Kleikamp  * main filesystem now.
5458ac27a0ecSDave Kleikamp  */
545911215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
5460617ba13bSMingming Cao 				   struct ext4_super_block *es)
5461ac27a0ecSDave Kleikamp {
5462ac27a0ecSDave Kleikamp 	journal_t *journal;
5463ac27a0ecSDave Kleikamp 	int j_errno;
5464ac27a0ecSDave Kleikamp 	const char *errstr;
5465ac27a0ecSDave Kleikamp 
546611215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
546711215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
546811215630SJan Kara 		return -EFSCORRUPTED;
546911215630SJan Kara 	}
54700390131bSFrank Mayhar 
5471617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5472ac27a0ecSDave Kleikamp 
5473ac27a0ecSDave Kleikamp 	/*
5474ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5475617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5476ac27a0ecSDave Kleikamp 	 */
5477ac27a0ecSDave Kleikamp 
5478dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5479ac27a0ecSDave Kleikamp 	if (j_errno) {
5480ac27a0ecSDave Kleikamp 		char nbuf[16];
5481ac27a0ecSDave Kleikamp 
5482617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
548312062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5484ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
548512062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5486ac27a0ecSDave Kleikamp 
5487617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5488617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5489e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5490ac27a0ecSDave Kleikamp 
5491dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5492d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5493ac27a0ecSDave Kleikamp 	}
549411215630SJan Kara 	return 0;
5495ac27a0ecSDave Kleikamp }
5496ac27a0ecSDave Kleikamp 
5497ac27a0ecSDave Kleikamp /*
5498ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5499ac27a0ecSDave Kleikamp  * and wait on the commit.
5500ac27a0ecSDave Kleikamp  */
5501617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5502ac27a0ecSDave Kleikamp {
5503ac27a0ecSDave Kleikamp 	journal_t *journal;
5504ac27a0ecSDave Kleikamp 
5505bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5506ac27a0ecSDave Kleikamp 		return 0;
5507ac27a0ecSDave Kleikamp 
5508617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5509b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5510ac27a0ecSDave Kleikamp }
5511ac27a0ecSDave Kleikamp 
5512617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5513ac27a0ecSDave Kleikamp {
551414ce0cb4STheodore Ts'o 	int ret = 0;
55159eddacf9SJan Kara 	tid_t target;
551606a407f1SDmitry Monakhov 	bool needs_barrier = false;
55178d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5518ac27a0ecSDave Kleikamp 
551949598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
55200db1ff22STheodore Ts'o 		return 0;
55210db1ff22STheodore Ts'o 
55229bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
55232e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5524a1177825SJan Kara 	/*
5525a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5526a1177825SJan Kara 	 * no dirty dquots
5527a1177825SJan Kara 	 */
5528a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
552906a407f1SDmitry Monakhov 	/*
553006a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
553106a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
553206a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
553306a407f1SDmitry Monakhov 	 */
5534bda32530STheodore Ts'o 	if (sbi->s_journal) {
553506a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
553606a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
553706a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
553806a407f1SDmitry Monakhov 			needs_barrier = true;
553906a407f1SDmitry Monakhov 
55408d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5541ac27a0ecSDave Kleikamp 			if (wait)
5542bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5543bda32530STheodore Ts'o 							   target);
55440390131bSFrank Mayhar 		}
5545bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5546bda32530STheodore Ts'o 		needs_barrier = true;
554706a407f1SDmitry Monakhov 	if (needs_barrier) {
554806a407f1SDmitry Monakhov 		int err;
55499398554fSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
555006a407f1SDmitry Monakhov 		if (!ret)
555106a407f1SDmitry Monakhov 			ret = err;
555206a407f1SDmitry Monakhov 	}
555306a407f1SDmitry Monakhov 
555406a407f1SDmitry Monakhov 	return ret;
555506a407f1SDmitry Monakhov }
555606a407f1SDmitry Monakhov 
5557ac27a0ecSDave Kleikamp /*
5558ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5559ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5560be4f27d3SYongqiang Yang  *
5561be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
55628e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
55638e8ad8a5SJan Kara  * modifications.
5564ac27a0ecSDave Kleikamp  */
5565c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5566ac27a0ecSDave Kleikamp {
5567c4be0c1dSTakashi Sato 	int error = 0;
5568c4be0c1dSTakashi Sato 	journal_t *journal;
5569ac27a0ecSDave Kleikamp 
5570bc98a42cSDavid Howells 	if (sb_rdonly(sb))
55719ca92389STheodore Ts'o 		return 0;
55729ca92389STheodore Ts'o 
5573c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5574ac27a0ecSDave Kleikamp 
5575bb044576STheodore Ts'o 	if (journal) {
5576ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5577dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
55787ffe1ea8SHidehiro Kawai 
55797ffe1ea8SHidehiro Kawai 		/*
5580bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5581bb044576STheodore Ts'o 		 * flush the journal.
55827ffe1ea8SHidehiro Kawai 		 */
5583c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
55846b0310fbSEric Sandeen 		if (error < 0)
55856b0310fbSEric Sandeen 			goto out;
5586ac27a0ecSDave Kleikamp 
5587ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5588e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5589c642dc9eSEric Sandeen 	}
5590c642dc9eSEric Sandeen 
5591e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
55926b0310fbSEric Sandeen out:
5593bb044576STheodore Ts'o 	if (journal)
55948e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5595bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
55966b0310fbSEric Sandeen 	return error;
5597ac27a0ecSDave Kleikamp }
5598ac27a0ecSDave Kleikamp 
5599ac27a0ecSDave Kleikamp /*
5600ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5601ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5602ac27a0ecSDave Kleikamp  */
5603c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5604ac27a0ecSDave Kleikamp {
5605bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
56069ca92389STheodore Ts'o 		return 0;
56079ca92389STheodore Ts'o 
5608c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
56099ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5610e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5611c642dc9eSEric Sandeen 	}
5612c642dc9eSEric Sandeen 
5613e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5614c4be0c1dSTakashi Sato 	return 0;
5615ac27a0ecSDave Kleikamp }
5616ac27a0ecSDave Kleikamp 
5617673c6100STheodore Ts'o /*
5618673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5619673c6100STheodore Ts'o  */
5620673c6100STheodore Ts'o struct ext4_mount_options {
5621673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5622a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
562308cefc7aSEric W. Biederman 	kuid_t s_resuid;
562408cefc7aSEric W. Biederman 	kgid_t s_resgid;
5625673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5626673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5627673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5628673c6100STheodore Ts'o 	int s_jquota_fmt;
5629a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5630673c6100STheodore Ts'o #endif
5631673c6100STheodore Ts'o };
5632673c6100STheodore Ts'o 
5633617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5634ac27a0ecSDave Kleikamp {
5635617ba13bSMingming Cao 	struct ext4_super_block *es;
5636617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5637f25391ebSLukas Czerner 	unsigned long old_sb_flags, vfs_flags;
5638617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5639c79d967dSChristoph Hellwig 	int enable_quota = 0;
56408a266467STheodore Ts'o 	ext4_group_t g;
5641b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5642c5e06d10SJohann Lombardi 	int err = 0;
5643ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
564403dafb5fSChen Gang 	int i, j;
564533458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5646ac27a0ecSDave Kleikamp #endif
5647d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5648ac27a0ecSDave Kleikamp 
564921ac738eSChengguang Xu 	if (data && !orig_data)
565021ac738eSChengguang Xu 		return -ENOMEM;
565121ac738eSChengguang Xu 
5652ac27a0ecSDave Kleikamp 	/* Store the original options */
5653ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5654ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5655a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5656ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5657ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5658ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
565930773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
566030773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5661ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5662ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5663a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
566403dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
566533458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
566633458eabSTheodore Ts'o 
566733458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
566803dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
566903dafb5fSChen Gang 				for (j = 0; j < i; j++)
567003dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
56713e36a163SWei Yongjun 				kfree(orig_data);
567203dafb5fSChen Gang 				return -ENOMEM;
567303dafb5fSChen Gang 			}
567403dafb5fSChen Gang 		} else
567503dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5676ac27a0ecSDave Kleikamp #endif
5677b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5678b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5679ac27a0ecSDave Kleikamp 
5680f25391ebSLukas Czerner 	/*
5681f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
5682f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
5683f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from *flags to s_flags
5684f25391ebSLukas Czerner 	 */
5685f25391ebSLukas Czerner 	vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5686f25391ebSLukas Czerner 	sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5687f25391ebSLukas Czerner 
5688661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5689ac27a0ecSDave Kleikamp 		err = -EINVAL;
5690ac27a0ecSDave Kleikamp 		goto restore_opts;
5691ac27a0ecSDave Kleikamp 	}
5692ac27a0ecSDave Kleikamp 
56936b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
56946b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
56956b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
56962d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
56972d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5698c6d3d56dSDarrick J. Wong 	}
5699c6d3d56dSDarrick J. Wong 
57006ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
57016ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
57026ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
57036ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
57046ae6514bSPiotr Sarna 			err = -EINVAL;
57056ae6514bSPiotr Sarna 			goto restore_opts;
57066ae6514bSPiotr Sarna 		}
57076ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
57086ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
57096ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
57106ae6514bSPiotr Sarna 			err = -EINVAL;
57116ae6514bSPiotr Sarna 			goto restore_opts;
57126ae6514bSPiotr Sarna 		}
5713ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5714ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5715ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5716ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5717ab04df78SJan Kara 			err = -EINVAL;
5718ab04df78SJan Kara 			goto restore_opts;
5719ab04df78SJan Kara 		}
5720923ae0ffSRoss Zwisler 	}
5721923ae0ffSRoss Zwisler 
5722cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5723cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5724cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5725cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5726cdb7ee4cSTahsin Erdogan 	}
5727cdb7ee4cSTahsin Erdogan 
57284ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
572954d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5730ac27a0ecSDave Kleikamp 
57311751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
57321751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5733ac27a0ecSDave Kleikamp 
5734ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5735ac27a0ecSDave Kleikamp 
5736b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5737617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5738b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5739b3881f74STheodore Ts'o 	}
5740ac27a0ecSDave Kleikamp 
57411751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
57424ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5743ac27a0ecSDave Kleikamp 			err = -EROFS;
5744ac27a0ecSDave Kleikamp 			goto restore_opts;
5745ac27a0ecSDave Kleikamp 		}
5746ac27a0ecSDave Kleikamp 
57471751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
574838c03b34STheodore Ts'o 			err = sync_filesystem(sb);
574938c03b34STheodore Ts'o 			if (err < 0)
575038c03b34STheodore Ts'o 				goto restore_opts;
57510f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
57520f0dd62fSChristoph Hellwig 			if (err < 0)
5753c79d967dSChristoph Hellwig 				goto restore_opts;
5754c79d967dSChristoph Hellwig 
5755ac27a0ecSDave Kleikamp 			/*
5756ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5757ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5758ac27a0ecSDave Kleikamp 			 */
57591751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5760ac27a0ecSDave Kleikamp 
5761ac27a0ecSDave Kleikamp 			/*
5762ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5763ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5764ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5765ac27a0ecSDave Kleikamp 			 */
5766617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5767617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5768ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5769ac27a0ecSDave Kleikamp 
577011215630SJan Kara 			if (sbi->s_journal) {
577111215630SJan Kara 				/*
577211215630SJan Kara 				 * We let remount-ro finish even if marking fs
577311215630SJan Kara 				 * as clean failed...
577411215630SJan Kara 				 */
5775617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
577611215630SJan Kara 			}
57772dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
57782dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5779ac27a0ecSDave Kleikamp 		} else {
5780a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5781e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
57822cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5783ac27a0ecSDave Kleikamp 				err = -EROFS;
5784ac27a0ecSDave Kleikamp 				goto restore_opts;
5785ac27a0ecSDave Kleikamp 			}
5786ead6596bSEric Sandeen 			/*
57878a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
57880b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
57898a266467STheodore Ts'o 			 */
57908a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
57918a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
57928a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
57938a266467STheodore Ts'o 
5794feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5795b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5796b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5797e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
57988a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
57996a797d27SDarrick J. Wong 					err = -EFSBADCRC;
58008a266467STheodore Ts'o 					goto restore_opts;
58018a266467STheodore Ts'o 				}
58028a266467STheodore Ts'o 			}
58038a266467STheodore Ts'o 
58048a266467STheodore Ts'o 			/*
5805ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5806ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5807ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5808ead6596bSEric Sandeen 			 */
5809ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5810b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5811ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5812ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5813b31e1552SEric Sandeen 				       "umount/remount instead");
5814ead6596bSEric Sandeen 				err = -EINVAL;
5815ead6596bSEric Sandeen 				goto restore_opts;
5816ead6596bSEric Sandeen 			}
5817ead6596bSEric Sandeen 
5818ac27a0ecSDave Kleikamp 			/*
5819ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5820ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5821ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5822ac27a0ecSDave Kleikamp 			 * the partition.)
5823ac27a0ecSDave Kleikamp 			 */
582411215630SJan Kara 			if (sbi->s_journal) {
582511215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
582611215630SJan Kara 				if (err)
582711215630SJan Kara 					goto restore_opts;
582811215630SJan Kara 			}
5829ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5830c89128a0SJaegeuk Kim 
5831c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5832c89128a0SJaegeuk Kim 			if (err)
5833c89128a0SJaegeuk Kim 				goto restore_opts;
5834c89128a0SJaegeuk Kim 
58351751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5836e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5837c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5838c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5839c5e06d10SJohann Lombardi 					err = -EROFS;
5840c5e06d10SJohann Lombardi 					goto restore_opts;
5841c5e06d10SJohann Lombardi 				}
5842c79d967dSChristoph Hellwig 			enable_quota = 1;
5843ac27a0ecSDave Kleikamp 		}
5844ac27a0ecSDave Kleikamp 	}
5845bfff6873SLukas Czerner 
5846bfff6873SLukas Czerner 	/*
5847bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5848bfff6873SLukas Czerner 	 * current settings
5849bfff6873SLukas Czerner 	 */
5850bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5851bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5852bfff6873SLukas Czerner 	else {
5853bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5854bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5855bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5856bfff6873SLukas Czerner 	}
5857bfff6873SLukas Czerner 
58580f5bde1dSJan Kara 	/*
58590f5bde1dSJan Kara 	 * Handle creation of system zone data early because it can fail.
58600f5bde1dSJan Kara 	 * Releasing of existing data is done when we are sure remount will
58610f5bde1dSJan Kara 	 * succeed.
58620f5bde1dSJan Kara 	 */
5863dd0db94fSChunguang Xu 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
5864d176b1f6SJan Kara 		err = ext4_setup_system_zone(sb);
5865d176b1f6SJan Kara 		if (err)
5866d176b1f6SJan Kara 			goto restore_opts;
58670f5bde1dSJan Kara 	}
5868d176b1f6SJan Kara 
5869c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5870c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5871c89128a0SJaegeuk Kim 		if (err)
5872c89128a0SJaegeuk Kim 			goto restore_opts;
5873c89128a0SJaegeuk Kim 	}
58740390131bSFrank Mayhar 
5875ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5876ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5877a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5878ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
58797c319d32SAditya Kali 	if (enable_quota) {
58807c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
58810f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5882e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
58837c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
588407724f98STheodore Ts'o 			if (err)
58857c319d32SAditya Kali 				goto restore_opts;
58867c319d32SAditya Kali 		}
58877c319d32SAditya Kali 	}
58887c319d32SAditya Kali #endif
5889dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
58900f5bde1dSJan Kara 		ext4_release_system_zone(sb);
5891d4c402d9SCurt Wohlgemuth 
5892f25391ebSLukas Czerner 	/*
5893f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
5894f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
5895f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from s_flags to *flags
5896f25391ebSLukas Czerner 	 */
5897f25391ebSLukas Czerner 	*flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
5898f25391ebSLukas Czerner 
5899d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5900d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5901ac27a0ecSDave Kleikamp 	return 0;
59020b8e58a1SAndreas Dilger 
5903ac27a0ecSDave Kleikamp restore_opts:
5904ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5905ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5906a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5907ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5908ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5909ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
591030773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
591130773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5912dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
59130f5bde1dSJan Kara 		ext4_release_system_zone(sb);
5914ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5915ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5916a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
591733458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
591833458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5919ac27a0ecSDave Kleikamp 	}
592033458eabSTheodore Ts'o 	synchronize_rcu();
592133458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
592233458eabSTheodore Ts'o 		kfree(to_free[i]);
5923ac27a0ecSDave Kleikamp #endif
5924d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5925ac27a0ecSDave Kleikamp 	return err;
5926ac27a0ecSDave Kleikamp }
5927ac27a0ecSDave Kleikamp 
5928689c958cSLi Xi #ifdef CONFIG_QUOTA
5929689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5930689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5931689c958cSLi Xi {
5932689c958cSLi Xi 	struct kqid qid;
5933689c958cSLi Xi 	struct dquot *dquot;
5934689c958cSLi Xi 	u64 limit;
5935689c958cSLi Xi 	u64 curblock;
5936689c958cSLi Xi 
5937689c958cSLi Xi 	qid = make_kqid_projid(projid);
5938689c958cSLi Xi 	dquot = dqget(sb, qid);
5939689c958cSLi Xi 	if (IS_ERR(dquot))
5940689c958cSLi Xi 		return PTR_ERR(dquot);
59417b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5942689c958cSLi Xi 
5943a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
5944a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
594557c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
594657c32ea4SChengguang Xu 
5947689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5948f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5949f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5950689c958cSLi Xi 		buf->f_blocks = limit;
5951689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5952689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5953689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5954689c958cSLi Xi 	}
5955689c958cSLi Xi 
5956a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
5957a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
5958689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5959689c958cSLi Xi 		buf->f_files = limit;
5960689c958cSLi Xi 		buf->f_ffree =
5961689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5962689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5963689c958cSLi Xi 	}
5964689c958cSLi Xi 
59657b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5966689c958cSLi Xi 	dqput(dquot);
5967689c958cSLi Xi 	return 0;
5968689c958cSLi Xi }
5969689c958cSLi Xi #endif
5970689c958cSLi Xi 
5971617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5972ac27a0ecSDave Kleikamp {
5973ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5974617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5975617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
597627dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5977960cc398SPekka Enberg 	u64 fsid;
5978d02a9391SKazuya Mio 	s64 bfree;
597927dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5980ac27a0ecSDave Kleikamp 
5981952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5982952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5983ac27a0ecSDave Kleikamp 
5984617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5985ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5986b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
598757042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
598857042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5989d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
599057042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
599127dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
599227dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
599327dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5994ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5995ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
599652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5997617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5998960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5999960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
6000960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
6001960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
60020b8e58a1SAndreas Dilger 
6003689c958cSLi Xi #ifdef CONFIG_QUOTA
6004689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6005689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6006689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6007689c958cSLi Xi #endif
6008ac27a0ecSDave Kleikamp 	return 0;
6009ac27a0ecSDave Kleikamp }
6010ac27a0ecSDave Kleikamp 
6011ac27a0ecSDave Kleikamp 
6012ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6013ac27a0ecSDave Kleikamp 
6014bc8230eeSJan Kara /*
6015bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
6016bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
6017bc8230eeSJan Kara  */
6018ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
6019ac27a0ecSDave Kleikamp {
60204c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6021ac27a0ecSDave Kleikamp }
6022ac27a0ecSDave Kleikamp 
6023617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
6024ac27a0ecSDave Kleikamp {
6025ac27a0ecSDave Kleikamp 	int ret, err;
6026ac27a0ecSDave Kleikamp 	handle_t *handle;
6027ac27a0ecSDave Kleikamp 	struct inode *inode;
6028ac27a0ecSDave Kleikamp 
6029ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
60309924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6031617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6032ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6033ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6034ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
6035617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6036ac27a0ecSDave Kleikamp 	if (!ret)
6037ac27a0ecSDave Kleikamp 		ret = err;
6038ac27a0ecSDave Kleikamp 	return ret;
6039ac27a0ecSDave Kleikamp }
6040ac27a0ecSDave Kleikamp 
6041617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
6042ac27a0ecSDave Kleikamp {
6043ac27a0ecSDave Kleikamp 	int ret, err;
6044ac27a0ecSDave Kleikamp 	handle_t *handle;
6045ac27a0ecSDave Kleikamp 
60469924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6047617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6048ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6049ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6050ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
6051617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6052ac27a0ecSDave Kleikamp 	if (!ret)
6053ac27a0ecSDave Kleikamp 		ret = err;
6054ac27a0ecSDave Kleikamp 	return ret;
6055ac27a0ecSDave Kleikamp }
6056ac27a0ecSDave Kleikamp 
6057617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
6058ac27a0ecSDave Kleikamp {
6059ac27a0ecSDave Kleikamp 	int ret, err;
6060ac27a0ecSDave Kleikamp 	handle_t *handle;
6061ac27a0ecSDave Kleikamp 
60629924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6063617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
60649c3013e9SJan Kara 	if (IS_ERR(handle)) {
60659c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
60669c3013e9SJan Kara 		dquot_release(dquot);
6067ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
60689c3013e9SJan Kara 	}
6069ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
6070617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6071ac27a0ecSDave Kleikamp 	if (!ret)
6072ac27a0ecSDave Kleikamp 		ret = err;
6073ac27a0ecSDave Kleikamp 	return ret;
6074ac27a0ecSDave Kleikamp }
6075ac27a0ecSDave Kleikamp 
6076617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
6077ac27a0ecSDave Kleikamp {
6078262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
6079262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6080262b4662SJan Kara 
60812c8be6b2SJan Kara 	/* Are we journaling quotas? */
6082e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
6083262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
6084ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
6085617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
6086ac27a0ecSDave Kleikamp 	} else {
6087ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
6088ac27a0ecSDave Kleikamp 	}
6089ac27a0ecSDave Kleikamp }
6090ac27a0ecSDave Kleikamp 
6091617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
6092ac27a0ecSDave Kleikamp {
6093ac27a0ecSDave Kleikamp 	int ret, err;
6094ac27a0ecSDave Kleikamp 	handle_t *handle;
6095ac27a0ecSDave Kleikamp 
6096ac27a0ecSDave Kleikamp 	/* Data block + inode block */
60972b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6098ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6099ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6100ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
6101617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6102ac27a0ecSDave Kleikamp 	if (!ret)
6103ac27a0ecSDave Kleikamp 		ret = err;
6104ac27a0ecSDave Kleikamp 	return ret;
6105ac27a0ecSDave Kleikamp }
6106ac27a0ecSDave Kleikamp 
6107ac27a0ecSDave Kleikamp /*
6108ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
6109ac27a0ecSDave Kleikamp  * the quota file and such...
6110ac27a0ecSDave Kleikamp  */
6111617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
6112ac27a0ecSDave Kleikamp {
611333458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
6114617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
6115ac27a0ecSDave Kleikamp }
6116ac27a0ecSDave Kleikamp 
6117daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6118daf647d2STheodore Ts'o {
6119daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
6120daf647d2STheodore Ts'o 
6121daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
6122daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
6123daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
6124daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
6125daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
6126daf647d2STheodore Ts'o 	 */
6127daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6128daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6129daf647d2STheodore Ts'o }
6130daf647d2STheodore Ts'o 
6131ac27a0ecSDave Kleikamp /*
6132ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
6133ac27a0ecSDave Kleikamp  */
6134617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
61358c54ca9cSAl Viro 			 const struct path *path)
6136ac27a0ecSDave Kleikamp {
6137ac27a0ecSDave Kleikamp 	int err;
6138ac27a0ecSDave Kleikamp 
6139ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
6140ac27a0ecSDave Kleikamp 		return -EINVAL;
61410623543bSJan Kara 
6142ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
6143d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
6144ac27a0ecSDave Kleikamp 		return -EXDEV;
61450623543bSJan Kara 	/* Journaling quota? */
61460623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
61472b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
6148f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
6149b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
6150b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
6151b31e1552SEric Sandeen 				"Journaled quota will not work");
615291389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
615391389240SJan Kara 	} else {
615491389240SJan Kara 		/*
615591389240SJan Kara 		 * Clear the flag just in case mount options changed since
615691389240SJan Kara 		 * last time.
615791389240SJan Kara 		 */
615891389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
61590623543bSJan Kara 	}
61600623543bSJan Kara 
61610623543bSJan Kara 	/*
61620623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
61630623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
61640623543bSJan Kara 	 */
61650390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
61662b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
61670623543bSJan Kara 		/*
61680623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
61690623543bSJan Kara 		 * otherwise be livelocked...
61700623543bSJan Kara 		 */
61710623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
61727ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
61730623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6174f00c9e44SJan Kara 		if (err)
61757ffe1ea8SHidehiro Kawai 			return err;
61767ffe1ea8SHidehiro Kawai 	}
6177957153fcSJan Kara 
6178daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6179daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
6180957153fcSJan Kara 	if (err) {
6181daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
6182daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
6183957153fcSJan Kara 	} else {
6184957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6185957153fcSJan Kara 		handle_t *handle;
6186957153fcSJan Kara 
618761a92987SJan Kara 		/*
618861a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
618961a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
619061a92987SJan Kara 		 * are already enabled and this is not a hard failure.
619161a92987SJan Kara 		 */
6192957153fcSJan Kara 		inode_lock(inode);
6193957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6194957153fcSJan Kara 		if (IS_ERR(handle))
6195957153fcSJan Kara 			goto unlock_inode;
6196957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6197957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6198957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
61994209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6200957153fcSJan Kara 		ext4_journal_stop(handle);
6201957153fcSJan Kara 	unlock_inode:
6202957153fcSJan Kara 		inode_unlock(inode);
6203957153fcSJan Kara 	}
6204daf647d2STheodore Ts'o 	return err;
6205ac27a0ecSDave Kleikamp }
6206ac27a0ecSDave Kleikamp 
62077c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
62087c319d32SAditya Kali 			     unsigned int flags)
62097c319d32SAditya Kali {
62107c319d32SAditya Kali 	int err;
62117c319d32SAditya Kali 	struct inode *qf_inode;
6212a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
62137c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6214689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6215689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
62167c319d32SAditya Kali 	};
62177c319d32SAditya Kali 
6218e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
62197c319d32SAditya Kali 
62207c319d32SAditya Kali 	if (!qf_inums[type])
62217c319d32SAditya Kali 		return -EPERM;
62227c319d32SAditya Kali 
62238a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
62247c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
62257c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
62267c319d32SAditya Kali 		return PTR_ERR(qf_inode);
62277c319d32SAditya Kali 	}
62287c319d32SAditya Kali 
6229bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6230bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6231daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
62327212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6233daf647d2STheodore Ts'o 	if (err)
6234daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
623561157b24SPan Bian 	iput(qf_inode);
62367c319d32SAditya Kali 
62377c319d32SAditya Kali 	return err;
62387c319d32SAditya Kali }
62397c319d32SAditya Kali 
62407c319d32SAditya Kali /* Enable usage tracking for all quota types. */
62417c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
62427c319d32SAditya Kali {
62437c319d32SAditya Kali 	int type, err = 0;
6244a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
62457c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6246689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6247689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
62487c319d32SAditya Kali 	};
624949da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
625049da9392SJan Kara 		test_opt(sb, USRQUOTA),
625149da9392SJan Kara 		test_opt(sb, GRPQUOTA),
625249da9392SJan Kara 		test_opt(sb, PRJQUOTA),
625349da9392SJan Kara 	};
62547c319d32SAditya Kali 
625591389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6256a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
62577c319d32SAditya Kali 		if (qf_inums[type]) {
62587c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
625949da9392SJan Kara 				DQUOT_USAGE_ENABLED |
626049da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
62617c319d32SAditya Kali 			if (err) {
62627c319d32SAditya Kali 				ext4_warning(sb,
626372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
626472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
626572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
62667f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
62677f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
62687f144fd0SJunichi Uekawa 
62697c319d32SAditya Kali 				return err;
62707c319d32SAditya Kali 			}
62717c319d32SAditya Kali 		}
62727c319d32SAditya Kali 	}
62737c319d32SAditya Kali 	return 0;
62747c319d32SAditya Kali }
62757c319d32SAditya Kali 
6276ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6277ca0e05e4SDmitry Monakhov {
627821f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
627921f97697SJan Kara 	handle_t *handle;
6280957153fcSJan Kara 	int err;
628121f97697SJan Kara 
628287009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
628387009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
628487009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6285ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6286ca0e05e4SDmitry Monakhov 
6287957153fcSJan Kara 	if (!inode || !igrab(inode))
62880b268590SAmir Goldstein 		goto out;
62890b268590SAmir Goldstein 
6290957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6291964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6292957153fcSJan Kara 		goto out_put;
6293957153fcSJan Kara 
6294957153fcSJan Kara 	inode_lock(inode);
629561a92987SJan Kara 	/*
629661a92987SJan Kara 	 * Update modification times of quota files when userspace can
629761a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
629861a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
629961a92987SJan Kara 	 */
63009924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
63014209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
63024209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6303957153fcSJan Kara 		goto out_unlock;
63044209ae12SHarshad Shirwadkar 	}
6305957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6306957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6307eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
63084209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
630921f97697SJan Kara 	ext4_journal_stop(handle);
6310957153fcSJan Kara out_unlock:
6311957153fcSJan Kara 	inode_unlock(inode);
6312957153fcSJan Kara out_put:
6313964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6314957153fcSJan Kara 	iput(inode);
6315957153fcSJan Kara 	return err;
631621f97697SJan Kara out:
6317ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6318ca0e05e4SDmitry Monakhov }
6319ca0e05e4SDmitry Monakhov 
6320ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6321ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6322ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6323ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6324617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6325ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6326ac27a0ecSDave Kleikamp {
6327ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6328725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6329ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6330ac27a0ecSDave Kleikamp 	int tocopy;
6331ac27a0ecSDave Kleikamp 	size_t toread;
6332ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6333ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6334ac27a0ecSDave Kleikamp 
6335ac27a0ecSDave Kleikamp 	if (off > i_size)
6336ac27a0ecSDave Kleikamp 		return 0;
6337ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6338ac27a0ecSDave Kleikamp 		len = i_size-off;
6339ac27a0ecSDave Kleikamp 	toread = len;
6340ac27a0ecSDave Kleikamp 	while (toread > 0) {
6341ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6342ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
63431c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
63441c215028STheodore Ts'o 		if (IS_ERR(bh))
63451c215028STheodore Ts'o 			return PTR_ERR(bh);
6346ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6347ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6348ac27a0ecSDave Kleikamp 		else
6349ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6350ac27a0ecSDave Kleikamp 		brelse(bh);
6351ac27a0ecSDave Kleikamp 		offset = 0;
6352ac27a0ecSDave Kleikamp 		toread -= tocopy;
6353ac27a0ecSDave Kleikamp 		data += tocopy;
6354ac27a0ecSDave Kleikamp 		blk++;
6355ac27a0ecSDave Kleikamp 	}
6356ac27a0ecSDave Kleikamp 	return len;
6357ac27a0ecSDave Kleikamp }
6358ac27a0ecSDave Kleikamp 
6359ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6360ac27a0ecSDave Kleikamp  * enough credits) */
6361617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6362ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6363ac27a0ecSDave Kleikamp {
6364ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6365725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
63664209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6367c5e298aeSTheodore Ts'o 	int retries = 0;
6368ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6369ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6370ac27a0ecSDave Kleikamp 
63710390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6372b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6373b31e1552SEric Sandeen 			" cancelled because transaction is not started",
63749c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
63759c3013e9SJan Kara 		return -EIO;
63769c3013e9SJan Kara 	}
637767eeb568SDmitry Monakhov 	/*
637867eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
637967eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
638067eeb568SDmitry Monakhov 	 */
638167eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
638267eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
638367eeb568SDmitry Monakhov 			" cancelled because not block aligned",
638467eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
638567eeb568SDmitry Monakhov 		return -EIO;
638667eeb568SDmitry Monakhov 	}
638767eeb568SDmitry Monakhov 
6388c5e298aeSTheodore Ts'o 	do {
6389c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6390c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6391c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
639245586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6393c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
63941c215028STheodore Ts'o 	if (IS_ERR(bh))
63951c215028STheodore Ts'o 		return PTR_ERR(bh);
6396ac27a0ecSDave Kleikamp 	if (!bh)
6397ac27a0ecSDave Kleikamp 		goto out;
63985d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6399617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6400ac27a0ecSDave Kleikamp 	if (err) {
6401ac27a0ecSDave Kleikamp 		brelse(bh);
64021c215028STheodore Ts'o 		return err;
6403ac27a0ecSDave Kleikamp 	}
6404ac27a0ecSDave Kleikamp 	lock_buffer(bh);
640567eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6406ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6407ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
64080390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6409ac27a0ecSDave Kleikamp 	brelse(bh);
6410ac27a0ecSDave Kleikamp out:
641167eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
641267eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6413617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
64144209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
64154209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
64164209ae12SHarshad Shirwadkar 			err = err2;
641721f97697SJan Kara 	}
64184209ae12SHarshad Shirwadkar 	return err ? err : len;
6419ac27a0ecSDave Kleikamp }
6420ac27a0ecSDave Kleikamp #endif
6421ac27a0ecSDave Kleikamp 
6422152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6423152a0836SAl Viro 		       const char *dev_name, void *data)
6424ac27a0ecSDave Kleikamp {
6425152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6426ac27a0ecSDave Kleikamp }
6427ac27a0ecSDave Kleikamp 
6428c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
642924b58424STheodore Ts'o static inline void register_as_ext2(void)
643024b58424STheodore Ts'o {
643124b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
643224b58424STheodore Ts'o 	if (err)
643324b58424STheodore Ts'o 		printk(KERN_WARNING
643424b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
643524b58424STheodore Ts'o }
643624b58424STheodore Ts'o 
643724b58424STheodore Ts'o static inline void unregister_as_ext2(void)
643824b58424STheodore Ts'o {
643924b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
644024b58424STheodore Ts'o }
64412035e776STheodore Ts'o 
64422035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
64432035e776STheodore Ts'o {
6444e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
64452035e776STheodore Ts'o 		return 0;
6446bc98a42cSDavid Howells 	if (sb_rdonly(sb))
64472035e776STheodore Ts'o 		return 1;
6448e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
64492035e776STheodore Ts'o 		return 0;
64502035e776STheodore Ts'o 	return 1;
64512035e776STheodore Ts'o }
645224b58424STheodore Ts'o #else
645324b58424STheodore Ts'o static inline void register_as_ext2(void) { }
645424b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
64552035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
645624b58424STheodore Ts'o #endif
645724b58424STheodore Ts'o 
645824b58424STheodore Ts'o static inline void register_as_ext3(void)
645924b58424STheodore Ts'o {
646024b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
646124b58424STheodore Ts'o 	if (err)
646224b58424STheodore Ts'o 		printk(KERN_WARNING
646324b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
646424b58424STheodore Ts'o }
646524b58424STheodore Ts'o 
646624b58424STheodore Ts'o static inline void unregister_as_ext3(void)
646724b58424STheodore Ts'o {
646824b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
646924b58424STheodore Ts'o }
64702035e776STheodore Ts'o 
64712035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
64722035e776STheodore Ts'o {
6473e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
64742035e776STheodore Ts'o 		return 0;
6475e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
64762035e776STheodore Ts'o 		return 0;
6477bc98a42cSDavid Howells 	if (sb_rdonly(sb))
64782035e776STheodore Ts'o 		return 1;
6479e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
64802035e776STheodore Ts'o 		return 0;
64812035e776STheodore Ts'o 	return 1;
64822035e776STheodore Ts'o }
648324b58424STheodore Ts'o 
648403010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6485ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
648603010a33STheodore Ts'o 	.name		= "ext4",
6487152a0836SAl Viro 	.mount		= ext4_mount,
6488ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6489ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6490ac27a0ecSDave Kleikamp };
64917f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6492ac27a0ecSDave Kleikamp 
6493e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6494e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6495e9e3bcecSEric Sandeen 
64965dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6497ac27a0ecSDave Kleikamp {
6498e9e3bcecSEric Sandeen 	int i, err;
6499c9de560dSAlex Tomas 
6500e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
650107c0c5d8SAl Viro 	ext4_li_info = NULL;
650207c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
650307c0c5d8SAl Viro 
65049a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
650512e9b892SDmitry Monakhov 	ext4_check_flag_values();
6506e9e3bcecSEric Sandeen 
6507e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6508e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6509e9e3bcecSEric Sandeen 
651051865fdaSZheng Liu 	err = ext4_init_es();
65116fd058f7STheodore Ts'o 	if (err)
65126fd058f7STheodore Ts'o 		return err;
651351865fdaSZheng Liu 
65141dc0aa46SEric Whitney 	err = ext4_init_pending();
65151dc0aa46SEric Whitney 	if (err)
651622cfe4b4SEric Biggers 		goto out7;
651722cfe4b4SEric Biggers 
651822cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
651922cfe4b4SEric Biggers 	if (err)
65201dc0aa46SEric Whitney 		goto out6;
65211dc0aa46SEric Whitney 
652251865fdaSZheng Liu 	err = ext4_init_pageio();
652351865fdaSZheng Liu 	if (err)
6524b5799018STheodore Ts'o 		goto out5;
652551865fdaSZheng Liu 
65265dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6527bd2d0210STheodore Ts'o 	if (err)
6528b5799018STheodore Ts'o 		goto out4;
6529857ac889SLukas Czerner 
6530b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6531dd68314cSTheodore Ts'o 	if (err)
6532b5799018STheodore Ts'o 		goto out3;
6533857ac889SLukas Czerner 
65345dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6535ac27a0ecSDave Kleikamp 	if (err)
6536c9de560dSAlex Tomas 		goto out2;
6537ac27a0ecSDave Kleikamp 	err = init_inodecache();
6538ac27a0ecSDave Kleikamp 	if (err)
6539ac27a0ecSDave Kleikamp 		goto out1;
654024b58424STheodore Ts'o 	register_as_ext3();
65412035e776STheodore Ts'o 	register_as_ext2();
654203010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6543ac27a0ecSDave Kleikamp 	if (err)
6544ac27a0ecSDave Kleikamp 		goto out;
6545bfff6873SLukas Czerner 
6546ac27a0ecSDave Kleikamp 	return 0;
6547ac27a0ecSDave Kleikamp out:
654824b58424STheodore Ts'o 	unregister_as_ext2();
654924b58424STheodore Ts'o 	unregister_as_ext3();
6550ac27a0ecSDave Kleikamp 	destroy_inodecache();
6551ac27a0ecSDave Kleikamp out1:
65525dabfc78STheodore Ts'o 	ext4_exit_mballoc();
65539c191f70ST Makphaibulchoke out2:
6554b5799018STheodore Ts'o 	ext4_exit_sysfs();
6555b5799018STheodore Ts'o out3:
6556dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6557b5799018STheodore Ts'o out4:
65585dabfc78STheodore Ts'o 	ext4_exit_pageio();
6559b5799018STheodore Ts'o out5:
656022cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
65611dc0aa46SEric Whitney out6:
656222cfe4b4SEric Biggers 	ext4_exit_pending();
656322cfe4b4SEric Biggers out7:
656451865fdaSZheng Liu 	ext4_exit_es();
656551865fdaSZheng Liu 
6566ac27a0ecSDave Kleikamp 	return err;
6567ac27a0ecSDave Kleikamp }
6568ac27a0ecSDave Kleikamp 
65695dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6570ac27a0ecSDave Kleikamp {
6571bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
657224b58424STheodore Ts'o 	unregister_as_ext2();
657324b58424STheodore Ts'o 	unregister_as_ext3();
657403010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6575ac27a0ecSDave Kleikamp 	destroy_inodecache();
65765dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6577b5799018STheodore Ts'o 	ext4_exit_sysfs();
65785dabfc78STheodore Ts'o 	ext4_exit_system_zone();
65795dabfc78STheodore Ts'o 	ext4_exit_pageio();
658022cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6581dd12ed14SEric Sandeen 	ext4_exit_es();
65821dc0aa46SEric Whitney 	ext4_exit_pending();
6583ac27a0ecSDave Kleikamp }
6584ac27a0ecSDave Kleikamp 
6585ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
658683982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6587ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
65887ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
65895dabfc78STheodore Ts'o module_init(ext4_init_fs)
65905dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6591