xref: /linux/fs/ext4/super.c (revision e85c81ba8859a4c839bcd69c5d83b32954133a5b)
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>
499a089b21SGabriel Krisman Bertazi #include <linux/fsnotify.h>
50e5a185c2SLukas Czerner #include <linux/fs_context.h>
51e5a185c2SLukas Czerner #include <linux/fs_parser.h>
52bfff6873SLukas Czerner 
533dcf5451SChristoph Hellwig #include "ext4.h"
544a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
553dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
56ac27a0ecSDave Kleikamp #include "xattr.h"
57ac27a0ecSDave Kleikamp #include "acl.h"
583661d286STheodore Ts'o #include "mballoc.h"
590c9ec4beSDarrick J. Wong #include "fsmap.h"
60ac27a0ecSDave Kleikamp 
619bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
629bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
639bffad1eSTheodore Ts'o 
640b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
6559ebc7fdSZheng Yongjun static DEFINE_MUTEX(ext4_li_mtx);
66e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
679f6200bbSTheodore Ts'o 
68617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
69ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
702adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
712d01ddc8SJan Kara static void ext4_update_super(struct super_block *sb);
724392fbc4SJan Kara static int ext4_commit_super(struct super_block *sb);
7311215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
74617ba13bSMingming Cao 					struct ext4_super_block *es);
7511215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
76617ba13bSMingming Cao 				  struct ext4_super_block *es);
77617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
78617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
79c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
80c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
822035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
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);
88da812f61SLukas Czerner static int ext4_validate_options(struct fs_context *fc);
89b6bd2435SLukas Czerner static int ext4_check_opt_consistency(struct fs_context *fc,
90e6e268cbSLukas Czerner 				      struct super_block *sb);
916e47a3ccSLukas Czerner static int ext4_apply_options(struct fs_context *fc, struct super_block *sb);
9202f960f8SLukas Czerner static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param);
93cebe85d5SLukas Czerner static int ext4_get_tree(struct fs_context *fc);
94cebe85d5SLukas Czerner static int ext4_reconfigure(struct fs_context *fc);
95cebe85d5SLukas Czerner static void ext4_fc_free(struct fs_context *fc);
96cebe85d5SLukas Czerner static int ext4_init_fs_context(struct fs_context *fc);
97cebe85d5SLukas Czerner static const struct fs_parameter_spec ext4_param_specs[];
98ac27a0ecSDave Kleikamp 
99e74031fdSJan Kara /*
100e74031fdSJan Kara  * Lock ordering
101e74031fdSJan Kara  *
102e74031fdSJan Kara  * page fault path:
103d4f5258eSJan Kara  * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
104d4f5258eSJan Kara  *   -> page lock -> i_data_sem (rw)
105e74031fdSJan Kara  *
106e74031fdSJan Kara  * buffered write path:
107c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
108e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
109e74031fdSJan Kara  *   i_data_sem (rw)
110e74031fdSJan Kara  *
111e74031fdSJan Kara  * truncate:
112d4f5258eSJan Kara  * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
113d4f5258eSJan Kara  *   page lock
114d4f5258eSJan Kara  * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
1151d39834fSNikolay Borisov  *   i_data_sem (rw)
116e74031fdSJan Kara  *
117e74031fdSJan Kara  * direct IO:
118c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
1191d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
120e74031fdSJan Kara  *
121e74031fdSJan Kara  * writepages:
122e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
123e74031fdSJan Kara  */
124e74031fdSJan Kara 
12502f960f8SLukas Czerner static const struct fs_context_operations ext4_context_ops = {
12602f960f8SLukas Czerner 	.parse_param	= ext4_parse_param,
127cebe85d5SLukas Czerner 	.get_tree	= ext4_get_tree,
128cebe85d5SLukas Czerner 	.reconfigure	= ext4_reconfigure,
129cebe85d5SLukas Czerner 	.free		= ext4_fc_free,
13002f960f8SLukas Czerner };
13102f960f8SLukas Czerner 
13202f960f8SLukas Czerner 
133c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1342035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1352035e776STheodore Ts'o 	.owner			= THIS_MODULE,
1362035e776STheodore Ts'o 	.name			= "ext2",
137cebe85d5SLukas Czerner 	.init_fs_context	= ext4_init_fs_context,
138cebe85d5SLukas Czerner 	.parameters		= ext4_param_specs,
1392035e776STheodore Ts'o 	.kill_sb		= kill_block_super,
1402035e776STheodore Ts'o 	.fs_flags		= FS_REQUIRES_DEV,
1412035e776STheodore Ts'o };
1427f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
143fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1442035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1452035e776STheodore Ts'o #else
1462035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1472035e776STheodore Ts'o #endif
1482035e776STheodore Ts'o 
1492035e776STheodore Ts'o 
150ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
151ba69f9abSJan Kara 	.owner			= THIS_MODULE,
152ba69f9abSJan Kara 	.name			= "ext3",
153cebe85d5SLukas Czerner 	.init_fs_context	= ext4_init_fs_context,
154cebe85d5SLukas Czerner 	.parameters		= ext4_param_specs,
155ba69f9abSJan Kara 	.kill_sb		= kill_block_super,
156ba69f9abSJan Kara 	.fs_flags		= FS_REQUIRES_DEV,
157ba69f9abSJan Kara };
1587f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
159fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
160ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
161bd81d8eeSLaurent Vivier 
162fa491b14Szhangyi (F) 
163fa491b14Szhangyi (F) static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
164fa491b14Szhangyi (F) 				  bh_end_io_t *end_io)
165fa491b14Szhangyi (F) {
166fb265c9cSTheodore Ts'o 	/*
167fa491b14Szhangyi (F) 	 * buffer's verified bit is no longer valid after reading from
168fa491b14Szhangyi (F) 	 * disk again due to write out error, clear it to make sure we
169fa491b14Szhangyi (F) 	 * recheck the buffer contents.
170fa491b14Szhangyi (F) 	 */
171fa491b14Szhangyi (F) 	clear_buffer_verified(bh);
172fa491b14Szhangyi (F) 
173fa491b14Szhangyi (F) 	bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
174fa491b14Szhangyi (F) 	get_bh(bh);
175fa491b14Szhangyi (F) 	submit_bh(REQ_OP_READ, op_flags, bh);
176fa491b14Szhangyi (F) }
177fa491b14Szhangyi (F) 
178fa491b14Szhangyi (F) void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
179fa491b14Szhangyi (F) 			 bh_end_io_t *end_io)
180fa491b14Szhangyi (F) {
181fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
182fa491b14Szhangyi (F) 
183fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
184fa491b14Szhangyi (F) 		unlock_buffer(bh);
185fa491b14Szhangyi (F) 		return;
186fa491b14Szhangyi (F) 	}
187fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
188fa491b14Szhangyi (F) }
189fa491b14Szhangyi (F) 
190fa491b14Szhangyi (F) int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
191fa491b14Szhangyi (F) {
192fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
193fa491b14Szhangyi (F) 
194fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
195fa491b14Szhangyi (F) 		unlock_buffer(bh);
196fa491b14Szhangyi (F) 		return 0;
197fa491b14Szhangyi (F) 	}
198fa491b14Szhangyi (F) 
199fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
200fa491b14Szhangyi (F) 
201fa491b14Szhangyi (F) 	wait_on_buffer(bh);
202fa491b14Szhangyi (F) 	if (buffer_uptodate(bh))
203fa491b14Szhangyi (F) 		return 0;
204fa491b14Szhangyi (F) 	return -EIO;
205fa491b14Szhangyi (F) }
206fa491b14Szhangyi (F) 
207fa491b14Szhangyi (F) int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
208fa491b14Szhangyi (F) {
209fa491b14Szhangyi (F) 	if (trylock_buffer(bh)) {
210fa491b14Szhangyi (F) 		if (wait)
211fa491b14Szhangyi (F) 			return ext4_read_bh(bh, op_flags, NULL);
212fa491b14Szhangyi (F) 		ext4_read_bh_nowait(bh, op_flags, NULL);
213fa491b14Szhangyi (F) 		return 0;
214fa491b14Szhangyi (F) 	}
215fa491b14Szhangyi (F) 	if (wait) {
216fa491b14Szhangyi (F) 		wait_on_buffer(bh);
217fa491b14Szhangyi (F) 		if (buffer_uptodate(bh))
218fa491b14Szhangyi (F) 			return 0;
219fa491b14Szhangyi (F) 		return -EIO;
220fa491b14Szhangyi (F) 	}
221fa491b14Szhangyi (F) 	return 0;
222fa491b14Szhangyi (F) }
223fa491b14Szhangyi (F) 
224fb265c9cSTheodore Ts'o /*
2258394a6abSzhangyi (F)  * This works like __bread_gfp() except it uses ERR_PTR for error
226fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
227fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
228fb265c9cSTheodore Ts'o  * return.
229fb265c9cSTheodore Ts'o  */
2308394a6abSzhangyi (F) static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
2318394a6abSzhangyi (F) 					       sector_t block, int op_flags,
2328394a6abSzhangyi (F) 					       gfp_t gfp)
233fb265c9cSTheodore Ts'o {
2342d069c08Szhangyi (F) 	struct buffer_head *bh;
2352d069c08Szhangyi (F) 	int ret;
236fb265c9cSTheodore Ts'o 
2378394a6abSzhangyi (F) 	bh = sb_getblk_gfp(sb, block, gfp);
238fb265c9cSTheodore Ts'o 	if (bh == NULL)
239fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
240cf2834a5STheodore Ts'o 	if (ext4_buffer_uptodate(bh))
241fb265c9cSTheodore Ts'o 		return bh;
2422d069c08Szhangyi (F) 
2432d069c08Szhangyi (F) 	ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
2442d069c08Szhangyi (F) 	if (ret) {
245fb265c9cSTheodore Ts'o 		put_bh(bh);
2462d069c08Szhangyi (F) 		return ERR_PTR(ret);
2472d069c08Szhangyi (F) 	}
2482d069c08Szhangyi (F) 	return bh;
249fb265c9cSTheodore Ts'o }
250fb265c9cSTheodore Ts'o 
2518394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
2528394a6abSzhangyi (F) 				   int op_flags)
2538394a6abSzhangyi (F) {
2548394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
2558394a6abSzhangyi (F) }
2568394a6abSzhangyi (F) 
2578394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
2588394a6abSzhangyi (F) 					    sector_t block)
2598394a6abSzhangyi (F) {
2608394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, 0, 0);
2618394a6abSzhangyi (F) }
2628394a6abSzhangyi (F) 
2635df1d412Szhangyi (F) void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
2645df1d412Szhangyi (F) {
2655df1d412Szhangyi (F) 	struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
2665df1d412Szhangyi (F) 
2675df1d412Szhangyi (F) 	if (likely(bh)) {
2685df1d412Szhangyi (F) 		ext4_read_bh_lock(bh, REQ_RAHEAD, false);
2695df1d412Szhangyi (F) 		brelse(bh);
2705df1d412Szhangyi (F) 	}
271c197855eSStephen Hemminger }
272a9c47317SDarrick J. Wong 
273a9c47317SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
2749aa5d32bSDmitry Monakhov 				 struct ext4_super_block *es)
275a9c47317SDarrick J. Wong {
276a9c47317SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
277a9c47317SDarrick J. Wong 		return 1;
278a9c47317SDarrick J. Wong 
279a9c47317SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
28006db49e6STheodore Ts'o }
281a9c47317SDarrick J. Wong 
282bbc605cdSLukas Czerner __le32 ext4_superblock_csum(struct super_block *sb,
283a9c47317SDarrick J. Wong 			    struct ext4_super_block *es)
284a9c47317SDarrick J. Wong {
285a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
286a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
287a9c47317SDarrick J. Wong 	__u32 csum;
288a9c47317SDarrick J. Wong 
289a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
290a9c47317SDarrick J. Wong 
291a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
292a9c47317SDarrick J. Wong }
293a9c47317SDarrick J. Wong 
294a9c47317SDarrick J. Wong static int ext4_superblock_csum_verify(struct super_block *sb,
295a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
296a9c47317SDarrick J. Wong {
297a9c47317SDarrick J. Wong 	if (!ext4_has_metadata_csum(sb))
298a9c47317SDarrick J. Wong 		return 1;
299a9c47317SDarrick J. Wong 
300a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
301a9c47317SDarrick J. Wong }
302a9c47317SDarrick J. Wong 
303a9c47317SDarrick J. Wong void ext4_superblock_csum_set(struct super_block *sb)
304a9c47317SDarrick J. Wong {
30506db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
30606db49e6STheodore Ts'o 
3079aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
308a9c47317SDarrick J. Wong 		return;
309a9c47317SDarrick J. Wong 
310a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
311a9c47317SDarrick J. Wong }
312a9c47317SDarrick J. Wong 
3138fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
3148fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
315bd81d8eeSLaurent Vivier {
3163a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
3178fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3188fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
319bd81d8eeSLaurent Vivier }
320bd81d8eeSLaurent Vivier 
3218fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
3228fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
323bd81d8eeSLaurent Vivier {
3245272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
3258fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3268fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
327bd81d8eeSLaurent Vivier }
328bd81d8eeSLaurent Vivier 
3298fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
3308fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
331bd81d8eeSLaurent Vivier {
3325272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
3338fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3348fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
335bd81d8eeSLaurent Vivier }
336bd81d8eeSLaurent Vivier 
337021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
338560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
339560671a0SAneesh Kumar K.V {
340560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
341560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
342560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
343560671a0SAneesh Kumar K.V }
344560671a0SAneesh Kumar K.V 
345560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
346560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
347560671a0SAneesh Kumar K.V {
348560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
349560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
350560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
351560671a0SAneesh Kumar K.V }
352560671a0SAneesh Kumar K.V 
353560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
354560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
355560671a0SAneesh Kumar K.V {
356560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
357560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
358560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
359560671a0SAneesh Kumar K.V }
360560671a0SAneesh Kumar K.V 
361560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
362560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
363560671a0SAneesh Kumar K.V {
364560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
365560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
366560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
367560671a0SAneesh Kumar K.V }
368560671a0SAneesh Kumar K.V 
3698fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
3708fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
371bd81d8eeSLaurent Vivier {
3723a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
3738fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3748fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
375bd81d8eeSLaurent Vivier }
376bd81d8eeSLaurent Vivier 
3778fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
3788fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
379bd81d8eeSLaurent Vivier {
3805272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
3818fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3828fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
383bd81d8eeSLaurent Vivier }
384bd81d8eeSLaurent Vivier 
3858fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
3868fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
387bd81d8eeSLaurent Vivier {
3885272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
3898fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3908fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
391bd81d8eeSLaurent Vivier }
392bd81d8eeSLaurent Vivier 
393021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
394560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
395560671a0SAneesh Kumar K.V {
396560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
397560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
398560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
399560671a0SAneesh Kumar K.V }
400560671a0SAneesh Kumar K.V 
401560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
402560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
403560671a0SAneesh Kumar K.V {
404560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
405560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
406560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
407560671a0SAneesh Kumar K.V }
408560671a0SAneesh Kumar K.V 
409560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
410560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
411560671a0SAneesh Kumar K.V {
412560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
413560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
414560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
415560671a0SAneesh Kumar K.V }
416560671a0SAneesh Kumar K.V 
417560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
418560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
419560671a0SAneesh Kumar K.V {
420560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
421560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
422560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
423560671a0SAneesh Kumar K.V }
424560671a0SAneesh Kumar K.V 
425c92dc856SJan Kara static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
4266a0678a7SArnd Bergmann {
4276a0678a7SArnd Bergmann 	now = clamp_val(now, 0, (1ull << 40) - 1);
4286a0678a7SArnd Bergmann 
4296a0678a7SArnd Bergmann 	*lo = cpu_to_le32(lower_32_bits(now));
4306a0678a7SArnd Bergmann 	*hi = upper_32_bits(now);
4316a0678a7SArnd Bergmann }
4326a0678a7SArnd Bergmann 
4336a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
4346a0678a7SArnd Bergmann {
4356a0678a7SArnd Bergmann 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
4366a0678a7SArnd Bergmann }
4376a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \
438c92dc856SJan Kara 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
439c92dc856SJan Kara 			     ktime_get_real_seconds())
4406a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \
4416a0678a7SArnd Bergmann 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
442d3d1faf6SCurt Wohlgemuth 
443bdfe0cbdSTheodore Ts'o /*
444bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
445bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
446bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
447bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
448bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
449bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
450bdfe0cbdSTheodore Ts'o  */
451bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
452bdfe0cbdSTheodore Ts'o {
453bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
454bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
455bdfe0cbdSTheodore Ts'o 
456bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
457bdfe0cbdSTheodore Ts'o }
458bdfe0cbdSTheodore Ts'o 
45918aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
46018aadd47SBobi Jam {
46118aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
46218aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
46318aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
4645d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
46518aadd47SBobi Jam 
4665d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
467a0154344SDaeho Jeong 
468a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
469a0154344SDaeho Jeong 
47018aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
4715d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
4725d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
4735d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
47418aadd47SBobi Jam 		list_del_init(&jce->jce_list);
47518aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
47618aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
47718aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
47818aadd47SBobi Jam 	}
47918aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
48018aadd47SBobi Jam }
4811c13d5c0STheodore Ts'o 
482afb585a9SMauricio Faria de Oliveira /*
483afb585a9SMauricio Faria de Oliveira  * This writepage callback for write_cache_pages()
484afb585a9SMauricio Faria de Oliveira  * takes care of a few cases after page cleaning.
485afb585a9SMauricio Faria de Oliveira  *
486afb585a9SMauricio Faria de Oliveira  * write_cache_pages() already checks for dirty pages
487afb585a9SMauricio Faria de Oliveira  * and calls clear_page_dirty_for_io(), which we want,
488afb585a9SMauricio Faria de Oliveira  * to write protect the pages.
489afb585a9SMauricio Faria de Oliveira  *
490afb585a9SMauricio Faria de Oliveira  * However, we may have to redirty a page (see below.)
491afb585a9SMauricio Faria de Oliveira  */
492afb585a9SMauricio Faria de Oliveira static int ext4_journalled_writepage_callback(struct page *page,
493afb585a9SMauricio Faria de Oliveira 					      struct writeback_control *wbc,
494afb585a9SMauricio Faria de Oliveira 					      void *data)
495afb585a9SMauricio Faria de Oliveira {
496afb585a9SMauricio Faria de Oliveira 	transaction_t *transaction = (transaction_t *) data;
497afb585a9SMauricio Faria de Oliveira 	struct buffer_head *bh, *head;
498afb585a9SMauricio Faria de Oliveira 	struct journal_head *jh;
499afb585a9SMauricio Faria de Oliveira 
500afb585a9SMauricio Faria de Oliveira 	bh = head = page_buffers(page);
501afb585a9SMauricio Faria de Oliveira 	do {
502afb585a9SMauricio Faria de Oliveira 		/*
503afb585a9SMauricio Faria de Oliveira 		 * We have to redirty a page in these cases:
504afb585a9SMauricio Faria de Oliveira 		 * 1) If buffer is dirty, it means the page was dirty because it
505afb585a9SMauricio Faria de Oliveira 		 * contains a buffer that needs checkpointing. So the dirty bit
506afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that checkpointing writes the buffer
507afb585a9SMauricio Faria de Oliveira 		 * properly.
508afb585a9SMauricio Faria de Oliveira 		 * 2) If buffer is not part of the committing transaction
509afb585a9SMauricio Faria de Oliveira 		 * (we may have just accidentally come across this buffer because
510afb585a9SMauricio Faria de Oliveira 		 * inode range tracking is not exact) or if the currently running
511afb585a9SMauricio Faria de Oliveira 		 * transaction already contains this buffer as well, dirty bit
512afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that the buffer gets writeprotected
513afb585a9SMauricio Faria de Oliveira 		 * properly on running transaction's commit.
514afb585a9SMauricio Faria de Oliveira 		 */
515afb585a9SMauricio Faria de Oliveira 		jh = bh2jh(bh);
516afb585a9SMauricio Faria de Oliveira 		if (buffer_dirty(bh) ||
517afb585a9SMauricio Faria de Oliveira 		    (jh && (jh->b_transaction != transaction ||
518afb585a9SMauricio Faria de Oliveira 			    jh->b_next_transaction))) {
519afb585a9SMauricio Faria de Oliveira 			redirty_page_for_writepage(wbc, page);
520afb585a9SMauricio Faria de Oliveira 			goto out;
521afb585a9SMauricio Faria de Oliveira 		}
522afb585a9SMauricio Faria de Oliveira 	} while ((bh = bh->b_this_page) != head);
523afb585a9SMauricio Faria de Oliveira 
524afb585a9SMauricio Faria de Oliveira out:
525afb585a9SMauricio Faria de Oliveira 	return AOP_WRITEPAGE_ACTIVATE;
526afb585a9SMauricio Faria de Oliveira }
527afb585a9SMauricio Faria de Oliveira 
528afb585a9SMauricio Faria de Oliveira static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
529afb585a9SMauricio Faria de Oliveira {
530afb585a9SMauricio Faria de Oliveira 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
531afb585a9SMauricio Faria de Oliveira 	struct writeback_control wbc = {
532afb585a9SMauricio Faria de Oliveira 		.sync_mode =  WB_SYNC_ALL,
533afb585a9SMauricio Faria de Oliveira 		.nr_to_write = LONG_MAX,
534afb585a9SMauricio Faria de Oliveira 		.range_start = jinode->i_dirty_start,
535afb585a9SMauricio Faria de Oliveira 		.range_end = jinode->i_dirty_end,
536afb585a9SMauricio Faria de Oliveira         };
537afb585a9SMauricio Faria de Oliveira 
538afb585a9SMauricio Faria de Oliveira 	return write_cache_pages(mapping, &wbc,
539afb585a9SMauricio Faria de Oliveira 				 ext4_journalled_writepage_callback,
540afb585a9SMauricio Faria de Oliveira 				 jinode->i_transaction);
541afb585a9SMauricio Faria de Oliveira }
542afb585a9SMauricio Faria de Oliveira 
543afb585a9SMauricio Faria de Oliveira static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
544afb585a9SMauricio Faria de Oliveira {
545afb585a9SMauricio Faria de Oliveira 	int ret;
546afb585a9SMauricio Faria de Oliveira 
547afb585a9SMauricio Faria de Oliveira 	if (ext4_should_journal_data(jinode->i_vfs_inode))
548afb585a9SMauricio Faria de Oliveira 		ret = ext4_journalled_submit_inode_data_buffers(jinode);
549afb585a9SMauricio Faria de Oliveira 	else
550afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_submit_inode_data_buffers(jinode);
551afb585a9SMauricio Faria de Oliveira 
552afb585a9SMauricio Faria de Oliveira 	return ret;
553afb585a9SMauricio Faria de Oliveira }
554afb585a9SMauricio Faria de Oliveira 
555afb585a9SMauricio Faria de Oliveira static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
556afb585a9SMauricio Faria de Oliveira {
557afb585a9SMauricio Faria de Oliveira 	int ret = 0;
558afb585a9SMauricio Faria de Oliveira 
559afb585a9SMauricio Faria de Oliveira 	if (!ext4_should_journal_data(jinode->i_vfs_inode))
560afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_finish_inode_data_buffers(jinode);
561afb585a9SMauricio Faria de Oliveira 
562afb585a9SMauricio Faria de Oliveira 	return ret;
563afb585a9SMauricio Faria de Oliveira }
564afb585a9SMauricio Faria de Oliveira 
5651dc1097fSJan Kara static bool system_going_down(void)
5661dc1097fSJan Kara {
5671dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
5681dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
5691dc1097fSJan Kara }
5701dc1097fSJan Kara 
57102a7780eSJan Kara struct ext4_err_translation {
57202a7780eSJan Kara 	int code;
57302a7780eSJan Kara 	int errno;
57402a7780eSJan Kara };
57502a7780eSJan Kara 
57602a7780eSJan Kara #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
57702a7780eSJan Kara 
57802a7780eSJan Kara static struct ext4_err_translation err_translation[] = {
57902a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EIO),
58002a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOMEM),
58102a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFSBADCRC),
58202a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFSCORRUPTED),
58302a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOSPC),
58402a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOKEY),
58502a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EROFS),
58602a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFBIG),
58702a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EEXIST),
58802a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ERANGE),
58902a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EOVERFLOW),
59002a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EBUSY),
59102a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOTDIR),
59202a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOTEMPTY),
59302a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ESHUTDOWN),
59402a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFAULT),
59502a7780eSJan Kara };
59602a7780eSJan Kara 
59702a7780eSJan Kara static int ext4_errno_to_code(int errno)
59802a7780eSJan Kara {
59902a7780eSJan Kara 	int i;
60002a7780eSJan Kara 
60102a7780eSJan Kara 	for (i = 0; i < ARRAY_SIZE(err_translation); i++)
60202a7780eSJan Kara 		if (err_translation[i].errno == errno)
60302a7780eSJan Kara 			return err_translation[i].code;
60402a7780eSJan Kara 	return EXT4_ERR_UNKNOWN;
60502a7780eSJan Kara }
60602a7780eSJan Kara 
6072d01ddc8SJan Kara static void save_error_info(struct super_block *sb, int error,
60840676623SJan Kara 			    __u32 ino, __u64 block,
60940676623SJan Kara 			    const char *func, unsigned int line)
61040676623SJan Kara {
611c92dc856SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
61240676623SJan Kara 
61302a7780eSJan Kara 	/* We default to EFSCORRUPTED error... */
61402a7780eSJan Kara 	if (error == 0)
61502a7780eSJan Kara 		error = EFSCORRUPTED;
616c92dc856SJan Kara 
617c92dc856SJan Kara 	spin_lock(&sbi->s_error_lock);
618c92dc856SJan Kara 	sbi->s_add_error_count++;
619c92dc856SJan Kara 	sbi->s_last_error_code = error;
620c92dc856SJan Kara 	sbi->s_last_error_line = line;
621c92dc856SJan Kara 	sbi->s_last_error_ino = ino;
622c92dc856SJan Kara 	sbi->s_last_error_block = block;
623c92dc856SJan Kara 	sbi->s_last_error_func = func;
624c92dc856SJan Kara 	sbi->s_last_error_time = ktime_get_real_seconds();
625c92dc856SJan Kara 	if (!sbi->s_first_error_time) {
626c92dc856SJan Kara 		sbi->s_first_error_code = error;
627c92dc856SJan Kara 		sbi->s_first_error_line = line;
628c92dc856SJan Kara 		sbi->s_first_error_ino = ino;
629c92dc856SJan Kara 		sbi->s_first_error_block = block;
630c92dc856SJan Kara 		sbi->s_first_error_func = func;
631c92dc856SJan Kara 		sbi->s_first_error_time = sbi->s_last_error_time;
63240676623SJan Kara 	}
633c92dc856SJan Kara 	spin_unlock(&sbi->s_error_lock);
63440676623SJan Kara }
63540676623SJan Kara 
636ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
637ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
638ac27a0ecSDave Kleikamp  *
639ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
640617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
641ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
642ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
643ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
644ac27a0ecSDave Kleikamp  * write out the superblock safely.
645ac27a0ecSDave Kleikamp  *
646dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
647d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
648ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
649014c9caaSJan Kara  *
650014c9caaSJan Kara  * If force_ro is set, we unconditionally force the filesystem into an
651014c9caaSJan Kara  * ABORT|READONLY state, unless the error response on the fs has been set to
652014c9caaSJan Kara  * panic in which case we take the easy way out and panic immediately. This is
653014c9caaSJan Kara  * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
654014c9caaSJan Kara  * at a critical moment in log management.
655ac27a0ecSDave Kleikamp  */
656e789ca0cSJan Kara static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
657e789ca0cSJan Kara 			      __u32 ino, __u64 block,
658e789ca0cSJan Kara 			      const char *func, unsigned int line)
659ac27a0ecSDave Kleikamp {
660b08070ecSJan Kara 	journal_t *journal = EXT4_SB(sb)->s_journal;
6612d01ddc8SJan Kara 	bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
662b08070ecSJan Kara 
663e789ca0cSJan Kara 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
664327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
665327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
666327eaf73STheodore Ts'o 
6672d01ddc8SJan Kara 	if (!continue_fs && !sb_rdonly(sb)) {
6689b5f6c9bSHarshad Shirwadkar 		ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
669ac27a0ecSDave Kleikamp 		if (journal)
670dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
6712d01ddc8SJan Kara 	}
6722d01ddc8SJan Kara 
6732d01ddc8SJan Kara 	if (!bdev_read_only(sb->s_bdev)) {
6742d01ddc8SJan Kara 		save_error_info(sb, error, ino, block, func, line);
6752d01ddc8SJan Kara 		/*
6762d01ddc8SJan Kara 		 * In case the fs should keep running, we need to writeout
6772d01ddc8SJan Kara 		 * superblock through the journal. Due to lock ordering
6782d01ddc8SJan Kara 		 * constraints, it may not be safe to do it right here so we
6792d01ddc8SJan Kara 		 * defer superblock flushing to a workqueue.
6802d01ddc8SJan Kara 		 */
681bb9464e0Syangerkun 		if (continue_fs && journal)
6822d01ddc8SJan Kara 			schedule_work(&EXT4_SB(sb)->s_error_work);
6832d01ddc8SJan Kara 		else
6842d01ddc8SJan Kara 			ext4_commit_super(sb);
6852d01ddc8SJan Kara 	}
6862d01ddc8SJan Kara 
6871dc1097fSJan Kara 	/*
6881dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
6891dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
6901dc1097fSJan Kara 	 * disabled.
6911dc1097fSJan Kara 	 */
692014c9caaSJan Kara 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
693617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
694ac27a0ecSDave Kleikamp 			sb->s_id);
695ac27a0ecSDave Kleikamp 	}
696ac2f7ca5SYe Bin 
697ac2f7ca5SYe Bin 	if (sb_rdonly(sb) || continue_fs)
698ac2f7ca5SYe Bin 		return;
699ac2f7ca5SYe Bin 
700014c9caaSJan Kara 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
701014c9caaSJan Kara 	/*
702014c9caaSJan Kara 	 * Make sure updated value of ->s_mount_flags will be visible before
703014c9caaSJan Kara 	 * ->s_flags update
704014c9caaSJan Kara 	 */
705014c9caaSJan Kara 	smp_wmb();
706014c9caaSJan Kara 	sb->s_flags |= SB_RDONLY;
7074327ba52SDaeho Jeong }
708ac27a0ecSDave Kleikamp 
709c92dc856SJan Kara static void flush_stashed_error_work(struct work_struct *work)
710c92dc856SJan Kara {
711c92dc856SJan Kara 	struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
712c92dc856SJan Kara 						s_error_work);
7132d01ddc8SJan Kara 	journal_t *journal = sbi->s_journal;
7142d01ddc8SJan Kara 	handle_t *handle;
715c92dc856SJan Kara 
7162d01ddc8SJan Kara 	/*
7172d01ddc8SJan Kara 	 * If the journal is still running, we have to write out superblock
7182d01ddc8SJan Kara 	 * through the journal to avoid collisions of other journalled sb
7192d01ddc8SJan Kara 	 * updates.
7202d01ddc8SJan Kara 	 *
7212d01ddc8SJan Kara 	 * We use directly jbd2 functions here to avoid recursing back into
7222d01ddc8SJan Kara 	 * ext4 error handling code during handling of previous errors.
7232d01ddc8SJan Kara 	 */
7242d01ddc8SJan Kara 	if (!sb_rdonly(sbi->s_sb) && journal) {
725558d6450SYe Bin 		struct buffer_head *sbh = sbi->s_sbh;
7262d01ddc8SJan Kara 		handle = jbd2_journal_start(journal, 1);
7272d01ddc8SJan Kara 		if (IS_ERR(handle))
7282d01ddc8SJan Kara 			goto write_directly;
729558d6450SYe Bin 		if (jbd2_journal_get_write_access(handle, sbh)) {
7302d01ddc8SJan Kara 			jbd2_journal_stop(handle);
7312d01ddc8SJan Kara 			goto write_directly;
7322d01ddc8SJan Kara 		}
7332d01ddc8SJan Kara 		ext4_update_super(sbi->s_sb);
734558d6450SYe Bin 		if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
735558d6450SYe Bin 			ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
736558d6450SYe Bin 				 "superblock detected");
737558d6450SYe Bin 			clear_buffer_write_io_error(sbh);
738558d6450SYe Bin 			set_buffer_uptodate(sbh);
739558d6450SYe Bin 		}
740558d6450SYe Bin 
741558d6450SYe Bin 		if (jbd2_journal_dirty_metadata(handle, sbh)) {
7422d01ddc8SJan Kara 			jbd2_journal_stop(handle);
7432d01ddc8SJan Kara 			goto write_directly;
7442d01ddc8SJan Kara 		}
7452d01ddc8SJan Kara 		jbd2_journal_stop(handle);
746d578b994SJonathan Davies 		ext4_notify_error_sysfs(sbi);
7472d01ddc8SJan Kara 		return;
7482d01ddc8SJan Kara 	}
7492d01ddc8SJan Kara write_directly:
7502d01ddc8SJan Kara 	/*
7512d01ddc8SJan Kara 	 * Write through journal failed. Write sb directly to get error info
7522d01ddc8SJan Kara 	 * out and hope for the best.
7532d01ddc8SJan Kara 	 */
7544392fbc4SJan Kara 	ext4_commit_super(sbi->s_sb);
755d578b994SJonathan Davies 	ext4_notify_error_sysfs(sbi);
756ac27a0ecSDave Kleikamp }
757ac27a0ecSDave Kleikamp 
758efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
759efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
760efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
761efbed4dcSTheodore Ts'o 
76212062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
763014c9caaSJan Kara 		  unsigned int line, bool force_ro, int error, __u64 block,
76454d3adbcSTheodore Ts'o 		  const char *fmt, ...)
765ac27a0ecSDave Kleikamp {
7660ff2ea7dSJoe Perches 	struct va_format vaf;
767ac27a0ecSDave Kleikamp 	va_list args;
768ac27a0ecSDave Kleikamp 
7690db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7700db1ff22STheodore Ts'o 		return;
7710db1ff22STheodore Ts'o 
772ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
773efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
774ac27a0ecSDave Kleikamp 		va_start(args, fmt);
7750ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7760ff2ea7dSJoe Perches 		vaf.va = &args;
777efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
778efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
7790ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
780ac27a0ecSDave Kleikamp 		va_end(args);
781efbed4dcSTheodore Ts'o 	}
7829a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(sb, NULL, error ? error : EFSCORRUPTED);
7839a089b21SGabriel Krisman Bertazi 
784e789ca0cSJan Kara 	ext4_handle_error(sb, force_ro, error, 0, block, function, line);
785ac27a0ecSDave Kleikamp }
786ac27a0ecSDave Kleikamp 
787e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
78854d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
789273df556SFrank Mayhar 			const char *fmt, ...)
790273df556SFrank Mayhar {
791273df556SFrank Mayhar 	va_list args;
792f7c21177STheodore Ts'o 	struct va_format vaf;
793273df556SFrank Mayhar 
7940db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
7950db1ff22STheodore Ts'o 		return;
7960db1ff22STheodore Ts'o 
797ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
798efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
799273df556SFrank Mayhar 		va_start(args, fmt);
800f7c21177STheodore Ts'o 		vaf.fmt = fmt;
801f7c21177STheodore Ts'o 		vaf.va = &args;
802c398eda0STheodore Ts'o 		if (block)
803d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
804d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
805d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
806d9ee81daSJoe Perches 			       block, current->comm, &vaf);
807d9ee81daSJoe Perches 		else
808d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
809d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
810d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
811d9ee81daSJoe Perches 			       current->comm, &vaf);
812273df556SFrank Mayhar 		va_end(args);
813efbed4dcSTheodore Ts'o 	}
8149a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(inode->i_sb, inode, error ? error : EFSCORRUPTED);
8159a089b21SGabriel Krisman Bertazi 
816e789ca0cSJan Kara 	ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
81754d3adbcSTheodore Ts'o 			  function, line);
818273df556SFrank Mayhar }
819273df556SFrank Mayhar 
820e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
821f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
822f7c21177STheodore Ts'o 		       const char *fmt, ...)
823273df556SFrank Mayhar {
824273df556SFrank Mayhar 	va_list args;
825f7c21177STheodore Ts'o 	struct va_format vaf;
826496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
827273df556SFrank Mayhar 	char pathname[80], *path;
828273df556SFrank Mayhar 
8290db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
8300db1ff22STheodore Ts'o 		return;
8310db1ff22STheodore Ts'o 
832ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
833efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
8349bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
835f9a62d09SDan Carpenter 		if (IS_ERR(path))
836273df556SFrank Mayhar 			path = "(unknown)";
837f7c21177STheodore Ts'o 		va_start(args, fmt);
838f7c21177STheodore Ts'o 		vaf.fmt = fmt;
839f7c21177STheodore Ts'o 		vaf.va = &args;
840d9ee81daSJoe Perches 		if (block)
841d9ee81daSJoe Perches 			printk(KERN_CRIT
842d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
843d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
844d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
845d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
846d9ee81daSJoe Perches 		else
847d9ee81daSJoe Perches 			printk(KERN_CRIT
848d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
849d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
850d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
851d9ee81daSJoe Perches 			       current->comm, path, &vaf);
852273df556SFrank Mayhar 		va_end(args);
853efbed4dcSTheodore Ts'o 	}
8549a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(inode->i_sb, inode, EFSCORRUPTED);
8559a089b21SGabriel Krisman Bertazi 
856e789ca0cSJan Kara 	ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
85754d3adbcSTheodore Ts'o 			  function, line);
858273df556SFrank Mayhar }
859273df556SFrank Mayhar 
860722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
861ac27a0ecSDave Kleikamp 			      char nbuf[16])
862ac27a0ecSDave Kleikamp {
863ac27a0ecSDave Kleikamp 	char *errstr = NULL;
864ac27a0ecSDave Kleikamp 
865ac27a0ecSDave Kleikamp 	switch (errno) {
8666a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
8676a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
8686a797d27SDarrick J. Wong 		break;
8696a797d27SDarrick J. Wong 	case -EFSBADCRC:
8706a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
8716a797d27SDarrick J. Wong 		break;
872ac27a0ecSDave Kleikamp 	case -EIO:
873ac27a0ecSDave Kleikamp 		errstr = "IO failure";
874ac27a0ecSDave Kleikamp 		break;
875ac27a0ecSDave Kleikamp 	case -ENOMEM:
876ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
877ac27a0ecSDave Kleikamp 		break;
878ac27a0ecSDave Kleikamp 	case -EROFS:
87978f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
88078f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
881ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
882ac27a0ecSDave Kleikamp 		else
883ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
884ac27a0ecSDave Kleikamp 		break;
885ac27a0ecSDave Kleikamp 	default:
886ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
887ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
888ac27a0ecSDave Kleikamp 		 * NULL. */
889ac27a0ecSDave Kleikamp 		if (nbuf) {
890ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
891ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
892ac27a0ecSDave Kleikamp 				errstr = nbuf;
893ac27a0ecSDave Kleikamp 		}
894ac27a0ecSDave Kleikamp 		break;
895ac27a0ecSDave Kleikamp 	}
896ac27a0ecSDave Kleikamp 
897ac27a0ecSDave Kleikamp 	return errstr;
898ac27a0ecSDave Kleikamp }
899ac27a0ecSDave Kleikamp 
900617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
901ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
902ac27a0ecSDave Kleikamp 
903c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
904c398eda0STheodore Ts'o 		      unsigned int line, int errno)
905ac27a0ecSDave Kleikamp {
906ac27a0ecSDave Kleikamp 	char nbuf[16];
907ac27a0ecSDave Kleikamp 	const char *errstr;
908ac27a0ecSDave Kleikamp 
9090db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
9100db1ff22STheodore Ts'o 		return;
9110db1ff22STheodore Ts'o 
912ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
913ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
914ac27a0ecSDave Kleikamp 	 * an error. */
915bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
916ac27a0ecSDave Kleikamp 		return;
917ac27a0ecSDave Kleikamp 
918efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
919617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
920c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
921c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
922efbed4dcSTheodore Ts'o 	}
9239a089b21SGabriel Krisman Bertazi 	fsnotify_sb_error(sb, NULL, errno ? errno : EFSCORRUPTED);
924ac27a0ecSDave Kleikamp 
925e789ca0cSJan Kara 	ext4_handle_error(sb, false, -errno, 0, 0, function, line);
926ac27a0ecSDave Kleikamp }
927ac27a0ecSDave Kleikamp 
928e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
929e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
930b31e1552SEric Sandeen {
9310ff2ea7dSJoe Perches 	struct va_format vaf;
932b31e1552SEric Sandeen 	va_list args;
933b31e1552SEric Sandeen 
934da812f61SLukas Czerner 	if (sb) {
9351cf006edSDmitry Monakhov 		atomic_inc(&EXT4_SB(sb)->s_msg_count);
936da812f61SLukas Czerner 		if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state),
937da812f61SLukas Czerner 				  "EXT4-fs"))
938efbed4dcSTheodore Ts'o 			return;
939da812f61SLukas Czerner 	}
940efbed4dcSTheodore Ts'o 
941b31e1552SEric Sandeen 	va_start(args, fmt);
9420ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9430ff2ea7dSJoe Perches 	vaf.va = &args;
944da812f61SLukas Czerner 	if (sb)
9450ff2ea7dSJoe Perches 		printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
946da812f61SLukas Czerner 	else
947da812f61SLukas Czerner 		printk("%sEXT4-fs: %pV\n", prefix, &vaf);
948b31e1552SEric Sandeen 	va_end(args);
949b31e1552SEric Sandeen }
950b31e1552SEric Sandeen 
9511cf006edSDmitry Monakhov static int ext4_warning_ratelimit(struct super_block *sb)
9521cf006edSDmitry Monakhov {
9531cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_warning_count);
9541cf006edSDmitry Monakhov 	return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
9551cf006edSDmitry Monakhov 			    "EXT4-fs warning");
9561cf006edSDmitry Monakhov }
957b03a2f7eSAndreas Dilger 
95812062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
959c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
960ac27a0ecSDave Kleikamp {
9610ff2ea7dSJoe Perches 	struct va_format vaf;
962ac27a0ecSDave Kleikamp 	va_list args;
963ac27a0ecSDave Kleikamp 
964b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
965efbed4dcSTheodore Ts'o 		return;
966efbed4dcSTheodore Ts'o 
967ac27a0ecSDave Kleikamp 	va_start(args, fmt);
9680ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9690ff2ea7dSJoe Perches 	vaf.va = &args;
9700ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
9710ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
972ac27a0ecSDave Kleikamp 	va_end(args);
973ac27a0ecSDave Kleikamp }
974ac27a0ecSDave Kleikamp 
975b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
976b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
977b03a2f7eSAndreas Dilger {
978b03a2f7eSAndreas Dilger 	struct va_format vaf;
979b03a2f7eSAndreas Dilger 	va_list args;
980b03a2f7eSAndreas Dilger 
981b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
982b03a2f7eSAndreas Dilger 		return;
983b03a2f7eSAndreas Dilger 
984b03a2f7eSAndreas Dilger 	va_start(args, fmt);
985b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
986b03a2f7eSAndreas Dilger 	vaf.va = &args;
987b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
988b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
989b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
990b03a2f7eSAndreas Dilger 	va_end(args);
991b03a2f7eSAndreas Dilger }
992b03a2f7eSAndreas Dilger 
993e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
994e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
995e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
996e29136f8STheodore Ts'o 			     const char *fmt, ...)
9975d1b1b3fSAneesh Kumar K.V __releases(bitlock)
9985d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
9995d1b1b3fSAneesh Kumar K.V {
10000ff2ea7dSJoe Perches 	struct va_format vaf;
10015d1b1b3fSAneesh Kumar K.V 	va_list args;
10025d1b1b3fSAneesh Kumar K.V 
10030db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
10040db1ff22STheodore Ts'o 		return;
10050db1ff22STheodore Ts'o 
1006ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
1007efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
10085d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
10090ff2ea7dSJoe Perches 		vaf.fmt = fmt;
10100ff2ea7dSJoe Perches 		vaf.va = &args;
101121149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
1012e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
1013e29136f8STheodore Ts'o 		if (ino)
10140ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
1015e29136f8STheodore Ts'o 		if (block)
1016efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
1017efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
10180ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
10195d1b1b3fSAneesh Kumar K.V 		va_end(args);
1020efbed4dcSTheodore Ts'o 	}
10215d1b1b3fSAneesh Kumar K.V 
1022c92dc856SJan Kara 	if (test_opt(sb, ERRORS_CONT)) {
1023327eaf73STheodore Ts'o 		if (test_opt(sb, WARN_ON_ERROR))
1024327eaf73STheodore Ts'o 			WARN_ON_ONCE(1);
1025e789ca0cSJan Kara 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
10262d01ddc8SJan Kara 		if (!bdev_read_only(sb->s_bdev)) {
10272d01ddc8SJan Kara 			save_error_info(sb, EFSCORRUPTED, ino, block, function,
10282d01ddc8SJan Kara 					line);
1029c92dc856SJan Kara 			schedule_work(&EXT4_SB(sb)->s_error_work);
10302d01ddc8SJan Kara 		}
10315d1b1b3fSAneesh Kumar K.V 		return;
10325d1b1b3fSAneesh Kumar K.V 	}
10335d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
1034e789ca0cSJan Kara 	ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
10355d1b1b3fSAneesh Kumar K.V 	/*
10365d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
10375d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
10385d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
10395d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
10405d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
104125985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
10425d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
10435d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
10445d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
10455d1b1b3fSAneesh Kumar K.V 	 */
10465d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
10475d1b1b3fSAneesh Kumar K.V 	return;
10485d1b1b3fSAneesh Kumar K.V }
10495d1b1b3fSAneesh Kumar K.V 
1050db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1051db79e6d1SWang Shilong 				     ext4_group_t group,
1052db79e6d1SWang Shilong 				     unsigned int flags)
1053db79e6d1SWang Shilong {
1054db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1055db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1056db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
10579af0b3d1SWang Shilong 	int ret;
1058db79e6d1SWang Shilong 
10599af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
10609af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
10619af0b3d1SWang Shilong 					    &grp->bb_state);
10629af0b3d1SWang Shilong 		if (!ret)
1063db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
1064db79e6d1SWang Shilong 					   grp->bb_free);
1065db79e6d1SWang Shilong 	}
1066db79e6d1SWang Shilong 
10679af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
10689af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
10699af0b3d1SWang Shilong 					    &grp->bb_state);
10709af0b3d1SWang Shilong 		if (!ret && gdp) {
1071db79e6d1SWang Shilong 			int count;
1072db79e6d1SWang Shilong 
1073db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
1074db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1075db79e6d1SWang Shilong 					   count);
1076db79e6d1SWang Shilong 		}
1077db79e6d1SWang Shilong 	}
1078db79e6d1SWang Shilong }
1079db79e6d1SWang Shilong 
1080617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
1081ac27a0ecSDave Kleikamp {
1082617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1083ac27a0ecSDave Kleikamp 
1084617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1085ac27a0ecSDave Kleikamp 		return;
1086ac27a0ecSDave Kleikamp 
108712062dddSEric Sandeen 	ext4_warning(sb,
1088ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
1089ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
1090617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
1091ac27a0ecSDave Kleikamp 
1092617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1093617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1094617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1095ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
1096ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
1097ac27a0ecSDave Kleikamp 
1098ac27a0ecSDave Kleikamp 	/*
1099ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
1100ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
1101ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
1102ac27a0ecSDave Kleikamp 	 */
1103ac27a0ecSDave Kleikamp }
1104ac27a0ecSDave Kleikamp 
1105ac27a0ecSDave Kleikamp /*
1106ac27a0ecSDave Kleikamp  * Open the external journal device
1107ac27a0ecSDave Kleikamp  */
1108b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1109ac27a0ecSDave Kleikamp {
1110ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1111ac27a0ecSDave Kleikamp 
1112d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1113ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
1114ac27a0ecSDave Kleikamp 		goto fail;
1115ac27a0ecSDave Kleikamp 	return bdev;
1116ac27a0ecSDave Kleikamp 
1117ac27a0ecSDave Kleikamp fail:
1118ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
1119ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
1120ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1121ac27a0ecSDave Kleikamp 	return NULL;
1122ac27a0ecSDave Kleikamp }
1123ac27a0ecSDave Kleikamp 
1124ac27a0ecSDave Kleikamp /*
1125ac27a0ecSDave Kleikamp  * Release the journal device
1126ac27a0ecSDave Kleikamp  */
11274385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
1128ac27a0ecSDave Kleikamp {
11294385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1130ac27a0ecSDave Kleikamp }
1131ac27a0ecSDave Kleikamp 
11324385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1133ac27a0ecSDave Kleikamp {
1134ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1135ee7ed3aaSChunguang Xu 	bdev = sbi->s_journal_bdev;
1136ac27a0ecSDave Kleikamp 	if (bdev) {
11374385bab1SAl Viro 		ext4_blkdev_put(bdev);
1138ee7ed3aaSChunguang Xu 		sbi->s_journal_bdev = NULL;
1139ac27a0ecSDave Kleikamp 	}
1140ac27a0ecSDave Kleikamp }
1141ac27a0ecSDave Kleikamp 
1142ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
1143ac27a0ecSDave Kleikamp {
1144617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1145ac27a0ecSDave Kleikamp }
1146ac27a0ecSDave Kleikamp 
1147617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1148ac27a0ecSDave Kleikamp {
1149ac27a0ecSDave Kleikamp 	struct list_head *l;
1150ac27a0ecSDave Kleikamp 
1151b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1152ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
1153ac27a0ecSDave Kleikamp 
1154ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
1155ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
1156ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
1157ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
1158ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1159ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
1160ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
1161ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1162ac27a0ecSDave Kleikamp 	}
1163ac27a0ecSDave Kleikamp }
1164ac27a0ecSDave Kleikamp 
1165957153fcSJan Kara #ifdef CONFIG_QUOTA
1166957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
1167957153fcSJan Kara 
1168957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1169957153fcSJan Kara {
1170957153fcSJan Kara 	int type;
1171957153fcSJan Kara 
1172957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
1173957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
1174957153fcSJan Kara 		ext4_quota_off(sb, type);
1175957153fcSJan Kara }
117633458eabSTheodore Ts'o 
117733458eabSTheodore Ts'o /*
117833458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
117933458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
118033458eabSTheodore Ts'o  */
118133458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
118233458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
118333458eabSTheodore Ts'o 				int type)
118433458eabSTheodore Ts'o {
118533458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
118633458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
118733458eabSTheodore Ts'o }
1188957153fcSJan Kara #else
1189957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1190957153fcSJan Kara {
1191957153fcSJan Kara }
1192957153fcSJan Kara #endif
1193957153fcSJan Kara 
1194617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1195ac27a0ecSDave Kleikamp {
1196617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1197617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
11981d0c3924STheodore Ts'o 	struct buffer_head **group_desc;
11997c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
120097abd7d4STheodore Ts'o 	int aborted = 0;
1201ef2cabf7SHidehiro Kawai 	int i, err;
1202ac27a0ecSDave Kleikamp 
1203857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1204957153fcSJan Kara 	ext4_quota_off_umount(sb);
1205e0ccfd95SChristoph Hellwig 
1206c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
12072e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
120802f310fcSJan Kara 	ext4_release_orphan_info(sb);
12094c0425ffSMingming Cao 
12105e47868fSRitesh Harjani 	/*
12115e47868fSRitesh Harjani 	 * Unregister sysfs before destroying jbd2 journal.
12125e47868fSRitesh Harjani 	 * Since we could still access attr_journal_task attribute via sysfs
12135e47868fSRitesh Harjani 	 * path which could have sbi->s_journal->j_task as NULL
12145e47868fSRitesh Harjani 	 */
12155e47868fSRitesh Harjani 	ext4_unregister_sysfs(sb);
12165e47868fSRitesh Harjani 
12170390131bSFrank Mayhar 	if (sbi->s_journal) {
121897abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1219ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
122047b4a50bSJan Kara 		sbi->s_journal = NULL;
1221878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
122254d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
12230390131bSFrank Mayhar 		}
1224878520acSTheodore Ts'o 	}
1225d4edac31SJosef Bacik 
1226d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
12279105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1228d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1229d4edac31SJosef Bacik 	ext4_mb_release(sb);
1230d4edac31SJosef Bacik 	ext4_ext_release(sb);
1231d4edac31SJosef Bacik 
1232bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1233e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
123402f310fcSJan Kara 		ext4_clear_feature_orphan_present(sb);
1235ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1236ac27a0ecSDave Kleikamp 	}
1237bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
12384392fbc4SJan Kara 		ext4_commit_super(sb);
1239a8e25a83SArtem Bityutskiy 
12401d0c3924STheodore Ts'o 	rcu_read_lock();
12411d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
1242ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
12431d0c3924STheodore Ts'o 		brelse(group_desc[i]);
12441d0c3924STheodore Ts'o 	kvfree(group_desc);
12457c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
12467c990728SSuraj Jitindar Singh 	if (flex_groups) {
12477c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
12487c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
12497c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
12507c990728SSuraj Jitindar Singh 	}
12511d0c3924STheodore Ts'o 	rcu_read_unlock();
125257042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1253ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1254ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
125557042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1256efc61345SEric Whitney 	percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
1257bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1258ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1259a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
126033458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1261ac27a0ecSDave Kleikamp #endif
1262ac27a0ecSDave Kleikamp 
1263ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1264ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1265ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1266ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1267ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1268ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1269837c23fbSChunguang Xu 	ASSERT(list_empty(&sbi->s_orphan));
1270ac27a0ecSDave Kleikamp 
127189d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1272f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1273ee7ed3aaSChunguang Xu 	if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
1274ac27a0ecSDave Kleikamp 		/*
1275ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1276ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1277ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1278ac27a0ecSDave Kleikamp 		 */
1279ee7ed3aaSChunguang Xu 		sync_blockdev(sbi->s_journal_bdev);
1280ee7ed3aaSChunguang Xu 		invalidate_bdev(sbi->s_journal_bdev);
1281617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1282ac27a0ecSDave Kleikamp 	}
128350c15df6SChengguang Xu 
1284dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1285dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
128650c15df6SChengguang Xu 
128747387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
128847387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
128950c15df6SChengguang Xu 
1290618f0031SPavel Skripkin 	ext4_stop_mmpd(sbi);
1291618f0031SPavel Skripkin 
12929060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1293ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
12943197ebdbSTheodore Ts'o 	/*
12953197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
12963197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
12973197ebdbSTheodore Ts'o 	 */
12983197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
12993197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
13000441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
13010441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1302705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
13035e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1304ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
1305c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1306f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
1307c83ad55eSGabriel Krisman Bertazi #endif
1308ac27a0ecSDave Kleikamp 	kfree(sbi);
1309ac27a0ecSDave Kleikamp }
1310ac27a0ecSDave Kleikamp 
1311e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1312ac27a0ecSDave Kleikamp 
1313ac27a0ecSDave Kleikamp /*
1314ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1315ac27a0ecSDave Kleikamp  */
1316617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1317ac27a0ecSDave Kleikamp {
1318617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1319ac27a0ecSDave Kleikamp 
1320e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1321ac27a0ecSDave Kleikamp 	if (!ei)
1322ac27a0ecSDave Kleikamp 		return NULL;
13230b8e58a1SAndreas Dilger 
1324ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1325202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1326c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
132727bc446eSbrookxu 	atomic_set(&ei->i_prealloc_active, 0);
1328c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
13299a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
13309a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1331edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1332eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1333edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1334dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1335d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1336d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
13371dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1338a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1339a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
134096c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1341a9e7f447SDmitry Monakhov #endif
13428aefcd55STheodore Ts'o 	ei->jinode = NULL;
13432e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1344744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1345b436b9beSJan Kara 	ei->i_sync_tid = 0;
1346b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1347e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
13482e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1349aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1350aa75f4d3SHarshad Shirwadkar 	mutex_init(&ei->i_fc_lock);
1351ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1352ac27a0ecSDave Kleikamp }
1353ac27a0ecSDave Kleikamp 
13547ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
13557ff9c073STheodore Ts'o {
13567ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
13577ff9c073STheodore Ts'o 
135829b3692eSEric Biggers 	if (!drop)
135929b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
136029b3692eSEric Biggers 
13617ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
13627ff9c073STheodore Ts'o 	return drop;
13637ff9c073STheodore Ts'o }
13647ff9c073STheodore Ts'o 
136594053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1366fa0d7e3dSNick Piggin {
13672c58d548SEric Biggers 	fscrypt_free_inode(inode);
1368aa75f4d3SHarshad Shirwadkar 	if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1369aa75f4d3SHarshad Shirwadkar 		pr_warn("%s: inode %ld still in fc list",
1370aa75f4d3SHarshad Shirwadkar 			__func__, inode->i_ino);
1371aa75f4d3SHarshad Shirwadkar 	}
1372fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1373fa0d7e3dSNick Piggin }
1374fa0d7e3dSNick Piggin 
1375617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1376ac27a0ecSDave Kleikamp {
13779f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1378b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1379b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1380b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
13819f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
13829f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
13839f7dd93dSVasily Averin 				true);
13849f7dd93dSVasily Averin 		dump_stack();
13859f7dd93dSVasily Averin 	}
13866fed8395SJeffle Xu 
13876fed8395SJeffle Xu 	if (EXT4_I(inode)->i_reserved_data_blocks)
13886fed8395SJeffle Xu 		ext4_msg(inode->i_sb, KERN_ERR,
13896fed8395SJeffle Xu 			 "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
13906fed8395SJeffle Xu 			 inode->i_ino, EXT4_I(inode),
13916fed8395SJeffle Xu 			 EXT4_I(inode)->i_reserved_data_blocks);
1392ac27a0ecSDave Kleikamp }
1393ac27a0ecSDave Kleikamp 
139451cc5068SAlexey Dobriyan static void init_once(void *foo)
1395ac27a0ecSDave Kleikamp {
1396617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1397ac27a0ecSDave Kleikamp 
1398ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1399ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
14000e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1401ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1402aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1403ac27a0ecSDave Kleikamp }
1404ac27a0ecSDave Kleikamp 
1405e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1406ac27a0ecSDave Kleikamp {
1407f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1408f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1409f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1410f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1411f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1412f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
141320c2df83SPaul Mundt 				init_once);
1414617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1415ac27a0ecSDave Kleikamp 		return -ENOMEM;
1416ac27a0ecSDave Kleikamp 	return 0;
1417ac27a0ecSDave Kleikamp }
1418ac27a0ecSDave Kleikamp 
1419ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1420ac27a0ecSDave Kleikamp {
14218c0a8537SKirill A. Shutemov 	/*
14228c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
14238c0a8537SKirill A. Shutemov 	 * destroy cache.
14248c0a8537SKirill A. Shutemov 	 */
14258c0a8537SKirill A. Shutemov 	rcu_barrier();
1426617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1427ac27a0ecSDave Kleikamp }
1428ac27a0ecSDave Kleikamp 
14290930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1430ac27a0ecSDave Kleikamp {
1431aa75f4d3SHarshad Shirwadkar 	ext4_fc_del(inode);
14320930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1433dbd5768fSJan Kara 	clear_inode(inode);
143427bc446eSbrookxu 	ext4_discard_preallocations(inode, 0);
143551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1436f4c2d372SJan Kara 	dquot_drop(inode);
14378aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
14388aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
14398aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
14408aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
14418aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
14428aefcd55STheodore Ts'o 	}
14433d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1444c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1445ac27a0ecSDave Kleikamp }
1446ac27a0ecSDave Kleikamp 
14471b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
14481b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1449ac27a0ecSDave Kleikamp {
1450ac27a0ecSDave Kleikamp 	struct inode *inode;
1451ac27a0ecSDave Kleikamp 
14528a363970STheodore Ts'o 	/*
1453ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1454ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1455ac27a0ecSDave Kleikamp 	 */
14568a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
14571d1fe1eeSDavid Howells 	if (IS_ERR(inode))
14581d1fe1eeSDavid Howells 		return ERR_CAST(inode);
14591d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1460ac27a0ecSDave Kleikamp 		iput(inode);
1461ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1462ac27a0ecSDave Kleikamp 	}
14631b961ac0SChristoph Hellwig 
14641b961ac0SChristoph Hellwig 	return inode;
1465ac27a0ecSDave Kleikamp }
14661b961ac0SChristoph Hellwig 
14671b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
14681b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
14691b961ac0SChristoph Hellwig {
14701b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
14711b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
14721b961ac0SChristoph Hellwig }
14731b961ac0SChristoph Hellwig 
14741b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
14751b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
14761b961ac0SChristoph Hellwig {
14771b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
14781b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1479ac27a0ecSDave Kleikamp }
1480ac27a0ecSDave Kleikamp 
1481fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1482fde87268STheodore Ts'o {
1483fde87268STheodore Ts'o 	struct writeback_control wbc = {
1484fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1485fde87268STheodore Ts'o 	};
1486fde87268STheodore Ts'o 
1487fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1488fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1489fde87268STheodore Ts'o }
1490fde87268STheodore Ts'o 
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,
1608b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1609b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1610a7550b30SJaegeuk Kim };
1611a7550b30SJaegeuk Kim #endif
1612a7550b30SJaegeuk Kim 
1613ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1614d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1615689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1616ac27a0ecSDave Kleikamp 
1617617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1618617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1619617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1620617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1621617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
16226f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
16238c54ca9cSAl Viro 			 const struct path *path);
1624617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1625ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1626617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1627ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
16287c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
16297c319d32SAditya Kali 			     unsigned int flags);
1630ac27a0ecSDave Kleikamp 
163196c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
163296c7e0d9SJan Kara {
163396c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
163496c7e0d9SJan Kara }
163596c7e0d9SJan Kara 
163661e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
163760e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1638617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1639617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1640617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1641617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1642a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1643a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1644a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1645040cb378SLi Xi 	.get_projid		= ext4_get_projid,
16467a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1647ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1648ac27a0ecSDave Kleikamp };
1649ac27a0ecSDave Kleikamp 
16500d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1651617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1652ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1653287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
16540a240339SJan Kara 	.get_state	= dquot_get_state,
1655287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1656287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
16576332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
16586332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1659ac27a0ecSDave Kleikamp };
1660ac27a0ecSDave Kleikamp #endif
1661ac27a0ecSDave Kleikamp 
1662ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1663617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
166494053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1665617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1666617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1667617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
16687ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
16690930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1670617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1671617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1672c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1673c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1674617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1675617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1676ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1677617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1678617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
167996c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1680ac27a0ecSDave Kleikamp #endif
1681ac27a0ecSDave Kleikamp };
1682ac27a0ecSDave Kleikamp 
168339655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
16841b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
16851b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1686617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1687fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1688ac27a0ecSDave Kleikamp };
1689ac27a0ecSDave Kleikamp 
1690ac27a0ecSDave Kleikamp enum {
1691ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1692ba2e524dSLukas Czerner 	Opt_resgid, Opt_resuid, Opt_sb,
169372578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1694ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
169572578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1696ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1697ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1698ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
16996ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
17004f74d15fSEric Biggers 	Opt_inlinecrypt,
1701ba2e524dSLukas Czerner 	Opt_usrjquota, Opt_grpjquota, Opt_quota,
1702ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
17039cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
17049cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1705327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
17064437992bSLukas Czerner 	Opt_nowarn_on_error, Opt_mblk_io_submit, Opt_debug_want_extra_isize,
17071449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
17085328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1709744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1710fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1711cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
171221175ca4SHarshad Shirwadkar 	Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
1713e5a185c2SLukas Czerner 	Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
17148016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
171599c880deSHarshad Shirwadkar 	Opt_fc_debug_max_replay, Opt_fc_debug_force
17168016e29fSHarshad Shirwadkar #endif
1717ac27a0ecSDave Kleikamp };
1718ac27a0ecSDave Kleikamp 
1719e5a185c2SLukas Czerner static const struct constant_table ext4_param_errors[] = {
1720ba2e524dSLukas Czerner 	{"continue",	EXT4_MOUNT_ERRORS_CONT},
1721ba2e524dSLukas Czerner 	{"panic",	EXT4_MOUNT_ERRORS_PANIC},
1722ba2e524dSLukas Czerner 	{"remount-ro",	EXT4_MOUNT_ERRORS_RO},
1723e5a185c2SLukas Czerner 	{}
1724e5a185c2SLukas Czerner };
1725e5a185c2SLukas Czerner 
1726e5a185c2SLukas Czerner static const struct constant_table ext4_param_data[] = {
1727ba2e524dSLukas Czerner 	{"journal",	EXT4_MOUNT_JOURNAL_DATA},
1728ba2e524dSLukas Czerner 	{"ordered",	EXT4_MOUNT_ORDERED_DATA},
1729ba2e524dSLukas Czerner 	{"writeback",	EXT4_MOUNT_WRITEBACK_DATA},
1730e5a185c2SLukas Czerner 	{}
1731e5a185c2SLukas Czerner };
1732e5a185c2SLukas Czerner 
1733e5a185c2SLukas Czerner static const struct constant_table ext4_param_data_err[] = {
1734e5a185c2SLukas Czerner 	{"abort",	Opt_data_err_abort},
1735e5a185c2SLukas Czerner 	{"ignore",	Opt_data_err_ignore},
1736e5a185c2SLukas Czerner 	{}
1737e5a185c2SLukas Czerner };
1738e5a185c2SLukas Czerner 
1739e5a185c2SLukas Czerner static const struct constant_table ext4_param_jqfmt[] = {
1740ba2e524dSLukas Czerner 	{"vfsold",	QFMT_VFS_OLD},
1741ba2e524dSLukas Czerner 	{"vfsv0",	QFMT_VFS_V0},
1742ba2e524dSLukas Czerner 	{"vfsv1",	QFMT_VFS_V1},
1743e5a185c2SLukas Czerner 	{}
1744e5a185c2SLukas Czerner };
1745e5a185c2SLukas Czerner 
1746e5a185c2SLukas Czerner static const struct constant_table ext4_param_dax[] = {
1747e5a185c2SLukas Czerner 	{"always",	Opt_dax_always},
1748e5a185c2SLukas Czerner 	{"inode",	Opt_dax_inode},
1749e5a185c2SLukas Czerner 	{"never",	Opt_dax_never},
1750e5a185c2SLukas Czerner 	{}
1751e5a185c2SLukas Czerner };
1752e5a185c2SLukas Czerner 
1753e5a185c2SLukas Czerner /* String parameter that allows empty argument */
1754e5a185c2SLukas Czerner #define fsparam_string_empty(NAME, OPT) \
1755e5a185c2SLukas Czerner 	__fsparam(fs_param_is_string, NAME, OPT, fs_param_can_be_empty, NULL)
1756e5a185c2SLukas Czerner 
1757e5a185c2SLukas Czerner /*
1758e5a185c2SLukas Czerner  * Mount option specification
1759e5a185c2SLukas Czerner  * We don't use fsparam_flag_no because of the way we set the
1760e5a185c2SLukas Czerner  * options and the way we show them in _ext4_show_options(). To
1761e5a185c2SLukas Czerner  * keep the changes to a minimum, let's keep the negative options
1762e5a185c2SLukas Czerner  * separate for now.
1763e5a185c2SLukas Czerner  */
1764e5a185c2SLukas Czerner static const struct fs_parameter_spec ext4_param_specs[] = {
1765e5a185c2SLukas Czerner 	fsparam_flag	("bsddf",		Opt_bsd_df),
1766e5a185c2SLukas Czerner 	fsparam_flag	("minixdf",		Opt_minix_df),
1767e5a185c2SLukas Czerner 	fsparam_flag	("grpid",		Opt_grpid),
1768e5a185c2SLukas Czerner 	fsparam_flag	("bsdgroups",		Opt_grpid),
1769e5a185c2SLukas Czerner 	fsparam_flag	("nogrpid",		Opt_nogrpid),
1770e5a185c2SLukas Czerner 	fsparam_flag	("sysvgroups",		Opt_nogrpid),
1771e5a185c2SLukas Czerner 	fsparam_u32	("resgid",		Opt_resgid),
1772e5a185c2SLukas Czerner 	fsparam_u32	("resuid",		Opt_resuid),
1773e5a185c2SLukas Czerner 	fsparam_u32	("sb",			Opt_sb),
1774e5a185c2SLukas Czerner 	fsparam_enum	("errors",		Opt_errors, ext4_param_errors),
1775e5a185c2SLukas Czerner 	fsparam_flag	("nouid32",		Opt_nouid32),
1776e5a185c2SLukas Czerner 	fsparam_flag	("debug",		Opt_debug),
1777e5a185c2SLukas Czerner 	fsparam_flag	("oldalloc",		Opt_removed),
1778e5a185c2SLukas Czerner 	fsparam_flag	("orlov",		Opt_removed),
1779e5a185c2SLukas Czerner 	fsparam_flag	("user_xattr",		Opt_user_xattr),
1780e5a185c2SLukas Czerner 	fsparam_flag	("nouser_xattr",	Opt_nouser_xattr),
1781e5a185c2SLukas Czerner 	fsparam_flag	("acl",			Opt_acl),
1782e5a185c2SLukas Czerner 	fsparam_flag	("noacl",		Opt_noacl),
1783e5a185c2SLukas Czerner 	fsparam_flag	("norecovery",		Opt_noload),
1784e5a185c2SLukas Czerner 	fsparam_flag	("noload",		Opt_noload),
1785e5a185c2SLukas Czerner 	fsparam_flag	("bh",			Opt_removed),
1786e5a185c2SLukas Czerner 	fsparam_flag	("nobh",		Opt_removed),
1787e5a185c2SLukas Czerner 	fsparam_u32	("commit",		Opt_commit),
1788e5a185c2SLukas Czerner 	fsparam_u32	("min_batch_time",	Opt_min_batch_time),
1789e5a185c2SLukas Czerner 	fsparam_u32	("max_batch_time",	Opt_max_batch_time),
1790e5a185c2SLukas Czerner 	fsparam_u32	("journal_dev",		Opt_journal_dev),
1791e5a185c2SLukas Czerner 	fsparam_bdev	("journal_path",	Opt_journal_path),
1792e5a185c2SLukas Czerner 	fsparam_flag	("journal_checksum",	Opt_journal_checksum),
1793e5a185c2SLukas Czerner 	fsparam_flag	("nojournal_checksum",	Opt_nojournal_checksum),
1794e5a185c2SLukas Czerner 	fsparam_flag	("journal_async_commit",Opt_journal_async_commit),
1795e5a185c2SLukas Czerner 	fsparam_flag	("abort",		Opt_abort),
1796e5a185c2SLukas Czerner 	fsparam_enum	("data",		Opt_data, ext4_param_data),
1797e5a185c2SLukas Czerner 	fsparam_enum	("data_err",		Opt_data_err,
1798e5a185c2SLukas Czerner 						ext4_param_data_err),
1799e5a185c2SLukas Czerner 	fsparam_string_empty
1800e5a185c2SLukas Czerner 			("usrjquota",		Opt_usrjquota),
1801e5a185c2SLukas Czerner 	fsparam_string_empty
1802e5a185c2SLukas Czerner 			("grpjquota",		Opt_grpjquota),
1803e5a185c2SLukas Czerner 	fsparam_enum	("jqfmt",		Opt_jqfmt, ext4_param_jqfmt),
1804e5a185c2SLukas Czerner 	fsparam_flag	("grpquota",		Opt_grpquota),
1805e5a185c2SLukas Czerner 	fsparam_flag	("quota",		Opt_quota),
1806e5a185c2SLukas Czerner 	fsparam_flag	("noquota",		Opt_noquota),
1807e5a185c2SLukas Czerner 	fsparam_flag	("usrquota",		Opt_usrquota),
1808e5a185c2SLukas Czerner 	fsparam_flag	("prjquota",		Opt_prjquota),
1809e5a185c2SLukas Czerner 	fsparam_flag	("barrier",		Opt_barrier),
1810e5a185c2SLukas Czerner 	fsparam_u32	("barrier",		Opt_barrier),
1811e5a185c2SLukas Czerner 	fsparam_flag	("nobarrier",		Opt_nobarrier),
1812e5a185c2SLukas Czerner 	fsparam_flag	("i_version",		Opt_i_version),
1813e5a185c2SLukas Czerner 	fsparam_flag	("dax",			Opt_dax),
1814e5a185c2SLukas Czerner 	fsparam_enum	("dax",			Opt_dax_type, ext4_param_dax),
1815e5a185c2SLukas Czerner 	fsparam_u32	("stripe",		Opt_stripe),
1816e5a185c2SLukas Czerner 	fsparam_flag	("delalloc",		Opt_delalloc),
1817e5a185c2SLukas Czerner 	fsparam_flag	("nodelalloc",		Opt_nodelalloc),
1818e5a185c2SLukas Czerner 	fsparam_flag	("warn_on_error",	Opt_warn_on_error),
1819e5a185c2SLukas Czerner 	fsparam_flag	("nowarn_on_error",	Opt_nowarn_on_error),
1820e5a185c2SLukas Czerner 	fsparam_u32	("debug_want_extra_isize",
1821e5a185c2SLukas Czerner 						Opt_debug_want_extra_isize),
1822e5a185c2SLukas Czerner 	fsparam_flag	("mblk_io_submit",	Opt_removed),
1823e5a185c2SLukas Czerner 	fsparam_flag	("nomblk_io_submit",	Opt_removed),
1824e5a185c2SLukas Czerner 	fsparam_flag	("block_validity",	Opt_block_validity),
1825e5a185c2SLukas Czerner 	fsparam_flag	("noblock_validity",	Opt_noblock_validity),
1826e5a185c2SLukas Czerner 	fsparam_u32	("inode_readahead_blks",
1827e5a185c2SLukas Czerner 						Opt_inode_readahead_blks),
1828e5a185c2SLukas Czerner 	fsparam_u32	("journal_ioprio",	Opt_journal_ioprio),
1829e5a185c2SLukas Czerner 	fsparam_u32	("auto_da_alloc",	Opt_auto_da_alloc),
1830e5a185c2SLukas Czerner 	fsparam_flag	("auto_da_alloc",	Opt_auto_da_alloc),
1831e5a185c2SLukas Czerner 	fsparam_flag	("noauto_da_alloc",	Opt_noauto_da_alloc),
1832e5a185c2SLukas Czerner 	fsparam_flag	("dioread_nolock",	Opt_dioread_nolock),
1833e5a185c2SLukas Czerner 	fsparam_flag	("nodioread_nolock",	Opt_dioread_lock),
1834e5a185c2SLukas Czerner 	fsparam_flag	("dioread_lock",	Opt_dioread_lock),
1835e5a185c2SLukas Czerner 	fsparam_flag	("discard",		Opt_discard),
1836e5a185c2SLukas Czerner 	fsparam_flag	("nodiscard",		Opt_nodiscard),
1837e5a185c2SLukas Czerner 	fsparam_u32	("init_itable",		Opt_init_itable),
1838e5a185c2SLukas Czerner 	fsparam_flag	("init_itable",		Opt_init_itable),
1839e5a185c2SLukas Czerner 	fsparam_flag	("noinit_itable",	Opt_noinit_itable),
1840e5a185c2SLukas Czerner #ifdef CONFIG_EXT4_DEBUG
1841e5a185c2SLukas Czerner 	fsparam_flag	("fc_debug_force",	Opt_fc_debug_force),
1842e5a185c2SLukas Czerner 	fsparam_u32	("fc_debug_max_replay",	Opt_fc_debug_max_replay),
1843e5a185c2SLukas Czerner #endif
1844e5a185c2SLukas Czerner 	fsparam_u32	("max_dir_size_kb",	Opt_max_dir_size_kb),
1845e5a185c2SLukas Czerner 	fsparam_flag	("test_dummy_encryption",
1846e5a185c2SLukas Czerner 						Opt_test_dummy_encryption),
1847e5a185c2SLukas Czerner 	fsparam_string	("test_dummy_encryption",
1848e5a185c2SLukas Czerner 						Opt_test_dummy_encryption),
1849e5a185c2SLukas Czerner 	fsparam_flag	("inlinecrypt",		Opt_inlinecrypt),
1850e5a185c2SLukas Czerner 	fsparam_flag	("nombcache",		Opt_nombcache),
1851e5a185c2SLukas Czerner 	fsparam_flag	("no_mbcache",		Opt_nombcache),	/* for backward compatibility */
1852e5a185c2SLukas Czerner 	fsparam_flag	("prefetch_block_bitmaps",
1853e5a185c2SLukas Czerner 						Opt_removed),
1854e5a185c2SLukas Czerner 	fsparam_flag	("no_prefetch_block_bitmaps",
1855e5a185c2SLukas Czerner 						Opt_no_prefetch_block_bitmaps),
1856e5a185c2SLukas Czerner 	fsparam_s32	("mb_optimize_scan",	Opt_mb_optimize_scan),
1857e5a185c2SLukas Czerner 	fsparam_string	("check",		Opt_removed),	/* mount option from ext2/3 */
1858e5a185c2SLukas Czerner 	fsparam_flag	("nocheck",		Opt_removed),	/* mount option from ext2/3 */
1859e5a185c2SLukas Czerner 	fsparam_flag	("reservation",		Opt_removed),	/* mount option from ext2/3 */
1860e5a185c2SLukas Czerner 	fsparam_flag	("noreservation",	Opt_removed),	/* mount option from ext2/3 */
1861e5a185c2SLukas Czerner 	fsparam_u32	("journal",		Opt_removed),	/* mount option from ext2/3 */
1862e5a185c2SLukas Czerner 	{}
1863e5a185c2SLukas Czerner };
1864e5a185c2SLukas Czerner 
1865b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1866196e402aSHarshad Shirwadkar #define DEFAULT_MB_OPTIMIZE_SCAN	(-1)
1867196e402aSHarshad Shirwadkar 
1868d6006186SEric Biggers static const char deprecated_msg[] =
1869d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1870437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1871b3881f74STheodore Ts'o 
187226092bf5STheodore Ts'o #define MOPT_SET	0x0001
187326092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
187426092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
187526092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
187626092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
187726092bf5STheodore Ts'o #define MOPT_Q		0
1878ba2e524dSLukas Czerner #define MOPT_QFMT	0x0010
187926092bf5STheodore Ts'o #else
188026092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
188126092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
188226092bf5STheodore Ts'o #endif
1883ba2e524dSLukas Czerner #define MOPT_NO_EXT2	0x0020
1884ba2e524dSLukas Czerner #define MOPT_NO_EXT3	0x0040
18858dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1886ba2e524dSLukas Czerner #define MOPT_SKIP	0x0080
1887ba2e524dSLukas Czerner #define	MOPT_2		0x0100
188826092bf5STheodore Ts'o 
188926092bf5STheodore Ts'o static const struct mount_opts {
189026092bf5STheodore Ts'o 	int	token;
189126092bf5STheodore Ts'o 	int	mount_opt;
189226092bf5STheodore Ts'o 	int	flags;
189326092bf5STheodore Ts'o } ext4_mount_opts[] = {
189426092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
189526092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
189626092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
189726092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
189826092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
189926092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
19008dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
19018dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
19028dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
19038dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
190426092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
190526092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
19068dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
19078dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
19088dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
190959d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1910327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1911327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1912c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1913c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
19148dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
19151e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
191626092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
19178dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
19181e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
19198dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
1920ba2e524dSLukas Czerner 	{Opt_data_err, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_NO_EXT2},
192126092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
192226092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
192326092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
192426092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
192526092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1926ba2e524dSLukas Czerner 	{Opt_dax_type, 0, MOPT_EXT4_ONLY},
1927ba2e524dSLukas Czerner 	{Opt_journal_dev, 0, MOPT_NO_EXT2},
1928ba2e524dSLukas Czerner 	{Opt_journal_path, 0, MOPT_NO_EXT2},
1929ba2e524dSLukas Czerner 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2},
1930ba2e524dSLukas Czerner 	{Opt_data, 0, MOPT_NO_EXT2},
193126092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
193226092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
193326092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
193426092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
193526092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
193626092bf5STheodore Ts'o #else
193726092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
193826092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
193926092bf5STheodore Ts'o #endif
194026092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
194126092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
194226092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
194326092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
194426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
194526092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
194626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
194749da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
194849da9392SJan Kara 							MOPT_SET | MOPT_Q},
194926092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
195049da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
195149da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
1952ba2e524dSLukas Czerner 	{Opt_usrjquota, 0, MOPT_Q},
1953ba2e524dSLukas Czerner 	{Opt_grpjquota, 0, MOPT_Q},
1954ba2e524dSLukas Czerner 	{Opt_jqfmt, 0, MOPT_QFMT},
1955cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
195621175ca4SHarshad Shirwadkar 	{Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
19573d392b26STheodore Ts'o 	 MOPT_SET},
195899c880deSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
19590f0672ffSHarshad Shirwadkar 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
19600f0672ffSHarshad Shirwadkar 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
19618016e29fSHarshad Shirwadkar #endif
196226092bf5STheodore Ts'o 	{Opt_err, 0, 0}
196326092bf5STheodore Ts'o };
196426092bf5STheodore Ts'o 
1965c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1966c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1967c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1968c83ad55eSGabriel Krisman Bertazi 	char *name;
1969c83ad55eSGabriel Krisman Bertazi 	char *version;
1970c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1971c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1972c83ad55eSGabriel Krisman Bertazi };
1973c83ad55eSGabriel Krisman Bertazi 
1974c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1975c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1976c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1977c83ad55eSGabriel Krisman Bertazi {
1978c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1979c83ad55eSGabriel Krisman Bertazi 	int i;
1980c83ad55eSGabriel Krisman Bertazi 
1981c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1982c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1983c83ad55eSGabriel Krisman Bertazi 			break;
1984c83ad55eSGabriel Krisman Bertazi 
1985c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1986c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1987c83ad55eSGabriel Krisman Bertazi 
1988c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1989c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1990c83ad55eSGabriel Krisman Bertazi 
1991c83ad55eSGabriel Krisman Bertazi 	return 0;
1992c83ad55eSGabriel Krisman Bertazi }
1993c83ad55eSGabriel Krisman Bertazi #endif
1994c83ad55eSGabriel Krisman Bertazi 
19956e47a3ccSLukas Czerner static int ext4_set_test_dummy_encryption(struct super_block *sb, char *arg)
1996ed318a6cSEric Biggers {
1997ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
1998ed318a6cSEric Biggers 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1999ed318a6cSEric Biggers 	int err;
2000ed318a6cSEric Biggers 
20016e47a3ccSLukas Czerner 	err = fscrypt_set_test_dummy_encryption(sb, arg,
2002ac4acb1fSEric Biggers 						&sbi->s_dummy_enc_policy);
2003ed318a6cSEric Biggers 	if (err) {
2004ed318a6cSEric Biggers 		ext4_msg(sb, KERN_WARNING,
20056e47a3ccSLukas Czerner 			 "Error while setting test dummy encryption [%d]", err);
20066e47a3ccSLukas Czerner 		return err;
2007ed318a6cSEric Biggers 	}
2008ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2009ed318a6cSEric Biggers #endif
20106e47a3ccSLukas Czerner 	return 0;
2011ed318a6cSEric Biggers }
2012ed318a6cSEric Biggers 
20136e47a3ccSLukas Czerner #define EXT4_SPEC_JQUOTA			(1 <<  0)
20146e47a3ccSLukas Czerner #define EXT4_SPEC_JQFMT				(1 <<  1)
20156e47a3ccSLukas Czerner #define EXT4_SPEC_DATAJ				(1 <<  2)
20166e47a3ccSLukas Czerner #define EXT4_SPEC_SB_BLOCK			(1 <<  3)
20176e47a3ccSLukas Czerner #define EXT4_SPEC_JOURNAL_DEV			(1 <<  4)
20186e47a3ccSLukas Czerner #define EXT4_SPEC_JOURNAL_IOPRIO		(1 <<  5)
20196e47a3ccSLukas Czerner #define EXT4_SPEC_DUMMY_ENCRYPTION		(1 <<  6)
20206e47a3ccSLukas Czerner #define EXT4_SPEC_s_want_extra_isize		(1 <<  7)
20216e47a3ccSLukas Czerner #define EXT4_SPEC_s_max_batch_time		(1 <<  8)
20226e47a3ccSLukas Czerner #define EXT4_SPEC_s_min_batch_time		(1 <<  9)
20236e47a3ccSLukas Czerner #define EXT4_SPEC_s_inode_readahead_blks	(1 << 10)
20246e47a3ccSLukas Czerner #define EXT4_SPEC_s_li_wait_mult		(1 << 11)
20256e47a3ccSLukas Czerner #define EXT4_SPEC_s_max_dir_size_kb		(1 << 12)
20266e47a3ccSLukas Czerner #define EXT4_SPEC_s_stripe			(1 << 13)
20276e47a3ccSLukas Czerner #define EXT4_SPEC_s_resuid			(1 << 14)
20286e47a3ccSLukas Czerner #define EXT4_SPEC_s_resgid			(1 << 15)
20296e47a3ccSLukas Czerner #define EXT4_SPEC_s_commit_interval		(1 << 16)
20306e47a3ccSLukas Czerner #define EXT4_SPEC_s_fc_debug_max_replay		(1 << 17)
20317edfd85bSLukas Czerner #define EXT4_SPEC_s_sb_block			(1 << 18)
20326e47a3ccSLukas Czerner 
2033461c3af0SLukas Czerner struct ext4_fs_context {
2034e6e268cbSLukas Czerner 	char		*s_qf_names[EXT4_MAXQUOTAS];
20356e47a3ccSLukas Czerner 	char		*test_dummy_enc_arg;
2036e6e268cbSLukas Czerner 	int		s_jquota_fmt;	/* Format of quota to use */
2037196e402aSHarshad Shirwadkar 	int		mb_optimize_scan;
20386e47a3ccSLukas Czerner #ifdef CONFIG_EXT4_DEBUG
20396e47a3ccSLukas Czerner 	int s_fc_debug_max_replay;
20406e47a3ccSLukas Czerner #endif
20416e47a3ccSLukas Czerner 	unsigned short	qname_spec;
20426e47a3ccSLukas Czerner 	unsigned long	vals_s_flags;	/* Bits to set in s_flags */
20436e47a3ccSLukas Czerner 	unsigned long	mask_s_flags;	/* Bits changed in s_flags */
20446e47a3ccSLukas Czerner 	unsigned long	journal_devnum;
20456e47a3ccSLukas Czerner 	unsigned long	s_commit_interval;
20466e47a3ccSLukas Czerner 	unsigned long	s_stripe;
20476e47a3ccSLukas Czerner 	unsigned int	s_inode_readahead_blks;
20486e47a3ccSLukas Czerner 	unsigned int	s_want_extra_isize;
20496e47a3ccSLukas Czerner 	unsigned int	s_li_wait_mult;
20506e47a3ccSLukas Czerner 	unsigned int	s_max_dir_size_kb;
20516e47a3ccSLukas Czerner 	unsigned int	journal_ioprio;
20526e47a3ccSLukas Czerner 	unsigned int	vals_s_mount_opt;
20536e47a3ccSLukas Czerner 	unsigned int	mask_s_mount_opt;
20546e47a3ccSLukas Czerner 	unsigned int	vals_s_mount_opt2;
20556e47a3ccSLukas Czerner 	unsigned int	mask_s_mount_opt2;
20566e47a3ccSLukas Czerner 	unsigned int	vals_s_mount_flags;
20576e47a3ccSLukas Czerner 	unsigned int	mask_s_mount_flags;
2058b6bd2435SLukas Czerner 	unsigned int	opt_flags;	/* MOPT flags */
20596e47a3ccSLukas Czerner 	unsigned int	spec;
20606e47a3ccSLukas Czerner 	u32		s_max_batch_time;
20616e47a3ccSLukas Czerner 	u32		s_min_batch_time;
20626e47a3ccSLukas Czerner 	kuid_t		s_resuid;
20636e47a3ccSLukas Czerner 	kgid_t		s_resgid;
20647edfd85bSLukas Czerner 	ext4_fsblk_t	s_sb_block;
2065b237e304SHarshad Shirwadkar };
2066b237e304SHarshad Shirwadkar 
2067cebe85d5SLukas Czerner static void ext4_fc_free(struct fs_context *fc)
2068cebe85d5SLukas Czerner {
2069cebe85d5SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2070cebe85d5SLukas Czerner 	int i;
2071cebe85d5SLukas Czerner 
2072cebe85d5SLukas Czerner 	if (!ctx)
2073cebe85d5SLukas Czerner 		return;
2074cebe85d5SLukas Czerner 
2075cebe85d5SLukas Czerner 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
2076cebe85d5SLukas Czerner 		kfree(ctx->s_qf_names[i]);
2077cebe85d5SLukas Czerner 
2078cebe85d5SLukas Czerner 	kfree(ctx->test_dummy_enc_arg);
2079cebe85d5SLukas Czerner 	kfree(ctx);
2080cebe85d5SLukas Czerner }
2081cebe85d5SLukas Czerner 
2082cebe85d5SLukas Czerner int ext4_init_fs_context(struct fs_context *fc)
2083cebe85d5SLukas Czerner {
2084da9e4802SDan Carpenter 	struct ext4_fs_context *ctx;
2085cebe85d5SLukas Czerner 
2086cebe85d5SLukas Czerner 	ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
2087cebe85d5SLukas Czerner 	if (!ctx)
2088cebe85d5SLukas Czerner 		return -ENOMEM;
2089cebe85d5SLukas Czerner 
2090cebe85d5SLukas Czerner 	fc->fs_private = ctx;
2091cebe85d5SLukas Czerner 	fc->ops = &ext4_context_ops;
2092cebe85d5SLukas Czerner 
2093cebe85d5SLukas Czerner 	return 0;
2094cebe85d5SLukas Czerner }
2095cebe85d5SLukas Czerner 
2096e6e268cbSLukas Czerner #ifdef CONFIG_QUOTA
2097e6e268cbSLukas Czerner /*
2098e6e268cbSLukas Czerner  * Note the name of the specified quota file.
2099e6e268cbSLukas Czerner  */
2100e6e268cbSLukas Czerner static int note_qf_name(struct fs_context *fc, int qtype,
2101e6e268cbSLukas Czerner 		       struct fs_parameter *param)
2102e6e268cbSLukas Czerner {
2103e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2104e6e268cbSLukas Czerner 	char *qname;
2105e6e268cbSLukas Czerner 
2106e6e268cbSLukas Czerner 	if (param->size < 1) {
2107e6e268cbSLukas Czerner 		ext4_msg(NULL, KERN_ERR, "Missing quota name");
2108e6e268cbSLukas Czerner 		return -EINVAL;
2109e6e268cbSLukas Czerner 	}
2110e6e268cbSLukas Czerner 	if (strchr(param->string, '/')) {
2111e6e268cbSLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2112e6e268cbSLukas Czerner 			 "quotafile must be on filesystem root");
2113e6e268cbSLukas Czerner 		return -EINVAL;
2114e6e268cbSLukas Czerner 	}
2115e6e268cbSLukas Czerner 	if (ctx->s_qf_names[qtype]) {
2116e6e268cbSLukas Czerner 		if (strcmp(ctx->s_qf_names[qtype], param->string) != 0) {
2117e6e268cbSLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2118e6e268cbSLukas Czerner 				 "%s quota file already specified",
2119e6e268cbSLukas Czerner 				 QTYPE2NAME(qtype));
2120e6e268cbSLukas Czerner 			return -EINVAL;
2121e6e268cbSLukas Czerner 		}
2122e6e268cbSLukas Czerner 		return 0;
2123e6e268cbSLukas Czerner 	}
2124e6e268cbSLukas Czerner 
2125e6e268cbSLukas Czerner 	qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
2126e6e268cbSLukas Czerner 	if (!qname) {
2127e6e268cbSLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2128e6e268cbSLukas Czerner 			 "Not enough memory for storing quotafile name");
2129e6e268cbSLukas Czerner 		return -ENOMEM;
2130e6e268cbSLukas Czerner 	}
2131e6e268cbSLukas Czerner 	ctx->s_qf_names[qtype] = qname;
2132e6e268cbSLukas Czerner 	ctx->qname_spec |= 1 << qtype;
21336e47a3ccSLukas Czerner 	ctx->spec |= EXT4_SPEC_JQUOTA;
2134e6e268cbSLukas Czerner 	return 0;
2135e6e268cbSLukas Czerner }
2136e6e268cbSLukas Czerner 
2137e6e268cbSLukas Czerner /*
2138e6e268cbSLukas Czerner  * Clear the name of the specified quota file.
2139e6e268cbSLukas Czerner  */
2140e6e268cbSLukas Czerner static int unnote_qf_name(struct fs_context *fc, int qtype)
2141e6e268cbSLukas Czerner {
2142e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2143e6e268cbSLukas Czerner 
2144e6e268cbSLukas Czerner 	if (ctx->s_qf_names[qtype])
2145e6e268cbSLukas Czerner 		kfree(ctx->s_qf_names[qtype]);
2146e6e268cbSLukas Czerner 
2147e6e268cbSLukas Czerner 	ctx->s_qf_names[qtype] = NULL;
2148e6e268cbSLukas Czerner 	ctx->qname_spec |= 1 << qtype;
21496e47a3ccSLukas Czerner 	ctx->spec |= EXT4_SPEC_JQUOTA;
2150e6e268cbSLukas Czerner 	return 0;
2151e6e268cbSLukas Czerner }
2152e6e268cbSLukas Czerner #endif
2153e6e268cbSLukas Czerner 
21546e47a3ccSLukas Czerner #define EXT4_SET_CTX(name)						\
21554c246728SLukas Czerner static inline void ctx_set_##name(struct ext4_fs_context *ctx,		\
21564c246728SLukas Czerner 				  unsigned long flag)			\
21576e47a3ccSLukas Czerner {									\
21586e47a3ccSLukas Czerner 	ctx->mask_s_##name |= flag;					\
21596e47a3ccSLukas Czerner 	ctx->vals_s_##name |= flag;					\
21606e47a3ccSLukas Czerner }									\
21614c246728SLukas Czerner static inline void ctx_clear_##name(struct ext4_fs_context *ctx,	\
21624c246728SLukas Czerner 				    unsigned long flag)			\
21636e47a3ccSLukas Czerner {									\
21646e47a3ccSLukas Czerner 	ctx->mask_s_##name |= flag;					\
21656e47a3ccSLukas Czerner 	ctx->vals_s_##name &= ~flag;					\
21666e47a3ccSLukas Czerner }									\
21674c246728SLukas Czerner static inline unsigned long						\
21684c246728SLukas Czerner ctx_test_##name(struct ext4_fs_context *ctx, unsigned long flag)	\
21696e47a3ccSLukas Czerner {									\
21704c246728SLukas Czerner 	return (ctx->vals_s_##name & flag);				\
21716e47a3ccSLukas Czerner }									\
21726e47a3ccSLukas Czerner 
21736e47a3ccSLukas Czerner EXT4_SET_CTX(flags);
21746e47a3ccSLukas Czerner EXT4_SET_CTX(mount_opt);
21756e47a3ccSLukas Czerner EXT4_SET_CTX(mount_opt2);
21766e47a3ccSLukas Czerner EXT4_SET_CTX(mount_flags);
21776e47a3ccSLukas Czerner 
217802f960f8SLukas Czerner static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param)
217926092bf5STheodore Ts'o {
2180461c3af0SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2181461c3af0SLukas Czerner 	struct fs_parse_result result;
218226092bf5STheodore Ts'o 	const struct mount_opts *m;
2183461c3af0SLukas Czerner 	int is_remount;
218408cefc7aSEric W. Biederman 	kuid_t uid;
218508cefc7aSEric W. Biederman 	kgid_t gid;
2186461c3af0SLukas Czerner 	int token;
2187461c3af0SLukas Czerner 
2188461c3af0SLukas Czerner 	token = fs_parse(fc, ext4_param_specs, param, &result);
2189461c3af0SLukas Czerner 	if (token < 0)
2190461c3af0SLukas Czerner 		return token;
2191461c3af0SLukas Czerner 	is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
219226092bf5STheodore Ts'o 
2193ba2e524dSLukas Czerner 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
2194ba2e524dSLukas Czerner 		if (token == m->token)
2195ba2e524dSLukas Czerner 			break;
2196ba2e524dSLukas Czerner 
2197ba2e524dSLukas Czerner 	ctx->opt_flags |= m->flags;
2198ba2e524dSLukas Czerner 
2199ba2e524dSLukas Czerner 	if (m->flags & MOPT_EXPLICIT) {
2200ba2e524dSLukas Czerner 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2201ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_EXPLICIT_DELALLOC);
2202ba2e524dSLukas Czerner 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2203ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx,
2204ba2e524dSLukas Czerner 				       EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM);
2205ba2e524dSLukas Czerner 		} else
2206ba2e524dSLukas Czerner 			return -EINVAL;
2207ba2e524dSLukas Czerner 	}
2208ba2e524dSLukas Czerner 
2209ba2e524dSLukas Czerner 	if (m->flags & MOPT_NOSUPPORT) {
2210ba2e524dSLukas Czerner 		ext4_msg(NULL, KERN_ERR, "%s option not supported",
2211ba2e524dSLukas Czerner 			 param->key);
2212ba2e524dSLukas Czerner 		return 0;
2213ba2e524dSLukas Czerner 	}
2214ba2e524dSLukas Czerner 
2215ba2e524dSLukas Czerner 	switch (token) {
221657f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
2217ba2e524dSLukas Czerner 	case Opt_usrjquota:
2218461c3af0SLukas Czerner 		if (!*param->string)
2219e6e268cbSLukas Czerner 			return unnote_qf_name(fc, USRQUOTA);
2220461c3af0SLukas Czerner 		else
2221e6e268cbSLukas Czerner 			return note_qf_name(fc, USRQUOTA, param);
2222ba2e524dSLukas Czerner 	case Opt_grpjquota:
2223461c3af0SLukas Czerner 		if (!*param->string)
2224e6e268cbSLukas Czerner 			return unnote_qf_name(fc, GRPQUOTA);
2225461c3af0SLukas Czerner 		else
2226e6e268cbSLukas Czerner 			return note_qf_name(fc, GRPQUOTA, param);
222757f73c2cSTheodore Ts'o #endif
2228f7048605STheodore Ts'o 	case Opt_noacl:
2229f7048605STheodore Ts'o 	case Opt_nouser_xattr:
2230da812f61SLukas Czerner 		ext4_msg(NULL, KERN_WARNING, deprecated_msg, param->key, "3.5");
2231f7048605STheodore Ts'o 		break;
223226092bf5STheodore Ts'o 	case Opt_sb:
22337edfd85bSLukas Czerner 		if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
22347edfd85bSLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
22357edfd85bSLukas Czerner 				 "Ignoring %s option on remount", param->key);
22367edfd85bSLukas Czerner 		} else {
22377edfd85bSLukas Czerner 			ctx->s_sb_block = result.uint_32;
22387edfd85bSLukas Czerner 			ctx->spec |= EXT4_SPEC_s_sb_block;
22397edfd85bSLukas Czerner 		}
224002f960f8SLukas Czerner 		return 0;
224126092bf5STheodore Ts'o 	case Opt_removed:
2242da812f61SLukas Czerner 		ext4_msg(NULL, KERN_WARNING, "Ignoring removed %s option",
2243461c3af0SLukas Czerner 			 param->key);
224402f960f8SLukas Czerner 		return 0;
224526092bf5STheodore Ts'o 	case Opt_abort:
22466e47a3ccSLukas Czerner 		ctx_set_mount_flags(ctx, EXT4_MF_FS_ABORTED);
224702f960f8SLukas Czerner 		return 0;
224826092bf5STheodore Ts'o 	case Opt_i_version:
2249960e0ab6SLukas Czerner 		ext4_msg(NULL, KERN_WARNING, deprecated_msg, param->key, "5.20");
2250960e0ab6SLukas Czerner 		ext4_msg(NULL, KERN_WARNING, "Use iversion instead\n");
22516e47a3ccSLukas Czerner 		ctx_set_flags(ctx, SB_I_VERSION);
225202f960f8SLukas Czerner 		return 0;
22534f74d15fSEric Biggers 	case Opt_inlinecrypt:
22544f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
22556e47a3ccSLukas Czerner 		ctx_set_flags(ctx, SB_INLINECRYPT);
22564f74d15fSEric Biggers #else
2257da812f61SLukas Czerner 		ext4_msg(NULL, KERN_ERR, "inline encryption not supported");
22584f74d15fSEric Biggers #endif
225902f960f8SLukas Czerner 		return 0;
2260461c3af0SLukas Czerner 	case Opt_errors:
22616e47a3ccSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_ERRORS_MASK);
2262ba2e524dSLukas Czerner 		ctx_set_mount_opt(ctx, result.uint_32);
2263ba2e524dSLukas Czerner 		return 0;
2264ba2e524dSLukas Czerner #ifdef CONFIG_QUOTA
2265ba2e524dSLukas Czerner 	case Opt_jqfmt:
2266ba2e524dSLukas Czerner 		ctx->s_jquota_fmt = result.uint_32;
2267ba2e524dSLukas Czerner 		ctx->spec |= EXT4_SPEC_JQFMT;
2268ba2e524dSLukas Czerner 		return 0;
2269ba2e524dSLukas Czerner #endif
2270ba2e524dSLukas Czerner 	case Opt_data:
2271ba2e524dSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
2272ba2e524dSLukas Czerner 		ctx_set_mount_opt(ctx, result.uint_32);
2273ba2e524dSLukas Czerner 		ctx->spec |= EXT4_SPEC_DATAJ;
2274ba2e524dSLukas Czerner 		return 0;
2275ba2e524dSLukas Czerner 	case Opt_commit:
2276461c3af0SLukas Czerner 		if (result.uint_32 == 0)
22776e47a3ccSLukas Czerner 			ctx->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE;
2278461c3af0SLukas Czerner 		else if (result.uint_32 > INT_MAX / HZ) {
2279da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
22809ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
22819ba55543Szhangyi (F) 				 "must be smaller than %d",
2282461c3af0SLukas Czerner 				 result.uint_32, INT_MAX / HZ);
2283da812f61SLukas Czerner 			return -EINVAL;
22849ba55543Szhangyi (F) 		}
22856e47a3ccSLukas Czerner 		ctx->s_commit_interval = HZ * result.uint_32;
22866e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_commit_interval;
2287ba2e524dSLukas Czerner 		return 0;
2288ba2e524dSLukas Czerner 	case Opt_debug_want_extra_isize:
22896e47a3ccSLukas Czerner 		if ((result.uint_32 & 1) || (result.uint_32 < 4)) {
2290da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2291461c3af0SLukas Czerner 				 "Invalid want_extra_isize %d", result.uint_32);
2292da812f61SLukas Czerner 			return -EINVAL;
22939803387cSTheodore Ts'o 		}
22946e47a3ccSLukas Czerner 		ctx->s_want_extra_isize = result.uint_32;
22956e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_want_extra_isize;
2296ba2e524dSLukas Czerner 		return 0;
2297ba2e524dSLukas Czerner 	case Opt_max_batch_time:
22986e47a3ccSLukas Czerner 		ctx->s_max_batch_time = result.uint_32;
22996e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_max_batch_time;
2300ba2e524dSLukas Czerner 		return 0;
2301ba2e524dSLukas Czerner 	case Opt_min_batch_time:
23026e47a3ccSLukas Czerner 		ctx->s_min_batch_time = result.uint_32;
23036e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_min_batch_time;
2304ba2e524dSLukas Czerner 		return 0;
2305ba2e524dSLukas Czerner 	case Opt_inode_readahead_blks:
2306461c3af0SLukas Czerner 		if (result.uint_32 &&
2307461c3af0SLukas Czerner 		    (result.uint_32 > (1 << 30) ||
2308461c3af0SLukas Czerner 		     !is_power_of_2(result.uint_32))) {
2309da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2310e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2311e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
2312da812f61SLukas Czerner 			return -EINVAL;
231326092bf5STheodore Ts'o 		}
23146e47a3ccSLukas Czerner 		ctx->s_inode_readahead_blks = result.uint_32;
23156e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_inode_readahead_blks;
2316ba2e524dSLukas Czerner 		return 0;
2317ba2e524dSLukas Czerner 	case Opt_init_itable:
23186e47a3ccSLukas Czerner 		ctx_set_mount_opt(ctx, EXT4_MOUNT_INIT_INODE_TABLE);
23196e47a3ccSLukas Czerner 		ctx->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
2320461c3af0SLukas Czerner 		if (param->type == fs_value_is_string)
23216e47a3ccSLukas Czerner 			ctx->s_li_wait_mult = result.uint_32;
23226e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_li_wait_mult;
2323ba2e524dSLukas Czerner 		return 0;
2324ba2e524dSLukas Czerner 	case Opt_max_dir_size_kb:
23256e47a3ccSLukas Czerner 		ctx->s_max_dir_size_kb = result.uint_32;
23266e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_max_dir_size_kb;
2327ba2e524dSLukas Czerner 		return 0;
23288016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
2329ba2e524dSLukas Czerner 	case Opt_fc_debug_max_replay:
23306e47a3ccSLukas Czerner 		ctx->s_fc_debug_max_replay = result.uint_32;
23316e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_fc_debug_max_replay;
2332ba2e524dSLukas Czerner 		return 0;
23338016e29fSHarshad Shirwadkar #endif
2334ba2e524dSLukas Czerner 	case Opt_stripe:
23356e47a3ccSLukas Czerner 		ctx->s_stripe = result.uint_32;
23366e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_stripe;
2337ba2e524dSLukas Czerner 		return 0;
2338ba2e524dSLukas Czerner 	case Opt_resuid:
2339461c3af0SLukas Czerner 		uid = make_kuid(current_user_ns(), result.uint_32);
23400efb3b23SJan Kara 		if (!uid_valid(uid)) {
2341da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Invalid uid value %d",
2342461c3af0SLukas Czerner 				 result.uint_32);
2343da812f61SLukas Czerner 			return -EINVAL;
23440efb3b23SJan Kara 		}
23456e47a3ccSLukas Czerner 		ctx->s_resuid = uid;
23466e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_resuid;
2347ba2e524dSLukas Czerner 		return 0;
2348ba2e524dSLukas Czerner 	case Opt_resgid:
2349461c3af0SLukas Czerner 		gid = make_kgid(current_user_ns(), result.uint_32);
23500efb3b23SJan Kara 		if (!gid_valid(gid)) {
2351da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Invalid gid value %d",
2352461c3af0SLukas Czerner 				 result.uint_32);
2353da812f61SLukas Czerner 			return -EINVAL;
23540efb3b23SJan Kara 		}
23556e47a3ccSLukas Czerner 		ctx->s_resgid = gid;
23566e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_s_resgid;
2357ba2e524dSLukas Czerner 		return 0;
2358ba2e524dSLukas Czerner 	case Opt_journal_dev:
23590efb3b23SJan Kara 		if (is_remount) {
2360da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
23610efb3b23SJan Kara 				 "Cannot specify journal on remount");
2362da812f61SLukas Czerner 			return -EINVAL;
23630efb3b23SJan Kara 		}
2364461c3af0SLukas Czerner 		ctx->journal_devnum = result.uint_32;
23656e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
2366ba2e524dSLukas Czerner 		return 0;
2367ba2e524dSLukas Czerner 	case Opt_journal_path:
2368ba2e524dSLukas Czerner 	{
2369ad4eec61SEric Sandeen 		struct inode *journal_inode;
2370ad4eec61SEric Sandeen 		struct path path;
2371ad4eec61SEric Sandeen 		int error;
2372ad4eec61SEric Sandeen 
2373ad4eec61SEric Sandeen 		if (is_remount) {
2374da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR,
2375ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2376da812f61SLukas Czerner 			return -EINVAL;
2377ad4eec61SEric Sandeen 		}
2378ad4eec61SEric Sandeen 
2379461c3af0SLukas Czerner 		error = fs_lookup_param(fc, param, 1, &path);
2380ad4eec61SEric Sandeen 		if (error) {
2381da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "error: could not find "
2382461c3af0SLukas Czerner 				 "journal device path");
2383da812f61SLukas Czerner 			return -EINVAL;
2384ad4eec61SEric Sandeen 		}
2385ad4eec61SEric Sandeen 
23862b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2387461c3af0SLukas Czerner 		ctx->journal_devnum = new_encode_dev(journal_inode->i_rdev);
23886e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
2389ad4eec61SEric Sandeen 		path_put(&path);
2390ba2e524dSLukas Czerner 		return 0;
2391ba2e524dSLukas Czerner 	}
2392ba2e524dSLukas Czerner 	case Opt_journal_ioprio:
2393461c3af0SLukas Czerner 		if (result.uint_32 > 7) {
2394da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Invalid journal IO priority"
23950efb3b23SJan Kara 				 " (must be 0-7)");
2396da812f61SLukas Czerner 			return -EINVAL;
23970efb3b23SJan Kara 		}
2398461c3af0SLukas Czerner 		ctx->journal_ioprio =
2399461c3af0SLukas Czerner 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, result.uint_32);
24006e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_JOURNAL_IOPRIO;
2401ba2e524dSLukas Czerner 		return 0;
2402ba2e524dSLukas Czerner 	case Opt_test_dummy_encryption:
24036e47a3ccSLukas Czerner #ifdef CONFIG_FS_ENCRYPTION
24046e47a3ccSLukas Czerner 		if (param->type == fs_value_is_flag) {
24056e47a3ccSLukas Czerner 			ctx->spec |= EXT4_SPEC_DUMMY_ENCRYPTION;
24066e47a3ccSLukas Czerner 			ctx->test_dummy_enc_arg = NULL;
240702f960f8SLukas Czerner 			return 0;
24086e47a3ccSLukas Czerner 		}
24096e47a3ccSLukas Czerner 		if (*param->string &&
24106e47a3ccSLukas Czerner 		    !(!strcmp(param->string, "v1") ||
24116e47a3ccSLukas Czerner 		      !strcmp(param->string, "v2"))) {
24126e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
24136e47a3ccSLukas Czerner 				 "Value of option \"%s\" is unrecognized",
24146e47a3ccSLukas Czerner 				 param->key);
2415da812f61SLukas Czerner 			return -EINVAL;
241626092bf5STheodore Ts'o 		}
24176e47a3ccSLukas Czerner 		ctx->spec |= EXT4_SPEC_DUMMY_ENCRYPTION;
24186e47a3ccSLukas Czerner 		ctx->test_dummy_enc_arg = kmemdup_nul(param->string, param->size,
24196e47a3ccSLukas Czerner 						      GFP_KERNEL);
24206e47a3ccSLukas Czerner #else
24216e47a3ccSLukas Czerner 		ext4_msg(NULL, KERN_WARNING,
24226e47a3ccSLukas Czerner 			 "Test dummy encryption mount option ignored");
24236e47a3ccSLukas Czerner #endif
2424ba2e524dSLukas Czerner 		return 0;
2425ba2e524dSLukas Czerner 	case Opt_dax:
2426ba2e524dSLukas Czerner 	case Opt_dax_type:
2427ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2428ba2e524dSLukas Czerner 	{
2429ba2e524dSLukas Czerner 		int type = (token == Opt_dax) ?
2430ba2e524dSLukas Czerner 			   Opt_dax : result.uint_32;
2431ba2e524dSLukas Czerner 
2432ba2e524dSLukas Czerner 		switch (type) {
24339cb20f94SIra Weiny 		case Opt_dax:
24349cb20f94SIra Weiny 		case Opt_dax_always:
2435ba2e524dSLukas Czerner 			ctx_set_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
24366e47a3ccSLukas Czerner 			ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
24379cb20f94SIra Weiny 			break;
24389cb20f94SIra Weiny 		case Opt_dax_never:
2439ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
24406e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
24419cb20f94SIra Weiny 			break;
24429cb20f94SIra Weiny 		case Opt_dax_inode:
24436e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
24446e47a3ccSLukas Czerner 			ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
24459cb20f94SIra Weiny 			/* Strictly for printing options */
2446ba2e524dSLukas Czerner 			ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE);
24479cb20f94SIra Weiny 			break;
24489cb20f94SIra Weiny 		}
2449ba2e524dSLukas Czerner 		return 0;
2450ba2e524dSLukas Czerner 	}
2451ef83b6e8SDan Williams #else
2452da812f61SLukas Czerner 		ext4_msg(NULL, KERN_INFO, "dax option not supported");
2453da812f61SLukas Czerner 		return -EINVAL;
2454923ae0ffSRoss Zwisler #endif
2455ba2e524dSLukas Czerner 	case Opt_data_err:
2456ba2e524dSLukas Czerner 		if (result.uint_32 == Opt_data_err_abort)
24576e47a3ccSLukas Czerner 			ctx_set_mount_opt(ctx, m->mount_opt);
2458ba2e524dSLukas Czerner 		else if (result.uint_32 == Opt_data_err_ignore)
24596e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, m->mount_opt);
2460ba2e524dSLukas Czerner 		return 0;
2461ba2e524dSLukas Czerner 	case Opt_mb_optimize_scan:
2462461c3af0SLukas Czerner 		if (result.int_32 != 0 && result.int_32 != 1) {
2463da812f61SLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
2464196e402aSHarshad Shirwadkar 				 "mb_optimize_scan should be set to 0 or 1.");
2465da812f61SLukas Czerner 			return -EINVAL;
2466196e402aSHarshad Shirwadkar 		}
2467461c3af0SLukas Czerner 		ctx->mb_optimize_scan = result.int_32;
2468ba2e524dSLukas Czerner 		return 0;
2469ba2e524dSLukas Czerner 	}
2470ba2e524dSLukas Czerner 
2471ba2e524dSLukas Czerner 	/*
2472ba2e524dSLukas Czerner 	 * At this point we should only be getting options requiring MOPT_SET,
2473ba2e524dSLukas Czerner 	 * or MOPT_CLEAR. Anything else is a bug
2474ba2e524dSLukas Czerner 	 */
2475ba2e524dSLukas Czerner 	if (m->token == Opt_err) {
2476ba2e524dSLukas Czerner 		ext4_msg(NULL, KERN_WARNING, "buggy handling of option %s",
2477ba2e524dSLukas Czerner 			 param->key);
2478ba2e524dSLukas Czerner 		WARN_ON(1);
2479ba2e524dSLukas Czerner 		return -EINVAL;
2480ba2e524dSLukas Czerner 	}
2481ba2e524dSLukas Czerner 
2482ba2e524dSLukas Czerner 	else {
2483461c3af0SLukas Czerner 		unsigned int set = 0;
2484461c3af0SLukas Czerner 
2485461c3af0SLukas Czerner 		if ((param->type == fs_value_is_flag) ||
2486461c3af0SLukas Czerner 		    result.uint_32 > 0)
2487461c3af0SLukas Czerner 			set = 1;
2488461c3af0SLukas Czerner 
248926092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
2490461c3af0SLukas Czerner 			set = !set;
249126092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
2492da812f61SLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
2493461c3af0SLukas Czerner 				 "buggy handling of option %s",
2494461c3af0SLukas Czerner 				 param->key);
249526092bf5STheodore Ts'o 			WARN_ON(1);
2496da812f61SLukas Czerner 			return -EINVAL;
249726092bf5STheodore Ts'o 		}
2498995a3ed6SHarshad Shirwadkar 		if (m->flags & MOPT_2) {
2499461c3af0SLukas Czerner 			if (set != 0)
25006e47a3ccSLukas Czerner 				ctx_set_mount_opt2(ctx, m->mount_opt);
2501995a3ed6SHarshad Shirwadkar 			else
25026e47a3ccSLukas Czerner 				ctx_clear_mount_opt2(ctx, m->mount_opt);
2503995a3ed6SHarshad Shirwadkar 		} else {
2504461c3af0SLukas Czerner 			if (set != 0)
25056e47a3ccSLukas Czerner 				ctx_set_mount_opt(ctx, m->mount_opt);
250626092bf5STheodore Ts'o 			else
25076e47a3ccSLukas Czerner 				ctx_clear_mount_opt(ctx, m->mount_opt);
250826092bf5STheodore Ts'o 		}
2509995a3ed6SHarshad Shirwadkar 	}
2510ba2e524dSLukas Czerner 
251102f960f8SLukas Czerner 	return 0;
251226092bf5STheodore Ts'o }
251326092bf5STheodore Ts'o 
25147edfd85bSLukas Czerner static int parse_options(struct fs_context *fc, char *options)
2515ac27a0ecSDave Kleikamp {
2516461c3af0SLukas Czerner 	struct fs_parameter param;
2517461c3af0SLukas Czerner 	int ret;
2518461c3af0SLukas Czerner 	char *key;
2519ac27a0ecSDave Kleikamp 
2520ac27a0ecSDave Kleikamp 	if (!options)
25217edfd85bSLukas Czerner 		return 0;
2522461c3af0SLukas Czerner 
2523461c3af0SLukas Czerner 	while ((key = strsep(&options, ",")) != NULL) {
2524461c3af0SLukas Czerner 		if (*key) {
2525461c3af0SLukas Czerner 			size_t v_len = 0;
2526461c3af0SLukas Czerner 			char *value = strchr(key, '=');
2527461c3af0SLukas Czerner 
2528461c3af0SLukas Czerner 			param.type = fs_value_is_flag;
2529461c3af0SLukas Czerner 			param.string = NULL;
2530461c3af0SLukas Czerner 
2531461c3af0SLukas Czerner 			if (value) {
2532461c3af0SLukas Czerner 				if (value == key)
2533ac27a0ecSDave Kleikamp 					continue;
2534461c3af0SLukas Czerner 
2535461c3af0SLukas Czerner 				*value++ = 0;
2536461c3af0SLukas Czerner 				v_len = strlen(value);
2537461c3af0SLukas Czerner 				param.string = kmemdup_nul(value, v_len,
2538461c3af0SLukas Czerner 							   GFP_KERNEL);
2539461c3af0SLukas Czerner 				if (!param.string)
25407edfd85bSLukas Czerner 					return -ENOMEM;
2541461c3af0SLukas Czerner 				param.type = fs_value_is_string;
2542461c3af0SLukas Czerner 			}
2543461c3af0SLukas Czerner 
2544461c3af0SLukas Czerner 			param.key = key;
2545461c3af0SLukas Czerner 			param.size = v_len;
2546461c3af0SLukas Czerner 
254702f960f8SLukas Czerner 			ret = ext4_parse_param(fc, &param);
2548461c3af0SLukas Czerner 			if (param.string)
2549461c3af0SLukas Czerner 				kfree(param.string);
2550461c3af0SLukas Czerner 			if (ret < 0)
25517edfd85bSLukas Czerner 				return ret;
2552ac27a0ecSDave Kleikamp 		}
2553461c3af0SLukas Czerner 	}
2554461c3af0SLukas Czerner 
25557edfd85bSLukas Czerner 	ret = ext4_validate_options(fc);
2556da812f61SLukas Czerner 	if (ret < 0)
25577edfd85bSLukas Czerner 		return ret;
25587edfd85bSLukas Czerner 
25597edfd85bSLukas Czerner 	return 0;
25607edfd85bSLukas Czerner }
25617edfd85bSLukas Czerner 
25627edfd85bSLukas Czerner static int parse_apply_sb_mount_options(struct super_block *sb,
25637edfd85bSLukas Czerner 					struct ext4_fs_context *m_ctx)
25647edfd85bSLukas Czerner {
25657edfd85bSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
25667edfd85bSLukas Czerner 	char *s_mount_opts = NULL;
25677edfd85bSLukas Czerner 	struct ext4_fs_context *s_ctx = NULL;
25687edfd85bSLukas Czerner 	struct fs_context *fc = NULL;
25697edfd85bSLukas Czerner 	int ret = -ENOMEM;
25707edfd85bSLukas Czerner 
25717edfd85bSLukas Czerner 	if (!sbi->s_es->s_mount_opts[0])
2572da812f61SLukas Czerner 		return 0;
2573da812f61SLukas Czerner 
25747edfd85bSLukas Czerner 	s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
25757edfd85bSLukas Czerner 				sizeof(sbi->s_es->s_mount_opts),
25767edfd85bSLukas Czerner 				GFP_KERNEL);
25777edfd85bSLukas Czerner 	if (!s_mount_opts)
25787edfd85bSLukas Czerner 		return ret;
2579e6e268cbSLukas Czerner 
25807edfd85bSLukas Czerner 	fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
25817edfd85bSLukas Czerner 	if (!fc)
25827edfd85bSLukas Czerner 		goto out_free;
2583e6e268cbSLukas Czerner 
25847edfd85bSLukas Czerner 	s_ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
25857edfd85bSLukas Czerner 	if (!s_ctx)
25867edfd85bSLukas Czerner 		goto out_free;
25877edfd85bSLukas Czerner 
25887edfd85bSLukas Czerner 	fc->fs_private = s_ctx;
25897edfd85bSLukas Czerner 	fc->s_fs_info = sbi;
25907edfd85bSLukas Czerner 
25917edfd85bSLukas Czerner 	ret = parse_options(fc, s_mount_opts);
25927edfd85bSLukas Czerner 	if (ret < 0)
25937edfd85bSLukas Czerner 		goto parse_failed;
25947edfd85bSLukas Czerner 
25957edfd85bSLukas Czerner 	ret = ext4_check_opt_consistency(fc, sb);
25967edfd85bSLukas Czerner 	if (ret < 0) {
25977edfd85bSLukas Czerner parse_failed:
25987edfd85bSLukas Czerner 		ext4_msg(sb, KERN_WARNING,
25997edfd85bSLukas Czerner 			 "failed to parse options in superblock: %s",
26007edfd85bSLukas Czerner 			 s_mount_opts);
26017edfd85bSLukas Czerner 		ret = 0;
26027edfd85bSLukas Czerner 		goto out_free;
26037edfd85bSLukas Czerner 	}
26047edfd85bSLukas Czerner 
26057edfd85bSLukas Czerner 	if (s_ctx->spec & EXT4_SPEC_JOURNAL_DEV)
26067edfd85bSLukas Czerner 		m_ctx->journal_devnum = s_ctx->journal_devnum;
26077edfd85bSLukas Czerner 	if (s_ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)
26087edfd85bSLukas Czerner 		m_ctx->journal_ioprio = s_ctx->journal_ioprio;
26097edfd85bSLukas Czerner 
26107edfd85bSLukas Czerner 	ret = ext4_apply_options(fc, sb);
26117edfd85bSLukas Czerner 
26127edfd85bSLukas Czerner out_free:
26137edfd85bSLukas Czerner 	kfree(s_ctx);
26147edfd85bSLukas Czerner 	kfree(fc);
26157edfd85bSLukas Czerner 	kfree(s_mount_opts);
26167edfd85bSLukas Czerner 	return ret;
26174c94bff9SLukas Czerner }
26184c94bff9SLukas Czerner 
2619e6e268cbSLukas Czerner static void ext4_apply_quota_options(struct fs_context *fc,
2620e6e268cbSLukas Czerner 				     struct super_block *sb)
2621e6e268cbSLukas Czerner {
2622e6e268cbSLukas Czerner #ifdef CONFIG_QUOTA
26236e47a3ccSLukas Czerner 	bool quota_feature = ext4_has_feature_quota(sb);
2624e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2625e6e268cbSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2626e6e268cbSLukas Czerner 	char *qname;
2627e6e268cbSLukas Czerner 	int i;
2628e6e268cbSLukas Czerner 
26296e47a3ccSLukas Czerner 	if (quota_feature)
26306e47a3ccSLukas Czerner 		return;
26316e47a3ccSLukas Czerner 
26326e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQUOTA) {
2633e6e268cbSLukas Czerner 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2634e6e268cbSLukas Czerner 			if (!(ctx->qname_spec & (1 << i)))
2635e6e268cbSLukas Czerner 				continue;
26366e47a3ccSLukas Czerner 
2637e6e268cbSLukas Czerner 			qname = ctx->s_qf_names[i]; /* May be NULL */
26384c1bd5a9SLukas Czerner 			if (qname)
26394c1bd5a9SLukas Czerner 				set_opt(sb, QUOTA);
2640e6e268cbSLukas Czerner 			ctx->s_qf_names[i] = NULL;
264113b215a9SLukas Czerner 			qname = rcu_replace_pointer(sbi->s_qf_names[i], qname,
264213b215a9SLukas Czerner 						lockdep_is_held(&sb->s_umount));
264313b215a9SLukas Czerner 			if (qname)
264413b215a9SLukas Czerner 				kfree_rcu(qname);
2645e6e268cbSLukas Czerner 		}
26466e47a3ccSLukas Czerner 	}
26476e47a3ccSLukas Czerner 
26486e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQFMT)
26496e47a3ccSLukas Czerner 		sbi->s_jquota_fmt = ctx->s_jquota_fmt;
2650e6e268cbSLukas Czerner #endif
2651e6e268cbSLukas Czerner }
2652e6e268cbSLukas Czerner 
2653e6e268cbSLukas Czerner /*
2654e6e268cbSLukas Czerner  * Check quota settings consistency.
2655e6e268cbSLukas Czerner  */
2656e6e268cbSLukas Czerner static int ext4_check_quota_consistency(struct fs_context *fc,
2657e6e268cbSLukas Czerner 					struct super_block *sb)
2658e6e268cbSLukas Czerner {
2659e6e268cbSLukas Czerner #ifdef CONFIG_QUOTA
2660e6e268cbSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
2661e6e268cbSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2662e6e268cbSLukas Czerner 	bool quota_feature = ext4_has_feature_quota(sb);
2663e6e268cbSLukas Czerner 	bool quota_loaded = sb_any_quota_loaded(sb);
26646e47a3ccSLukas Czerner 	bool usr_qf_name, grp_qf_name, usrquota, grpquota;
26656e47a3ccSLukas Czerner 	int quota_flags, i;
2666e6e268cbSLukas Czerner 
26676e47a3ccSLukas Czerner 	/*
26686e47a3ccSLukas Czerner 	 * We do the test below only for project quotas. 'usrquota' and
26696e47a3ccSLukas Czerner 	 * 'grpquota' mount options are allowed even without quota feature
26706e47a3ccSLukas Czerner 	 * to support legacy quotas in quota files.
26716e47a3ccSLukas Czerner 	 */
26726e47a3ccSLukas Czerner 	if (ctx_test_mount_opt(ctx, EXT4_MOUNT_PRJQUOTA) &&
26736e47a3ccSLukas Czerner 	    !ext4_has_feature_project(sb)) {
26746e47a3ccSLukas Czerner 		ext4_msg(NULL, KERN_ERR, "Project quota feature not enabled. "
26756e47a3ccSLukas Czerner 			 "Cannot enable project quota enforcement.");
26766e47a3ccSLukas Czerner 		return -EINVAL;
26776e47a3ccSLukas Czerner 	}
26786e47a3ccSLukas Czerner 
26796e47a3ccSLukas Czerner 	quota_flags = EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
26806e47a3ccSLukas Czerner 		      EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA;
26816e47a3ccSLukas Czerner 	if (quota_loaded &&
26826e47a3ccSLukas Czerner 	    ctx->mask_s_mount_opt & quota_flags &&
26836e47a3ccSLukas Czerner 	    !ctx_test_mount_opt(ctx, quota_flags))
26846e47a3ccSLukas Czerner 		goto err_quota_change;
26856e47a3ccSLukas Czerner 
26866e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQUOTA) {
2687e6e268cbSLukas Czerner 
2688e6e268cbSLukas Czerner 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2689e6e268cbSLukas Czerner 			if (!(ctx->qname_spec & (1 << i)))
2690e6e268cbSLukas Czerner 				continue;
2691e6e268cbSLukas Czerner 
26926e47a3ccSLukas Czerner 			if (quota_loaded &&
26936e47a3ccSLukas Czerner 			    !!sbi->s_qf_names[i] != !!ctx->s_qf_names[i])
2694e6e268cbSLukas Czerner 				goto err_jquota_change;
2695e6e268cbSLukas Czerner 
2696e6e268cbSLukas Czerner 			if (sbi->s_qf_names[i] && ctx->s_qf_names[i] &&
269713b215a9SLukas Czerner 			    strcmp(get_qf_name(sb, sbi, i),
2698e6e268cbSLukas Czerner 				   ctx->s_qf_names[i]) != 0)
2699e6e268cbSLukas Czerner 				goto err_jquota_specified;
2700e6e268cbSLukas Czerner 		}
27016e47a3ccSLukas Czerner 
27026e47a3ccSLukas Czerner 		if (quota_feature) {
27036e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_INFO,
27046e47a3ccSLukas Czerner 				 "Journaled quota options ignored when "
27056e47a3ccSLukas Czerner 				 "QUOTA feature is enabled");
27066e47a3ccSLukas Czerner 			return 0;
27076e47a3ccSLukas Czerner 		}
2708e6e268cbSLukas Czerner 	}
2709e6e268cbSLukas Czerner 
27106e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_JQFMT) {
2711e6e268cbSLukas Czerner 		if (sbi->s_jquota_fmt != ctx->s_jquota_fmt && quota_loaded)
27126e47a3ccSLukas Czerner 			goto err_jquota_change;
2713e6e268cbSLukas Czerner 		if (quota_feature) {
2714e6e268cbSLukas Czerner 			ext4_msg(NULL, KERN_INFO, "Quota format mount options "
2715e6e268cbSLukas Czerner 				 "ignored when QUOTA feature is enabled");
2716e6e268cbSLukas Czerner 			return 0;
2717e6e268cbSLukas Czerner 		}
2718e6e268cbSLukas Czerner 	}
27196e47a3ccSLukas Czerner 
27206e47a3ccSLukas Czerner 	/* Make sure we don't mix old and new quota format */
27216e47a3ccSLukas Czerner 	usr_qf_name = (get_qf_name(sb, sbi, USRQUOTA) ||
27226e47a3ccSLukas Czerner 		       ctx->s_qf_names[USRQUOTA]);
27236e47a3ccSLukas Czerner 	grp_qf_name = (get_qf_name(sb, sbi, GRPQUOTA) ||
27246e47a3ccSLukas Czerner 		       ctx->s_qf_names[GRPQUOTA]);
27256e47a3ccSLukas Czerner 
27266e47a3ccSLukas Czerner 	usrquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
27276e47a3ccSLukas Czerner 		    test_opt(sb, USRQUOTA));
27286e47a3ccSLukas Czerner 
27296e47a3ccSLukas Czerner 	grpquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) ||
27306e47a3ccSLukas Czerner 		    test_opt(sb, GRPQUOTA));
27316e47a3ccSLukas Czerner 
27326e47a3ccSLukas Czerner 	if (usr_qf_name) {
27336e47a3ccSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
27346e47a3ccSLukas Czerner 		usrquota = false;
27356e47a3ccSLukas Czerner 	}
27366e47a3ccSLukas Czerner 	if (grp_qf_name) {
27376e47a3ccSLukas Czerner 		ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
27386e47a3ccSLukas Czerner 		grpquota = false;
27396e47a3ccSLukas Czerner 	}
27406e47a3ccSLukas Czerner 
27416e47a3ccSLukas Czerner 	if (usr_qf_name || grp_qf_name) {
27426e47a3ccSLukas Czerner 		if (usrquota || grpquota) {
27436e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "old and new quota "
27446e47a3ccSLukas Czerner 				 "format mixing");
27456e47a3ccSLukas Czerner 			return -EINVAL;
27466e47a3ccSLukas Czerner 		}
27476e47a3ccSLukas Czerner 
27486e47a3ccSLukas Czerner 		if (!(ctx->spec & EXT4_SPEC_JQFMT || sbi->s_jquota_fmt)) {
27496e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "journaled quota format "
27506e47a3ccSLukas Czerner 				 "not specified");
27516e47a3ccSLukas Czerner 			return -EINVAL;
27526e47a3ccSLukas Czerner 		}
27536e47a3ccSLukas Czerner 	}
27546e47a3ccSLukas Czerner 
2755e6e268cbSLukas Czerner 	return 0;
2756e6e268cbSLukas Czerner 
2757e6e268cbSLukas Czerner err_quota_change:
2758e6e268cbSLukas Czerner 	ext4_msg(NULL, KERN_ERR,
2759e6e268cbSLukas Czerner 		 "Cannot change quota options when quota turned on");
2760e6e268cbSLukas Czerner 	return -EINVAL;
2761e6e268cbSLukas Czerner err_jquota_change:
2762e6e268cbSLukas Czerner 	ext4_msg(NULL, KERN_ERR, "Cannot change journaled quota "
2763e6e268cbSLukas Czerner 		 "options when quota turned on");
2764e6e268cbSLukas Czerner 	return -EINVAL;
2765e6e268cbSLukas Czerner err_jquota_specified:
2766e6e268cbSLukas Czerner 	ext4_msg(NULL, KERN_ERR, "%s quota file already specified",
2767e6e268cbSLukas Czerner 		 QTYPE2NAME(i));
2768e6e268cbSLukas Czerner 	return -EINVAL;
2769e6e268cbSLukas Czerner #else
2770e6e268cbSLukas Czerner 	return 0;
2771e6e268cbSLukas Czerner #endif
2772e6e268cbSLukas Czerner }
2773e6e268cbSLukas Czerner 
2774b6bd2435SLukas Czerner static int ext4_check_opt_consistency(struct fs_context *fc,
2775b6bd2435SLukas Czerner 				      struct super_block *sb)
2776b6bd2435SLukas Czerner {
2777b6bd2435SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
27786e47a3ccSLukas Czerner 	struct ext4_sb_info *sbi = fc->s_fs_info;
27796e47a3ccSLukas Czerner 	int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
2780b6bd2435SLukas Czerner 
2781b6bd2435SLukas Czerner 	if ((ctx->opt_flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2782b6bd2435SLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2783b6bd2435SLukas Czerner 			 "Mount option(s) incompatible with ext2");
2784b6bd2435SLukas Czerner 		return -EINVAL;
2785b6bd2435SLukas Czerner 	}
2786b6bd2435SLukas Czerner 	if ((ctx->opt_flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2787b6bd2435SLukas Czerner 		ext4_msg(NULL, KERN_ERR,
2788b6bd2435SLukas Czerner 			 "Mount option(s) incompatible with ext3");
2789b6bd2435SLukas Czerner 		return -EINVAL;
2790b6bd2435SLukas Czerner 	}
2791b6bd2435SLukas Czerner 
27926e47a3ccSLukas Czerner 	if (ctx->s_want_extra_isize >
27936e47a3ccSLukas Czerner 	    (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE)) {
27946e47a3ccSLukas Czerner 		ext4_msg(NULL, KERN_ERR,
27956e47a3ccSLukas Czerner 			 "Invalid want_extra_isize %d",
27966e47a3ccSLukas Czerner 			 ctx->s_want_extra_isize);
27976e47a3ccSLukas Czerner 		return -EINVAL;
27986e47a3ccSLukas Czerner 	}
27996e47a3ccSLukas Czerner 
28006e47a3ccSLukas Czerner 	if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DIOREAD_NOLOCK)) {
28016e47a3ccSLukas Czerner 		int blocksize =
28026e47a3ccSLukas Czerner 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
28036e47a3ccSLukas Czerner 		if (blocksize < PAGE_SIZE)
28046e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_WARNING, "Warning: mounting with an "
28056e47a3ccSLukas Czerner 				 "experimental mount option 'dioread_nolock' "
28066e47a3ccSLukas Czerner 				 "for blocksize < PAGE_SIZE");
28076e47a3ccSLukas Czerner 	}
28086e47a3ccSLukas Czerner 
28096e47a3ccSLukas Czerner #ifdef CONFIG_FS_ENCRYPTION
28106e47a3ccSLukas Czerner 	/*
28116e47a3ccSLukas Czerner 	 * This mount option is just for testing, and it's not worthwhile to
28126e47a3ccSLukas Czerner 	 * implement the extra complexity (e.g. RCU protection) that would be
28136e47a3ccSLukas Czerner 	 * needed to allow it to be set or changed during remount.  We do allow
28146e47a3ccSLukas Czerner 	 * it to be specified during remount, but only if there is no change.
28156e47a3ccSLukas Czerner 	 */
28166e47a3ccSLukas Czerner 	if ((ctx->spec & EXT4_SPEC_DUMMY_ENCRYPTION) &&
28176e47a3ccSLukas Czerner 	    is_remount && !sbi->s_dummy_enc_policy.policy) {
28186e47a3ccSLukas Czerner 		ext4_msg(NULL, KERN_WARNING,
28196e47a3ccSLukas Czerner 			 "Can't set test_dummy_encryption on remount");
28206e47a3ccSLukas Czerner 		return -1;
28216e47a3ccSLukas Czerner 	}
28226e47a3ccSLukas Czerner #endif
28236e47a3ccSLukas Czerner 
28246e47a3ccSLukas Czerner 	if ((ctx->spec & EXT4_SPEC_DATAJ) && is_remount) {
28256e47a3ccSLukas Czerner 		if (!sbi->s_journal) {
28266e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_WARNING,
28276e47a3ccSLukas Czerner 				 "Remounting file system with no journal "
28286e47a3ccSLukas Czerner 				 "so ignoring journalled data option");
28296e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
28304c246728SLukas Czerner 		} else if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS) !=
28314c246728SLukas Czerner 			   test_opt(sb, DATA_FLAGS)) {
28326e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "Cannot change data mode "
28336e47a3ccSLukas Czerner 				 "on remount");
28346e47a3ccSLukas Czerner 			return -EINVAL;
28356e47a3ccSLukas Czerner 		}
28366e47a3ccSLukas Czerner 	}
28376e47a3ccSLukas Czerner 
28386e47a3ccSLukas Czerner 	if (is_remount) {
28396e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
28406e47a3ccSLukas Czerner 		    (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
28416e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "can't mount with "
28426e47a3ccSLukas Czerner 				 "both data=journal and dax");
28436e47a3ccSLukas Czerner 			return -EINVAL;
28446e47a3ccSLukas Czerner 		}
28456e47a3ccSLukas Czerner 
28466e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
28476e47a3ccSLukas Czerner 		    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
28486e47a3ccSLukas Czerner 		     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
28496e47a3ccSLukas Czerner fail_dax_change_remount:
28506e47a3ccSLukas Czerner 			ext4_msg(NULL, KERN_ERR, "can't change "
28516e47a3ccSLukas Czerner 				 "dax mount option while remounting");
28526e47a3ccSLukas Czerner 			return -EINVAL;
28536e47a3ccSLukas Czerner 		} else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER) &&
28546e47a3ccSLukas Czerner 			 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
28556e47a3ccSLukas Czerner 			  (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) {
28566e47a3ccSLukas Czerner 			goto fail_dax_change_remount;
28576e47a3ccSLukas Czerner 		} else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE) &&
28586e47a3ccSLukas Czerner 			   ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
28596e47a3ccSLukas Czerner 			    (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
28606e47a3ccSLukas Czerner 			    !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) {
28616e47a3ccSLukas Czerner 			goto fail_dax_change_remount;
28626e47a3ccSLukas Czerner 		}
28636e47a3ccSLukas Czerner 	}
28646e47a3ccSLukas Czerner 
2865b6bd2435SLukas Czerner 	return ext4_check_quota_consistency(fc, sb);
2866b6bd2435SLukas Czerner }
2867b6bd2435SLukas Czerner 
28686e47a3ccSLukas Czerner static int ext4_apply_options(struct fs_context *fc, struct super_block *sb)
28696e47a3ccSLukas Czerner {
28706e47a3ccSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
28716e47a3ccSLukas Czerner 	struct ext4_sb_info *sbi = fc->s_fs_info;
28726e47a3ccSLukas Czerner 	int ret = 0;
28736e47a3ccSLukas Czerner 
28746e47a3ccSLukas Czerner 	sbi->s_mount_opt &= ~ctx->mask_s_mount_opt;
28756e47a3ccSLukas Czerner 	sbi->s_mount_opt |= ctx->vals_s_mount_opt;
28766e47a3ccSLukas Czerner 	sbi->s_mount_opt2 &= ~ctx->mask_s_mount_opt2;
28776e47a3ccSLukas Czerner 	sbi->s_mount_opt2 |= ctx->vals_s_mount_opt2;
28786e47a3ccSLukas Czerner 	sbi->s_mount_flags &= ~ctx->mask_s_mount_flags;
28796e47a3ccSLukas Czerner 	sbi->s_mount_flags |= ctx->vals_s_mount_flags;
28806e47a3ccSLukas Czerner 	sb->s_flags &= ~ctx->mask_s_flags;
28816e47a3ccSLukas Czerner 	sb->s_flags |= ctx->vals_s_flags;
28826e47a3ccSLukas Czerner 
2883960e0ab6SLukas Czerner 	/*
2884960e0ab6SLukas Czerner 	 * i_version differs from common mount option iversion so we have
2885960e0ab6SLukas Czerner 	 * to let vfs know that it was set, otherwise it would get cleared
2886960e0ab6SLukas Czerner 	 * on remount
2887960e0ab6SLukas Czerner 	 */
2888960e0ab6SLukas Czerner 	if (ctx->mask_s_flags & SB_I_VERSION)
2889960e0ab6SLukas Czerner 		fc->sb_flags |= SB_I_VERSION;
2890960e0ab6SLukas Czerner 
28916e47a3ccSLukas Czerner #define APPLY(X) ({ if (ctx->spec & EXT4_SPEC_##X) sbi->X = ctx->X; })
28926e47a3ccSLukas Czerner 	APPLY(s_commit_interval);
28936e47a3ccSLukas Czerner 	APPLY(s_stripe);
28946e47a3ccSLukas Czerner 	APPLY(s_max_batch_time);
28956e47a3ccSLukas Czerner 	APPLY(s_min_batch_time);
28966e47a3ccSLukas Czerner 	APPLY(s_want_extra_isize);
28976e47a3ccSLukas Czerner 	APPLY(s_inode_readahead_blks);
28986e47a3ccSLukas Czerner 	APPLY(s_max_dir_size_kb);
28996e47a3ccSLukas Czerner 	APPLY(s_li_wait_mult);
29006e47a3ccSLukas Czerner 	APPLY(s_resgid);
29016e47a3ccSLukas Czerner 	APPLY(s_resuid);
29026e47a3ccSLukas Czerner 
29036e47a3ccSLukas Czerner #ifdef CONFIG_EXT4_DEBUG
29046e47a3ccSLukas Czerner 	APPLY(s_fc_debug_max_replay);
29056e47a3ccSLukas Czerner #endif
29066e47a3ccSLukas Czerner 
29076e47a3ccSLukas Czerner 	ext4_apply_quota_options(fc, sb);
29086e47a3ccSLukas Czerner 
29096e47a3ccSLukas Czerner 	if (ctx->spec & EXT4_SPEC_DUMMY_ENCRYPTION)
29106e47a3ccSLukas Czerner 		ret = ext4_set_test_dummy_encryption(sb, ctx->test_dummy_enc_arg);
29116e47a3ccSLukas Czerner 
29126e47a3ccSLukas Czerner 	return ret;
29136e47a3ccSLukas Czerner }
29146e47a3ccSLukas Czerner 
29156e47a3ccSLukas Czerner 
2916da812f61SLukas Czerner static int ext4_validate_options(struct fs_context *fc)
29174c94bff9SLukas Czerner {
2918ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
29196e47a3ccSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
29204c94bff9SLukas Czerner 	char *usr_qf_name, *grp_qf_name;
29216e47a3ccSLukas Czerner 
29226e47a3ccSLukas Czerner 	usr_qf_name = ctx->s_qf_names[USRQUOTA];
29236e47a3ccSLukas Czerner 	grp_qf_name = ctx->s_qf_names[GRPQUOTA];
29246e47a3ccSLukas Czerner 
292533458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
29266e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) && usr_qf_name)
29276e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
2928ac27a0ecSDave Kleikamp 
29296e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) && grp_qf_name)
29306e47a3ccSLukas Czerner 			ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
2931ac27a0ecSDave Kleikamp 
29326e47a3ccSLukas Czerner 		if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
29336e47a3ccSLukas Czerner 		    ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA)) {
2934da812f61SLukas Czerner 			ext4_msg(NULL, KERN_ERR, "old and new quota "
2935b31e1552SEric Sandeen 				 "format mixing");
2936da812f61SLukas Czerner 			return -EINVAL;
2937ac27a0ecSDave Kleikamp 		}
2938ac27a0ecSDave Kleikamp 	}
2939ac27a0ecSDave Kleikamp #endif
29406e47a3ccSLukas Czerner 	return 1;
2941ac27a0ecSDave Kleikamp }
2942ac27a0ecSDave Kleikamp 
29432adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
29442adf6da8STheodore Ts'o 					   struct super_block *sb)
29452adf6da8STheodore Ts'o {
29462adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
29472adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
294833458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
29492adf6da8STheodore Ts'o 
29502adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
29512adf6da8STheodore Ts'o 		char *fmtname = "";
29522adf6da8STheodore Ts'o 
29532adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
29542adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
29552adf6da8STheodore Ts'o 			fmtname = "vfsold";
29562adf6da8STheodore Ts'o 			break;
29572adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
29582adf6da8STheodore Ts'o 			fmtname = "vfsv0";
29592adf6da8STheodore Ts'o 			break;
29602adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
29612adf6da8STheodore Ts'o 			fmtname = "vfsv1";
29622adf6da8STheodore Ts'o 			break;
29632adf6da8STheodore Ts'o 		}
29642adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
29652adf6da8STheodore Ts'o 	}
29662adf6da8STheodore Ts'o 
296733458eabSTheodore Ts'o 	rcu_read_lock();
296833458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
296933458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
297033458eabSTheodore Ts'o 	if (usr_qf_name)
297133458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
297233458eabSTheodore Ts'o 	if (grp_qf_name)
297333458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
297433458eabSTheodore Ts'o 	rcu_read_unlock();
29752adf6da8STheodore Ts'o #endif
29762adf6da8STheodore Ts'o }
29772adf6da8STheodore Ts'o 
29785a916be1STheodore Ts'o static const char *token2str(int token)
29795a916be1STheodore Ts'o {
298097d8a670SLukas Czerner 	const struct fs_parameter_spec *spec;
29815a916be1STheodore Ts'o 
298297d8a670SLukas Czerner 	for (spec = ext4_param_specs; spec->name != NULL; spec++)
298397d8a670SLukas Czerner 		if (spec->opt == token && !spec->type)
29845a916be1STheodore Ts'o 			break;
298597d8a670SLukas Czerner 	return spec->name;
29865a916be1STheodore Ts'o }
29875a916be1STheodore Ts'o 
29882adf6da8STheodore Ts'o /*
29892adf6da8STheodore Ts'o  * Show an option if
29902adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
29912adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
29922adf6da8STheodore Ts'o  */
299366acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
299466acdcf4STheodore Ts'o 			      int nodefs)
29952adf6da8STheodore Ts'o {
29962adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
29972adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
299868afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
29995a916be1STheodore Ts'o 	const struct mount_opts *m;
300066acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
30012adf6da8STheodore Ts'o 
300266acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
300366acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
30042adf6da8STheodore Ts'o 
30052adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
30065a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
30075a916be1STheodore Ts'o 
30085a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
30095a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
30105a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
3011ba2e524dSLukas Czerner 		    m->flags & MOPT_SKIP)
30125a916be1STheodore Ts'o 			continue;
301368afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
30145a916be1STheodore Ts'o 			continue; /* skip if same as the default */
30155a916be1STheodore Ts'o 		if ((want_set &&
30165a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
30175a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
30185a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
30195a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
30205a916be1STheodore Ts'o 	}
30215a916be1STheodore Ts'o 
302208cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
30235a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
302408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
302508cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
302608cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
30275a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
302808cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
302908cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
303066acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
30315a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
30325a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
30332adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
30345a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
30352adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
30365a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
303766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
30385a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
303966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
30405a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
304166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
30425a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
3043357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
30445a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
304566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
30465a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
304768afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
304868afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
30492adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
30505a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
30512adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
30525a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
30532adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
30545a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
30555a916be1STheodore Ts'o 	}
305666acdcf4STheodore Ts'o 	if (nodefs ||
305766acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
30585a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
30592adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
30602adf6da8STheodore Ts'o 
3061ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
306266acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
30635a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
3064df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
3065df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
30667915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
30677915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
3068ed318a6cSEric Biggers 
3069ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
30702adf6da8STheodore Ts'o 
30714f74d15fSEric Biggers 	if (sb->s_flags & SB_INLINECRYPT)
30724f74d15fSEric Biggers 		SEQ_OPTS_PUTS("inlinecrypt");
30734f74d15fSEric Biggers 
30749cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
30759cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
30769cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
30779cb20f94SIra Weiny 		else
30789cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
30799cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
30809cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
30819cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
30829cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
30839cb20f94SIra Weiny 	}
30842adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
30852adf6da8STheodore Ts'o 	return 0;
30862adf6da8STheodore Ts'o }
30872adf6da8STheodore Ts'o 
308866acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
308966acdcf4STheodore Ts'o {
309066acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
309166acdcf4STheodore Ts'o }
309266acdcf4STheodore Ts'o 
3093ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
309466acdcf4STheodore Ts'o {
309566acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
309666acdcf4STheodore Ts'o 	int rc;
309766acdcf4STheodore Ts'o 
3098bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
309966acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
310066acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
310166acdcf4STheodore Ts'o 	return rc;
310266acdcf4STheodore Ts'o }
310366acdcf4STheodore Ts'o 
3104617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
3105ac27a0ecSDave Kleikamp 			    int read_only)
3106ac27a0ecSDave Kleikamp {
3107617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3108c89128a0SJaegeuk Kim 	int err = 0;
3109ac27a0ecSDave Kleikamp 
3110617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
3111b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
3112b31e1552SEric Sandeen 			 "forcing read-only mode");
3113c89128a0SJaegeuk Kim 		err = -EROFS;
31145adaccacSyangerkun 		goto done;
3115ac27a0ecSDave Kleikamp 	}
3116ac27a0ecSDave Kleikamp 	if (read_only)
3117281b5995STheodore Ts'o 		goto done;
3118617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
3119b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
3120b31e1552SEric Sandeen 			 "running e2fsck is recommended");
3121c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
3122b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3123b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
3124b31e1552SEric Sandeen 			 "running e2fsck is recommended");
3125ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
3126ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
3127ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
3128b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3129b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
3130b31e1552SEric Sandeen 			 "running e2fsck is recommended");
3131ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
31326a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
31336a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
3134b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3135b31e1552SEric Sandeen 			 "warning: checktime reached, "
3136b31e1552SEric Sandeen 			 "running e2fsck is recommended");
31370390131bSFrank Mayhar 	if (!sbi->s_journal)
3138216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
3139ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
3140617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
3141e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
31426a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
314302f310fcSJan Kara 	if (sbi->s_journal) {
3144e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
314502f310fcSJan Kara 		if (ext4_has_feature_orphan_file(sb))
314602f310fcSJan Kara 			ext4_set_feature_orphan_present(sb);
314702f310fcSJan Kara 	}
3148ac27a0ecSDave Kleikamp 
31494392fbc4SJan Kara 	err = ext4_commit_super(sb);
3150281b5995STheodore Ts'o done:
3151ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
3152a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
3153a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
3154ac27a0ecSDave Kleikamp 			sb->s_blocksize,
3155ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
3156617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
3157617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
3158a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
3159ac27a0ecSDave Kleikamp 
31607abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
3161c89128a0SJaegeuk Kim 	return err;
3162ac27a0ecSDave Kleikamp }
3163ac27a0ecSDave Kleikamp 
3164117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
3165117fff10STheodore Ts'o {
3166117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
31677c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
316837b0b6b8SDan Carpenter 	int size, i, j;
3169117fff10STheodore Ts'o 
3170117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
3171117fff10STheodore Ts'o 		return 0;
3172117fff10STheodore Ts'o 
3173117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
3174117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
3175117fff10STheodore Ts'o 		return 0;
3176117fff10STheodore Ts'o 
31777c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
31787c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
3179117fff10STheodore Ts'o 	if (!new_groups) {
31807c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
31817c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
3182117fff10STheodore Ts'o 		return -ENOMEM;
3183117fff10STheodore Ts'o 	}
31847c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
31857c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
31867c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
31877c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
31887c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
318937b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
319037b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
31917c990728SSuraj Jitindar Singh 			kvfree(new_groups);
31927c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
31937c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
31947c990728SSuraj Jitindar Singh 			return -ENOMEM;
3195117fff10STheodore Ts'o 		}
31967c990728SSuraj Jitindar Singh 	}
31977c990728SSuraj Jitindar Singh 	rcu_read_lock();
31987c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
31997c990728SSuraj Jitindar Singh 	if (old_groups)
32007c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
32017c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
32027c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
32037c990728SSuraj Jitindar Singh 	rcu_read_unlock();
32047c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
32057c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
32067c990728SSuraj Jitindar Singh 	if (old_groups)
32077c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
3208117fff10STheodore Ts'o 	return 0;
3209117fff10STheodore Ts'o }
3210117fff10STheodore Ts'o 
3211772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
3212772cb7c8SJose R. Santos {
3213772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3214772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
32157c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
3216772cb7c8SJose R. Santos 	ext4_group_t flex_group;
3217117fff10STheodore Ts'o 	int i, err;
3218772cb7c8SJose R. Santos 
3219503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
3220d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
3221772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
3222772cb7c8SJose R. Santos 		return 1;
3223772cb7c8SJose R. Santos 	}
3224772cb7c8SJose R. Santos 
3225117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
3226117fff10STheodore Ts'o 	if (err)
3227772cb7c8SJose R. Santos 		goto failed;
3228772cb7c8SJose R. Santos 
3229772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
323088b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3231772cb7c8SJose R. Santos 
3232772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
32337c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
32347c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
323590ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
32367c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
32377c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
3238772cb7c8SJose R. Santos 	}
3239772cb7c8SJose R. Santos 
3240772cb7c8SJose R. Santos 	return 1;
3241772cb7c8SJose R. Santos failed:
3242772cb7c8SJose R. Santos 	return 0;
3243772cb7c8SJose R. Santos }
3244772cb7c8SJose R. Santos 
3245e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
3246717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
3247717d50e4SAndreas Dilger {
3248b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
3249717d50e4SAndreas Dilger 	__u16 crc = 0;
3250717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
3251e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3252717d50e4SAndreas Dilger 
32539aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
3254feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
3255feb0ab32SDarrick J. Wong 		__u32 csum32;
3256b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
3257feb0ab32SDarrick J. Wong 
3258feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
3259feb0ab32SDarrick J. Wong 				     sizeof(le_group));
3260b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
3261b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
3262b47820edSDaeho Jeong 				     sizeof(dummy_csum));
3263b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
3264b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
3265b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
3266b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
3267feb0ab32SDarrick J. Wong 
3268feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
3269feb0ab32SDarrick J. Wong 		goto out;
3270feb0ab32SDarrick J. Wong 	}
3271feb0ab32SDarrick J. Wong 
3272feb0ab32SDarrick J. Wong 	/* old crc16 code */
3273e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
3274813d32f9SDarrick J. Wong 		return 0;
3275813d32f9SDarrick J. Wong 
3276717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
3277717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
3278717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
3279717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
3280717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
3281e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
3282717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
3283717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
3284717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
3285717d50e4SAndreas Dilger 				offset);
3286717d50e4SAndreas Dilger 
3287feb0ab32SDarrick J. Wong out:
3288717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
3289717d50e4SAndreas Dilger }
3290717d50e4SAndreas Dilger 
3291feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
3292717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
3293717d50e4SAndreas Dilger {
3294feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
3295e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
3296717d50e4SAndreas Dilger 		return 0;
3297717d50e4SAndreas Dilger 
3298717d50e4SAndreas Dilger 	return 1;
3299717d50e4SAndreas Dilger }
3300717d50e4SAndreas Dilger 
3301feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
3302feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
3303feb0ab32SDarrick J. Wong {
3304feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
3305feb0ab32SDarrick J. Wong 		return;
3306e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
3307feb0ab32SDarrick J. Wong }
3308feb0ab32SDarrick J. Wong 
3309ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
3310bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
3311829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
3312bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
3313ac27a0ecSDave Kleikamp {
3314617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3315617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
3316617ba13bSMingming Cao 	ext4_fsblk_t last_block;
331744de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
3318bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
3319bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
3320bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
3321ce421581SJose R. Santos 	int flexbg_flag = 0;
3322bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
3323ac27a0ecSDave Kleikamp 
3324e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
3325ce421581SJose R. Santos 		flexbg_flag = 1;
3326ce421581SJose R. Santos 
3327617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
3328ac27a0ecSDave Kleikamp 
3329197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
3330197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
3331197cd65aSAkinobu Mita 
3332ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
3333bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
3334ac27a0ecSDave Kleikamp 		else
3335ac27a0ecSDave Kleikamp 			last_block = first_block +
3336617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
3337ac27a0ecSDave Kleikamp 
3338bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
3339bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3340bfff6873SLukas Czerner 			grp = i;
3341bfff6873SLukas Czerner 
33428fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
3343829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
3344829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3345829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
3346829fa70dSTheodore Ts'o 				 "superblock", i);
334718db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
334818db4b4eSTheodore Ts'o 				return 0;
3349829fa70dSTheodore Ts'o 		}
335077260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
335177260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
335277260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
335377260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
335477260807STheodore Ts'o 				 "block group descriptors", i);
335577260807STheodore Ts'o 			if (!sb_rdonly(sb))
335677260807STheodore Ts'o 				return 0;
335777260807STheodore Ts'o 		}
33582b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
3359b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3360a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
3361b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
3362ac27a0ecSDave Kleikamp 			return 0;
3363ac27a0ecSDave Kleikamp 		}
33648fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
3365829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
3366829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3367829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
3368829fa70dSTheodore Ts'o 				 "superblock", i);
336918db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
337018db4b4eSTheodore Ts'o 				return 0;
3371829fa70dSTheodore Ts'o 		}
337277260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
337377260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
337477260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
337577260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
337677260807STheodore Ts'o 				 "block group descriptors", i);
337777260807STheodore Ts'o 			if (!sb_rdonly(sb))
337877260807STheodore Ts'o 				return 0;
337977260807STheodore Ts'o 		}
33802b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
3381b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3382a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
3383b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
3384ac27a0ecSDave Kleikamp 			return 0;
3385ac27a0ecSDave Kleikamp 		}
33868fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
3387829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
3388829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3389829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
3390829fa70dSTheodore Ts'o 				 "superblock", i);
339118db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
339218db4b4eSTheodore Ts'o 				return 0;
3393829fa70dSTheodore Ts'o 		}
339477260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
339577260807STheodore Ts'o 		    inode_table <= last_bg_block) {
339677260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
339777260807STheodore Ts'o 				 "Inode table for group %u overlaps "
339877260807STheodore Ts'o 				 "block group descriptors", i);
339977260807STheodore Ts'o 			if (!sb_rdonly(sb))
340077260807STheodore Ts'o 				return 0;
340177260807STheodore Ts'o 		}
3402bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
34032b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
3404b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3405a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
3406b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
3407ac27a0ecSDave Kleikamp 			return 0;
3408ac27a0ecSDave Kleikamp 		}
3409955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
3410feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
3411b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3412b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
3413e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
3414fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
3415bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
3416955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
3417717d50e4SAndreas Dilger 				return 0;
3418717d50e4SAndreas Dilger 			}
34197ee1ec4cSLi Zefan 		}
3420955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
3421ce421581SJose R. Santos 		if (!flexbg_flag)
3422617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
3423ac27a0ecSDave Kleikamp 	}
3424bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
3425bfff6873SLukas Czerner 		*first_not_zeroed = grp;
3426ac27a0ecSDave Kleikamp 	return 1;
3427ac27a0ecSDave Kleikamp }
3428ac27a0ecSDave Kleikamp 
3429cd2291a4SEric Sandeen /*
3430cd2291a4SEric Sandeen  * Maximal extent format file size.
3431cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
3432cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
3433cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
3434cd2291a4SEric Sandeen  * so that won't be a limiting factor.
3435cd2291a4SEric Sandeen  *
3436f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
3437f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
3438f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
3439f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
3440f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3441f17722f9SLukas Czerner  *
3442cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3443cd2291a4SEric Sandeen  */
3444f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
3445cd2291a4SEric Sandeen {
3446cd2291a4SEric Sandeen 	loff_t res;
3447cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
3448cd2291a4SEric Sandeen 
344972deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
345072deb455SChristoph Hellwig 
345172deb455SChristoph Hellwig 	if (!has_huge_files) {
3452cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
3453cd2291a4SEric Sandeen 
3454cd2291a4SEric Sandeen 		/* total blocks in file system block size */
3455cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
3456cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
3457cd2291a4SEric Sandeen 	}
3458cd2291a4SEric Sandeen 
3459f17722f9SLukas Czerner 	/*
3460f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
3461f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
3462f17722f9SLukas Czerner 	 * size
3463f17722f9SLukas Czerner 	 */
3464f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
3465cd2291a4SEric Sandeen 	res <<= blkbits;
3466cd2291a4SEric Sandeen 
3467cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
3468cd2291a4SEric Sandeen 	if (res > upper_limit)
3469cd2291a4SEric Sandeen 		res = upper_limit;
3470cd2291a4SEric Sandeen 
3471cd2291a4SEric Sandeen 	return res;
3472cd2291a4SEric Sandeen }
3473ac27a0ecSDave Kleikamp 
3474ac27a0ecSDave Kleikamp /*
3475cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
34760fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
34770fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
3478ac27a0ecSDave Kleikamp  */
3479f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3480ac27a0ecSDave Kleikamp {
348175ca6ad4SRitesh Harjani 	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
34820fc1b451SAneesh Kumar K.V 	int meta_blocks;
348375ca6ad4SRitesh Harjani 
348475ca6ad4SRitesh Harjani 	/*
348575ca6ad4SRitesh Harjani 	 * This is calculated to be the largest file size for a dense, block
34860b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
34870b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
34880b8e58a1SAndreas Dilger 	 *
34890b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
34900b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
34910fc1b451SAneesh Kumar K.V 	 */
349272deb455SChristoph Hellwig 	if (!has_huge_files) {
34930fc1b451SAneesh Kumar K.V 		/*
349472deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
349572deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
349672deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
34970fc1b451SAneesh Kumar K.V 		 */
34980fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
34990fc1b451SAneesh Kumar K.V 
35000fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
35010fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
35020fc1b451SAneesh Kumar K.V 
35030fc1b451SAneesh Kumar K.V 	} else {
35048180a562SAneesh Kumar K.V 		/*
35058180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
35068180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
35078180a562SAneesh Kumar K.V 		 * represent total number of blocks in
35088180a562SAneesh Kumar K.V 		 * file system block size
35098180a562SAneesh Kumar K.V 		 */
35100fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
35110fc1b451SAneesh Kumar K.V 
35120fc1b451SAneesh Kumar K.V 	}
35130fc1b451SAneesh Kumar K.V 
35140fc1b451SAneesh Kumar K.V 	/* indirect blocks */
35150fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
35160fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
35170fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
35180fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
35190fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
35200fc1b451SAneesh Kumar K.V 
35210fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
35220fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
3523ac27a0ecSDave Kleikamp 
3524ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
3525ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
3526ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
3527ac27a0ecSDave Kleikamp 	res <<= bits;
3528ac27a0ecSDave Kleikamp 	if (res > upper_limit)
3529ac27a0ecSDave Kleikamp 		res = upper_limit;
35300fc1b451SAneesh Kumar K.V 
35310fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
35320fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
35330fc1b451SAneesh Kumar K.V 
353475ca6ad4SRitesh Harjani 	return (loff_t)res;
3535ac27a0ecSDave Kleikamp }
3536ac27a0ecSDave Kleikamp 
3537617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
353870bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3539ac27a0ecSDave Kleikamp {
3540617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3541fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3542ac27a0ecSDave Kleikamp 	int has_super = 0;
3543ac27a0ecSDave Kleikamp 
3544ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3545ac27a0ecSDave Kleikamp 
3546e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
354770bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3548ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3549617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3550ac27a0ecSDave Kleikamp 		has_super = 1;
35510b8e58a1SAndreas Dilger 
3552bd63f6b0SDarrick J. Wong 	/*
3553bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3554bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3555bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3556bd63f6b0SDarrick J. Wong 	 * compensate.
3557bd63f6b0SDarrick J. Wong 	 */
3558bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
355949598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3560bd63f6b0SDarrick J. Wong 		has_super++;
3561bd63f6b0SDarrick J. Wong 
3562617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3563ac27a0ecSDave Kleikamp }
3564ac27a0ecSDave Kleikamp 
3565c9de560dSAlex Tomas /**
3566c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3567c9de560dSAlex Tomas  * @sbi: In memory super block info
3568c9de560dSAlex Tomas  *
3569c9de560dSAlex Tomas  * If we have specified it via mount option, then
3570c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3571c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3572c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3573c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3574c9de560dSAlex Tomas  *
3575c9de560dSAlex Tomas  */
3576c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3577c9de560dSAlex Tomas {
3578c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3579c9de560dSAlex Tomas 	unsigned long stripe_width =
3580c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
35813eb08658SDan Ehrenberg 	int ret;
3582c9de560dSAlex Tomas 
3583c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
35843eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
35855469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
35863eb08658SDan Ehrenberg 		ret = stripe_width;
35875469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
35883eb08658SDan Ehrenberg 		ret = stride;
35893eb08658SDan Ehrenberg 	else
35903eb08658SDan Ehrenberg 		ret = 0;
3591c9de560dSAlex Tomas 
35923eb08658SDan Ehrenberg 	/*
35933eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
35943eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
35953eb08658SDan Ehrenberg 	 */
35963eb08658SDan Ehrenberg 	if (ret <= 1)
35973eb08658SDan Ehrenberg 		ret = 0;
3598c9de560dSAlex Tomas 
35993eb08658SDan Ehrenberg 	return ret;
3600c9de560dSAlex Tomas }
3601ac27a0ecSDave Kleikamp 
3602a13fb1a4SEric Sandeen /*
3603a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3604a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3605a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3606a13fb1a4SEric Sandeen  * 0 if it cannot be.
3607a13fb1a4SEric Sandeen  */
360825c6d98fSJan Kara int ext4_feature_set_ok(struct super_block *sb, int readonly)
3609a13fb1a4SEric Sandeen {
3610e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3611a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3612a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3613a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3614a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3615a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3616a13fb1a4SEric Sandeen 		return 0;
3617a13fb1a4SEric Sandeen 	}
3618a13fb1a4SEric Sandeen 
3619c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3620c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3621c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3622c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3623c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3624c83ad55eSGabriel Krisman Bertazi 		return 0;
3625c83ad55eSGabriel Krisman Bertazi 	}
3626c83ad55eSGabriel Krisman Bertazi #endif
3627c83ad55eSGabriel Krisman Bertazi 
3628a13fb1a4SEric Sandeen 	if (readonly)
3629a13fb1a4SEric Sandeen 		return 1;
3630a13fb1a4SEric Sandeen 
3631e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
36322cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
36331751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
36342cb5cc8bSDarrick J. Wong 		return 1;
36352cb5cc8bSDarrick J. Wong 	}
36362cb5cc8bSDarrick J. Wong 
3637a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3638e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3639a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3640a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3641a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3642a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3643a13fb1a4SEric Sandeen 		return 0;
3644a13fb1a4SEric Sandeen 	}
3645e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3646bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3647bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3648bab08ab9STheodore Ts'o 			 "extents feature\n");
3649bab08ab9STheodore Ts'o 		return 0;
3650bab08ab9STheodore Ts'o 	}
36517c319d32SAditya Kali 
36529db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3653d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3654d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
36557c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3656d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3657689c958cSLi Xi 		return 0;
3658689c958cSLi Xi 	}
36597c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3660a13fb1a4SEric Sandeen 	return 1;
3661a13fb1a4SEric Sandeen }
3662a13fb1a4SEric Sandeen 
366366e61a9eSTheodore Ts'o /*
366466e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
366566e61a9eSTheodore Ts'o  * on the file system
366666e61a9eSTheodore Ts'o  */
3667235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
366866e61a9eSTheodore Ts'o {
3669235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3670235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3671235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
367266e61a9eSTheodore Ts'o 
367366e61a9eSTheodore Ts'o 	if (es->s_error_count)
3674ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3675ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
367666e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
367766e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
36786a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
36796a0678a7SArnd Bergmann 		       sb->s_id,
36806a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
368166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
368266e61a9eSTheodore Ts'o 		       es->s_first_error_func,
368366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
368466e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3685651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
368666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
368766e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3688651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
368966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3690651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
369166e61a9eSTheodore Ts'o 	}
369266e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
36936a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
36946a0678a7SArnd Bergmann 		       sb->s_id,
36956a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
369666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
369766e61a9eSTheodore Ts'o 		       es->s_last_error_func,
369866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
369966e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3700651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
370166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
370266e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3703651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
370466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3705651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
370666e61a9eSTheodore Ts'o 	}
370766e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
370866e61a9eSTheodore Ts'o }
370966e61a9eSTheodore Ts'o 
3710bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3711bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3712bfff6873SLukas Czerner {
3713bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
37143d392b26STheodore Ts'o 	struct super_block *sb = elr->lr_super;
37153d392b26STheodore Ts'o 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
37163d392b26STheodore Ts'o 	ext4_group_t group = elr->lr_next_group;
37173d392b26STheodore Ts'o 	unsigned int prefetch_ios = 0;
3718bfff6873SLukas Czerner 	int ret = 0;
371939fec688SShaoying Xu 	u64 start_time;
3720bfff6873SLukas Czerner 
37213d392b26STheodore Ts'o 	if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
37223d392b26STheodore Ts'o 		elr->lr_next_group = ext4_mb_prefetch(sb, group,
37233d392b26STheodore Ts'o 				EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
37243d392b26STheodore Ts'o 		if (prefetch_ios)
37253d392b26STheodore Ts'o 			ext4_mb_prefetch_fini(sb, elr->lr_next_group,
37263d392b26STheodore Ts'o 					      prefetch_ios);
37273d392b26STheodore Ts'o 		trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
37283d392b26STheodore Ts'o 					    prefetch_ios);
37293d392b26STheodore Ts'o 		if (group >= elr->lr_next_group) {
37303d392b26STheodore Ts'o 			ret = 1;
37313d392b26STheodore Ts'o 			if (elr->lr_first_not_zeroed != ngroups &&
37323d392b26STheodore Ts'o 			    !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
37333d392b26STheodore Ts'o 				elr->lr_next_group = elr->lr_first_not_zeroed;
37343d392b26STheodore Ts'o 				elr->lr_mode = EXT4_LI_MODE_ITABLE;
37353d392b26STheodore Ts'o 				ret = 0;
37363d392b26STheodore Ts'o 			}
37373d392b26STheodore Ts'o 		}
37383d392b26STheodore Ts'o 		return ret;
37393d392b26STheodore Ts'o 	}
3740bfff6873SLukas Czerner 
37413d392b26STheodore Ts'o 	for (; group < ngroups; group++) {
3742bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3743bfff6873SLukas Czerner 		if (!gdp) {
3744bfff6873SLukas Czerner 			ret = 1;
3745bfff6873SLukas Czerner 			break;
3746bfff6873SLukas Czerner 		}
3747bfff6873SLukas Czerner 
3748bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3749bfff6873SLukas Czerner 			break;
3750bfff6873SLukas Czerner 	}
3751bfff6873SLukas Czerner 
37527f511862STheodore Ts'o 	if (group >= ngroups)
3753bfff6873SLukas Czerner 		ret = 1;
3754bfff6873SLukas Czerner 
3755bfff6873SLukas Czerner 	if (!ret) {
375639fec688SShaoying Xu 		start_time = ktime_get_real_ns();
3757bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3758bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
37593d392b26STheodore Ts'o 		trace_ext4_lazy_itable_init(sb, group);
3760bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
376139fec688SShaoying Xu 			elr->lr_timeout = nsecs_to_jiffies((ktime_get_real_ns() - start_time) *
376239fec688SShaoying Xu 				EXT4_SB(elr->lr_super)->s_li_wait_mult);
3763bfff6873SLukas Czerner 		}
3764bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3765bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3766bfff6873SLukas Czerner 	}
3767bfff6873SLukas Czerner 	return ret;
3768bfff6873SLukas Czerner }
3769bfff6873SLukas Czerner 
3770bfff6873SLukas Czerner /*
3771bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
37724ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3773bfff6873SLukas Czerner  */
3774bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3775bfff6873SLukas Czerner {
3776bfff6873SLukas Czerner 	if (!elr)
3777bfff6873SLukas Czerner 		return;
3778bfff6873SLukas Czerner 
3779bfff6873SLukas Czerner 	list_del(&elr->lr_request);
37803d392b26STheodore Ts'o 	EXT4_SB(elr->lr_super)->s_li_request = NULL;
3781bfff6873SLukas Czerner 	kfree(elr);
3782bfff6873SLukas Czerner }
3783bfff6873SLukas Czerner 
3784bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3785bfff6873SLukas Czerner {
37861bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
37871bb933fbSLukas Czerner 	if (!ext4_li_info) {
37881bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3789bfff6873SLukas Czerner 		return;
37901bb933fbSLukas Czerner 	}
3791bfff6873SLukas Czerner 
3792bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
37931bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3794bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
37951bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3796bfff6873SLukas Czerner }
3797bfff6873SLukas Czerner 
37988f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
37998f1f7453SEric Sandeen 
3800bfff6873SLukas Czerner /*
3801bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3802bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3803bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3804bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3805bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3806bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3807bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3808bfff6873SLukas Czerner  */
3809bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3810bfff6873SLukas Czerner {
3811bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3812bfff6873SLukas Czerner 	struct list_head *pos, *n;
3813bfff6873SLukas Czerner 	struct ext4_li_request *elr;
38144ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3815bfff6873SLukas Czerner 
3816bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3817bfff6873SLukas Czerner 
3818bfff6873SLukas Czerner cont_thread:
3819bfff6873SLukas Czerner 	while (true) {
3820bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3821bfff6873SLukas Czerner 
3822bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3823bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3824bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3825bfff6873SLukas Czerner 			goto exit_thread;
3826bfff6873SLukas Czerner 		}
3827bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3828e22834f0SDmitry Monakhov 			int err = 0;
3829e22834f0SDmitry Monakhov 			int progress = 0;
3830bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3831bfff6873SLukas Czerner 					 lr_request);
3832bfff6873SLukas Czerner 
3833e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3834e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3835e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3836e22834f0SDmitry Monakhov 				continue;
3837e22834f0SDmitry Monakhov 			}
3838e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3839e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3840e22834f0SDmitry Monakhov 					progress = 1;
3841e22834f0SDmitry Monakhov 					/*
3842e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3843e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3844e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3845e22834f0SDmitry Monakhov 					 */
3846e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3847e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3848e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3849e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3850e22834f0SDmitry Monakhov 					n = pos->next;
3851e22834f0SDmitry Monakhov 				}
3852e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3853e22834f0SDmitry Monakhov 			}
3854b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3855e22834f0SDmitry Monakhov 			if (err) {
3856bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3857bfff6873SLukas Czerner 				continue;
3858bfff6873SLukas Czerner 			}
3859e22834f0SDmitry Monakhov 			if (!progress) {
3860e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3861e22834f0SDmitry Monakhov 					(prandom_u32()
3862e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3863b2c78cd0STheodore Ts'o 			}
3864bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3865bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3866bfff6873SLukas Czerner 		}
3867bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3868bfff6873SLukas Czerner 
3869a0acae0eSTejun Heo 		try_to_freeze();
3870bfff6873SLukas Czerner 
38714ed5c033SLukas Czerner 		cur = jiffies;
38724ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3873f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3874bfff6873SLukas Czerner 			cond_resched();
3875bfff6873SLukas Czerner 			continue;
3876bfff6873SLukas Czerner 		}
3877bfff6873SLukas Czerner 
38784ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
38794ed5c033SLukas Czerner 
38808f1f7453SEric Sandeen 		if (kthread_should_stop()) {
38818f1f7453SEric Sandeen 			ext4_clear_request_list();
38828f1f7453SEric Sandeen 			goto exit_thread;
38838f1f7453SEric Sandeen 		}
3884bfff6873SLukas Czerner 	}
3885bfff6873SLukas Czerner 
3886bfff6873SLukas Czerner exit_thread:
3887bfff6873SLukas Czerner 	/*
3888bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3889bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3890bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3891bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3892bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3893bfff6873SLukas Czerner 	 * new one.
3894bfff6873SLukas Czerner 	 */
3895bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3896bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3897bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3898bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3899bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3900bfff6873SLukas Czerner 		goto cont_thread;
3901bfff6873SLukas Czerner 	}
3902bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3903bfff6873SLukas Czerner 	kfree(ext4_li_info);
3904bfff6873SLukas Czerner 	ext4_li_info = NULL;
3905bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3906bfff6873SLukas Czerner 
3907bfff6873SLukas Czerner 	return 0;
3908bfff6873SLukas Czerner }
3909bfff6873SLukas Czerner 
3910bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3911bfff6873SLukas Czerner {
3912bfff6873SLukas Czerner 	struct list_head *pos, *n;
3913bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3914bfff6873SLukas Czerner 
3915bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3916bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3917bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3918bfff6873SLukas Czerner 				 lr_request);
3919bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3920bfff6873SLukas Czerner 	}
3921bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3922bfff6873SLukas Czerner }
3923bfff6873SLukas Czerner 
3924bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3925bfff6873SLukas Czerner {
39268f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
39278f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
39288f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
39298f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3930bfff6873SLukas Czerner 		ext4_clear_request_list();
3931bfff6873SLukas Czerner 		kfree(ext4_li_info);
3932bfff6873SLukas Czerner 		ext4_li_info = NULL;
393392b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3934bfff6873SLukas Czerner 				 "initialization thread\n",
3935bfff6873SLukas Czerner 				 err);
3936bfff6873SLukas Czerner 		return err;
3937bfff6873SLukas Czerner 	}
3938bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3939bfff6873SLukas Czerner 	return 0;
3940bfff6873SLukas Czerner }
3941bfff6873SLukas Czerner 
3942bfff6873SLukas Czerner /*
3943bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3944bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3945bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3946bfff6873SLukas Czerner  * groups and return total number of groups.
3947bfff6873SLukas Czerner  */
3948bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3949bfff6873SLukas Czerner {
3950bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3951bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3952bfff6873SLukas Czerner 
39538844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
39548844618dSTheodore Ts'o 		return ngroups;
39558844618dSTheodore Ts'o 
3956bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3957bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3958bfff6873SLukas Czerner 		if (!gdp)
3959bfff6873SLukas Czerner 			continue;
3960bfff6873SLukas Czerner 
396150122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3962bfff6873SLukas Czerner 			break;
3963bfff6873SLukas Czerner 	}
3964bfff6873SLukas Czerner 
3965bfff6873SLukas Czerner 	return group;
3966bfff6873SLukas Czerner }
3967bfff6873SLukas Czerner 
3968bfff6873SLukas Czerner static int ext4_li_info_new(void)
3969bfff6873SLukas Czerner {
3970bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3971bfff6873SLukas Czerner 
3972bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3973bfff6873SLukas Czerner 	if (!eli)
3974bfff6873SLukas Czerner 		return -ENOMEM;
3975bfff6873SLukas Czerner 
3976bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3977bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3978bfff6873SLukas Czerner 
3979bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3980bfff6873SLukas Czerner 
3981bfff6873SLukas Czerner 	ext4_li_info = eli;
3982bfff6873SLukas Czerner 
3983bfff6873SLukas Czerner 	return 0;
3984bfff6873SLukas Czerner }
3985bfff6873SLukas Czerner 
3986bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3987bfff6873SLukas Czerner 					    ext4_group_t start)
3988bfff6873SLukas Czerner {
3989bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3990bfff6873SLukas Czerner 
3991bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3992bfff6873SLukas Czerner 	if (!elr)
3993bfff6873SLukas Czerner 		return NULL;
3994bfff6873SLukas Czerner 
3995bfff6873SLukas Czerner 	elr->lr_super = sb;
39963d392b26STheodore Ts'o 	elr->lr_first_not_zeroed = start;
399721175ca4SHarshad Shirwadkar 	if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
39983d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_ITABLE;
3999bfff6873SLukas Czerner 		elr->lr_next_group = start;
400021175ca4SHarshad Shirwadkar 	} else {
400121175ca4SHarshad Shirwadkar 		elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
40023d392b26STheodore Ts'o 	}
4003bfff6873SLukas Czerner 
4004bfff6873SLukas Czerner 	/*
4005bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
4006bfff6873SLukas Czerner 	 * spread the inode table initialization requests
4007bfff6873SLukas Czerner 	 * better.
4008bfff6873SLukas Czerner 	 */
4009dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
4010dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
4011bfff6873SLukas Czerner 	return elr;
4012bfff6873SLukas Czerner }
4013bfff6873SLukas Czerner 
40147f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
4015bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
4016bfff6873SLukas Czerner {
4017bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
40187f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
401949598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
40206c5a6cb9SAndrew Morton 	int ret = 0;
4021bfff6873SLukas Czerner 
40227f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
402351ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
402451ce6511SLukas Czerner 		/*
402551ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
402651ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
402751ce6511SLukas Czerner 		 */
402851ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
40297f511862STheodore Ts'o 		goto out;
403051ce6511SLukas Czerner 	}
4031bfff6873SLukas Czerner 
403221175ca4SHarshad Shirwadkar 	if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
40333d392b26STheodore Ts'o 	    (first_not_zeroed == ngroups || sb_rdonly(sb) ||
40343d392b26STheodore Ts'o 	     !test_opt(sb, INIT_INODE_TABLE)))
40357f511862STheodore Ts'o 		goto out;
4036bfff6873SLukas Czerner 
4037bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
40387f511862STheodore Ts'o 	if (!elr) {
40397f511862STheodore Ts'o 		ret = -ENOMEM;
40407f511862STheodore Ts'o 		goto out;
40417f511862STheodore Ts'o 	}
4042bfff6873SLukas Czerner 
4043bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
4044bfff6873SLukas Czerner 		ret = ext4_li_info_new();
4045bfff6873SLukas Czerner 		if (ret)
4046bfff6873SLukas Czerner 			goto out;
4047bfff6873SLukas Czerner 	}
4048bfff6873SLukas Czerner 
4049bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
4050bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
4051bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
4052bfff6873SLukas Czerner 
4053bfff6873SLukas Czerner 	sbi->s_li_request = elr;
405446e4690bSTao Ma 	/*
405546e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
405646e4690bSTao Ma 	 * the request_list and the removal and free of it is
405746e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
405846e4690bSTao Ma 	 */
405946e4690bSTao Ma 	elr = NULL;
4060bfff6873SLukas Czerner 
4061bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
4062bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
4063bfff6873SLukas Czerner 		if (ret)
4064bfff6873SLukas Czerner 			goto out;
4065bfff6873SLukas Czerner 	}
4066bfff6873SLukas Czerner out:
4067bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
4068beed5ecbSNicolas Kaiser 	if (ret)
4069bfff6873SLukas Czerner 		kfree(elr);
4070bfff6873SLukas Czerner 	return ret;
4071bfff6873SLukas Czerner }
4072bfff6873SLukas Czerner 
4073bfff6873SLukas Czerner /*
4074bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
4075bfff6873SLukas Czerner  * module unload.
4076bfff6873SLukas Czerner  */
4077bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
4078bfff6873SLukas Czerner {
4079bfff6873SLukas Czerner 	/*
4080bfff6873SLukas Czerner 	 * If thread exited earlier
4081bfff6873SLukas Czerner 	 * there's nothing to be done.
4082bfff6873SLukas Czerner 	 */
40838f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
4084bfff6873SLukas Czerner 		return;
4085bfff6873SLukas Czerner 
40868f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
4087bfff6873SLukas Czerner }
4088bfff6873SLukas Czerner 
408925ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
409025ed6e8aSDarrick J. Wong {
409125ed6e8aSDarrick J. Wong 	int ret = 1;
409225ed6e8aSDarrick J. Wong 	int compat, incompat;
409325ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
409425ed6e8aSDarrick J. Wong 
40959aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
4096db9ee220SDarrick J. Wong 		/* journal checksum v3 */
409725ed6e8aSDarrick J. Wong 		compat = 0;
4098db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
409925ed6e8aSDarrick J. Wong 	} else {
410025ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
410125ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
410225ed6e8aSDarrick J. Wong 		incompat = 0;
410325ed6e8aSDarrick J. Wong 	}
410425ed6e8aSDarrick J. Wong 
4105feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
4106feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
4107feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
4108feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
410925ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
411025ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
411125ed6e8aSDarrick J. Wong 				compat, 0,
411225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
411325ed6e8aSDarrick J. Wong 				incompat);
411425ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
411525ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
411625ed6e8aSDarrick J. Wong 				compat, 0,
411725ed6e8aSDarrick J. Wong 				incompat);
411825ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
411925ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
412025ed6e8aSDarrick J. Wong 	} else {
4121feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
4122feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
412325ed6e8aSDarrick J. Wong 	}
412425ed6e8aSDarrick J. Wong 
412525ed6e8aSDarrick J. Wong 	return ret;
412625ed6e8aSDarrick J. Wong }
412725ed6e8aSDarrick J. Wong 
4128952fc18eSTheodore Ts'o /*
4129952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
4130952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
4131952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
4132952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
4133952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
4134952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
4135952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
4136952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
4137952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
4138952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
4139952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
4140952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
4141952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
4142952fc18eSTheodore Ts'o  */
4143952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
4144952fc18eSTheodore Ts'o 			  char *buf)
4145952fc18eSTheodore Ts'o {
4146952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
4147952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
4148952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
4149952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
4150952fc18eSTheodore Ts'o 	int			s, j, count = 0;
4151952fc18eSTheodore Ts'o 
4152e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
41530548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
41540548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
41550548bbb8STheodore Ts'o 
4156952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
4157952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
4158952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
4159952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
4160952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
4161952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
4162952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
4163952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4164952fc18eSTheodore Ts'o 			count++;
4165952fc18eSTheodore Ts'o 		}
4166952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
4167952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
4168952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4169952fc18eSTheodore Ts'o 			count++;
4170952fc18eSTheodore Ts'o 		}
4171952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
4172952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
4173952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
4174952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
4175952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
4176952fc18eSTheodore Ts'o 				count++;
4177952fc18eSTheodore Ts'o 			}
4178952fc18eSTheodore Ts'o 		if (i != grp)
4179952fc18eSTheodore Ts'o 			continue;
4180952fc18eSTheodore Ts'o 		s = 0;
4181952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
4182952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
4183952fc18eSTheodore Ts'o 			count++;
4184952fc18eSTheodore Ts'o 		}
4185c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
4186c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
4187c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
4188c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
4189c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
4190952fc18eSTheodore Ts'o 		}
4191c48ae41bSTheodore Ts'o 		count += j;
4192c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
4193c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
4194952fc18eSTheodore Ts'o 	}
4195952fc18eSTheodore Ts'o 	if (!count)
4196952fc18eSTheodore Ts'o 		return 0;
4197952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
4198952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
4199952fc18eSTheodore Ts'o }
4200952fc18eSTheodore Ts'o 
4201952fc18eSTheodore Ts'o /*
4202952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
4203952fc18eSTheodore Ts'o  */
4204952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
4205952fc18eSTheodore Ts'o {
4206952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4207952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
42083c816dedSEric Whitney 	struct inode *j_inode;
42093c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
4210952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4211952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
42124fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
4213952fc18eSTheodore Ts'o 
4214952fc18eSTheodore Ts'o 	if (!buf)
4215952fc18eSTheodore Ts'o 		return -ENOMEM;
4216952fc18eSTheodore Ts'o 
4217952fc18eSTheodore Ts'o 	/*
4218952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
4219952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
4220952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
4221952fc18eSTheodore Ts'o 	 */
4222952fc18eSTheodore Ts'o 
4223952fc18eSTheodore Ts'o 	/*
4224952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
4225952fc18eSTheodore Ts'o 	 */
4226952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
4227952fc18eSTheodore Ts'o 
4228952fc18eSTheodore Ts'o 	/*
4229952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
4230952fc18eSTheodore Ts'o 	 */
4231952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
4232952fc18eSTheodore Ts'o 		int blks;
4233952fc18eSTheodore Ts'o 
4234952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
4235952fc18eSTheodore Ts'o 		overhead += blks;
4236952fc18eSTheodore Ts'o 		if (blks)
4237952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
4238952fc18eSTheodore Ts'o 		cond_resched();
4239952fc18eSTheodore Ts'o 	}
42403c816dedSEric Whitney 
42413c816dedSEric Whitney 	/*
42423c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
42433c816dedSEric Whitney 	 * loaded or not
42443c816dedSEric Whitney 	 */
4245ee7ed3aaSChunguang Xu 	if (sbi->s_journal && !sbi->s_journal_bdev)
4246ede7dc7fSHarshad Shirwadkar 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
4247f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
4248f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
42493c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
42503c816dedSEric Whitney 		if (j_inode) {
42513c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
42523c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
42533c816dedSEric Whitney 			iput(j_inode);
42543c816dedSEric Whitney 		} else {
42553c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
42563c816dedSEric Whitney 		}
42573c816dedSEric Whitney 	}
4258952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
4259952fc18eSTheodore Ts'o 	smp_wmb();
4260952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
4261952fc18eSTheodore Ts'o 	return 0;
4262952fc18eSTheodore Ts'o }
4263952fc18eSTheodore Ts'o 
4264b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
426527dd4385SLukas Czerner {
426627dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
4267b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
426827dd4385SLukas Czerner 
426927dd4385SLukas Czerner 	/*
427030fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
427130fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
427230fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
427330fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
427430fac0f7SJan Kara 	 */
4275e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
4276b5799018STheodore Ts'o 		return;
427730fac0f7SJan Kara 	/*
427827dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
427927dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
428027dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
4281556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
428227dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
428327dd4385SLukas Czerner 	 * very rare.
428427dd4385SLukas Czerner 	 */
4285b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
4286b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
428727dd4385SLukas Czerner 
428827dd4385SLukas Czerner 	do_div(resv_clusters, 50);
428927dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
429027dd4385SLukas Czerner 
4291b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
429227dd4385SLukas Czerner }
429327dd4385SLukas Czerner 
4294ca9b404fSRoman Anufriev static const char *ext4_quota_mode(struct super_block *sb)
4295ca9b404fSRoman Anufriev {
4296ca9b404fSRoman Anufriev #ifdef CONFIG_QUOTA
4297ca9b404fSRoman Anufriev 	if (!ext4_quota_capable(sb))
4298ca9b404fSRoman Anufriev 		return "none";
4299ca9b404fSRoman Anufriev 
4300ca9b404fSRoman Anufriev 	if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
4301ca9b404fSRoman Anufriev 		return "journalled";
4302ca9b404fSRoman Anufriev 	else
4303ca9b404fSRoman Anufriev 		return "writeback";
4304ca9b404fSRoman Anufriev #else
4305ca9b404fSRoman Anufriev 	return "disabled";
4306ca9b404fSRoman Anufriev #endif
4307ca9b404fSRoman Anufriev }
4308ca9b404fSRoman Anufriev 
4309188c299eSJan Kara static void ext4_setup_csum_trigger(struct super_block *sb,
4310188c299eSJan Kara 				    enum ext4_journal_trigger_type type,
4311188c299eSJan Kara 				    void (*trigger)(
4312188c299eSJan Kara 					struct jbd2_buffer_trigger_type *type,
4313188c299eSJan Kara 					struct buffer_head *bh,
4314188c299eSJan Kara 					void *mapped_data,
4315188c299eSJan Kara 					size_t size))
4316188c299eSJan Kara {
4317188c299eSJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4318188c299eSJan Kara 
4319188c299eSJan Kara 	sbi->s_journal_triggers[type].sb = sb;
4320188c299eSJan Kara 	sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
4321188c299eSJan Kara }
4322188c299eSJan Kara 
43237edfd85bSLukas Czerner static void ext4_free_sbi(struct ext4_sb_info *sbi)
4324ac27a0ecSDave Kleikamp {
43257edfd85bSLukas Czerner 	if (!sbi)
43267edfd85bSLukas Czerner 		return;
43277edfd85bSLukas Czerner 
43287edfd85bSLukas Czerner 	kfree(sbi->s_blockgroup_lock);
43297edfd85bSLukas Czerner 	fs_put_dax(sbi->s_daxdev);
43307edfd85bSLukas Czerner 	kfree(sbi);
43317edfd85bSLukas Czerner }
43327edfd85bSLukas Czerner 
43337edfd85bSLukas Czerner static struct ext4_sb_info *ext4_alloc_sbi(struct super_block *sb)
43347edfd85bSLukas Czerner {
43357edfd85bSLukas Czerner 	struct ext4_sb_info *sbi;
43367edfd85bSLukas Czerner 
43377edfd85bSLukas Czerner 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
43387edfd85bSLukas Czerner 	if (!sbi)
43397edfd85bSLukas Czerner 		return NULL;
43407edfd85bSLukas Czerner 
43417edfd85bSLukas Czerner 	sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev);
43427edfd85bSLukas Czerner 
43437edfd85bSLukas Czerner 	sbi->s_blockgroup_lock =
43447edfd85bSLukas Czerner 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
43457edfd85bSLukas Czerner 
43467edfd85bSLukas Czerner 	if (!sbi->s_blockgroup_lock)
43477edfd85bSLukas Czerner 		goto err_out;
43487edfd85bSLukas Czerner 
43497edfd85bSLukas Czerner 	sb->s_fs_info = sbi;
43507edfd85bSLukas Czerner 	sbi->s_sb = sb;
43517edfd85bSLukas Czerner 	return sbi;
43527edfd85bSLukas Czerner err_out:
43537edfd85bSLukas Czerner 	fs_put_dax(sbi->s_daxdev);
43547edfd85bSLukas Czerner 	kfree(sbi);
43557edfd85bSLukas Czerner 	return NULL;
43567edfd85bSLukas Czerner }
43577edfd85bSLukas Czerner 
4358960e0ab6SLukas Czerner static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
43597edfd85bSLukas Czerner {
43601d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
4361617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
43627edfd85bSLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
43637c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
4364617ba13bSMingming Cao 	ext4_fsblk_t block;
436570bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
4366ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
4367ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
4368ac27a0ecSDave Kleikamp 	struct inode *root;
4369dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
4370281b5995STheodore Ts'o 	int blocksize, clustersize;
43714ec11028STheodore Ts'o 	unsigned int db_count;
43724ec11028STheodore Ts'o 	unsigned int i;
4373ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
4374bd81d8eeSLaurent Vivier 	__u64 blocks_count;
437507aa2ea1SLukas Czerner 	int err = 0;
4376bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
43777edfd85bSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
4378960e0ab6SLukas Czerner 	int silent = fc->sb_flags & SB_SILENT;
4379b237e304SHarshad Shirwadkar 
4380b237e304SHarshad Shirwadkar 	/* Set defaults for the variables that will be set during parsing */
43817edfd85bSLukas Czerner 	ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
43827edfd85bSLukas Czerner 	ctx->mb_optimize_scan = DEFAULT_MB_OPTIMIZE_SCAN;
4383ac27a0ecSDave Kleikamp 
4384240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
4385f613dfcbSTheodore Ts'o 	sbi->s_sectors_written_start =
43868446fe92SChristoph Hellwig 		part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
4387ac27a0ecSDave Kleikamp 
438807aa2ea1SLukas Czerner 	/* -EINVAL is default */
4389dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
4390617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
4391ac27a0ecSDave Kleikamp 	if (!blocksize) {
4392b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
4393ac27a0ecSDave Kleikamp 		goto out_fail;
4394ac27a0ecSDave Kleikamp 	}
4395ac27a0ecSDave Kleikamp 
4396ac27a0ecSDave Kleikamp 	/*
4397617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
4398ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
4399ac27a0ecSDave Kleikamp 	 */
4400617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
44017edfd85bSLukas Czerner 		logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
440270bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4403ac27a0ecSDave Kleikamp 	} else {
44047edfd85bSLukas Czerner 		logical_sb_block = sbi->s_sb_block;
4405ac27a0ecSDave Kleikamp 	}
4406ac27a0ecSDave Kleikamp 
44078394a6abSzhangyi (F) 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
44088394a6abSzhangyi (F) 	if (IS_ERR(bh)) {
4409b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
44108394a6abSzhangyi (F) 		ret = PTR_ERR(bh);
4411ac27a0ecSDave Kleikamp 		goto out_fail;
4412ac27a0ecSDave Kleikamp 	}
4413ac27a0ecSDave Kleikamp 	/*
4414ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
4415617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
4416ac27a0ecSDave Kleikamp 	 */
44172716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4418ac27a0ecSDave Kleikamp 	sbi->s_es = es;
4419ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
4420617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
4421617ba13bSMingming Cao 		goto cantfind_ext4;
4422afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
4423ac27a0ecSDave Kleikamp 
4424feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
4425e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
4426e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
4427363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
4428feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
4429feb0ab32SDarrick J. Wong 
4430d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
4431d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
4432d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4433d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
4434d25425f8SDarrick J. Wong 		silent = 1;
4435d25425f8SDarrick J. Wong 		goto cantfind_ext4;
4436d25425f8SDarrick J. Wong 	}
443702f310fcSJan Kara 	ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
443802f310fcSJan Kara 				ext4_orphan_file_block_trigger);
4439d25425f8SDarrick J. Wong 
44400441984aSDarrick J. Wong 	/* Load the checksum driver */
44410441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
44420441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
44430441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
44440441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
44450441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
44460441984aSDarrick J. Wong 		goto failed_mount;
44470441984aSDarrick J. Wong 	}
44480441984aSDarrick J. Wong 
4449a9c47317SDarrick J. Wong 	/* Check superblock checksum */
4450a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
4451a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4452a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
4453a9c47317SDarrick J. Wong 		silent = 1;
44546a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
4455a9c47317SDarrick J. Wong 		goto cantfind_ext4;
4456a9c47317SDarrick J. Wong 	}
4457a9c47317SDarrick J. Wong 
4458a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
4459e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
44608c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4461dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4462a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4463a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
4464a9c47317SDarrick J. Wong 
4465ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
4466ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4467fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
4468617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
4469fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
447087f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4471fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
4472617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
4473fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
4474ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
4475fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
447603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
4477fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
44782e7842b8SHugh Dickins #endif
4479995a3ed6SHarshad Shirwadkar 	if (ext4_has_feature_fast_commit(sb))
4480995a3ed6SHarshad Shirwadkar 		set_opt2(sb, JOURNAL_FAST_COMMIT);
448198c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
448298c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
448398c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
448498c1a759SDarrick J. Wong 
4485617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4486fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
4487617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4488fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
4489617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4490fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
4491ac27a0ecSDave Kleikamp 
4492617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
4493fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
4494bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
4495fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
4496bb4f397aSAneesh Kumar K.V 	else
4497fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
449845f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
4499fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
45008b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
4501fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
4502ac27a0ecSDave Kleikamp 
450308cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
450408cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
450530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
450630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
450730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
4508ac27a0ecSDave Kleikamp 
45098b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4510fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
4511ac27a0ecSDave Kleikamp 
45121e2462f9SMingming Cao 	/*
4513dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
4514dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
4515dd919b98SAneesh Kumar K.V 	 */
4516bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
45178b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4518fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
4519dd919b98SAneesh Kumar K.V 
452051ce6511SLukas Czerner 	/*
452151ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
452251ce6511SLukas Czerner 	 * no mount option specified.
452351ce6511SLukas Czerner 	 */
452451ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
452551ce6511SLukas Czerner 
4526c9200760STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
4527c9200760STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4528c9200760STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4529c9200760STheodore Ts'o 			 "Invalid log block size: %u",
4530c9200760STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4531c9200760STheodore Ts'o 		goto failed_mount;
4532c9200760STheodore Ts'o 	}
4533c9200760STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4534c9200760STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4535c9200760STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4536c9200760STheodore Ts'o 			 "Invalid log cluster size: %u",
4537c9200760STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4538c9200760STheodore Ts'o 		goto failed_mount;
4539c9200760STheodore Ts'o 	}
4540c9200760STheodore Ts'o 
4541c9200760STheodore Ts'o 	blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4542626b035bSRitesh Harjani 
4543626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
4544626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
4545626b035bSRitesh Harjani 
45469803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
45479803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
45489803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
45499803387cSTheodore Ts'o 	} else {
45509803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
45519803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
45529803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
45539803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
45549803387cSTheodore Ts'o 				 sbi->s_first_ino);
45559803387cSTheodore Ts'o 			goto failed_mount;
45569803387cSTheodore Ts'o 		}
45579803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
45589803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
45599803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
45609803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
45619803387cSTheodore Ts'o 			       "unsupported inode size: %d",
45629803387cSTheodore Ts'o 			       sbi->s_inode_size);
45634f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
45649803387cSTheodore Ts'o 			goto failed_mount;
45659803387cSTheodore Ts'o 		}
45669803387cSTheodore Ts'o 		/*
45679803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
45689803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
45699803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
45709803387cSTheodore Ts'o 		 * for all three.
45719803387cSTheodore Ts'o 		 */
45729803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
45739803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
45749803387cSTheodore Ts'o 			sb->s_time_gran = 1;
45759803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
45769803387cSTheodore Ts'o 		} else {
45779803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
45789803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
45799803387cSTheodore Ts'o 		}
45809803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
45819803387cSTheodore Ts'o 	}
45829803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
45839803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
45849803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
45859803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
45869803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
45879803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
45889803387cSTheodore Ts'o 
45899803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
45909803387cSTheodore Ts'o 			if (v > max) {
45919803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
45929803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
45939803387cSTheodore Ts'o 				goto failed_mount;
45949803387cSTheodore Ts'o 			}
45959803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
45969803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
45979803387cSTheodore Ts'o 
45989803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
45999803387cSTheodore Ts'o 			if (v > max) {
46009803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
46019803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
46029803387cSTheodore Ts'o 				goto failed_mount;
46039803387cSTheodore Ts'o 			}
46049803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
46059803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
46069803387cSTheodore Ts'o 		}
46079803387cSTheodore Ts'o 	}
46089803387cSTheodore Ts'o 
46097edfd85bSLukas Czerner 	err = parse_apply_sb_mount_options(sb, ctx);
46107edfd85bSLukas Czerner 	if (err < 0)
46115aee0f8aSTheodore Ts'o 		goto failed_mount;
46127edfd85bSLukas Czerner 
46135a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
46147edfd85bSLukas Czerner 
46157edfd85bSLukas Czerner 	err = ext4_check_opt_consistency(fc, sb);
46167edfd85bSLukas Czerner 	if (err < 0)
46177edfd85bSLukas Czerner 		goto failed_mount;
46187edfd85bSLukas Czerner 
46197edfd85bSLukas Czerner 	err = ext4_apply_options(fc, sb);
46207edfd85bSLukas Czerner 	if (err < 0)
4621ac27a0ecSDave Kleikamp 		goto failed_mount;
4622ac27a0ecSDave Kleikamp 
4623c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4624f8f4acb6SDaniel Rosenberg 	if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
4625c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
4626c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
4627c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
4628c83ad55eSGabriel Krisman Bertazi 
4629c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
4630c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
4631c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4632c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
4633c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4634c83ad55eSGabriel Krisman Bertazi 		}
4635c83ad55eSGabriel Krisman Bertazi 
4636c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
4637c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
4638c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4639c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
4640c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4641c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4642c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4643c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4644c83ad55eSGabriel Krisman Bertazi 		}
4645c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4646c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4647c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4648c83ad55eSGabriel Krisman Bertazi 
4649f8f4acb6SDaniel Rosenberg 		sb->s_encoding = encoding;
4650f8f4acb6SDaniel Rosenberg 		sb->s_encoding_flags = encoding_flags;
4651c83ad55eSGabriel Krisman Bertazi 	}
4652c83ad55eSGabriel Krisman Bertazi #endif
4653c83ad55eSGabriel Krisman Bertazi 
465456889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4655556e0319SHarshad Shirwadkar 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, O_DIRECT and fast_commit support!\n");
4656781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4657244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
4658556e0319SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
465956889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
466056889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
466156889787STheodore Ts'o 				 "both data=journal and delalloc");
466256889787STheodore Ts'o 			goto failed_mount;
466356889787STheodore Ts'o 		}
4664fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4665923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4666923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4667923ae0ffSRoss Zwisler 			goto failed_mount;
4668923ae0ffSRoss Zwisler 		}
466973b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
467073b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
467173b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
467273b92a2aSSergey Karamov 				 "instead of data journaling mode");
467373b92a2aSSergey Karamov 		}
467456889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
467556889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4676001e4a87STejun Heo 	} else {
4677001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
467856889787STheodore Ts'o 	}
467956889787STheodore Ts'o 
46801751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
46811751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4682ac27a0ecSDave Kleikamp 
4683617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4684e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4685e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4686e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4687b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4688b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4689b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4690469108ffSTheodore Tso 
4691ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4692ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4693e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4694ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4695ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4696ed3654ebSTheodore Ts'o 			goto failed_mount;
4697ed3654ebSTheodore Ts'o 		}
4698dec214d0STahsin Erdogan 
4699dec214d0STahsin Erdogan 		/*
4700dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4701dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4702dec214d0STahsin Erdogan 		 */
4703dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4704dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4705dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4706dec214d0STahsin Erdogan 			goto failed_mount;
4707dec214d0STahsin Erdogan 		}
4708ed3654ebSTheodore Ts'o 	}
4709ed3654ebSTheodore Ts'o 
47102035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
47112035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
47122035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
47132035e776STheodore Ts'o 				 "using the ext4 subsystem");
47142035e776STheodore Ts'o 		else {
47150d9366d6SEric Sandeen 			/*
47160d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
47170d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
47180d9366d6SEric Sandeen 			 */
47190d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
47200d9366d6SEric Sandeen 				goto failed_mount;
47212035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
47222035e776STheodore Ts'o 				 "to feature incompatibilities");
47232035e776STheodore Ts'o 			goto failed_mount;
47242035e776STheodore Ts'o 		}
47252035e776STheodore Ts'o 	}
47262035e776STheodore Ts'o 
47272035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
47282035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
47292035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
47302035e776STheodore Ts'o 				 "using the ext4 subsystem");
47312035e776STheodore Ts'o 		else {
47320d9366d6SEric Sandeen 			/*
47330d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
47340d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
47350d9366d6SEric Sandeen 			 */
47360d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
47370d9366d6SEric Sandeen 				goto failed_mount;
47382035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
47392035e776STheodore Ts'o 				 "to feature incompatibilities");
47402035e776STheodore Ts'o 			goto failed_mount;
47412035e776STheodore Ts'o 		}
47422035e776STheodore Ts'o 	}
47432035e776STheodore Ts'o 
4744469108ffSTheodore Tso 	/*
4745ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4746ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4747ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4748ac27a0ecSDave Kleikamp 	 */
4749bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4750ac27a0ecSDave Kleikamp 		goto failed_mount;
4751a13fb1a4SEric Sandeen 
47525b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
47535b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
47545b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
47555b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
47565b9554dcSTheodore Ts'o 		goto failed_mount;
47575b9554dcSTheodore Ts'o 	}
47585b9554dcSTheodore Ts'o 
47597edfd85bSLukas Czerner 	if (dax_supported(sbi->s_daxdev, sb->s_bdev, blocksize, 0,
4760bdd3c50dSChristoph Hellwig 			bdev_nr_sectors(sb->s_bdev)))
4761a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4762a8ab6d38SIra Weiny 
4763fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4764559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4765559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4766559db4c6SRoss Zwisler 					" that may contain inline data");
4767361d24d4SEric Sandeen 			goto failed_mount;
4768559db4c6SRoss Zwisler 		}
4769a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
477024f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4771361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4772361d24d4SEric Sandeen 			goto failed_mount;
477324f3478dSDan Williams 		}
4774923ae0ffSRoss Zwisler 	}
4775923ae0ffSRoss Zwisler 
4776e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
47776ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
47786ddb2447STheodore Ts'o 			 es->s_encryption_level);
47796ddb2447STheodore Ts'o 		goto failed_mount;
47806ddb2447STheodore Ts'o 	}
47816ddb2447STheodore Ts'o 
4782ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4783afd09b61SAlexey Makhalov 		/*
4784afd09b61SAlexey Makhalov 		 * bh must be released before kill_bdev(), otherwise
4785afd09b61SAlexey Makhalov 		 * it won't be freed and its page also. kill_bdev()
4786afd09b61SAlexey Makhalov 		 * is called by sb_set_blocksize().
4787afd09b61SAlexey Makhalov 		 */
4788afd09b61SAlexey Makhalov 		brelse(bh);
4789ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4790ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4791b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4792ce40733cSAneesh Kumar K.V 					blocksize);
4793afd09b61SAlexey Makhalov 			bh = NULL;
4794ac27a0ecSDave Kleikamp 			goto failed_mount;
4795ac27a0ecSDave Kleikamp 		}
4796ac27a0ecSDave Kleikamp 
47977edfd85bSLukas Czerner 		logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
479870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
47998394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
48008394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4801b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4802b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
48038394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
48048394a6abSzhangyi (F) 			bh = NULL;
4805ac27a0ecSDave Kleikamp 			goto failed_mount;
4806ac27a0ecSDave Kleikamp 		}
48072716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4808ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4809617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4810b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4811b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4812ac27a0ecSDave Kleikamp 			goto failed_mount;
4813ac27a0ecSDave Kleikamp 		}
4814ac27a0ecSDave Kleikamp 	}
4815ac27a0ecSDave Kleikamp 
4816e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4817f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4818f287a1a5STheodore Ts'o 						      has_huge_files);
4819f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4820ac27a0ecSDave Kleikamp 
48210d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4822e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
48238fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
48240d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4825d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4826b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4827b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
48280d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
48290d1ee42fSAlexandre Ratchov 			goto failed_mount;
48300d1ee42fSAlexandre Ratchov 		}
48310d1ee42fSAlexandre Ratchov 	} else
48320d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
48330b8e58a1SAndreas Dilger 
4834ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4835ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
48360b8e58a1SAndreas Dilger 
4837617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4838ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4839617ba13bSMingming Cao 		goto cantfind_ext4;
4840cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4841cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4842cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4843b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4844cd6bb35bSTheodore Ts'o 		goto failed_mount;
4845cd6bb35bSTheodore Ts'o 	}
4846ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4847ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
48480d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4849ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4850ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4851e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4852e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
48530b8e58a1SAndreas Dilger 
4854ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4855ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4856ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4857e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4858f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4859f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4860f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4861f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4862f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4863bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
486423301410STheodore Ts'o 				es->s_flags |=
486523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4866f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4867f99b2589STheodore Ts'o #else
4868bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
486923301410STheodore Ts'o 				es->s_flags |=
487023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4871f99b2589STheodore Ts'o #endif
4872f99b2589STheodore Ts'o 		}
487323301410STheodore Ts'o 	}
4874ac27a0ecSDave Kleikamp 
4875281b5995STheodore Ts'o 	/* Handle clustersize */
4876281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4877ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4878281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4879281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4880281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4881281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4882281b5995STheodore Ts'o 			goto failed_mount;
4883281b5995STheodore Ts'o 		}
4884281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4885281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4886281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4887281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4888281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4889281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4890281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4891281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4892281b5995STheodore Ts'o 			goto failed_mount;
4893281b5995STheodore Ts'o 		}
4894281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4895281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4896281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4897281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4898281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4899281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4900281b5995STheodore Ts'o 			goto failed_mount;
4901281b5995STheodore Ts'o 		}
4902281b5995STheodore Ts'o 	} else {
4903281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4904bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4905bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4906bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4907bfe0a5f4STheodore Ts'o 			goto failed_mount;
4908281b5995STheodore Ts'o 		}
4909ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4910b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4911b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4912ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4913ac27a0ecSDave Kleikamp 			goto failed_mount;
4914ac27a0ecSDave Kleikamp 		}
4915281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4916281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4917281b5995STheodore Ts'o 	}
4918281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4919281b5995STheodore Ts'o 
4920960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4921960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4922960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4923960fd856STheodore Ts'o 
4924bf43d84bSEric Sandeen 	/*
4925bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4926bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4927bf43d84bSEric Sandeen 	 */
49285a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
492930ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
49305a9ae68aSDarrick J. Wong 	if (err) {
4931b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4932bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4933ac27a0ecSDave Kleikamp 		goto failed_mount;
4934ac27a0ecSDave Kleikamp 	}
4935ac27a0ecSDave Kleikamp 
4936617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4937617ba13bSMingming Cao 		goto cantfind_ext4;
4938e7c95593SEric Sandeen 
49390f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
49405513b241SChristoph Hellwig 	blocks_count = sb_bdev_nr_blocks(sb);
49410f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4942b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4943b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
49440f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
49450f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
49460f2ddca6SFrom: Thiemo Nagel 	}
49470f2ddca6SFrom: Thiemo Nagel 
49484ec11028STheodore Ts'o 	/*
49494ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
49504ec11028STheodore Ts'o 	 * of the filesystem.
49514ec11028STheodore Ts'o 	 */
49524ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4953b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4954b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4955e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
49564ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4957e7c95593SEric Sandeen 		goto failed_mount;
4958e7c95593SEric Sandeen 	}
4959bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4960bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4961bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4962bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4963bfe0a5f4STheodore Ts'o 		goto failed_mount;
4964bfe0a5f4STheodore Ts'o 	}
4965bfe0a5f4STheodore Ts'o 
4966bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4967bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4968bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4969bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
49704ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4971df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
49724ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4973df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
49744ec11028STheodore Ts'o 		       ext4_blocks_count(es),
49754ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
49764ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
49774ec11028STheodore Ts'o 		goto failed_mount;
49784ec11028STheodore Ts'o 	}
4979bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4980fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4981fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
49829e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
49839e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
49849e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
49859e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
49869e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
49879e463084STheodore Ts'o 		ret = -EINVAL;
49889e463084STheodore Ts'o 		goto failed_mount;
49899e463084STheodore Ts'o 	}
4990617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4991617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
49923a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
49932ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
49943a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
49953a4b77cdSEryu Guan 				 "first meta block group too large: %u "
49963a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
49973a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
49983a4b77cdSEryu Guan 			goto failed_mount;
49993a4b77cdSEryu Guan 		}
50003a4b77cdSEryu Guan 	}
50011d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
50021d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
5003f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
50041d0c3924STheodore Ts'o 					  GFP_KERNEL));
5005ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
5006b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
50072cde417dSTheodore Ts'o 		ret = -ENOMEM;
5008ac27a0ecSDave Kleikamp 		goto failed_mount;
5009ac27a0ecSDave Kleikamp 	}
5010ac27a0ecSDave Kleikamp 
5011705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
5012ac27a0ecSDave Kleikamp 
501385c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
501485c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
501585c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
50165df1d412Szhangyi (F) 		ext4_sb_breadahead_unmovable(sb, block);
501785c8f176SAndrew Perepechko 	}
501885c8f176SAndrew Perepechko 
5019ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
50201d0c3924STheodore Ts'o 		struct buffer_head *bh;
50211d0c3924STheodore Ts'o 
502270bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
50238394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, block);
50248394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
5025b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
5026b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
5027ac27a0ecSDave Kleikamp 			db_count = i;
50288394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
5029ac27a0ecSDave Kleikamp 			goto failed_mount2;
5030ac27a0ecSDave Kleikamp 		}
50311d0c3924STheodore Ts'o 		rcu_read_lock();
50321d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
50331d0c3924STheodore Ts'o 		rcu_read_unlock();
5034ac27a0ecSDave Kleikamp 	}
503544de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
5036829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
5037b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
50386a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
5039f9ae9cf5STheodore Ts'o 		goto failed_mount2;
5040ac27a0ecSDave Kleikamp 	}
5041772cb7c8SJose R. Santos 
5042235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
5043c92dc856SJan Kara 	spin_lock_init(&sbi->s_error_lock);
5044c92dc856SJan Kara 	INIT_WORK(&sbi->s_error_work, flush_stashed_error_work);
504504496411STao Ma 
5046a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
5047eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
5048ce7e010aSTheodore Ts'o 		goto failed_mount3;
5049ce7e010aSTheodore Ts'o 
5050c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
505167a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
5052c9de560dSAlex Tomas 
5053f9ae9cf5STheodore Ts'o 	/*
5054f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
5055f9ae9cf5STheodore Ts'o 	 */
5056f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
5057617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
5058617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
5059643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
5060a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
5061ffcc4182SEric Biggers #endif
5062c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
5063c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
5064c93d8f88SEric Biggers #endif
5065ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5066617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
5067e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
50681fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
5069262b4662SJan Kara 	else
5070262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
5071689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
5072ac27a0ecSDave Kleikamp #endif
507385787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
5074f2fa2ffcSAneesh Kumar K.V 
5075ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
50763b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
5077ac27a0ecSDave Kleikamp 
5078aa75f4d3SHarshad Shirwadkar 	/* Initialize fast commit stuff */
5079aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_subtid, 0);
5080aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
5081aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
5082aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
5083aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
5084aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bytes = 0;
50859b5f6c9bSHarshad Shirwadkar 	ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
50869b5f6c9bSHarshad Shirwadkar 	ext4_clear_mount_flag(sb, EXT4_MF_FC_COMMITTING);
5087*e85c81baSXin Yin 	sbi->s_fc_ineligible_tid = 0;
5088aa75f4d3SHarshad Shirwadkar 	spin_lock_init(&sbi->s_fc_lock);
5089aa75f4d3SHarshad Shirwadkar 	memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
50908016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions = NULL;
50918016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_size = 0;
50928016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_used = 0;
50938016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_valid = 0;
50948016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes = NULL;
50958016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
50968016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
5097aa75f4d3SHarshad Shirwadkar 
5098ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
5099ac27a0ecSDave Kleikamp 
5100ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
510102f310fcSJan Kara 			  ext4_has_feature_orphan_present(sb) ||
5102e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
5103ac27a0ecSDave Kleikamp 
5104bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
5105c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
510650460fe8SDarrick J. Wong 			goto failed_mount3a;
5107c5e06d10SJohann Lombardi 
5108ac27a0ecSDave Kleikamp 	/*
5109ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
5110ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
5111ac27a0ecSDave Kleikamp 	 */
5112e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
51137edfd85bSLukas Czerner 		err = ext4_load_journal(sb, es, ctx->journal_devnum);
51144753d8a2STheodore Ts'o 		if (err)
511550460fe8SDarrick J. Wong 			goto failed_mount3a;
5116bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
5117e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
5118b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
5119b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
5120744692dcSJiaying Zhang 		goto failed_mount_wq;
5121ac27a0ecSDave Kleikamp 	} else {
51221e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
51231e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
51241e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
51251e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
51261e381f60SDmitry Monakhov 			goto failed_mount_wq;
51271e381f60SDmitry Monakhov 		}
51281e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
51291e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
51301e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
51311e381f60SDmitry Monakhov 			goto failed_mount_wq;
51321e381f60SDmitry Monakhov 		}
51331e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
51341e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
51351e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
51361e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
51371e381f60SDmitry Monakhov 			goto failed_mount_wq;
51381e381f60SDmitry Monakhov 		}
51391e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
51401e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
51411e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
51421e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
51431e381f60SDmitry Monakhov 			goto failed_mount_wq;
51441e381f60SDmitry Monakhov 		}
514550b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
51461e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
5147fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
5148995a3ed6SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
51490390131bSFrank Mayhar 		sbi->s_journal = NULL;
51500390131bSFrank Mayhar 		needs_recovery = 0;
51510390131bSFrank Mayhar 		goto no_journal;
5152ac27a0ecSDave Kleikamp 	}
5153ac27a0ecSDave Kleikamp 
5154e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
5155eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
5156eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
5157b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
5158744692dcSJiaying Zhang 		goto failed_mount_wq;
5159eb40a09cSJose R. Santos 	}
5160eb40a09cSJose R. Santos 
516125ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
516225ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
516325ed6e8aSDarrick J. Wong 			 "feature set");
516425ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
5165d4da6c9cSLinus Torvalds 	}
5166818d276cSGirish Shilamkar 
5167a1e5e465SHarshad Shirwadkar 	if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
5168a1e5e465SHarshad Shirwadkar 		!jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
5169a1e5e465SHarshad Shirwadkar 					  JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
5170a1e5e465SHarshad Shirwadkar 		ext4_msg(sb, KERN_ERR,
5171a1e5e465SHarshad Shirwadkar 			"Failed to set fast commit journal feature");
5172a1e5e465SHarshad Shirwadkar 		goto failed_mount_wq;
5173a1e5e465SHarshad Shirwadkar 	}
5174a1e5e465SHarshad Shirwadkar 
5175ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
5176ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
5177ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
5178ac27a0ecSDave Kleikamp 	case 0:
5179ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
518063f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
518163f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
518263f57933SAndrew Morton 		 */
5183dab291afSMingming Cao 		if (jbd2_journal_check_available_features
518427f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
5185fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
518627f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
518727f394a7STyson Nottingham 		} else {
5188fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
518927f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
519027f394a7STyson Nottingham 		}
5191ac27a0ecSDave Kleikamp 		break;
5192ac27a0ecSDave Kleikamp 
5193617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
5194617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
5195dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
5196dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
5197b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
5198b31e1552SEric Sandeen 			       "requested data journaling mode");
5199744692dcSJiaying Zhang 			goto failed_mount_wq;
5200ac27a0ecSDave Kleikamp 		}
52015a150bdeSGustavo A. R. Silva 		break;
5202ac27a0ecSDave Kleikamp 	default:
5203ac27a0ecSDave Kleikamp 		break;
5204ac27a0ecSDave Kleikamp 	}
5205ab04df78SJan Kara 
5206ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
5207ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5208ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
5209ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
5210ab04df78SJan Kara 		goto failed_mount_wq;
5211ab04df78SJan Kara 	}
5212ab04df78SJan Kara 
52137edfd85bSLukas Czerner 	set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
5214ac27a0ecSDave Kleikamp 
5215342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_submit_inode_data_buffers =
5216afb585a9SMauricio Faria de Oliveira 		ext4_journal_submit_inode_data_buffers;
5217342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_finish_inode_data_buffers =
5218afb585a9SMauricio Faria de Oliveira 		ext4_journal_finish_inode_data_buffers;
521918aadd47SBobi Jam 
5220ce7e010aSTheodore Ts'o no_journal:
5221cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
522247387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
522347387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
5224cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
5225cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
52269c191f70ST Makphaibulchoke 			goto failed_mount_wq;
52279c191f70ST Makphaibulchoke 		}
52289c191f70ST Makphaibulchoke 
5229dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
5230dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
5231dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
5232dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
5233dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
5234dec214d0STahsin Erdogan 				goto failed_mount_wq;
5235dec214d0STahsin Erdogan 			}
5236dec214d0STahsin Erdogan 		}
5237cdb7ee4cSTahsin Erdogan 	}
5238dec214d0STahsin Erdogan 
5239c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
5240c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
5241c93d8f88SEric Biggers 		goto failed_mount_wq;
5242c93d8f88SEric Biggers 	}
5243c93d8f88SEric Biggers 
5244bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
5245e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
5246e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
52474392fbc4SJan Kara 		ext4_commit_super(sb);
52486ddb2447STheodore Ts'o 	}
52496ddb2447STheodore Ts'o 
5250fd89d5f2STejun Heo 	/*
5251952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
5252952fc18eSTheodore Ts'o 	 * superblock if present.
5253952fc18eSTheodore Ts'o 	 */
5254952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
5255952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
5256952fc18eSTheodore Ts'o 	else {
525707aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
525807aa2ea1SLukas Czerner 		if (err)
5259952fc18eSTheodore Ts'o 			goto failed_mount_wq;
5260952fc18eSTheodore Ts'o 	}
5261952fc18eSTheodore Ts'o 
5262952fc18eSTheodore Ts'o 	/*
5263fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
5264fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
5265fd89d5f2STejun Heo 	 */
52662e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
52672e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
52682e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
52692e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
527007aa2ea1SLukas Czerner 		ret = -ENOMEM;
52712e8fa54eSJan Kara 		goto failed_mount4;
52722e8fa54eSJan Kara 	}
52732e8fa54eSJan Kara 
5274ac27a0ecSDave Kleikamp 	/*
5275dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
5276ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
5277ac27a0ecSDave Kleikamp 	 */
5278ac27a0ecSDave Kleikamp 
52798a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
52801d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
5281b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
52821d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
528332a9bb57SManish Katiyar 		root = NULL;
5284ac27a0ecSDave Kleikamp 		goto failed_mount4;
5285ac27a0ecSDave Kleikamp 	}
5286ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
5287b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
528894bf608aSAl Viro 		iput(root);
5289ac27a0ecSDave Kleikamp 		goto failed_mount4;
5290ac27a0ecSDave Kleikamp 	}
5291b886ee3eSGabriel Krisman Bertazi 
529248fde701SAl Viro 	sb->s_root = d_make_root(root);
52931d1fe1eeSDavid Howells 	if (!sb->s_root) {
5294b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
52951d1fe1eeSDavid Howells 		ret = -ENOMEM;
52961d1fe1eeSDavid Howells 		goto failed_mount4;
52971d1fe1eeSDavid Howells 	}
5298ac27a0ecSDave Kleikamp 
5299c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
5300c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
53011751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
5302c89128a0SJaegeuk Kim 		ret = 0;
5303c89128a0SJaegeuk Kim 	} else if (ret)
5304c89128a0SJaegeuk Kim 		goto failed_mount4a;
5305ef7f3835SKalpak Shah 
5306b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
530727dd4385SLukas Czerner 
53080f5bde1dSJan Kara 	if (test_opt(sb, BLOCK_VALIDITY)) {
53096fd058f7STheodore Ts'o 		err = ext4_setup_system_zone(sb);
53106fd058f7STheodore Ts'o 		if (err) {
5311b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
5312fbe845ddSCurt Wohlgemuth 				 "zone (%d)", err);
5313f9ae9cf5STheodore Ts'o 			goto failed_mount4a;
5314f9ae9cf5STheodore Ts'o 		}
53150f5bde1dSJan Kara 	}
53168016e29fSHarshad Shirwadkar 	ext4_fc_replay_cleanup(sb);
5317f9ae9cf5STheodore Ts'o 
5318f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
5319196e402aSHarshad Shirwadkar 
5320196e402aSHarshad Shirwadkar 	/*
5321196e402aSHarshad Shirwadkar 	 * Enable optimize_scan if number of groups is > threshold. This can be
5322196e402aSHarshad Shirwadkar 	 * turned off by passing "mb_optimize_scan=0". This can also be
5323196e402aSHarshad Shirwadkar 	 * turned on forcefully by passing "mb_optimize_scan=1".
5324196e402aSHarshad Shirwadkar 	 */
53257edfd85bSLukas Czerner 	if (ctx->mb_optimize_scan == 1)
5326196e402aSHarshad Shirwadkar 		set_opt2(sb, MB_OPTIMIZE_SCAN);
53277edfd85bSLukas Czerner 	else if (ctx->mb_optimize_scan == 0)
5328196e402aSHarshad Shirwadkar 		clear_opt2(sb, MB_OPTIMIZE_SCAN);
5329196e402aSHarshad Shirwadkar 	else if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
5330196e402aSHarshad Shirwadkar 		set_opt2(sb, MB_OPTIMIZE_SCAN);
5331196e402aSHarshad Shirwadkar 
5332f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
5333f9ae9cf5STheodore Ts'o 	if (err) {
5334f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5335f9ae9cf5STheodore Ts'o 			 err);
5336dcf2d804STao Ma 		goto failed_mount5;
5337c2774d84SAneesh Kumar K.V 	}
5338c2774d84SAneesh Kumar K.V 
5339027f14f5STheodore Ts'o 	/*
5340027f14f5STheodore Ts'o 	 * We can only set up the journal commit callback once
5341027f14f5STheodore Ts'o 	 * mballoc is initialized
5342027f14f5STheodore Ts'o 	 */
5343027f14f5STheodore Ts'o 	if (sbi->s_journal)
5344027f14f5STheodore Ts'o 		sbi->s_journal->j_commit_callback =
5345027f14f5STheodore Ts'o 			ext4_journal_commit_callback;
5346027f14f5STheodore Ts'o 
5347d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
5348d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
5349d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
5350908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
5351908c7f19STejun Heo 				  GFP_KERNEL);
5352d5e03cbbSTheodore Ts'o 	if (!err) {
5353d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
5354d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
5355908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
5356908c7f19STejun Heo 					  GFP_KERNEL);
5357d5e03cbbSTheodore Ts'o 	}
5358b2bbb92fSJan Kara 	/*
5359b2bbb92fSJan Kara 	 * Update the checksum after updating free space/inode
5360b2bbb92fSJan Kara 	 * counters.  Otherwise the superblock can have an incorrect
5361b2bbb92fSJan Kara 	 * checksum in the buffer cache until it is written out and
5362b2bbb92fSJan Kara 	 * e2fsprogs programs trying to open a file system immediately
5363b2bbb92fSJan Kara 	 * after it is mounted can fail.
5364b2bbb92fSJan Kara 	 */
5365b2bbb92fSJan Kara 	ext4_superblock_csum_set(sb);
5366d5e03cbbSTheodore Ts'o 	if (!err)
5367d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
5368908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
5369d5e03cbbSTheodore Ts'o 	if (!err)
5370908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
5371908c7f19STejun Heo 					  GFP_KERNEL);
5372c8585c6fSDaeho Jeong 	if (!err)
5373efc61345SEric Whitney 		err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
5374efc61345SEric Whitney 					  GFP_KERNEL);
5375efc61345SEric Whitney 	if (!err)
5376bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
5377c8585c6fSDaeho Jeong 
5378d5e03cbbSTheodore Ts'o 	if (err) {
5379d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
5380d5e03cbbSTheodore Ts'o 		goto failed_mount6;
5381d5e03cbbSTheodore Ts'o 	}
5382d5e03cbbSTheodore Ts'o 
5383e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
5384d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
5385d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
5386d5e03cbbSTheodore Ts'o 			       "unable to initialize "
5387d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
53888f6840c4SYang Yingliang 			ret = -ENOMEM;
5389d5e03cbbSTheodore Ts'o 			goto failed_mount6;
5390d5e03cbbSTheodore Ts'o 		}
5391d5e03cbbSTheodore Ts'o 
5392bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
5393bfff6873SLukas Czerner 	if (err)
5394dcf2d804STao Ma 		goto failed_mount6;
5395bfff6873SLukas Czerner 
5396b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
5397dcf2d804STao Ma 	if (err)
5398dcf2d804STao Ma 		goto failed_mount7;
53993197ebdbSTheodore Ts'o 
540002f310fcSJan Kara 	err = ext4_init_orphan_info(sb);
540102f310fcSJan Kara 	if (err)
540202f310fcSJan Kara 		goto failed_mount8;
54039b2ff357SJan Kara #ifdef CONFIG_QUOTA
54049b2ff357SJan Kara 	/* Enable quota usage during mount. */
5405bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
54069b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
54079b2ff357SJan Kara 		if (err)
540802f310fcSJan Kara 			goto failed_mount9;
54099b2ff357SJan Kara 	}
54109b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
54119b2ff357SJan Kara 
5412bc71726cSzhangyi (F) 	/*
5413bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
5414bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
5415bc71726cSzhangyi (F) 	 */
5416bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
5417bc71726cSzhangyi (F) 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5418bc71726cSzhangyi (F) 				 &sbi->s_bdev_wb_err);
5419bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
5420617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5421617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
5422617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
54230390131bSFrank Mayhar 	if (needs_recovery) {
5424b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
542511215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
542611215630SJan Kara 		if (err)
542702f310fcSJan Kara 			goto failed_mount9;
54280390131bSFrank Mayhar 	}
54290390131bSFrank Mayhar 
543079add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
543179add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
543279add3a3SLukas Czerner 		if (!blk_queue_discard(q))
543379add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
543479add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
543579add3a3SLukas Czerner 				 "the device does not support discard");
543679add3a3SLukas Czerner 	}
543779add3a3SLukas Czerner 
543866e61a9eSTheodore Ts'o 	if (es->s_error_count)
543966e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5440ac27a0ecSDave Kleikamp 
5441efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5442efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5443efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5444efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
54451cf006edSDmitry Monakhov 	atomic_set(&sbi->s_warning_count, 0);
54461cf006edSDmitry Monakhov 	atomic_set(&sbi->s_msg_count, 0);
5447efbed4dcSTheodore Ts'o 
5448ac27a0ecSDave Kleikamp 	return 0;
5449ac27a0ecSDave Kleikamp 
5450617ba13bSMingming Cao cantfind_ext4:
5451ac27a0ecSDave Kleikamp 	if (!silent)
5452b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5453ac27a0ecSDave Kleikamp 	goto failed_mount;
5454ac27a0ecSDave Kleikamp 
545502f310fcSJan Kara failed_mount9:
545602f310fcSJan Kara 	ext4_release_orphan_info(sb);
545772ba7450STheodore Ts'o failed_mount8:
5458ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
5459cb8d53d2SEric Biggers 	kobject_put(&sbi->s_kobj);
5460dcf2d804STao Ma failed_mount7:
5461dcf2d804STao Ma 	ext4_unregister_li_request(sb);
5462dcf2d804STao Ma failed_mount6:
5463f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
54647c990728SSuraj Jitindar Singh 	rcu_read_lock();
54657c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
54667c990728SSuraj Jitindar Singh 	if (flex_groups) {
54677c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
54687c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
54697c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
54707c990728SSuraj Jitindar Singh 	}
54717c990728SSuraj Jitindar Singh 	rcu_read_unlock();
5472d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
5473d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
5474d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
5475d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5476efc61345SEric Whitney 	percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
5477bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
547800764937SAzat Khuzhin failed_mount5:
5479f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
5480f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
5481f9ae9cf5STheodore Ts'o failed_mount4a:
548294bf608aSAl Viro 	dput(sb->s_root);
548332a9bb57SManish Katiyar 	sb->s_root = NULL;
548494bf608aSAl Viro failed_mount4:
5485b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
54862e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
54872e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
54884c0425ffSMingming Cao failed_mount_wq:
5489dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5490dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
549150c15df6SChengguang Xu 
549247387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
549347387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
549450c15df6SChengguang Xu 
54950390131bSFrank Mayhar 	if (sbi->s_journal) {
5496bb9464e0Syangerkun 		/* flush s_error_work before journal destroy. */
5497bb9464e0Syangerkun 		flush_work(&sbi->s_error_work);
5498dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
549947b4a50bSJan Kara 		sbi->s_journal = NULL;
55000390131bSFrank Mayhar 	}
550150460fe8SDarrick J. Wong failed_mount3a:
5502d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
5503eb68d0e2SZheng Liu failed_mount3:
5504bb9464e0Syangerkun 	/* flush s_error_work before sbi destroy */
5505c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
55062a4ae3bcSJan Kara 	del_timer_sync(&sbi->s_err_report);
5507618f0031SPavel Skripkin 	ext4_stop_mmpd(sbi);
5508ac27a0ecSDave Kleikamp failed_mount2:
55091d0c3924STheodore Ts'o 	rcu_read_lock();
55101d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
5511ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
55121d0c3924STheodore Ts'o 		brelse(group_desc[i]);
55131d0c3924STheodore Ts'o 	kvfree(group_desc);
55141d0c3924STheodore Ts'o 	rcu_read_unlock();
5515ac27a0ecSDave Kleikamp failed_mount:
55160441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
55170441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
5518c83ad55eSGabriel Krisman Bertazi 
5519c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
5520f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
5521c83ad55eSGabriel Krisman Bertazi #endif
5522c83ad55eSGabriel Krisman Bertazi 
5523ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5524a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
55250ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
5526ac27a0ecSDave Kleikamp #endif
5527ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5528afd09b61SAlexey Makhalov 	/* ext4_blkdev_remove() calls kill_bdev(), release bh before it. */
5529ac27a0ecSDave Kleikamp 	brelse(bh);
5530afd09b61SAlexey Makhalov 	ext4_blkdev_remove(sbi);
5531ac27a0ecSDave Kleikamp out_fail:
5532ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
553307aa2ea1SLukas Czerner 	return err ? err : ret;
5534ac27a0ecSDave Kleikamp }
5535ac27a0ecSDave Kleikamp 
5536cebe85d5SLukas Czerner static int ext4_fill_super(struct super_block *sb, struct fs_context *fc)
55377edfd85bSLukas Czerner {
5538cebe85d5SLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
55397edfd85bSLukas Czerner 	struct ext4_sb_info *sbi;
55407edfd85bSLukas Czerner 	const char *descr;
5541cebe85d5SLukas Czerner 	int ret;
55427edfd85bSLukas Czerner 
5543cebe85d5SLukas Czerner 	sbi = ext4_alloc_sbi(sb);
5544cebe85d5SLukas Czerner 	if (!sbi)
5545cebe85d5SLukas Czerner 		ret = -ENOMEM;
5546cebe85d5SLukas Czerner 
5547cebe85d5SLukas Czerner 	fc->s_fs_info = sbi;
55487edfd85bSLukas Czerner 
55497edfd85bSLukas Czerner 	/* Cleanup superblock name */
55507edfd85bSLukas Czerner 	strreplace(sb->s_id, '/', '!');
55517edfd85bSLukas Czerner 
55527edfd85bSLukas Czerner 	sbi->s_sb_block = 1;	/* Default super block location */
5553cebe85d5SLukas Czerner 	if (ctx->spec & EXT4_SPEC_s_sb_block)
5554cebe85d5SLukas Czerner 		sbi->s_sb_block = ctx->s_sb_block;
55557edfd85bSLukas Czerner 
5556960e0ab6SLukas Czerner 	ret = __ext4_fill_super(fc, sb);
55577edfd85bSLukas Czerner 	if (ret < 0)
55587edfd85bSLukas Czerner 		goto free_sbi;
55597edfd85bSLukas Czerner 
5560cebe85d5SLukas Czerner 	if (sbi->s_journal) {
55617edfd85bSLukas Czerner 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
55627edfd85bSLukas Czerner 			descr = " journalled data mode";
55637edfd85bSLukas Czerner 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
55647edfd85bSLukas Czerner 			descr = " ordered data mode";
55657edfd85bSLukas Czerner 		else
55667edfd85bSLukas Czerner 			descr = " writeback data mode";
55677edfd85bSLukas Czerner 	} else
55687edfd85bSLukas Czerner 		descr = "out journal";
55697edfd85bSLukas Czerner 
55707edfd85bSLukas Czerner 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
55717edfd85bSLukas Czerner 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
5572cebe85d5SLukas Czerner 			 "Quota mode: %s.", descr, ext4_quota_mode(sb));
55737edfd85bSLukas Czerner 
55747edfd85bSLukas Czerner 	return 0;
5575cebe85d5SLukas Czerner 
55767edfd85bSLukas Czerner free_sbi:
55777edfd85bSLukas Czerner 	ext4_free_sbi(sbi);
5578cebe85d5SLukas Czerner 	fc->s_fs_info = NULL;
55797edfd85bSLukas Czerner 	return ret;
55807edfd85bSLukas Czerner }
55817edfd85bSLukas Czerner 
5582cebe85d5SLukas Czerner static int ext4_get_tree(struct fs_context *fc)
5583cebe85d5SLukas Czerner {
5584cebe85d5SLukas Czerner 	return get_tree_bdev(fc, ext4_fill_super);
5585cebe85d5SLukas Czerner }
5586cebe85d5SLukas Czerner 
5587ac27a0ecSDave Kleikamp /*
5588ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
5589ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
5590ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
5591ac27a0ecSDave Kleikamp  */
5592617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5593ac27a0ecSDave Kleikamp {
5594617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5595ac27a0ecSDave Kleikamp 
5596ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
559730773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
559830773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
55996866d7b3SHarshad Shirwadkar 	ext4_fc_init(sb, journal);
5600ac27a0ecSDave Kleikamp 
5601a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
5602ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
5603dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
5604ac27a0ecSDave Kleikamp 	else
5605dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
56065bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
56075bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
56085bf5683aSHidehiro Kawai 	else
56095bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5610a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
5611ac27a0ecSDave Kleikamp }
5612ac27a0ecSDave Kleikamp 
5613c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
5614ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
5615ac27a0ecSDave Kleikamp {
5616ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
5617ac27a0ecSDave Kleikamp 
5618c6cb7e77SEric Whitney 	/*
5619c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
5620c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
5621c6cb7e77SEric Whitney 	 * will try to delete it.
5622c6cb7e77SEric Whitney 	 */
56238a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
56241d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
5625b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
5626ac27a0ecSDave Kleikamp 		return NULL;
5627ac27a0ecSDave Kleikamp 	}
5628ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
5629ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
5630ac27a0ecSDave Kleikamp 		iput(journal_inode);
5631b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5632ac27a0ecSDave Kleikamp 		return NULL;
5633ac27a0ecSDave Kleikamp 	}
5634ac27a0ecSDave Kleikamp 
5635e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5636ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
56371d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
5638b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5639ac27a0ecSDave Kleikamp 		iput(journal_inode);
5640ac27a0ecSDave Kleikamp 		return NULL;
5641ac27a0ecSDave Kleikamp 	}
5642c6cb7e77SEric Whitney 	return journal_inode;
5643c6cb7e77SEric Whitney }
5644c6cb7e77SEric Whitney 
5645c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
5646c6cb7e77SEric Whitney 				   unsigned int journal_inum)
5647c6cb7e77SEric Whitney {
5648c6cb7e77SEric Whitney 	struct inode *journal_inode;
5649c6cb7e77SEric Whitney 	journal_t *journal;
5650c6cb7e77SEric Whitney 
565111215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
565211215630SJan Kara 		return NULL;
5653c6cb7e77SEric Whitney 
5654c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5655c6cb7e77SEric Whitney 	if (!journal_inode)
5656c6cb7e77SEric Whitney 		return NULL;
5657ac27a0ecSDave Kleikamp 
5658dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
5659ac27a0ecSDave Kleikamp 	if (!journal) {
5660b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5661ac27a0ecSDave Kleikamp 		iput(journal_inode);
5662ac27a0ecSDave Kleikamp 		return NULL;
5663ac27a0ecSDave Kleikamp 	}
5664ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5665617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5666ac27a0ecSDave Kleikamp 	return journal;
5667ac27a0ecSDave Kleikamp }
5668ac27a0ecSDave Kleikamp 
5669617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
5670ac27a0ecSDave Kleikamp 				       dev_t j_dev)
5671ac27a0ecSDave Kleikamp {
5672ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5673ac27a0ecSDave Kleikamp 	journal_t *journal;
5674617ba13bSMingming Cao 	ext4_fsblk_t start;
5675617ba13bSMingming Cao 	ext4_fsblk_t len;
5676ac27a0ecSDave Kleikamp 	int hblock, blocksize;
5677617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
5678ac27a0ecSDave Kleikamp 	unsigned long offset;
5679617ba13bSMingming Cao 	struct ext4_super_block *es;
5680ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5681ac27a0ecSDave Kleikamp 
568211215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
568311215630SJan Kara 		return NULL;
56840390131bSFrank Mayhar 
5685b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5686ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5687ac27a0ecSDave Kleikamp 		return NULL;
5688ac27a0ecSDave Kleikamp 
5689ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5690e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5691ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5692b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5693b31e1552SEric Sandeen 			"blocksize too small for journal device");
5694ac27a0ecSDave Kleikamp 		goto out_bdev;
5695ac27a0ecSDave Kleikamp 	}
5696ac27a0ecSDave Kleikamp 
5697617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5698617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5699ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5700ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5701b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5702b31e1552SEric Sandeen 		       "external journal");
5703ac27a0ecSDave Kleikamp 		goto out_bdev;
5704ac27a0ecSDave Kleikamp 	}
5705ac27a0ecSDave Kleikamp 
57062716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5707617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5708ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5709617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5710b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5711b31e1552SEric Sandeen 					"bad superblock");
5712ac27a0ecSDave Kleikamp 		brelse(bh);
5713ac27a0ecSDave Kleikamp 		goto out_bdev;
5714ac27a0ecSDave Kleikamp 	}
5715ac27a0ecSDave Kleikamp 
5716df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5717df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5718df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5719df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5720df4763beSDarrick J. Wong 				       "corrupt superblock");
5721df4763beSDarrick J. Wong 		brelse(bh);
5722df4763beSDarrick J. Wong 		goto out_bdev;
5723df4763beSDarrick J. Wong 	}
5724df4763beSDarrick J. Wong 
5725617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5726b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5727ac27a0ecSDave Kleikamp 		brelse(bh);
5728ac27a0ecSDave Kleikamp 		goto out_bdev;
5729ac27a0ecSDave Kleikamp 	}
5730ac27a0ecSDave Kleikamp 
5731bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5732ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5733ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5734ac27a0ecSDave Kleikamp 
5735dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5736ac27a0ecSDave Kleikamp 					start, len, blocksize);
5737ac27a0ecSDave Kleikamp 	if (!journal) {
5738b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5739ac27a0ecSDave Kleikamp 		goto out_bdev;
5740ac27a0ecSDave Kleikamp 	}
5741ac27a0ecSDave Kleikamp 	journal->j_private = sb;
57422d069c08Szhangyi (F) 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5743b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5744ac27a0ecSDave Kleikamp 		goto out_journal;
5745ac27a0ecSDave Kleikamp 	}
5746ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5747b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5748b31e1552SEric Sandeen 					"user (unsupported) - %d",
5749ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5750ac27a0ecSDave Kleikamp 		goto out_journal;
5751ac27a0ecSDave Kleikamp 	}
5752ee7ed3aaSChunguang Xu 	EXT4_SB(sb)->s_journal_bdev = bdev;
5753617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5754ac27a0ecSDave Kleikamp 	return journal;
57550b8e58a1SAndreas Dilger 
5756ac27a0ecSDave Kleikamp out_journal:
5757dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5758ac27a0ecSDave Kleikamp out_bdev:
5759617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5760ac27a0ecSDave Kleikamp 	return NULL;
5761ac27a0ecSDave Kleikamp }
5762ac27a0ecSDave Kleikamp 
5763617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5764617ba13bSMingming Cao 			     struct ext4_super_block *es,
5765ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5766ac27a0ecSDave Kleikamp {
5767ac27a0ecSDave Kleikamp 	journal_t *journal;
5768ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5769ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5770ac27a0ecSDave Kleikamp 	int err = 0;
5771ac27a0ecSDave Kleikamp 	int really_read_only;
5772273108faSLukas Czerner 	int journal_dev_ro;
5773ac27a0ecSDave Kleikamp 
577411215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
577511215630SJan Kara 		return -EFSCORRUPTED;
57760390131bSFrank Mayhar 
5777ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5778ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5779b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5780b31e1552SEric Sandeen 			"numbers have changed");
5781ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5782ac27a0ecSDave Kleikamp 	} else
5783ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5784ac27a0ecSDave Kleikamp 
5785273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5786273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5787273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5788273108faSLukas Czerner 		return -EINVAL;
5789273108faSLukas Czerner 	}
5790273108faSLukas Czerner 
5791273108faSLukas Czerner 	if (journal_inum) {
5792273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5793273108faSLukas Czerner 		if (!journal)
5794273108faSLukas Czerner 			return -EINVAL;
5795273108faSLukas Czerner 	} else {
5796273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5797273108faSLukas Czerner 		if (!journal)
5798273108faSLukas Czerner 			return -EINVAL;
5799273108faSLukas Czerner 	}
5800273108faSLukas Czerner 
5801273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5802273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5803273108faSLukas Czerner 
5804273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5805273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5806273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5807273108faSLukas Czerner 		err = -EROFS;
5808273108faSLukas Czerner 		goto err_out;
5809273108faSLukas Czerner 	}
5810ac27a0ecSDave Kleikamp 
5811ac27a0ecSDave Kleikamp 	/*
5812ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5813ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5814ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5815ac27a0ecSDave Kleikamp 	 */
5816e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5817bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5818b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5819b31e1552SEric Sandeen 					"required on readonly filesystem");
5820ac27a0ecSDave Kleikamp 			if (really_read_only) {
5821b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5822d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5823d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5824273108faSLukas Czerner 				err = -EROFS;
5825273108faSLukas Czerner 				goto err_out;
5826ac27a0ecSDave Kleikamp 			}
5827b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5828b31e1552SEric Sandeen 			       "be enabled during recovery");
5829ac27a0ecSDave Kleikamp 		}
5830ac27a0ecSDave Kleikamp 	}
5831ac27a0ecSDave Kleikamp 
583290576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5833b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
58344776004fSTheodore Ts'o 
5835e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5836dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
58371c13d5c0STheodore Ts'o 	if (!err) {
58381c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
58391c13d5c0STheodore Ts'o 		if (save)
58401c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
58411c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5842dab291afSMingming Cao 		err = jbd2_journal_load(journal);
58431c13d5c0STheodore Ts'o 		if (save)
58441c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
58451c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
58461c13d5c0STheodore Ts'o 		kfree(save);
58471c13d5c0STheodore Ts'o 	}
5848ac27a0ecSDave Kleikamp 
5849ac27a0ecSDave Kleikamp 	if (err) {
5850b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5851273108faSLukas Czerner 		goto err_out;
5852ac27a0ecSDave Kleikamp 	}
5853ac27a0ecSDave Kleikamp 
5854617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
585511215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
585611215630SJan Kara 	if (err) {
585711215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
585811215630SJan Kara 		jbd2_journal_destroy(journal);
585911215630SJan Kara 		return err;
586011215630SJan Kara 	}
5861ac27a0ecSDave Kleikamp 
5862c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5863ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5864ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5865ac27a0ecSDave Kleikamp 
5866ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
58674392fbc4SJan Kara 		ext4_commit_super(sb);
5868ac27a0ecSDave Kleikamp 	}
5869ac27a0ecSDave Kleikamp 
5870ac27a0ecSDave Kleikamp 	return 0;
5871273108faSLukas Czerner 
5872273108faSLukas Czerner err_out:
5873273108faSLukas Czerner 	jbd2_journal_destroy(journal);
5874273108faSLukas Czerner 	return err;
5875ac27a0ecSDave Kleikamp }
5876ac27a0ecSDave Kleikamp 
58772d01ddc8SJan Kara /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
58782d01ddc8SJan Kara static void ext4_update_super(struct super_block *sb)
5879ac27a0ecSDave Kleikamp {
5880c92dc856SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5881e92ad03fSJan Kara 	struct ext4_super_block *es = sbi->s_es;
5882e92ad03fSJan Kara 	struct buffer_head *sbh = sbi->s_sbh;
5883ac27a0ecSDave Kleikamp 
588405c2c00fSJan Kara 	lock_buffer(sbh);
5885a17712c8SJon Derrick 	/*
588671290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
588771290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
588871290b36STheodore Ts'o 	 * write time when we are mounting the root file system
588971290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
589071290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
589171290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
589271290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
589371290b36STheodore Ts'o 	 * to complain and force a full file system check.
589471290b36STheodore Ts'o 	 */
58951751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
58966a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5897afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
5898e92ad03fSJan Kara 		cpu_to_le64(sbi->s_kbytes_written +
58998446fe92SChristoph Hellwig 		    ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
5900e92ad03fSJan Kara 		      sbi->s_sectors_written_start) >> 1));
5901e92ad03fSJan Kara 	if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
590257042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
5903e92ad03fSJan Kara 			EXT4_C2B(sbi, percpu_counter_sum_positive(
5904e92ad03fSJan Kara 				&sbi->s_freeclusters_counter)));
5905e92ad03fSJan Kara 	if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
59067f93cff9STheodore Ts'o 		es->s_free_inodes_count =
59077f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
5908e92ad03fSJan Kara 				&sbi->s_freeinodes_counter));
5909c92dc856SJan Kara 	/* Copy error information to the on-disk superblock */
5910c92dc856SJan Kara 	spin_lock(&sbi->s_error_lock);
5911c92dc856SJan Kara 	if (sbi->s_add_error_count > 0) {
5912c92dc856SJan Kara 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5913c92dc856SJan Kara 		if (!es->s_first_error_time && !es->s_first_error_time_hi) {
5914c92dc856SJan Kara 			__ext4_update_tstamp(&es->s_first_error_time,
5915c92dc856SJan Kara 					     &es->s_first_error_time_hi,
5916c92dc856SJan Kara 					     sbi->s_first_error_time);
5917c92dc856SJan Kara 			strncpy(es->s_first_error_func, sbi->s_first_error_func,
5918c92dc856SJan Kara 				sizeof(es->s_first_error_func));
5919c92dc856SJan Kara 			es->s_first_error_line =
5920c92dc856SJan Kara 				cpu_to_le32(sbi->s_first_error_line);
5921c92dc856SJan Kara 			es->s_first_error_ino =
5922c92dc856SJan Kara 				cpu_to_le32(sbi->s_first_error_ino);
5923c92dc856SJan Kara 			es->s_first_error_block =
5924c92dc856SJan Kara 				cpu_to_le64(sbi->s_first_error_block);
5925c92dc856SJan Kara 			es->s_first_error_errcode =
5926c92dc856SJan Kara 				ext4_errno_to_code(sbi->s_first_error_code);
5927c92dc856SJan Kara 		}
5928c92dc856SJan Kara 		__ext4_update_tstamp(&es->s_last_error_time,
5929c92dc856SJan Kara 				     &es->s_last_error_time_hi,
5930c92dc856SJan Kara 				     sbi->s_last_error_time);
5931c92dc856SJan Kara 		strncpy(es->s_last_error_func, sbi->s_last_error_func,
5932c92dc856SJan Kara 			sizeof(es->s_last_error_func));
5933c92dc856SJan Kara 		es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
5934c92dc856SJan Kara 		es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
5935c92dc856SJan Kara 		es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
5936c92dc856SJan Kara 		es->s_last_error_errcode =
5937c92dc856SJan Kara 				ext4_errno_to_code(sbi->s_last_error_code);
5938c92dc856SJan Kara 		/*
5939c92dc856SJan Kara 		 * Start the daily error reporting function if it hasn't been
5940c92dc856SJan Kara 		 * started already
5941c92dc856SJan Kara 		 */
5942c92dc856SJan Kara 		if (!es->s_error_count)
5943c92dc856SJan Kara 			mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
5944c92dc856SJan Kara 		le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
5945c92dc856SJan Kara 		sbi->s_add_error_count = 0;
5946c92dc856SJan Kara 	}
5947c92dc856SJan Kara 	spin_unlock(&sbi->s_error_lock);
5948c92dc856SJan Kara 
594906db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
59502d01ddc8SJan Kara 	unlock_buffer(sbh);
59512d01ddc8SJan Kara }
59522d01ddc8SJan Kara 
59532d01ddc8SJan Kara static int ext4_commit_super(struct super_block *sb)
59542d01ddc8SJan Kara {
59552d01ddc8SJan Kara 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
59562d01ddc8SJan Kara 	int error = 0;
59572d01ddc8SJan Kara 
5958f88f1466SFengnan Chang 	if (!sbh)
5959f88f1466SFengnan Chang 		return -EINVAL;
5960f88f1466SFengnan Chang 	if (block_device_ejected(sb))
5961f88f1466SFengnan Chang 		return -ENODEV;
59622d01ddc8SJan Kara 
59632d01ddc8SJan Kara 	ext4_update_super(sb);
59642d01ddc8SJan Kara 
5965e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
59664743f839SPranay Kr. Srivastava 		/*
59674743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
59684743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
59694743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
59704743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
59714743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
59724743f839SPranay Kr. Srivastava 		 * write and hope for the best.
59734743f839SPranay Kr. Srivastava 		 */
59744743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
59754743f839SPranay Kr. Srivastava 		       "superblock detected");
59764743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
59774743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
59784743f839SPranay Kr. Srivastava 	}
59792d01ddc8SJan Kara 	BUFFER_TRACE(sbh, "marking dirty");
5980ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5981564bc402SDaeho Jeong 	error = __sync_dirty_buffer(sbh,
598200473374SJan Kara 		REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5983c89128a0SJaegeuk Kim 	if (buffer_write_io_error(sbh)) {
5984b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error while writing "
5985b31e1552SEric Sandeen 		       "superblock");
5986914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
5987914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
5988914258bfSTheodore Ts'o 	}
5989c4be0c1dSTakashi Sato 	return error;
5990ac27a0ecSDave Kleikamp }
5991ac27a0ecSDave Kleikamp 
5992ac27a0ecSDave Kleikamp /*
5993ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5994ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5995ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5996ac27a0ecSDave Kleikamp  */
599711215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
5998617ba13bSMingming Cao 				       struct ext4_super_block *es)
5999ac27a0ecSDave Kleikamp {
600011215630SJan Kara 	int err;
6001617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
6002ac27a0ecSDave Kleikamp 
6003e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
600411215630SJan Kara 		if (journal != NULL) {
600511215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
600611215630SJan Kara 				   "mounted!");
600711215630SJan Kara 			return -EFSCORRUPTED;
600811215630SJan Kara 		}
600911215630SJan Kara 		return 0;
60100390131bSFrank Mayhar 	}
6011dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
601201d5d965SLeah Rumancik 	err = jbd2_journal_flush(journal, 0);
601311215630SJan Kara 	if (err < 0)
60147ffe1ea8SHidehiro Kawai 		goto out;
60157ffe1ea8SHidehiro Kawai 
601602f310fcSJan Kara 	if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
601702f310fcSJan Kara 	    ext4_has_feature_orphan_present(sb))) {
601802f310fcSJan Kara 		if (!ext4_orphan_file_empty(sb)) {
601902f310fcSJan Kara 			ext4_error(sb, "Orphan file not empty on read-only fs.");
602002f310fcSJan Kara 			err = -EFSCORRUPTED;
602102f310fcSJan Kara 			goto out;
602202f310fcSJan Kara 		}
6023e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
602402f310fcSJan Kara 		ext4_clear_feature_orphan_present(sb);
60254392fbc4SJan Kara 		ext4_commit_super(sb);
6026ac27a0ecSDave Kleikamp 	}
60277ffe1ea8SHidehiro Kawai out:
6028dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
602911215630SJan Kara 	return err;
6030ac27a0ecSDave Kleikamp }
6031ac27a0ecSDave Kleikamp 
6032ac27a0ecSDave Kleikamp /*
6033ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
6034ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
6035ac27a0ecSDave Kleikamp  * main filesystem now.
6036ac27a0ecSDave Kleikamp  */
603711215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
6038617ba13bSMingming Cao 				   struct ext4_super_block *es)
6039ac27a0ecSDave Kleikamp {
6040ac27a0ecSDave Kleikamp 	journal_t *journal;
6041ac27a0ecSDave Kleikamp 	int j_errno;
6042ac27a0ecSDave Kleikamp 	const char *errstr;
6043ac27a0ecSDave Kleikamp 
604411215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
604511215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
604611215630SJan Kara 		return -EFSCORRUPTED;
604711215630SJan Kara 	}
60480390131bSFrank Mayhar 
6049617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
6050ac27a0ecSDave Kleikamp 
6051ac27a0ecSDave Kleikamp 	/*
6052ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
6053617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
6054ac27a0ecSDave Kleikamp 	 */
6055ac27a0ecSDave Kleikamp 
6056dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
6057ac27a0ecSDave Kleikamp 	if (j_errno) {
6058ac27a0ecSDave Kleikamp 		char nbuf[16];
6059ac27a0ecSDave Kleikamp 
6060617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
606112062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
6062ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
606312062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
6064ac27a0ecSDave Kleikamp 
6065617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
6066617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
60674392fbc4SJan Kara 		ext4_commit_super(sb);
6068ac27a0ecSDave Kleikamp 
6069dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
6070d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
6071ac27a0ecSDave Kleikamp 	}
607211215630SJan Kara 	return 0;
6073ac27a0ecSDave Kleikamp }
6074ac27a0ecSDave Kleikamp 
6075ac27a0ecSDave Kleikamp /*
6076ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
6077ac27a0ecSDave Kleikamp  * and wait on the commit.
6078ac27a0ecSDave Kleikamp  */
6079617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
6080ac27a0ecSDave Kleikamp {
6081ac27a0ecSDave Kleikamp 	journal_t *journal;
6082ac27a0ecSDave Kleikamp 
6083bc98a42cSDavid Howells 	if (sb_rdonly(sb))
6084ac27a0ecSDave Kleikamp 		return 0;
6085ac27a0ecSDave Kleikamp 
6086617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
6087b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
6088ac27a0ecSDave Kleikamp }
6089ac27a0ecSDave Kleikamp 
6090617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
6091ac27a0ecSDave Kleikamp {
609214ce0cb4STheodore Ts'o 	int ret = 0;
60939eddacf9SJan Kara 	tid_t target;
609406a407f1SDmitry Monakhov 	bool needs_barrier = false;
60958d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6096ac27a0ecSDave Kleikamp 
609749598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
60980db1ff22STheodore Ts'o 		return 0;
60990db1ff22STheodore Ts'o 
61009bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
61012e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
6102a1177825SJan Kara 	/*
6103a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
6104a1177825SJan Kara 	 * no dirty dquots
6105a1177825SJan Kara 	 */
6106a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
610706a407f1SDmitry Monakhov 	/*
610806a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
610906a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
611006a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
611106a407f1SDmitry Monakhov 	 */
6112bda32530STheodore Ts'o 	if (sbi->s_journal) {
611306a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
611406a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
611506a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
611606a407f1SDmitry Monakhov 			needs_barrier = true;
611706a407f1SDmitry Monakhov 
61188d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
6119ac27a0ecSDave Kleikamp 			if (wait)
6120bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
6121bda32530STheodore Ts'o 							   target);
61220390131bSFrank Mayhar 		}
6123bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
6124bda32530STheodore Ts'o 		needs_barrier = true;
612506a407f1SDmitry Monakhov 	if (needs_barrier) {
612606a407f1SDmitry Monakhov 		int err;
6127c6bf3f0eSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev);
612806a407f1SDmitry Monakhov 		if (!ret)
612906a407f1SDmitry Monakhov 			ret = err;
613006a407f1SDmitry Monakhov 	}
613106a407f1SDmitry Monakhov 
613206a407f1SDmitry Monakhov 	return ret;
613306a407f1SDmitry Monakhov }
613406a407f1SDmitry Monakhov 
6135ac27a0ecSDave Kleikamp /*
6136ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
6137ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
6138be4f27d3SYongqiang Yang  *
6139be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
61408e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
61418e8ad8a5SJan Kara  * modifications.
6142ac27a0ecSDave Kleikamp  */
6143c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
6144ac27a0ecSDave Kleikamp {
6145c4be0c1dSTakashi Sato 	int error = 0;
6146c4be0c1dSTakashi Sato 	journal_t *journal;
6147ac27a0ecSDave Kleikamp 
6148bc98a42cSDavid Howells 	if (sb_rdonly(sb))
61499ca92389STheodore Ts'o 		return 0;
61509ca92389STheodore Ts'o 
6151c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
6152ac27a0ecSDave Kleikamp 
6153bb044576STheodore Ts'o 	if (journal) {
6154ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
6155dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
61567ffe1ea8SHidehiro Kawai 
61577ffe1ea8SHidehiro Kawai 		/*
6158bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
6159bb044576STheodore Ts'o 		 * flush the journal.
61607ffe1ea8SHidehiro Kawai 		 */
616101d5d965SLeah Rumancik 		error = jbd2_journal_flush(journal, 0);
61626b0310fbSEric Sandeen 		if (error < 0)
61636b0310fbSEric Sandeen 			goto out;
6164ac27a0ecSDave Kleikamp 
6165ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
6166e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
616702f310fcSJan Kara 		if (ext4_orphan_file_empty(sb))
616802f310fcSJan Kara 			ext4_clear_feature_orphan_present(sb);
6169c642dc9eSEric Sandeen 	}
6170c642dc9eSEric Sandeen 
61714392fbc4SJan Kara 	error = ext4_commit_super(sb);
61726b0310fbSEric Sandeen out:
6173bb044576STheodore Ts'o 	if (journal)
61748e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
6175bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
61766b0310fbSEric Sandeen 	return error;
6177ac27a0ecSDave Kleikamp }
6178ac27a0ecSDave Kleikamp 
6179ac27a0ecSDave Kleikamp /*
6180ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
6181ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
6182ac27a0ecSDave Kleikamp  */
6183c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
6184ac27a0ecSDave Kleikamp {
6185bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
61869ca92389STheodore Ts'o 		return 0;
61879ca92389STheodore Ts'o 
6188c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
61899ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
6190e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
619102f310fcSJan Kara 		if (ext4_has_feature_orphan_file(sb))
619202f310fcSJan Kara 			ext4_set_feature_orphan_present(sb);
6193c642dc9eSEric Sandeen 	}
6194c642dc9eSEric Sandeen 
61954392fbc4SJan Kara 	ext4_commit_super(sb);
6196c4be0c1dSTakashi Sato 	return 0;
6197ac27a0ecSDave Kleikamp }
6198ac27a0ecSDave Kleikamp 
6199673c6100STheodore Ts'o /*
6200673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
6201673c6100STheodore Ts'o  */
6202673c6100STheodore Ts'o struct ext4_mount_options {
6203673c6100STheodore Ts'o 	unsigned long s_mount_opt;
6204a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
620508cefc7aSEric W. Biederman 	kuid_t s_resuid;
620608cefc7aSEric W. Biederman 	kgid_t s_resgid;
6207673c6100STheodore Ts'o 	unsigned long s_commit_interval;
6208673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
6209673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
6210673c6100STheodore Ts'o 	int s_jquota_fmt;
6211a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
6212673c6100STheodore Ts'o #endif
6213673c6100STheodore Ts'o };
6214673c6100STheodore Ts'o 
6215960e0ab6SLukas Czerner static int __ext4_remount(struct fs_context *fc, struct super_block *sb)
6216ac27a0ecSDave Kleikamp {
62177edfd85bSLukas Czerner 	struct ext4_fs_context *ctx = fc->fs_private;
6218617ba13bSMingming Cao 	struct ext4_super_block *es;
6219617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6220960e0ab6SLukas Czerner 	unsigned long old_sb_flags;
6221617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
62228a266467STheodore Ts'o 	ext4_group_t g;
6223c5e06d10SJohann Lombardi 	int err = 0;
6224ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
62253bbef91bSAustin Kim 	int enable_quota = 0;
622603dafb5fSChen Gang 	int i, j;
622733458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
6228ac27a0ecSDave Kleikamp #endif
6229b237e304SHarshad Shirwadkar 
62307edfd85bSLukas Czerner 	ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
623121ac738eSChengguang Xu 
6232ac27a0ecSDave Kleikamp 	/* Store the original options */
6233ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
6234ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
6235a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
6236ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
6237ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
6238ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
623930773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
624030773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
6241ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6242ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
6243a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
624403dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
624533458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
624633458eabSTheodore Ts'o 
624733458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
624803dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
624903dafb5fSChen Gang 				for (j = 0; j < i; j++)
625003dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
625103dafb5fSChen Gang 				return -ENOMEM;
625203dafb5fSChen Gang 			}
625303dafb5fSChen Gang 		} else
625403dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
6255ac27a0ecSDave Kleikamp #endif
6256b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
62577edfd85bSLukas Czerner 		ctx->journal_ioprio =
6258b237e304SHarshad Shirwadkar 			sbi->s_journal->j_task->io_context->ioprio;
6259ac27a0ecSDave Kleikamp 
62607edfd85bSLukas Czerner 	ext4_apply_options(fc, sb);
6261ac27a0ecSDave Kleikamp 
62626b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
62636b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
62646b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
62652d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
62662d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
6267c6d3d56dSDarrick J. Wong 	}
6268c6d3d56dSDarrick J. Wong 
62696ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
62706ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
62716ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
62726ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
62736ae6514bSPiotr Sarna 			err = -EINVAL;
62746ae6514bSPiotr Sarna 			goto restore_opts;
62756ae6514bSPiotr Sarna 		}
62766ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
62776ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
62786ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
62796ae6514bSPiotr Sarna 			err = -EINVAL;
62806ae6514bSPiotr Sarna 			goto restore_opts;
62816ae6514bSPiotr Sarna 		}
6282ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
6283ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
6284ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
6285ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
6286ab04df78SJan Kara 			err = -EINVAL;
6287ab04df78SJan Kara 			goto restore_opts;
6288ab04df78SJan Kara 		}
6289923ae0ffSRoss Zwisler 	}
6290923ae0ffSRoss Zwisler 
6291cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
6292cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
6293cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
6294cdb7ee4cSTahsin Erdogan 		goto restore_opts;
6295cdb7ee4cSTahsin Erdogan 	}
6296cdb7ee4cSTahsin Erdogan 
62979b5f6c9bSHarshad Shirwadkar 	if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
6298124e7c61SGabriel Krisman Bertazi 		ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
6299ac27a0ecSDave Kleikamp 
63001751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
63011751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
6302ac27a0ecSDave Kleikamp 
6303ac27a0ecSDave Kleikamp 	es = sbi->s_es;
6304ac27a0ecSDave Kleikamp 
6305b3881f74STheodore Ts'o 	if (sbi->s_journal) {
6306617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
63077edfd85bSLukas Czerner 		set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
6308b3881f74STheodore Ts'o 	}
6309ac27a0ecSDave Kleikamp 
6310c92dc856SJan Kara 	/* Flush outstanding errors before changing fs state */
6311c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
6312c92dc856SJan Kara 
6313960e0ab6SLukas Czerner 	if ((bool)(fc->sb_flags & SB_RDONLY) != sb_rdonly(sb)) {
63149b5f6c9bSHarshad Shirwadkar 		if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) {
6315ac27a0ecSDave Kleikamp 			err = -EROFS;
6316ac27a0ecSDave Kleikamp 			goto restore_opts;
6317ac27a0ecSDave Kleikamp 		}
6318ac27a0ecSDave Kleikamp 
6319960e0ab6SLukas Czerner 		if (fc->sb_flags & SB_RDONLY) {
632038c03b34STheodore Ts'o 			err = sync_filesystem(sb);
632138c03b34STheodore Ts'o 			if (err < 0)
632238c03b34STheodore Ts'o 				goto restore_opts;
63230f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
63240f0dd62fSChristoph Hellwig 			if (err < 0)
6325c79d967dSChristoph Hellwig 				goto restore_opts;
6326c79d967dSChristoph Hellwig 
6327ac27a0ecSDave Kleikamp 			/*
6328ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
6329ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
6330ac27a0ecSDave Kleikamp 			 */
63311751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
6332ac27a0ecSDave Kleikamp 
6333ac27a0ecSDave Kleikamp 			/*
6334ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
6335ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
6336ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
6337ac27a0ecSDave Kleikamp 			 */
6338617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
6339617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
6340ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
6341ac27a0ecSDave Kleikamp 
634211215630SJan Kara 			if (sbi->s_journal) {
634311215630SJan Kara 				/*
634411215630SJan Kara 				 * We let remount-ro finish even if marking fs
634511215630SJan Kara 				 * as clean failed...
634611215630SJan Kara 				 */
6347617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
634811215630SJan Kara 			}
6349ac27a0ecSDave Kleikamp 		} else {
6350a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
6351e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
63522cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
6353ac27a0ecSDave Kleikamp 				err = -EROFS;
6354ac27a0ecSDave Kleikamp 				goto restore_opts;
6355ac27a0ecSDave Kleikamp 			}
6356ead6596bSEric Sandeen 			/*
63578a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
63580b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
63598a266467STheodore Ts'o 			 */
63608a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
63618a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
63628a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
63638a266467STheodore Ts'o 
6364feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
6365b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
6366b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
6367e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
63688a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
63696a797d27SDarrick J. Wong 					err = -EFSBADCRC;
63708a266467STheodore Ts'o 					goto restore_opts;
63718a266467STheodore Ts'o 				}
63728a266467STheodore Ts'o 			}
63738a266467STheodore Ts'o 
63748a266467STheodore Ts'o 			/*
6375ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
6376ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
6377ead6596bSEric Sandeen 			 * require a full umount/remount for now.
6378ead6596bSEric Sandeen 			 */
637902f310fcSJan Kara 			if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
6380b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
6381ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
6382ead6596bSEric Sandeen 				       "orphan inode list.  Please "
6383b31e1552SEric Sandeen 				       "umount/remount instead");
6384ead6596bSEric Sandeen 				err = -EINVAL;
6385ead6596bSEric Sandeen 				goto restore_opts;
6386ead6596bSEric Sandeen 			}
6387ead6596bSEric Sandeen 
6388ac27a0ecSDave Kleikamp 			/*
6389ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
6390ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
6391ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
6392ac27a0ecSDave Kleikamp 			 * the partition.)
6393ac27a0ecSDave Kleikamp 			 */
639411215630SJan Kara 			if (sbi->s_journal) {
639511215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
639611215630SJan Kara 				if (err)
639711215630SJan Kara 					goto restore_opts;
639811215630SJan Kara 			}
6399ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
6400c89128a0SJaegeuk Kim 
6401c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
6402c89128a0SJaegeuk Kim 			if (err)
6403c89128a0SJaegeuk Kim 				goto restore_opts;
6404c89128a0SJaegeuk Kim 
64051751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
6406e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
6407c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
6408c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
6409c5e06d10SJohann Lombardi 					err = -EROFS;
6410c5e06d10SJohann Lombardi 					goto restore_opts;
6411c5e06d10SJohann Lombardi 				}
64123bbef91bSAustin Kim #ifdef CONFIG_QUOTA
6413c79d967dSChristoph Hellwig 			enable_quota = 1;
64143bbef91bSAustin Kim #endif
6415ac27a0ecSDave Kleikamp 		}
6416ac27a0ecSDave Kleikamp 	}
6417bfff6873SLukas Czerner 
6418bfff6873SLukas Czerner 	/*
6419bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
6420bfff6873SLukas Czerner 	 * current settings
6421bfff6873SLukas Czerner 	 */
6422bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
6423bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
6424bfff6873SLukas Czerner 	else {
6425bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
6426bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
6427bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
6428bfff6873SLukas Czerner 	}
6429bfff6873SLukas Czerner 
64300f5bde1dSJan Kara 	/*
64310f5bde1dSJan Kara 	 * Handle creation of system zone data early because it can fail.
64320f5bde1dSJan Kara 	 * Releasing of existing data is done when we are sure remount will
64330f5bde1dSJan Kara 	 * succeed.
64340f5bde1dSJan Kara 	 */
6435dd0db94fSChunguang Xu 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
6436d176b1f6SJan Kara 		err = ext4_setup_system_zone(sb);
6437d176b1f6SJan Kara 		if (err)
6438d176b1f6SJan Kara 			goto restore_opts;
64390f5bde1dSJan Kara 	}
6440d176b1f6SJan Kara 
6441c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
64424392fbc4SJan Kara 		err = ext4_commit_super(sb);
6443c89128a0SJaegeuk Kim 		if (err)
6444c89128a0SJaegeuk Kim 			goto restore_opts;
6445c89128a0SJaegeuk Kim 	}
64460390131bSFrank Mayhar 
6447ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6448ac27a0ecSDave Kleikamp 	/* Release old quota file names */
6449a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
6450ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
64517c319d32SAditya Kali 	if (enable_quota) {
64527c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
64530f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
6454e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
64557c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
645607724f98STheodore Ts'o 			if (err)
64577c319d32SAditya Kali 				goto restore_opts;
64587c319d32SAditya Kali 		}
64597c319d32SAditya Kali 	}
64607c319d32SAditya Kali #endif
6461dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
64620f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6463d4c402d9SCurt Wohlgemuth 
646461bb4a1cSTheodore Ts'o 	if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
646561bb4a1cSTheodore Ts'o 		ext4_stop_mmpd(sbi);
646661bb4a1cSTheodore Ts'o 
6467ac27a0ecSDave Kleikamp 	return 0;
64680b8e58a1SAndreas Dilger 
6469ac27a0ecSDave Kleikamp restore_opts:
6470ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
6471ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
6472a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6473ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
6474ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
6475ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
647630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
647730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
6478dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
64790f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6480ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6481ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6482a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
648333458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
648433458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6485ac27a0ecSDave Kleikamp 	}
648633458eabSTheodore Ts'o 	synchronize_rcu();
648733458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
648833458eabSTheodore Ts'o 		kfree(to_free[i]);
6489ac27a0ecSDave Kleikamp #endif
649061bb4a1cSTheodore Ts'o 	if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
649161bb4a1cSTheodore Ts'o 		ext4_stop_mmpd(sbi);
6492ac27a0ecSDave Kleikamp 	return err;
6493ac27a0ecSDave Kleikamp }
6494ac27a0ecSDave Kleikamp 
6495cebe85d5SLukas Czerner static int ext4_reconfigure(struct fs_context *fc)
64967edfd85bSLukas Czerner {
6497cebe85d5SLukas Czerner 	struct super_block *sb = fc->root->d_sb;
64987edfd85bSLukas Czerner 	int ret;
64997edfd85bSLukas Czerner 
6500cebe85d5SLukas Czerner 	fc->s_fs_info = EXT4_SB(sb);
65017edfd85bSLukas Czerner 
6502cebe85d5SLukas Czerner 	ret = ext4_check_opt_consistency(fc, sb);
65037edfd85bSLukas Czerner 	if (ret < 0)
65047edfd85bSLukas Czerner 		return ret;
6505cebe85d5SLukas Czerner 
6506960e0ab6SLukas Czerner 	ret = __ext4_remount(fc, sb);
6507cebe85d5SLukas Czerner 	if (ret < 0)
6508cebe85d5SLukas Czerner 		return ret;
6509cebe85d5SLukas Czerner 
6510cebe85d5SLukas Czerner 	ext4_msg(sb, KERN_INFO, "re-mounted. Quota mode: %s.",
6511cebe85d5SLukas Czerner 		 ext4_quota_mode(sb));
6512cebe85d5SLukas Czerner 
6513cebe85d5SLukas Czerner 	return 0;
65147edfd85bSLukas Czerner }
65157edfd85bSLukas Czerner 
6516689c958cSLi Xi #ifdef CONFIG_QUOTA
6517689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
6518689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
6519689c958cSLi Xi {
6520689c958cSLi Xi 	struct kqid qid;
6521689c958cSLi Xi 	struct dquot *dquot;
6522689c958cSLi Xi 	u64 limit;
6523689c958cSLi Xi 	u64 curblock;
6524689c958cSLi Xi 
6525689c958cSLi Xi 	qid = make_kqid_projid(projid);
6526689c958cSLi Xi 	dquot = dqget(sb, qid);
6527689c958cSLi Xi 	if (IS_ERR(dquot))
6528689c958cSLi Xi 		return PTR_ERR(dquot);
65297b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
6530689c958cSLi Xi 
6531a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6532a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
653357c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
653457c32ea4SChengguang Xu 
6535689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
6536f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
6537f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6538689c958cSLi Xi 		buf->f_blocks = limit;
6539689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
6540689c958cSLi Xi 			(buf->f_blocks > curblock) ?
6541689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
6542689c958cSLi Xi 	}
6543689c958cSLi Xi 
6544a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6545a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
6546689c958cSLi Xi 	if (limit && buf->f_files > limit) {
6547689c958cSLi Xi 		buf->f_files = limit;
6548689c958cSLi Xi 		buf->f_ffree =
6549689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6550689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6551689c958cSLi Xi 	}
6552689c958cSLi Xi 
65537b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
6554689c958cSLi Xi 	dqput(dquot);
6555689c958cSLi Xi 	return 0;
6556689c958cSLi Xi }
6557689c958cSLi Xi #endif
6558689c958cSLi Xi 
6559617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6560ac27a0ecSDave Kleikamp {
6561ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
6562617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6563617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
656427dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
6565d02a9391SKazuya Mio 	s64 bfree;
656627dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6567ac27a0ecSDave Kleikamp 
6568952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
6569952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
6570ac27a0ecSDave Kleikamp 
6571617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
6572ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
6573b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
657457042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
657557042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6576d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
657757042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
657827dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
657927dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
658027dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6581ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
6582ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
658352d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6584617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
65859591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(es->s_uuid);
65860b8e58a1SAndreas Dilger 
6587689c958cSLi Xi #ifdef CONFIG_QUOTA
6588689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6589689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6590689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6591689c958cSLi Xi #endif
6592ac27a0ecSDave Kleikamp 	return 0;
6593ac27a0ecSDave Kleikamp }
6594ac27a0ecSDave Kleikamp 
6595ac27a0ecSDave Kleikamp 
6596ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6597ac27a0ecSDave Kleikamp 
6598bc8230eeSJan Kara /*
6599bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
6600bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
6601bc8230eeSJan Kara  */
6602ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
6603ac27a0ecSDave Kleikamp {
66044c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6605ac27a0ecSDave Kleikamp }
6606ac27a0ecSDave Kleikamp 
6607617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
6608ac27a0ecSDave Kleikamp {
6609ac27a0ecSDave Kleikamp 	int ret, err;
6610ac27a0ecSDave Kleikamp 	handle_t *handle;
6611ac27a0ecSDave Kleikamp 	struct inode *inode;
6612ac27a0ecSDave Kleikamp 
6613ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
66149924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6615617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6616ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6617ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6618ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
6619617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6620ac27a0ecSDave Kleikamp 	if (!ret)
6621ac27a0ecSDave Kleikamp 		ret = err;
6622ac27a0ecSDave Kleikamp 	return ret;
6623ac27a0ecSDave Kleikamp }
6624ac27a0ecSDave Kleikamp 
6625617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
6626ac27a0ecSDave Kleikamp {
6627ac27a0ecSDave Kleikamp 	int ret, err;
6628ac27a0ecSDave Kleikamp 	handle_t *handle;
6629ac27a0ecSDave Kleikamp 
66309924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6631617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6632ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6633ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6634ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
6635617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6636ac27a0ecSDave Kleikamp 	if (!ret)
6637ac27a0ecSDave Kleikamp 		ret = err;
6638ac27a0ecSDave Kleikamp 	return ret;
6639ac27a0ecSDave Kleikamp }
6640ac27a0ecSDave Kleikamp 
6641617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
6642ac27a0ecSDave Kleikamp {
6643ac27a0ecSDave Kleikamp 	int ret, err;
6644ac27a0ecSDave Kleikamp 	handle_t *handle;
6645ac27a0ecSDave Kleikamp 
66469924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6647617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
66489c3013e9SJan Kara 	if (IS_ERR(handle)) {
66499c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
66509c3013e9SJan Kara 		dquot_release(dquot);
6651ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
66529c3013e9SJan Kara 	}
6653ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
6654617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6655ac27a0ecSDave Kleikamp 	if (!ret)
6656ac27a0ecSDave Kleikamp 		ret = err;
6657ac27a0ecSDave Kleikamp 	return ret;
6658ac27a0ecSDave Kleikamp }
6659ac27a0ecSDave Kleikamp 
6660617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
6661ac27a0ecSDave Kleikamp {
6662262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
6663262b4662SJan Kara 
6664f177ee08SRoman Anufriev 	if (ext4_is_quota_journalled(sb)) {
6665ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
6666617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
6667ac27a0ecSDave Kleikamp 	} else {
6668ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
6669ac27a0ecSDave Kleikamp 	}
6670ac27a0ecSDave Kleikamp }
6671ac27a0ecSDave Kleikamp 
6672617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
6673ac27a0ecSDave Kleikamp {
6674ac27a0ecSDave Kleikamp 	int ret, err;
6675ac27a0ecSDave Kleikamp 	handle_t *handle;
6676ac27a0ecSDave Kleikamp 
6677ac27a0ecSDave Kleikamp 	/* Data block + inode block */
66782b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6679ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6680ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6681ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
6682617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6683ac27a0ecSDave Kleikamp 	if (!ret)
6684ac27a0ecSDave Kleikamp 		ret = err;
6685ac27a0ecSDave Kleikamp 	return ret;
6686ac27a0ecSDave Kleikamp }
6687ac27a0ecSDave Kleikamp 
6688daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6689daf647d2STheodore Ts'o {
6690daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
6691daf647d2STheodore Ts'o 
6692daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
6693daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
6694daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
6695daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
6696daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
6697daf647d2STheodore Ts'o 	 */
6698daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6699daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6700daf647d2STheodore Ts'o }
6701daf647d2STheodore Ts'o 
6702ac27a0ecSDave Kleikamp /*
6703ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
6704ac27a0ecSDave Kleikamp  */
6705617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
67068c54ca9cSAl Viro 			 const struct path *path)
6707ac27a0ecSDave Kleikamp {
6708ac27a0ecSDave Kleikamp 	int err;
6709ac27a0ecSDave Kleikamp 
6710ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
6711ac27a0ecSDave Kleikamp 		return -EINVAL;
67120623543bSJan Kara 
6713ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
6714d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
6715ac27a0ecSDave Kleikamp 		return -EXDEV;
6716e0770e91SJan Kara 
6717e0770e91SJan Kara 	/* Quota already enabled for this file? */
6718e0770e91SJan Kara 	if (IS_NOQUOTA(d_inode(path->dentry)))
6719e0770e91SJan Kara 		return -EBUSY;
6720e0770e91SJan Kara 
67210623543bSJan Kara 	/* Journaling quota? */
67220623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
67232b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
6724f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
6725b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
6726b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
6727b31e1552SEric Sandeen 				"Journaled quota will not work");
672891389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
672991389240SJan Kara 	} else {
673091389240SJan Kara 		/*
673191389240SJan Kara 		 * Clear the flag just in case mount options changed since
673291389240SJan Kara 		 * last time.
673391389240SJan Kara 		 */
673491389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
67350623543bSJan Kara 	}
67360623543bSJan Kara 
67370623543bSJan Kara 	/*
67380623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
67390623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
67400623543bSJan Kara 	 */
67410390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
67422b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
67430623543bSJan Kara 		/*
67440623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
67450623543bSJan Kara 		 * otherwise be livelocked...
67460623543bSJan Kara 		 */
67470623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
674801d5d965SLeah Rumancik 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal, 0);
67490623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6750f00c9e44SJan Kara 		if (err)
67517ffe1ea8SHidehiro Kawai 			return err;
67527ffe1ea8SHidehiro Kawai 	}
6753957153fcSJan Kara 
6754daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6755daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
675615fc69bbSJan Kara 	if (!err) {
6757957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6758957153fcSJan Kara 		handle_t *handle;
6759957153fcSJan Kara 
676061a92987SJan Kara 		/*
676161a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
676261a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
676361a92987SJan Kara 		 * are already enabled and this is not a hard failure.
676461a92987SJan Kara 		 */
6765957153fcSJan Kara 		inode_lock(inode);
6766957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6767957153fcSJan Kara 		if (IS_ERR(handle))
6768957153fcSJan Kara 			goto unlock_inode;
6769957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6770957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6771957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
67724209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6773957153fcSJan Kara 		ext4_journal_stop(handle);
6774957153fcSJan Kara 	unlock_inode:
6775957153fcSJan Kara 		inode_unlock(inode);
677615fc69bbSJan Kara 		if (err)
677715fc69bbSJan Kara 			dquot_quota_off(sb, type);
6778957153fcSJan Kara 	}
677915fc69bbSJan Kara 	if (err)
678015fc69bbSJan Kara 		lockdep_set_quota_inode(path->dentry->d_inode,
678115fc69bbSJan Kara 					     I_DATA_SEM_NORMAL);
6782daf647d2STheodore Ts'o 	return err;
6783ac27a0ecSDave Kleikamp }
6784ac27a0ecSDave Kleikamp 
67857c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
67867c319d32SAditya Kali 			     unsigned int flags)
67877c319d32SAditya Kali {
67887c319d32SAditya Kali 	int err;
67897c319d32SAditya Kali 	struct inode *qf_inode;
6790a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
67917c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6792689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6793689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
67947c319d32SAditya Kali 	};
67957c319d32SAditya Kali 
6796e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
67977c319d32SAditya Kali 
67987c319d32SAditya Kali 	if (!qf_inums[type])
67997c319d32SAditya Kali 		return -EPERM;
68007c319d32SAditya Kali 
68018a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
68027c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
68037c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
68047c319d32SAditya Kali 		return PTR_ERR(qf_inode);
68057c319d32SAditya Kali 	}
68067c319d32SAditya Kali 
6807bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6808bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6809daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
68107212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6811daf647d2STheodore Ts'o 	if (err)
6812daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
681361157b24SPan Bian 	iput(qf_inode);
68147c319d32SAditya Kali 
68157c319d32SAditya Kali 	return err;
68167c319d32SAditya Kali }
68177c319d32SAditya Kali 
68187c319d32SAditya Kali /* Enable usage tracking for all quota types. */
681925c6d98fSJan Kara int ext4_enable_quotas(struct super_block *sb)
68207c319d32SAditya Kali {
68217c319d32SAditya Kali 	int type, err = 0;
6822a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
68237c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6824689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6825689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
68267c319d32SAditya Kali 	};
682749da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
682849da9392SJan Kara 		test_opt(sb, USRQUOTA),
682949da9392SJan Kara 		test_opt(sb, GRPQUOTA),
683049da9392SJan Kara 		test_opt(sb, PRJQUOTA),
683149da9392SJan Kara 	};
68327c319d32SAditya Kali 
683391389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6834a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
68357c319d32SAditya Kali 		if (qf_inums[type]) {
68367c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
683749da9392SJan Kara 				DQUOT_USAGE_ENABLED |
683849da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
68397c319d32SAditya Kali 			if (err) {
68407c319d32SAditya Kali 				ext4_warning(sb,
684172ba7450STheodore Ts'o 					"Failed to enable quota tracking "
684272ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
684372ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
68444013d47aSJan Kara 				for (type--; type >= 0; type--) {
68454013d47aSJan Kara 					struct inode *inode;
68464013d47aSJan Kara 
68474013d47aSJan Kara 					inode = sb_dqopt(sb)->files[type];
68484013d47aSJan Kara 					if (inode)
68494013d47aSJan Kara 						inode = igrab(inode);
68507f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
68514013d47aSJan Kara 					if (inode) {
68524013d47aSJan Kara 						lockdep_set_quota_inode(inode,
68534013d47aSJan Kara 							I_DATA_SEM_NORMAL);
68544013d47aSJan Kara 						iput(inode);
68554013d47aSJan Kara 					}
68564013d47aSJan Kara 				}
68577f144fd0SJunichi Uekawa 
68587c319d32SAditya Kali 				return err;
68597c319d32SAditya Kali 			}
68607c319d32SAditya Kali 		}
68617c319d32SAditya Kali 	}
68627c319d32SAditya Kali 	return 0;
68637c319d32SAditya Kali }
68647c319d32SAditya Kali 
6865ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6866ca0e05e4SDmitry Monakhov {
686721f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
686821f97697SJan Kara 	handle_t *handle;
6869957153fcSJan Kara 	int err;
687021f97697SJan Kara 
687187009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
687287009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
687387009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6874ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6875ca0e05e4SDmitry Monakhov 
6876957153fcSJan Kara 	if (!inode || !igrab(inode))
68770b268590SAmir Goldstein 		goto out;
68780b268590SAmir Goldstein 
6879957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6880964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6881957153fcSJan Kara 		goto out_put;
6882957153fcSJan Kara 
6883957153fcSJan Kara 	inode_lock(inode);
688461a92987SJan Kara 	/*
688561a92987SJan Kara 	 * Update modification times of quota files when userspace can
688661a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
688761a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
688861a92987SJan Kara 	 */
68899924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
68904209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
68914209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6892957153fcSJan Kara 		goto out_unlock;
68934209ae12SHarshad Shirwadkar 	}
6894957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6895957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6896eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
68974209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
689821f97697SJan Kara 	ext4_journal_stop(handle);
6899957153fcSJan Kara out_unlock:
6900957153fcSJan Kara 	inode_unlock(inode);
6901957153fcSJan Kara out_put:
6902964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6903957153fcSJan Kara 	iput(inode);
6904957153fcSJan Kara 	return err;
690521f97697SJan Kara out:
6906ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6907ca0e05e4SDmitry Monakhov }
6908ca0e05e4SDmitry Monakhov 
6909ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6910ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6911ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6912ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6913617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6914ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6915ac27a0ecSDave Kleikamp {
6916ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6917725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6918ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6919ac27a0ecSDave Kleikamp 	int tocopy;
6920ac27a0ecSDave Kleikamp 	size_t toread;
6921ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6922ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6923ac27a0ecSDave Kleikamp 
6924ac27a0ecSDave Kleikamp 	if (off > i_size)
6925ac27a0ecSDave Kleikamp 		return 0;
6926ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6927ac27a0ecSDave Kleikamp 		len = i_size-off;
6928ac27a0ecSDave Kleikamp 	toread = len;
6929ac27a0ecSDave Kleikamp 	while (toread > 0) {
6930ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6931ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
69321c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
69331c215028STheodore Ts'o 		if (IS_ERR(bh))
69341c215028STheodore Ts'o 			return PTR_ERR(bh);
6935ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6936ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6937ac27a0ecSDave Kleikamp 		else
6938ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6939ac27a0ecSDave Kleikamp 		brelse(bh);
6940ac27a0ecSDave Kleikamp 		offset = 0;
6941ac27a0ecSDave Kleikamp 		toread -= tocopy;
6942ac27a0ecSDave Kleikamp 		data += tocopy;
6943ac27a0ecSDave Kleikamp 		blk++;
6944ac27a0ecSDave Kleikamp 	}
6945ac27a0ecSDave Kleikamp 	return len;
6946ac27a0ecSDave Kleikamp }
6947ac27a0ecSDave Kleikamp 
6948ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6949ac27a0ecSDave Kleikamp  * enough credits) */
6950617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6951ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6952ac27a0ecSDave Kleikamp {
6953ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6954725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
69554209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6956c5e298aeSTheodore Ts'o 	int retries = 0;
6957ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6958ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6959ac27a0ecSDave Kleikamp 
6960380a0091SYe Bin 	if (!handle) {
6961b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6962b31e1552SEric Sandeen 			" cancelled because transaction is not started",
69639c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
69649c3013e9SJan Kara 		return -EIO;
69659c3013e9SJan Kara 	}
696667eeb568SDmitry Monakhov 	/*
696767eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
696867eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
696967eeb568SDmitry Monakhov 	 */
697067eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
697167eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
697267eeb568SDmitry Monakhov 			" cancelled because not block aligned",
697367eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
697467eeb568SDmitry Monakhov 		return -EIO;
697567eeb568SDmitry Monakhov 	}
697667eeb568SDmitry Monakhov 
6977c5e298aeSTheodore Ts'o 	do {
6978c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6979c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6980c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
698145586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6982c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
69831c215028STheodore Ts'o 	if (IS_ERR(bh))
69841c215028STheodore Ts'o 		return PTR_ERR(bh);
6985ac27a0ecSDave Kleikamp 	if (!bh)
6986ac27a0ecSDave Kleikamp 		goto out;
69875d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6988188c299eSJan Kara 	err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
6989ac27a0ecSDave Kleikamp 	if (err) {
6990ac27a0ecSDave Kleikamp 		brelse(bh);
69911c215028STheodore Ts'o 		return err;
6992ac27a0ecSDave Kleikamp 	}
6993ac27a0ecSDave Kleikamp 	lock_buffer(bh);
699467eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6995ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6996ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
69970390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6998ac27a0ecSDave Kleikamp 	brelse(bh);
6999ac27a0ecSDave Kleikamp out:
700067eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
700167eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
7002617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
70034209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
70044209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
70054209ae12SHarshad Shirwadkar 			err = err2;
700621f97697SJan Kara 	}
70074209ae12SHarshad Shirwadkar 	return err ? err : len;
7008ac27a0ecSDave Kleikamp }
7009ac27a0ecSDave Kleikamp #endif
7010ac27a0ecSDave Kleikamp 
7011c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
701224b58424STheodore Ts'o static inline void register_as_ext2(void)
701324b58424STheodore Ts'o {
701424b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
701524b58424STheodore Ts'o 	if (err)
701624b58424STheodore Ts'o 		printk(KERN_WARNING
701724b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
701824b58424STheodore Ts'o }
701924b58424STheodore Ts'o 
702024b58424STheodore Ts'o static inline void unregister_as_ext2(void)
702124b58424STheodore Ts'o {
702224b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
702324b58424STheodore Ts'o }
70242035e776STheodore Ts'o 
70252035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
70262035e776STheodore Ts'o {
7027e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
70282035e776STheodore Ts'o 		return 0;
7029bc98a42cSDavid Howells 	if (sb_rdonly(sb))
70302035e776STheodore Ts'o 		return 1;
7031e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
70322035e776STheodore Ts'o 		return 0;
70332035e776STheodore Ts'o 	return 1;
70342035e776STheodore Ts'o }
703524b58424STheodore Ts'o #else
703624b58424STheodore Ts'o static inline void register_as_ext2(void) { }
703724b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
70382035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
703924b58424STheodore Ts'o #endif
704024b58424STheodore Ts'o 
704124b58424STheodore Ts'o static inline void register_as_ext3(void)
704224b58424STheodore Ts'o {
704324b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
704424b58424STheodore Ts'o 	if (err)
704524b58424STheodore Ts'o 		printk(KERN_WARNING
704624b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
704724b58424STheodore Ts'o }
704824b58424STheodore Ts'o 
704924b58424STheodore Ts'o static inline void unregister_as_ext3(void)
705024b58424STheodore Ts'o {
705124b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
705224b58424STheodore Ts'o }
70532035e776STheodore Ts'o 
70542035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
70552035e776STheodore Ts'o {
7056e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
70572035e776STheodore Ts'o 		return 0;
7058e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
70592035e776STheodore Ts'o 		return 0;
7060bc98a42cSDavid Howells 	if (sb_rdonly(sb))
70612035e776STheodore Ts'o 		return 1;
7062e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
70632035e776STheodore Ts'o 		return 0;
70642035e776STheodore Ts'o 	return 1;
70652035e776STheodore Ts'o }
706624b58424STheodore Ts'o 
706703010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
7068ac27a0ecSDave Kleikamp 	.owner			= THIS_MODULE,
706903010a33STheodore Ts'o 	.name			= "ext4",
7070cebe85d5SLukas Czerner 	.init_fs_context	= ext4_init_fs_context,
7071cebe85d5SLukas Czerner 	.parameters		= ext4_param_specs,
7072ac27a0ecSDave Kleikamp 	.kill_sb		= kill_block_super,
707314f3db55SChristian Brauner 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
7074ac27a0ecSDave Kleikamp };
70757f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
7076ac27a0ecSDave Kleikamp 
7077e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
7078e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
7079e9e3bcecSEric Sandeen 
70805dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
7081ac27a0ecSDave Kleikamp {
7082e9e3bcecSEric Sandeen 	int i, err;
7083c9de560dSAlex Tomas 
7084e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
708507c0c5d8SAl Viro 	ext4_li_info = NULL;
708607c0c5d8SAl Viro 
70879a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
708812e9b892SDmitry Monakhov 	ext4_check_flag_values();
7089e9e3bcecSEric Sandeen 
7090e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
7091e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
7092e9e3bcecSEric Sandeen 
709351865fdaSZheng Liu 	err = ext4_init_es();
70946fd058f7STheodore Ts'o 	if (err)
70956fd058f7STheodore Ts'o 		return err;
709651865fdaSZheng Liu 
70971dc0aa46SEric Whitney 	err = ext4_init_pending();
70981dc0aa46SEric Whitney 	if (err)
709922cfe4b4SEric Biggers 		goto out7;
710022cfe4b4SEric Biggers 
710122cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
710222cfe4b4SEric Biggers 	if (err)
71031dc0aa46SEric Whitney 		goto out6;
71041dc0aa46SEric Whitney 
710551865fdaSZheng Liu 	err = ext4_init_pageio();
710651865fdaSZheng Liu 	if (err)
7107b5799018STheodore Ts'o 		goto out5;
710851865fdaSZheng Liu 
71095dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
7110bd2d0210STheodore Ts'o 	if (err)
7111b5799018STheodore Ts'o 		goto out4;
7112857ac889SLukas Czerner 
7113b5799018STheodore Ts'o 	err = ext4_init_sysfs();
7114dd68314cSTheodore Ts'o 	if (err)
7115b5799018STheodore Ts'o 		goto out3;
7116857ac889SLukas Czerner 
71175dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
7118ac27a0ecSDave Kleikamp 	if (err)
7119c9de560dSAlex Tomas 		goto out2;
7120ac27a0ecSDave Kleikamp 	err = init_inodecache();
7121ac27a0ecSDave Kleikamp 	if (err)
7122ac27a0ecSDave Kleikamp 		goto out1;
7123aa75f4d3SHarshad Shirwadkar 
7124aa75f4d3SHarshad Shirwadkar 	err = ext4_fc_init_dentry_cache();
7125aa75f4d3SHarshad Shirwadkar 	if (err)
7126aa75f4d3SHarshad Shirwadkar 		goto out05;
7127aa75f4d3SHarshad Shirwadkar 
712824b58424STheodore Ts'o 	register_as_ext3();
71292035e776STheodore Ts'o 	register_as_ext2();
713003010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
7131ac27a0ecSDave Kleikamp 	if (err)
7132ac27a0ecSDave Kleikamp 		goto out;
7133bfff6873SLukas Czerner 
7134ac27a0ecSDave Kleikamp 	return 0;
7135ac27a0ecSDave Kleikamp out:
713624b58424STheodore Ts'o 	unregister_as_ext2();
713724b58424STheodore Ts'o 	unregister_as_ext3();
7138ab047d51SSebastian Andrzej Siewior 	ext4_fc_destroy_dentry_cache();
7139aa75f4d3SHarshad Shirwadkar out05:
7140ac27a0ecSDave Kleikamp 	destroy_inodecache();
7141ac27a0ecSDave Kleikamp out1:
71425dabfc78STheodore Ts'o 	ext4_exit_mballoc();
71439c191f70ST Makphaibulchoke out2:
7144b5799018STheodore Ts'o 	ext4_exit_sysfs();
7145b5799018STheodore Ts'o out3:
7146dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
7147b5799018STheodore Ts'o out4:
71485dabfc78STheodore Ts'o 	ext4_exit_pageio();
7149b5799018STheodore Ts'o out5:
715022cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
71511dc0aa46SEric Whitney out6:
715222cfe4b4SEric Biggers 	ext4_exit_pending();
715322cfe4b4SEric Biggers out7:
715451865fdaSZheng Liu 	ext4_exit_es();
715551865fdaSZheng Liu 
7156ac27a0ecSDave Kleikamp 	return err;
7157ac27a0ecSDave Kleikamp }
7158ac27a0ecSDave Kleikamp 
71595dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
7160ac27a0ecSDave Kleikamp {
7161bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
716224b58424STheodore Ts'o 	unregister_as_ext2();
716324b58424STheodore Ts'o 	unregister_as_ext3();
716403010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
7165ab047d51SSebastian Andrzej Siewior 	ext4_fc_destroy_dentry_cache();
7166ac27a0ecSDave Kleikamp 	destroy_inodecache();
71675dabfc78STheodore Ts'o 	ext4_exit_mballoc();
7168b5799018STheodore Ts'o 	ext4_exit_sysfs();
71695dabfc78STheodore Ts'o 	ext4_exit_system_zone();
71705dabfc78STheodore Ts'o 	ext4_exit_pageio();
717122cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
7172dd12ed14SEric Sandeen 	ext4_exit_es();
71731dc0aa46SEric Whitney 	ext4_exit_pending();
7174ac27a0ecSDave Kleikamp }
7175ac27a0ecSDave Kleikamp 
7176ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
717783982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
7178ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
71797ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
71805dabfc78STheodore Ts'o module_init(ext4_init_fs)
71815dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
7182