xref: /linux/fs/ext4/super.c (revision 556e0319fbb8eee3fa19fdcc27c8bcb4af1c7211)
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) {
148fb265c9cSTheodore Ts'o 	/*
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) 	}
253c197855eSStephen Hemminger }
254a9c47317SDarrick J. Wong 
255a9c47317SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
2569aa5d32bSDmitry Monakhov 				 struct ext4_super_block *es)
257a9c47317SDarrick J. Wong {
258a9c47317SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
259a9c47317SDarrick J. Wong 		return 1;
260a9c47317SDarrick J. Wong 
261a9c47317SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
26206db49e6STheodore Ts'o }
263a9c47317SDarrick J. Wong 
26406db49e6STheodore Ts'o static __le32 ext4_superblock_csum(struct super_block *sb,
26506db49e6STheodore Ts'o 				   struct ext4_super_block *es)
2669aa5d32bSDmitry Monakhov {
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);
2729933fc0aSTheodore Ts'o 
2739933fc0aSTheodore Ts'o 	return cpu_to_le32(csum);
2749933fc0aSTheodore Ts'o }
2759933fc0aSTheodore Ts'o 
2768be04b93SJoe Perches static int ext4_superblock_csum_verify(struct super_block *sb,
2779933fc0aSTheodore Ts'o 				       struct ext4_super_block *es)
2789933fc0aSTheodore Ts'o {
2799933fc0aSTheodore Ts'o 	if (!ext4_has_metadata_csum(sb))
2809933fc0aSTheodore Ts'o 		return 1;
2819933fc0aSTheodore Ts'o 
2829933fc0aSTheodore Ts'o 	return es->s_checksum == ext4_superblock_csum(sb, es);
2839933fc0aSTheodore Ts'o }
2849933fc0aSTheodore Ts'o 
2859933fc0aSTheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
2868be04b93SJoe Perches {
2879933fc0aSTheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2889933fc0aSTheodore Ts'o 
2899933fc0aSTheodore Ts'o 	if (!ext4_has_metadata_csum(sb))
2909933fc0aSTheodore Ts'o 		return;
2919933fc0aSTheodore Ts'o 
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);
302ac27a0ecSDave Kleikamp 	es->s_checksum = ext4_superblock_csum(sb, es);
303acaa5326SConstantine Sapuntzakis 	unlock_buffer(EXT4_SB(sb)->s_sbh);
304ac27a0ecSDave Kleikamp }
305bd81d8eeSLaurent Vivier 
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 
574afb585a9SMauricio Faria de Oliveira /*
575afb585a9SMauricio Faria de Oliveira  * This writepage callback for write_cache_pages()
576afb585a9SMauricio Faria de Oliveira  * takes care of a few cases after page cleaning.
577afb585a9SMauricio Faria de Oliveira  *
578afb585a9SMauricio Faria de Oliveira  * write_cache_pages() already checks for dirty pages
579afb585a9SMauricio Faria de Oliveira  * and calls clear_page_dirty_for_io(), which we want,
580afb585a9SMauricio Faria de Oliveira  * to write protect the pages.
581afb585a9SMauricio Faria de Oliveira  *
582afb585a9SMauricio Faria de Oliveira  * However, we may have to redirty a page (see below.)
583afb585a9SMauricio Faria de Oliveira  */
584afb585a9SMauricio Faria de Oliveira static int ext4_journalled_writepage_callback(struct page *page,
585afb585a9SMauricio Faria de Oliveira 					      struct writeback_control *wbc,
586afb585a9SMauricio Faria de Oliveira 					      void *data)
587afb585a9SMauricio Faria de Oliveira {
588afb585a9SMauricio Faria de Oliveira 	transaction_t *transaction = (transaction_t *) data;
589afb585a9SMauricio Faria de Oliveira 	struct buffer_head *bh, *head;
590afb585a9SMauricio Faria de Oliveira 	struct journal_head *jh;
591afb585a9SMauricio Faria de Oliveira 
592afb585a9SMauricio Faria de Oliveira 	bh = head = page_buffers(page);
593afb585a9SMauricio Faria de Oliveira 	do {
594afb585a9SMauricio Faria de Oliveira 		/*
595afb585a9SMauricio Faria de Oliveira 		 * We have to redirty a page in these cases:
596afb585a9SMauricio Faria de Oliveira 		 * 1) If buffer is dirty, it means the page was dirty because it
597afb585a9SMauricio Faria de Oliveira 		 * contains a buffer that needs checkpointing. So the dirty bit
598afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that checkpointing writes the buffer
599afb585a9SMauricio Faria de Oliveira 		 * properly.
600afb585a9SMauricio Faria de Oliveira 		 * 2) If buffer is not part of the committing transaction
601afb585a9SMauricio Faria de Oliveira 		 * (we may have just accidentally come across this buffer because
602afb585a9SMauricio Faria de Oliveira 		 * inode range tracking is not exact) or if the currently running
603afb585a9SMauricio Faria de Oliveira 		 * transaction already contains this buffer as well, dirty bit
604afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that the buffer gets writeprotected
605afb585a9SMauricio Faria de Oliveira 		 * properly on running transaction's commit.
606afb585a9SMauricio Faria de Oliveira 		 */
607afb585a9SMauricio Faria de Oliveira 		jh = bh2jh(bh);
608afb585a9SMauricio Faria de Oliveira 		if (buffer_dirty(bh) ||
609afb585a9SMauricio Faria de Oliveira 		    (jh && (jh->b_transaction != transaction ||
610afb585a9SMauricio Faria de Oliveira 			    jh->b_next_transaction))) {
611afb585a9SMauricio Faria de Oliveira 			redirty_page_for_writepage(wbc, page);
612afb585a9SMauricio Faria de Oliveira 			goto out;
613afb585a9SMauricio Faria de Oliveira 		}
614afb585a9SMauricio Faria de Oliveira 	} while ((bh = bh->b_this_page) != head);
615afb585a9SMauricio Faria de Oliveira 
616afb585a9SMauricio Faria de Oliveira out:
617afb585a9SMauricio Faria de Oliveira 	return AOP_WRITEPAGE_ACTIVATE;
618afb585a9SMauricio Faria de Oliveira }
619afb585a9SMauricio Faria de Oliveira 
620afb585a9SMauricio Faria de Oliveira static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
621afb585a9SMauricio Faria de Oliveira {
622afb585a9SMauricio Faria de Oliveira 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
623afb585a9SMauricio Faria de Oliveira 	struct writeback_control wbc = {
624afb585a9SMauricio Faria de Oliveira 		.sync_mode =  WB_SYNC_ALL,
625afb585a9SMauricio Faria de Oliveira 		.nr_to_write = LONG_MAX,
626afb585a9SMauricio Faria de Oliveira 		.range_start = jinode->i_dirty_start,
627afb585a9SMauricio Faria de Oliveira 		.range_end = jinode->i_dirty_end,
628afb585a9SMauricio Faria de Oliveira         };
629afb585a9SMauricio Faria de Oliveira 
630afb585a9SMauricio Faria de Oliveira 	return write_cache_pages(mapping, &wbc,
631afb585a9SMauricio Faria de Oliveira 				 ext4_journalled_writepage_callback,
632afb585a9SMauricio Faria de Oliveira 				 jinode->i_transaction);
633afb585a9SMauricio Faria de Oliveira }
634afb585a9SMauricio Faria de Oliveira 
635afb585a9SMauricio Faria de Oliveira static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
636afb585a9SMauricio Faria de Oliveira {
637afb585a9SMauricio Faria de Oliveira 	int ret;
638afb585a9SMauricio Faria de Oliveira 
639afb585a9SMauricio Faria de Oliveira 	if (ext4_should_journal_data(jinode->i_vfs_inode))
640afb585a9SMauricio Faria de Oliveira 		ret = ext4_journalled_submit_inode_data_buffers(jinode);
641afb585a9SMauricio Faria de Oliveira 	else
642afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_submit_inode_data_buffers(jinode);
643afb585a9SMauricio Faria de Oliveira 
644afb585a9SMauricio Faria de Oliveira 	return ret;
645afb585a9SMauricio Faria de Oliveira }
646afb585a9SMauricio Faria de Oliveira 
647afb585a9SMauricio Faria de Oliveira static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
648afb585a9SMauricio Faria de Oliveira {
649afb585a9SMauricio Faria de Oliveira 	int ret = 0;
650afb585a9SMauricio Faria de Oliveira 
651afb585a9SMauricio Faria de Oliveira 	if (!ext4_should_journal_data(jinode->i_vfs_inode))
652afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_finish_inode_data_buffers(jinode);
653afb585a9SMauricio Faria de Oliveira 
654afb585a9SMauricio Faria de Oliveira 	return ret;
655afb585a9SMauricio Faria de Oliveira }
656afb585a9SMauricio Faria de Oliveira 
6571dc1097fSJan Kara static bool system_going_down(void)
6581dc1097fSJan Kara {
6591dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
6601dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
6611dc1097fSJan Kara }
6621dc1097fSJan Kara 
663ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
664ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
665ac27a0ecSDave Kleikamp  *
666ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
667617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
668ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
669ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
670ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
671ac27a0ecSDave Kleikamp  * write out the superblock safely.
672ac27a0ecSDave Kleikamp  *
673dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
674d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
675ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
676ac27a0ecSDave Kleikamp  */
677ac27a0ecSDave Kleikamp 
678617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
679ac27a0ecSDave Kleikamp {
680327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
681327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
682327eaf73STheodore Ts'o 
683bc98a42cSDavid Howells 	if (sb_rdonly(sb))
684ac27a0ecSDave Kleikamp 		return;
685ac27a0ecSDave Kleikamp 
686ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
687617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
688ac27a0ecSDave Kleikamp 
6894ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
690ac27a0ecSDave Kleikamp 		if (journal)
691dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
692ac27a0ecSDave Kleikamp 	}
6931dc1097fSJan Kara 	/*
6941dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
6951dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
6961dc1097fSJan Kara 	 * disabled.
6971dc1097fSJan Kara 	 */
6981dc1097fSJan Kara 	if (test_opt(sb, ERRORS_RO) || system_going_down()) {
699b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
7004418e141SDmitry Monakhov 		/*
7014418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
7024418e141SDmitry Monakhov 		 * before ->s_flags update
7034418e141SDmitry Monakhov 		 */
7044418e141SDmitry Monakhov 		smp_wmb();
7051751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
7061dc1097fSJan Kara 	} else if (test_opt(sb, ERRORS_PANIC)) {
707617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
708ac27a0ecSDave Kleikamp 			sb->s_id);
709ac27a0ecSDave Kleikamp 	}
7104327ba52SDaeho Jeong }
711ac27a0ecSDave Kleikamp 
712efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
713efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
714efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
715efbed4dcSTheodore Ts'o 
71612062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
71754d3adbcSTheodore Ts'o 		  unsigned int line, int error, __u64 block,
71854d3adbcSTheodore Ts'o 		  const char *fmt, ...)
719ac27a0ecSDave Kleikamp {
7200ff2ea7dSJoe Perches 	struct va_format vaf;
721ac27a0ecSDave Kleikamp 	va_list args;
722ac27a0ecSDave Kleikamp 
7230db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7240db1ff22STheodore Ts'o 		return;
7250db1ff22STheodore Ts'o 
726ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
727efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
728ac27a0ecSDave Kleikamp 		va_start(args, fmt);
7290ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7300ff2ea7dSJoe Perches 		vaf.va = &args;
731efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
732efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
7330ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
734ac27a0ecSDave Kleikamp 		va_end(args);
735efbed4dcSTheodore Ts'o 	}
73654d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, block, function, line);
737617ba13bSMingming Cao 	ext4_handle_error(sb);
738ac27a0ecSDave Kleikamp }
739ac27a0ecSDave Kleikamp 
740e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
74154d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
742273df556SFrank Mayhar 			const char *fmt, ...)
743273df556SFrank Mayhar {
744273df556SFrank Mayhar 	va_list args;
745f7c21177STheodore Ts'o 	struct va_format vaf;
746273df556SFrank Mayhar 
7470db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
7480db1ff22STheodore Ts'o 		return;
7490db1ff22STheodore Ts'o 
750ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
751efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
752273df556SFrank Mayhar 		va_start(args, fmt);
753f7c21177STheodore Ts'o 		vaf.fmt = fmt;
754f7c21177STheodore Ts'o 		vaf.va = &args;
755c398eda0STheodore Ts'o 		if (block)
756d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
757d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
758d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
759d9ee81daSJoe Perches 			       block, current->comm, &vaf);
760d9ee81daSJoe Perches 		else
761d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
762d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
763d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
764d9ee81daSJoe Perches 			       current->comm, &vaf);
765273df556SFrank Mayhar 		va_end(args);
766efbed4dcSTheodore Ts'o 	}
76754d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, error, inode->i_ino, block,
76854d3adbcSTheodore Ts'o 			function, line);
769273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
770273df556SFrank Mayhar }
771273df556SFrank Mayhar 
772e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
773f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
774f7c21177STheodore Ts'o 		       const char *fmt, ...)
775273df556SFrank Mayhar {
776273df556SFrank Mayhar 	va_list args;
777f7c21177STheodore Ts'o 	struct va_format vaf;
778496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
779273df556SFrank Mayhar 	char pathname[80], *path;
780273df556SFrank Mayhar 
7810db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
7820db1ff22STheodore Ts'o 		return;
7830db1ff22STheodore Ts'o 
784ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
785efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
7869bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
787f9a62d09SDan Carpenter 		if (IS_ERR(path))
788273df556SFrank Mayhar 			path = "(unknown)";
789f7c21177STheodore Ts'o 		va_start(args, fmt);
790f7c21177STheodore Ts'o 		vaf.fmt = fmt;
791f7c21177STheodore Ts'o 		vaf.va = &args;
792d9ee81daSJoe Perches 		if (block)
793d9ee81daSJoe Perches 			printk(KERN_CRIT
794d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
795d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
796d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
797d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
798d9ee81daSJoe Perches 		else
799d9ee81daSJoe Perches 			printk(KERN_CRIT
800d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
801d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
802d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
803d9ee81daSJoe Perches 			       current->comm, path, &vaf);
804273df556SFrank Mayhar 		va_end(args);
805efbed4dcSTheodore Ts'o 	}
80654d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block,
80754d3adbcSTheodore Ts'o 			function, line);
808273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
809273df556SFrank Mayhar }
810273df556SFrank Mayhar 
811722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
812ac27a0ecSDave Kleikamp 			      char nbuf[16])
813ac27a0ecSDave Kleikamp {
814ac27a0ecSDave Kleikamp 	char *errstr = NULL;
815ac27a0ecSDave Kleikamp 
816ac27a0ecSDave Kleikamp 	switch (errno) {
8176a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
8186a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
8196a797d27SDarrick J. Wong 		break;
8206a797d27SDarrick J. Wong 	case -EFSBADCRC:
8216a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
8226a797d27SDarrick J. Wong 		break;
823ac27a0ecSDave Kleikamp 	case -EIO:
824ac27a0ecSDave Kleikamp 		errstr = "IO failure";
825ac27a0ecSDave Kleikamp 		break;
826ac27a0ecSDave Kleikamp 	case -ENOMEM:
827ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
828ac27a0ecSDave Kleikamp 		break;
829ac27a0ecSDave Kleikamp 	case -EROFS:
83078f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
83178f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
832ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
833ac27a0ecSDave Kleikamp 		else
834ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
835ac27a0ecSDave Kleikamp 		break;
836ac27a0ecSDave Kleikamp 	default:
837ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
838ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
839ac27a0ecSDave Kleikamp 		 * NULL. */
840ac27a0ecSDave Kleikamp 		if (nbuf) {
841ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
842ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
843ac27a0ecSDave Kleikamp 				errstr = nbuf;
844ac27a0ecSDave Kleikamp 		}
845ac27a0ecSDave Kleikamp 		break;
846ac27a0ecSDave Kleikamp 	}
847ac27a0ecSDave Kleikamp 
848ac27a0ecSDave Kleikamp 	return errstr;
849ac27a0ecSDave Kleikamp }
850ac27a0ecSDave Kleikamp 
851617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
852ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
853ac27a0ecSDave Kleikamp 
854c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
855c398eda0STheodore Ts'o 		      unsigned int line, int errno)
856ac27a0ecSDave Kleikamp {
857ac27a0ecSDave Kleikamp 	char nbuf[16];
858ac27a0ecSDave Kleikamp 	const char *errstr;
859ac27a0ecSDave Kleikamp 
8600db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8610db1ff22STheodore Ts'o 		return;
8620db1ff22STheodore Ts'o 
863ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
864ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
865ac27a0ecSDave Kleikamp 	 * an error. */
866bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
867ac27a0ecSDave Kleikamp 		return;
868ac27a0ecSDave Kleikamp 
869efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
870617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
871c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
872c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
873efbed4dcSTheodore Ts'o 	}
874ac27a0ecSDave Kleikamp 
87554d3adbcSTheodore Ts'o 	save_error_info(sb, -errno, 0, 0, function, line);
876617ba13bSMingming Cao 	ext4_handle_error(sb);
877ac27a0ecSDave Kleikamp }
878ac27a0ecSDave Kleikamp 
879ac27a0ecSDave Kleikamp /*
880617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
881ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
882ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
883ac27a0ecSDave Kleikamp  *
884ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
885ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
886ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
887ac27a0ecSDave Kleikamp  */
888ac27a0ecSDave Kleikamp 
889c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
89054d3adbcSTheodore Ts'o 		  unsigned int line, int error, const char *fmt, ...)
891ac27a0ecSDave Kleikamp {
892651e1c3bSJoe Perches 	struct va_format vaf;
893ac27a0ecSDave Kleikamp 	va_list args;
894ac27a0ecSDave Kleikamp 
8950db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8960db1ff22STheodore Ts'o 		return;
8970db1ff22STheodore Ts'o 
89854d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, 0, function, line);
899ac27a0ecSDave Kleikamp 	va_start(args, fmt);
900651e1c3bSJoe Perches 	vaf.fmt = fmt;
901651e1c3bSJoe Perches 	vaf.va = &args;
902651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
903651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
904ac27a0ecSDave Kleikamp 	va_end(args);
905ac27a0ecSDave Kleikamp 
906bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
9074ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
9087b97d868Szhangyi (F) 		if (EXT4_SB(sb)->s_journal)
9097b97d868Szhangyi (F) 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
9107b97d868Szhangyi (F) 
9117b97d868Szhangyi (F) 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
9124418e141SDmitry Monakhov 		/*
9134418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
9144418e141SDmitry Monakhov 		 * before ->s_flags update
9154418e141SDmitry Monakhov 		 */
9164418e141SDmitry Monakhov 		smp_wmb();
9171751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
9181c13d5c0STheodore Ts'o 	}
9197b97d868Szhangyi (F) 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down())
9201c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
921ac27a0ecSDave Kleikamp }
922ac27a0ecSDave Kleikamp 
923e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
924e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
925b31e1552SEric Sandeen {
9260ff2ea7dSJoe Perches 	struct va_format vaf;
927b31e1552SEric Sandeen 	va_list args;
928b31e1552SEric Sandeen 
9291cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_msg_count);
930efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
931efbed4dcSTheodore Ts'o 		return;
932efbed4dcSTheodore Ts'o 
933b31e1552SEric Sandeen 	va_start(args, fmt);
9340ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9350ff2ea7dSJoe Perches 	vaf.va = &args;
9360ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
937b31e1552SEric Sandeen 	va_end(args);
938b31e1552SEric Sandeen }
939b31e1552SEric Sandeen 
9401cf006edSDmitry Monakhov static int ext4_warning_ratelimit(struct super_block *sb)
9411cf006edSDmitry Monakhov {
9421cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_warning_count);
9431cf006edSDmitry Monakhov 	return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
9441cf006edSDmitry Monakhov 			    "EXT4-fs warning");
9451cf006edSDmitry Monakhov }
946b03a2f7eSAndreas Dilger 
94712062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
948c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
949ac27a0ecSDave Kleikamp {
9500ff2ea7dSJoe Perches 	struct va_format vaf;
951ac27a0ecSDave Kleikamp 	va_list args;
952ac27a0ecSDave Kleikamp 
953b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
954efbed4dcSTheodore Ts'o 		return;
955efbed4dcSTheodore Ts'o 
956ac27a0ecSDave Kleikamp 	va_start(args, fmt);
9570ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9580ff2ea7dSJoe Perches 	vaf.va = &args;
9590ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
9600ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
961ac27a0ecSDave Kleikamp 	va_end(args);
962ac27a0ecSDave Kleikamp }
963ac27a0ecSDave Kleikamp 
964b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
965b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
966b03a2f7eSAndreas Dilger {
967b03a2f7eSAndreas Dilger 	struct va_format vaf;
968b03a2f7eSAndreas Dilger 	va_list args;
969b03a2f7eSAndreas Dilger 
970b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
971b03a2f7eSAndreas Dilger 		return;
972b03a2f7eSAndreas Dilger 
973b03a2f7eSAndreas Dilger 	va_start(args, fmt);
974b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
975b03a2f7eSAndreas Dilger 	vaf.va = &args;
976b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
977b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
978b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
979b03a2f7eSAndreas Dilger 	va_end(args);
980b03a2f7eSAndreas Dilger }
981b03a2f7eSAndreas Dilger 
982e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
983e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
984e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
985e29136f8STheodore Ts'o 			     const char *fmt, ...)
9865d1b1b3fSAneesh Kumar K.V __releases(bitlock)
9875d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
9885d1b1b3fSAneesh Kumar K.V {
9890ff2ea7dSJoe Perches 	struct va_format vaf;
9905d1b1b3fSAneesh Kumar K.V 	va_list args;
9915d1b1b3fSAneesh Kumar K.V 
9920db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
9930db1ff22STheodore Ts'o 		return;
9940db1ff22STheodore Ts'o 
995ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
99654d3adbcSTheodore Ts'o 	__save_error_info(sb, EFSCORRUPTED, ino, block, function, line);
9970ff2ea7dSJoe Perches 
998efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
9995d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
10000ff2ea7dSJoe Perches 		vaf.fmt = fmt;
10010ff2ea7dSJoe Perches 		vaf.va = &args;
100221149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
1003e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
1004e29136f8STheodore Ts'o 		if (ino)
10050ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
1006e29136f8STheodore Ts'o 		if (block)
1007efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
1008efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
10090ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
10105d1b1b3fSAneesh Kumar K.V 		va_end(args);
1011efbed4dcSTheodore Ts'o 	}
10125d1b1b3fSAneesh Kumar K.V 
1013327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
1014327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
1015327eaf73STheodore Ts'o 
10165d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
1017e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
10185d1b1b3fSAneesh Kumar K.V 		return;
10195d1b1b3fSAneesh Kumar K.V 	}
10201c13d5c0STheodore Ts'o 
10215d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
102206f29cc8SZhouyi Zhou 	ext4_commit_super(sb, 1);
10235d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
10245d1b1b3fSAneesh Kumar K.V 	/*
10255d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
10265d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
10275d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
10285d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
10295d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
103025985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
10315d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
10325d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
10335d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
10345d1b1b3fSAneesh Kumar K.V 	 */
10355d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
10365d1b1b3fSAneesh Kumar K.V 	return;
10375d1b1b3fSAneesh Kumar K.V }
10385d1b1b3fSAneesh Kumar K.V 
1039db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1040db79e6d1SWang Shilong 				     ext4_group_t group,
1041db79e6d1SWang Shilong 				     unsigned int flags)
1042db79e6d1SWang Shilong {
1043db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1044db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1045db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
10469af0b3d1SWang Shilong 	int ret;
1047db79e6d1SWang Shilong 
10489af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
10499af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
10509af0b3d1SWang Shilong 					    &grp->bb_state);
10519af0b3d1SWang Shilong 		if (!ret)
1052db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
1053db79e6d1SWang Shilong 					   grp->bb_free);
1054db79e6d1SWang Shilong 	}
1055db79e6d1SWang Shilong 
10569af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
10579af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
10589af0b3d1SWang Shilong 					    &grp->bb_state);
10599af0b3d1SWang Shilong 		if (!ret && gdp) {
1060db79e6d1SWang Shilong 			int count;
1061db79e6d1SWang Shilong 
1062db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
1063db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1064db79e6d1SWang Shilong 					   count);
1065db79e6d1SWang Shilong 		}
1066db79e6d1SWang Shilong 	}
1067db79e6d1SWang Shilong }
1068db79e6d1SWang Shilong 
1069617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
1070ac27a0ecSDave Kleikamp {
1071617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1072ac27a0ecSDave Kleikamp 
1073617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1074ac27a0ecSDave Kleikamp 		return;
1075ac27a0ecSDave Kleikamp 
107612062dddSEric Sandeen 	ext4_warning(sb,
1077ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
1078ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
1079617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
1080ac27a0ecSDave Kleikamp 
1081617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1082617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1083617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1084ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
1085ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
1086ac27a0ecSDave Kleikamp 
1087ac27a0ecSDave Kleikamp 	/*
1088ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
1089ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
1090ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
1091ac27a0ecSDave Kleikamp 	 */
1092ac27a0ecSDave Kleikamp }
1093ac27a0ecSDave Kleikamp 
1094ac27a0ecSDave Kleikamp /*
1095ac27a0ecSDave Kleikamp  * Open the external journal device
1096ac27a0ecSDave Kleikamp  */
1097b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1098ac27a0ecSDave Kleikamp {
1099ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1100ac27a0ecSDave Kleikamp 
1101d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1102ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
1103ac27a0ecSDave Kleikamp 		goto fail;
1104ac27a0ecSDave Kleikamp 	return bdev;
1105ac27a0ecSDave Kleikamp 
1106ac27a0ecSDave Kleikamp fail:
1107ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
1108ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
1109ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1110ac27a0ecSDave Kleikamp 	return NULL;
1111ac27a0ecSDave Kleikamp }
1112ac27a0ecSDave Kleikamp 
1113ac27a0ecSDave Kleikamp /*
1114ac27a0ecSDave Kleikamp  * Release the journal device
1115ac27a0ecSDave Kleikamp  */
11164385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
1117ac27a0ecSDave Kleikamp {
11184385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1119ac27a0ecSDave Kleikamp }
1120ac27a0ecSDave Kleikamp 
11214385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1122ac27a0ecSDave Kleikamp {
1123ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1124ee7ed3aaSChunguang Xu 	bdev = sbi->s_journal_bdev;
1125ac27a0ecSDave Kleikamp 	if (bdev) {
11264385bab1SAl Viro 		ext4_blkdev_put(bdev);
1127ee7ed3aaSChunguang Xu 		sbi->s_journal_bdev = NULL;
1128ac27a0ecSDave Kleikamp 	}
1129ac27a0ecSDave Kleikamp }
1130ac27a0ecSDave Kleikamp 
1131ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
1132ac27a0ecSDave Kleikamp {
1133617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1134ac27a0ecSDave Kleikamp }
1135ac27a0ecSDave Kleikamp 
1136617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1137ac27a0ecSDave Kleikamp {
1138ac27a0ecSDave Kleikamp 	struct list_head *l;
1139ac27a0ecSDave Kleikamp 
1140b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1141ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
1142ac27a0ecSDave Kleikamp 
1143ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
1144ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
1145ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
1146ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
1147ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1148ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
1149ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
1150ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1151ac27a0ecSDave Kleikamp 	}
1152ac27a0ecSDave Kleikamp }
1153ac27a0ecSDave Kleikamp 
1154957153fcSJan Kara #ifdef CONFIG_QUOTA
1155957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
1156957153fcSJan Kara 
1157957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1158957153fcSJan Kara {
1159957153fcSJan Kara 	int type;
1160957153fcSJan Kara 
1161957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
1162957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
1163957153fcSJan Kara 		ext4_quota_off(sb, type);
1164957153fcSJan Kara }
116533458eabSTheodore Ts'o 
116633458eabSTheodore Ts'o /*
116733458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
116833458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
116933458eabSTheodore Ts'o  */
117033458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
117133458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
117233458eabSTheodore Ts'o 				int type)
117333458eabSTheodore Ts'o {
117433458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
117533458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
117633458eabSTheodore Ts'o }
1177957153fcSJan Kara #else
1178957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1179957153fcSJan Kara {
1180957153fcSJan Kara }
1181957153fcSJan Kara #endif
1182957153fcSJan Kara 
1183617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1184ac27a0ecSDave Kleikamp {
1185617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1186617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
11871d0c3924STheodore Ts'o 	struct buffer_head **group_desc;
11887c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
118997abd7d4STheodore Ts'o 	int aborted = 0;
1190ef2cabf7SHidehiro Kawai 	int i, err;
1191ac27a0ecSDave Kleikamp 
1192857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1193957153fcSJan Kara 	ext4_quota_off_umount(sb);
1194e0ccfd95SChristoph Hellwig 
11952e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
11964c0425ffSMingming Cao 
11975e47868fSRitesh Harjani 	/*
11985e47868fSRitesh Harjani 	 * Unregister sysfs before destroying jbd2 journal.
11995e47868fSRitesh Harjani 	 * Since we could still access attr_journal_task attribute via sysfs
12005e47868fSRitesh Harjani 	 * path which could have sbi->s_journal->j_task as NULL
12015e47868fSRitesh Harjani 	 */
12025e47868fSRitesh Harjani 	ext4_unregister_sysfs(sb);
12035e47868fSRitesh Harjani 
12040390131bSFrank Mayhar 	if (sbi->s_journal) {
120597abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1206ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
120747b4a50bSJan Kara 		sbi->s_journal = NULL;
1208878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
120954d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
12100390131bSFrank Mayhar 		}
1211878520acSTheodore Ts'o 	}
1212d4edac31SJosef Bacik 
1213d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
12149105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1215d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1216d4edac31SJosef Bacik 	ext4_mb_release(sb);
1217d4edac31SJosef Bacik 	ext4_ext_release(sb);
1218d4edac31SJosef Bacik 
1219bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1220e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
1221ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1222ac27a0ecSDave Kleikamp 	}
1223bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
1224a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
1225a8e25a83SArtem Bityutskiy 
12261d0c3924STheodore Ts'o 	rcu_read_lock();
12271d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
1228ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
12291d0c3924STheodore Ts'o 		brelse(group_desc[i]);
12301d0c3924STheodore Ts'o 	kvfree(group_desc);
12317c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
12327c990728SSuraj Jitindar Singh 	if (flex_groups) {
12337c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
12347c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
12357c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
12367c990728SSuraj Jitindar Singh 	}
12371d0c3924STheodore Ts'o 	rcu_read_unlock();
123857042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1239ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1240ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
124157042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1242bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1243ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1244a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
124533458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1246ac27a0ecSDave Kleikamp #endif
1247ac27a0ecSDave Kleikamp 
1248ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1249ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1250ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1251ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1252ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1253ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1254ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
1255ac27a0ecSDave Kleikamp 
125689d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1257f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1258ee7ed3aaSChunguang Xu 	if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
1259ac27a0ecSDave Kleikamp 		/*
1260ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1261ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1262ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1263ac27a0ecSDave Kleikamp 		 */
1264ee7ed3aaSChunguang Xu 		sync_blockdev(sbi->s_journal_bdev);
1265ee7ed3aaSChunguang Xu 		invalidate_bdev(sbi->s_journal_bdev);
1266617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1267ac27a0ecSDave Kleikamp 	}
126850c15df6SChengguang Xu 
1269dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1270dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
127150c15df6SChengguang Xu 
127247387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
127347387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
127450c15df6SChengguang Xu 
1275c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1276c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
12779060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1278ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
12793197ebdbSTheodore Ts'o 	/*
12803197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
12813197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
12823197ebdbSTheodore Ts'o 	 */
12833197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
12843197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
12850441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
12860441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1287705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
12885e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1289ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
1290c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1291f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
1292c83ad55eSGabriel Krisman Bertazi #endif
1293ac27a0ecSDave Kleikamp 	kfree(sbi);
1294ac27a0ecSDave Kleikamp }
1295ac27a0ecSDave Kleikamp 
1296e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1297ac27a0ecSDave Kleikamp 
1298ac27a0ecSDave Kleikamp /*
1299ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1300ac27a0ecSDave Kleikamp  */
1301617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1302ac27a0ecSDave Kleikamp {
1303617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1304ac27a0ecSDave Kleikamp 
1305e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1306ac27a0ecSDave Kleikamp 	if (!ei)
1307ac27a0ecSDave Kleikamp 		return NULL;
13080b8e58a1SAndreas Dilger 
1309ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1310202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1311c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
131227bc446eSbrookxu 	atomic_set(&ei->i_prealloc_active, 0);
1313c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
13149a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
13159a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1316edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1317eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1318edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1319dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1320d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1321d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
13221dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1323a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1324a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
132596c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1326a9e7f447SDmitry Monakhov #endif
13278aefcd55STheodore Ts'o 	ei->jinode = NULL;
13282e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1329744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1330b436b9beSJan Kara 	ei->i_sync_tid = 0;
1331b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1332e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
13332e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1334aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1335aa75f4d3SHarshad Shirwadkar 	mutex_init(&ei->i_fc_lock);
1336ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1337ac27a0ecSDave Kleikamp }
1338ac27a0ecSDave Kleikamp 
13397ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
13407ff9c073STheodore Ts'o {
13417ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
13427ff9c073STheodore Ts'o 
134329b3692eSEric Biggers 	if (!drop)
134429b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
134529b3692eSEric Biggers 
13467ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
13477ff9c073STheodore Ts'o 	return drop;
13487ff9c073STheodore Ts'o }
13497ff9c073STheodore Ts'o 
135094053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1351fa0d7e3dSNick Piggin {
13522c58d548SEric Biggers 	fscrypt_free_inode(inode);
1353aa75f4d3SHarshad Shirwadkar 	if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1354aa75f4d3SHarshad Shirwadkar 		pr_warn("%s: inode %ld still in fc list",
1355aa75f4d3SHarshad Shirwadkar 			__func__, inode->i_ino);
1356aa75f4d3SHarshad Shirwadkar 	}
1357fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1358fa0d7e3dSNick Piggin }
1359fa0d7e3dSNick Piggin 
1360617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1361ac27a0ecSDave Kleikamp {
13629f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1363b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1364b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1365b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
13669f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
13679f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
13689f7dd93dSVasily Averin 				true);
13699f7dd93dSVasily Averin 		dump_stack();
13709f7dd93dSVasily Averin 	}
1371ac27a0ecSDave Kleikamp }
1372ac27a0ecSDave Kleikamp 
137351cc5068SAlexey Dobriyan static void init_once(void *foo)
1374ac27a0ecSDave Kleikamp {
1375617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1376ac27a0ecSDave Kleikamp 
1377ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1378ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
13790e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1380ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1381ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1382aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1383ac27a0ecSDave Kleikamp }
1384ac27a0ecSDave Kleikamp 
1385e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1386ac27a0ecSDave Kleikamp {
1387f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1388f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1389f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1390f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1391f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1392f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
139320c2df83SPaul Mundt 				init_once);
1394617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1395ac27a0ecSDave Kleikamp 		return -ENOMEM;
1396ac27a0ecSDave Kleikamp 	return 0;
1397ac27a0ecSDave Kleikamp }
1398ac27a0ecSDave Kleikamp 
1399ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1400ac27a0ecSDave Kleikamp {
14018c0a8537SKirill A. Shutemov 	/*
14028c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
14038c0a8537SKirill A. Shutemov 	 * destroy cache.
14048c0a8537SKirill A. Shutemov 	 */
14058c0a8537SKirill A. Shutemov 	rcu_barrier();
1406617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1407ac27a0ecSDave Kleikamp }
1408ac27a0ecSDave Kleikamp 
14090930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1410ac27a0ecSDave Kleikamp {
1411aa75f4d3SHarshad Shirwadkar 	ext4_fc_del(inode);
14120930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1413dbd5768fSJan Kara 	clear_inode(inode);
141427bc446eSbrookxu 	ext4_discard_preallocations(inode, 0);
141551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1416f4c2d372SJan Kara 	dquot_drop(inode);
14178aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
14188aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
14198aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
14208aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
14218aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
14228aefcd55STheodore Ts'o 	}
14233d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1424c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1425ac27a0ecSDave Kleikamp }
1426ac27a0ecSDave Kleikamp 
14271b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
14281b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1429ac27a0ecSDave Kleikamp {
1430ac27a0ecSDave Kleikamp 	struct inode *inode;
1431ac27a0ecSDave Kleikamp 
14328a363970STheodore Ts'o 	/*
1433ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1434ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1435ac27a0ecSDave Kleikamp 	 */
14368a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
14371d1fe1eeSDavid Howells 	if (IS_ERR(inode))
14381d1fe1eeSDavid Howells 		return ERR_CAST(inode);
14391d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1440ac27a0ecSDave Kleikamp 		iput(inode);
1441ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1442ac27a0ecSDave Kleikamp 	}
14431b961ac0SChristoph Hellwig 
14441b961ac0SChristoph Hellwig 	return inode;
1445ac27a0ecSDave Kleikamp }
14461b961ac0SChristoph Hellwig 
14471b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
14481b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
14491b961ac0SChristoph Hellwig {
14501b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
14511b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
14521b961ac0SChristoph Hellwig }
14531b961ac0SChristoph Hellwig 
14541b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
14551b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
14561b961ac0SChristoph Hellwig {
14571b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
14581b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1459ac27a0ecSDave Kleikamp }
1460ac27a0ecSDave Kleikamp 
1461fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1462fde87268STheodore Ts'o {
1463fde87268STheodore Ts'o 	struct writeback_control wbc = {
1464fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1465fde87268STheodore Ts'o 	};
1466fde87268STheodore Ts'o 
1467fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1468fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1469fde87268STheodore Ts'o }
1470fde87268STheodore Ts'o 
1471c39a7f84SToshiyuki Okajima /*
1472c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1473c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1474c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1475c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1476c39a7f84SToshiyuki Okajima  */
14770b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
14780b8e58a1SAndreas Dilger 				 gfp_t wait)
1479c39a7f84SToshiyuki Okajima {
1480c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1481c39a7f84SToshiyuki Okajima 
1482c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1483c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1484c39a7f84SToshiyuki Okajima 		return 0;
1485c39a7f84SToshiyuki Okajima 	if (journal)
1486529a781eSzhangyi (F) 		return jbd2_journal_try_to_free_buffers(journal, page);
1487529a781eSzhangyi (F) 
1488c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1489c39a7f84SToshiyuki Okajima }
1490c39a7f84SToshiyuki Okajima 
1491643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1492a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1493a7550b30SJaegeuk Kim {
1494a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1495a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1496a7550b30SJaegeuk Kim }
1497a7550b30SJaegeuk Kim 
1498a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1499a7550b30SJaegeuk Kim 							void *fs_data)
1500a7550b30SJaegeuk Kim {
15012f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1502c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1503a7550b30SJaegeuk Kim 
15049ce0151aSEric Biggers 	/*
15059ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
15069ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
15079ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
15089ce0151aSEric Biggers 	 * the filename "lost+found" itself.
15099ce0151aSEric Biggers 	 */
15109ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
15119ce0151aSEric Biggers 		return -EPERM;
1512a7550b30SJaegeuk Kim 
15137d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
15147d3e06a8SRoss Zwisler 		return -EINVAL;
15157d3e06a8SRoss Zwisler 
1516b383a73fSIra Weiny 	if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1517b383a73fSIra Weiny 		return -EOPNOTSUPP;
1518b383a73fSIra Weiny 
151994840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
152094840e3cSEric Biggers 	if (res)
152194840e3cSEric Biggers 		return res;
152294840e3cSEric Biggers 
15232f8f5e76SEric Biggers 	/*
15242f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
15252f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
15262f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
15272f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
15282f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
15292f8f5e76SEric Biggers 	 */
15302f8f5e76SEric Biggers 
15312f8f5e76SEric Biggers 	if (handle) {
15322f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
15332f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
15342f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
15352f8f5e76SEric Biggers 					    ctx, len, 0);
1536a7550b30SJaegeuk Kim 		if (!res) {
1537a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1538a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1539a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1540a3caa24bSJan Kara 			/*
15412ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
15422ee6a576SEric Biggers 			 * S_DAX may be disabled
1543a3caa24bSJan Kara 			 */
1544043546e4SIra Weiny 			ext4_set_inode_flags(inode, false);
1545a7550b30SJaegeuk Kim 		}
1546a7550b30SJaegeuk Kim 		return res;
1547a7550b30SJaegeuk Kim 	}
1548a7550b30SJaegeuk Kim 
1549b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1550b8cb5a54STahsin Erdogan 	if (res)
1551b8cb5a54STahsin Erdogan 		return res;
15522f8f5e76SEric Biggers retry:
1553af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1554af65207cSTahsin Erdogan 				     &credits);
1555dec214d0STahsin Erdogan 	if (res)
1556dec214d0STahsin Erdogan 		return res;
1557dec214d0STahsin Erdogan 
1558c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1559a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1560a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1561a7550b30SJaegeuk Kim 
15622f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
15632f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
15642f8f5e76SEric Biggers 				    ctx, len, 0);
1565a7550b30SJaegeuk Kim 	if (!res) {
1566a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
15672ee6a576SEric Biggers 		/*
15682ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
15692ee6a576SEric Biggers 		 * S_DAX may be disabled
15702ee6a576SEric Biggers 		 */
1571043546e4SIra Weiny 		ext4_set_inode_flags(inode, false);
1572a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1573a7550b30SJaegeuk Kim 		if (res)
1574a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1575a7550b30SJaegeuk Kim 	}
1576a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
15772f8f5e76SEric Biggers 
15782f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
15792f8f5e76SEric Biggers 		goto retry;
1580a7550b30SJaegeuk Kim 	if (!res)
1581a7550b30SJaegeuk Kim 		res = res2;
1582a7550b30SJaegeuk Kim 	return res;
1583a7550b30SJaegeuk Kim }
1584a7550b30SJaegeuk Kim 
1585ac4acb1fSEric Biggers static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
1586a7550b30SJaegeuk Kim {
1587ac4acb1fSEric Biggers 	return EXT4_SB(sb)->s_dummy_enc_policy.policy;
1588a7550b30SJaegeuk Kim }
1589a7550b30SJaegeuk Kim 
1590b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1591b925acb8SEric Biggers {
1592b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1593b925acb8SEric Biggers }
1594b925acb8SEric Biggers 
1595b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1596b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1597b925acb8SEric Biggers {
1598b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1599b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1600b925acb8SEric Biggers }
1601b925acb8SEric Biggers 
16026f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1603a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1604a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1605a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1606ac4acb1fSEric Biggers 	.get_dummy_policy	= ext4_get_dummy_policy,
1607a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1608e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1609b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1610b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1611a7550b30SJaegeuk Kim };
1612a7550b30SJaegeuk Kim #endif
1613a7550b30SJaegeuk Kim 
1614ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1615d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1616689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1617ac27a0ecSDave Kleikamp 
1618617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1619617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1620617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1621617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1622617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
16236f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
16248c54ca9cSAl Viro 			 const struct path *path);
1625617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1626617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1627ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1628617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1629ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
16307c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
16317c319d32SAditya Kali 			     unsigned int flags);
16327c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1633ac27a0ecSDave Kleikamp 
163496c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
163596c7e0d9SJan Kara {
163696c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
163796c7e0d9SJan Kara }
163896c7e0d9SJan Kara 
163961e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
164060e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1641617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1642617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1643617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1644617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1645a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1646a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1647a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1648040cb378SLi Xi 	.get_projid		= ext4_get_projid,
16497a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1650ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1651ac27a0ecSDave Kleikamp };
1652ac27a0ecSDave Kleikamp 
16530d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1654617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1655ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1656287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
16570a240339SJan Kara 	.get_state	= dquot_get_state,
1658287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1659287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
16606332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
16616332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1662ac27a0ecSDave Kleikamp };
1663ac27a0ecSDave Kleikamp #endif
1664ac27a0ecSDave Kleikamp 
1665ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1666617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
166794053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1668617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1669617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1670617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
16717ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
16720930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1673617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1674617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1675c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1676c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1677617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1678617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1679617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1680ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1681617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1682617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
168396c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1684ac27a0ecSDave Kleikamp #endif
1685c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1686ac27a0ecSDave Kleikamp };
1687ac27a0ecSDave Kleikamp 
168839655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
16891b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
16901b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1691617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1692fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1693ac27a0ecSDave Kleikamp };
1694ac27a0ecSDave Kleikamp 
1695ac27a0ecSDave Kleikamp enum {
1696ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1697ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
169872578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1699ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
170072578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1701ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1702ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1703ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
17046ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
17054f74d15fSEric Biggers 	Opt_inlinecrypt,
1706ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
17075a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1708ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
17099cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
17109cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1711327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1712327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1713670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
17141449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
17155328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1716744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1717fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1718cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1719995a3ed6SHarshad Shirwadkar 	Opt_prefetch_block_bitmaps, Opt_no_fc,
17208016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
17210f0672ffSHarshad Shirwadkar 	Opt_fc_debug_max_replay,
17228016e29fSHarshad Shirwadkar #endif
17230f0672ffSHarshad Shirwadkar 	Opt_fc_debug_force
1724ac27a0ecSDave Kleikamp };
1725ac27a0ecSDave Kleikamp 
1726a447c093SSteven Whitehouse static const match_table_t tokens = {
1727ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1728ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1729ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1730ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1731ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1732ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1733ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1734ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1735ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1736ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1737ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1738ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1739ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1740ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
174172578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
174272578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1743ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1744ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1745ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1746ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1747e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
17485a916be1STheodore Ts'o 	{Opt_noload, "noload"},
174972578c33STheodore Ts'o 	{Opt_removed, "nobh"},
175072578c33STheodore Ts'o 	{Opt_removed, "bh"},
1751ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
175230773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
175330773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1754ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1755ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1756818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1757c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1758818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1759ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1760ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1761ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1762ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
17635bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
17645bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1765ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1766ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1767ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1768ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1769ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1770ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
17715a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1772ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1773ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1774ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1775ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
177649da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1777ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
177806705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
177906705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
178025ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1781923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
17829cb20f94SIra Weiny 	{Opt_dax_always, "dax=always"},
17839cb20f94SIra Weiny 	{Opt_dax_inode, "dax=inode"},
17849cb20f94SIra Weiny 	{Opt_dax_never, "dax=never"},
1785c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
178664769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1787327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1788327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1789a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1790a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1791670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1792dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
179336ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
179436ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
17956fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
17966fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1797240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1798b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1799afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
180006705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
180106705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1802744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1803244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1804744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
18055328e635SEric Sandeen 	{Opt_discard, "discard"},
18065328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1807fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1808fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1809fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1810995a3ed6SHarshad Shirwadkar 	{Opt_no_fc, "no_fc"},
18110f0672ffSHarshad Shirwadkar 	{Opt_fc_debug_force, "fc_debug_force"},
18128016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
18138016e29fSHarshad Shirwadkar 	{Opt_fc_debug_max_replay, "fc_debug_max_replay=%u"},
18148016e29fSHarshad Shirwadkar #endif
1815df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1816ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
18176ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
18184f74d15fSEric Biggers 	{Opt_inlinecrypt, "inlinecrypt"},
1819cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1820cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
18213d392b26STheodore Ts'o 	{Opt_prefetch_block_bitmaps, "prefetch_block_bitmaps"},
1822c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1823c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1824c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1825c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1826c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1827f3f12faaSJosef Bacik 	{Opt_err, NULL},
1828ac27a0ecSDave Kleikamp };
1829ac27a0ecSDave Kleikamp 
1830617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1831ac27a0ecSDave Kleikamp {
1832617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1833ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1834ac27a0ecSDave Kleikamp 
1835ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1836ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
18370b8e58a1SAndreas Dilger 
1838ac27a0ecSDave Kleikamp 	options += 3;
18390b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1840ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1841ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
18424776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1843ac27a0ecSDave Kleikamp 		       (char *) *data);
1844ac27a0ecSDave Kleikamp 		return 1;
1845ac27a0ecSDave Kleikamp 	}
1846ac27a0ecSDave Kleikamp 	if (*options == ',')
1847ac27a0ecSDave Kleikamp 		options++;
1848ac27a0ecSDave Kleikamp 	*data = (void *) options;
18490b8e58a1SAndreas Dilger 
1850ac27a0ecSDave Kleikamp 	return sb_block;
1851ac27a0ecSDave Kleikamp }
1852ac27a0ecSDave Kleikamp 
1853b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1854d6006186SEric Biggers static const char deprecated_msg[] =
1855d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1856437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1857b3881f74STheodore Ts'o 
185856c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
185956c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
186056c50f11SDmitry Monakhov {
186156c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
186233458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
186303dafb5fSChen Gang 	int ret = -1;
186456c50f11SDmitry Monakhov 
186533458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
186656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
186756c50f11SDmitry Monakhov 			"Cannot change journaled "
186856c50f11SDmitry Monakhov 			"quota options when quota turned on");
186957f73c2cSTheodore Ts'o 		return -1;
187056c50f11SDmitry Monakhov 	}
1871e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1872c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1873c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1874c325a67cSTheodore Ts'o 		return 1;
1875262b4662SJan Kara 	}
187656c50f11SDmitry Monakhov 	qname = match_strdup(args);
187756c50f11SDmitry Monakhov 	if (!qname) {
187856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
187956c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
188057f73c2cSTheodore Ts'o 		return -1;
188156c50f11SDmitry Monakhov 	}
188233458eabSTheodore Ts'o 	if (old_qname) {
188333458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
188403dafb5fSChen Gang 			ret = 1;
188503dafb5fSChen Gang 		else
188656c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
188703dafb5fSChen Gang 				 "%s quota file already specified",
188803dafb5fSChen Gang 				 QTYPE2NAME(qtype));
188903dafb5fSChen Gang 		goto errout;
189056c50f11SDmitry Monakhov 	}
189103dafb5fSChen Gang 	if (strchr(qname, '/')) {
189256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
189356c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
189403dafb5fSChen Gang 		goto errout;
189556c50f11SDmitry Monakhov 	}
189633458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1897fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
189856c50f11SDmitry Monakhov 	return 1;
189903dafb5fSChen Gang errout:
190003dafb5fSChen Gang 	kfree(qname);
190103dafb5fSChen Gang 	return ret;
190256c50f11SDmitry Monakhov }
190356c50f11SDmitry Monakhov 
190456c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
190556c50f11SDmitry Monakhov {
190656c50f11SDmitry Monakhov 
190756c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
190833458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
190956c50f11SDmitry Monakhov 
191033458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
191156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
191256c50f11SDmitry Monakhov 			" when quota turned on");
191357f73c2cSTheodore Ts'o 		return -1;
191456c50f11SDmitry Monakhov 	}
191533458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
191633458eabSTheodore Ts'o 	synchronize_rcu();
191733458eabSTheodore Ts'o 	kfree(old_qname);
191856c50f11SDmitry Monakhov 	return 1;
191956c50f11SDmitry Monakhov }
192056c50f11SDmitry Monakhov #endif
192156c50f11SDmitry Monakhov 
192226092bf5STheodore Ts'o #define MOPT_SET	0x0001
192326092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
192426092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
192526092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
192626092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
192726092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
192826092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
192926092bf5STheodore Ts'o #define MOPT_Q		0
193026092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
193126092bf5STheodore Ts'o #else
193226092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
193326092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
193426092bf5STheodore Ts'o #endif
193526092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
19368dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
19378dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
19388dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1939ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
19409cb20f94SIra Weiny #define MOPT_SKIP	0x0800
1941995a3ed6SHarshad Shirwadkar #define	MOPT_2		0x1000
194226092bf5STheodore Ts'o 
194326092bf5STheodore Ts'o static const struct mount_opts {
194426092bf5STheodore Ts'o 	int	token;
194526092bf5STheodore Ts'o 	int	mount_opt;
194626092bf5STheodore Ts'o 	int	flags;
194726092bf5STheodore Ts'o } ext4_mount_opts[] = {
194826092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
194926092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
195026092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
195126092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
195226092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
195326092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
19548dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
19558dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
19568dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
19578dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
195826092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
195926092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
19608dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
19618dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
19628dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
196359d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1964327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1965327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1966c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1967c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
19688dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
19691e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
197026092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
19718dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
19721e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
19738dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
197426092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
197526092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
197626092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
19778dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
19787915a861SAles Novak 	 MOPT_NO_EXT2},
19798dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
19807915a861SAles Novak 	 MOPT_NO_EXT2},
198126092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
198226092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
198326092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
198426092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
198526092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
198626092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
198726092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
198826092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
198926092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
199026092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
19919cb20f94SIra Weiny 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
19929cb20f94SIra Weiny 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
19939cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
19949cb20f94SIra Weiny 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
19959cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
19969cb20f94SIra Weiny 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
19979cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
199826092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
19990efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
20000efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
20015ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
20025ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
20035ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
20048dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
20058dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
20068dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
20078dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
200826092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
200926092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
201026092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
201126092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
201226092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
201326092bf5STheodore Ts'o #else
201426092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
201526092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
201626092bf5STheodore Ts'o #endif
201726092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
201826092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
2019670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
202026092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
202126092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
202226092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
202326092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
202426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
202549da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
202649da9392SJan Kara 							MOPT_SET | MOPT_Q},
202726092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
202849da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
202949da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
2030174fe5baSKaixu Xia 	{Opt_usrjquota, 0, MOPT_Q | MOPT_STRING},
2031174fe5baSKaixu Xia 	{Opt_grpjquota, 0, MOPT_Q | MOPT_STRING},
203226092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
203326092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
203426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
203526092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
203626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
2037df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
2038ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, 0, MOPT_STRING},
2039cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
20403d392b26STheodore Ts'o 	{Opt_prefetch_block_bitmaps, EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS,
20413d392b26STheodore Ts'o 	 MOPT_SET},
2042995a3ed6SHarshad Shirwadkar 	{Opt_no_fc, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
2043995a3ed6SHarshad Shirwadkar 	 MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},
20440f0672ffSHarshad Shirwadkar 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
20450f0672ffSHarshad Shirwadkar 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
20468016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
20478016e29fSHarshad Shirwadkar 	{Opt_fc_debug_max_replay, 0, MOPT_GTE0},
20488016e29fSHarshad Shirwadkar #endif
204926092bf5STheodore Ts'o 	{Opt_err, 0, 0}
205026092bf5STheodore Ts'o };
205126092bf5STheodore Ts'o 
2052c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
2053c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
2054c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
2055c83ad55eSGabriel Krisman Bertazi 	char *name;
2056c83ad55eSGabriel Krisman Bertazi 	char *version;
2057c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
2058c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
2059c83ad55eSGabriel Krisman Bertazi };
2060c83ad55eSGabriel Krisman Bertazi 
2061c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
2062c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
2063c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
2064c83ad55eSGabriel Krisman Bertazi {
2065c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
2066c83ad55eSGabriel Krisman Bertazi 	int i;
2067c83ad55eSGabriel Krisman Bertazi 
2068c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
2069c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
2070c83ad55eSGabriel Krisman Bertazi 			break;
2071c83ad55eSGabriel Krisman Bertazi 
2072c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
2073c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
2074c83ad55eSGabriel Krisman Bertazi 
2075c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
2076c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
2077c83ad55eSGabriel Krisman Bertazi 
2078c83ad55eSGabriel Krisman Bertazi 	return 0;
2079c83ad55eSGabriel Krisman Bertazi }
2080c83ad55eSGabriel Krisman Bertazi #endif
2081c83ad55eSGabriel Krisman Bertazi 
2082ed318a6cSEric Biggers static int ext4_set_test_dummy_encryption(struct super_block *sb,
2083ed318a6cSEric Biggers 					  const char *opt,
2084ed318a6cSEric Biggers 					  const substring_t *arg,
2085ed318a6cSEric Biggers 					  bool is_remount)
2086ed318a6cSEric Biggers {
2087ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
2088ed318a6cSEric Biggers 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2089ed318a6cSEric Biggers 	int err;
2090ed318a6cSEric Biggers 
2091ed318a6cSEric Biggers 	/*
2092ed318a6cSEric Biggers 	 * This mount option is just for testing, and it's not worthwhile to
2093ed318a6cSEric Biggers 	 * implement the extra complexity (e.g. RCU protection) that would be
2094ed318a6cSEric Biggers 	 * needed to allow it to be set or changed during remount.  We do allow
2095ed318a6cSEric Biggers 	 * it to be specified during remount, but only if there is no change.
2096ed318a6cSEric Biggers 	 */
2097ac4acb1fSEric Biggers 	if (is_remount && !sbi->s_dummy_enc_policy.policy) {
2098ed318a6cSEric Biggers 		ext4_msg(sb, KERN_WARNING,
2099ed318a6cSEric Biggers 			 "Can't set test_dummy_encryption on remount");
2100ed318a6cSEric Biggers 		return -1;
2101ed318a6cSEric Biggers 	}
2102c8c868abSEric Biggers 	err = fscrypt_set_test_dummy_encryption(sb, arg->from,
2103ac4acb1fSEric Biggers 						&sbi->s_dummy_enc_policy);
2104ed318a6cSEric Biggers 	if (err) {
2105ed318a6cSEric Biggers 		if (err == -EEXIST)
2106ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2107ed318a6cSEric Biggers 				 "Can't change test_dummy_encryption on remount");
2108ed318a6cSEric Biggers 		else if (err == -EINVAL)
2109ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2110ed318a6cSEric Biggers 				 "Value of option \"%s\" is unrecognized", opt);
2111ed318a6cSEric Biggers 		else
2112ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2113ed318a6cSEric Biggers 				 "Error processing option \"%s\" [%d]",
2114ed318a6cSEric Biggers 				 opt, err);
2115ed318a6cSEric Biggers 		return -1;
2116ed318a6cSEric Biggers 	}
2117ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2118ed318a6cSEric Biggers #else
2119ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING,
2120ed318a6cSEric Biggers 		 "Test dummy encryption mount option ignored");
2121ed318a6cSEric Biggers #endif
2122ed318a6cSEric Biggers 	return 1;
2123ed318a6cSEric Biggers }
2124ed318a6cSEric Biggers 
212526092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
212626092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
212726092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
212826092bf5STheodore Ts'o {
212926092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
213026092bf5STheodore Ts'o 	const struct mount_opts *m;
213108cefc7aSEric W. Biederman 	kuid_t uid;
213208cefc7aSEric W. Biederman 	kgid_t gid;
213326092bf5STheodore Ts'o 	int arg = 0;
213426092bf5STheodore Ts'o 
213557f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
213657f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
213757f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
213857f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
213957f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
214057f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
214157f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
214257f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
214357f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
214457f73c2cSTheodore Ts'o #endif
214526092bf5STheodore Ts'o 	switch (token) {
2146f7048605STheodore Ts'o 	case Opt_noacl:
2147f7048605STheodore Ts'o 	case Opt_nouser_xattr:
2148f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
2149f7048605STheodore Ts'o 		break;
215026092bf5STheodore Ts'o 	case Opt_sb:
215126092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
215226092bf5STheodore Ts'o 	case Opt_removed:
21535f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
215426092bf5STheodore Ts'o 		return 1;
215526092bf5STheodore Ts'o 	case Opt_abort:
215626092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
215726092bf5STheodore Ts'o 		return 1;
215826092bf5STheodore Ts'o 	case Opt_i_version:
2159357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
216026092bf5STheodore Ts'o 		return 1;
2161a26f4992STheodore Ts'o 	case Opt_lazytime:
21621751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
2163a26f4992STheodore Ts'o 		return 1;
2164a26f4992STheodore Ts'o 	case Opt_nolazytime:
21651751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
2166a26f4992STheodore Ts'o 		return 1;
21674f74d15fSEric Biggers 	case Opt_inlinecrypt:
21684f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
21694f74d15fSEric Biggers 		sb->s_flags |= SB_INLINECRYPT;
21704f74d15fSEric Biggers #else
21714f74d15fSEric Biggers 		ext4_msg(sb, KERN_ERR, "inline encryption not supported");
21724f74d15fSEric Biggers #endif
21734f74d15fSEric Biggers 		return 1;
217426092bf5STheodore Ts'o 	}
217526092bf5STheodore Ts'o 
21765f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
21775f3633e3SJan Kara 		if (token == m->token)
21785f3633e3SJan Kara 			break;
21795f3633e3SJan Kara 
21805f3633e3SJan Kara 	if (m->token == Opt_err) {
21815f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
21825f3633e3SJan Kara 			 "or missing value", opt);
21835f3633e3SJan Kara 		return -1;
21845f3633e3SJan Kara 	}
21855f3633e3SJan Kara 
21868dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
21878dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
21888dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
21898dc0aa8cSTheodore Ts'o 		return -1;
21908dc0aa8cSTheodore Ts'o 	}
21918dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
21928dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
21938dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
21948dc0aa8cSTheodore Ts'o 		return -1;
21958dc0aa8cSTheodore Ts'o 	}
21968dc0aa8cSTheodore Ts'o 
2197ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
21980efb3b23SJan Kara 		return -1;
219926092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
220026092bf5STheodore Ts'o 		return -1;
2201c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
2202c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
220326092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
22041e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
22051e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
2206c93cf2d7SDmitry Monakhov 		} else
2207c93cf2d7SDmitry Monakhov 			return -1;
2208c93cf2d7SDmitry Monakhov 	}
220926092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
221026092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
221126092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
221226092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
221326092bf5STheodore Ts'o 			 "options when quota turned on");
221426092bf5STheodore Ts'o 		return -1;
221526092bf5STheodore Ts'o 	}
221626092bf5STheodore Ts'o 
221726092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
221826092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
221926092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
222026092bf5STheodore Ts'o 		if (arg == 0)
222126092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
22229ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
22239ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
22249ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
22259ba55543Szhangyi (F) 				 "must be smaller than %d",
22269ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
22279ba55543Szhangyi (F) 			return -1;
22289ba55543Szhangyi (F) 		}
222926092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
2230670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
22319803387cSTheodore Ts'o 		if ((arg & 1) ||
22329803387cSTheodore Ts'o 		    (arg < 4) ||
22339803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
22349803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
22359803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
22369803387cSTheodore Ts'o 			return -1;
22379803387cSTheodore Ts'o 		}
2238670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
223926092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
224026092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
224126092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
224226092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
224326092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
2244e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2245e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
2246e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2247e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
224826092bf5STheodore Ts'o 			return -1;
224926092bf5STheodore Ts'o 		}
225026092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
225126092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
225226092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
225326092bf5STheodore Ts'o 		if (!args->from)
225426092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
225526092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
2256df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
2257df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
22588016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
22598016e29fSHarshad Shirwadkar 	} else if (token == Opt_fc_debug_max_replay) {
22608016e29fSHarshad Shirwadkar 		sbi->s_fc_debug_max_replay = arg;
22618016e29fSHarshad Shirwadkar #endif
226226092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
226326092bf5STheodore Ts'o 		sbi->s_stripe = arg;
22640efb3b23SJan Kara 	} else if (token == Opt_resuid) {
22650efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
22660efb3b23SJan Kara 		if (!uid_valid(uid)) {
22675f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
22680efb3b23SJan Kara 			return -1;
22690efb3b23SJan Kara 		}
22700efb3b23SJan Kara 		sbi->s_resuid = uid;
22710efb3b23SJan Kara 	} else if (token == Opt_resgid) {
22720efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
22730efb3b23SJan Kara 		if (!gid_valid(gid)) {
22745f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
22750efb3b23SJan Kara 			return -1;
22760efb3b23SJan Kara 		}
22770efb3b23SJan Kara 		sbi->s_resgid = gid;
22780efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
22790efb3b23SJan Kara 		if (is_remount) {
22800efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
22810efb3b23SJan Kara 				 "Cannot specify journal on remount");
22820efb3b23SJan Kara 			return -1;
22830efb3b23SJan Kara 		}
22840efb3b23SJan Kara 		*journal_devnum = arg;
2285ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2286ad4eec61SEric Sandeen 		char *journal_path;
2287ad4eec61SEric Sandeen 		struct inode *journal_inode;
2288ad4eec61SEric Sandeen 		struct path path;
2289ad4eec61SEric Sandeen 		int error;
2290ad4eec61SEric Sandeen 
2291ad4eec61SEric Sandeen 		if (is_remount) {
2292ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2293ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2294ad4eec61SEric Sandeen 			return -1;
2295ad4eec61SEric Sandeen 		}
2296ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2297ad4eec61SEric Sandeen 		if (!journal_path) {
2298ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2299ad4eec61SEric Sandeen 				"journal device string");
2300ad4eec61SEric Sandeen 			return -1;
2301ad4eec61SEric Sandeen 		}
2302ad4eec61SEric Sandeen 
2303ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2304ad4eec61SEric Sandeen 		if (error) {
2305ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2306ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2307ad4eec61SEric Sandeen 			kfree(journal_path);
2308ad4eec61SEric Sandeen 			return -1;
2309ad4eec61SEric Sandeen 		}
2310ad4eec61SEric Sandeen 
23112b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2312ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2313ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2314ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2315ad4eec61SEric Sandeen 			path_put(&path);
2316ad4eec61SEric Sandeen 			kfree(journal_path);
2317ad4eec61SEric Sandeen 			return -1;
2318ad4eec61SEric Sandeen 		}
2319ad4eec61SEric Sandeen 
2320ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2321ad4eec61SEric Sandeen 		path_put(&path);
2322ad4eec61SEric Sandeen 		kfree(journal_path);
23230efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
23240efb3b23SJan Kara 		if (arg > 7) {
23255f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
23260efb3b23SJan Kara 				 " (must be 0-7)");
23270efb3b23SJan Kara 			return -1;
23280efb3b23SJan Kara 		}
23290efb3b23SJan Kara 		*journal_ioprio =
23300efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
23316ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2332ed318a6cSEric Biggers 		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2333ed318a6cSEric Biggers 						      is_remount);
233426092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
233526092bf5STheodore Ts'o 		if (is_remount) {
233626092bf5STheodore Ts'o 			if (!sbi->s_journal)
233726092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
23385f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
233926092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
234026092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
234126092bf5STheodore Ts'o 				return -1;
234226092bf5STheodore Ts'o 			}
234326092bf5STheodore Ts'o 		} else {
234426092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
234526092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
234626092bf5STheodore Ts'o 		}
234726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
234826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
234926092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
235026092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
23515f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
23525f3633e3SJan Kara 				 "quota options when quota turned on");
235326092bf5STheodore Ts'o 			return -1;
235426092bf5STheodore Ts'o 		}
2355e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2356c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2357c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2358262b4662SJan Kara 				 "when QUOTA feature is enabled");
2359c325a67cSTheodore Ts'o 			return 1;
2360262b4662SJan Kara 		}
236126092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
236226092bf5STheodore Ts'o #endif
23639cb20f94SIra Weiny 	} else if (token == Opt_dax || token == Opt_dax_always ||
23649cb20f94SIra Weiny 		   token == Opt_dax_inode || token == Opt_dax_never) {
2365ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
23669cb20f94SIra Weiny 		switch (token) {
23679cb20f94SIra Weiny 		case Opt_dax:
23689cb20f94SIra Weiny 		case Opt_dax_always:
2369829b37b8STheodore Ts'o 			if (is_remount &&
2370829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2371829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2372829b37b8STheodore Ts'o 			fail_dax_change_remount:
2373829b37b8STheodore Ts'o 				ext4_msg(sb, KERN_ERR, "can't change "
2374829b37b8STheodore Ts'o 					 "dax mount option while remounting");
2375829b37b8STheodore Ts'o 				return -1;
2376829b37b8STheodore Ts'o 			}
2377829b37b8STheodore Ts'o 			if (is_remount &&
2378829b37b8STheodore Ts'o 			    (test_opt(sb, DATA_FLAGS) ==
2379829b37b8STheodore Ts'o 			     EXT4_MOUNT_JOURNAL_DATA)) {
2380829b37b8STheodore Ts'o 				    ext4_msg(sb, KERN_ERR, "can't mount with "
2381829b37b8STheodore Ts'o 					     "both data=journal and dax");
2382829b37b8STheodore Ts'o 				    return -1;
2383829b37b8STheodore Ts'o 			}
2384ef83b6e8SDan Williams 			ext4_msg(sb, KERN_WARNING,
2385ef83b6e8SDan Williams 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
23869cb20f94SIra Weiny 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
23879cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
23889cb20f94SIra Weiny 			break;
23899cb20f94SIra Weiny 		case Opt_dax_never:
2390829b37b8STheodore Ts'o 			if (is_remount &&
2391829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2392829b37b8STheodore Ts'o 			     (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2393829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
23949cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
23959cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
23969cb20f94SIra Weiny 			break;
23979cb20f94SIra Weiny 		case Opt_dax_inode:
2398829b37b8STheodore Ts'o 			if (is_remount &&
2399829b37b8STheodore Ts'o 			    ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2400829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2401829b37b8STheodore Ts'o 			     !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2402829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
24039cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
24049cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
24059cb20f94SIra Weiny 			/* Strictly for printing options */
24069cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
24079cb20f94SIra Weiny 			break;
24089cb20f94SIra Weiny 		}
2409ef83b6e8SDan Williams #else
2410923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
24119cb20f94SIra Weiny 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
24129cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2413923ae0ffSRoss Zwisler 		return -1;
2414923ae0ffSRoss Zwisler #endif
24157915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
24167915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
24177915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
24187915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
241926092bf5STheodore Ts'o 	} else {
242026092bf5STheodore Ts'o 		if (!args->from)
242126092bf5STheodore Ts'o 			arg = 1;
242226092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
242326092bf5STheodore Ts'o 			arg = !arg;
242426092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
242526092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
242626092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
242726092bf5STheodore Ts'o 			WARN_ON(1);
242826092bf5STheodore Ts'o 			return -1;
242926092bf5STheodore Ts'o 		}
2430995a3ed6SHarshad Shirwadkar 		if (m->flags & MOPT_2) {
2431995a3ed6SHarshad Shirwadkar 			if (arg != 0)
2432995a3ed6SHarshad Shirwadkar 				sbi->s_mount_opt2 |= m->mount_opt;
2433995a3ed6SHarshad Shirwadkar 			else
2434995a3ed6SHarshad Shirwadkar 				sbi->s_mount_opt2 &= ~m->mount_opt;
2435995a3ed6SHarshad Shirwadkar 		} else {
243626092bf5STheodore Ts'o 			if (arg != 0)
243726092bf5STheodore Ts'o 				sbi->s_mount_opt |= m->mount_opt;
243826092bf5STheodore Ts'o 			else
243926092bf5STheodore Ts'o 				sbi->s_mount_opt &= ~m->mount_opt;
244026092bf5STheodore Ts'o 		}
2441995a3ed6SHarshad Shirwadkar 	}
244226092bf5STheodore Ts'o 	return 1;
244326092bf5STheodore Ts'o }
244426092bf5STheodore Ts'o 
2445ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2446c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2447b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2448661aa520SEric Sandeen 			 int is_remount)
2449ac27a0ecSDave Kleikamp {
24501e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
245133458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2452ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
245326092bf5STheodore Ts'o 	int token;
2454ac27a0ecSDave Kleikamp 
2455ac27a0ecSDave Kleikamp 	if (!options)
2456ac27a0ecSDave Kleikamp 		return 1;
2457ac27a0ecSDave Kleikamp 
2458ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2459ac27a0ecSDave Kleikamp 		if (!*p)
2460ac27a0ecSDave Kleikamp 			continue;
246115121c18SEric Sandeen 		/*
246215121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
246315121c18SEric Sandeen 		 * found; some options take optional arguments.
246415121c18SEric Sandeen 		 */
2465caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2466ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
246726092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
246826092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2469ac27a0ecSDave Kleikamp 			return 0;
2470ac27a0ecSDave Kleikamp 	}
2471ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
247249da9392SJan Kara 	/*
247349da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
247449da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
247549da9392SJan Kara 	 * to support legacy quotas in quota files.
247649da9392SJan Kara 	 */
247749da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
247849da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
247949da9392SJan Kara 			 "Cannot enable project quota enforcement.");
248049da9392SJan Kara 		return 0;
248149da9392SJan Kara 	}
248233458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
248333458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
248433458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
248533458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2486fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2487ac27a0ecSDave Kleikamp 
248833458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2489fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2490ac27a0ecSDave Kleikamp 
249156c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2492b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2493b31e1552SEric Sandeen 					"format mixing");
2494ac27a0ecSDave Kleikamp 			return 0;
2495ac27a0ecSDave Kleikamp 		}
2496ac27a0ecSDave Kleikamp 
2497ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2498b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2499b31e1552SEric Sandeen 					"not specified");
2500ac27a0ecSDave Kleikamp 			return 0;
2501ac27a0ecSDave Kleikamp 		}
2502ac27a0ecSDave Kleikamp 	}
2503ac27a0ecSDave Kleikamp #endif
2504626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2505626b035bSRitesh Harjani 		int blocksize =
2506626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2507626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2508626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2509626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2510626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2511626b035bSRitesh Harjani 	}
2512ac27a0ecSDave Kleikamp 	return 1;
2513ac27a0ecSDave Kleikamp }
2514ac27a0ecSDave Kleikamp 
25152adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
25162adf6da8STheodore Ts'o 					   struct super_block *sb)
25172adf6da8STheodore Ts'o {
25182adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
25192adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
252033458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
25212adf6da8STheodore Ts'o 
25222adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
25232adf6da8STheodore Ts'o 		char *fmtname = "";
25242adf6da8STheodore Ts'o 
25252adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
25262adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
25272adf6da8STheodore Ts'o 			fmtname = "vfsold";
25282adf6da8STheodore Ts'o 			break;
25292adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
25302adf6da8STheodore Ts'o 			fmtname = "vfsv0";
25312adf6da8STheodore Ts'o 			break;
25322adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
25332adf6da8STheodore Ts'o 			fmtname = "vfsv1";
25342adf6da8STheodore Ts'o 			break;
25352adf6da8STheodore Ts'o 		}
25362adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
25372adf6da8STheodore Ts'o 	}
25382adf6da8STheodore Ts'o 
253933458eabSTheodore Ts'o 	rcu_read_lock();
254033458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
254133458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
254233458eabSTheodore Ts'o 	if (usr_qf_name)
254333458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
254433458eabSTheodore Ts'o 	if (grp_qf_name)
254533458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
254633458eabSTheodore Ts'o 	rcu_read_unlock();
25472adf6da8STheodore Ts'o #endif
25482adf6da8STheodore Ts'o }
25492adf6da8STheodore Ts'o 
25505a916be1STheodore Ts'o static const char *token2str(int token)
25515a916be1STheodore Ts'o {
255250df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
25535a916be1STheodore Ts'o 
25545a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
25555a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
25565a916be1STheodore Ts'o 			break;
25575a916be1STheodore Ts'o 	return t->pattern;
25585a916be1STheodore Ts'o }
25595a916be1STheodore Ts'o 
25602adf6da8STheodore Ts'o /*
25612adf6da8STheodore Ts'o  * Show an option if
25622adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
25632adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
25642adf6da8STheodore Ts'o  */
256566acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
256666acdcf4STheodore Ts'o 			      int nodefs)
25672adf6da8STheodore Ts'o {
25682adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
25692adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
257068afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
25715a916be1STheodore Ts'o 	const struct mount_opts *m;
257266acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
25732adf6da8STheodore Ts'o 
257466acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
257566acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
25762adf6da8STheodore Ts'o 
25772adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
25785a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
25795a916be1STheodore Ts'o 
25805a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
25815a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
25825a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
25839cb20f94SIra Weiny 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
25845a916be1STheodore Ts'o 			continue;
258568afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
25865a916be1STheodore Ts'o 			continue; /* skip if same as the default */
25875a916be1STheodore Ts'o 		if ((want_set &&
25885a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
25895a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
25905a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
25915a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
25925a916be1STheodore Ts'o 	}
25935a916be1STheodore Ts'o 
259408cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
25955a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
259608cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
259708cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
259808cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
25995a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
260008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
260108cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
260266acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
26035a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
26045a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
26052adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
26065a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
26072adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
26085a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
260966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
26105a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
261166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
26125a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
261366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
26145a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2615357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
26165a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
261766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
26185a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
261968afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
262068afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
26212adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
26225a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
26232adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
26245a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
26252adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
26265a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
26275a916be1STheodore Ts'o 	}
262866acdcf4STheodore Ts'o 	if (nodefs ||
262966acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
26305a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
26312adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
26322adf6da8STheodore Ts'o 
2633ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
263466acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
26355a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2636df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2637df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
26387915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
26397915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2640ed318a6cSEric Biggers 
2641ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
26422adf6da8STheodore Ts'o 
26434f74d15fSEric Biggers 	if (sb->s_flags & SB_INLINECRYPT)
26444f74d15fSEric Biggers 		SEQ_OPTS_PUTS("inlinecrypt");
26454f74d15fSEric Biggers 
26469cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
26479cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
26489cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
26499cb20f94SIra Weiny 		else
26509cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
26519cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
26529cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
26539cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
26549cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
26559cb20f94SIra Weiny 	}
26569cb20f94SIra Weiny 
2657995a3ed6SHarshad Shirwadkar 	if (test_opt2(sb, JOURNAL_FAST_COMMIT))
2658995a3ed6SHarshad Shirwadkar 		SEQ_OPTS_PUTS("fast_commit");
2659995a3ed6SHarshad Shirwadkar 
26602adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
26612adf6da8STheodore Ts'o 	return 0;
26622adf6da8STheodore Ts'o }
26632adf6da8STheodore Ts'o 
266466acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
266566acdcf4STheodore Ts'o {
266666acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
266766acdcf4STheodore Ts'o }
266866acdcf4STheodore Ts'o 
2669ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
267066acdcf4STheodore Ts'o {
267166acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
267266acdcf4STheodore Ts'o 	int rc;
267366acdcf4STheodore Ts'o 
2674bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
267566acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
267666acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
267766acdcf4STheodore Ts'o 	return rc;
267866acdcf4STheodore Ts'o }
267966acdcf4STheodore Ts'o 
2680617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2681ac27a0ecSDave Kleikamp 			    int read_only)
2682ac27a0ecSDave Kleikamp {
2683617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2684c89128a0SJaegeuk Kim 	int err = 0;
2685ac27a0ecSDave Kleikamp 
2686617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2687b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2688b31e1552SEric Sandeen 			 "forcing read-only mode");
2689c89128a0SJaegeuk Kim 		err = -EROFS;
26905adaccacSyangerkun 		goto done;
2691ac27a0ecSDave Kleikamp 	}
2692ac27a0ecSDave Kleikamp 	if (read_only)
2693281b5995STheodore Ts'o 		goto done;
2694617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2695b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2696b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2697c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2698b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2699b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2700b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2701ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2702ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2703ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2704b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2705b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2706b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2707ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
27086a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
27096a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2710b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2711b31e1552SEric Sandeen 			 "warning: checktime reached, "
2712b31e1552SEric Sandeen 			 "running e2fsck is recommended");
27130390131bSFrank Mayhar 	if (!sbi->s_journal)
2714216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2715ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2716617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2717e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
27186a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
27190390131bSFrank Mayhar 	if (sbi->s_journal)
2720e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2721ac27a0ecSDave Kleikamp 
2722c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2723281b5995STheodore Ts'o done:
2724ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2725a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2726a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2727ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2728ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2729617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2730617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2731a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2732ac27a0ecSDave Kleikamp 
27337abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2734c89128a0SJaegeuk Kim 	return err;
2735ac27a0ecSDave Kleikamp }
2736ac27a0ecSDave Kleikamp 
2737117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2738117fff10STheodore Ts'o {
2739117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
27407c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
274137b0b6b8SDan Carpenter 	int size, i, j;
2742117fff10STheodore Ts'o 
2743117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2744117fff10STheodore Ts'o 		return 0;
2745117fff10STheodore Ts'o 
2746117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2747117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2748117fff10STheodore Ts'o 		return 0;
2749117fff10STheodore Ts'o 
27507c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
27517c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2752117fff10STheodore Ts'o 	if (!new_groups) {
27537c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
27547c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2755117fff10STheodore Ts'o 		return -ENOMEM;
2756117fff10STheodore Ts'o 	}
27577c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
27587c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
27597c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
27607c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
27617c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
276237b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
276337b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
27647c990728SSuraj Jitindar Singh 			kvfree(new_groups);
27657c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
27667c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
27677c990728SSuraj Jitindar Singh 			return -ENOMEM;
2768117fff10STheodore Ts'o 		}
27697c990728SSuraj Jitindar Singh 	}
27707c990728SSuraj Jitindar Singh 	rcu_read_lock();
27717c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
27727c990728SSuraj Jitindar Singh 	if (old_groups)
27737c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
27747c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
27757c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
27767c990728SSuraj Jitindar Singh 	rcu_read_unlock();
27777c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
27787c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
27797c990728SSuraj Jitindar Singh 	if (old_groups)
27807c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2781117fff10STheodore Ts'o 	return 0;
2782117fff10STheodore Ts'o }
2783117fff10STheodore Ts'o 
2784772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2785772cb7c8SJose R. Santos {
2786772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2787772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
27887c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2789772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2790117fff10STheodore Ts'o 	int i, err;
2791772cb7c8SJose R. Santos 
2792503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2793d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2794772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2795772cb7c8SJose R. Santos 		return 1;
2796772cb7c8SJose R. Santos 	}
2797772cb7c8SJose R. Santos 
2798117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2799117fff10STheodore Ts'o 	if (err)
2800772cb7c8SJose R. Santos 		goto failed;
2801772cb7c8SJose R. Santos 
2802772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
280388b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2804772cb7c8SJose R. Santos 
2805772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
28067c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
28077c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
280890ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
28097c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
28107c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2811772cb7c8SJose R. Santos 	}
2812772cb7c8SJose R. Santos 
2813772cb7c8SJose R. Santos 	return 1;
2814772cb7c8SJose R. Santos failed:
2815772cb7c8SJose R. Santos 	return 0;
2816772cb7c8SJose R. Santos }
2817772cb7c8SJose R. Santos 
2818e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2819717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2820717d50e4SAndreas Dilger {
2821b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2822717d50e4SAndreas Dilger 	__u16 crc = 0;
2823717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2824e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2825717d50e4SAndreas Dilger 
28269aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2827feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2828feb0ab32SDarrick J. Wong 		__u32 csum32;
2829b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2830feb0ab32SDarrick J. Wong 
2831feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2832feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2833b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2834b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2835b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2836b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2837b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2838b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2839b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2840feb0ab32SDarrick J. Wong 
2841feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2842feb0ab32SDarrick J. Wong 		goto out;
2843feb0ab32SDarrick J. Wong 	}
2844feb0ab32SDarrick J. Wong 
2845feb0ab32SDarrick J. Wong 	/* old crc16 code */
2846e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2847813d32f9SDarrick J. Wong 		return 0;
2848813d32f9SDarrick J. Wong 
2849717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2850717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2851717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2852717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2853717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2854e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2855717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2856717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2857717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2858717d50e4SAndreas Dilger 				offset);
2859717d50e4SAndreas Dilger 
2860feb0ab32SDarrick J. Wong out:
2861717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2862717d50e4SAndreas Dilger }
2863717d50e4SAndreas Dilger 
2864feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2865717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2866717d50e4SAndreas Dilger {
2867feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2868e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2869717d50e4SAndreas Dilger 		return 0;
2870717d50e4SAndreas Dilger 
2871717d50e4SAndreas Dilger 	return 1;
2872717d50e4SAndreas Dilger }
2873717d50e4SAndreas Dilger 
2874feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2875feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2876feb0ab32SDarrick J. Wong {
2877feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2878feb0ab32SDarrick J. Wong 		return;
2879e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2880feb0ab32SDarrick J. Wong }
2881feb0ab32SDarrick J. Wong 
2882ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2883bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2884829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2885bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2886ac27a0ecSDave Kleikamp {
2887617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2888617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2889617ba13bSMingming Cao 	ext4_fsblk_t last_block;
289044de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2891bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2892bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2893bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2894ce421581SJose R. Santos 	int flexbg_flag = 0;
2895bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2896ac27a0ecSDave Kleikamp 
2897e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2898ce421581SJose R. Santos 		flexbg_flag = 1;
2899ce421581SJose R. Santos 
2900617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2901ac27a0ecSDave Kleikamp 
2902197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2903197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2904197cd65aSAkinobu Mita 
2905ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2906bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2907ac27a0ecSDave Kleikamp 		else
2908ac27a0ecSDave Kleikamp 			last_block = first_block +
2909617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2910ac27a0ecSDave Kleikamp 
2911bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2912bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2913bfff6873SLukas Czerner 			grp = i;
2914bfff6873SLukas Czerner 
29158fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2916829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2917829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2918829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2919829fa70dSTheodore Ts'o 				 "superblock", i);
292018db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
292118db4b4eSTheodore Ts'o 				return 0;
2922829fa70dSTheodore Ts'o 		}
292377260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
292477260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
292577260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
292677260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
292777260807STheodore Ts'o 				 "block group descriptors", i);
292877260807STheodore Ts'o 			if (!sb_rdonly(sb))
292977260807STheodore Ts'o 				return 0;
293077260807STheodore Ts'o 		}
29312b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2932b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2933a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2934b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2935ac27a0ecSDave Kleikamp 			return 0;
2936ac27a0ecSDave Kleikamp 		}
29378fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2938829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2939829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2940829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2941829fa70dSTheodore Ts'o 				 "superblock", i);
294218db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
294318db4b4eSTheodore Ts'o 				return 0;
2944829fa70dSTheodore Ts'o 		}
294577260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
294677260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
294777260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
294877260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
294977260807STheodore Ts'o 				 "block group descriptors", i);
295077260807STheodore Ts'o 			if (!sb_rdonly(sb))
295177260807STheodore Ts'o 				return 0;
295277260807STheodore Ts'o 		}
29532b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2954b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2955a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2956b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2957ac27a0ecSDave Kleikamp 			return 0;
2958ac27a0ecSDave Kleikamp 		}
29598fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2960829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2961829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2962829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2963829fa70dSTheodore Ts'o 				 "superblock", i);
296418db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
296518db4b4eSTheodore Ts'o 				return 0;
2966829fa70dSTheodore Ts'o 		}
296777260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
296877260807STheodore Ts'o 		    inode_table <= last_bg_block) {
296977260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
297077260807STheodore Ts'o 				 "Inode table for group %u overlaps "
297177260807STheodore Ts'o 				 "block group descriptors", i);
297277260807STheodore Ts'o 			if (!sb_rdonly(sb))
297377260807STheodore Ts'o 				return 0;
297477260807STheodore Ts'o 		}
2975bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
29762b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2977b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2978a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2979b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2980ac27a0ecSDave Kleikamp 			return 0;
2981ac27a0ecSDave Kleikamp 		}
2982955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2983feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2984b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2985b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2986e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2987fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2988bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2989955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2990717d50e4SAndreas Dilger 				return 0;
2991717d50e4SAndreas Dilger 			}
29927ee1ec4cSLi Zefan 		}
2993955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2994ce421581SJose R. Santos 		if (!flexbg_flag)
2995617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2996ac27a0ecSDave Kleikamp 	}
2997bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2998bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2999ac27a0ecSDave Kleikamp 	return 1;
3000ac27a0ecSDave Kleikamp }
3001ac27a0ecSDave Kleikamp 
3002617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
3003ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
3004ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
3005ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
3006ac27a0ecSDave Kleikamp  *
3007ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
3008ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
3009ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
3010ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
3011ac27a0ecSDave Kleikamp  *
3012ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
3013ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
3014ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
3015617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
3016ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
3017ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
3018ac27a0ecSDave Kleikamp  */
3019617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
3020617ba13bSMingming Cao 				struct ext4_super_block *es)
3021ac27a0ecSDave Kleikamp {
3022ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
30232c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
3024ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
302595f1fda4Szhangyi (F) 	int quota_update = 0;
3026ac27a0ecSDave Kleikamp 	int i;
3027ac27a0ecSDave Kleikamp #endif
3028ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
3029ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
3030ac27a0ecSDave Kleikamp 		return;
3031ac27a0ecSDave Kleikamp 	}
3032ac27a0ecSDave Kleikamp 
3033a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
3034b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
3035b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
3036a8f48a95SEric Sandeen 		return;
3037a8f48a95SEric Sandeen 	}
3038a8f48a95SEric Sandeen 
3039d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
3040d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
3041d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
3042d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
3043d39195c3SAmir Goldstein 		return;
3044d39195c3SAmir Goldstein 	}
3045d39195c3SAmir Goldstein 
3046617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
3047c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
30481751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
304984474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
3050ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
3051ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
3052c25f9bc6SEric Sandeen 		}
3053ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
3054ac27a0ecSDave Kleikamp 		return;
3055ac27a0ecSDave Kleikamp 	}
3056ac27a0ecSDave Kleikamp 
30571751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
3058b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
30591751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
3060ac27a0ecSDave Kleikamp 	}
3061ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3062ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
30631751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
306495f1fda4Szhangyi (F) 
306595f1fda4Szhangyi (F) 	/*
306695f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
306795f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
306895f1fda4Szhangyi (F) 	 */
30691751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
307095f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
307195f1fda4Szhangyi (F) 
307295f1fda4Szhangyi (F) 		if (!ret)
307395f1fda4Szhangyi (F) 			quota_update = 1;
307495f1fda4Szhangyi (F) 		else
307595f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
307695f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
307795f1fda4Szhangyi (F) 	}
307895f1fda4Szhangyi (F) 
307995f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
3080a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
3081617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
3082617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
308395f1fda4Szhangyi (F) 
308495f1fda4Szhangyi (F) 			if (!ret)
308595f1fda4Szhangyi (F) 				quota_update = 1;
308695f1fda4Szhangyi (F) 			else
3087b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
3088b31e1552SEric Sandeen 					"Cannot turn on journaled "
308995f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
3090ac27a0ecSDave Kleikamp 		}
3091ac27a0ecSDave Kleikamp 	}
3092ac27a0ecSDave Kleikamp #endif
3093ac27a0ecSDave Kleikamp 
3094ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
3095ac27a0ecSDave Kleikamp 		struct inode *inode;
3096ac27a0ecSDave Kleikamp 
3097c65d5c6cSVegard Nossum 		/*
3098c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
3099c65d5c6cSVegard Nossum 		 * so, skip the rest.
3100c65d5c6cSVegard Nossum 		 */
3101c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
3102c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
3103c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
3104c65d5c6cSVegard Nossum 			break;
3105c65d5c6cSVegard Nossum 		}
3106c65d5c6cSVegard Nossum 
310797bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
310897bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
3109ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
3110ac27a0ecSDave Kleikamp 			break;
3111ac27a0ecSDave Kleikamp 		}
3112ac27a0ecSDave Kleikamp 
3113617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
3114871a2931SChristoph Hellwig 		dquot_initialize(inode);
3115ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
3116566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
3117b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
3118b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
311946e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
3120e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
3121ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
31225955102cSAl Viro 			inode_lock(inode);
312355f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
31242c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
31252c98eb5eSTheodore Ts'o 			if (ret)
31262c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
31275955102cSAl Viro 			inode_unlock(inode);
3128ac27a0ecSDave Kleikamp 			nr_truncates++;
3129ac27a0ecSDave Kleikamp 		} else {
3130566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
3131b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
3132b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
313346e665e9SHarvey Harrison 					__func__, inode->i_ino);
3134ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
3135ac27a0ecSDave Kleikamp 				  inode->i_ino);
3136ac27a0ecSDave Kleikamp 			nr_orphans++;
3137ac27a0ecSDave Kleikamp 		}
3138ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
3139ac27a0ecSDave Kleikamp 	}
3140ac27a0ecSDave Kleikamp 
3141ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
3142ac27a0ecSDave Kleikamp 
3143ac27a0ecSDave Kleikamp 	if (nr_orphans)
3144b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
3145b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
3146ac27a0ecSDave Kleikamp 	if (nr_truncates)
3147b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
3148b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
3149ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
315095f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
315195f1fda4Szhangyi (F) 	if (quota_update) {
3152a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
315395f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
3154287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
3155ac27a0ecSDave Kleikamp 		}
315695f1fda4Szhangyi (F) 	}
3157ac27a0ecSDave Kleikamp #endif
31581751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
3159ac27a0ecSDave Kleikamp }
31600b8e58a1SAndreas Dilger 
3161cd2291a4SEric Sandeen /*
3162cd2291a4SEric Sandeen  * Maximal extent format file size.
3163cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
3164cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
3165cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
3166cd2291a4SEric Sandeen  * so that won't be a limiting factor.
3167cd2291a4SEric Sandeen  *
3168f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
3169f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
3170f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
3171f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
3172f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3173f17722f9SLukas Czerner  *
3174cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3175cd2291a4SEric Sandeen  */
3176f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
3177cd2291a4SEric Sandeen {
3178cd2291a4SEric Sandeen 	loff_t res;
3179cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
3180cd2291a4SEric Sandeen 
318172deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
318272deb455SChristoph Hellwig 
318372deb455SChristoph Hellwig 	if (!has_huge_files) {
3184cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
3185cd2291a4SEric Sandeen 
3186cd2291a4SEric Sandeen 		/* total blocks in file system block size */
3187cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
3188cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
3189cd2291a4SEric Sandeen 	}
3190cd2291a4SEric Sandeen 
3191f17722f9SLukas Czerner 	/*
3192f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
3193f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
3194f17722f9SLukas Czerner 	 * size
3195f17722f9SLukas Czerner 	 */
3196f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
3197cd2291a4SEric Sandeen 	res <<= blkbits;
3198cd2291a4SEric Sandeen 
3199cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
3200cd2291a4SEric Sandeen 	if (res > upper_limit)
3201cd2291a4SEric Sandeen 		res = upper_limit;
3202cd2291a4SEric Sandeen 
3203cd2291a4SEric Sandeen 	return res;
3204cd2291a4SEric Sandeen }
3205ac27a0ecSDave Kleikamp 
3206ac27a0ecSDave Kleikamp /*
3207cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
32080fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
32090fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
3210ac27a0ecSDave Kleikamp  */
3211f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3212ac27a0ecSDave Kleikamp {
3213617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
32140fc1b451SAneesh Kumar K.V 	int meta_blocks;
32150fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
32160b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
32170b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
32180b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
32190b8e58a1SAndreas Dilger 	 *
32200b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
32210b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
32220fc1b451SAneesh Kumar K.V 	 */
32230fc1b451SAneesh Kumar K.V 
322472deb455SChristoph Hellwig 	if (!has_huge_files) {
32250fc1b451SAneesh Kumar K.V 		/*
322672deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
322772deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
322872deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
32290fc1b451SAneesh Kumar K.V 		 */
32300fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
32310fc1b451SAneesh Kumar K.V 
32320fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
32330fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
32340fc1b451SAneesh Kumar K.V 
32350fc1b451SAneesh Kumar K.V 	} else {
32368180a562SAneesh Kumar K.V 		/*
32378180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
32388180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
32398180a562SAneesh Kumar K.V 		 * represent total number of blocks in
32408180a562SAneesh Kumar K.V 		 * file system block size
32418180a562SAneesh Kumar K.V 		 */
32420fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
32430fc1b451SAneesh Kumar K.V 
32440fc1b451SAneesh Kumar K.V 	}
32450fc1b451SAneesh Kumar K.V 
32460fc1b451SAneesh Kumar K.V 	/* indirect blocks */
32470fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
32480fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
32490fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
32500fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
32510fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
32520fc1b451SAneesh Kumar K.V 
32530fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
32540fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
3255ac27a0ecSDave Kleikamp 
3256ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
3257ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
3258ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
3259ac27a0ecSDave Kleikamp 	res <<= bits;
3260ac27a0ecSDave Kleikamp 	if (res > upper_limit)
3261ac27a0ecSDave Kleikamp 		res = upper_limit;
32620fc1b451SAneesh Kumar K.V 
32630fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
32640fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
32650fc1b451SAneesh Kumar K.V 
3266ac27a0ecSDave Kleikamp 	return res;
3267ac27a0ecSDave Kleikamp }
3268ac27a0ecSDave Kleikamp 
3269617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
327070bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3271ac27a0ecSDave Kleikamp {
3272617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3273fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3274ac27a0ecSDave Kleikamp 	int has_super = 0;
3275ac27a0ecSDave Kleikamp 
3276ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3277ac27a0ecSDave Kleikamp 
3278e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
327970bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3280ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3281617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3282ac27a0ecSDave Kleikamp 		has_super = 1;
32830b8e58a1SAndreas Dilger 
3284bd63f6b0SDarrick J. Wong 	/*
3285bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3286bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3287bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3288bd63f6b0SDarrick J. Wong 	 * compensate.
3289bd63f6b0SDarrick J. Wong 	 */
3290bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
329149598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3292bd63f6b0SDarrick J. Wong 		has_super++;
3293bd63f6b0SDarrick J. Wong 
3294617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3295ac27a0ecSDave Kleikamp }
3296ac27a0ecSDave Kleikamp 
3297c9de560dSAlex Tomas /**
3298c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3299c9de560dSAlex Tomas  * @sbi: In memory super block info
3300c9de560dSAlex Tomas  *
3301c9de560dSAlex Tomas  * If we have specified it via mount option, then
3302c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3303c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3304c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3305c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3306c9de560dSAlex Tomas  *
3307c9de560dSAlex Tomas  */
3308c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3309c9de560dSAlex Tomas {
3310c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3311c9de560dSAlex Tomas 	unsigned long stripe_width =
3312c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
33133eb08658SDan Ehrenberg 	int ret;
3314c9de560dSAlex Tomas 
3315c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
33163eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
33175469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
33183eb08658SDan Ehrenberg 		ret = stripe_width;
33195469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
33203eb08658SDan Ehrenberg 		ret = stride;
33213eb08658SDan Ehrenberg 	else
33223eb08658SDan Ehrenberg 		ret = 0;
3323c9de560dSAlex Tomas 
33243eb08658SDan Ehrenberg 	/*
33253eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
33263eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
33273eb08658SDan Ehrenberg 	 */
33283eb08658SDan Ehrenberg 	if (ret <= 1)
33293eb08658SDan Ehrenberg 		ret = 0;
3330c9de560dSAlex Tomas 
33313eb08658SDan Ehrenberg 	return ret;
3332c9de560dSAlex Tomas }
3333ac27a0ecSDave Kleikamp 
3334a13fb1a4SEric Sandeen /*
3335a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3336a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3337a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3338a13fb1a4SEric Sandeen  * 0 if it cannot be.
3339a13fb1a4SEric Sandeen  */
3340a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3341a13fb1a4SEric Sandeen {
3342e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3343a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3344a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3345a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3346a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3347a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3348a13fb1a4SEric Sandeen 		return 0;
3349a13fb1a4SEric Sandeen 	}
3350a13fb1a4SEric Sandeen 
3351c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3352c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3353c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3354c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3355c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3356c83ad55eSGabriel Krisman Bertazi 		return 0;
3357c83ad55eSGabriel Krisman Bertazi 	}
3358c83ad55eSGabriel Krisman Bertazi #endif
3359c83ad55eSGabriel Krisman Bertazi 
3360a13fb1a4SEric Sandeen 	if (readonly)
3361a13fb1a4SEric Sandeen 		return 1;
3362a13fb1a4SEric Sandeen 
3363e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
33642cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
33651751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
33662cb5cc8bSDarrick J. Wong 		return 1;
33672cb5cc8bSDarrick J. Wong 	}
33682cb5cc8bSDarrick J. Wong 
3369a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3370e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3371a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3372a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3373a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3374a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3375a13fb1a4SEric Sandeen 		return 0;
3376a13fb1a4SEric Sandeen 	}
3377e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3378bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3379bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3380bab08ab9STheodore Ts'o 			 "extents feature\n");
3381bab08ab9STheodore Ts'o 		return 0;
3382bab08ab9STheodore Ts'o 	}
33837c319d32SAditya Kali 
33849db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3385d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3386d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
33877c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3388d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3389689c958cSLi Xi 		return 0;
3390689c958cSLi Xi 	}
33917c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3392a13fb1a4SEric Sandeen 	return 1;
3393a13fb1a4SEric Sandeen }
3394a13fb1a4SEric Sandeen 
339566e61a9eSTheodore Ts'o /*
339666e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
339766e61a9eSTheodore Ts'o  * on the file system
339866e61a9eSTheodore Ts'o  */
3399235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
340066e61a9eSTheodore Ts'o {
3401235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3402235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3403235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
340466e61a9eSTheodore Ts'o 
340566e61a9eSTheodore Ts'o 	if (es->s_error_count)
3406ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3407ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
340866e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
340966e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
34106a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
34116a0678a7SArnd Bergmann 		       sb->s_id,
34126a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
341366e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
341466e61a9eSTheodore Ts'o 		       es->s_first_error_func,
341566e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
341666e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3417651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
341866e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
341966e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3420651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
342166e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3422651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
342366e61a9eSTheodore Ts'o 	}
342466e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
34256a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
34266a0678a7SArnd Bergmann 		       sb->s_id,
34276a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
342866e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
342966e61a9eSTheodore Ts'o 		       es->s_last_error_func,
343066e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
343166e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3432651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
343366e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
343466e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3435651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
343666e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3437651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
343866e61a9eSTheodore Ts'o 	}
343966e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
344066e61a9eSTheodore Ts'o }
344166e61a9eSTheodore Ts'o 
3442bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3443bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3444bfff6873SLukas Czerner {
3445bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
34463d392b26STheodore Ts'o 	struct super_block *sb = elr->lr_super;
34473d392b26STheodore Ts'o 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
34483d392b26STheodore Ts'o 	ext4_group_t group = elr->lr_next_group;
3449bfff6873SLukas Czerner 	unsigned long timeout = 0;
34503d392b26STheodore Ts'o 	unsigned int prefetch_ios = 0;
3451bfff6873SLukas Czerner 	int ret = 0;
3452bfff6873SLukas Czerner 
34533d392b26STheodore Ts'o 	if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
34543d392b26STheodore Ts'o 		elr->lr_next_group = ext4_mb_prefetch(sb, group,
34553d392b26STheodore Ts'o 				EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
34563d392b26STheodore Ts'o 		if (prefetch_ios)
34573d392b26STheodore Ts'o 			ext4_mb_prefetch_fini(sb, elr->lr_next_group,
34583d392b26STheodore Ts'o 					      prefetch_ios);
34593d392b26STheodore Ts'o 		trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
34603d392b26STheodore Ts'o 					    prefetch_ios);
34613d392b26STheodore Ts'o 		if (group >= elr->lr_next_group) {
34623d392b26STheodore Ts'o 			ret = 1;
34633d392b26STheodore Ts'o 			if (elr->lr_first_not_zeroed != ngroups &&
34643d392b26STheodore Ts'o 			    !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
34653d392b26STheodore Ts'o 				elr->lr_next_group = elr->lr_first_not_zeroed;
34663d392b26STheodore Ts'o 				elr->lr_mode = EXT4_LI_MODE_ITABLE;
34673d392b26STheodore Ts'o 				ret = 0;
34683d392b26STheodore Ts'o 			}
34693d392b26STheodore Ts'o 		}
34703d392b26STheodore Ts'o 		return ret;
34713d392b26STheodore Ts'o 	}
3472bfff6873SLukas Czerner 
34733d392b26STheodore Ts'o 	for (; group < ngroups; group++) {
3474bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3475bfff6873SLukas Czerner 		if (!gdp) {
3476bfff6873SLukas Czerner 			ret = 1;
3477bfff6873SLukas Czerner 			break;
3478bfff6873SLukas Czerner 		}
3479bfff6873SLukas Czerner 
3480bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3481bfff6873SLukas Czerner 			break;
3482bfff6873SLukas Czerner 	}
3483bfff6873SLukas Czerner 
34847f511862STheodore Ts'o 	if (group >= ngroups)
3485bfff6873SLukas Czerner 		ret = 1;
3486bfff6873SLukas Czerner 
3487bfff6873SLukas Czerner 	if (!ret) {
3488bfff6873SLukas Czerner 		timeout = jiffies;
3489bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3490bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
34913d392b26STheodore Ts'o 		trace_ext4_lazy_itable_init(sb, group);
3492bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
349351ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
34943d392b26STheodore Ts'o 				EXT4_SB(elr->lr_super)->s_li_wait_mult;
3495bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3496bfff6873SLukas Czerner 		}
3497bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3498bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3499bfff6873SLukas Czerner 	}
3500bfff6873SLukas Czerner 	return ret;
3501bfff6873SLukas Czerner }
3502bfff6873SLukas Czerner 
3503bfff6873SLukas Czerner /*
3504bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
35054ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3506bfff6873SLukas Czerner  */
3507bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3508bfff6873SLukas Czerner {
3509bfff6873SLukas Czerner 	if (!elr)
3510bfff6873SLukas Czerner 		return;
3511bfff6873SLukas Czerner 
3512bfff6873SLukas Czerner 	list_del(&elr->lr_request);
35133d392b26STheodore Ts'o 	EXT4_SB(elr->lr_super)->s_li_request = NULL;
3514bfff6873SLukas Czerner 	kfree(elr);
3515bfff6873SLukas Czerner }
3516bfff6873SLukas Czerner 
3517bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3518bfff6873SLukas Czerner {
35191bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
35201bb933fbSLukas Czerner 	if (!ext4_li_info) {
35211bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3522bfff6873SLukas Czerner 		return;
35231bb933fbSLukas Czerner 	}
3524bfff6873SLukas Czerner 
3525bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
35261bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3527bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
35281bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3529bfff6873SLukas Czerner }
3530bfff6873SLukas Czerner 
35318f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
35328f1f7453SEric Sandeen 
3533bfff6873SLukas Czerner /*
3534bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3535bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3536bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3537bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3538bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3539bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3540bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3541bfff6873SLukas Czerner  */
3542bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3543bfff6873SLukas Czerner {
3544bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3545bfff6873SLukas Czerner 	struct list_head *pos, *n;
3546bfff6873SLukas Czerner 	struct ext4_li_request *elr;
35474ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3548bfff6873SLukas Czerner 
3549bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3550bfff6873SLukas Czerner 
3551bfff6873SLukas Czerner cont_thread:
3552bfff6873SLukas Czerner 	while (true) {
3553bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3554bfff6873SLukas Czerner 
3555bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3556bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3557bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3558bfff6873SLukas Czerner 			goto exit_thread;
3559bfff6873SLukas Czerner 		}
3560bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3561e22834f0SDmitry Monakhov 			int err = 0;
3562e22834f0SDmitry Monakhov 			int progress = 0;
3563bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3564bfff6873SLukas Czerner 					 lr_request);
3565bfff6873SLukas Czerner 
3566e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3567e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3568e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3569e22834f0SDmitry Monakhov 				continue;
3570e22834f0SDmitry Monakhov 			}
3571e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3572e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3573e22834f0SDmitry Monakhov 					progress = 1;
3574e22834f0SDmitry Monakhov 					/*
3575e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3576e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3577e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3578e22834f0SDmitry Monakhov 					 */
3579e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3580e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3581e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3582e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3583e22834f0SDmitry Monakhov 					n = pos->next;
3584e22834f0SDmitry Monakhov 				}
3585e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3586e22834f0SDmitry Monakhov 			}
3587b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3588e22834f0SDmitry Monakhov 			if (err) {
3589bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3590bfff6873SLukas Czerner 				continue;
3591bfff6873SLukas Czerner 			}
3592e22834f0SDmitry Monakhov 			if (!progress) {
3593e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3594e22834f0SDmitry Monakhov 					(prandom_u32()
3595e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3596b2c78cd0STheodore Ts'o 			}
3597bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3598bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3599bfff6873SLukas Czerner 		}
3600bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3601bfff6873SLukas Czerner 
3602a0acae0eSTejun Heo 		try_to_freeze();
3603bfff6873SLukas Czerner 
36044ed5c033SLukas Czerner 		cur = jiffies;
36054ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3606f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3607bfff6873SLukas Czerner 			cond_resched();
3608bfff6873SLukas Czerner 			continue;
3609bfff6873SLukas Czerner 		}
3610bfff6873SLukas Czerner 
36114ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
36124ed5c033SLukas Czerner 
36138f1f7453SEric Sandeen 		if (kthread_should_stop()) {
36148f1f7453SEric Sandeen 			ext4_clear_request_list();
36158f1f7453SEric Sandeen 			goto exit_thread;
36168f1f7453SEric Sandeen 		}
3617bfff6873SLukas Czerner 	}
3618bfff6873SLukas Czerner 
3619bfff6873SLukas Czerner exit_thread:
3620bfff6873SLukas Czerner 	/*
3621bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3622bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3623bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3624bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3625bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3626bfff6873SLukas Czerner 	 * new one.
3627bfff6873SLukas Czerner 	 */
3628bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3629bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3630bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3631bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3632bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3633bfff6873SLukas Czerner 		goto cont_thread;
3634bfff6873SLukas Czerner 	}
3635bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3636bfff6873SLukas Czerner 	kfree(ext4_li_info);
3637bfff6873SLukas Czerner 	ext4_li_info = NULL;
3638bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3639bfff6873SLukas Czerner 
3640bfff6873SLukas Czerner 	return 0;
3641bfff6873SLukas Czerner }
3642bfff6873SLukas Czerner 
3643bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3644bfff6873SLukas Czerner {
3645bfff6873SLukas Czerner 	struct list_head *pos, *n;
3646bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3647bfff6873SLukas Czerner 
3648bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3649bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3650bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3651bfff6873SLukas Czerner 				 lr_request);
3652bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3653bfff6873SLukas Czerner 	}
3654bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3655bfff6873SLukas Czerner }
3656bfff6873SLukas Czerner 
3657bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3658bfff6873SLukas Czerner {
36598f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
36608f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
36618f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
36628f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3663bfff6873SLukas Czerner 		ext4_clear_request_list();
3664bfff6873SLukas Czerner 		kfree(ext4_li_info);
3665bfff6873SLukas Czerner 		ext4_li_info = NULL;
366692b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3667bfff6873SLukas Czerner 				 "initialization thread\n",
3668bfff6873SLukas Czerner 				 err);
3669bfff6873SLukas Czerner 		return err;
3670bfff6873SLukas Czerner 	}
3671bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3672bfff6873SLukas Czerner 	return 0;
3673bfff6873SLukas Czerner }
3674bfff6873SLukas Czerner 
3675bfff6873SLukas Czerner /*
3676bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3677bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3678bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3679bfff6873SLukas Czerner  * groups and return total number of groups.
3680bfff6873SLukas Czerner  */
3681bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3682bfff6873SLukas Czerner {
3683bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3684bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3685bfff6873SLukas Czerner 
36868844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
36878844618dSTheodore Ts'o 		return ngroups;
36888844618dSTheodore Ts'o 
3689bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3690bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3691bfff6873SLukas Czerner 		if (!gdp)
3692bfff6873SLukas Czerner 			continue;
3693bfff6873SLukas Czerner 
369450122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3695bfff6873SLukas Czerner 			break;
3696bfff6873SLukas Czerner 	}
3697bfff6873SLukas Czerner 
3698bfff6873SLukas Czerner 	return group;
3699bfff6873SLukas Czerner }
3700bfff6873SLukas Czerner 
3701bfff6873SLukas Czerner static int ext4_li_info_new(void)
3702bfff6873SLukas Czerner {
3703bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3704bfff6873SLukas Czerner 
3705bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3706bfff6873SLukas Czerner 	if (!eli)
3707bfff6873SLukas Czerner 		return -ENOMEM;
3708bfff6873SLukas Czerner 
3709bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3710bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3711bfff6873SLukas Czerner 
3712bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3713bfff6873SLukas Czerner 
3714bfff6873SLukas Czerner 	ext4_li_info = eli;
3715bfff6873SLukas Czerner 
3716bfff6873SLukas Czerner 	return 0;
3717bfff6873SLukas Czerner }
3718bfff6873SLukas Czerner 
3719bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3720bfff6873SLukas Czerner 					    ext4_group_t start)
3721bfff6873SLukas Czerner {
3722bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3723bfff6873SLukas Czerner 
3724bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3725bfff6873SLukas Czerner 	if (!elr)
3726bfff6873SLukas Czerner 		return NULL;
3727bfff6873SLukas Czerner 
3728bfff6873SLukas Czerner 	elr->lr_super = sb;
37293d392b26STheodore Ts'o 	elr->lr_first_not_zeroed = start;
37303d392b26STheodore Ts'o 	if (test_opt(sb, PREFETCH_BLOCK_BITMAPS))
37313d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
37323d392b26STheodore Ts'o 	else {
37333d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_ITABLE;
3734bfff6873SLukas Czerner 		elr->lr_next_group = start;
37353d392b26STheodore Ts'o 	}
3736bfff6873SLukas Czerner 
3737bfff6873SLukas Czerner 	/*
3738bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3739bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3740bfff6873SLukas Czerner 	 * better.
3741bfff6873SLukas Czerner 	 */
3742dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3743dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3744bfff6873SLukas Czerner 	return elr;
3745bfff6873SLukas Czerner }
3746bfff6873SLukas Czerner 
37477f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3748bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3749bfff6873SLukas Czerner {
3750bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
37517f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
375249598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
37536c5a6cb9SAndrew Morton 	int ret = 0;
3754bfff6873SLukas Czerner 
37557f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
375651ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
375751ce6511SLukas Czerner 		/*
375851ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
375951ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
376051ce6511SLukas Czerner 		 */
376151ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
37627f511862STheodore Ts'o 		goto out;
376351ce6511SLukas Czerner 	}
3764bfff6873SLukas Czerner 
37653d392b26STheodore Ts'o 	if (!test_opt(sb, PREFETCH_BLOCK_BITMAPS) &&
37663d392b26STheodore Ts'o 	    (first_not_zeroed == ngroups || sb_rdonly(sb) ||
37673d392b26STheodore Ts'o 	     !test_opt(sb, INIT_INODE_TABLE)))
37687f511862STheodore Ts'o 		goto out;
3769bfff6873SLukas Czerner 
3770bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
37717f511862STheodore Ts'o 	if (!elr) {
37727f511862STheodore Ts'o 		ret = -ENOMEM;
37737f511862STheodore Ts'o 		goto out;
37747f511862STheodore Ts'o 	}
3775bfff6873SLukas Czerner 
3776bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3777bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3778bfff6873SLukas Czerner 		if (ret)
3779bfff6873SLukas Czerner 			goto out;
3780bfff6873SLukas Czerner 	}
3781bfff6873SLukas Czerner 
3782bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3783bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3784bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3785bfff6873SLukas Czerner 
3786bfff6873SLukas Czerner 	sbi->s_li_request = elr;
378746e4690bSTao Ma 	/*
378846e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
378946e4690bSTao Ma 	 * the request_list and the removal and free of it is
379046e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
379146e4690bSTao Ma 	 */
379246e4690bSTao Ma 	elr = NULL;
3793bfff6873SLukas Czerner 
3794bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3795bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3796bfff6873SLukas Czerner 		if (ret)
3797bfff6873SLukas Czerner 			goto out;
3798bfff6873SLukas Czerner 	}
3799bfff6873SLukas Czerner out:
3800bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3801beed5ecbSNicolas Kaiser 	if (ret)
3802bfff6873SLukas Czerner 		kfree(elr);
3803bfff6873SLukas Czerner 	return ret;
3804bfff6873SLukas Czerner }
3805bfff6873SLukas Czerner 
3806bfff6873SLukas Czerner /*
3807bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3808bfff6873SLukas Czerner  * module unload.
3809bfff6873SLukas Czerner  */
3810bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3811bfff6873SLukas Czerner {
3812bfff6873SLukas Czerner 	/*
3813bfff6873SLukas Czerner 	 * If thread exited earlier
3814bfff6873SLukas Czerner 	 * there's nothing to be done.
3815bfff6873SLukas Czerner 	 */
38168f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3817bfff6873SLukas Czerner 		return;
3818bfff6873SLukas Czerner 
38198f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3820bfff6873SLukas Czerner }
3821bfff6873SLukas Czerner 
382225ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
382325ed6e8aSDarrick J. Wong {
382425ed6e8aSDarrick J. Wong 	int ret = 1;
382525ed6e8aSDarrick J. Wong 	int compat, incompat;
382625ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
382725ed6e8aSDarrick J. Wong 
38289aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3829db9ee220SDarrick J. Wong 		/* journal checksum v3 */
383025ed6e8aSDarrick J. Wong 		compat = 0;
3831db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
383225ed6e8aSDarrick J. Wong 	} else {
383325ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
383425ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
383525ed6e8aSDarrick J. Wong 		incompat = 0;
383625ed6e8aSDarrick J. Wong 	}
383725ed6e8aSDarrick J. Wong 
3838feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3839feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3840feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3841feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
384225ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
384325ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
384425ed6e8aSDarrick J. Wong 				compat, 0,
384525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
384625ed6e8aSDarrick J. Wong 				incompat);
384725ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
384825ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
384925ed6e8aSDarrick J. Wong 				compat, 0,
385025ed6e8aSDarrick J. Wong 				incompat);
385125ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
385225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
385325ed6e8aSDarrick J. Wong 	} else {
3854feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3855feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
385625ed6e8aSDarrick J. Wong 	}
385725ed6e8aSDarrick J. Wong 
385825ed6e8aSDarrick J. Wong 	return ret;
385925ed6e8aSDarrick J. Wong }
386025ed6e8aSDarrick J. Wong 
3861952fc18eSTheodore Ts'o /*
3862952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3863952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3864952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3865952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3866952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3867952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3868952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3869952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3870952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3871952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3872952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3873952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3874952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3875952fc18eSTheodore Ts'o  */
3876952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3877952fc18eSTheodore Ts'o 			  char *buf)
3878952fc18eSTheodore Ts'o {
3879952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3880952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3881952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3882952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3883952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3884952fc18eSTheodore Ts'o 
3885e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
38860548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
38870548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
38880548bbb8STheodore Ts'o 
3889952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3890952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3891952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3892952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3893952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3894952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3895952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3896952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3897952fc18eSTheodore Ts'o 			count++;
3898952fc18eSTheodore Ts'o 		}
3899952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3900952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3901952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3902952fc18eSTheodore Ts'o 			count++;
3903952fc18eSTheodore Ts'o 		}
3904952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3905952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3906952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3907952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3908952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3909952fc18eSTheodore Ts'o 				count++;
3910952fc18eSTheodore Ts'o 			}
3911952fc18eSTheodore Ts'o 		if (i != grp)
3912952fc18eSTheodore Ts'o 			continue;
3913952fc18eSTheodore Ts'o 		s = 0;
3914952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3915952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3916952fc18eSTheodore Ts'o 			count++;
3917952fc18eSTheodore Ts'o 		}
3918c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3919c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3920c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3921c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3922c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3923952fc18eSTheodore Ts'o 		}
3924c48ae41bSTheodore Ts'o 		count += j;
3925c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3926c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3927952fc18eSTheodore Ts'o 	}
3928952fc18eSTheodore Ts'o 	if (!count)
3929952fc18eSTheodore Ts'o 		return 0;
3930952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3931952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3932952fc18eSTheodore Ts'o }
3933952fc18eSTheodore Ts'o 
3934952fc18eSTheodore Ts'o /*
3935952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3936952fc18eSTheodore Ts'o  */
3937952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3938952fc18eSTheodore Ts'o {
3939952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3940952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
39413c816dedSEric Whitney 	struct inode *j_inode;
39423c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3943952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3944952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
39454fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3946952fc18eSTheodore Ts'o 
3947952fc18eSTheodore Ts'o 	if (!buf)
3948952fc18eSTheodore Ts'o 		return -ENOMEM;
3949952fc18eSTheodore Ts'o 
3950952fc18eSTheodore Ts'o 	/*
3951952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3952952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3953952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3954952fc18eSTheodore Ts'o 	 */
3955952fc18eSTheodore Ts'o 
3956952fc18eSTheodore Ts'o 	/*
3957952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3958952fc18eSTheodore Ts'o 	 */
3959952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3960952fc18eSTheodore Ts'o 
3961952fc18eSTheodore Ts'o 	/*
3962952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3963952fc18eSTheodore Ts'o 	 */
3964952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3965952fc18eSTheodore Ts'o 		int blks;
3966952fc18eSTheodore Ts'o 
3967952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3968952fc18eSTheodore Ts'o 		overhead += blks;
3969952fc18eSTheodore Ts'o 		if (blks)
3970952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3971952fc18eSTheodore Ts'o 		cond_resched();
3972952fc18eSTheodore Ts'o 	}
39733c816dedSEric Whitney 
39743c816dedSEric Whitney 	/*
39753c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
39763c816dedSEric Whitney 	 * loaded or not
39773c816dedSEric Whitney 	 */
3978ee7ed3aaSChunguang Xu 	if (sbi->s_journal && !sbi->s_journal_bdev)
3979ede7dc7fSHarshad Shirwadkar 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
3980f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3981f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
39823c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
39833c816dedSEric Whitney 		if (j_inode) {
39843c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
39853c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
39863c816dedSEric Whitney 			iput(j_inode);
39873c816dedSEric Whitney 		} else {
39883c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
39893c816dedSEric Whitney 		}
39903c816dedSEric Whitney 	}
3991952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3992952fc18eSTheodore Ts'o 	smp_wmb();
3993952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3994952fc18eSTheodore Ts'o 	return 0;
3995952fc18eSTheodore Ts'o }
3996952fc18eSTheodore Ts'o 
3997b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
399827dd4385SLukas Czerner {
399927dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
4000b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
400127dd4385SLukas Czerner 
400227dd4385SLukas Czerner 	/*
400330fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
400430fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
400530fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
400630fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
400730fac0f7SJan Kara 	 */
4008e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
4009b5799018STheodore Ts'o 		return;
401030fac0f7SJan Kara 	/*
401127dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
401227dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
401327dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
4014556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
401527dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
401627dd4385SLukas Czerner 	 * very rare.
401727dd4385SLukas Czerner 	 */
4018b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
4019b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
402027dd4385SLukas Czerner 
402127dd4385SLukas Czerner 	do_div(resv_clusters, 50);
402227dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
402327dd4385SLukas Czerner 
4024b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
402527dd4385SLukas Czerner }
402627dd4385SLukas Czerner 
4027617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
4028ac27a0ecSDave Kleikamp {
40295e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
4030d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
40311d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
4032617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
40335aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
40347c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
4035617ba13bSMingming Cao 	ext4_fsblk_t block;
4036617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
403770bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
4038ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
4039ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
4040ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
4041ac27a0ecSDave Kleikamp 	struct inode *root;
40420390131bSFrank Mayhar 	const char *descr;
4043dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
4044281b5995STheodore Ts'o 	int blocksize, clustersize;
40454ec11028STheodore Ts'o 	unsigned int db_count;
40464ec11028STheodore Ts'o 	unsigned int i;
4047ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
4048bd81d8eeSLaurent Vivier 	__u64 blocks_count;
404907aa2ea1SLukas Czerner 	int err = 0;
4050b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4051bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
4052ac27a0ecSDave Kleikamp 
40535aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
40545aee0f8aSTheodore Ts'o 		goto out_free_base;
4055705895b6SPekka Enberg 
4056aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
4057705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
4058705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
40595aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
40605aee0f8aSTheodore Ts'o 		goto out_free_base;
40615aee0f8aSTheodore Ts'o 
4062ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
40632c0544b2STheodore Ts'o 	sbi->s_sb = sb;
4064240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
4065d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
4066f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4067f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
4068dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
4069ac27a0ecSDave Kleikamp 
40709f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
4071ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
40729f6200bbSTheodore Ts'o 
407307aa2ea1SLukas Czerner 	/* -EINVAL is default */
4074dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
4075617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
4076ac27a0ecSDave Kleikamp 	if (!blocksize) {
4077b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
4078ac27a0ecSDave Kleikamp 		goto out_fail;
4079ac27a0ecSDave Kleikamp 	}
4080ac27a0ecSDave Kleikamp 
4081ac27a0ecSDave Kleikamp 	/*
4082617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
4083ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
4084ac27a0ecSDave Kleikamp 	 */
4085617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
408670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
408770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4088ac27a0ecSDave Kleikamp 	} else {
408970bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
4090ac27a0ecSDave Kleikamp 	}
4091ac27a0ecSDave Kleikamp 
40928394a6abSzhangyi (F) 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
40938394a6abSzhangyi (F) 	if (IS_ERR(bh)) {
4094b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
40958394a6abSzhangyi (F) 		ret = PTR_ERR(bh);
40968394a6abSzhangyi (F) 		bh = NULL;
4097ac27a0ecSDave Kleikamp 		goto out_fail;
4098ac27a0ecSDave Kleikamp 	}
4099ac27a0ecSDave Kleikamp 	/*
4100ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
4101617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
4102ac27a0ecSDave Kleikamp 	 */
41032716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4104ac27a0ecSDave Kleikamp 	sbi->s_es = es;
4105ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
4106617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
4107617ba13bSMingming Cao 		goto cantfind_ext4;
4108afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
4109ac27a0ecSDave Kleikamp 
4110feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
4111e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
4112e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
4113363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
4114feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
4115feb0ab32SDarrick J. Wong 
4116d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
4117d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
4118d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4119d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
4120d25425f8SDarrick J. Wong 		silent = 1;
4121d25425f8SDarrick J. Wong 		goto cantfind_ext4;
4122d25425f8SDarrick J. Wong 	}
4123d25425f8SDarrick J. Wong 
41240441984aSDarrick J. Wong 	/* Load the checksum driver */
41250441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
41260441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
41270441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
41280441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
41290441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
41300441984aSDarrick J. Wong 		goto failed_mount;
41310441984aSDarrick J. Wong 	}
41320441984aSDarrick J. Wong 
4133a9c47317SDarrick J. Wong 	/* Check superblock checksum */
4134a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
4135a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4136a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
4137a9c47317SDarrick J. Wong 		silent = 1;
41386a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
4139a9c47317SDarrick J. Wong 		goto cantfind_ext4;
4140a9c47317SDarrick J. Wong 	}
4141a9c47317SDarrick J. Wong 
4142a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
4143e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
41448c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4145dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4146a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4147a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
4148a9c47317SDarrick J. Wong 
4149ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
4150ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4151fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
4152617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
4153fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
415487f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4155fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
4156617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
4157fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
4158ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
4159fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
416003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
4161fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
41622e7842b8SHugh Dickins #endif
4163995a3ed6SHarshad Shirwadkar 	if (ext4_has_feature_fast_commit(sb))
4164995a3ed6SHarshad Shirwadkar 		set_opt2(sb, JOURNAL_FAST_COMMIT);
416598c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
416698c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
416798c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
416898c1a759SDarrick J. Wong 
4169617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4170fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
4171617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4172fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
4173617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4174fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
4175ac27a0ecSDave Kleikamp 
4176617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
4177fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
4178bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
4179fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
4180bb4f397aSAneesh Kumar K.V 	else
4181fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
418245f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
4183fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
41848b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
4185fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
4186ac27a0ecSDave Kleikamp 
418708cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
418808cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
418930773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
419030773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
419130773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
4192ac27a0ecSDave Kleikamp 
41938b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4194fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
4195ac27a0ecSDave Kleikamp 
41961e2462f9SMingming Cao 	/*
4197dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
4198dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
4199dd919b98SAneesh Kumar K.V 	 */
4200bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
42018b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4202fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
4203dd919b98SAneesh Kumar K.V 
420451ce6511SLukas Czerner 	/*
420551ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
420651ce6511SLukas Czerner 	 * no mount option specified.
420751ce6511SLukas Czerner 	 */
420851ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
420951ce6511SLukas Czerner 
42104f97a681STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4211626b035bSRitesh Harjani 
4212626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
4213626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
4214626b035bSRitesh Harjani 
42154f97a681STheodore Ts'o 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
42164f97a681STheodore Ts'o 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
42174f97a681STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
42184f97a681STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
42194f97a681STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
42204f97a681STheodore Ts'o 		goto failed_mount;
42214f97a681STheodore Ts'o 	}
42224f97a681STheodore Ts'o 
42239803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
42249803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
42259803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
42269803387cSTheodore Ts'o 	} else {
42279803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
42289803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
42299803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
42309803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
42319803387cSTheodore Ts'o 				 sbi->s_first_ino);
42329803387cSTheodore Ts'o 			goto failed_mount;
42339803387cSTheodore Ts'o 		}
42349803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
42359803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
42369803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
42379803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
42389803387cSTheodore Ts'o 			       "unsupported inode size: %d",
42399803387cSTheodore Ts'o 			       sbi->s_inode_size);
42404f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
42419803387cSTheodore Ts'o 			goto failed_mount;
42429803387cSTheodore Ts'o 		}
42439803387cSTheodore Ts'o 		/*
42449803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
42459803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
42469803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
42479803387cSTheodore Ts'o 		 * for all three.
42489803387cSTheodore Ts'o 		 */
42499803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
42509803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
42519803387cSTheodore Ts'o 			sb->s_time_gran = 1;
42529803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
42539803387cSTheodore Ts'o 		} else {
42549803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
42559803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
42569803387cSTheodore Ts'o 		}
42579803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
42589803387cSTheodore Ts'o 	}
42599803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
42609803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
42619803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
42629803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
42639803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
42649803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
42659803387cSTheodore Ts'o 
42669803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
42679803387cSTheodore Ts'o 			if (v > max) {
42689803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
42699803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
42709803387cSTheodore Ts'o 				goto failed_mount;
42719803387cSTheodore Ts'o 			}
42729803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
42739803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
42749803387cSTheodore Ts'o 
42759803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
42769803387cSTheodore Ts'o 			if (v > max) {
42779803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
42789803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
42799803387cSTheodore Ts'o 				goto failed_mount;
42809803387cSTheodore Ts'o 			}
42819803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
42829803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
42839803387cSTheodore Ts'o 		}
42849803387cSTheodore Ts'o 	}
42859803387cSTheodore Ts'o 
42865aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
42875aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
42885aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
42895aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
42905aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
42915aee0f8aSTheodore Ts'o 			goto failed_mount;
42925aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
42935aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
42948b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
42958b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
42965aee0f8aSTheodore Ts'o 				 s_mount_opts);
42975aee0f8aSTheodore Ts'o 		}
42985aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
42998b67f04aSTheodore Ts'o 	}
43005a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
4301b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
4302661aa520SEric Sandeen 			   &journal_ioprio, 0))
4303ac27a0ecSDave Kleikamp 		goto failed_mount;
4304ac27a0ecSDave Kleikamp 
4305c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4306f8f4acb6SDaniel Rosenberg 	if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
4307c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
4308c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
4309c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
4310c83ad55eSGabriel Krisman Bertazi 
4311c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
4312c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4313c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
4314c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4315c83ad55eSGabriel Krisman Bertazi 		}
4316c83ad55eSGabriel Krisman Bertazi 
4317c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
4318c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
4319c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4320c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
4321c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4322c83ad55eSGabriel Krisman Bertazi 		}
4323c83ad55eSGabriel Krisman Bertazi 
4324c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
4325c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
4326c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4327c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
4328c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4329c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4330c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4331c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4332c83ad55eSGabriel Krisman Bertazi 		}
4333c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4334c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4335c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4336c83ad55eSGabriel Krisman Bertazi 
4337f8f4acb6SDaniel Rosenberg 		sb->s_encoding = encoding;
4338f8f4acb6SDaniel Rosenberg 		sb->s_encoding_flags = encoding_flags;
4339c83ad55eSGabriel Krisman Bertazi 	}
4340c83ad55eSGabriel Krisman Bertazi #endif
4341c83ad55eSGabriel Krisman Bertazi 
434256889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4343*556e0319SHarshad Shirwadkar 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, O_DIRECT and fast_commit support!\n");
4344781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4345244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
4346*556e0319SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
434756889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
434856889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
434956889787STheodore Ts'o 				 "both data=journal and delalloc");
435056889787STheodore Ts'o 			goto failed_mount;
435156889787STheodore Ts'o 		}
4352fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4353923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4354923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4355923ae0ffSRoss Zwisler 			goto failed_mount;
4356923ae0ffSRoss Zwisler 		}
435773b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
435873b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
435973b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
436073b92a2aSSergey Karamov 				 "instead of data journaling mode");
436173b92a2aSSergey Karamov 		}
436256889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
436356889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4364001e4a87STejun Heo 	} else {
4365001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
436656889787STheodore Ts'o 	}
436756889787STheodore Ts'o 
43681751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
43691751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4370ac27a0ecSDave Kleikamp 
4371617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4372e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4373e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4374e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4375b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4376b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4377b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4378469108ffSTheodore Tso 
4379ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4380ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4381e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4382ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4383ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4384ed3654ebSTheodore Ts'o 			goto failed_mount;
4385ed3654ebSTheodore Ts'o 		}
4386dec214d0STahsin Erdogan 
4387dec214d0STahsin Erdogan 		/*
4388dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4389dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4390dec214d0STahsin Erdogan 		 */
4391dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4392dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4393dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4394dec214d0STahsin Erdogan 			goto failed_mount;
4395dec214d0STahsin Erdogan 		}
4396ed3654ebSTheodore Ts'o 	}
4397ed3654ebSTheodore Ts'o 
43982035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
43992035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
44002035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
44012035e776STheodore Ts'o 				 "using the ext4 subsystem");
44022035e776STheodore Ts'o 		else {
44030d9366d6SEric Sandeen 			/*
44040d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
44050d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
44060d9366d6SEric Sandeen 			 */
44070d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
44080d9366d6SEric Sandeen 				goto failed_mount;
44092035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
44102035e776STheodore Ts'o 				 "to feature incompatibilities");
44112035e776STheodore Ts'o 			goto failed_mount;
44122035e776STheodore Ts'o 		}
44132035e776STheodore Ts'o 	}
44142035e776STheodore Ts'o 
44152035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
44162035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
44172035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
44182035e776STheodore Ts'o 				 "using the ext4 subsystem");
44192035e776STheodore Ts'o 		else {
44200d9366d6SEric Sandeen 			/*
44210d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
44220d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
44230d9366d6SEric Sandeen 			 */
44240d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
44250d9366d6SEric Sandeen 				goto failed_mount;
44262035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
44272035e776STheodore Ts'o 				 "to feature incompatibilities");
44282035e776STheodore Ts'o 			goto failed_mount;
44292035e776STheodore Ts'o 		}
44302035e776STheodore Ts'o 	}
44312035e776STheodore Ts'o 
4432469108ffSTheodore Tso 	/*
4433ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4434ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4435ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4436ac27a0ecSDave Kleikamp 	 */
4437bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4438ac27a0ecSDave Kleikamp 		goto failed_mount;
4439a13fb1a4SEric Sandeen 
44408cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
44418cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
44428cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44438cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
44448cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4445ac27a0ecSDave Kleikamp 		goto failed_mount;
4446ac27a0ecSDave Kleikamp 	}
4447bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4448bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4449bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4450bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4451bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4452bfe0a5f4STheodore Ts'o 		goto failed_mount;
4453bfe0a5f4STheodore Ts'o 	}
4454ac27a0ecSDave Kleikamp 
44555b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
44565b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44575b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
44585b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
44595b9554dcSTheodore Ts'o 		goto failed_mount;
44605b9554dcSTheodore Ts'o 	}
44615b9554dcSTheodore Ts'o 
4462a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4463a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4464a8ab6d38SIra Weiny 
4465fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4466559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4467559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4468559db4c6SRoss Zwisler 					" that may contain inline data");
4469361d24d4SEric Sandeen 			goto failed_mount;
4470559db4c6SRoss Zwisler 		}
4471a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
447224f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4473361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4474361d24d4SEric Sandeen 			goto failed_mount;
447524f3478dSDan Williams 		}
4476923ae0ffSRoss Zwisler 	}
4477923ae0ffSRoss Zwisler 
4478e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
44796ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
44806ddb2447STheodore Ts'o 			 es->s_encryption_level);
44816ddb2447STheodore Ts'o 		goto failed_mount;
44826ddb2447STheodore Ts'o 	}
44836ddb2447STheodore Ts'o 
4484ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4485ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4486ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4487b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4488ce40733cSAneesh Kumar K.V 					blocksize);
4489ac27a0ecSDave Kleikamp 			goto failed_mount;
4490ac27a0ecSDave Kleikamp 		}
4491ac27a0ecSDave Kleikamp 
4492ac27a0ecSDave Kleikamp 		brelse(bh);
449370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
449470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
44958394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
44968394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4497b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4498b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
44998394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
45008394a6abSzhangyi (F) 			bh = NULL;
4501ac27a0ecSDave Kleikamp 			goto failed_mount;
4502ac27a0ecSDave Kleikamp 		}
45032716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4504ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4505617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4506b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4507b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4508ac27a0ecSDave Kleikamp 			goto failed_mount;
4509ac27a0ecSDave Kleikamp 		}
4510ac27a0ecSDave Kleikamp 	}
4511ac27a0ecSDave Kleikamp 
4512e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4513f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4514f287a1a5STheodore Ts'o 						      has_huge_files);
4515f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4516ac27a0ecSDave Kleikamp 
45170d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4518e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
45198fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
45200d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4521d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4522b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4523b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
45240d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
45250d1ee42fSAlexandre Ratchov 			goto failed_mount;
45260d1ee42fSAlexandre Ratchov 		}
45270d1ee42fSAlexandre Ratchov 	} else
45280d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
45290b8e58a1SAndreas Dilger 
4530ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4531ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
45320b8e58a1SAndreas Dilger 
4533617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4534ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4535617ba13bSMingming Cao 		goto cantfind_ext4;
4536cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4537cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4538cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4539b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4540cd6bb35bSTheodore Ts'o 		goto failed_mount;
4541cd6bb35bSTheodore Ts'o 	}
4542ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4543ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
45440d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4545ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4546ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4547e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4548e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
45490b8e58a1SAndreas Dilger 
4550ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4551ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4552ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4553e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4554f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4555f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4556f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4557f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4558f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4559bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
456023301410STheodore Ts'o 				es->s_flags |=
456123301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4562f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4563f99b2589STheodore Ts'o #else
4564bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
456523301410STheodore Ts'o 				es->s_flags |=
456623301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4567f99b2589STheodore Ts'o #endif
4568f99b2589STheodore Ts'o 		}
456923301410STheodore Ts'o 	}
4570ac27a0ecSDave Kleikamp 
4571281b5995STheodore Ts'o 	/* Handle clustersize */
4572281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4573ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4574281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4575281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4576281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4577281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4578281b5995STheodore Ts'o 			goto failed_mount;
4579281b5995STheodore Ts'o 		}
4580281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4581281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4582281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4583281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4584281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4585281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4586281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4587281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4588281b5995STheodore Ts'o 			goto failed_mount;
4589281b5995STheodore Ts'o 		}
4590281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4591281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4592281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4593281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4594281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4595281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4596281b5995STheodore Ts'o 			goto failed_mount;
4597281b5995STheodore Ts'o 		}
4598281b5995STheodore Ts'o 	} else {
4599281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4600bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4601bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4602bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4603bfe0a5f4STheodore Ts'o 			goto failed_mount;
4604281b5995STheodore Ts'o 		}
4605ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4606b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4607b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4608ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4609ac27a0ecSDave Kleikamp 			goto failed_mount;
4610ac27a0ecSDave Kleikamp 		}
4611281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4612281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4613281b5995STheodore Ts'o 	}
4614281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4615281b5995STheodore Ts'o 
4616960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4617960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4618960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4619960fd856STheodore Ts'o 
4620bf43d84bSEric Sandeen 	/*
4621bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4622bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4623bf43d84bSEric Sandeen 	 */
46245a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
462530ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
46265a9ae68aSDarrick J. Wong 	if (err) {
4627b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4628bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4629ac27a0ecSDave Kleikamp 		goto failed_mount;
4630ac27a0ecSDave Kleikamp 	}
4631ac27a0ecSDave Kleikamp 
4632617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4633617ba13bSMingming Cao 		goto cantfind_ext4;
4634e7c95593SEric Sandeen 
46350f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
46360f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
46370f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4638b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4639b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
46400f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
46410f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
46420f2ddca6SFrom: Thiemo Nagel 	}
46430f2ddca6SFrom: Thiemo Nagel 
46444ec11028STheodore Ts'o 	/*
46454ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
46464ec11028STheodore Ts'o 	 * of the filesystem.
46474ec11028STheodore Ts'o 	 */
46484ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4649b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4650b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4651e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
46524ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4653e7c95593SEric Sandeen 		goto failed_mount;
4654e7c95593SEric Sandeen 	}
4655bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4656bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4657bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4658bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4659bfe0a5f4STheodore Ts'o 		goto failed_mount;
4660bfe0a5f4STheodore Ts'o 	}
4661bfe0a5f4STheodore Ts'o 
4662bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4663bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4664bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4665bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
46664ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4667df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
46684ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4669df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
46704ec11028STheodore Ts'o 		       ext4_blocks_count(es),
46714ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
46724ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
46734ec11028STheodore Ts'o 		goto failed_mount;
46744ec11028STheodore Ts'o 	}
4675bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4676fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4677fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
46789e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
46799e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
46809e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
46819e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
46829e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
46839e463084STheodore Ts'o 		ret = -EINVAL;
46849e463084STheodore Ts'o 		goto failed_mount;
46859e463084STheodore Ts'o 	}
4686617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4687617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
46883a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
46892ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
46903a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
46913a4b77cdSEryu Guan 				 "first meta block group too large: %u "
46923a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
46933a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
46943a4b77cdSEryu Guan 			goto failed_mount;
46953a4b77cdSEryu Guan 		}
46963a4b77cdSEryu Guan 	}
46971d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
46981d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4699f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
47001d0c3924STheodore Ts'o 					  GFP_KERNEL));
4701ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4702b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
47032cde417dSTheodore Ts'o 		ret = -ENOMEM;
4704ac27a0ecSDave Kleikamp 		goto failed_mount;
4705ac27a0ecSDave Kleikamp 	}
4706ac27a0ecSDave Kleikamp 
4707705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4708ac27a0ecSDave Kleikamp 
470985c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
471085c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
471185c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
47125df1d412Szhangyi (F) 		ext4_sb_breadahead_unmovable(sb, block);
471385c8f176SAndrew Perepechko 	}
471485c8f176SAndrew Perepechko 
4715ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
47161d0c3924STheodore Ts'o 		struct buffer_head *bh;
47171d0c3924STheodore Ts'o 
471870bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
47198394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, block);
47208394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4721b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4722b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4723ac27a0ecSDave Kleikamp 			db_count = i;
47248394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
47258394a6abSzhangyi (F) 			bh = NULL;
4726ac27a0ecSDave Kleikamp 			goto failed_mount2;
4727ac27a0ecSDave Kleikamp 		}
47281d0c3924STheodore Ts'o 		rcu_read_lock();
47291d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
47301d0c3924STheodore Ts'o 		rcu_read_unlock();
4731ac27a0ecSDave Kleikamp 	}
473244de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4733829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4734b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
47356a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4736f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4737ac27a0ecSDave Kleikamp 	}
4738772cb7c8SJose R. Santos 
4739235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
474004496411STao Ma 
4741a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4742eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4743ce7e010aSTheodore Ts'o 		goto failed_mount3;
4744ce7e010aSTheodore Ts'o 
4745c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
474667a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4747c9de560dSAlex Tomas 
4748f9ae9cf5STheodore Ts'o 	/*
4749f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4750f9ae9cf5STheodore Ts'o 	 */
4751f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4752617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4753617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4754643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4755a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4756ffcc4182SEric Biggers #endif
4757c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4758c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4759c93d8f88SEric Biggers #endif
4760ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4761617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4762e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
47631fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4764262b4662SJan Kara 	else
4765262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4766689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4767ac27a0ecSDave Kleikamp #endif
476885787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4769f2fa2ffcSAneesh Kumar K.V 
4770ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
47713b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4772ac27a0ecSDave Kleikamp 
4773aa75f4d3SHarshad Shirwadkar 	/* Initialize fast commit stuff */
4774aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_subtid, 0);
4775aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_ineligible_updates, 0);
4776aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4777aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4778aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4779aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4780aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bytes = 0;
4781ababea77SHarshad Shirwadkar 	sbi->s_mount_flags &= ~EXT4_MF_FC_INELIGIBLE;
4782ababea77SHarshad Shirwadkar 	sbi->s_mount_flags &= ~EXT4_MF_FC_COMMITTING;
4783aa75f4d3SHarshad Shirwadkar 	spin_lock_init(&sbi->s_fc_lock);
4784aa75f4d3SHarshad Shirwadkar 	memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
47858016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions = NULL;
47868016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_size = 0;
47878016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_used = 0;
47888016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_valid = 0;
47898016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes = NULL;
47908016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
47918016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4792aa75f4d3SHarshad Shirwadkar 
4793ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4794ac27a0ecSDave Kleikamp 
4795ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4796e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4797ac27a0ecSDave Kleikamp 
4798bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4799c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
480050460fe8SDarrick J. Wong 			goto failed_mount3a;
4801c5e06d10SJohann Lombardi 
4802ac27a0ecSDave Kleikamp 	/*
4803ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4804ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4805ac27a0ecSDave Kleikamp 	 */
4806e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
48074753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
48084753d8a2STheodore Ts'o 		if (err)
480950460fe8SDarrick J. Wong 			goto failed_mount3a;
4810bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4811e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4812b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4813b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4814744692dcSJiaying Zhang 		goto failed_mount_wq;
4815ac27a0ecSDave Kleikamp 	} else {
48161e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
48171e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
48181e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48191e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
48201e381f60SDmitry Monakhov 			goto failed_mount_wq;
48211e381f60SDmitry Monakhov 		}
48221e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
48231e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48241e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
48251e381f60SDmitry Monakhov 			goto failed_mount_wq;
48261e381f60SDmitry Monakhov 		}
48271e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
48281e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48291e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
48301e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
48311e381f60SDmitry Monakhov 			goto failed_mount_wq;
48321e381f60SDmitry Monakhov 		}
48331e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
48341e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
48351e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48361e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
48371e381f60SDmitry Monakhov 			goto failed_mount_wq;
48381e381f60SDmitry Monakhov 		}
483950b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
48401e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4841fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
4842995a3ed6SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
48430390131bSFrank Mayhar 		sbi->s_journal = NULL;
48440390131bSFrank Mayhar 		needs_recovery = 0;
48450390131bSFrank Mayhar 		goto no_journal;
4846ac27a0ecSDave Kleikamp 	}
4847ac27a0ecSDave Kleikamp 
4848e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4849eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4850eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4851b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4852744692dcSJiaying Zhang 		goto failed_mount_wq;
4853eb40a09cSJose R. Santos 	}
4854eb40a09cSJose R. Santos 
485525ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
485625ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
485725ed6e8aSDarrick J. Wong 			 "feature set");
485825ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4859d4da6c9cSLinus Torvalds 	}
4860818d276cSGirish Shilamkar 
4861a1e5e465SHarshad Shirwadkar 	if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4862a1e5e465SHarshad Shirwadkar 		!jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4863a1e5e465SHarshad Shirwadkar 					  JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4864a1e5e465SHarshad Shirwadkar 		ext4_msg(sb, KERN_ERR,
4865a1e5e465SHarshad Shirwadkar 			"Failed to set fast commit journal feature");
4866a1e5e465SHarshad Shirwadkar 		goto failed_mount_wq;
4867a1e5e465SHarshad Shirwadkar 	}
4868a1e5e465SHarshad Shirwadkar 
4869ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4870ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4871ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4872ac27a0ecSDave Kleikamp 	case 0:
4873ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
487463f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
487563f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
487663f57933SAndrew Morton 		 */
4877dab291afSMingming Cao 		if (jbd2_journal_check_available_features
487827f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4879fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
488027f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
488127f394a7STyson Nottingham 		} else {
4882fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
488327f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
488427f394a7STyson Nottingham 		}
4885ac27a0ecSDave Kleikamp 		break;
4886ac27a0ecSDave Kleikamp 
4887617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4888617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4889dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4890dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4891b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4892b31e1552SEric Sandeen 			       "requested data journaling mode");
4893744692dcSJiaying Zhang 			goto failed_mount_wq;
4894ac27a0ecSDave Kleikamp 		}
4895ac27a0ecSDave Kleikamp 	default:
4896ac27a0ecSDave Kleikamp 		break;
4897ac27a0ecSDave Kleikamp 	}
4898ab04df78SJan Kara 
4899ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4900ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4901ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4902ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4903ab04df78SJan Kara 		goto failed_mount_wq;
4904ab04df78SJan Kara 	}
4905ab04df78SJan Kara 
4906b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4907ac27a0ecSDave Kleikamp 
490818aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
4909342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_submit_inode_data_buffers =
4910afb585a9SMauricio Faria de Oliveira 		ext4_journal_submit_inode_data_buffers;
4911342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_finish_inode_data_buffers =
4912afb585a9SMauricio Faria de Oliveira 		ext4_journal_finish_inode_data_buffers;
491318aadd47SBobi Jam 
4914ce7e010aSTheodore Ts'o no_journal:
4915cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
491647387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
491747387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4918cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4919cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
49209c191f70ST Makphaibulchoke 			goto failed_mount_wq;
49219c191f70ST Makphaibulchoke 		}
49229c191f70ST Makphaibulchoke 
4923dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4924dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4925dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4926dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4927dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4928dec214d0STahsin Erdogan 				goto failed_mount_wq;
4929dec214d0STahsin Erdogan 			}
4930dec214d0STahsin Erdogan 		}
4931cdb7ee4cSTahsin Erdogan 	}
4932dec214d0STahsin Erdogan 
4933c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4934c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4935c93d8f88SEric Biggers 		goto failed_mount_wq;
4936c93d8f88SEric Biggers 	}
4937c93d8f88SEric Biggers 
4938bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4939e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4940e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
49416ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
49426ddb2447STheodore Ts'o 	}
49436ddb2447STheodore Ts'o 
4944fd89d5f2STejun Heo 	/*
4945952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4946952fc18eSTheodore Ts'o 	 * superblock if present.
4947952fc18eSTheodore Ts'o 	 */
4948952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4949952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4950952fc18eSTheodore Ts'o 	else {
495107aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
495207aa2ea1SLukas Czerner 		if (err)
4953952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4954952fc18eSTheodore Ts'o 	}
4955952fc18eSTheodore Ts'o 
4956952fc18eSTheodore Ts'o 	/*
4957fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4958fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4959fd89d5f2STejun Heo 	 */
49602e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
49612e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
49622e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
49632e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
496407aa2ea1SLukas Czerner 		ret = -ENOMEM;
49652e8fa54eSJan Kara 		goto failed_mount4;
49662e8fa54eSJan Kara 	}
49672e8fa54eSJan Kara 
4968ac27a0ecSDave Kleikamp 	/*
4969dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4970ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4971ac27a0ecSDave Kleikamp 	 */
4972ac27a0ecSDave Kleikamp 
49738a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
49741d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4975b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
49761d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
497732a9bb57SManish Katiyar 		root = NULL;
4978ac27a0ecSDave Kleikamp 		goto failed_mount4;
4979ac27a0ecSDave Kleikamp 	}
4980ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4981b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
498294bf608aSAl Viro 		iput(root);
4983ac27a0ecSDave Kleikamp 		goto failed_mount4;
4984ac27a0ecSDave Kleikamp 	}
4985b886ee3eSGabriel Krisman Bertazi 
4986b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4987f8f4acb6SDaniel Rosenberg 	if (sb->s_encoding)
4988b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4989b886ee3eSGabriel Krisman Bertazi #endif
4990b886ee3eSGabriel Krisman Bertazi 
499148fde701SAl Viro 	sb->s_root = d_make_root(root);
49921d1fe1eeSDavid Howells 	if (!sb->s_root) {
4993b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
49941d1fe1eeSDavid Howells 		ret = -ENOMEM;
49951d1fe1eeSDavid Howells 		goto failed_mount4;
49961d1fe1eeSDavid Howells 	}
4997ac27a0ecSDave Kleikamp 
4998c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4999c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
50001751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
5001c89128a0SJaegeuk Kim 		ret = 0;
5002c89128a0SJaegeuk Kim 	} else if (ret)
5003c89128a0SJaegeuk Kim 		goto failed_mount4a;
5004ef7f3835SKalpak Shah 
5005b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
500627dd4385SLukas Czerner 
50070f5bde1dSJan Kara 	if (test_opt(sb, BLOCK_VALIDITY)) {
50086fd058f7STheodore Ts'o 		err = ext4_setup_system_zone(sb);
50096fd058f7STheodore Ts'o 		if (err) {
5010b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
5011fbe845ddSCurt Wohlgemuth 				 "zone (%d)", err);
5012f9ae9cf5STheodore Ts'o 			goto failed_mount4a;
5013f9ae9cf5STheodore Ts'o 		}
50140f5bde1dSJan Kara 	}
50158016e29fSHarshad Shirwadkar 	ext4_fc_replay_cleanup(sb);
5016f9ae9cf5STheodore Ts'o 
5017f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
5018f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
5019f9ae9cf5STheodore Ts'o 	if (err) {
5020f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5021f9ae9cf5STheodore Ts'o 			 err);
5022dcf2d804STao Ma 		goto failed_mount5;
5023c2774d84SAneesh Kumar K.V 	}
5024c2774d84SAneesh Kumar K.V 
5025d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
5026d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
5027d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
50284274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
5029908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
5030908c7f19STejun Heo 				  GFP_KERNEL);
5031d5e03cbbSTheodore Ts'o 	if (!err) {
5032d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
5033d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
50344274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
5035908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
5036908c7f19STejun Heo 					  GFP_KERNEL);
5037d5e03cbbSTheodore Ts'o 	}
5038d5e03cbbSTheodore Ts'o 	if (!err)
5039d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
5040908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
5041d5e03cbbSTheodore Ts'o 	if (!err)
5042908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
5043908c7f19STejun Heo 					  GFP_KERNEL);
5044c8585c6fSDaeho Jeong 	if (!err)
5045bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
5046c8585c6fSDaeho Jeong 
5047d5e03cbbSTheodore Ts'o 	if (err) {
5048d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
5049d5e03cbbSTheodore Ts'o 		goto failed_mount6;
5050d5e03cbbSTheodore Ts'o 	}
5051d5e03cbbSTheodore Ts'o 
5052e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
5053d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
5054d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
5055d5e03cbbSTheodore Ts'o 			       "unable to initialize "
5056d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
5057d5e03cbbSTheodore Ts'o 			goto failed_mount6;
5058d5e03cbbSTheodore Ts'o 		}
5059d5e03cbbSTheodore Ts'o 
5060bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
5061bfff6873SLukas Czerner 	if (err)
5062dcf2d804STao Ma 		goto failed_mount6;
5063bfff6873SLukas Czerner 
5064b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
5065dcf2d804STao Ma 	if (err)
5066dcf2d804STao Ma 		goto failed_mount7;
50673197ebdbSTheodore Ts'o 
50689b2ff357SJan Kara #ifdef CONFIG_QUOTA
50699b2ff357SJan Kara 	/* Enable quota usage during mount. */
5070bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
50719b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
50729b2ff357SJan Kara 		if (err)
50739b2ff357SJan Kara 			goto failed_mount8;
50749b2ff357SJan Kara 	}
50759b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
50769b2ff357SJan Kara 
5077bc71726cSzhangyi (F) 	/*
5078bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
5079bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
5080bc71726cSzhangyi (F) 	 */
5081bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
5082bc71726cSzhangyi (F) 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5083bc71726cSzhangyi (F) 				 &sbi->s_bdev_wb_err);
5084bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
5085617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5086617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
5087617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
50880390131bSFrank Mayhar 	if (needs_recovery) {
5089b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
509011215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
509111215630SJan Kara 		if (err)
509211215630SJan Kara 			goto failed_mount8;
50930390131bSFrank Mayhar 	}
50940390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
50950390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
50960390131bSFrank Mayhar 			descr = " journalled data mode";
50970390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
50980390131bSFrank Mayhar 			descr = " ordered data mode";
50990390131bSFrank Mayhar 		else
51000390131bSFrank Mayhar 			descr = " writeback data mode";
51010390131bSFrank Mayhar 	} else
51020390131bSFrank Mayhar 		descr = "out journal";
51030390131bSFrank Mayhar 
510479add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
510579add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
510679add3a3SLukas Czerner 		if (!blk_queue_discard(q))
510779add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
510879add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
510979add3a3SLukas Czerner 				 "the device does not support discard");
511079add3a3SLukas Czerner 	}
511179add3a3SLukas Czerner 
5112e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
5113d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
51145aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
51155aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
51165aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
51178b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
5118ac27a0ecSDave Kleikamp 
511966e61a9eSTheodore Ts'o 	if (es->s_error_count)
512066e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5121ac27a0ecSDave Kleikamp 
5122efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5123efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5124efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5125efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
51261cf006edSDmitry Monakhov 	atomic_set(&sbi->s_warning_count, 0);
51271cf006edSDmitry Monakhov 	atomic_set(&sbi->s_msg_count, 0);
5128efbed4dcSTheodore Ts'o 
5129d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5130ac27a0ecSDave Kleikamp 	return 0;
5131ac27a0ecSDave Kleikamp 
5132617ba13bSMingming Cao cantfind_ext4:
5133ac27a0ecSDave Kleikamp 	if (!silent)
5134b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5135ac27a0ecSDave Kleikamp 	goto failed_mount;
5136ac27a0ecSDave Kleikamp 
513772ba7450STheodore Ts'o failed_mount8:
5138ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
5139cb8d53d2SEric Biggers 	kobject_put(&sbi->s_kobj);
5140dcf2d804STao Ma failed_mount7:
5141dcf2d804STao Ma 	ext4_unregister_li_request(sb);
5142dcf2d804STao Ma failed_mount6:
5143f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
51447c990728SSuraj Jitindar Singh 	rcu_read_lock();
51457c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
51467c990728SSuraj Jitindar Singh 	if (flex_groups) {
51477c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
51487c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
51497c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
51507c990728SSuraj Jitindar Singh 	}
51517c990728SSuraj Jitindar Singh 	rcu_read_unlock();
5152d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
5153d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
5154d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
5155d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5156bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
515700764937SAzat Khuzhin failed_mount5:
5158f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
5159f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
5160f9ae9cf5STheodore Ts'o failed_mount4a:
516194bf608aSAl Viro 	dput(sb->s_root);
516232a9bb57SManish Katiyar 	sb->s_root = NULL;
516394bf608aSAl Viro failed_mount4:
5164b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
51652e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
51662e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
51674c0425ffSMingming Cao failed_mount_wq:
5168dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5169dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
517050c15df6SChengguang Xu 
517147387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
517247387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
517350c15df6SChengguang Xu 
51740390131bSFrank Mayhar 	if (sbi->s_journal) {
5175dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
517647b4a50bSJan Kara 		sbi->s_journal = NULL;
51770390131bSFrank Mayhar 	}
517850460fe8SDarrick J. Wong failed_mount3a:
5179d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
5180eb68d0e2SZheng Liu failed_mount3:
51819105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
5182c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
5183c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
5184ac27a0ecSDave Kleikamp failed_mount2:
51851d0c3924STheodore Ts'o 	rcu_read_lock();
51861d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
5187ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
51881d0c3924STheodore Ts'o 		brelse(group_desc[i]);
51891d0c3924STheodore Ts'o 	kvfree(group_desc);
51901d0c3924STheodore Ts'o 	rcu_read_unlock();
5191ac27a0ecSDave Kleikamp failed_mount:
51920441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
51930441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
5194c83ad55eSGabriel Krisman Bertazi 
5195c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
5196f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
5197c83ad55eSGabriel Krisman Bertazi #endif
5198c83ad55eSGabriel Krisman Bertazi 
5199ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5200a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
52010ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
5202ac27a0ecSDave Kleikamp #endif
5203ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5204617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
5205ac27a0ecSDave Kleikamp 	brelse(bh);
5206ac27a0ecSDave Kleikamp out_fail:
5207ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
5208f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
52095aee0f8aSTheodore Ts'o out_free_base:
5210ac27a0ecSDave Kleikamp 	kfree(sbi);
5211d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
52125e405595SDan Williams 	fs_put_dax(dax_dev);
521307aa2ea1SLukas Czerner 	return err ? err : ret;
5214ac27a0ecSDave Kleikamp }
5215ac27a0ecSDave Kleikamp 
5216ac27a0ecSDave Kleikamp /*
5217ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
5218ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
5219ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
5220ac27a0ecSDave Kleikamp  */
5221617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5222ac27a0ecSDave Kleikamp {
5223617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5224ac27a0ecSDave Kleikamp 
5225ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
522630773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
522730773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
52286866d7b3SHarshad Shirwadkar 	ext4_fc_init(sb, journal);
5229ac27a0ecSDave Kleikamp 
5230a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
5231ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
5232dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
5233ac27a0ecSDave Kleikamp 	else
5234dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
52355bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
52365bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
52375bf5683aSHidehiro Kawai 	else
52385bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5239a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
5240ac27a0ecSDave Kleikamp }
5241ac27a0ecSDave Kleikamp 
5242c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
5243ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
5244ac27a0ecSDave Kleikamp {
5245ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
5246ac27a0ecSDave Kleikamp 
5247c6cb7e77SEric Whitney 	/*
5248c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
5249c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
5250c6cb7e77SEric Whitney 	 * will try to delete it.
5251c6cb7e77SEric Whitney 	 */
52528a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
52531d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
5254b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
5255ac27a0ecSDave Kleikamp 		return NULL;
5256ac27a0ecSDave Kleikamp 	}
5257ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
5258ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
5259ac27a0ecSDave Kleikamp 		iput(journal_inode);
5260b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5261ac27a0ecSDave Kleikamp 		return NULL;
5262ac27a0ecSDave Kleikamp 	}
5263ac27a0ecSDave Kleikamp 
5264e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5265ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
52661d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
5267b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5268ac27a0ecSDave Kleikamp 		iput(journal_inode);
5269ac27a0ecSDave Kleikamp 		return NULL;
5270ac27a0ecSDave Kleikamp 	}
5271c6cb7e77SEric Whitney 	return journal_inode;
5272c6cb7e77SEric Whitney }
5273c6cb7e77SEric Whitney 
5274c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
5275c6cb7e77SEric Whitney 				   unsigned int journal_inum)
5276c6cb7e77SEric Whitney {
5277c6cb7e77SEric Whitney 	struct inode *journal_inode;
5278c6cb7e77SEric Whitney 	journal_t *journal;
5279c6cb7e77SEric Whitney 
528011215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
528111215630SJan Kara 		return NULL;
5282c6cb7e77SEric Whitney 
5283c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5284c6cb7e77SEric Whitney 	if (!journal_inode)
5285c6cb7e77SEric Whitney 		return NULL;
5286ac27a0ecSDave Kleikamp 
5287dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
5288ac27a0ecSDave Kleikamp 	if (!journal) {
5289b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5290ac27a0ecSDave Kleikamp 		iput(journal_inode);
5291ac27a0ecSDave Kleikamp 		return NULL;
5292ac27a0ecSDave Kleikamp 	}
5293ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5294617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5295ac27a0ecSDave Kleikamp 	return journal;
5296ac27a0ecSDave Kleikamp }
5297ac27a0ecSDave Kleikamp 
5298617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
5299ac27a0ecSDave Kleikamp 				       dev_t j_dev)
5300ac27a0ecSDave Kleikamp {
5301ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5302ac27a0ecSDave Kleikamp 	journal_t *journal;
5303617ba13bSMingming Cao 	ext4_fsblk_t start;
5304617ba13bSMingming Cao 	ext4_fsblk_t len;
5305ac27a0ecSDave Kleikamp 	int hblock, blocksize;
5306617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
5307ac27a0ecSDave Kleikamp 	unsigned long offset;
5308617ba13bSMingming Cao 	struct ext4_super_block *es;
5309ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5310ac27a0ecSDave Kleikamp 
531111215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
531211215630SJan Kara 		return NULL;
53130390131bSFrank Mayhar 
5314b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5315ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5316ac27a0ecSDave Kleikamp 		return NULL;
5317ac27a0ecSDave Kleikamp 
5318ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5319e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5320ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5321b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5322b31e1552SEric Sandeen 			"blocksize too small for journal device");
5323ac27a0ecSDave Kleikamp 		goto out_bdev;
5324ac27a0ecSDave Kleikamp 	}
5325ac27a0ecSDave Kleikamp 
5326617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5327617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5328ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5329ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5330b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5331b31e1552SEric Sandeen 		       "external journal");
5332ac27a0ecSDave Kleikamp 		goto out_bdev;
5333ac27a0ecSDave Kleikamp 	}
5334ac27a0ecSDave Kleikamp 
53352716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5336617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5337ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5338617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5339b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5340b31e1552SEric Sandeen 					"bad superblock");
5341ac27a0ecSDave Kleikamp 		brelse(bh);
5342ac27a0ecSDave Kleikamp 		goto out_bdev;
5343ac27a0ecSDave Kleikamp 	}
5344ac27a0ecSDave Kleikamp 
5345df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5346df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5347df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5348df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5349df4763beSDarrick J. Wong 				       "corrupt superblock");
5350df4763beSDarrick J. Wong 		brelse(bh);
5351df4763beSDarrick J. Wong 		goto out_bdev;
5352df4763beSDarrick J. Wong 	}
5353df4763beSDarrick J. Wong 
5354617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5355b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5356ac27a0ecSDave Kleikamp 		brelse(bh);
5357ac27a0ecSDave Kleikamp 		goto out_bdev;
5358ac27a0ecSDave Kleikamp 	}
5359ac27a0ecSDave Kleikamp 
5360bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5361ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5362ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5363ac27a0ecSDave Kleikamp 
5364dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5365ac27a0ecSDave Kleikamp 					start, len, blocksize);
5366ac27a0ecSDave Kleikamp 	if (!journal) {
5367b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5368ac27a0ecSDave Kleikamp 		goto out_bdev;
5369ac27a0ecSDave Kleikamp 	}
5370ac27a0ecSDave Kleikamp 	journal->j_private = sb;
53712d069c08Szhangyi (F) 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5372b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5373ac27a0ecSDave Kleikamp 		goto out_journal;
5374ac27a0ecSDave Kleikamp 	}
5375ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5376b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5377b31e1552SEric Sandeen 					"user (unsupported) - %d",
5378ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5379ac27a0ecSDave Kleikamp 		goto out_journal;
5380ac27a0ecSDave Kleikamp 	}
5381ee7ed3aaSChunguang Xu 	EXT4_SB(sb)->s_journal_bdev = bdev;
5382617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5383ac27a0ecSDave Kleikamp 	return journal;
53840b8e58a1SAndreas Dilger 
5385ac27a0ecSDave Kleikamp out_journal:
5386dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5387ac27a0ecSDave Kleikamp out_bdev:
5388617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5389ac27a0ecSDave Kleikamp 	return NULL;
5390ac27a0ecSDave Kleikamp }
5391ac27a0ecSDave Kleikamp 
5392617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5393617ba13bSMingming Cao 			     struct ext4_super_block *es,
5394ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5395ac27a0ecSDave Kleikamp {
5396ac27a0ecSDave Kleikamp 	journal_t *journal;
5397ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5398ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5399ac27a0ecSDave Kleikamp 	int err = 0;
5400ac27a0ecSDave Kleikamp 	int really_read_only;
5401273108faSLukas Czerner 	int journal_dev_ro;
5402ac27a0ecSDave Kleikamp 
540311215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
540411215630SJan Kara 		return -EFSCORRUPTED;
54050390131bSFrank Mayhar 
5406ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5407ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5408b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5409b31e1552SEric Sandeen 			"numbers have changed");
5410ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5411ac27a0ecSDave Kleikamp 	} else
5412ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5413ac27a0ecSDave Kleikamp 
5414273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5415273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5416273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5417273108faSLukas Czerner 		return -EINVAL;
5418273108faSLukas Czerner 	}
5419273108faSLukas Czerner 
5420273108faSLukas Czerner 	if (journal_inum) {
5421273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5422273108faSLukas Czerner 		if (!journal)
5423273108faSLukas Czerner 			return -EINVAL;
5424273108faSLukas Czerner 	} else {
5425273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5426273108faSLukas Czerner 		if (!journal)
5427273108faSLukas Czerner 			return -EINVAL;
5428273108faSLukas Czerner 	}
5429273108faSLukas Czerner 
5430273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5431273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5432273108faSLukas Czerner 
5433273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5434273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5435273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5436273108faSLukas Czerner 		err = -EROFS;
5437273108faSLukas Czerner 		goto err_out;
5438273108faSLukas Czerner 	}
5439ac27a0ecSDave Kleikamp 
5440ac27a0ecSDave Kleikamp 	/*
5441ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5442ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5443ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5444ac27a0ecSDave Kleikamp 	 */
5445e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5446bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5447b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5448b31e1552SEric Sandeen 					"required on readonly filesystem");
5449ac27a0ecSDave Kleikamp 			if (really_read_only) {
5450b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5451d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5452d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5453273108faSLukas Czerner 				err = -EROFS;
5454273108faSLukas Czerner 				goto err_out;
5455ac27a0ecSDave Kleikamp 			}
5456b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5457b31e1552SEric Sandeen 			       "be enabled during recovery");
5458ac27a0ecSDave Kleikamp 		}
5459ac27a0ecSDave Kleikamp 	}
5460ac27a0ecSDave Kleikamp 
546190576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5462b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
54634776004fSTheodore Ts'o 
5464e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5465dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
54661c13d5c0STheodore Ts'o 	if (!err) {
54671c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
54681c13d5c0STheodore Ts'o 		if (save)
54691c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
54701c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5471dab291afSMingming Cao 		err = jbd2_journal_load(journal);
54721c13d5c0STheodore Ts'o 		if (save)
54731c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
54741c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
54751c13d5c0STheodore Ts'o 		kfree(save);
54761c13d5c0STheodore Ts'o 	}
5477ac27a0ecSDave Kleikamp 
5478ac27a0ecSDave Kleikamp 	if (err) {
5479b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5480273108faSLukas Czerner 		goto err_out;
5481ac27a0ecSDave Kleikamp 	}
5482ac27a0ecSDave Kleikamp 
5483617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
548411215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
548511215630SJan Kara 	if (err) {
548611215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
548711215630SJan Kara 		jbd2_journal_destroy(journal);
548811215630SJan Kara 		return err;
548911215630SJan Kara 	}
5490ac27a0ecSDave Kleikamp 
5491c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5492ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5493ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5494ac27a0ecSDave Kleikamp 
5495ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5496e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5497ac27a0ecSDave Kleikamp 	}
5498ac27a0ecSDave Kleikamp 
5499ac27a0ecSDave Kleikamp 	return 0;
5500273108faSLukas Czerner 
5501273108faSLukas Czerner err_out:
5502273108faSLukas Czerner 	jbd2_journal_destroy(journal);
5503273108faSLukas Czerner 	return err;
5504ac27a0ecSDave Kleikamp }
5505ac27a0ecSDave Kleikamp 
5506e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5507ac27a0ecSDave Kleikamp {
5508e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5509617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5510c4be0c1dSTakashi Sato 	int error = 0;
5511ac27a0ecSDave Kleikamp 
5512bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5513c4be0c1dSTakashi Sato 		return error;
5514a17712c8SJon Derrick 
5515a17712c8SJon Derrick 	/*
551671290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
551771290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
551871290b36STheodore Ts'o 	 * write time when we are mounting the root file system
551971290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
552071290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
552171290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
552271290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
552371290b36STheodore Ts'o 	 * to complain and force a full file system check.
552471290b36STheodore Ts'o 	 */
55251751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
55266a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5527f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5528afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5529afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5530dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5531dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5532afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5533f613dfcbSTheodore Ts'o 	else
5534f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5535f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5536d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
553757042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
553857042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
553957042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5540d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
55417f93cff9STheodore Ts'o 		es->s_free_inodes_count =
55427f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
55435d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5544ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
554506db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
55461566a48aSTheodore Ts'o 	if (sync)
55474743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5548e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
55494743f839SPranay Kr. Srivastava 		/*
55504743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
55514743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
55524743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
55534743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
55544743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
55554743f839SPranay Kr. Srivastava 		 * write and hope for the best.
55564743f839SPranay Kr. Srivastava 		 */
55574743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
55584743f839SPranay Kr. Srivastava 		       "superblock detected");
55594743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
55604743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
55614743f839SPranay Kr. Srivastava 	}
5562ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5563914258bfSTheodore Ts'o 	if (sync) {
55641566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5565564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
556600473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5567c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5568b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5569b31e1552SEric Sandeen 			       "superblock");
5570914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5571914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5572914258bfSTheodore Ts'o 		}
5573914258bfSTheodore Ts'o 	}
5574c4be0c1dSTakashi Sato 	return error;
5575ac27a0ecSDave Kleikamp }
5576ac27a0ecSDave Kleikamp 
5577ac27a0ecSDave Kleikamp /*
5578ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5579ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5580ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5581ac27a0ecSDave Kleikamp  */
558211215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
5583617ba13bSMingming Cao 				       struct ext4_super_block *es)
5584ac27a0ecSDave Kleikamp {
558511215630SJan Kara 	int err;
5586617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5587ac27a0ecSDave Kleikamp 
5588e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
558911215630SJan Kara 		if (journal != NULL) {
559011215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
559111215630SJan Kara 				   "mounted!");
559211215630SJan Kara 			return -EFSCORRUPTED;
559311215630SJan Kara 		}
559411215630SJan Kara 		return 0;
55950390131bSFrank Mayhar 	}
5596dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
559711215630SJan Kara 	err = jbd2_journal_flush(journal);
559811215630SJan Kara 	if (err < 0)
55997ffe1ea8SHidehiro Kawai 		goto out;
56007ffe1ea8SHidehiro Kawai 
5601bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5602e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5603e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5604ac27a0ecSDave Kleikamp 	}
56057ffe1ea8SHidehiro Kawai out:
5606dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
560711215630SJan Kara 	return err;
5608ac27a0ecSDave Kleikamp }
5609ac27a0ecSDave Kleikamp 
5610ac27a0ecSDave Kleikamp /*
5611ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5612ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5613ac27a0ecSDave Kleikamp  * main filesystem now.
5614ac27a0ecSDave Kleikamp  */
561511215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
5616617ba13bSMingming Cao 				   struct ext4_super_block *es)
5617ac27a0ecSDave Kleikamp {
5618ac27a0ecSDave Kleikamp 	journal_t *journal;
5619ac27a0ecSDave Kleikamp 	int j_errno;
5620ac27a0ecSDave Kleikamp 	const char *errstr;
5621ac27a0ecSDave Kleikamp 
562211215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
562311215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
562411215630SJan Kara 		return -EFSCORRUPTED;
562511215630SJan Kara 	}
56260390131bSFrank Mayhar 
5627617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5628ac27a0ecSDave Kleikamp 
5629ac27a0ecSDave Kleikamp 	/*
5630ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5631617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5632ac27a0ecSDave Kleikamp 	 */
5633ac27a0ecSDave Kleikamp 
5634dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5635ac27a0ecSDave Kleikamp 	if (j_errno) {
5636ac27a0ecSDave Kleikamp 		char nbuf[16];
5637ac27a0ecSDave Kleikamp 
5638617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
563912062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5640ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
564112062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5642ac27a0ecSDave Kleikamp 
5643617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5644617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5645e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5646ac27a0ecSDave Kleikamp 
5647dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5648d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5649ac27a0ecSDave Kleikamp 	}
565011215630SJan Kara 	return 0;
5651ac27a0ecSDave Kleikamp }
5652ac27a0ecSDave Kleikamp 
5653ac27a0ecSDave Kleikamp /*
5654ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5655ac27a0ecSDave Kleikamp  * and wait on the commit.
5656ac27a0ecSDave Kleikamp  */
5657617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5658ac27a0ecSDave Kleikamp {
5659ac27a0ecSDave Kleikamp 	journal_t *journal;
5660ac27a0ecSDave Kleikamp 
5661bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5662ac27a0ecSDave Kleikamp 		return 0;
5663ac27a0ecSDave Kleikamp 
5664617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5665b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5666ac27a0ecSDave Kleikamp }
5667ac27a0ecSDave Kleikamp 
5668617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5669ac27a0ecSDave Kleikamp {
567014ce0cb4STheodore Ts'o 	int ret = 0;
56719eddacf9SJan Kara 	tid_t target;
567206a407f1SDmitry Monakhov 	bool needs_barrier = false;
56738d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5674ac27a0ecSDave Kleikamp 
567549598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
56760db1ff22STheodore Ts'o 		return 0;
56770db1ff22STheodore Ts'o 
56789bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
56792e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5680a1177825SJan Kara 	/*
5681a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5682a1177825SJan Kara 	 * no dirty dquots
5683a1177825SJan Kara 	 */
5684a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
568506a407f1SDmitry Monakhov 	/*
568606a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
568706a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
568806a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
568906a407f1SDmitry Monakhov 	 */
5690bda32530STheodore Ts'o 	if (sbi->s_journal) {
569106a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
569206a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
569306a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
569406a407f1SDmitry Monakhov 			needs_barrier = true;
569506a407f1SDmitry Monakhov 
56968d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5697ac27a0ecSDave Kleikamp 			if (wait)
5698bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5699bda32530STheodore Ts'o 							   target);
57000390131bSFrank Mayhar 		}
5701bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5702bda32530STheodore Ts'o 		needs_barrier = true;
570306a407f1SDmitry Monakhov 	if (needs_barrier) {
570406a407f1SDmitry Monakhov 		int err;
57059398554fSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
570606a407f1SDmitry Monakhov 		if (!ret)
570706a407f1SDmitry Monakhov 			ret = err;
570806a407f1SDmitry Monakhov 	}
570906a407f1SDmitry Monakhov 
571006a407f1SDmitry Monakhov 	return ret;
571106a407f1SDmitry Monakhov }
571206a407f1SDmitry Monakhov 
5713ac27a0ecSDave Kleikamp /*
5714ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5715ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5716be4f27d3SYongqiang Yang  *
5717be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
57188e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
57198e8ad8a5SJan Kara  * modifications.
5720ac27a0ecSDave Kleikamp  */
5721c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5722ac27a0ecSDave Kleikamp {
5723c4be0c1dSTakashi Sato 	int error = 0;
5724c4be0c1dSTakashi Sato 	journal_t *journal;
5725ac27a0ecSDave Kleikamp 
5726bc98a42cSDavid Howells 	if (sb_rdonly(sb))
57279ca92389STheodore Ts'o 		return 0;
57289ca92389STheodore Ts'o 
5729c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5730ac27a0ecSDave Kleikamp 
5731bb044576STheodore Ts'o 	if (journal) {
5732ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5733dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
57347ffe1ea8SHidehiro Kawai 
57357ffe1ea8SHidehiro Kawai 		/*
5736bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5737bb044576STheodore Ts'o 		 * flush the journal.
57387ffe1ea8SHidehiro Kawai 		 */
5739c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
57406b0310fbSEric Sandeen 		if (error < 0)
57416b0310fbSEric Sandeen 			goto out;
5742ac27a0ecSDave Kleikamp 
5743ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5744e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5745c642dc9eSEric Sandeen 	}
5746c642dc9eSEric Sandeen 
5747e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
57486b0310fbSEric Sandeen out:
5749bb044576STheodore Ts'o 	if (journal)
57508e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5751bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
57526b0310fbSEric Sandeen 	return error;
5753ac27a0ecSDave Kleikamp }
5754ac27a0ecSDave Kleikamp 
5755ac27a0ecSDave Kleikamp /*
5756ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5757ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5758ac27a0ecSDave Kleikamp  */
5759c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5760ac27a0ecSDave Kleikamp {
5761bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
57629ca92389STheodore Ts'o 		return 0;
57639ca92389STheodore Ts'o 
5764c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
57659ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5766e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5767c642dc9eSEric Sandeen 	}
5768c642dc9eSEric Sandeen 
5769e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5770c4be0c1dSTakashi Sato 	return 0;
5771ac27a0ecSDave Kleikamp }
5772ac27a0ecSDave Kleikamp 
5773673c6100STheodore Ts'o /*
5774673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5775673c6100STheodore Ts'o  */
5776673c6100STheodore Ts'o struct ext4_mount_options {
5777673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5778a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
577908cefc7aSEric W. Biederman 	kuid_t s_resuid;
578008cefc7aSEric W. Biederman 	kgid_t s_resgid;
5781673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5782673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5783673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5784673c6100STheodore Ts'o 	int s_jquota_fmt;
5785a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5786673c6100STheodore Ts'o #endif
5787673c6100STheodore Ts'o };
5788673c6100STheodore Ts'o 
5789617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5790ac27a0ecSDave Kleikamp {
5791617ba13bSMingming Cao 	struct ext4_super_block *es;
5792617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5793f25391ebSLukas Czerner 	unsigned long old_sb_flags, vfs_flags;
5794617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5795c79d967dSChristoph Hellwig 	int enable_quota = 0;
57968a266467STheodore Ts'o 	ext4_group_t g;
5797b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5798c5e06d10SJohann Lombardi 	int err = 0;
5799ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
580003dafb5fSChen Gang 	int i, j;
580133458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5802ac27a0ecSDave Kleikamp #endif
5803d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5804ac27a0ecSDave Kleikamp 
580521ac738eSChengguang Xu 	if (data && !orig_data)
580621ac738eSChengguang Xu 		return -ENOMEM;
580721ac738eSChengguang Xu 
5808ac27a0ecSDave Kleikamp 	/* Store the original options */
5809ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5810ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5811a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5812ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5813ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5814ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
581530773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
581630773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5817ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5818ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5819a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
582003dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
582133458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
582233458eabSTheodore Ts'o 
582333458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
582403dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
582503dafb5fSChen Gang 				for (j = 0; j < i; j++)
582603dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
58273e36a163SWei Yongjun 				kfree(orig_data);
582803dafb5fSChen Gang 				return -ENOMEM;
582903dafb5fSChen Gang 			}
583003dafb5fSChen Gang 		} else
583103dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5832ac27a0ecSDave Kleikamp #endif
5833b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5834b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5835ac27a0ecSDave Kleikamp 
5836f25391ebSLukas Czerner 	/*
5837f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
5838f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
5839f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from *flags to s_flags
5840f25391ebSLukas Czerner 	 */
5841f25391ebSLukas Czerner 	vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5842f25391ebSLukas Czerner 	sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5843f25391ebSLukas Czerner 
5844661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5845ac27a0ecSDave Kleikamp 		err = -EINVAL;
5846ac27a0ecSDave Kleikamp 		goto restore_opts;
5847ac27a0ecSDave Kleikamp 	}
5848ac27a0ecSDave Kleikamp 
58496b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
58506b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
58516b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
58522d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
58532d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5854c6d3d56dSDarrick J. Wong 	}
5855c6d3d56dSDarrick J. Wong 
58566ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
58576ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
58586ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
58596ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
58606ae6514bSPiotr Sarna 			err = -EINVAL;
58616ae6514bSPiotr Sarna 			goto restore_opts;
58626ae6514bSPiotr Sarna 		}
58636ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
58646ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
58656ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
58666ae6514bSPiotr Sarna 			err = -EINVAL;
58676ae6514bSPiotr Sarna 			goto restore_opts;
58686ae6514bSPiotr Sarna 		}
5869ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5870ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5871ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5872ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5873ab04df78SJan Kara 			err = -EINVAL;
5874ab04df78SJan Kara 			goto restore_opts;
5875ab04df78SJan Kara 		}
5876923ae0ffSRoss Zwisler 	}
5877923ae0ffSRoss Zwisler 
5878cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5879cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5880cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5881cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5882cdb7ee4cSTahsin Erdogan 	}
5883cdb7ee4cSTahsin Erdogan 
58844ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
588554d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5886ac27a0ecSDave Kleikamp 
58871751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
58881751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5889ac27a0ecSDave Kleikamp 
5890ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5891ac27a0ecSDave Kleikamp 
5892b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5893617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5894b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5895b3881f74STheodore Ts'o 	}
5896ac27a0ecSDave Kleikamp 
58971751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
58984ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5899ac27a0ecSDave Kleikamp 			err = -EROFS;
5900ac27a0ecSDave Kleikamp 			goto restore_opts;
5901ac27a0ecSDave Kleikamp 		}
5902ac27a0ecSDave Kleikamp 
59031751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
590438c03b34STheodore Ts'o 			err = sync_filesystem(sb);
590538c03b34STheodore Ts'o 			if (err < 0)
590638c03b34STheodore Ts'o 				goto restore_opts;
59070f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
59080f0dd62fSChristoph Hellwig 			if (err < 0)
5909c79d967dSChristoph Hellwig 				goto restore_opts;
5910c79d967dSChristoph Hellwig 
5911ac27a0ecSDave Kleikamp 			/*
5912ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5913ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5914ac27a0ecSDave Kleikamp 			 */
59151751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5916ac27a0ecSDave Kleikamp 
5917ac27a0ecSDave Kleikamp 			/*
5918ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5919ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5920ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5921ac27a0ecSDave Kleikamp 			 */
5922617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5923617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5924ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5925ac27a0ecSDave Kleikamp 
592611215630SJan Kara 			if (sbi->s_journal) {
592711215630SJan Kara 				/*
592811215630SJan Kara 				 * We let remount-ro finish even if marking fs
592911215630SJan Kara 				 * as clean failed...
593011215630SJan Kara 				 */
5931617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
593211215630SJan Kara 			}
59332dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
59342dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5935ac27a0ecSDave Kleikamp 		} else {
5936a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5937e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
59382cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5939ac27a0ecSDave Kleikamp 				err = -EROFS;
5940ac27a0ecSDave Kleikamp 				goto restore_opts;
5941ac27a0ecSDave Kleikamp 			}
5942ead6596bSEric Sandeen 			/*
59438a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
59440b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
59458a266467STheodore Ts'o 			 */
59468a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
59478a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
59488a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
59498a266467STheodore Ts'o 
5950feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5951b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5952b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5953e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
59548a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
59556a797d27SDarrick J. Wong 					err = -EFSBADCRC;
59568a266467STheodore Ts'o 					goto restore_opts;
59578a266467STheodore Ts'o 				}
59588a266467STheodore Ts'o 			}
59598a266467STheodore Ts'o 
59608a266467STheodore Ts'o 			/*
5961ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5962ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5963ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5964ead6596bSEric Sandeen 			 */
5965ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5966b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5967ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5968ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5969b31e1552SEric Sandeen 				       "umount/remount instead");
5970ead6596bSEric Sandeen 				err = -EINVAL;
5971ead6596bSEric Sandeen 				goto restore_opts;
5972ead6596bSEric Sandeen 			}
5973ead6596bSEric Sandeen 
5974ac27a0ecSDave Kleikamp 			/*
5975ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5976ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5977ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5978ac27a0ecSDave Kleikamp 			 * the partition.)
5979ac27a0ecSDave Kleikamp 			 */
598011215630SJan Kara 			if (sbi->s_journal) {
598111215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
598211215630SJan Kara 				if (err)
598311215630SJan Kara 					goto restore_opts;
598411215630SJan Kara 			}
5985ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5986c89128a0SJaegeuk Kim 
5987c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5988c89128a0SJaegeuk Kim 			if (err)
5989c89128a0SJaegeuk Kim 				goto restore_opts;
5990c89128a0SJaegeuk Kim 
59911751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5992e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5993c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5994c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5995c5e06d10SJohann Lombardi 					err = -EROFS;
5996c5e06d10SJohann Lombardi 					goto restore_opts;
5997c5e06d10SJohann Lombardi 				}
5998c79d967dSChristoph Hellwig 			enable_quota = 1;
5999ac27a0ecSDave Kleikamp 		}
6000ac27a0ecSDave Kleikamp 	}
6001bfff6873SLukas Czerner 
6002bfff6873SLukas Czerner 	/*
6003bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
6004bfff6873SLukas Czerner 	 * current settings
6005bfff6873SLukas Czerner 	 */
6006bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
6007bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
6008bfff6873SLukas Czerner 	else {
6009bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
6010bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
6011bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
6012bfff6873SLukas Czerner 	}
6013bfff6873SLukas Czerner 
60140f5bde1dSJan Kara 	/*
60150f5bde1dSJan Kara 	 * Handle creation of system zone data early because it can fail.
60160f5bde1dSJan Kara 	 * Releasing of existing data is done when we are sure remount will
60170f5bde1dSJan Kara 	 * succeed.
60180f5bde1dSJan Kara 	 */
6019dd0db94fSChunguang Xu 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
6020d176b1f6SJan Kara 		err = ext4_setup_system_zone(sb);
6021d176b1f6SJan Kara 		if (err)
6022d176b1f6SJan Kara 			goto restore_opts;
60230f5bde1dSJan Kara 	}
6024d176b1f6SJan Kara 
6025c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
6026c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
6027c89128a0SJaegeuk Kim 		if (err)
6028c89128a0SJaegeuk Kim 			goto restore_opts;
6029c89128a0SJaegeuk Kim 	}
60300390131bSFrank Mayhar 
6031ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6032ac27a0ecSDave Kleikamp 	/* Release old quota file names */
6033a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
6034ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
60357c319d32SAditya Kali 	if (enable_quota) {
60367c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
60370f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
6038e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
60397c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
604007724f98STheodore Ts'o 			if (err)
60417c319d32SAditya Kali 				goto restore_opts;
60427c319d32SAditya Kali 		}
60437c319d32SAditya Kali 	}
60447c319d32SAditya Kali #endif
6045dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
60460f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6047d4c402d9SCurt Wohlgemuth 
6048f25391ebSLukas Czerner 	/*
6049f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
6050f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
6051f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from s_flags to *flags
6052f25391ebSLukas Czerner 	 */
6053f25391ebSLukas Czerner 	*flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
6054f25391ebSLukas Czerner 
6055d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
6056d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
6057ac27a0ecSDave Kleikamp 	return 0;
60580b8e58a1SAndreas Dilger 
6059ac27a0ecSDave Kleikamp restore_opts:
6060ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
6061ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
6062a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6063ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
6064ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
6065ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
606630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
606730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
6068dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
60690f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6070ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6071ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6072a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
607333458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
607433458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6075ac27a0ecSDave Kleikamp 	}
607633458eabSTheodore Ts'o 	synchronize_rcu();
607733458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
607833458eabSTheodore Ts'o 		kfree(to_free[i]);
6079ac27a0ecSDave Kleikamp #endif
6080d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
6081ac27a0ecSDave Kleikamp 	return err;
6082ac27a0ecSDave Kleikamp }
6083ac27a0ecSDave Kleikamp 
6084689c958cSLi Xi #ifdef CONFIG_QUOTA
6085689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
6086689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
6087689c958cSLi Xi {
6088689c958cSLi Xi 	struct kqid qid;
6089689c958cSLi Xi 	struct dquot *dquot;
6090689c958cSLi Xi 	u64 limit;
6091689c958cSLi Xi 	u64 curblock;
6092689c958cSLi Xi 
6093689c958cSLi Xi 	qid = make_kqid_projid(projid);
6094689c958cSLi Xi 	dquot = dqget(sb, qid);
6095689c958cSLi Xi 	if (IS_ERR(dquot))
6096689c958cSLi Xi 		return PTR_ERR(dquot);
60977b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
6098689c958cSLi Xi 
6099a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6100a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
610157c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
610257c32ea4SChengguang Xu 
6103689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
6104f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
6105f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6106689c958cSLi Xi 		buf->f_blocks = limit;
6107689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
6108689c958cSLi Xi 			(buf->f_blocks > curblock) ?
6109689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
6110689c958cSLi Xi 	}
6111689c958cSLi Xi 
6112a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6113a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
6114689c958cSLi Xi 	if (limit && buf->f_files > limit) {
6115689c958cSLi Xi 		buf->f_files = limit;
6116689c958cSLi Xi 		buf->f_ffree =
6117689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6118689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6119689c958cSLi Xi 	}
6120689c958cSLi Xi 
61217b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
6122689c958cSLi Xi 	dqput(dquot);
6123689c958cSLi Xi 	return 0;
6124689c958cSLi Xi }
6125689c958cSLi Xi #endif
6126689c958cSLi Xi 
6127617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6128ac27a0ecSDave Kleikamp {
6129ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
6130617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6131617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
613227dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
6133960cc398SPekka Enberg 	u64 fsid;
6134d02a9391SKazuya Mio 	s64 bfree;
613527dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6136ac27a0ecSDave Kleikamp 
6137952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
6138952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
6139ac27a0ecSDave Kleikamp 
6140617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
6141ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
6142b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
614357042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
614457042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6145d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
614657042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
614727dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
614827dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
614927dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6150ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
6151ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
615252d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6153617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
6154960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
6155960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
61566d1349c7SAl Viro 	buf->f_fsid = u64_to_fsid(fsid);
61570b8e58a1SAndreas Dilger 
6158689c958cSLi Xi #ifdef CONFIG_QUOTA
6159689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6160689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6161689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6162689c958cSLi Xi #endif
6163ac27a0ecSDave Kleikamp 	return 0;
6164ac27a0ecSDave Kleikamp }
6165ac27a0ecSDave Kleikamp 
6166ac27a0ecSDave Kleikamp 
6167ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6168ac27a0ecSDave Kleikamp 
6169bc8230eeSJan Kara /*
6170bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
6171bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
6172bc8230eeSJan Kara  */
6173ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
6174ac27a0ecSDave Kleikamp {
61754c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6176ac27a0ecSDave Kleikamp }
6177ac27a0ecSDave Kleikamp 
6178617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
6179ac27a0ecSDave Kleikamp {
6180ac27a0ecSDave Kleikamp 	int ret, err;
6181ac27a0ecSDave Kleikamp 	handle_t *handle;
6182ac27a0ecSDave Kleikamp 	struct inode *inode;
6183ac27a0ecSDave Kleikamp 
6184ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
61859924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6186617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6187ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6188ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6189ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
6190617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6191ac27a0ecSDave Kleikamp 	if (!ret)
6192ac27a0ecSDave Kleikamp 		ret = err;
6193ac27a0ecSDave Kleikamp 	return ret;
6194ac27a0ecSDave Kleikamp }
6195ac27a0ecSDave Kleikamp 
6196617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
6197ac27a0ecSDave Kleikamp {
6198ac27a0ecSDave Kleikamp 	int ret, err;
6199ac27a0ecSDave Kleikamp 	handle_t *handle;
6200ac27a0ecSDave Kleikamp 
62019924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6202617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6203ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6204ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6205ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
6206617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6207ac27a0ecSDave Kleikamp 	if (!ret)
6208ac27a0ecSDave Kleikamp 		ret = err;
6209ac27a0ecSDave Kleikamp 	return ret;
6210ac27a0ecSDave Kleikamp }
6211ac27a0ecSDave Kleikamp 
6212617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
6213ac27a0ecSDave Kleikamp {
6214ac27a0ecSDave Kleikamp 	int ret, err;
6215ac27a0ecSDave Kleikamp 	handle_t *handle;
6216ac27a0ecSDave Kleikamp 
62179924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6218617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
62199c3013e9SJan Kara 	if (IS_ERR(handle)) {
62209c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
62219c3013e9SJan Kara 		dquot_release(dquot);
6222ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
62239c3013e9SJan Kara 	}
6224ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
6225617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6226ac27a0ecSDave Kleikamp 	if (!ret)
6227ac27a0ecSDave Kleikamp 		ret = err;
6228ac27a0ecSDave Kleikamp 	return ret;
6229ac27a0ecSDave Kleikamp }
6230ac27a0ecSDave Kleikamp 
6231617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
6232ac27a0ecSDave Kleikamp {
6233262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
6234262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6235262b4662SJan Kara 
62362c8be6b2SJan Kara 	/* Are we journaling quotas? */
6237e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
6238262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
6239ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
6240617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
6241ac27a0ecSDave Kleikamp 	} else {
6242ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
6243ac27a0ecSDave Kleikamp 	}
6244ac27a0ecSDave Kleikamp }
6245ac27a0ecSDave Kleikamp 
6246617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
6247ac27a0ecSDave Kleikamp {
6248ac27a0ecSDave Kleikamp 	int ret, err;
6249ac27a0ecSDave Kleikamp 	handle_t *handle;
6250ac27a0ecSDave Kleikamp 
6251ac27a0ecSDave Kleikamp 	/* Data block + inode block */
62522b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6253ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6254ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6255ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
6256617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6257ac27a0ecSDave Kleikamp 	if (!ret)
6258ac27a0ecSDave Kleikamp 		ret = err;
6259ac27a0ecSDave Kleikamp 	return ret;
6260ac27a0ecSDave Kleikamp }
6261ac27a0ecSDave Kleikamp 
6262ac27a0ecSDave Kleikamp /*
6263ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
6264ac27a0ecSDave Kleikamp  * the quota file and such...
6265ac27a0ecSDave Kleikamp  */
6266617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
6267ac27a0ecSDave Kleikamp {
626833458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
6269617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
6270ac27a0ecSDave Kleikamp }
6271ac27a0ecSDave Kleikamp 
6272daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6273daf647d2STheodore Ts'o {
6274daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
6275daf647d2STheodore Ts'o 
6276daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
6277daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
6278daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
6279daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
6280daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
6281daf647d2STheodore Ts'o 	 */
6282daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6283daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6284daf647d2STheodore Ts'o }
6285daf647d2STheodore Ts'o 
6286ac27a0ecSDave Kleikamp /*
6287ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
6288ac27a0ecSDave Kleikamp  */
6289617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
62908c54ca9cSAl Viro 			 const struct path *path)
6291ac27a0ecSDave Kleikamp {
6292ac27a0ecSDave Kleikamp 	int err;
6293ac27a0ecSDave Kleikamp 
6294ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
6295ac27a0ecSDave Kleikamp 		return -EINVAL;
62960623543bSJan Kara 
6297ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
6298d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
6299ac27a0ecSDave Kleikamp 		return -EXDEV;
6300e0770e91SJan Kara 
6301e0770e91SJan Kara 	/* Quota already enabled for this file? */
6302e0770e91SJan Kara 	if (IS_NOQUOTA(d_inode(path->dentry)))
6303e0770e91SJan Kara 		return -EBUSY;
6304e0770e91SJan Kara 
63050623543bSJan Kara 	/* Journaling quota? */
63060623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
63072b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
6308f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
6309b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
6310b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
6311b31e1552SEric Sandeen 				"Journaled quota will not work");
631291389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
631391389240SJan Kara 	} else {
631491389240SJan Kara 		/*
631591389240SJan Kara 		 * Clear the flag just in case mount options changed since
631691389240SJan Kara 		 * last time.
631791389240SJan Kara 		 */
631891389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
63190623543bSJan Kara 	}
63200623543bSJan Kara 
63210623543bSJan Kara 	/*
63220623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
63230623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
63240623543bSJan Kara 	 */
63250390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
63262b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
63270623543bSJan Kara 		/*
63280623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
63290623543bSJan Kara 		 * otherwise be livelocked...
63300623543bSJan Kara 		 */
63310623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
63327ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
63330623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6334f00c9e44SJan Kara 		if (err)
63357ffe1ea8SHidehiro Kawai 			return err;
63367ffe1ea8SHidehiro Kawai 	}
6337957153fcSJan Kara 
6338daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6339daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
6340957153fcSJan Kara 	if (err) {
6341daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
6342daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
6343957153fcSJan Kara 	} else {
6344957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6345957153fcSJan Kara 		handle_t *handle;
6346957153fcSJan Kara 
634761a92987SJan Kara 		/*
634861a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
634961a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
635061a92987SJan Kara 		 * are already enabled and this is not a hard failure.
635161a92987SJan Kara 		 */
6352957153fcSJan Kara 		inode_lock(inode);
6353957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6354957153fcSJan Kara 		if (IS_ERR(handle))
6355957153fcSJan Kara 			goto unlock_inode;
6356957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6357957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6358957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
63594209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6360957153fcSJan Kara 		ext4_journal_stop(handle);
6361957153fcSJan Kara 	unlock_inode:
6362957153fcSJan Kara 		inode_unlock(inode);
6363957153fcSJan Kara 	}
6364daf647d2STheodore Ts'o 	return err;
6365ac27a0ecSDave Kleikamp }
6366ac27a0ecSDave Kleikamp 
63677c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
63687c319d32SAditya Kali 			     unsigned int flags)
63697c319d32SAditya Kali {
63707c319d32SAditya Kali 	int err;
63717c319d32SAditya Kali 	struct inode *qf_inode;
6372a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
63737c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6374689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6375689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
63767c319d32SAditya Kali 	};
63777c319d32SAditya Kali 
6378e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
63797c319d32SAditya Kali 
63807c319d32SAditya Kali 	if (!qf_inums[type])
63817c319d32SAditya Kali 		return -EPERM;
63827c319d32SAditya Kali 
63838a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
63847c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
63857c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
63867c319d32SAditya Kali 		return PTR_ERR(qf_inode);
63877c319d32SAditya Kali 	}
63887c319d32SAditya Kali 
6389bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6390bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6391daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
63927212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6393daf647d2STheodore Ts'o 	if (err)
6394daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
639561157b24SPan Bian 	iput(qf_inode);
63967c319d32SAditya Kali 
63977c319d32SAditya Kali 	return err;
63987c319d32SAditya Kali }
63997c319d32SAditya Kali 
64007c319d32SAditya Kali /* Enable usage tracking for all quota types. */
64017c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
64027c319d32SAditya Kali {
64037c319d32SAditya Kali 	int type, err = 0;
6404a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
64057c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6406689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6407689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
64087c319d32SAditya Kali 	};
640949da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
641049da9392SJan Kara 		test_opt(sb, USRQUOTA),
641149da9392SJan Kara 		test_opt(sb, GRPQUOTA),
641249da9392SJan Kara 		test_opt(sb, PRJQUOTA),
641349da9392SJan Kara 	};
64147c319d32SAditya Kali 
641591389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6416a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
64177c319d32SAditya Kali 		if (qf_inums[type]) {
64187c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
641949da9392SJan Kara 				DQUOT_USAGE_ENABLED |
642049da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
64217c319d32SAditya Kali 			if (err) {
64227c319d32SAditya Kali 				ext4_warning(sb,
642372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
642472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
642572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
64267f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
64277f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
64287f144fd0SJunichi Uekawa 
64297c319d32SAditya Kali 				return err;
64307c319d32SAditya Kali 			}
64317c319d32SAditya Kali 		}
64327c319d32SAditya Kali 	}
64337c319d32SAditya Kali 	return 0;
64347c319d32SAditya Kali }
64357c319d32SAditya Kali 
6436ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6437ca0e05e4SDmitry Monakhov {
643821f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
643921f97697SJan Kara 	handle_t *handle;
6440957153fcSJan Kara 	int err;
644121f97697SJan Kara 
644287009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
644387009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
644487009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6445ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6446ca0e05e4SDmitry Monakhov 
6447957153fcSJan Kara 	if (!inode || !igrab(inode))
64480b268590SAmir Goldstein 		goto out;
64490b268590SAmir Goldstein 
6450957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6451964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6452957153fcSJan Kara 		goto out_put;
6453957153fcSJan Kara 
6454957153fcSJan Kara 	inode_lock(inode);
645561a92987SJan Kara 	/*
645661a92987SJan Kara 	 * Update modification times of quota files when userspace can
645761a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
645861a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
645961a92987SJan Kara 	 */
64609924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
64614209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
64624209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6463957153fcSJan Kara 		goto out_unlock;
64644209ae12SHarshad Shirwadkar 	}
6465957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6466957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6467eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
64684209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
646921f97697SJan Kara 	ext4_journal_stop(handle);
6470957153fcSJan Kara out_unlock:
6471957153fcSJan Kara 	inode_unlock(inode);
6472957153fcSJan Kara out_put:
6473964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6474957153fcSJan Kara 	iput(inode);
6475957153fcSJan Kara 	return err;
647621f97697SJan Kara out:
6477ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6478ca0e05e4SDmitry Monakhov }
6479ca0e05e4SDmitry Monakhov 
6480ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6481ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6482ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6483ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6484617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6485ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6486ac27a0ecSDave Kleikamp {
6487ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6488725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6489ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6490ac27a0ecSDave Kleikamp 	int tocopy;
6491ac27a0ecSDave Kleikamp 	size_t toread;
6492ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6493ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6494ac27a0ecSDave Kleikamp 
6495ac27a0ecSDave Kleikamp 	if (off > i_size)
6496ac27a0ecSDave Kleikamp 		return 0;
6497ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6498ac27a0ecSDave Kleikamp 		len = i_size-off;
6499ac27a0ecSDave Kleikamp 	toread = len;
6500ac27a0ecSDave Kleikamp 	while (toread > 0) {
6501ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6502ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
65031c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
65041c215028STheodore Ts'o 		if (IS_ERR(bh))
65051c215028STheodore Ts'o 			return PTR_ERR(bh);
6506ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6507ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6508ac27a0ecSDave Kleikamp 		else
6509ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6510ac27a0ecSDave Kleikamp 		brelse(bh);
6511ac27a0ecSDave Kleikamp 		offset = 0;
6512ac27a0ecSDave Kleikamp 		toread -= tocopy;
6513ac27a0ecSDave Kleikamp 		data += tocopy;
6514ac27a0ecSDave Kleikamp 		blk++;
6515ac27a0ecSDave Kleikamp 	}
6516ac27a0ecSDave Kleikamp 	return len;
6517ac27a0ecSDave Kleikamp }
6518ac27a0ecSDave Kleikamp 
6519ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6520ac27a0ecSDave Kleikamp  * enough credits) */
6521617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6522ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6523ac27a0ecSDave Kleikamp {
6524ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6525725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
65264209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6527c5e298aeSTheodore Ts'o 	int retries = 0;
6528ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6529ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6530ac27a0ecSDave Kleikamp 
65310390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6532b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6533b31e1552SEric Sandeen 			" cancelled because transaction is not started",
65349c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
65359c3013e9SJan Kara 		return -EIO;
65369c3013e9SJan Kara 	}
653767eeb568SDmitry Monakhov 	/*
653867eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
653967eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
654067eeb568SDmitry Monakhov 	 */
654167eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
654267eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
654367eeb568SDmitry Monakhov 			" cancelled because not block aligned",
654467eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
654567eeb568SDmitry Monakhov 		return -EIO;
654667eeb568SDmitry Monakhov 	}
654767eeb568SDmitry Monakhov 
6548c5e298aeSTheodore Ts'o 	do {
6549c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6550c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6551c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
655245586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6553c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
65541c215028STheodore Ts'o 	if (IS_ERR(bh))
65551c215028STheodore Ts'o 		return PTR_ERR(bh);
6556ac27a0ecSDave Kleikamp 	if (!bh)
6557ac27a0ecSDave Kleikamp 		goto out;
65585d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6559617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6560ac27a0ecSDave Kleikamp 	if (err) {
6561ac27a0ecSDave Kleikamp 		brelse(bh);
65621c215028STheodore Ts'o 		return err;
6563ac27a0ecSDave Kleikamp 	}
6564ac27a0ecSDave Kleikamp 	lock_buffer(bh);
656567eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6566ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6567ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
65680390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6569ac27a0ecSDave Kleikamp 	brelse(bh);
6570ac27a0ecSDave Kleikamp out:
657167eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
657267eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6573617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
65744209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
65754209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
65764209ae12SHarshad Shirwadkar 			err = err2;
657721f97697SJan Kara 	}
65784209ae12SHarshad Shirwadkar 	return err ? err : len;
6579ac27a0ecSDave Kleikamp }
6580ac27a0ecSDave Kleikamp #endif
6581ac27a0ecSDave Kleikamp 
6582152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6583152a0836SAl Viro 		       const char *dev_name, void *data)
6584ac27a0ecSDave Kleikamp {
6585152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6586ac27a0ecSDave Kleikamp }
6587ac27a0ecSDave Kleikamp 
6588c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
658924b58424STheodore Ts'o static inline void register_as_ext2(void)
659024b58424STheodore Ts'o {
659124b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
659224b58424STheodore Ts'o 	if (err)
659324b58424STheodore Ts'o 		printk(KERN_WARNING
659424b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
659524b58424STheodore Ts'o }
659624b58424STheodore Ts'o 
659724b58424STheodore Ts'o static inline void unregister_as_ext2(void)
659824b58424STheodore Ts'o {
659924b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
660024b58424STheodore Ts'o }
66012035e776STheodore Ts'o 
66022035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
66032035e776STheodore Ts'o {
6604e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
66052035e776STheodore Ts'o 		return 0;
6606bc98a42cSDavid Howells 	if (sb_rdonly(sb))
66072035e776STheodore Ts'o 		return 1;
6608e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
66092035e776STheodore Ts'o 		return 0;
66102035e776STheodore Ts'o 	return 1;
66112035e776STheodore Ts'o }
661224b58424STheodore Ts'o #else
661324b58424STheodore Ts'o static inline void register_as_ext2(void) { }
661424b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
66152035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
661624b58424STheodore Ts'o #endif
661724b58424STheodore Ts'o 
661824b58424STheodore Ts'o static inline void register_as_ext3(void)
661924b58424STheodore Ts'o {
662024b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
662124b58424STheodore Ts'o 	if (err)
662224b58424STheodore Ts'o 		printk(KERN_WARNING
662324b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
662424b58424STheodore Ts'o }
662524b58424STheodore Ts'o 
662624b58424STheodore Ts'o static inline void unregister_as_ext3(void)
662724b58424STheodore Ts'o {
662824b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
662924b58424STheodore Ts'o }
66302035e776STheodore Ts'o 
66312035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
66322035e776STheodore Ts'o {
6633e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
66342035e776STheodore Ts'o 		return 0;
6635e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
66362035e776STheodore Ts'o 		return 0;
6637bc98a42cSDavid Howells 	if (sb_rdonly(sb))
66382035e776STheodore Ts'o 		return 1;
6639e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
66402035e776STheodore Ts'o 		return 0;
66412035e776STheodore Ts'o 	return 1;
66422035e776STheodore Ts'o }
664324b58424STheodore Ts'o 
664403010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6645ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
664603010a33STheodore Ts'o 	.name		= "ext4",
6647152a0836SAl Viro 	.mount		= ext4_mount,
6648ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6649ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6650ac27a0ecSDave Kleikamp };
66517f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6652ac27a0ecSDave Kleikamp 
6653e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6654e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6655e9e3bcecSEric Sandeen 
66565dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6657ac27a0ecSDave Kleikamp {
6658e9e3bcecSEric Sandeen 	int i, err;
6659c9de560dSAlex Tomas 
6660e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
666107c0c5d8SAl Viro 	ext4_li_info = NULL;
666207c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
666307c0c5d8SAl Viro 
66649a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
666512e9b892SDmitry Monakhov 	ext4_check_flag_values();
6666e9e3bcecSEric Sandeen 
6667e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6668e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6669e9e3bcecSEric Sandeen 
667051865fdaSZheng Liu 	err = ext4_init_es();
66716fd058f7STheodore Ts'o 	if (err)
66726fd058f7STheodore Ts'o 		return err;
667351865fdaSZheng Liu 
66741dc0aa46SEric Whitney 	err = ext4_init_pending();
66751dc0aa46SEric Whitney 	if (err)
667622cfe4b4SEric Biggers 		goto out7;
667722cfe4b4SEric Biggers 
667822cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
667922cfe4b4SEric Biggers 	if (err)
66801dc0aa46SEric Whitney 		goto out6;
66811dc0aa46SEric Whitney 
668251865fdaSZheng Liu 	err = ext4_init_pageio();
668351865fdaSZheng Liu 	if (err)
6684b5799018STheodore Ts'o 		goto out5;
668551865fdaSZheng Liu 
66865dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6687bd2d0210STheodore Ts'o 	if (err)
6688b5799018STheodore Ts'o 		goto out4;
6689857ac889SLukas Czerner 
6690b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6691dd68314cSTheodore Ts'o 	if (err)
6692b5799018STheodore Ts'o 		goto out3;
6693857ac889SLukas Czerner 
66945dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6695ac27a0ecSDave Kleikamp 	if (err)
6696c9de560dSAlex Tomas 		goto out2;
6697ac27a0ecSDave Kleikamp 	err = init_inodecache();
6698ac27a0ecSDave Kleikamp 	if (err)
6699ac27a0ecSDave Kleikamp 		goto out1;
6700aa75f4d3SHarshad Shirwadkar 
6701aa75f4d3SHarshad Shirwadkar 	err = ext4_fc_init_dentry_cache();
6702aa75f4d3SHarshad Shirwadkar 	if (err)
6703aa75f4d3SHarshad Shirwadkar 		goto out05;
6704aa75f4d3SHarshad Shirwadkar 
670524b58424STheodore Ts'o 	register_as_ext3();
67062035e776STheodore Ts'o 	register_as_ext2();
670703010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6708ac27a0ecSDave Kleikamp 	if (err)
6709ac27a0ecSDave Kleikamp 		goto out;
6710bfff6873SLukas Czerner 
6711ac27a0ecSDave Kleikamp 	return 0;
6712ac27a0ecSDave Kleikamp out:
671324b58424STheodore Ts'o 	unregister_as_ext2();
671424b58424STheodore Ts'o 	unregister_as_ext3();
6715aa75f4d3SHarshad Shirwadkar out05:
6716ac27a0ecSDave Kleikamp 	destroy_inodecache();
6717ac27a0ecSDave Kleikamp out1:
67185dabfc78STheodore Ts'o 	ext4_exit_mballoc();
67199c191f70ST Makphaibulchoke out2:
6720b5799018STheodore Ts'o 	ext4_exit_sysfs();
6721b5799018STheodore Ts'o out3:
6722dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6723b5799018STheodore Ts'o out4:
67245dabfc78STheodore Ts'o 	ext4_exit_pageio();
6725b5799018STheodore Ts'o out5:
672622cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
67271dc0aa46SEric Whitney out6:
672822cfe4b4SEric Biggers 	ext4_exit_pending();
672922cfe4b4SEric Biggers out7:
673051865fdaSZheng Liu 	ext4_exit_es();
673151865fdaSZheng Liu 
6732ac27a0ecSDave Kleikamp 	return err;
6733ac27a0ecSDave Kleikamp }
6734ac27a0ecSDave Kleikamp 
67355dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6736ac27a0ecSDave Kleikamp {
6737bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
673824b58424STheodore Ts'o 	unregister_as_ext2();
673924b58424STheodore Ts'o 	unregister_as_ext3();
674003010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6741ac27a0ecSDave Kleikamp 	destroy_inodecache();
67425dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6743b5799018STheodore Ts'o 	ext4_exit_sysfs();
67445dabfc78STheodore Ts'o 	ext4_exit_system_zone();
67455dabfc78STheodore Ts'o 	ext4_exit_pageio();
674622cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6747dd12ed14SEric Sandeen 	ext4_exit_es();
67481dc0aa46SEric Whitney 	ext4_exit_pending();
6749ac27a0ecSDave Kleikamp }
6750ac27a0ecSDave Kleikamp 
6751ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
675283982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6753ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
67547ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
67555dabfc78STheodore Ts'o module_init(ext4_init_fs)
67565dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6757