xref: /linux/fs/ext4/super.c (revision f8f4acb6cded4e455b2d390ce2221391fc3f09ee)
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
1291*f8f4acb6SDaniel 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},
203026092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
203126092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
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)
3979810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
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
4306*f8f4acb6SDaniel 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 
4337*f8f4acb6SDaniel Rosenberg 		sb->s_encoding = encoding;
4338*f8f4acb6SDaniel 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) {
4343244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
4344781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4345244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
434656889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
434756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
434856889787STheodore Ts'o 				 "both data=journal and delalloc");
434956889787STheodore Ts'o 			goto failed_mount;
435056889787STheodore Ts'o 		}
4351fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4352923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4353923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4354923ae0ffSRoss Zwisler 			goto failed_mount;
4355923ae0ffSRoss Zwisler 		}
435673b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
435773b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
435873b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
435973b92a2aSSergey Karamov 				 "instead of data journaling mode");
436073b92a2aSSergey Karamov 		}
436156889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
436256889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4363001e4a87STejun Heo 	} else {
4364001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
436556889787STheodore Ts'o 	}
436656889787STheodore Ts'o 
43671751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
43681751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4369ac27a0ecSDave Kleikamp 
4370617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4371e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4372e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4373e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4374b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4375b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4376b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4377469108ffSTheodore Tso 
4378ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4379ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4380e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4381ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4382ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4383ed3654ebSTheodore Ts'o 			goto failed_mount;
4384ed3654ebSTheodore Ts'o 		}
4385dec214d0STahsin Erdogan 
4386dec214d0STahsin Erdogan 		/*
4387dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4388dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4389dec214d0STahsin Erdogan 		 */
4390dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4391dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4392dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4393dec214d0STahsin Erdogan 			goto failed_mount;
4394dec214d0STahsin Erdogan 		}
4395ed3654ebSTheodore Ts'o 	}
4396ed3654ebSTheodore Ts'o 
43972035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
43982035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
43992035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
44002035e776STheodore Ts'o 				 "using the ext4 subsystem");
44012035e776STheodore Ts'o 		else {
44020d9366d6SEric Sandeen 			/*
44030d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
44040d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
44050d9366d6SEric Sandeen 			 */
44060d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
44070d9366d6SEric Sandeen 				goto failed_mount;
44082035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
44092035e776STheodore Ts'o 				 "to feature incompatibilities");
44102035e776STheodore Ts'o 			goto failed_mount;
44112035e776STheodore Ts'o 		}
44122035e776STheodore Ts'o 	}
44132035e776STheodore Ts'o 
44142035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
44152035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
44162035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
44172035e776STheodore Ts'o 				 "using the ext4 subsystem");
44182035e776STheodore Ts'o 		else {
44190d9366d6SEric Sandeen 			/*
44200d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
44210d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
44220d9366d6SEric Sandeen 			 */
44230d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
44240d9366d6SEric Sandeen 				goto failed_mount;
44252035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
44262035e776STheodore Ts'o 				 "to feature incompatibilities");
44272035e776STheodore Ts'o 			goto failed_mount;
44282035e776STheodore Ts'o 		}
44292035e776STheodore Ts'o 	}
44302035e776STheodore Ts'o 
4431469108ffSTheodore Tso 	/*
4432ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4433ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4434ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4435ac27a0ecSDave Kleikamp 	 */
4436bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4437ac27a0ecSDave Kleikamp 		goto failed_mount;
4438a13fb1a4SEric Sandeen 
44398cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
44408cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
44418cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44428cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
44438cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4444ac27a0ecSDave Kleikamp 		goto failed_mount;
4445ac27a0ecSDave Kleikamp 	}
4446bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4447bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4448bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4449bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4450bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4451bfe0a5f4STheodore Ts'o 		goto failed_mount;
4452bfe0a5f4STheodore Ts'o 	}
4453ac27a0ecSDave Kleikamp 
44545b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
44555b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44565b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
44575b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
44585b9554dcSTheodore Ts'o 		goto failed_mount;
44595b9554dcSTheodore Ts'o 	}
44605b9554dcSTheodore Ts'o 
4461a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4462a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4463a8ab6d38SIra Weiny 
4464fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4465559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4466559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4467559db4c6SRoss Zwisler 					" that may contain inline data");
4468361d24d4SEric Sandeen 			goto failed_mount;
4469559db4c6SRoss Zwisler 		}
4470a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
447124f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4472361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4473361d24d4SEric Sandeen 			goto failed_mount;
447424f3478dSDan Williams 		}
4475923ae0ffSRoss Zwisler 	}
4476923ae0ffSRoss Zwisler 
4477e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
44786ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
44796ddb2447STheodore Ts'o 			 es->s_encryption_level);
44806ddb2447STheodore Ts'o 		goto failed_mount;
44816ddb2447STheodore Ts'o 	}
44826ddb2447STheodore Ts'o 
4483ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4484ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4485ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4486b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4487ce40733cSAneesh Kumar K.V 					blocksize);
4488ac27a0ecSDave Kleikamp 			goto failed_mount;
4489ac27a0ecSDave Kleikamp 		}
4490ac27a0ecSDave Kleikamp 
4491ac27a0ecSDave Kleikamp 		brelse(bh);
449270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
449370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
44948394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
44958394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4496b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4497b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
44988394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
44998394a6abSzhangyi (F) 			bh = NULL;
4500ac27a0ecSDave Kleikamp 			goto failed_mount;
4501ac27a0ecSDave Kleikamp 		}
45022716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4503ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4504617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4505b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4506b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4507ac27a0ecSDave Kleikamp 			goto failed_mount;
4508ac27a0ecSDave Kleikamp 		}
4509ac27a0ecSDave Kleikamp 	}
4510ac27a0ecSDave Kleikamp 
4511e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4512f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4513f287a1a5STheodore Ts'o 						      has_huge_files);
4514f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4515ac27a0ecSDave Kleikamp 
45160d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4517e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
45188fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
45190d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4520d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4521b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4522b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
45230d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
45240d1ee42fSAlexandre Ratchov 			goto failed_mount;
45250d1ee42fSAlexandre Ratchov 		}
45260d1ee42fSAlexandre Ratchov 	} else
45270d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
45280b8e58a1SAndreas Dilger 
4529ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4530ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
45310b8e58a1SAndreas Dilger 
4532617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4533ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4534617ba13bSMingming Cao 		goto cantfind_ext4;
4535cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4536cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4537cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4538b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4539cd6bb35bSTheodore Ts'o 		goto failed_mount;
4540cd6bb35bSTheodore Ts'o 	}
4541ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4542ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
45430d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4544ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4545ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4546e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4547e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
45480b8e58a1SAndreas Dilger 
4549ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4550ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4551ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4552e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4553f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4554f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4555f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4556f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4557f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4558bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
455923301410STheodore Ts'o 				es->s_flags |=
456023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4561f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4562f99b2589STheodore Ts'o #else
4563bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
456423301410STheodore Ts'o 				es->s_flags |=
456523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4566f99b2589STheodore Ts'o #endif
4567f99b2589STheodore Ts'o 		}
456823301410STheodore Ts'o 	}
4569ac27a0ecSDave Kleikamp 
4570281b5995STheodore Ts'o 	/* Handle clustersize */
4571281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4572ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4573281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4574281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4575281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4576281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4577281b5995STheodore Ts'o 			goto failed_mount;
4578281b5995STheodore Ts'o 		}
4579281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4580281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4581281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4582281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4583281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4584281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4585281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4586281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4587281b5995STheodore Ts'o 			goto failed_mount;
4588281b5995STheodore Ts'o 		}
4589281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4590281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4591281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4592281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4593281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4594281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4595281b5995STheodore Ts'o 			goto failed_mount;
4596281b5995STheodore Ts'o 		}
4597281b5995STheodore Ts'o 	} else {
4598281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4599bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4600bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4601bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4602bfe0a5f4STheodore Ts'o 			goto failed_mount;
4603281b5995STheodore Ts'o 		}
4604ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4605b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4606b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4607ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4608ac27a0ecSDave Kleikamp 			goto failed_mount;
4609ac27a0ecSDave Kleikamp 		}
4610281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4611281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4612281b5995STheodore Ts'o 	}
4613281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4614281b5995STheodore Ts'o 
4615960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4616960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4617960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4618960fd856STheodore Ts'o 
4619bf43d84bSEric Sandeen 	/*
4620bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4621bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4622bf43d84bSEric Sandeen 	 */
46235a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
462430ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
46255a9ae68aSDarrick J. Wong 	if (err) {
4626b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4627bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4628ac27a0ecSDave Kleikamp 		goto failed_mount;
4629ac27a0ecSDave Kleikamp 	}
4630ac27a0ecSDave Kleikamp 
4631617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4632617ba13bSMingming Cao 		goto cantfind_ext4;
4633e7c95593SEric Sandeen 
46340f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
46350f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
46360f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4637b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4638b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
46390f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
46400f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
46410f2ddca6SFrom: Thiemo Nagel 	}
46420f2ddca6SFrom: Thiemo Nagel 
46434ec11028STheodore Ts'o 	/*
46444ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
46454ec11028STheodore Ts'o 	 * of the filesystem.
46464ec11028STheodore Ts'o 	 */
46474ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4648b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4649b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4650e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
46514ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4652e7c95593SEric Sandeen 		goto failed_mount;
4653e7c95593SEric Sandeen 	}
4654bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4655bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4656bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4657bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4658bfe0a5f4STheodore Ts'o 		goto failed_mount;
4659bfe0a5f4STheodore Ts'o 	}
4660bfe0a5f4STheodore Ts'o 
4661bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4662bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4663bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4664bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
46654ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4666df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
46674ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4668df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
46694ec11028STheodore Ts'o 		       ext4_blocks_count(es),
46704ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
46714ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
46724ec11028STheodore Ts'o 		goto failed_mount;
46734ec11028STheodore Ts'o 	}
4674bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4675fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4676fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
46779e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
46789e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
46799e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
46809e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
46819e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
46829e463084STheodore Ts'o 		ret = -EINVAL;
46839e463084STheodore Ts'o 		goto failed_mount;
46849e463084STheodore Ts'o 	}
4685617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4686617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
46873a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
46882ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
46893a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
46903a4b77cdSEryu Guan 				 "first meta block group too large: %u "
46913a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
46923a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
46933a4b77cdSEryu Guan 			goto failed_mount;
46943a4b77cdSEryu Guan 		}
46953a4b77cdSEryu Guan 	}
46961d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
46971d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4698f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
46991d0c3924STheodore Ts'o 					  GFP_KERNEL));
4700ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4701b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
47022cde417dSTheodore Ts'o 		ret = -ENOMEM;
4703ac27a0ecSDave Kleikamp 		goto failed_mount;
4704ac27a0ecSDave Kleikamp 	}
4705ac27a0ecSDave Kleikamp 
4706705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4707ac27a0ecSDave Kleikamp 
470885c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
470985c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
471085c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
47115df1d412Szhangyi (F) 		ext4_sb_breadahead_unmovable(sb, block);
471285c8f176SAndrew Perepechko 	}
471385c8f176SAndrew Perepechko 
4714ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
47151d0c3924STheodore Ts'o 		struct buffer_head *bh;
47161d0c3924STheodore Ts'o 
471770bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
47188394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, block);
47198394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4720b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4721b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4722ac27a0ecSDave Kleikamp 			db_count = i;
47238394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
47248394a6abSzhangyi (F) 			bh = NULL;
4725ac27a0ecSDave Kleikamp 			goto failed_mount2;
4726ac27a0ecSDave Kleikamp 		}
47271d0c3924STheodore Ts'o 		rcu_read_lock();
47281d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
47291d0c3924STheodore Ts'o 		rcu_read_unlock();
4730ac27a0ecSDave Kleikamp 	}
473144de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4732829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4733b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
47346a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4735f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4736ac27a0ecSDave Kleikamp 	}
4737772cb7c8SJose R. Santos 
4738235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
473904496411STao Ma 
4740a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4741eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4742ce7e010aSTheodore Ts'o 		goto failed_mount3;
4743ce7e010aSTheodore Ts'o 
4744c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
474567a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4746c9de560dSAlex Tomas 
4747f9ae9cf5STheodore Ts'o 	/*
4748f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4749f9ae9cf5STheodore Ts'o 	 */
4750f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4751617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4752617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4753643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4754a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4755ffcc4182SEric Biggers #endif
4756c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4757c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4758c93d8f88SEric Biggers #endif
4759ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4760617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4761e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
47621fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4763262b4662SJan Kara 	else
4764262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4765689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4766ac27a0ecSDave Kleikamp #endif
476785787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4768f2fa2ffcSAneesh Kumar K.V 
4769ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
47703b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4771ac27a0ecSDave Kleikamp 
4772aa75f4d3SHarshad Shirwadkar 	/* Initialize fast commit stuff */
4773aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_subtid, 0);
4774aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_ineligible_updates, 0);
4775aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4776aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4777aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4778aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4779aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bytes = 0;
4780ababea77SHarshad Shirwadkar 	sbi->s_mount_flags &= ~EXT4_MF_FC_INELIGIBLE;
4781ababea77SHarshad Shirwadkar 	sbi->s_mount_flags &= ~EXT4_MF_FC_COMMITTING;
4782aa75f4d3SHarshad Shirwadkar 	spin_lock_init(&sbi->s_fc_lock);
4783aa75f4d3SHarshad Shirwadkar 	memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
47848016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions = NULL;
47858016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_size = 0;
47868016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_used = 0;
47878016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_valid = 0;
47888016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes = NULL;
47898016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
47908016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4791aa75f4d3SHarshad Shirwadkar 
4792ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4793ac27a0ecSDave Kleikamp 
4794ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4795e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4796ac27a0ecSDave Kleikamp 
4797bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4798c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
479950460fe8SDarrick J. Wong 			goto failed_mount3a;
4800c5e06d10SJohann Lombardi 
4801ac27a0ecSDave Kleikamp 	/*
4802ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4803ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4804ac27a0ecSDave Kleikamp 	 */
4805e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
48064753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
48074753d8a2STheodore Ts'o 		if (err)
480850460fe8SDarrick J. Wong 			goto failed_mount3a;
4809bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4810e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4811b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4812b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4813744692dcSJiaying Zhang 		goto failed_mount_wq;
4814ac27a0ecSDave Kleikamp 	} else {
48151e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
48161e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
48171e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48181e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
48191e381f60SDmitry Monakhov 			goto failed_mount_wq;
48201e381f60SDmitry Monakhov 		}
48211e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
48221e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48231e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
48241e381f60SDmitry Monakhov 			goto failed_mount_wq;
48251e381f60SDmitry Monakhov 		}
48261e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
48271e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48281e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
48291e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
48301e381f60SDmitry Monakhov 			goto failed_mount_wq;
48311e381f60SDmitry Monakhov 		}
48321e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
48331e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
48341e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48351e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
48361e381f60SDmitry Monakhov 			goto failed_mount_wq;
48371e381f60SDmitry Monakhov 		}
483850b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
48391e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4840fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
4841995a3ed6SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
48420390131bSFrank Mayhar 		sbi->s_journal = NULL;
48430390131bSFrank Mayhar 		needs_recovery = 0;
48440390131bSFrank Mayhar 		goto no_journal;
4845ac27a0ecSDave Kleikamp 	}
4846ac27a0ecSDave Kleikamp 
4847e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4848eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4849eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4850b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4851744692dcSJiaying Zhang 		goto failed_mount_wq;
4852eb40a09cSJose R. Santos 	}
4853eb40a09cSJose R. Santos 
485425ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
485525ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
485625ed6e8aSDarrick J. Wong 			 "feature set");
485725ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4858d4da6c9cSLinus Torvalds 	}
4859818d276cSGirish Shilamkar 
4860ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4861ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4862ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4863ac27a0ecSDave Kleikamp 	case 0:
4864ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
486563f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
486663f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
486763f57933SAndrew Morton 		 */
4868dab291afSMingming Cao 		if (jbd2_journal_check_available_features
486927f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4870fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
487127f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
487227f394a7STyson Nottingham 		} else {
4873fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
487427f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
487527f394a7STyson Nottingham 		}
4876ac27a0ecSDave Kleikamp 		break;
4877ac27a0ecSDave Kleikamp 
4878617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4879617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4880dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4881dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4882b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4883b31e1552SEric Sandeen 			       "requested data journaling mode");
4884744692dcSJiaying Zhang 			goto failed_mount_wq;
4885ac27a0ecSDave Kleikamp 		}
4886ac27a0ecSDave Kleikamp 	default:
4887ac27a0ecSDave Kleikamp 		break;
4888ac27a0ecSDave Kleikamp 	}
4889ab04df78SJan Kara 
4890ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4891ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4892ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4893ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4894ab04df78SJan Kara 		goto failed_mount_wq;
4895ab04df78SJan Kara 	}
4896ab04df78SJan Kara 
4897b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4898ac27a0ecSDave Kleikamp 
489918aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
4900342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_submit_inode_data_buffers =
4901afb585a9SMauricio Faria de Oliveira 		ext4_journal_submit_inode_data_buffers;
4902342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_finish_inode_data_buffers =
4903afb585a9SMauricio Faria de Oliveira 		ext4_journal_finish_inode_data_buffers;
490418aadd47SBobi Jam 
4905ce7e010aSTheodore Ts'o no_journal:
4906cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
490747387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
490847387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4909cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4910cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
49119c191f70ST Makphaibulchoke 			goto failed_mount_wq;
49129c191f70ST Makphaibulchoke 		}
49139c191f70ST Makphaibulchoke 
4914dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4915dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4916dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4917dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4918dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4919dec214d0STahsin Erdogan 				goto failed_mount_wq;
4920dec214d0STahsin Erdogan 			}
4921dec214d0STahsin Erdogan 		}
4922cdb7ee4cSTahsin Erdogan 	}
4923dec214d0STahsin Erdogan 
4924c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4925c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4926c93d8f88SEric Biggers 		goto failed_mount_wq;
4927c93d8f88SEric Biggers 	}
4928c93d8f88SEric Biggers 
4929bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4930e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4931e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
49326ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
49336ddb2447STheodore Ts'o 	}
49346ddb2447STheodore Ts'o 
4935fd89d5f2STejun Heo 	/*
4936952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4937952fc18eSTheodore Ts'o 	 * superblock if present.
4938952fc18eSTheodore Ts'o 	 */
4939952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4940952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4941952fc18eSTheodore Ts'o 	else {
494207aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
494307aa2ea1SLukas Czerner 		if (err)
4944952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4945952fc18eSTheodore Ts'o 	}
4946952fc18eSTheodore Ts'o 
4947952fc18eSTheodore Ts'o 	/*
4948fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4949fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4950fd89d5f2STejun Heo 	 */
49512e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
49522e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
49532e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
49542e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
495507aa2ea1SLukas Czerner 		ret = -ENOMEM;
49562e8fa54eSJan Kara 		goto failed_mount4;
49572e8fa54eSJan Kara 	}
49582e8fa54eSJan Kara 
4959ac27a0ecSDave Kleikamp 	/*
4960dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4961ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4962ac27a0ecSDave Kleikamp 	 */
4963ac27a0ecSDave Kleikamp 
49648a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
49651d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4966b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
49671d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
496832a9bb57SManish Katiyar 		root = NULL;
4969ac27a0ecSDave Kleikamp 		goto failed_mount4;
4970ac27a0ecSDave Kleikamp 	}
4971ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4972b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
497394bf608aSAl Viro 		iput(root);
4974ac27a0ecSDave Kleikamp 		goto failed_mount4;
4975ac27a0ecSDave Kleikamp 	}
4976b886ee3eSGabriel Krisman Bertazi 
4977b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4978*f8f4acb6SDaniel Rosenberg 	if (sb->s_encoding)
4979b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4980b886ee3eSGabriel Krisman Bertazi #endif
4981b886ee3eSGabriel Krisman Bertazi 
498248fde701SAl Viro 	sb->s_root = d_make_root(root);
49831d1fe1eeSDavid Howells 	if (!sb->s_root) {
4984b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
49851d1fe1eeSDavid Howells 		ret = -ENOMEM;
49861d1fe1eeSDavid Howells 		goto failed_mount4;
49871d1fe1eeSDavid Howells 	}
4988ac27a0ecSDave Kleikamp 
4989c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4990c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
49911751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4992c89128a0SJaegeuk Kim 		ret = 0;
4993c89128a0SJaegeuk Kim 	} else if (ret)
4994c89128a0SJaegeuk Kim 		goto failed_mount4a;
4995ef7f3835SKalpak Shah 
4996b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
499727dd4385SLukas Czerner 
49980f5bde1dSJan Kara 	if (test_opt(sb, BLOCK_VALIDITY)) {
49996fd058f7STheodore Ts'o 		err = ext4_setup_system_zone(sb);
50006fd058f7STheodore Ts'o 		if (err) {
5001b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
5002fbe845ddSCurt Wohlgemuth 				 "zone (%d)", err);
5003f9ae9cf5STheodore Ts'o 			goto failed_mount4a;
5004f9ae9cf5STheodore Ts'o 		}
50050f5bde1dSJan Kara 	}
50068016e29fSHarshad Shirwadkar 	ext4_fc_replay_cleanup(sb);
5007f9ae9cf5STheodore Ts'o 
5008f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
5009f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
5010f9ae9cf5STheodore Ts'o 	if (err) {
5011f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5012f9ae9cf5STheodore Ts'o 			 err);
5013dcf2d804STao Ma 		goto failed_mount5;
5014c2774d84SAneesh Kumar K.V 	}
5015c2774d84SAneesh Kumar K.V 
5016d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
5017d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
5018d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
50194274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
5020908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
5021908c7f19STejun Heo 				  GFP_KERNEL);
5022d5e03cbbSTheodore Ts'o 	if (!err) {
5023d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
5024d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
50254274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
5026908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
5027908c7f19STejun Heo 					  GFP_KERNEL);
5028d5e03cbbSTheodore Ts'o 	}
5029d5e03cbbSTheodore Ts'o 	if (!err)
5030d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
5031908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
5032d5e03cbbSTheodore Ts'o 	if (!err)
5033908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
5034908c7f19STejun Heo 					  GFP_KERNEL);
5035c8585c6fSDaeho Jeong 	if (!err)
5036bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
5037c8585c6fSDaeho Jeong 
5038d5e03cbbSTheodore Ts'o 	if (err) {
5039d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
5040d5e03cbbSTheodore Ts'o 		goto failed_mount6;
5041d5e03cbbSTheodore Ts'o 	}
5042d5e03cbbSTheodore Ts'o 
5043e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
5044d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
5045d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
5046d5e03cbbSTheodore Ts'o 			       "unable to initialize "
5047d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
5048d5e03cbbSTheodore Ts'o 			goto failed_mount6;
5049d5e03cbbSTheodore Ts'o 		}
5050d5e03cbbSTheodore Ts'o 
5051bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
5052bfff6873SLukas Czerner 	if (err)
5053dcf2d804STao Ma 		goto failed_mount6;
5054bfff6873SLukas Czerner 
5055b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
5056dcf2d804STao Ma 	if (err)
5057dcf2d804STao Ma 		goto failed_mount7;
50583197ebdbSTheodore Ts'o 
50599b2ff357SJan Kara #ifdef CONFIG_QUOTA
50609b2ff357SJan Kara 	/* Enable quota usage during mount. */
5061bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
50629b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
50639b2ff357SJan Kara 		if (err)
50649b2ff357SJan Kara 			goto failed_mount8;
50659b2ff357SJan Kara 	}
50669b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
50679b2ff357SJan Kara 
5068bc71726cSzhangyi (F) 	/*
5069bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
5070bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
5071bc71726cSzhangyi (F) 	 */
5072bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
5073bc71726cSzhangyi (F) 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5074bc71726cSzhangyi (F) 				 &sbi->s_bdev_wb_err);
5075bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
5076617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5077617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
5078617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
50790390131bSFrank Mayhar 	if (needs_recovery) {
5080b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
508111215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
508211215630SJan Kara 		if (err)
508311215630SJan Kara 			goto failed_mount8;
50840390131bSFrank Mayhar 	}
50850390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
50860390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
50870390131bSFrank Mayhar 			descr = " journalled data mode";
50880390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
50890390131bSFrank Mayhar 			descr = " ordered data mode";
50900390131bSFrank Mayhar 		else
50910390131bSFrank Mayhar 			descr = " writeback data mode";
50920390131bSFrank Mayhar 	} else
50930390131bSFrank Mayhar 		descr = "out journal";
50940390131bSFrank Mayhar 
509579add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
509679add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
509779add3a3SLukas Czerner 		if (!blk_queue_discard(q))
509879add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
509979add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
510079add3a3SLukas Czerner 				 "the device does not support discard");
510179add3a3SLukas Czerner 	}
510279add3a3SLukas Czerner 
5103e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
5104d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
51055aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
51065aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
51075aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
51088b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
5109ac27a0ecSDave Kleikamp 
511066e61a9eSTheodore Ts'o 	if (es->s_error_count)
511166e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5112ac27a0ecSDave Kleikamp 
5113efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5114efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5115efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5116efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
51171cf006edSDmitry Monakhov 	atomic_set(&sbi->s_warning_count, 0);
51181cf006edSDmitry Monakhov 	atomic_set(&sbi->s_msg_count, 0);
5119efbed4dcSTheodore Ts'o 
5120d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5121ac27a0ecSDave Kleikamp 	return 0;
5122ac27a0ecSDave Kleikamp 
5123617ba13bSMingming Cao cantfind_ext4:
5124ac27a0ecSDave Kleikamp 	if (!silent)
5125b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5126ac27a0ecSDave Kleikamp 	goto failed_mount;
5127ac27a0ecSDave Kleikamp 
512872ba7450STheodore Ts'o failed_mount8:
5129ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
5130cb8d53d2SEric Biggers 	kobject_put(&sbi->s_kobj);
5131dcf2d804STao Ma failed_mount7:
5132dcf2d804STao Ma 	ext4_unregister_li_request(sb);
5133dcf2d804STao Ma failed_mount6:
5134f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
51357c990728SSuraj Jitindar Singh 	rcu_read_lock();
51367c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
51377c990728SSuraj Jitindar Singh 	if (flex_groups) {
51387c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
51397c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
51407c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
51417c990728SSuraj Jitindar Singh 	}
51427c990728SSuraj Jitindar Singh 	rcu_read_unlock();
5143d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
5144d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
5145d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
5146d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5147bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
514800764937SAzat Khuzhin failed_mount5:
5149f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
5150f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
5151f9ae9cf5STheodore Ts'o failed_mount4a:
515294bf608aSAl Viro 	dput(sb->s_root);
515332a9bb57SManish Katiyar 	sb->s_root = NULL;
515494bf608aSAl Viro failed_mount4:
5155b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
51562e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
51572e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
51584c0425ffSMingming Cao failed_mount_wq:
5159dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5160dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
516150c15df6SChengguang Xu 
516247387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
516347387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
516450c15df6SChengguang Xu 
51650390131bSFrank Mayhar 	if (sbi->s_journal) {
5166dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
516747b4a50bSJan Kara 		sbi->s_journal = NULL;
51680390131bSFrank Mayhar 	}
516950460fe8SDarrick J. Wong failed_mount3a:
5170d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
5171eb68d0e2SZheng Liu failed_mount3:
51729105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
5173c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
5174c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
5175ac27a0ecSDave Kleikamp failed_mount2:
51761d0c3924STheodore Ts'o 	rcu_read_lock();
51771d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
5178ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
51791d0c3924STheodore Ts'o 		brelse(group_desc[i]);
51801d0c3924STheodore Ts'o 	kvfree(group_desc);
51811d0c3924STheodore Ts'o 	rcu_read_unlock();
5182ac27a0ecSDave Kleikamp failed_mount:
51830441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
51840441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
5185c83ad55eSGabriel Krisman Bertazi 
5186c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
5187*f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
5188c83ad55eSGabriel Krisman Bertazi #endif
5189c83ad55eSGabriel Krisman Bertazi 
5190ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5191a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
51920ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
5193ac27a0ecSDave Kleikamp #endif
5194ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5195617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
5196ac27a0ecSDave Kleikamp 	brelse(bh);
5197ac27a0ecSDave Kleikamp out_fail:
5198ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
5199f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
52005aee0f8aSTheodore Ts'o out_free_base:
5201ac27a0ecSDave Kleikamp 	kfree(sbi);
5202d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
52035e405595SDan Williams 	fs_put_dax(dax_dev);
520407aa2ea1SLukas Czerner 	return err ? err : ret;
5205ac27a0ecSDave Kleikamp }
5206ac27a0ecSDave Kleikamp 
5207ac27a0ecSDave Kleikamp /*
5208ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
5209ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
5210ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
5211ac27a0ecSDave Kleikamp  */
5212617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5213ac27a0ecSDave Kleikamp {
5214617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5215ac27a0ecSDave Kleikamp 
5216ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
521730773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
521830773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
52196866d7b3SHarshad Shirwadkar 	ext4_fc_init(sb, journal);
5220ac27a0ecSDave Kleikamp 
5221a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
5222ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
5223dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
5224ac27a0ecSDave Kleikamp 	else
5225dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
52265bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
52275bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
52285bf5683aSHidehiro Kawai 	else
52295bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5230a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
5231ac27a0ecSDave Kleikamp }
5232ac27a0ecSDave Kleikamp 
5233c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
5234ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
5235ac27a0ecSDave Kleikamp {
5236ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
5237ac27a0ecSDave Kleikamp 
5238c6cb7e77SEric Whitney 	/*
5239c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
5240c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
5241c6cb7e77SEric Whitney 	 * will try to delete it.
5242c6cb7e77SEric Whitney 	 */
52438a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
52441d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
5245b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
5246ac27a0ecSDave Kleikamp 		return NULL;
5247ac27a0ecSDave Kleikamp 	}
5248ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
5249ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
5250ac27a0ecSDave Kleikamp 		iput(journal_inode);
5251b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5252ac27a0ecSDave Kleikamp 		return NULL;
5253ac27a0ecSDave Kleikamp 	}
5254ac27a0ecSDave Kleikamp 
5255e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5256ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
52571d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
5258b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5259ac27a0ecSDave Kleikamp 		iput(journal_inode);
5260ac27a0ecSDave Kleikamp 		return NULL;
5261ac27a0ecSDave Kleikamp 	}
5262c6cb7e77SEric Whitney 	return journal_inode;
5263c6cb7e77SEric Whitney }
5264c6cb7e77SEric Whitney 
5265c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
5266c6cb7e77SEric Whitney 				   unsigned int journal_inum)
5267c6cb7e77SEric Whitney {
5268c6cb7e77SEric Whitney 	struct inode *journal_inode;
5269c6cb7e77SEric Whitney 	journal_t *journal;
5270c6cb7e77SEric Whitney 
527111215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
527211215630SJan Kara 		return NULL;
5273c6cb7e77SEric Whitney 
5274c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5275c6cb7e77SEric Whitney 	if (!journal_inode)
5276c6cb7e77SEric Whitney 		return NULL;
5277ac27a0ecSDave Kleikamp 
5278dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
5279ac27a0ecSDave Kleikamp 	if (!journal) {
5280b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5281ac27a0ecSDave Kleikamp 		iput(journal_inode);
5282ac27a0ecSDave Kleikamp 		return NULL;
5283ac27a0ecSDave Kleikamp 	}
5284ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5285617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5286ac27a0ecSDave Kleikamp 	return journal;
5287ac27a0ecSDave Kleikamp }
5288ac27a0ecSDave Kleikamp 
5289617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
5290ac27a0ecSDave Kleikamp 				       dev_t j_dev)
5291ac27a0ecSDave Kleikamp {
5292ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5293ac27a0ecSDave Kleikamp 	journal_t *journal;
5294617ba13bSMingming Cao 	ext4_fsblk_t start;
5295617ba13bSMingming Cao 	ext4_fsblk_t len;
5296ac27a0ecSDave Kleikamp 	int hblock, blocksize;
5297617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
5298ac27a0ecSDave Kleikamp 	unsigned long offset;
5299617ba13bSMingming Cao 	struct ext4_super_block *es;
5300ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5301ac27a0ecSDave Kleikamp 
530211215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
530311215630SJan Kara 		return NULL;
53040390131bSFrank Mayhar 
5305b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5306ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5307ac27a0ecSDave Kleikamp 		return NULL;
5308ac27a0ecSDave Kleikamp 
5309ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5310e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5311ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5312b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5313b31e1552SEric Sandeen 			"blocksize too small for journal device");
5314ac27a0ecSDave Kleikamp 		goto out_bdev;
5315ac27a0ecSDave Kleikamp 	}
5316ac27a0ecSDave Kleikamp 
5317617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5318617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5319ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5320ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5321b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5322b31e1552SEric Sandeen 		       "external journal");
5323ac27a0ecSDave Kleikamp 		goto out_bdev;
5324ac27a0ecSDave Kleikamp 	}
5325ac27a0ecSDave Kleikamp 
53262716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5327617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5328ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5329617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5330b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5331b31e1552SEric Sandeen 					"bad superblock");
5332ac27a0ecSDave Kleikamp 		brelse(bh);
5333ac27a0ecSDave Kleikamp 		goto out_bdev;
5334ac27a0ecSDave Kleikamp 	}
5335ac27a0ecSDave Kleikamp 
5336df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5337df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5338df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5339df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5340df4763beSDarrick J. Wong 				       "corrupt superblock");
5341df4763beSDarrick J. Wong 		brelse(bh);
5342df4763beSDarrick J. Wong 		goto out_bdev;
5343df4763beSDarrick J. Wong 	}
5344df4763beSDarrick J. Wong 
5345617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5346b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5347ac27a0ecSDave Kleikamp 		brelse(bh);
5348ac27a0ecSDave Kleikamp 		goto out_bdev;
5349ac27a0ecSDave Kleikamp 	}
5350ac27a0ecSDave Kleikamp 
5351bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5352ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5353ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5354ac27a0ecSDave Kleikamp 
5355dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5356ac27a0ecSDave Kleikamp 					start, len, blocksize);
5357ac27a0ecSDave Kleikamp 	if (!journal) {
5358b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5359ac27a0ecSDave Kleikamp 		goto out_bdev;
5360ac27a0ecSDave Kleikamp 	}
5361ac27a0ecSDave Kleikamp 	journal->j_private = sb;
53622d069c08Szhangyi (F) 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5363b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5364ac27a0ecSDave Kleikamp 		goto out_journal;
5365ac27a0ecSDave Kleikamp 	}
5366ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5367b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5368b31e1552SEric Sandeen 					"user (unsupported) - %d",
5369ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5370ac27a0ecSDave Kleikamp 		goto out_journal;
5371ac27a0ecSDave Kleikamp 	}
5372ee7ed3aaSChunguang Xu 	EXT4_SB(sb)->s_journal_bdev = bdev;
5373617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5374ac27a0ecSDave Kleikamp 	return journal;
53750b8e58a1SAndreas Dilger 
5376ac27a0ecSDave Kleikamp out_journal:
5377dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5378ac27a0ecSDave Kleikamp out_bdev:
5379617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5380ac27a0ecSDave Kleikamp 	return NULL;
5381ac27a0ecSDave Kleikamp }
5382ac27a0ecSDave Kleikamp 
5383617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5384617ba13bSMingming Cao 			     struct ext4_super_block *es,
5385ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5386ac27a0ecSDave Kleikamp {
5387ac27a0ecSDave Kleikamp 	journal_t *journal;
5388ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5389ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5390ac27a0ecSDave Kleikamp 	int err = 0;
5391ac27a0ecSDave Kleikamp 	int really_read_only;
5392273108faSLukas Czerner 	int journal_dev_ro;
5393ac27a0ecSDave Kleikamp 
539411215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
539511215630SJan Kara 		return -EFSCORRUPTED;
53960390131bSFrank Mayhar 
5397ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5398ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5399b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5400b31e1552SEric Sandeen 			"numbers have changed");
5401ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5402ac27a0ecSDave Kleikamp 	} else
5403ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5404ac27a0ecSDave Kleikamp 
5405273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5406273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5407273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5408273108faSLukas Czerner 		return -EINVAL;
5409273108faSLukas Czerner 	}
5410273108faSLukas Czerner 
5411273108faSLukas Czerner 	if (journal_inum) {
5412273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5413273108faSLukas Czerner 		if (!journal)
5414273108faSLukas Czerner 			return -EINVAL;
5415273108faSLukas Czerner 	} else {
5416273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5417273108faSLukas Czerner 		if (!journal)
5418273108faSLukas Czerner 			return -EINVAL;
5419273108faSLukas Czerner 	}
5420273108faSLukas Czerner 
5421273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5422273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5423273108faSLukas Czerner 
5424273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5425273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5426273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5427273108faSLukas Czerner 		err = -EROFS;
5428273108faSLukas Czerner 		goto err_out;
5429273108faSLukas Czerner 	}
5430ac27a0ecSDave Kleikamp 
5431ac27a0ecSDave Kleikamp 	/*
5432ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5433ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5434ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5435ac27a0ecSDave Kleikamp 	 */
5436e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5437bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5438b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5439b31e1552SEric Sandeen 					"required on readonly filesystem");
5440ac27a0ecSDave Kleikamp 			if (really_read_only) {
5441b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5442d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5443d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5444273108faSLukas Czerner 				err = -EROFS;
5445273108faSLukas Czerner 				goto err_out;
5446ac27a0ecSDave Kleikamp 			}
5447b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5448b31e1552SEric Sandeen 			       "be enabled during recovery");
5449ac27a0ecSDave Kleikamp 		}
5450ac27a0ecSDave Kleikamp 	}
5451ac27a0ecSDave Kleikamp 
545290576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5453b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
54544776004fSTheodore Ts'o 
5455e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5456dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
54571c13d5c0STheodore Ts'o 	if (!err) {
54581c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
54591c13d5c0STheodore Ts'o 		if (save)
54601c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
54611c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5462dab291afSMingming Cao 		err = jbd2_journal_load(journal);
54631c13d5c0STheodore Ts'o 		if (save)
54641c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
54651c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
54661c13d5c0STheodore Ts'o 		kfree(save);
54671c13d5c0STheodore Ts'o 	}
5468ac27a0ecSDave Kleikamp 
5469ac27a0ecSDave Kleikamp 	if (err) {
5470b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5471273108faSLukas Czerner 		goto err_out;
5472ac27a0ecSDave Kleikamp 	}
5473ac27a0ecSDave Kleikamp 
5474617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
547511215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
547611215630SJan Kara 	if (err) {
547711215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
547811215630SJan Kara 		jbd2_journal_destroy(journal);
547911215630SJan Kara 		return err;
548011215630SJan Kara 	}
5481ac27a0ecSDave Kleikamp 
5482c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5483ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5484ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5485ac27a0ecSDave Kleikamp 
5486ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5487e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5488ac27a0ecSDave Kleikamp 	}
5489ac27a0ecSDave Kleikamp 
5490ac27a0ecSDave Kleikamp 	return 0;
5491273108faSLukas Czerner 
5492273108faSLukas Czerner err_out:
5493273108faSLukas Czerner 	jbd2_journal_destroy(journal);
5494273108faSLukas Czerner 	return err;
5495ac27a0ecSDave Kleikamp }
5496ac27a0ecSDave Kleikamp 
5497e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5498ac27a0ecSDave Kleikamp {
5499e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5500617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5501c4be0c1dSTakashi Sato 	int error = 0;
5502ac27a0ecSDave Kleikamp 
5503bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5504c4be0c1dSTakashi Sato 		return error;
5505a17712c8SJon Derrick 
5506a17712c8SJon Derrick 	/*
550771290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
550871290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
550971290b36STheodore Ts'o 	 * write time when we are mounting the root file system
551071290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
551171290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
551271290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
551371290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
551471290b36STheodore Ts'o 	 * to complain and force a full file system check.
551571290b36STheodore Ts'o 	 */
55161751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
55176a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5518f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5519afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5520afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5521dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5522dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5523afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5524f613dfcbSTheodore Ts'o 	else
5525f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5526f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5527d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
552857042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
552957042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
553057042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5531d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
55327f93cff9STheodore Ts'o 		es->s_free_inodes_count =
55337f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
55345d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5535ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
553606db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
55371566a48aSTheodore Ts'o 	if (sync)
55384743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5539e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
55404743f839SPranay Kr. Srivastava 		/*
55414743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
55424743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
55434743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
55444743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
55454743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
55464743f839SPranay Kr. Srivastava 		 * write and hope for the best.
55474743f839SPranay Kr. Srivastava 		 */
55484743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
55494743f839SPranay Kr. Srivastava 		       "superblock detected");
55504743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
55514743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
55524743f839SPranay Kr. Srivastava 	}
5553ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5554914258bfSTheodore Ts'o 	if (sync) {
55551566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5556564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
555700473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5558c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5559b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5560b31e1552SEric Sandeen 			       "superblock");
5561914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5562914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5563914258bfSTheodore Ts'o 		}
5564914258bfSTheodore Ts'o 	}
5565c4be0c1dSTakashi Sato 	return error;
5566ac27a0ecSDave Kleikamp }
5567ac27a0ecSDave Kleikamp 
5568ac27a0ecSDave Kleikamp /*
5569ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5570ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5571ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5572ac27a0ecSDave Kleikamp  */
557311215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
5574617ba13bSMingming Cao 				       struct ext4_super_block *es)
5575ac27a0ecSDave Kleikamp {
557611215630SJan Kara 	int err;
5577617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5578ac27a0ecSDave Kleikamp 
5579e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
558011215630SJan Kara 		if (journal != NULL) {
558111215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
558211215630SJan Kara 				   "mounted!");
558311215630SJan Kara 			return -EFSCORRUPTED;
558411215630SJan Kara 		}
558511215630SJan Kara 		return 0;
55860390131bSFrank Mayhar 	}
5587dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
558811215630SJan Kara 	err = jbd2_journal_flush(journal);
558911215630SJan Kara 	if (err < 0)
55907ffe1ea8SHidehiro Kawai 		goto out;
55917ffe1ea8SHidehiro Kawai 
5592bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5593e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5594e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5595ac27a0ecSDave Kleikamp 	}
55967ffe1ea8SHidehiro Kawai out:
5597dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
559811215630SJan Kara 	return err;
5599ac27a0ecSDave Kleikamp }
5600ac27a0ecSDave Kleikamp 
5601ac27a0ecSDave Kleikamp /*
5602ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5603ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5604ac27a0ecSDave Kleikamp  * main filesystem now.
5605ac27a0ecSDave Kleikamp  */
560611215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
5607617ba13bSMingming Cao 				   struct ext4_super_block *es)
5608ac27a0ecSDave Kleikamp {
5609ac27a0ecSDave Kleikamp 	journal_t *journal;
5610ac27a0ecSDave Kleikamp 	int j_errno;
5611ac27a0ecSDave Kleikamp 	const char *errstr;
5612ac27a0ecSDave Kleikamp 
561311215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
561411215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
561511215630SJan Kara 		return -EFSCORRUPTED;
561611215630SJan Kara 	}
56170390131bSFrank Mayhar 
5618617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5619ac27a0ecSDave Kleikamp 
5620ac27a0ecSDave Kleikamp 	/*
5621ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5622617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5623ac27a0ecSDave Kleikamp 	 */
5624ac27a0ecSDave Kleikamp 
5625dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5626ac27a0ecSDave Kleikamp 	if (j_errno) {
5627ac27a0ecSDave Kleikamp 		char nbuf[16];
5628ac27a0ecSDave Kleikamp 
5629617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
563012062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5631ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
563212062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5633ac27a0ecSDave Kleikamp 
5634617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5635617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5636e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5637ac27a0ecSDave Kleikamp 
5638dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5639d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5640ac27a0ecSDave Kleikamp 	}
564111215630SJan Kara 	return 0;
5642ac27a0ecSDave Kleikamp }
5643ac27a0ecSDave Kleikamp 
5644ac27a0ecSDave Kleikamp /*
5645ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5646ac27a0ecSDave Kleikamp  * and wait on the commit.
5647ac27a0ecSDave Kleikamp  */
5648617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5649ac27a0ecSDave Kleikamp {
5650ac27a0ecSDave Kleikamp 	journal_t *journal;
5651ac27a0ecSDave Kleikamp 
5652bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5653ac27a0ecSDave Kleikamp 		return 0;
5654ac27a0ecSDave Kleikamp 
5655617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5656b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5657ac27a0ecSDave Kleikamp }
5658ac27a0ecSDave Kleikamp 
5659617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5660ac27a0ecSDave Kleikamp {
566114ce0cb4STheodore Ts'o 	int ret = 0;
56629eddacf9SJan Kara 	tid_t target;
566306a407f1SDmitry Monakhov 	bool needs_barrier = false;
56648d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5665ac27a0ecSDave Kleikamp 
566649598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
56670db1ff22STheodore Ts'o 		return 0;
56680db1ff22STheodore Ts'o 
56699bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
56702e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5671a1177825SJan Kara 	/*
5672a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5673a1177825SJan Kara 	 * no dirty dquots
5674a1177825SJan Kara 	 */
5675a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
567606a407f1SDmitry Monakhov 	/*
567706a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
567806a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
567906a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
568006a407f1SDmitry Monakhov 	 */
5681bda32530STheodore Ts'o 	if (sbi->s_journal) {
568206a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
568306a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
568406a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
568506a407f1SDmitry Monakhov 			needs_barrier = true;
568606a407f1SDmitry Monakhov 
56878d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5688ac27a0ecSDave Kleikamp 			if (wait)
5689bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5690bda32530STheodore Ts'o 							   target);
56910390131bSFrank Mayhar 		}
5692bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5693bda32530STheodore Ts'o 		needs_barrier = true;
569406a407f1SDmitry Monakhov 	if (needs_barrier) {
569506a407f1SDmitry Monakhov 		int err;
56969398554fSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
569706a407f1SDmitry Monakhov 		if (!ret)
569806a407f1SDmitry Monakhov 			ret = err;
569906a407f1SDmitry Monakhov 	}
570006a407f1SDmitry Monakhov 
570106a407f1SDmitry Monakhov 	return ret;
570206a407f1SDmitry Monakhov }
570306a407f1SDmitry Monakhov 
5704ac27a0ecSDave Kleikamp /*
5705ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5706ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5707be4f27d3SYongqiang Yang  *
5708be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
57098e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
57108e8ad8a5SJan Kara  * modifications.
5711ac27a0ecSDave Kleikamp  */
5712c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5713ac27a0ecSDave Kleikamp {
5714c4be0c1dSTakashi Sato 	int error = 0;
5715c4be0c1dSTakashi Sato 	journal_t *journal;
5716ac27a0ecSDave Kleikamp 
5717bc98a42cSDavid Howells 	if (sb_rdonly(sb))
57189ca92389STheodore Ts'o 		return 0;
57199ca92389STheodore Ts'o 
5720c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5721ac27a0ecSDave Kleikamp 
5722bb044576STheodore Ts'o 	if (journal) {
5723ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5724dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
57257ffe1ea8SHidehiro Kawai 
57267ffe1ea8SHidehiro Kawai 		/*
5727bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5728bb044576STheodore Ts'o 		 * flush the journal.
57297ffe1ea8SHidehiro Kawai 		 */
5730c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
57316b0310fbSEric Sandeen 		if (error < 0)
57326b0310fbSEric Sandeen 			goto out;
5733ac27a0ecSDave Kleikamp 
5734ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5735e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5736c642dc9eSEric Sandeen 	}
5737c642dc9eSEric Sandeen 
5738e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
57396b0310fbSEric Sandeen out:
5740bb044576STheodore Ts'o 	if (journal)
57418e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5742bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
57436b0310fbSEric Sandeen 	return error;
5744ac27a0ecSDave Kleikamp }
5745ac27a0ecSDave Kleikamp 
5746ac27a0ecSDave Kleikamp /*
5747ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5748ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5749ac27a0ecSDave Kleikamp  */
5750c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5751ac27a0ecSDave Kleikamp {
5752bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
57539ca92389STheodore Ts'o 		return 0;
57549ca92389STheodore Ts'o 
5755c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
57569ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5757e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5758c642dc9eSEric Sandeen 	}
5759c642dc9eSEric Sandeen 
5760e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5761c4be0c1dSTakashi Sato 	return 0;
5762ac27a0ecSDave Kleikamp }
5763ac27a0ecSDave Kleikamp 
5764673c6100STheodore Ts'o /*
5765673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5766673c6100STheodore Ts'o  */
5767673c6100STheodore Ts'o struct ext4_mount_options {
5768673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5769a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
577008cefc7aSEric W. Biederman 	kuid_t s_resuid;
577108cefc7aSEric W. Biederman 	kgid_t s_resgid;
5772673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5773673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5774673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5775673c6100STheodore Ts'o 	int s_jquota_fmt;
5776a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5777673c6100STheodore Ts'o #endif
5778673c6100STheodore Ts'o };
5779673c6100STheodore Ts'o 
5780617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5781ac27a0ecSDave Kleikamp {
5782617ba13bSMingming Cao 	struct ext4_super_block *es;
5783617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5784f25391ebSLukas Czerner 	unsigned long old_sb_flags, vfs_flags;
5785617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5786c79d967dSChristoph Hellwig 	int enable_quota = 0;
57878a266467STheodore Ts'o 	ext4_group_t g;
5788b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5789c5e06d10SJohann Lombardi 	int err = 0;
5790ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
579103dafb5fSChen Gang 	int i, j;
579233458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5793ac27a0ecSDave Kleikamp #endif
5794d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5795ac27a0ecSDave Kleikamp 
579621ac738eSChengguang Xu 	if (data && !orig_data)
579721ac738eSChengguang Xu 		return -ENOMEM;
579821ac738eSChengguang Xu 
5799ac27a0ecSDave Kleikamp 	/* Store the original options */
5800ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5801ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5802a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5803ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5804ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5805ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
580630773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
580730773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5808ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5809ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5810a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
581103dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
581233458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
581333458eabSTheodore Ts'o 
581433458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
581503dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
581603dafb5fSChen Gang 				for (j = 0; j < i; j++)
581703dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
58183e36a163SWei Yongjun 				kfree(orig_data);
581903dafb5fSChen Gang 				return -ENOMEM;
582003dafb5fSChen Gang 			}
582103dafb5fSChen Gang 		} else
582203dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5823ac27a0ecSDave Kleikamp #endif
5824b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5825b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5826ac27a0ecSDave Kleikamp 
5827f25391ebSLukas Czerner 	/*
5828f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
5829f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
5830f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from *flags to s_flags
5831f25391ebSLukas Czerner 	 */
5832f25391ebSLukas Czerner 	vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5833f25391ebSLukas Czerner 	sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5834f25391ebSLukas Czerner 
5835661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5836ac27a0ecSDave Kleikamp 		err = -EINVAL;
5837ac27a0ecSDave Kleikamp 		goto restore_opts;
5838ac27a0ecSDave Kleikamp 	}
5839ac27a0ecSDave Kleikamp 
58406b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
58416b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
58426b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
58432d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
58442d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5845c6d3d56dSDarrick J. Wong 	}
5846c6d3d56dSDarrick J. Wong 
58476ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
58486ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
58496ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
58506ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
58516ae6514bSPiotr Sarna 			err = -EINVAL;
58526ae6514bSPiotr Sarna 			goto restore_opts;
58536ae6514bSPiotr Sarna 		}
58546ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
58556ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
58566ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
58576ae6514bSPiotr Sarna 			err = -EINVAL;
58586ae6514bSPiotr Sarna 			goto restore_opts;
58596ae6514bSPiotr Sarna 		}
5860ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5861ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5862ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5863ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5864ab04df78SJan Kara 			err = -EINVAL;
5865ab04df78SJan Kara 			goto restore_opts;
5866ab04df78SJan Kara 		}
5867923ae0ffSRoss Zwisler 	}
5868923ae0ffSRoss Zwisler 
5869cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5870cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5871cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5872cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5873cdb7ee4cSTahsin Erdogan 	}
5874cdb7ee4cSTahsin Erdogan 
58754ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
587654d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5877ac27a0ecSDave Kleikamp 
58781751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
58791751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5880ac27a0ecSDave Kleikamp 
5881ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5882ac27a0ecSDave Kleikamp 
5883b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5884617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5885b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5886b3881f74STheodore Ts'o 	}
5887ac27a0ecSDave Kleikamp 
58881751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
58894ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5890ac27a0ecSDave Kleikamp 			err = -EROFS;
5891ac27a0ecSDave Kleikamp 			goto restore_opts;
5892ac27a0ecSDave Kleikamp 		}
5893ac27a0ecSDave Kleikamp 
58941751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
589538c03b34STheodore Ts'o 			err = sync_filesystem(sb);
589638c03b34STheodore Ts'o 			if (err < 0)
589738c03b34STheodore Ts'o 				goto restore_opts;
58980f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
58990f0dd62fSChristoph Hellwig 			if (err < 0)
5900c79d967dSChristoph Hellwig 				goto restore_opts;
5901c79d967dSChristoph Hellwig 
5902ac27a0ecSDave Kleikamp 			/*
5903ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5904ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5905ac27a0ecSDave Kleikamp 			 */
59061751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5907ac27a0ecSDave Kleikamp 
5908ac27a0ecSDave Kleikamp 			/*
5909ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5910ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5911ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5912ac27a0ecSDave Kleikamp 			 */
5913617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5914617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5915ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5916ac27a0ecSDave Kleikamp 
591711215630SJan Kara 			if (sbi->s_journal) {
591811215630SJan Kara 				/*
591911215630SJan Kara 				 * We let remount-ro finish even if marking fs
592011215630SJan Kara 				 * as clean failed...
592111215630SJan Kara 				 */
5922617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
592311215630SJan Kara 			}
59242dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
59252dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5926ac27a0ecSDave Kleikamp 		} else {
5927a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5928e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
59292cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5930ac27a0ecSDave Kleikamp 				err = -EROFS;
5931ac27a0ecSDave Kleikamp 				goto restore_opts;
5932ac27a0ecSDave Kleikamp 			}
5933ead6596bSEric Sandeen 			/*
59348a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
59350b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
59368a266467STheodore Ts'o 			 */
59378a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
59388a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
59398a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
59408a266467STheodore Ts'o 
5941feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5942b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5943b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5944e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
59458a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
59466a797d27SDarrick J. Wong 					err = -EFSBADCRC;
59478a266467STheodore Ts'o 					goto restore_opts;
59488a266467STheodore Ts'o 				}
59498a266467STheodore Ts'o 			}
59508a266467STheodore Ts'o 
59518a266467STheodore Ts'o 			/*
5952ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5953ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5954ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5955ead6596bSEric Sandeen 			 */
5956ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5957b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5958ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5959ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5960b31e1552SEric Sandeen 				       "umount/remount instead");
5961ead6596bSEric Sandeen 				err = -EINVAL;
5962ead6596bSEric Sandeen 				goto restore_opts;
5963ead6596bSEric Sandeen 			}
5964ead6596bSEric Sandeen 
5965ac27a0ecSDave Kleikamp 			/*
5966ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5967ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5968ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5969ac27a0ecSDave Kleikamp 			 * the partition.)
5970ac27a0ecSDave Kleikamp 			 */
597111215630SJan Kara 			if (sbi->s_journal) {
597211215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
597311215630SJan Kara 				if (err)
597411215630SJan Kara 					goto restore_opts;
597511215630SJan Kara 			}
5976ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5977c89128a0SJaegeuk Kim 
5978c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5979c89128a0SJaegeuk Kim 			if (err)
5980c89128a0SJaegeuk Kim 				goto restore_opts;
5981c89128a0SJaegeuk Kim 
59821751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5983e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5984c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5985c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5986c5e06d10SJohann Lombardi 					err = -EROFS;
5987c5e06d10SJohann Lombardi 					goto restore_opts;
5988c5e06d10SJohann Lombardi 				}
5989c79d967dSChristoph Hellwig 			enable_quota = 1;
5990ac27a0ecSDave Kleikamp 		}
5991ac27a0ecSDave Kleikamp 	}
5992bfff6873SLukas Czerner 
5993bfff6873SLukas Czerner 	/*
5994bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5995bfff6873SLukas Czerner 	 * current settings
5996bfff6873SLukas Czerner 	 */
5997bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5998bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5999bfff6873SLukas Czerner 	else {
6000bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
6001bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
6002bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
6003bfff6873SLukas Czerner 	}
6004bfff6873SLukas Czerner 
60050f5bde1dSJan Kara 	/*
60060f5bde1dSJan Kara 	 * Handle creation of system zone data early because it can fail.
60070f5bde1dSJan Kara 	 * Releasing of existing data is done when we are sure remount will
60080f5bde1dSJan Kara 	 * succeed.
60090f5bde1dSJan Kara 	 */
6010dd0db94fSChunguang Xu 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
6011d176b1f6SJan Kara 		err = ext4_setup_system_zone(sb);
6012d176b1f6SJan Kara 		if (err)
6013d176b1f6SJan Kara 			goto restore_opts;
60140f5bde1dSJan Kara 	}
6015d176b1f6SJan Kara 
6016c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
6017c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
6018c89128a0SJaegeuk Kim 		if (err)
6019c89128a0SJaegeuk Kim 			goto restore_opts;
6020c89128a0SJaegeuk Kim 	}
60210390131bSFrank Mayhar 
6022ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6023ac27a0ecSDave Kleikamp 	/* Release old quota file names */
6024a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
6025ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
60267c319d32SAditya Kali 	if (enable_quota) {
60277c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
60280f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
6029e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
60307c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
603107724f98STheodore Ts'o 			if (err)
60327c319d32SAditya Kali 				goto restore_opts;
60337c319d32SAditya Kali 		}
60347c319d32SAditya Kali 	}
60357c319d32SAditya Kali #endif
6036dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
60370f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6038d4c402d9SCurt Wohlgemuth 
6039f25391ebSLukas Czerner 	/*
6040f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
6041f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
6042f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from s_flags to *flags
6043f25391ebSLukas Czerner 	 */
6044f25391ebSLukas Czerner 	*flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
6045f25391ebSLukas Czerner 
6046d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
6047d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
6048ac27a0ecSDave Kleikamp 	return 0;
60490b8e58a1SAndreas Dilger 
6050ac27a0ecSDave Kleikamp restore_opts:
6051ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
6052ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
6053a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6054ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
6055ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
6056ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
605730773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
605830773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
6059dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
60600f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6061ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6062ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6063a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
606433458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
606533458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6066ac27a0ecSDave Kleikamp 	}
606733458eabSTheodore Ts'o 	synchronize_rcu();
606833458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
606933458eabSTheodore Ts'o 		kfree(to_free[i]);
6070ac27a0ecSDave Kleikamp #endif
6071d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
6072ac27a0ecSDave Kleikamp 	return err;
6073ac27a0ecSDave Kleikamp }
6074ac27a0ecSDave Kleikamp 
6075689c958cSLi Xi #ifdef CONFIG_QUOTA
6076689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
6077689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
6078689c958cSLi Xi {
6079689c958cSLi Xi 	struct kqid qid;
6080689c958cSLi Xi 	struct dquot *dquot;
6081689c958cSLi Xi 	u64 limit;
6082689c958cSLi Xi 	u64 curblock;
6083689c958cSLi Xi 
6084689c958cSLi Xi 	qid = make_kqid_projid(projid);
6085689c958cSLi Xi 	dquot = dqget(sb, qid);
6086689c958cSLi Xi 	if (IS_ERR(dquot))
6087689c958cSLi Xi 		return PTR_ERR(dquot);
60887b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
6089689c958cSLi Xi 
6090a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6091a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
609257c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
609357c32ea4SChengguang Xu 
6094689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
6095f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
6096f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6097689c958cSLi Xi 		buf->f_blocks = limit;
6098689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
6099689c958cSLi Xi 			(buf->f_blocks > curblock) ?
6100689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
6101689c958cSLi Xi 	}
6102689c958cSLi Xi 
6103a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6104a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
6105689c958cSLi Xi 	if (limit && buf->f_files > limit) {
6106689c958cSLi Xi 		buf->f_files = limit;
6107689c958cSLi Xi 		buf->f_ffree =
6108689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6109689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6110689c958cSLi Xi 	}
6111689c958cSLi Xi 
61127b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
6113689c958cSLi Xi 	dqput(dquot);
6114689c958cSLi Xi 	return 0;
6115689c958cSLi Xi }
6116689c958cSLi Xi #endif
6117689c958cSLi Xi 
6118617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6119ac27a0ecSDave Kleikamp {
6120ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
6121617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6122617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
612327dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
6124960cc398SPekka Enberg 	u64 fsid;
6125d02a9391SKazuya Mio 	s64 bfree;
612627dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6127ac27a0ecSDave Kleikamp 
6128952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
6129952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
6130ac27a0ecSDave Kleikamp 
6131617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
6132ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
6133b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
613457042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
613557042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6136d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
613757042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
613827dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
613927dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
614027dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6141ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
6142ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
614352d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6144617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
6145960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
6146960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
6147960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
6148960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
61490b8e58a1SAndreas Dilger 
6150689c958cSLi Xi #ifdef CONFIG_QUOTA
6151689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6152689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6153689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6154689c958cSLi Xi #endif
6155ac27a0ecSDave Kleikamp 	return 0;
6156ac27a0ecSDave Kleikamp }
6157ac27a0ecSDave Kleikamp 
6158ac27a0ecSDave Kleikamp 
6159ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6160ac27a0ecSDave Kleikamp 
6161bc8230eeSJan Kara /*
6162bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
6163bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
6164bc8230eeSJan Kara  */
6165ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
6166ac27a0ecSDave Kleikamp {
61674c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6168ac27a0ecSDave Kleikamp }
6169ac27a0ecSDave Kleikamp 
6170617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
6171ac27a0ecSDave Kleikamp {
6172ac27a0ecSDave Kleikamp 	int ret, err;
6173ac27a0ecSDave Kleikamp 	handle_t *handle;
6174ac27a0ecSDave Kleikamp 	struct inode *inode;
6175ac27a0ecSDave Kleikamp 
6176ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
61779924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6178617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6179ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6180ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6181ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
6182617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6183ac27a0ecSDave Kleikamp 	if (!ret)
6184ac27a0ecSDave Kleikamp 		ret = err;
6185ac27a0ecSDave Kleikamp 	return ret;
6186ac27a0ecSDave Kleikamp }
6187ac27a0ecSDave Kleikamp 
6188617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
6189ac27a0ecSDave Kleikamp {
6190ac27a0ecSDave Kleikamp 	int ret, err;
6191ac27a0ecSDave Kleikamp 	handle_t *handle;
6192ac27a0ecSDave Kleikamp 
61939924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6194617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6195ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6196ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6197ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
6198617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6199ac27a0ecSDave Kleikamp 	if (!ret)
6200ac27a0ecSDave Kleikamp 		ret = err;
6201ac27a0ecSDave Kleikamp 	return ret;
6202ac27a0ecSDave Kleikamp }
6203ac27a0ecSDave Kleikamp 
6204617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
6205ac27a0ecSDave Kleikamp {
6206ac27a0ecSDave Kleikamp 	int ret, err;
6207ac27a0ecSDave Kleikamp 	handle_t *handle;
6208ac27a0ecSDave Kleikamp 
62099924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6210617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
62119c3013e9SJan Kara 	if (IS_ERR(handle)) {
62129c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
62139c3013e9SJan Kara 		dquot_release(dquot);
6214ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
62159c3013e9SJan Kara 	}
6216ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
6217617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6218ac27a0ecSDave Kleikamp 	if (!ret)
6219ac27a0ecSDave Kleikamp 		ret = err;
6220ac27a0ecSDave Kleikamp 	return ret;
6221ac27a0ecSDave Kleikamp }
6222ac27a0ecSDave Kleikamp 
6223617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
6224ac27a0ecSDave Kleikamp {
6225262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
6226262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6227262b4662SJan Kara 
62282c8be6b2SJan Kara 	/* Are we journaling quotas? */
6229e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
6230262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
6231ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
6232617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
6233ac27a0ecSDave Kleikamp 	} else {
6234ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
6235ac27a0ecSDave Kleikamp 	}
6236ac27a0ecSDave Kleikamp }
6237ac27a0ecSDave Kleikamp 
6238617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
6239ac27a0ecSDave Kleikamp {
6240ac27a0ecSDave Kleikamp 	int ret, err;
6241ac27a0ecSDave Kleikamp 	handle_t *handle;
6242ac27a0ecSDave Kleikamp 
6243ac27a0ecSDave Kleikamp 	/* Data block + inode block */
62442b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6245ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6246ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6247ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
6248617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6249ac27a0ecSDave Kleikamp 	if (!ret)
6250ac27a0ecSDave Kleikamp 		ret = err;
6251ac27a0ecSDave Kleikamp 	return ret;
6252ac27a0ecSDave Kleikamp }
6253ac27a0ecSDave Kleikamp 
6254ac27a0ecSDave Kleikamp /*
6255ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
6256ac27a0ecSDave Kleikamp  * the quota file and such...
6257ac27a0ecSDave Kleikamp  */
6258617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
6259ac27a0ecSDave Kleikamp {
626033458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
6261617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
6262ac27a0ecSDave Kleikamp }
6263ac27a0ecSDave Kleikamp 
6264daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6265daf647d2STheodore Ts'o {
6266daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
6267daf647d2STheodore Ts'o 
6268daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
6269daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
6270daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
6271daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
6272daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
6273daf647d2STheodore Ts'o 	 */
6274daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6275daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6276daf647d2STheodore Ts'o }
6277daf647d2STheodore Ts'o 
6278ac27a0ecSDave Kleikamp /*
6279ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
6280ac27a0ecSDave Kleikamp  */
6281617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
62828c54ca9cSAl Viro 			 const struct path *path)
6283ac27a0ecSDave Kleikamp {
6284ac27a0ecSDave Kleikamp 	int err;
6285ac27a0ecSDave Kleikamp 
6286ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
6287ac27a0ecSDave Kleikamp 		return -EINVAL;
62880623543bSJan Kara 
6289ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
6290d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
6291ac27a0ecSDave Kleikamp 		return -EXDEV;
6292e0770e91SJan Kara 
6293e0770e91SJan Kara 	/* Quota already enabled for this file? */
6294e0770e91SJan Kara 	if (IS_NOQUOTA(d_inode(path->dentry)))
6295e0770e91SJan Kara 		return -EBUSY;
6296e0770e91SJan Kara 
62970623543bSJan Kara 	/* Journaling quota? */
62980623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
62992b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
6300f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
6301b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
6302b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
6303b31e1552SEric Sandeen 				"Journaled quota will not work");
630491389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
630591389240SJan Kara 	} else {
630691389240SJan Kara 		/*
630791389240SJan Kara 		 * Clear the flag just in case mount options changed since
630891389240SJan Kara 		 * last time.
630991389240SJan Kara 		 */
631091389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
63110623543bSJan Kara 	}
63120623543bSJan Kara 
63130623543bSJan Kara 	/*
63140623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
63150623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
63160623543bSJan Kara 	 */
63170390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
63182b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
63190623543bSJan Kara 		/*
63200623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
63210623543bSJan Kara 		 * otherwise be livelocked...
63220623543bSJan Kara 		 */
63230623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
63247ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
63250623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6326f00c9e44SJan Kara 		if (err)
63277ffe1ea8SHidehiro Kawai 			return err;
63287ffe1ea8SHidehiro Kawai 	}
6329957153fcSJan Kara 
6330daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6331daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
6332957153fcSJan Kara 	if (err) {
6333daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
6334daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
6335957153fcSJan Kara 	} else {
6336957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6337957153fcSJan Kara 		handle_t *handle;
6338957153fcSJan Kara 
633961a92987SJan Kara 		/*
634061a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
634161a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
634261a92987SJan Kara 		 * are already enabled and this is not a hard failure.
634361a92987SJan Kara 		 */
6344957153fcSJan Kara 		inode_lock(inode);
6345957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6346957153fcSJan Kara 		if (IS_ERR(handle))
6347957153fcSJan Kara 			goto unlock_inode;
6348957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6349957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6350957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
63514209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6352957153fcSJan Kara 		ext4_journal_stop(handle);
6353957153fcSJan Kara 	unlock_inode:
6354957153fcSJan Kara 		inode_unlock(inode);
6355957153fcSJan Kara 	}
6356daf647d2STheodore Ts'o 	return err;
6357ac27a0ecSDave Kleikamp }
6358ac27a0ecSDave Kleikamp 
63597c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
63607c319d32SAditya Kali 			     unsigned int flags)
63617c319d32SAditya Kali {
63627c319d32SAditya Kali 	int err;
63637c319d32SAditya Kali 	struct inode *qf_inode;
6364a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
63657c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6366689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6367689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
63687c319d32SAditya Kali 	};
63697c319d32SAditya Kali 
6370e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
63717c319d32SAditya Kali 
63727c319d32SAditya Kali 	if (!qf_inums[type])
63737c319d32SAditya Kali 		return -EPERM;
63747c319d32SAditya Kali 
63758a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
63767c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
63777c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
63787c319d32SAditya Kali 		return PTR_ERR(qf_inode);
63797c319d32SAditya Kali 	}
63807c319d32SAditya Kali 
6381bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6382bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6383daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
63847212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6385daf647d2STheodore Ts'o 	if (err)
6386daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
638761157b24SPan Bian 	iput(qf_inode);
63887c319d32SAditya Kali 
63897c319d32SAditya Kali 	return err;
63907c319d32SAditya Kali }
63917c319d32SAditya Kali 
63927c319d32SAditya Kali /* Enable usage tracking for all quota types. */
63937c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
63947c319d32SAditya Kali {
63957c319d32SAditya Kali 	int type, err = 0;
6396a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
63977c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6398689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6399689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
64007c319d32SAditya Kali 	};
640149da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
640249da9392SJan Kara 		test_opt(sb, USRQUOTA),
640349da9392SJan Kara 		test_opt(sb, GRPQUOTA),
640449da9392SJan Kara 		test_opt(sb, PRJQUOTA),
640549da9392SJan Kara 	};
64067c319d32SAditya Kali 
640791389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6408a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
64097c319d32SAditya Kali 		if (qf_inums[type]) {
64107c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
641149da9392SJan Kara 				DQUOT_USAGE_ENABLED |
641249da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
64137c319d32SAditya Kali 			if (err) {
64147c319d32SAditya Kali 				ext4_warning(sb,
641572ba7450STheodore Ts'o 					"Failed to enable quota tracking "
641672ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
641772ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
64187f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
64197f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
64207f144fd0SJunichi Uekawa 
64217c319d32SAditya Kali 				return err;
64227c319d32SAditya Kali 			}
64237c319d32SAditya Kali 		}
64247c319d32SAditya Kali 	}
64257c319d32SAditya Kali 	return 0;
64267c319d32SAditya Kali }
64277c319d32SAditya Kali 
6428ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6429ca0e05e4SDmitry Monakhov {
643021f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
643121f97697SJan Kara 	handle_t *handle;
6432957153fcSJan Kara 	int err;
643321f97697SJan Kara 
643487009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
643587009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
643687009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6437ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6438ca0e05e4SDmitry Monakhov 
6439957153fcSJan Kara 	if (!inode || !igrab(inode))
64400b268590SAmir Goldstein 		goto out;
64410b268590SAmir Goldstein 
6442957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6443964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6444957153fcSJan Kara 		goto out_put;
6445957153fcSJan Kara 
6446957153fcSJan Kara 	inode_lock(inode);
644761a92987SJan Kara 	/*
644861a92987SJan Kara 	 * Update modification times of quota files when userspace can
644961a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
645061a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
645161a92987SJan Kara 	 */
64529924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
64534209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
64544209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6455957153fcSJan Kara 		goto out_unlock;
64564209ae12SHarshad Shirwadkar 	}
6457957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6458957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6459eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
64604209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
646121f97697SJan Kara 	ext4_journal_stop(handle);
6462957153fcSJan Kara out_unlock:
6463957153fcSJan Kara 	inode_unlock(inode);
6464957153fcSJan Kara out_put:
6465964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6466957153fcSJan Kara 	iput(inode);
6467957153fcSJan Kara 	return err;
646821f97697SJan Kara out:
6469ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6470ca0e05e4SDmitry Monakhov }
6471ca0e05e4SDmitry Monakhov 
6472ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6473ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6474ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6475ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6476617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6477ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6478ac27a0ecSDave Kleikamp {
6479ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6480725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6481ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6482ac27a0ecSDave Kleikamp 	int tocopy;
6483ac27a0ecSDave Kleikamp 	size_t toread;
6484ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6485ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6486ac27a0ecSDave Kleikamp 
6487ac27a0ecSDave Kleikamp 	if (off > i_size)
6488ac27a0ecSDave Kleikamp 		return 0;
6489ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6490ac27a0ecSDave Kleikamp 		len = i_size-off;
6491ac27a0ecSDave Kleikamp 	toread = len;
6492ac27a0ecSDave Kleikamp 	while (toread > 0) {
6493ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6494ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
64951c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
64961c215028STheodore Ts'o 		if (IS_ERR(bh))
64971c215028STheodore Ts'o 			return PTR_ERR(bh);
6498ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6499ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6500ac27a0ecSDave Kleikamp 		else
6501ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6502ac27a0ecSDave Kleikamp 		brelse(bh);
6503ac27a0ecSDave Kleikamp 		offset = 0;
6504ac27a0ecSDave Kleikamp 		toread -= tocopy;
6505ac27a0ecSDave Kleikamp 		data += tocopy;
6506ac27a0ecSDave Kleikamp 		blk++;
6507ac27a0ecSDave Kleikamp 	}
6508ac27a0ecSDave Kleikamp 	return len;
6509ac27a0ecSDave Kleikamp }
6510ac27a0ecSDave Kleikamp 
6511ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6512ac27a0ecSDave Kleikamp  * enough credits) */
6513617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6514ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6515ac27a0ecSDave Kleikamp {
6516ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6517725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
65184209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6519c5e298aeSTheodore Ts'o 	int retries = 0;
6520ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6521ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6522ac27a0ecSDave Kleikamp 
65230390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6524b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6525b31e1552SEric Sandeen 			" cancelled because transaction is not started",
65269c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
65279c3013e9SJan Kara 		return -EIO;
65289c3013e9SJan Kara 	}
652967eeb568SDmitry Monakhov 	/*
653067eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
653167eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
653267eeb568SDmitry Monakhov 	 */
653367eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
653467eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
653567eeb568SDmitry Monakhov 			" cancelled because not block aligned",
653667eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
653767eeb568SDmitry Monakhov 		return -EIO;
653867eeb568SDmitry Monakhov 	}
653967eeb568SDmitry Monakhov 
6540c5e298aeSTheodore Ts'o 	do {
6541c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6542c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6543c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
654445586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6545c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
65461c215028STheodore Ts'o 	if (IS_ERR(bh))
65471c215028STheodore Ts'o 		return PTR_ERR(bh);
6548ac27a0ecSDave Kleikamp 	if (!bh)
6549ac27a0ecSDave Kleikamp 		goto out;
65505d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6551617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6552ac27a0ecSDave Kleikamp 	if (err) {
6553ac27a0ecSDave Kleikamp 		brelse(bh);
65541c215028STheodore Ts'o 		return err;
6555ac27a0ecSDave Kleikamp 	}
6556ac27a0ecSDave Kleikamp 	lock_buffer(bh);
655767eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6558ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6559ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
65600390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6561ac27a0ecSDave Kleikamp 	brelse(bh);
6562ac27a0ecSDave Kleikamp out:
656367eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
6564aa75f4d3SHarshad Shirwadkar 		ext4_fc_track_range(inode,
6565aa75f4d3SHarshad Shirwadkar 			(inode->i_size > 0 ? inode->i_size - 1 : 0)
6566aa75f4d3SHarshad Shirwadkar 				>> inode->i_sb->s_blocksize_bits,
6567aa75f4d3SHarshad Shirwadkar 			(off + len) >> inode->i_sb->s_blocksize_bits);
656867eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6569617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
65704209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
65714209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
65724209ae12SHarshad Shirwadkar 			err = err2;
657321f97697SJan Kara 	}
65744209ae12SHarshad Shirwadkar 	return err ? err : len;
6575ac27a0ecSDave Kleikamp }
6576ac27a0ecSDave Kleikamp #endif
6577ac27a0ecSDave Kleikamp 
6578152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6579152a0836SAl Viro 		       const char *dev_name, void *data)
6580ac27a0ecSDave Kleikamp {
6581152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6582ac27a0ecSDave Kleikamp }
6583ac27a0ecSDave Kleikamp 
6584c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
658524b58424STheodore Ts'o static inline void register_as_ext2(void)
658624b58424STheodore Ts'o {
658724b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
658824b58424STheodore Ts'o 	if (err)
658924b58424STheodore Ts'o 		printk(KERN_WARNING
659024b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
659124b58424STheodore Ts'o }
659224b58424STheodore Ts'o 
659324b58424STheodore Ts'o static inline void unregister_as_ext2(void)
659424b58424STheodore Ts'o {
659524b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
659624b58424STheodore Ts'o }
65972035e776STheodore Ts'o 
65982035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
65992035e776STheodore Ts'o {
6600e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
66012035e776STheodore Ts'o 		return 0;
6602bc98a42cSDavid Howells 	if (sb_rdonly(sb))
66032035e776STheodore Ts'o 		return 1;
6604e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
66052035e776STheodore Ts'o 		return 0;
66062035e776STheodore Ts'o 	return 1;
66072035e776STheodore Ts'o }
660824b58424STheodore Ts'o #else
660924b58424STheodore Ts'o static inline void register_as_ext2(void) { }
661024b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
66112035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
661224b58424STheodore Ts'o #endif
661324b58424STheodore Ts'o 
661424b58424STheodore Ts'o static inline void register_as_ext3(void)
661524b58424STheodore Ts'o {
661624b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
661724b58424STheodore Ts'o 	if (err)
661824b58424STheodore Ts'o 		printk(KERN_WARNING
661924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
662024b58424STheodore Ts'o }
662124b58424STheodore Ts'o 
662224b58424STheodore Ts'o static inline void unregister_as_ext3(void)
662324b58424STheodore Ts'o {
662424b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
662524b58424STheodore Ts'o }
66262035e776STheodore Ts'o 
66272035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
66282035e776STheodore Ts'o {
6629e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
66302035e776STheodore Ts'o 		return 0;
6631e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
66322035e776STheodore Ts'o 		return 0;
6633bc98a42cSDavid Howells 	if (sb_rdonly(sb))
66342035e776STheodore Ts'o 		return 1;
6635e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
66362035e776STheodore Ts'o 		return 0;
66372035e776STheodore Ts'o 	return 1;
66382035e776STheodore Ts'o }
663924b58424STheodore Ts'o 
664003010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6641ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
664203010a33STheodore Ts'o 	.name		= "ext4",
6643152a0836SAl Viro 	.mount		= ext4_mount,
6644ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6645ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6646ac27a0ecSDave Kleikamp };
66477f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6648ac27a0ecSDave Kleikamp 
6649e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6650e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6651e9e3bcecSEric Sandeen 
66525dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6653ac27a0ecSDave Kleikamp {
6654e9e3bcecSEric Sandeen 	int i, err;
6655c9de560dSAlex Tomas 
6656e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
665707c0c5d8SAl Viro 	ext4_li_info = NULL;
665807c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
665907c0c5d8SAl Viro 
66609a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
666112e9b892SDmitry Monakhov 	ext4_check_flag_values();
6662e9e3bcecSEric Sandeen 
6663e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6664e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6665e9e3bcecSEric Sandeen 
666651865fdaSZheng Liu 	err = ext4_init_es();
66676fd058f7STheodore Ts'o 	if (err)
66686fd058f7STheodore Ts'o 		return err;
666951865fdaSZheng Liu 
66701dc0aa46SEric Whitney 	err = ext4_init_pending();
66711dc0aa46SEric Whitney 	if (err)
667222cfe4b4SEric Biggers 		goto out7;
667322cfe4b4SEric Biggers 
667422cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
667522cfe4b4SEric Biggers 	if (err)
66761dc0aa46SEric Whitney 		goto out6;
66771dc0aa46SEric Whitney 
667851865fdaSZheng Liu 	err = ext4_init_pageio();
667951865fdaSZheng Liu 	if (err)
6680b5799018STheodore Ts'o 		goto out5;
668151865fdaSZheng Liu 
66825dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6683bd2d0210STheodore Ts'o 	if (err)
6684b5799018STheodore Ts'o 		goto out4;
6685857ac889SLukas Czerner 
6686b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6687dd68314cSTheodore Ts'o 	if (err)
6688b5799018STheodore Ts'o 		goto out3;
6689857ac889SLukas Czerner 
66905dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6691ac27a0ecSDave Kleikamp 	if (err)
6692c9de560dSAlex Tomas 		goto out2;
6693ac27a0ecSDave Kleikamp 	err = init_inodecache();
6694ac27a0ecSDave Kleikamp 	if (err)
6695ac27a0ecSDave Kleikamp 		goto out1;
6696aa75f4d3SHarshad Shirwadkar 
6697aa75f4d3SHarshad Shirwadkar 	err = ext4_fc_init_dentry_cache();
6698aa75f4d3SHarshad Shirwadkar 	if (err)
6699aa75f4d3SHarshad Shirwadkar 		goto out05;
6700aa75f4d3SHarshad Shirwadkar 
670124b58424STheodore Ts'o 	register_as_ext3();
67022035e776STheodore Ts'o 	register_as_ext2();
670303010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6704ac27a0ecSDave Kleikamp 	if (err)
6705ac27a0ecSDave Kleikamp 		goto out;
6706bfff6873SLukas Czerner 
6707ac27a0ecSDave Kleikamp 	return 0;
6708ac27a0ecSDave Kleikamp out:
670924b58424STheodore Ts'o 	unregister_as_ext2();
671024b58424STheodore Ts'o 	unregister_as_ext3();
6711aa75f4d3SHarshad Shirwadkar out05:
6712ac27a0ecSDave Kleikamp 	destroy_inodecache();
6713ac27a0ecSDave Kleikamp out1:
67145dabfc78STheodore Ts'o 	ext4_exit_mballoc();
67159c191f70ST Makphaibulchoke out2:
6716b5799018STheodore Ts'o 	ext4_exit_sysfs();
6717b5799018STheodore Ts'o out3:
6718dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6719b5799018STheodore Ts'o out4:
67205dabfc78STheodore Ts'o 	ext4_exit_pageio();
6721b5799018STheodore Ts'o out5:
672222cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
67231dc0aa46SEric Whitney out6:
672422cfe4b4SEric Biggers 	ext4_exit_pending();
672522cfe4b4SEric Biggers out7:
672651865fdaSZheng Liu 	ext4_exit_es();
672751865fdaSZheng Liu 
6728ac27a0ecSDave Kleikamp 	return err;
6729ac27a0ecSDave Kleikamp }
6730ac27a0ecSDave Kleikamp 
67315dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6732ac27a0ecSDave Kleikamp {
6733bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
673424b58424STheodore Ts'o 	unregister_as_ext2();
673524b58424STheodore Ts'o 	unregister_as_ext3();
673603010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6737ac27a0ecSDave Kleikamp 	destroy_inodecache();
67385dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6739b5799018STheodore Ts'o 	ext4_exit_sysfs();
67405dabfc78STheodore Ts'o 	ext4_exit_system_zone();
67415dabfc78STheodore Ts'o 	ext4_exit_pageio();
674222cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6743dd12ed14SEric Sandeen 	ext4_exit_es();
67441dc0aa46SEric Whitney 	ext4_exit_pending();
6745ac27a0ecSDave Kleikamp }
6746ac27a0ecSDave Kleikamp 
6747ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
674883982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6749ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
67507ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
67515dabfc78STheodore Ts'o module_init(ext4_init_fs)
67525dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6753