xref: /linux/fs/ext4/super.c (revision 9591c3a34f7722bd77f42c98d76fd5a5bad465f0)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/super.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  from
11ac27a0ecSDave Kleikamp  *
12ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
13ac27a0ecSDave Kleikamp  *
14ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
15ac27a0ecSDave Kleikamp  *
16ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
17ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
18ac27a0ecSDave Kleikamp  */
19ac27a0ecSDave Kleikamp 
20ac27a0ecSDave Kleikamp #include <linux/module.h>
21ac27a0ecSDave Kleikamp #include <linux/string.h>
22ac27a0ecSDave Kleikamp #include <linux/fs.h>
23ac27a0ecSDave Kleikamp #include <linux/time.h>
24c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
2866114cadSTejun Heo #include <linux/backing-dev.h>
29ac27a0ecSDave Kleikamp #include <linux/parser.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.h>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ef510424SDan Williams #include <linux/dax.h>
427abc52c2SDan Magenheimer #include <linux/cleancache.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
44ee73f9a5SJeff Layton #include <linux/iversion.h>
45c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h>
46c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
47bfff6873SLukas Czerner #include <linux/kthread.h>
48bfff6873SLukas Czerner #include <linux/freezer.h>
49bfff6873SLukas Czerner 
503dcf5451SChristoph Hellwig #include "ext4.h"
514a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
523dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
53ac27a0ecSDave Kleikamp #include "xattr.h"
54ac27a0ecSDave Kleikamp #include "acl.h"
553661d286STheodore Ts'o #include "mballoc.h"
560c9ec4beSDarrick J. Wong #include "fsmap.h"
57ac27a0ecSDave Kleikamp 
589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
599bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
609bffad1eSTheodore Ts'o 
610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
6259ebc7fdSZheng Yongjun static DEFINE_MUTEX(ext4_li_mtx);
63e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
649f6200bbSTheodore Ts'o 
65617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
66ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
672adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
682d01ddc8SJan Kara static void ext4_update_super(struct super_block *sb);
694392fbc4SJan Kara static int ext4_commit_super(struct super_block *sb);
7011215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
71617ba13bSMingming Cao 					struct ext4_super_block *es);
7211215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
73617ba13bSMingming Cao 				  struct ext4_super_block *es);
74617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
75617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
76617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
77c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
78c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
79152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
80152a0836SAl Viro 		       const char *dev_name, void *data);
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);
83d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
84bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
85bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
868f1f7453SEric Sandeen static void ext4_clear_request_list(void);
87c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
88c6cb7e77SEric Whitney 					    unsigned int journal_inum);
89ac27a0ecSDave Kleikamp 
90e74031fdSJan Kara /*
91e74031fdSJan Kara  * Lock ordering
92e74031fdSJan Kara  *
93e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
94e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
95e74031fdSJan Kara  *
96e74031fdSJan Kara  * page fault path:
97c1e8d7c6SMichel Lespinasse  * mmap_lock -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
98e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
99e74031fdSJan Kara  *
100e74031fdSJan Kara  * buffered write path:
101c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
102e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
103e74031fdSJan Kara  *   i_data_sem (rw)
104e74031fdSJan Kara  *
105e74031fdSJan Kara  * truncate:
1061d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock
1071d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start ->
1081d39834fSNikolay Borisov  *   i_data_sem (rw)
109e74031fdSJan Kara  *
110e74031fdSJan Kara  * direct IO:
111c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
1121d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
113e74031fdSJan Kara  *
114e74031fdSJan Kara  * writepages:
115e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
116e74031fdSJan Kara  */
117e74031fdSJan Kara 
118c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1192035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1202035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1212035e776STheodore Ts'o 	.name		= "ext2",
1222035e776STheodore Ts'o 	.mount		= ext4_mount,
1232035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1242035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1252035e776STheodore Ts'o };
1267f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
127fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1282035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1292035e776STheodore Ts'o #else
1302035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1312035e776STheodore Ts'o #endif
1322035e776STheodore Ts'o 
1332035e776STheodore Ts'o 
134ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
135ba69f9abSJan Kara 	.owner		= THIS_MODULE,
136ba69f9abSJan Kara 	.name		= "ext3",
137152a0836SAl Viro 	.mount		= ext4_mount,
138ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
139ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
140ba69f9abSJan Kara };
1417f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
142fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
143ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
144bd81d8eeSLaurent Vivier 
145fa491b14Szhangyi (F) 
146fa491b14Szhangyi (F) static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
147fa491b14Szhangyi (F) 				  bh_end_io_t *end_io)
148fa491b14Szhangyi (F) {
149fb265c9cSTheodore Ts'o 	/*
150fa491b14Szhangyi (F) 	 * buffer's verified bit is no longer valid after reading from
151fa491b14Szhangyi (F) 	 * disk again due to write out error, clear it to make sure we
152fa491b14Szhangyi (F) 	 * recheck the buffer contents.
153fa491b14Szhangyi (F) 	 */
154fa491b14Szhangyi (F) 	clear_buffer_verified(bh);
155fa491b14Szhangyi (F) 
156fa491b14Szhangyi (F) 	bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
157fa491b14Szhangyi (F) 	get_bh(bh);
158fa491b14Szhangyi (F) 	submit_bh(REQ_OP_READ, op_flags, bh);
159fa491b14Szhangyi (F) }
160fa491b14Szhangyi (F) 
161fa491b14Szhangyi (F) void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
162fa491b14Szhangyi (F) 			 bh_end_io_t *end_io)
163fa491b14Szhangyi (F) {
164fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
165fa491b14Szhangyi (F) 
166fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
167fa491b14Szhangyi (F) 		unlock_buffer(bh);
168fa491b14Szhangyi (F) 		return;
169fa491b14Szhangyi (F) 	}
170fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
171fa491b14Szhangyi (F) }
172fa491b14Szhangyi (F) 
173fa491b14Szhangyi (F) int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
174fa491b14Szhangyi (F) {
175fa491b14Szhangyi (F) 	BUG_ON(!buffer_locked(bh));
176fa491b14Szhangyi (F) 
177fa491b14Szhangyi (F) 	if (ext4_buffer_uptodate(bh)) {
178fa491b14Szhangyi (F) 		unlock_buffer(bh);
179fa491b14Szhangyi (F) 		return 0;
180fa491b14Szhangyi (F) 	}
181fa491b14Szhangyi (F) 
182fa491b14Szhangyi (F) 	__ext4_read_bh(bh, op_flags, end_io);
183fa491b14Szhangyi (F) 
184fa491b14Szhangyi (F) 	wait_on_buffer(bh);
185fa491b14Szhangyi (F) 	if (buffer_uptodate(bh))
186fa491b14Szhangyi (F) 		return 0;
187fa491b14Szhangyi (F) 	return -EIO;
188fa491b14Szhangyi (F) }
189fa491b14Szhangyi (F) 
190fa491b14Szhangyi (F) int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
191fa491b14Szhangyi (F) {
192fa491b14Szhangyi (F) 	if (trylock_buffer(bh)) {
193fa491b14Szhangyi (F) 		if (wait)
194fa491b14Szhangyi (F) 			return ext4_read_bh(bh, op_flags, NULL);
195fa491b14Szhangyi (F) 		ext4_read_bh_nowait(bh, op_flags, NULL);
196fa491b14Szhangyi (F) 		return 0;
197fa491b14Szhangyi (F) 	}
198fa491b14Szhangyi (F) 	if (wait) {
199fa491b14Szhangyi (F) 		wait_on_buffer(bh);
200fa491b14Szhangyi (F) 		if (buffer_uptodate(bh))
201fa491b14Szhangyi (F) 			return 0;
202fa491b14Szhangyi (F) 		return -EIO;
203fa491b14Szhangyi (F) 	}
204fa491b14Szhangyi (F) 	return 0;
205fa491b14Szhangyi (F) }
206fa491b14Szhangyi (F) 
207fb265c9cSTheodore Ts'o /*
2088394a6abSzhangyi (F)  * This works like __bread_gfp() except it uses ERR_PTR for error
209fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
210fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
211fb265c9cSTheodore Ts'o  * return.
212fb265c9cSTheodore Ts'o  */
2138394a6abSzhangyi (F) static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
2148394a6abSzhangyi (F) 					       sector_t block, int op_flags,
2158394a6abSzhangyi (F) 					       gfp_t gfp)
216fb265c9cSTheodore Ts'o {
2172d069c08Szhangyi (F) 	struct buffer_head *bh;
2182d069c08Szhangyi (F) 	int ret;
219fb265c9cSTheodore Ts'o 
2208394a6abSzhangyi (F) 	bh = sb_getblk_gfp(sb, block, gfp);
221fb265c9cSTheodore Ts'o 	if (bh == NULL)
222fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
223cf2834a5STheodore Ts'o 	if (ext4_buffer_uptodate(bh))
224fb265c9cSTheodore Ts'o 		return bh;
2252d069c08Szhangyi (F) 
2262d069c08Szhangyi (F) 	ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
2272d069c08Szhangyi (F) 	if (ret) {
228fb265c9cSTheodore Ts'o 		put_bh(bh);
2292d069c08Szhangyi (F) 		return ERR_PTR(ret);
2302d069c08Szhangyi (F) 	}
2312d069c08Szhangyi (F) 	return bh;
232fb265c9cSTheodore Ts'o }
233fb265c9cSTheodore Ts'o 
2348394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
2358394a6abSzhangyi (F) 				   int op_flags)
2368394a6abSzhangyi (F) {
2378394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
2388394a6abSzhangyi (F) }
2398394a6abSzhangyi (F) 
2408394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
2418394a6abSzhangyi (F) 					    sector_t block)
2428394a6abSzhangyi (F) {
2438394a6abSzhangyi (F) 	return __ext4_sb_bread_gfp(sb, block, 0, 0);
2448394a6abSzhangyi (F) }
2458394a6abSzhangyi (F) 
2465df1d412Szhangyi (F) void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
2475df1d412Szhangyi (F) {
2485df1d412Szhangyi (F) 	struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
2495df1d412Szhangyi (F) 
2505df1d412Szhangyi (F) 	if (likely(bh)) {
2515df1d412Szhangyi (F) 		ext4_read_bh_lock(bh, REQ_RAHEAD, false);
2525df1d412Szhangyi (F) 		brelse(bh);
2535df1d412Szhangyi (F) 	}
254c197855eSStephen Hemminger }
255a9c47317SDarrick J. Wong 
256a9c47317SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
2579aa5d32bSDmitry Monakhov 				 struct ext4_super_block *es)
258a9c47317SDarrick J. Wong {
259a9c47317SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
260a9c47317SDarrick J. Wong 		return 1;
261a9c47317SDarrick J. Wong 
262a9c47317SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
26306db49e6STheodore Ts'o }
264a9c47317SDarrick J. Wong 
26506db49e6STheodore Ts'o static __le32 ext4_superblock_csum(struct super_block *sb,
26606db49e6STheodore Ts'o 				   struct ext4_super_block *es)
2679aa5d32bSDmitry Monakhov {
268a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
269a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
270a9c47317SDarrick J. Wong 	__u32 csum;
271a9c47317SDarrick J. Wong 
272a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
2739933fc0aSTheodore Ts'o 
2749933fc0aSTheodore Ts'o 	return cpu_to_le32(csum);
2759933fc0aSTheodore Ts'o }
2769933fc0aSTheodore Ts'o 
2778be04b93SJoe Perches static int ext4_superblock_csum_verify(struct super_block *sb,
2789933fc0aSTheodore Ts'o 				       struct ext4_super_block *es)
2799933fc0aSTheodore Ts'o {
2809933fc0aSTheodore Ts'o 	if (!ext4_has_metadata_csum(sb))
2819933fc0aSTheodore Ts'o 		return 1;
2829933fc0aSTheodore Ts'o 
2839933fc0aSTheodore Ts'o 	return es->s_checksum == ext4_superblock_csum(sb, es);
2849933fc0aSTheodore Ts'o }
2859933fc0aSTheodore Ts'o 
2869933fc0aSTheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
2878be04b93SJoe Perches {
2889933fc0aSTheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
2899933fc0aSTheodore Ts'o 
2909933fc0aSTheodore Ts'o 	if (!ext4_has_metadata_csum(sb))
2919933fc0aSTheodore Ts'o 		return;
2929933fc0aSTheodore Ts'o 
2938fadc143SAlexandre Ratchov 	es->s_checksum = ext4_superblock_csum(sb, es);
2948fadc143SAlexandre Ratchov }
295bd81d8eeSLaurent Vivier 
296bd81d8eeSLaurent Vivier ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
297bd81d8eeSLaurent Vivier 			       struct ext4_group_desc *bg)
298bd81d8eeSLaurent Vivier {
2993a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
3008fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3018fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
302bd81d8eeSLaurent Vivier }
303bd81d8eeSLaurent Vivier 
3048fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
3058fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
306bd81d8eeSLaurent Vivier {
3075272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
3088fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3098fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
310bd81d8eeSLaurent Vivier }
311bd81d8eeSLaurent Vivier 
3128fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
3138fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
314bd81d8eeSLaurent Vivier {
3155272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
3168fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
3178fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
318bd81d8eeSLaurent Vivier }
319bd81d8eeSLaurent Vivier 
320021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
321560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
322560671a0SAneesh Kumar K.V {
323560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
324560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
325560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
326560671a0SAneesh Kumar K.V }
327560671a0SAneesh Kumar K.V 
328560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
329560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
330560671a0SAneesh Kumar K.V {
331560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
332560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
333560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
334560671a0SAneesh Kumar K.V }
335560671a0SAneesh Kumar K.V 
336560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
337560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
338560671a0SAneesh Kumar K.V {
339560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
340560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
341560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
342560671a0SAneesh Kumar K.V }
343560671a0SAneesh Kumar K.V 
344560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
345560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
346560671a0SAneesh Kumar K.V {
347560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
348560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
349560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
350560671a0SAneesh Kumar K.V }
351560671a0SAneesh Kumar K.V 
3528fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
3538fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
354bd81d8eeSLaurent Vivier {
3553a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
3568fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3578fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
358bd81d8eeSLaurent Vivier }
359bd81d8eeSLaurent Vivier 
3608fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
3618fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
362bd81d8eeSLaurent Vivier {
3635272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
3648fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3658fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
366bd81d8eeSLaurent Vivier }
367bd81d8eeSLaurent Vivier 
3688fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
3698fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
370bd81d8eeSLaurent Vivier {
3715272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
3728fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3738fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
374bd81d8eeSLaurent Vivier }
375bd81d8eeSLaurent Vivier 
376021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
377560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
378560671a0SAneesh Kumar K.V {
379560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
380560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
381560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
382560671a0SAneesh Kumar K.V }
383560671a0SAneesh Kumar K.V 
384560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
385560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
386560671a0SAneesh Kumar K.V {
387560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
388560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
389560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
390560671a0SAneesh Kumar K.V }
391560671a0SAneesh Kumar K.V 
392560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
393560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
394560671a0SAneesh Kumar K.V {
395560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
396560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
397560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
398560671a0SAneesh Kumar K.V }
399560671a0SAneesh Kumar K.V 
400560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
401560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
402560671a0SAneesh Kumar K.V {
403560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
404560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
405560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
406560671a0SAneesh Kumar K.V }
407560671a0SAneesh Kumar K.V 
408c92dc856SJan Kara static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
4096a0678a7SArnd Bergmann {
4106a0678a7SArnd Bergmann 	now = clamp_val(now, 0, (1ull << 40) - 1);
4116a0678a7SArnd Bergmann 
4126a0678a7SArnd Bergmann 	*lo = cpu_to_le32(lower_32_bits(now));
4136a0678a7SArnd Bergmann 	*hi = upper_32_bits(now);
4146a0678a7SArnd Bergmann }
4156a0678a7SArnd Bergmann 
4166a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
4176a0678a7SArnd Bergmann {
4186a0678a7SArnd Bergmann 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
4196a0678a7SArnd Bergmann }
4206a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \
421c92dc856SJan Kara 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
422c92dc856SJan Kara 			     ktime_get_real_seconds())
4236a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \
4246a0678a7SArnd Bergmann 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
425d3d1faf6SCurt Wohlgemuth 
426bdfe0cbdSTheodore Ts'o /*
427bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
428bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
429bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
430bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
431bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
432bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
433bdfe0cbdSTheodore Ts'o  */
434bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
435bdfe0cbdSTheodore Ts'o {
436bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
437bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
438bdfe0cbdSTheodore Ts'o 
439bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
440bdfe0cbdSTheodore Ts'o }
441bdfe0cbdSTheodore Ts'o 
44218aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
44318aadd47SBobi Jam {
44418aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
44518aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
44618aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
4475d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
44818aadd47SBobi Jam 
4495d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
450a0154344SDaeho Jeong 
451a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
452a0154344SDaeho Jeong 
45318aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
4545d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
4555d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
4565d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
45718aadd47SBobi Jam 		list_del_init(&jce->jce_list);
45818aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
45918aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
46018aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
46118aadd47SBobi Jam 	}
46218aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
46318aadd47SBobi Jam }
4641c13d5c0STheodore Ts'o 
465afb585a9SMauricio Faria de Oliveira /*
466afb585a9SMauricio Faria de Oliveira  * This writepage callback for write_cache_pages()
467afb585a9SMauricio Faria de Oliveira  * takes care of a few cases after page cleaning.
468afb585a9SMauricio Faria de Oliveira  *
469afb585a9SMauricio Faria de Oliveira  * write_cache_pages() already checks for dirty pages
470afb585a9SMauricio Faria de Oliveira  * and calls clear_page_dirty_for_io(), which we want,
471afb585a9SMauricio Faria de Oliveira  * to write protect the pages.
472afb585a9SMauricio Faria de Oliveira  *
473afb585a9SMauricio Faria de Oliveira  * However, we may have to redirty a page (see below.)
474afb585a9SMauricio Faria de Oliveira  */
475afb585a9SMauricio Faria de Oliveira static int ext4_journalled_writepage_callback(struct page *page,
476afb585a9SMauricio Faria de Oliveira 					      struct writeback_control *wbc,
477afb585a9SMauricio Faria de Oliveira 					      void *data)
478afb585a9SMauricio Faria de Oliveira {
479afb585a9SMauricio Faria de Oliveira 	transaction_t *transaction = (transaction_t *) data;
480afb585a9SMauricio Faria de Oliveira 	struct buffer_head *bh, *head;
481afb585a9SMauricio Faria de Oliveira 	struct journal_head *jh;
482afb585a9SMauricio Faria de Oliveira 
483afb585a9SMauricio Faria de Oliveira 	bh = head = page_buffers(page);
484afb585a9SMauricio Faria de Oliveira 	do {
485afb585a9SMauricio Faria de Oliveira 		/*
486afb585a9SMauricio Faria de Oliveira 		 * We have to redirty a page in these cases:
487afb585a9SMauricio Faria de Oliveira 		 * 1) If buffer is dirty, it means the page was dirty because it
488afb585a9SMauricio Faria de Oliveira 		 * contains a buffer that needs checkpointing. So the dirty bit
489afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that checkpointing writes the buffer
490afb585a9SMauricio Faria de Oliveira 		 * properly.
491afb585a9SMauricio Faria de Oliveira 		 * 2) If buffer is not part of the committing transaction
492afb585a9SMauricio Faria de Oliveira 		 * (we may have just accidentally come across this buffer because
493afb585a9SMauricio Faria de Oliveira 		 * inode range tracking is not exact) or if the currently running
494afb585a9SMauricio Faria de Oliveira 		 * transaction already contains this buffer as well, dirty bit
495afb585a9SMauricio Faria de Oliveira 		 * needs to be preserved so that the buffer gets writeprotected
496afb585a9SMauricio Faria de Oliveira 		 * properly on running transaction's commit.
497afb585a9SMauricio Faria de Oliveira 		 */
498afb585a9SMauricio Faria de Oliveira 		jh = bh2jh(bh);
499afb585a9SMauricio Faria de Oliveira 		if (buffer_dirty(bh) ||
500afb585a9SMauricio Faria de Oliveira 		    (jh && (jh->b_transaction != transaction ||
501afb585a9SMauricio Faria de Oliveira 			    jh->b_next_transaction))) {
502afb585a9SMauricio Faria de Oliveira 			redirty_page_for_writepage(wbc, page);
503afb585a9SMauricio Faria de Oliveira 			goto out;
504afb585a9SMauricio Faria de Oliveira 		}
505afb585a9SMauricio Faria de Oliveira 	} while ((bh = bh->b_this_page) != head);
506afb585a9SMauricio Faria de Oliveira 
507afb585a9SMauricio Faria de Oliveira out:
508afb585a9SMauricio Faria de Oliveira 	return AOP_WRITEPAGE_ACTIVATE;
509afb585a9SMauricio Faria de Oliveira }
510afb585a9SMauricio Faria de Oliveira 
511afb585a9SMauricio Faria de Oliveira static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
512afb585a9SMauricio Faria de Oliveira {
513afb585a9SMauricio Faria de Oliveira 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
514afb585a9SMauricio Faria de Oliveira 	struct writeback_control wbc = {
515afb585a9SMauricio Faria de Oliveira 		.sync_mode =  WB_SYNC_ALL,
516afb585a9SMauricio Faria de Oliveira 		.nr_to_write = LONG_MAX,
517afb585a9SMauricio Faria de Oliveira 		.range_start = jinode->i_dirty_start,
518afb585a9SMauricio Faria de Oliveira 		.range_end = jinode->i_dirty_end,
519afb585a9SMauricio Faria de Oliveira         };
520afb585a9SMauricio Faria de Oliveira 
521afb585a9SMauricio Faria de Oliveira 	return write_cache_pages(mapping, &wbc,
522afb585a9SMauricio Faria de Oliveira 				 ext4_journalled_writepage_callback,
523afb585a9SMauricio Faria de Oliveira 				 jinode->i_transaction);
524afb585a9SMauricio Faria de Oliveira }
525afb585a9SMauricio Faria de Oliveira 
526afb585a9SMauricio Faria de Oliveira static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
527afb585a9SMauricio Faria de Oliveira {
528afb585a9SMauricio Faria de Oliveira 	int ret;
529afb585a9SMauricio Faria de Oliveira 
530afb585a9SMauricio Faria de Oliveira 	if (ext4_should_journal_data(jinode->i_vfs_inode))
531afb585a9SMauricio Faria de Oliveira 		ret = ext4_journalled_submit_inode_data_buffers(jinode);
532afb585a9SMauricio Faria de Oliveira 	else
533afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_submit_inode_data_buffers(jinode);
534afb585a9SMauricio Faria de Oliveira 
535afb585a9SMauricio Faria de Oliveira 	return ret;
536afb585a9SMauricio Faria de Oliveira }
537afb585a9SMauricio Faria de Oliveira 
538afb585a9SMauricio Faria de Oliveira static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
539afb585a9SMauricio Faria de Oliveira {
540afb585a9SMauricio Faria de Oliveira 	int ret = 0;
541afb585a9SMauricio Faria de Oliveira 
542afb585a9SMauricio Faria de Oliveira 	if (!ext4_should_journal_data(jinode->i_vfs_inode))
543afb585a9SMauricio Faria de Oliveira 		ret = jbd2_journal_finish_inode_data_buffers(jinode);
544afb585a9SMauricio Faria de Oliveira 
545afb585a9SMauricio Faria de Oliveira 	return ret;
546afb585a9SMauricio Faria de Oliveira }
547afb585a9SMauricio Faria de Oliveira 
5481dc1097fSJan Kara static bool system_going_down(void)
5491dc1097fSJan Kara {
5501dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
5511dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
5521dc1097fSJan Kara }
5531dc1097fSJan Kara 
55402a7780eSJan Kara struct ext4_err_translation {
55502a7780eSJan Kara 	int code;
55602a7780eSJan Kara 	int errno;
55702a7780eSJan Kara };
55802a7780eSJan Kara 
55902a7780eSJan Kara #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
56002a7780eSJan Kara 
56102a7780eSJan Kara static struct ext4_err_translation err_translation[] = {
56202a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EIO),
56302a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOMEM),
56402a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFSBADCRC),
56502a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFSCORRUPTED),
56602a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOSPC),
56702a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOKEY),
56802a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EROFS),
56902a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFBIG),
57002a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EEXIST),
57102a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ERANGE),
57202a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EOVERFLOW),
57302a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EBUSY),
57402a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOTDIR),
57502a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ENOTEMPTY),
57602a7780eSJan Kara 	EXT4_ERR_TRANSLATE(ESHUTDOWN),
57702a7780eSJan Kara 	EXT4_ERR_TRANSLATE(EFAULT),
57802a7780eSJan Kara };
57902a7780eSJan Kara 
58002a7780eSJan Kara static int ext4_errno_to_code(int errno)
58102a7780eSJan Kara {
58202a7780eSJan Kara 	int i;
58302a7780eSJan Kara 
58402a7780eSJan Kara 	for (i = 0; i < ARRAY_SIZE(err_translation); i++)
58502a7780eSJan Kara 		if (err_translation[i].errno == errno)
58602a7780eSJan Kara 			return err_translation[i].code;
58702a7780eSJan Kara 	return EXT4_ERR_UNKNOWN;
58802a7780eSJan Kara }
58902a7780eSJan Kara 
5902d01ddc8SJan Kara static void save_error_info(struct super_block *sb, int error,
59140676623SJan Kara 			    __u32 ino, __u64 block,
59240676623SJan Kara 			    const char *func, unsigned int line)
59340676623SJan Kara {
594c92dc856SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
59540676623SJan Kara 
59602a7780eSJan Kara 	/* We default to EFSCORRUPTED error... */
59702a7780eSJan Kara 	if (error == 0)
59802a7780eSJan Kara 		error = EFSCORRUPTED;
599c92dc856SJan Kara 
600c92dc856SJan Kara 	spin_lock(&sbi->s_error_lock);
601c92dc856SJan Kara 	sbi->s_add_error_count++;
602c92dc856SJan Kara 	sbi->s_last_error_code = error;
603c92dc856SJan Kara 	sbi->s_last_error_line = line;
604c92dc856SJan Kara 	sbi->s_last_error_ino = ino;
605c92dc856SJan Kara 	sbi->s_last_error_block = block;
606c92dc856SJan Kara 	sbi->s_last_error_func = func;
607c92dc856SJan Kara 	sbi->s_last_error_time = ktime_get_real_seconds();
608c92dc856SJan Kara 	if (!sbi->s_first_error_time) {
609c92dc856SJan Kara 		sbi->s_first_error_code = error;
610c92dc856SJan Kara 		sbi->s_first_error_line = line;
611c92dc856SJan Kara 		sbi->s_first_error_ino = ino;
612c92dc856SJan Kara 		sbi->s_first_error_block = block;
613c92dc856SJan Kara 		sbi->s_first_error_func = func;
614c92dc856SJan Kara 		sbi->s_first_error_time = sbi->s_last_error_time;
61540676623SJan Kara 	}
616c92dc856SJan Kara 	spin_unlock(&sbi->s_error_lock);
61740676623SJan Kara }
61840676623SJan Kara 
619ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
620ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
621ac27a0ecSDave Kleikamp  *
622ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
623617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
624ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
625ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
626ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
627ac27a0ecSDave Kleikamp  * write out the superblock safely.
628ac27a0ecSDave Kleikamp  *
629dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
630d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
631ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
632014c9caaSJan Kara  *
633014c9caaSJan Kara  * If force_ro is set, we unconditionally force the filesystem into an
634014c9caaSJan Kara  * ABORT|READONLY state, unless the error response on the fs has been set to
635014c9caaSJan Kara  * panic in which case we take the easy way out and panic immediately. This is
636014c9caaSJan Kara  * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
637014c9caaSJan Kara  * at a critical moment in log management.
638ac27a0ecSDave Kleikamp  */
639e789ca0cSJan Kara static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
640e789ca0cSJan Kara 			      __u32 ino, __u64 block,
641e789ca0cSJan Kara 			      const char *func, unsigned int line)
642ac27a0ecSDave Kleikamp {
643b08070ecSJan Kara 	journal_t *journal = EXT4_SB(sb)->s_journal;
6442d01ddc8SJan Kara 	bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
645b08070ecSJan Kara 
646e789ca0cSJan Kara 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
647327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
648327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
649327eaf73STheodore Ts'o 
6502d01ddc8SJan Kara 	if (!continue_fs && !sb_rdonly(sb)) {
6519b5f6c9bSHarshad Shirwadkar 		ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
652ac27a0ecSDave Kleikamp 		if (journal)
653dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
6542d01ddc8SJan Kara 	}
6552d01ddc8SJan Kara 
6562d01ddc8SJan Kara 	if (!bdev_read_only(sb->s_bdev)) {
6572d01ddc8SJan Kara 		save_error_info(sb, error, ino, block, func, line);
6582d01ddc8SJan Kara 		/*
6592d01ddc8SJan Kara 		 * In case the fs should keep running, we need to writeout
6602d01ddc8SJan Kara 		 * superblock through the journal. Due to lock ordering
6612d01ddc8SJan Kara 		 * constraints, it may not be safe to do it right here so we
6622d01ddc8SJan Kara 		 * defer superblock flushing to a workqueue.
6632d01ddc8SJan Kara 		 */
6642d01ddc8SJan Kara 		if (continue_fs)
6652d01ddc8SJan Kara 			schedule_work(&EXT4_SB(sb)->s_error_work);
6662d01ddc8SJan Kara 		else
6672d01ddc8SJan Kara 			ext4_commit_super(sb);
6682d01ddc8SJan Kara 	}
6692d01ddc8SJan Kara 
6702d01ddc8SJan Kara 	if (sb_rdonly(sb) || continue_fs)
6712d01ddc8SJan Kara 		return;
6722d01ddc8SJan Kara 
6731dc1097fSJan Kara 	/*
6741dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
6751dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
6761dc1097fSJan Kara 	 * disabled.
6771dc1097fSJan Kara 	 */
678014c9caaSJan Kara 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
679617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
680ac27a0ecSDave Kleikamp 			sb->s_id);
681ac27a0ecSDave Kleikamp 	}
682014c9caaSJan Kara 	ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
683014c9caaSJan Kara 	/*
684014c9caaSJan Kara 	 * Make sure updated value of ->s_mount_flags will be visible before
685014c9caaSJan Kara 	 * ->s_flags update
686014c9caaSJan Kara 	 */
687014c9caaSJan Kara 	smp_wmb();
688014c9caaSJan Kara 	sb->s_flags |= SB_RDONLY;
6894327ba52SDaeho Jeong }
690ac27a0ecSDave Kleikamp 
691c92dc856SJan Kara static void flush_stashed_error_work(struct work_struct *work)
692c92dc856SJan Kara {
693c92dc856SJan Kara 	struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
694c92dc856SJan Kara 						s_error_work);
6952d01ddc8SJan Kara 	journal_t *journal = sbi->s_journal;
6962d01ddc8SJan Kara 	handle_t *handle;
697c92dc856SJan Kara 
6982d01ddc8SJan Kara 	/*
6992d01ddc8SJan Kara 	 * If the journal is still running, we have to write out superblock
7002d01ddc8SJan Kara 	 * through the journal to avoid collisions of other journalled sb
7012d01ddc8SJan Kara 	 * updates.
7022d01ddc8SJan Kara 	 *
7032d01ddc8SJan Kara 	 * We use directly jbd2 functions here to avoid recursing back into
7042d01ddc8SJan Kara 	 * ext4 error handling code during handling of previous errors.
7052d01ddc8SJan Kara 	 */
7062d01ddc8SJan Kara 	if (!sb_rdonly(sbi->s_sb) && journal) {
7072d01ddc8SJan Kara 		handle = jbd2_journal_start(journal, 1);
7082d01ddc8SJan Kara 		if (IS_ERR(handle))
7092d01ddc8SJan Kara 			goto write_directly;
7102d01ddc8SJan Kara 		if (jbd2_journal_get_write_access(handle, sbi->s_sbh)) {
7112d01ddc8SJan Kara 			jbd2_journal_stop(handle);
7122d01ddc8SJan Kara 			goto write_directly;
7132d01ddc8SJan Kara 		}
7142d01ddc8SJan Kara 		ext4_update_super(sbi->s_sb);
7152d01ddc8SJan Kara 		if (jbd2_journal_dirty_metadata(handle, sbi->s_sbh)) {
7162d01ddc8SJan Kara 			jbd2_journal_stop(handle);
7172d01ddc8SJan Kara 			goto write_directly;
7182d01ddc8SJan Kara 		}
7192d01ddc8SJan Kara 		jbd2_journal_stop(handle);
7202d01ddc8SJan Kara 		return;
7212d01ddc8SJan Kara 	}
7222d01ddc8SJan Kara write_directly:
7232d01ddc8SJan Kara 	/*
7242d01ddc8SJan Kara 	 * Write through journal failed. Write sb directly to get error info
7252d01ddc8SJan Kara 	 * out and hope for the best.
7262d01ddc8SJan Kara 	 */
7274392fbc4SJan Kara 	ext4_commit_super(sbi->s_sb);
728ac27a0ecSDave Kleikamp }
729ac27a0ecSDave Kleikamp 
730efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
731efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
732efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
733efbed4dcSTheodore Ts'o 
73412062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
735014c9caaSJan Kara 		  unsigned int line, bool force_ro, int error, __u64 block,
73654d3adbcSTheodore Ts'o 		  const char *fmt, ...)
737ac27a0ecSDave Kleikamp {
7380ff2ea7dSJoe Perches 	struct va_format vaf;
739ac27a0ecSDave Kleikamp 	va_list args;
740ac27a0ecSDave Kleikamp 
7410db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7420db1ff22STheodore Ts'o 		return;
7430db1ff22STheodore Ts'o 
744ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
745efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
746ac27a0ecSDave Kleikamp 		va_start(args, fmt);
7470ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7480ff2ea7dSJoe Perches 		vaf.va = &args;
749efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
750efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
7510ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
752ac27a0ecSDave Kleikamp 		va_end(args);
753efbed4dcSTheodore Ts'o 	}
754e789ca0cSJan Kara 	ext4_handle_error(sb, force_ro, error, 0, block, function, line);
755ac27a0ecSDave Kleikamp }
756ac27a0ecSDave Kleikamp 
757e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
75854d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
759273df556SFrank Mayhar 			const char *fmt, ...)
760273df556SFrank Mayhar {
761273df556SFrank Mayhar 	va_list args;
762f7c21177STheodore Ts'o 	struct va_format vaf;
763273df556SFrank Mayhar 
7640db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
7650db1ff22STheodore Ts'o 		return;
7660db1ff22STheodore Ts'o 
767ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
768efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
769273df556SFrank Mayhar 		va_start(args, fmt);
770f7c21177STheodore Ts'o 		vaf.fmt = fmt;
771f7c21177STheodore Ts'o 		vaf.va = &args;
772c398eda0STheodore Ts'o 		if (block)
773d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
774d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
775d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
776d9ee81daSJoe Perches 			       block, current->comm, &vaf);
777d9ee81daSJoe Perches 		else
778d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
779d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
780d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
781d9ee81daSJoe Perches 			       current->comm, &vaf);
782273df556SFrank Mayhar 		va_end(args);
783efbed4dcSTheodore Ts'o 	}
784e789ca0cSJan Kara 	ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
78554d3adbcSTheodore Ts'o 			  function, line);
786273df556SFrank Mayhar }
787273df556SFrank Mayhar 
788e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
789f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
790f7c21177STheodore Ts'o 		       const char *fmt, ...)
791273df556SFrank Mayhar {
792273df556SFrank Mayhar 	va_list args;
793f7c21177STheodore Ts'o 	struct va_format vaf;
794496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
795273df556SFrank Mayhar 	char pathname[80], *path;
796273df556SFrank Mayhar 
7970db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
7980db1ff22STheodore Ts'o 		return;
7990db1ff22STheodore Ts'o 
800ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
801efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
8029bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
803f9a62d09SDan Carpenter 		if (IS_ERR(path))
804273df556SFrank Mayhar 			path = "(unknown)";
805f7c21177STheodore Ts'o 		va_start(args, fmt);
806f7c21177STheodore Ts'o 		vaf.fmt = fmt;
807f7c21177STheodore Ts'o 		vaf.va = &args;
808d9ee81daSJoe Perches 		if (block)
809d9ee81daSJoe Perches 			printk(KERN_CRIT
810d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
811d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
812d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
813d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
814d9ee81daSJoe Perches 		else
815d9ee81daSJoe Perches 			printk(KERN_CRIT
816d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
817d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
818d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
819d9ee81daSJoe Perches 			       current->comm, path, &vaf);
820273df556SFrank Mayhar 		va_end(args);
821efbed4dcSTheodore Ts'o 	}
822e789ca0cSJan Kara 	ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
82354d3adbcSTheodore Ts'o 			  function, line);
824273df556SFrank Mayhar }
825273df556SFrank Mayhar 
826722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
827ac27a0ecSDave Kleikamp 			      char nbuf[16])
828ac27a0ecSDave Kleikamp {
829ac27a0ecSDave Kleikamp 	char *errstr = NULL;
830ac27a0ecSDave Kleikamp 
831ac27a0ecSDave Kleikamp 	switch (errno) {
8326a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
8336a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
8346a797d27SDarrick J. Wong 		break;
8356a797d27SDarrick J. Wong 	case -EFSBADCRC:
8366a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
8376a797d27SDarrick J. Wong 		break;
838ac27a0ecSDave Kleikamp 	case -EIO:
839ac27a0ecSDave Kleikamp 		errstr = "IO failure";
840ac27a0ecSDave Kleikamp 		break;
841ac27a0ecSDave Kleikamp 	case -ENOMEM:
842ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
843ac27a0ecSDave Kleikamp 		break;
844ac27a0ecSDave Kleikamp 	case -EROFS:
84578f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
84678f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
847ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
848ac27a0ecSDave Kleikamp 		else
849ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
850ac27a0ecSDave Kleikamp 		break;
851ac27a0ecSDave Kleikamp 	default:
852ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
853ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
854ac27a0ecSDave Kleikamp 		 * NULL. */
855ac27a0ecSDave Kleikamp 		if (nbuf) {
856ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
857ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
858ac27a0ecSDave Kleikamp 				errstr = nbuf;
859ac27a0ecSDave Kleikamp 		}
860ac27a0ecSDave Kleikamp 		break;
861ac27a0ecSDave Kleikamp 	}
862ac27a0ecSDave Kleikamp 
863ac27a0ecSDave Kleikamp 	return errstr;
864ac27a0ecSDave Kleikamp }
865ac27a0ecSDave Kleikamp 
866617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
867ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
868ac27a0ecSDave Kleikamp 
869c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
870c398eda0STheodore Ts'o 		      unsigned int line, int errno)
871ac27a0ecSDave Kleikamp {
872ac27a0ecSDave Kleikamp 	char nbuf[16];
873ac27a0ecSDave Kleikamp 	const char *errstr;
874ac27a0ecSDave Kleikamp 
8750db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8760db1ff22STheodore Ts'o 		return;
8770db1ff22STheodore Ts'o 
878ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
879ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
880ac27a0ecSDave Kleikamp 	 * an error. */
881bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
882ac27a0ecSDave Kleikamp 		return;
883ac27a0ecSDave Kleikamp 
884efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
885617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
886c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
887c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
888efbed4dcSTheodore Ts'o 	}
889ac27a0ecSDave Kleikamp 
890e789ca0cSJan Kara 	ext4_handle_error(sb, false, -errno, 0, 0, function, line);
891ac27a0ecSDave Kleikamp }
892ac27a0ecSDave Kleikamp 
893e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
894e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
895b31e1552SEric Sandeen {
8960ff2ea7dSJoe Perches 	struct va_format vaf;
897b31e1552SEric Sandeen 	va_list args;
898b31e1552SEric Sandeen 
8991cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_msg_count);
900efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
901efbed4dcSTheodore Ts'o 		return;
902efbed4dcSTheodore Ts'o 
903b31e1552SEric Sandeen 	va_start(args, fmt);
9040ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9050ff2ea7dSJoe Perches 	vaf.va = &args;
9060ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
907b31e1552SEric Sandeen 	va_end(args);
908b31e1552SEric Sandeen }
909b31e1552SEric Sandeen 
9101cf006edSDmitry Monakhov static int ext4_warning_ratelimit(struct super_block *sb)
9111cf006edSDmitry Monakhov {
9121cf006edSDmitry Monakhov 	atomic_inc(&EXT4_SB(sb)->s_warning_count);
9131cf006edSDmitry Monakhov 	return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
9141cf006edSDmitry Monakhov 			    "EXT4-fs warning");
9151cf006edSDmitry Monakhov }
916b03a2f7eSAndreas Dilger 
91712062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
918c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
919ac27a0ecSDave Kleikamp {
9200ff2ea7dSJoe Perches 	struct va_format vaf;
921ac27a0ecSDave Kleikamp 	va_list args;
922ac27a0ecSDave Kleikamp 
923b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
924efbed4dcSTheodore Ts'o 		return;
925efbed4dcSTheodore Ts'o 
926ac27a0ecSDave Kleikamp 	va_start(args, fmt);
9270ff2ea7dSJoe Perches 	vaf.fmt = fmt;
9280ff2ea7dSJoe Perches 	vaf.va = &args;
9290ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
9300ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
931ac27a0ecSDave Kleikamp 	va_end(args);
932ac27a0ecSDave Kleikamp }
933ac27a0ecSDave Kleikamp 
934b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
935b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
936b03a2f7eSAndreas Dilger {
937b03a2f7eSAndreas Dilger 	struct va_format vaf;
938b03a2f7eSAndreas Dilger 	va_list args;
939b03a2f7eSAndreas Dilger 
940b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
941b03a2f7eSAndreas Dilger 		return;
942b03a2f7eSAndreas Dilger 
943b03a2f7eSAndreas Dilger 	va_start(args, fmt);
944b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
945b03a2f7eSAndreas Dilger 	vaf.va = &args;
946b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
947b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
948b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
949b03a2f7eSAndreas Dilger 	va_end(args);
950b03a2f7eSAndreas Dilger }
951b03a2f7eSAndreas Dilger 
952e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
953e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
954e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
955e29136f8STheodore Ts'o 			     const char *fmt, ...)
9565d1b1b3fSAneesh Kumar K.V __releases(bitlock)
9575d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
9585d1b1b3fSAneesh Kumar K.V {
9590ff2ea7dSJoe Perches 	struct va_format vaf;
9605d1b1b3fSAneesh Kumar K.V 	va_list args;
9615d1b1b3fSAneesh Kumar K.V 
9620db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
9630db1ff22STheodore Ts'o 		return;
9640db1ff22STheodore Ts'o 
965ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
966efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
9675d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
9680ff2ea7dSJoe Perches 		vaf.fmt = fmt;
9690ff2ea7dSJoe Perches 		vaf.va = &args;
97021149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
971e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
972e29136f8STheodore Ts'o 		if (ino)
9730ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
974e29136f8STheodore Ts'o 		if (block)
975efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
976efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
9770ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
9785d1b1b3fSAneesh Kumar K.V 		va_end(args);
979efbed4dcSTheodore Ts'o 	}
9805d1b1b3fSAneesh Kumar K.V 
981c92dc856SJan Kara 	if (test_opt(sb, ERRORS_CONT)) {
982327eaf73STheodore Ts'o 		if (test_opt(sb, WARN_ON_ERROR))
983327eaf73STheodore Ts'o 			WARN_ON_ONCE(1);
984e789ca0cSJan Kara 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
9852d01ddc8SJan Kara 		if (!bdev_read_only(sb->s_bdev)) {
9862d01ddc8SJan Kara 			save_error_info(sb, EFSCORRUPTED, ino, block, function,
9872d01ddc8SJan Kara 					line);
988c92dc856SJan Kara 			schedule_work(&EXT4_SB(sb)->s_error_work);
9892d01ddc8SJan Kara 		}
9905d1b1b3fSAneesh Kumar K.V 		return;
9915d1b1b3fSAneesh Kumar K.V 	}
9925d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
993e789ca0cSJan Kara 	ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
9945d1b1b3fSAneesh Kumar K.V 	/*
9955d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
9965d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
9975d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
9985d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
9995d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
100025985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
10015d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
10025d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
10035d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
10045d1b1b3fSAneesh Kumar K.V 	 */
10055d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
10065d1b1b3fSAneesh Kumar K.V 	return;
10075d1b1b3fSAneesh Kumar K.V }
10085d1b1b3fSAneesh Kumar K.V 
1009db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1010db79e6d1SWang Shilong 				     ext4_group_t group,
1011db79e6d1SWang Shilong 				     unsigned int flags)
1012db79e6d1SWang Shilong {
1013db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1014db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1015db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
10169af0b3d1SWang Shilong 	int ret;
1017db79e6d1SWang Shilong 
10189af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
10199af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
10209af0b3d1SWang Shilong 					    &grp->bb_state);
10219af0b3d1SWang Shilong 		if (!ret)
1022db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
1023db79e6d1SWang Shilong 					   grp->bb_free);
1024db79e6d1SWang Shilong 	}
1025db79e6d1SWang Shilong 
10269af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
10279af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
10289af0b3d1SWang Shilong 					    &grp->bb_state);
10299af0b3d1SWang Shilong 		if (!ret && gdp) {
1030db79e6d1SWang Shilong 			int count;
1031db79e6d1SWang Shilong 
1032db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
1033db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
1034db79e6d1SWang Shilong 					   count);
1035db79e6d1SWang Shilong 		}
1036db79e6d1SWang Shilong 	}
1037db79e6d1SWang Shilong }
1038db79e6d1SWang Shilong 
1039617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
1040ac27a0ecSDave Kleikamp {
1041617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
1042ac27a0ecSDave Kleikamp 
1043617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
1044ac27a0ecSDave Kleikamp 		return;
1045ac27a0ecSDave Kleikamp 
104612062dddSEric Sandeen 	ext4_warning(sb,
1047ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
1048ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
1049617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
1050ac27a0ecSDave Kleikamp 
1051617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1052617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1053617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
1054ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
1055ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
1056ac27a0ecSDave Kleikamp 
1057ac27a0ecSDave Kleikamp 	/*
1058ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
1059ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
1060ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
1061ac27a0ecSDave Kleikamp 	 */
1062ac27a0ecSDave Kleikamp }
1063ac27a0ecSDave Kleikamp 
1064ac27a0ecSDave Kleikamp /*
1065ac27a0ecSDave Kleikamp  * Open the external journal device
1066ac27a0ecSDave Kleikamp  */
1067b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
1068ac27a0ecSDave Kleikamp {
1069ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1070ac27a0ecSDave Kleikamp 
1071d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
1072ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
1073ac27a0ecSDave Kleikamp 		goto fail;
1074ac27a0ecSDave Kleikamp 	return bdev;
1075ac27a0ecSDave Kleikamp 
1076ac27a0ecSDave Kleikamp fail:
1077ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
1078ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
1079ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
1080ac27a0ecSDave Kleikamp 	return NULL;
1081ac27a0ecSDave Kleikamp }
1082ac27a0ecSDave Kleikamp 
1083ac27a0ecSDave Kleikamp /*
1084ac27a0ecSDave Kleikamp  * Release the journal device
1085ac27a0ecSDave Kleikamp  */
10864385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
1087ac27a0ecSDave Kleikamp {
10884385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1089ac27a0ecSDave Kleikamp }
1090ac27a0ecSDave Kleikamp 
10914385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
1092ac27a0ecSDave Kleikamp {
1093ac27a0ecSDave Kleikamp 	struct block_device *bdev;
1094ee7ed3aaSChunguang Xu 	bdev = sbi->s_journal_bdev;
1095ac27a0ecSDave Kleikamp 	if (bdev) {
10964385bab1SAl Viro 		ext4_blkdev_put(bdev);
1097ee7ed3aaSChunguang Xu 		sbi->s_journal_bdev = NULL;
1098ac27a0ecSDave Kleikamp 	}
1099ac27a0ecSDave Kleikamp }
1100ac27a0ecSDave Kleikamp 
1101ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
1102ac27a0ecSDave Kleikamp {
1103617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
1104ac27a0ecSDave Kleikamp }
1105ac27a0ecSDave Kleikamp 
1106617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
1107ac27a0ecSDave Kleikamp {
1108ac27a0ecSDave Kleikamp 	struct list_head *l;
1109ac27a0ecSDave Kleikamp 
1110b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1111ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
1112ac27a0ecSDave Kleikamp 
1113ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
1114ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
1115ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
1116ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
1117ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1118ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
1119ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
1120ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
1121ac27a0ecSDave Kleikamp 	}
1122ac27a0ecSDave Kleikamp }
1123ac27a0ecSDave Kleikamp 
1124957153fcSJan Kara #ifdef CONFIG_QUOTA
1125957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
1126957153fcSJan Kara 
1127957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1128957153fcSJan Kara {
1129957153fcSJan Kara 	int type;
1130957153fcSJan Kara 
1131957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
1132957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
1133957153fcSJan Kara 		ext4_quota_off(sb, type);
1134957153fcSJan Kara }
113533458eabSTheodore Ts'o 
113633458eabSTheodore Ts'o /*
113733458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
113833458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
113933458eabSTheodore Ts'o  */
114033458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
114133458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
114233458eabSTheodore Ts'o 				int type)
114333458eabSTheodore Ts'o {
114433458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
114533458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
114633458eabSTheodore Ts'o }
1147957153fcSJan Kara #else
1148957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
1149957153fcSJan Kara {
1150957153fcSJan Kara }
1151957153fcSJan Kara #endif
1152957153fcSJan Kara 
1153617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1154ac27a0ecSDave Kleikamp {
1155617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1156617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
11571d0c3924STheodore Ts'o 	struct buffer_head **group_desc;
11587c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
115997abd7d4STheodore Ts'o 	int aborted = 0;
1160ef2cabf7SHidehiro Kawai 	int i, err;
1161ac27a0ecSDave Kleikamp 
1162857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1163957153fcSJan Kara 	ext4_quota_off_umount(sb);
1164e0ccfd95SChristoph Hellwig 
1165c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
11662e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
11674c0425ffSMingming Cao 
11685e47868fSRitesh Harjani 	/*
11695e47868fSRitesh Harjani 	 * Unregister sysfs before destroying jbd2 journal.
11705e47868fSRitesh Harjani 	 * Since we could still access attr_journal_task attribute via sysfs
11715e47868fSRitesh Harjani 	 * path which could have sbi->s_journal->j_task as NULL
11725e47868fSRitesh Harjani 	 */
11735e47868fSRitesh Harjani 	ext4_unregister_sysfs(sb);
11745e47868fSRitesh Harjani 
11750390131bSFrank Mayhar 	if (sbi->s_journal) {
117697abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1177ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
117847b4a50bSJan Kara 		sbi->s_journal = NULL;
1179878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
118054d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
11810390131bSFrank Mayhar 		}
1182878520acSTheodore Ts'o 	}
1183d4edac31SJosef Bacik 
1184d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
11859105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1186d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1187d4edac31SJosef Bacik 	ext4_mb_release(sb);
1188d4edac31SJosef Bacik 	ext4_ext_release(sb);
1189d4edac31SJosef Bacik 
1190bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1191e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
1192ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1193ac27a0ecSDave Kleikamp 	}
1194bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
11954392fbc4SJan Kara 		ext4_commit_super(sb);
1196a8e25a83SArtem Bityutskiy 
11971d0c3924STheodore Ts'o 	rcu_read_lock();
11981d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
1199ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
12001d0c3924STheodore Ts'o 		brelse(group_desc[i]);
12011d0c3924STheodore Ts'o 	kvfree(group_desc);
12027c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
12037c990728SSuraj Jitindar Singh 	if (flex_groups) {
12047c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
12057c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
12067c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
12077c990728SSuraj Jitindar Singh 	}
12081d0c3924STheodore Ts'o 	rcu_read_unlock();
120957042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1210ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1211ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
121257042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1213bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1214ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1215a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
121633458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1217ac27a0ecSDave Kleikamp #endif
1218ac27a0ecSDave Kleikamp 
1219ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1220ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1221ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1222ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1223ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1224ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1225837c23fbSChunguang Xu 	ASSERT(list_empty(&sbi->s_orphan));
1226ac27a0ecSDave Kleikamp 
122789d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1228f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1229ee7ed3aaSChunguang Xu 	if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
1230ac27a0ecSDave Kleikamp 		/*
1231ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1232ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1233ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1234ac27a0ecSDave Kleikamp 		 */
1235ee7ed3aaSChunguang Xu 		sync_blockdev(sbi->s_journal_bdev);
1236ee7ed3aaSChunguang Xu 		invalidate_bdev(sbi->s_journal_bdev);
1237617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1238ac27a0ecSDave Kleikamp 	}
123950c15df6SChengguang Xu 
1240dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1241dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
124250c15df6SChengguang Xu 
124347387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
124447387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
124550c15df6SChengguang Xu 
1246c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1247c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
12489060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1249ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
12503197ebdbSTheodore Ts'o 	/*
12513197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
12523197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
12533197ebdbSTheodore Ts'o 	 */
12543197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
12553197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
12560441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
12570441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1258705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
12595e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1260ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
1261c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1262f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
1263c83ad55eSGabriel Krisman Bertazi #endif
1264ac27a0ecSDave Kleikamp 	kfree(sbi);
1265ac27a0ecSDave Kleikamp }
1266ac27a0ecSDave Kleikamp 
1267e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1268ac27a0ecSDave Kleikamp 
1269ac27a0ecSDave Kleikamp /*
1270ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1271ac27a0ecSDave Kleikamp  */
1272617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1273ac27a0ecSDave Kleikamp {
1274617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1275ac27a0ecSDave Kleikamp 
1276e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1277ac27a0ecSDave Kleikamp 	if (!ei)
1278ac27a0ecSDave Kleikamp 		return NULL;
12790b8e58a1SAndreas Dilger 
1280ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1281202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1282c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
128327bc446eSbrookxu 	atomic_set(&ei->i_prealloc_active, 0);
1284c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
12859a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
12869a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1287edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1288eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1289edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1290dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1291d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1292d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
12931dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1294a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1295a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
129696c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1297a9e7f447SDmitry Monakhov #endif
12988aefcd55STheodore Ts'o 	ei->jinode = NULL;
12992e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1300744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1301b436b9beSJan Kara 	ei->i_sync_tid = 0;
1302b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1303e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
13042e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1305aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1306aa75f4d3SHarshad Shirwadkar 	mutex_init(&ei->i_fc_lock);
1307ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1308ac27a0ecSDave Kleikamp }
1309ac27a0ecSDave Kleikamp 
13107ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
13117ff9c073STheodore Ts'o {
13127ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
13137ff9c073STheodore Ts'o 
131429b3692eSEric Biggers 	if (!drop)
131529b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
131629b3692eSEric Biggers 
13177ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
13187ff9c073STheodore Ts'o 	return drop;
13197ff9c073STheodore Ts'o }
13207ff9c073STheodore Ts'o 
132194053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1322fa0d7e3dSNick Piggin {
13232c58d548SEric Biggers 	fscrypt_free_inode(inode);
1324aa75f4d3SHarshad Shirwadkar 	if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1325aa75f4d3SHarshad Shirwadkar 		pr_warn("%s: inode %ld still in fc list",
1326aa75f4d3SHarshad Shirwadkar 			__func__, inode->i_ino);
1327aa75f4d3SHarshad Shirwadkar 	}
1328fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1329fa0d7e3dSNick Piggin }
1330fa0d7e3dSNick Piggin 
1331617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1332ac27a0ecSDave Kleikamp {
13339f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1334b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1335b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1336b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
13379f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
13389f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
13399f7dd93dSVasily Averin 				true);
13409f7dd93dSVasily Averin 		dump_stack();
13419f7dd93dSVasily Averin 	}
1342ac27a0ecSDave Kleikamp }
1343ac27a0ecSDave Kleikamp 
134451cc5068SAlexey Dobriyan static void init_once(void *foo)
1345ac27a0ecSDave Kleikamp {
1346617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1347ac27a0ecSDave Kleikamp 
1348ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1349ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
13500e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1351ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1352ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1353aa75f4d3SHarshad Shirwadkar 	ext4_fc_init_inode(&ei->vfs_inode);
1354ac27a0ecSDave Kleikamp }
1355ac27a0ecSDave Kleikamp 
1356e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1357ac27a0ecSDave Kleikamp {
1358f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1359f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1360f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1361f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1362f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1363f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
136420c2df83SPaul Mundt 				init_once);
1365617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1366ac27a0ecSDave Kleikamp 		return -ENOMEM;
1367ac27a0ecSDave Kleikamp 	return 0;
1368ac27a0ecSDave Kleikamp }
1369ac27a0ecSDave Kleikamp 
1370ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1371ac27a0ecSDave Kleikamp {
13728c0a8537SKirill A. Shutemov 	/*
13738c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
13748c0a8537SKirill A. Shutemov 	 * destroy cache.
13758c0a8537SKirill A. Shutemov 	 */
13768c0a8537SKirill A. Shutemov 	rcu_barrier();
1377617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1378ac27a0ecSDave Kleikamp }
1379ac27a0ecSDave Kleikamp 
13800930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1381ac27a0ecSDave Kleikamp {
1382aa75f4d3SHarshad Shirwadkar 	ext4_fc_del(inode);
13830930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1384dbd5768fSJan Kara 	clear_inode(inode);
138527bc446eSbrookxu 	ext4_discard_preallocations(inode, 0);
138651865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1387f4c2d372SJan Kara 	dquot_drop(inode);
13888aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
13898aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
13908aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
13918aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
13928aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
13938aefcd55STheodore Ts'o 	}
13943d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1395c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1396ac27a0ecSDave Kleikamp }
1397ac27a0ecSDave Kleikamp 
13981b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
13991b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1400ac27a0ecSDave Kleikamp {
1401ac27a0ecSDave Kleikamp 	struct inode *inode;
1402ac27a0ecSDave Kleikamp 
14038a363970STheodore Ts'o 	/*
1404ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1405ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1406ac27a0ecSDave Kleikamp 	 */
14078a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
14081d1fe1eeSDavid Howells 	if (IS_ERR(inode))
14091d1fe1eeSDavid Howells 		return ERR_CAST(inode);
14101d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1411ac27a0ecSDave Kleikamp 		iput(inode);
1412ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1413ac27a0ecSDave Kleikamp 	}
14141b961ac0SChristoph Hellwig 
14151b961ac0SChristoph Hellwig 	return inode;
1416ac27a0ecSDave Kleikamp }
14171b961ac0SChristoph Hellwig 
14181b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
14191b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
14201b961ac0SChristoph Hellwig {
14211b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
14221b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
14231b961ac0SChristoph Hellwig }
14241b961ac0SChristoph Hellwig 
14251b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
14261b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
14271b961ac0SChristoph Hellwig {
14281b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
14291b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1430ac27a0ecSDave Kleikamp }
1431ac27a0ecSDave Kleikamp 
1432fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1433fde87268STheodore Ts'o {
1434fde87268STheodore Ts'o 	struct writeback_control wbc = {
1435fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1436fde87268STheodore Ts'o 	};
1437fde87268STheodore Ts'o 
1438fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1439fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1440fde87268STheodore Ts'o }
1441fde87268STheodore Ts'o 
1442c39a7f84SToshiyuki Okajima /*
1443c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1444c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1445c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1446c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1447c39a7f84SToshiyuki Okajima  */
14480b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
14490b8e58a1SAndreas Dilger 				 gfp_t wait)
1450c39a7f84SToshiyuki Okajima {
1451c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1452c39a7f84SToshiyuki Okajima 
1453c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1454c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1455c39a7f84SToshiyuki Okajima 		return 0;
1456c39a7f84SToshiyuki Okajima 	if (journal)
1457529a781eSzhangyi (F) 		return jbd2_journal_try_to_free_buffers(journal, page);
1458529a781eSzhangyi (F) 
1459c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1460c39a7f84SToshiyuki Okajima }
1461c39a7f84SToshiyuki Okajima 
1462643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1463a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1464a7550b30SJaegeuk Kim {
1465a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1466a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1467a7550b30SJaegeuk Kim }
1468a7550b30SJaegeuk Kim 
1469a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1470a7550b30SJaegeuk Kim 							void *fs_data)
1471a7550b30SJaegeuk Kim {
14722f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1473c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1474a7550b30SJaegeuk Kim 
14759ce0151aSEric Biggers 	/*
14769ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
14779ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
14789ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
14799ce0151aSEric Biggers 	 * the filename "lost+found" itself.
14809ce0151aSEric Biggers 	 */
14819ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
14829ce0151aSEric Biggers 		return -EPERM;
1483a7550b30SJaegeuk Kim 
14847d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
14857d3e06a8SRoss Zwisler 		return -EINVAL;
14867d3e06a8SRoss Zwisler 
1487b383a73fSIra Weiny 	if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1488b383a73fSIra Weiny 		return -EOPNOTSUPP;
1489b383a73fSIra Weiny 
149094840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
149194840e3cSEric Biggers 	if (res)
149294840e3cSEric Biggers 		return res;
149394840e3cSEric Biggers 
14942f8f5e76SEric Biggers 	/*
14952f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
14962f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
14972f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
14982f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
14992f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
15002f8f5e76SEric Biggers 	 */
15012f8f5e76SEric Biggers 
15022f8f5e76SEric Biggers 	if (handle) {
15032f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
15042f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
15052f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
15062f8f5e76SEric Biggers 					    ctx, len, 0);
1507a7550b30SJaegeuk Kim 		if (!res) {
1508a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1509a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1510a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1511a3caa24bSJan Kara 			/*
15122ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
15132ee6a576SEric Biggers 			 * S_DAX may be disabled
1514a3caa24bSJan Kara 			 */
1515043546e4SIra Weiny 			ext4_set_inode_flags(inode, false);
1516a7550b30SJaegeuk Kim 		}
1517a7550b30SJaegeuk Kim 		return res;
1518a7550b30SJaegeuk Kim 	}
1519a7550b30SJaegeuk Kim 
1520b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1521b8cb5a54STahsin Erdogan 	if (res)
1522b8cb5a54STahsin Erdogan 		return res;
15232f8f5e76SEric Biggers retry:
1524af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1525af65207cSTahsin Erdogan 				     &credits);
1526dec214d0STahsin Erdogan 	if (res)
1527dec214d0STahsin Erdogan 		return res;
1528dec214d0STahsin Erdogan 
1529c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1530a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1531a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1532a7550b30SJaegeuk Kim 
15332f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, 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);
15382ee6a576SEric Biggers 		/*
15392ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
15402ee6a576SEric Biggers 		 * S_DAX may be disabled
15412ee6a576SEric Biggers 		 */
1542043546e4SIra Weiny 		ext4_set_inode_flags(inode, false);
1543a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1544a7550b30SJaegeuk Kim 		if (res)
1545a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1546a7550b30SJaegeuk Kim 	}
1547a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
15482f8f5e76SEric Biggers 
15492f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
15502f8f5e76SEric Biggers 		goto retry;
1551a7550b30SJaegeuk Kim 	if (!res)
1552a7550b30SJaegeuk Kim 		res = res2;
1553a7550b30SJaegeuk Kim 	return res;
1554a7550b30SJaegeuk Kim }
1555a7550b30SJaegeuk Kim 
1556ac4acb1fSEric Biggers static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
1557a7550b30SJaegeuk Kim {
1558ac4acb1fSEric Biggers 	return EXT4_SB(sb)->s_dummy_enc_policy.policy;
1559a7550b30SJaegeuk Kim }
1560a7550b30SJaegeuk Kim 
1561b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1562b925acb8SEric Biggers {
1563b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1564b925acb8SEric Biggers }
1565b925acb8SEric Biggers 
1566b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1567b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1568b925acb8SEric Biggers {
1569b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1570b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1571b925acb8SEric Biggers }
1572b925acb8SEric Biggers 
15736f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1574a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1575a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1576a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1577ac4acb1fSEric Biggers 	.get_dummy_policy	= ext4_get_dummy_policy,
1578a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1579e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1580b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1581b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1582a7550b30SJaegeuk Kim };
1583a7550b30SJaegeuk Kim #endif
1584a7550b30SJaegeuk Kim 
1585ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1586d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1587689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1588ac27a0ecSDave Kleikamp 
1589617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1590617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1591617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1592617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1593617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
15946f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
15958c54ca9cSAl Viro 			 const struct path *path);
1596617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1597617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1598ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1599617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1600ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
16017c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
16027c319d32SAditya Kali 			     unsigned int flags);
16037c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1604ac27a0ecSDave Kleikamp 
160596c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
160696c7e0d9SJan Kara {
160796c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
160896c7e0d9SJan Kara }
160996c7e0d9SJan Kara 
161061e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
161160e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1612617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1613617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1614617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1615617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1616a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1617a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1618a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1619040cb378SLi Xi 	.get_projid		= ext4_get_projid,
16207a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1621ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1622ac27a0ecSDave Kleikamp };
1623ac27a0ecSDave Kleikamp 
16240d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1625617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1626ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1627287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
16280a240339SJan Kara 	.get_state	= dquot_get_state,
1629287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1630287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
16316332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
16326332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1633ac27a0ecSDave Kleikamp };
1634ac27a0ecSDave Kleikamp #endif
1635ac27a0ecSDave Kleikamp 
1636ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1637617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
163894053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1639617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1640617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1641617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
16427ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
16430930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1644617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1645617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1646c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1647c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1648617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1649617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1650617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1651ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1652617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1653617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
165496c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1655ac27a0ecSDave Kleikamp #endif
1656c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1657ac27a0ecSDave Kleikamp };
1658ac27a0ecSDave Kleikamp 
165939655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
16601b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
16611b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1662617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1663fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1664ac27a0ecSDave Kleikamp };
1665ac27a0ecSDave Kleikamp 
1666ac27a0ecSDave Kleikamp enum {
1667ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1668ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
166972578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1670ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
167172578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1672ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1673ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1674ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
16756ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
16764f74d15fSEric Biggers 	Opt_inlinecrypt,
1677ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
16785a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1679ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
16809cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
16819cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1682327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1683327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1684670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
16851449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
16865328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1687744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1688fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1689cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
169099c880deSHarshad Shirwadkar 	Opt_prefetch_block_bitmaps,
16918016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
169299c880deSHarshad Shirwadkar 	Opt_fc_debug_max_replay, Opt_fc_debug_force
16938016e29fSHarshad Shirwadkar #endif
1694ac27a0ecSDave Kleikamp };
1695ac27a0ecSDave Kleikamp 
1696a447c093SSteven Whitehouse static const match_table_t tokens = {
1697ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1698ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1699ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1700ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1701ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1702ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1703ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1704ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1705ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1706ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1707ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1708ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1709ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1710ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
171172578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
171272578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1713ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1714ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1715ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1716ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1717e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
17185a916be1STheodore Ts'o 	{Opt_noload, "noload"},
171972578c33STheodore Ts'o 	{Opt_removed, "nobh"},
172072578c33STheodore Ts'o 	{Opt_removed, "bh"},
1721ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
172230773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
172330773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1724ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1725ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1726818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1727c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1728818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1729ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1730ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1731ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1732ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
17335bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
17345bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1735ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1736ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1737ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1738ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1739ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1740ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
17415a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1742ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1743ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1744ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1745ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
174649da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1747ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
174806705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
174906705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
175025ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1751923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
17529cb20f94SIra Weiny 	{Opt_dax_always, "dax=always"},
17539cb20f94SIra Weiny 	{Opt_dax_inode, "dax=inode"},
17549cb20f94SIra Weiny 	{Opt_dax_never, "dax=never"},
1755c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
175664769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1757327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1758327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1759a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1760a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1761670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1762dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
176336ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
176436ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
17656fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
17666fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1767240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1768b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1769afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
177006705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
177106705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1772744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1773244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1774744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
17755328e635SEric Sandeen 	{Opt_discard, "discard"},
17765328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1777fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1778fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1779fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
17808016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
178199c880deSHarshad Shirwadkar 	{Opt_fc_debug_force, "fc_debug_force"},
17828016e29fSHarshad Shirwadkar 	{Opt_fc_debug_max_replay, "fc_debug_max_replay=%u"},
17838016e29fSHarshad Shirwadkar #endif
1784df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1785ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
17866ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
17874f74d15fSEric Biggers 	{Opt_inlinecrypt, "inlinecrypt"},
1788cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1789cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
17903d392b26STheodore Ts'o 	{Opt_prefetch_block_bitmaps, "prefetch_block_bitmaps"},
1791c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1792c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1793c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1794c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1795c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1796f3f12faaSJosef Bacik 	{Opt_err, NULL},
1797ac27a0ecSDave Kleikamp };
1798ac27a0ecSDave Kleikamp 
1799617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1800ac27a0ecSDave Kleikamp {
1801617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1802ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1803ac27a0ecSDave Kleikamp 
1804ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1805ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
18060b8e58a1SAndreas Dilger 
1807ac27a0ecSDave Kleikamp 	options += 3;
18080b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1809ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1810ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
18114776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1812ac27a0ecSDave Kleikamp 		       (char *) *data);
1813ac27a0ecSDave Kleikamp 		return 1;
1814ac27a0ecSDave Kleikamp 	}
1815ac27a0ecSDave Kleikamp 	if (*options == ',')
1816ac27a0ecSDave Kleikamp 		options++;
1817ac27a0ecSDave Kleikamp 	*data = (void *) options;
18180b8e58a1SAndreas Dilger 
1819ac27a0ecSDave Kleikamp 	return sb_block;
1820ac27a0ecSDave Kleikamp }
1821ac27a0ecSDave Kleikamp 
1822b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1823d6006186SEric Biggers static const char deprecated_msg[] =
1824d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1825437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1826b3881f74STheodore Ts'o 
182756c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
182856c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
182956c50f11SDmitry Monakhov {
183056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
183133458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
183203dafb5fSChen Gang 	int ret = -1;
183356c50f11SDmitry Monakhov 
183433458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
183556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
183656c50f11SDmitry Monakhov 			"Cannot change journaled "
183756c50f11SDmitry Monakhov 			"quota options when quota turned on");
183857f73c2cSTheodore Ts'o 		return -1;
183956c50f11SDmitry Monakhov 	}
1840e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1841c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1842c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1843c325a67cSTheodore Ts'o 		return 1;
1844262b4662SJan Kara 	}
184556c50f11SDmitry Monakhov 	qname = match_strdup(args);
184656c50f11SDmitry Monakhov 	if (!qname) {
184756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
184856c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
184957f73c2cSTheodore Ts'o 		return -1;
185056c50f11SDmitry Monakhov 	}
185133458eabSTheodore Ts'o 	if (old_qname) {
185233458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
185303dafb5fSChen Gang 			ret = 1;
185403dafb5fSChen Gang 		else
185556c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
185603dafb5fSChen Gang 				 "%s quota file already specified",
185703dafb5fSChen Gang 				 QTYPE2NAME(qtype));
185803dafb5fSChen Gang 		goto errout;
185956c50f11SDmitry Monakhov 	}
186003dafb5fSChen Gang 	if (strchr(qname, '/')) {
186156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
186256c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
186303dafb5fSChen Gang 		goto errout;
186456c50f11SDmitry Monakhov 	}
186533458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1866fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
186756c50f11SDmitry Monakhov 	return 1;
186803dafb5fSChen Gang errout:
186903dafb5fSChen Gang 	kfree(qname);
187003dafb5fSChen Gang 	return ret;
187156c50f11SDmitry Monakhov }
187256c50f11SDmitry Monakhov 
187356c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
187456c50f11SDmitry Monakhov {
187556c50f11SDmitry Monakhov 
187656c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
187733458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
187856c50f11SDmitry Monakhov 
187933458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
188056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
188156c50f11SDmitry Monakhov 			" when quota turned on");
188257f73c2cSTheodore Ts'o 		return -1;
188356c50f11SDmitry Monakhov 	}
188433458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
188533458eabSTheodore Ts'o 	synchronize_rcu();
188633458eabSTheodore Ts'o 	kfree(old_qname);
188756c50f11SDmitry Monakhov 	return 1;
188856c50f11SDmitry Monakhov }
188956c50f11SDmitry Monakhov #endif
189056c50f11SDmitry Monakhov 
189126092bf5STheodore Ts'o #define MOPT_SET	0x0001
189226092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
189326092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
189426092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
189526092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
189626092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
189726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
189826092bf5STheodore Ts'o #define MOPT_Q		0
189926092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
190026092bf5STheodore Ts'o #else
190126092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
190226092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
190326092bf5STheodore Ts'o #endif
190426092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
19058dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
19068dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
19078dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1908ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
19099cb20f94SIra Weiny #define MOPT_SKIP	0x0800
1910995a3ed6SHarshad Shirwadkar #define	MOPT_2		0x1000
191126092bf5STheodore Ts'o 
191226092bf5STheodore Ts'o static const struct mount_opts {
191326092bf5STheodore Ts'o 	int	token;
191426092bf5STheodore Ts'o 	int	mount_opt;
191526092bf5STheodore Ts'o 	int	flags;
191626092bf5STheodore Ts'o } ext4_mount_opts[] = {
191726092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
191826092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
191926092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
192026092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
192126092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
192226092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
19238dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
19248dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
19258dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
19268dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
192726092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
192826092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
19298dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
19308dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
19318dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
193259d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1933327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1934327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1935c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1936c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
19378dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
19381e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
193926092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
19408dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
19411e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
19428dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
194326092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
194426092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
194526092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
19468dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
19477915a861SAles Novak 	 MOPT_NO_EXT2},
19488dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
19497915a861SAles Novak 	 MOPT_NO_EXT2},
195026092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
195126092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
195226092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
195326092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
195426092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
195526092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
195626092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
195726092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
195826092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
195926092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
19609cb20f94SIra Weiny 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
19619cb20f94SIra Weiny 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
19629cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
19639cb20f94SIra Weiny 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
19649cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
19659cb20f94SIra Weiny 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
19669cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
196726092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
19680efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
19690efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
19705ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
19715ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
19725ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
19738dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
19748dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
19758dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
19768dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
197726092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
197826092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
197926092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
198026092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
198126092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
198226092bf5STheodore Ts'o #else
198326092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
198426092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
198526092bf5STheodore Ts'o #endif
198626092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
198726092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1988670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
198926092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
199026092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
199126092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
199226092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
199326092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
199449da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
199549da9392SJan Kara 							MOPT_SET | MOPT_Q},
199626092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
199749da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
199849da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
1999174fe5baSKaixu Xia 	{Opt_usrjquota, 0, MOPT_Q | MOPT_STRING},
2000174fe5baSKaixu Xia 	{Opt_grpjquota, 0, MOPT_Q | MOPT_STRING},
200126092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
200226092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
200326092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
200426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
200526092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
2006df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
2007ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, 0, MOPT_STRING},
2008cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
20093d392b26STheodore Ts'o 	{Opt_prefetch_block_bitmaps, EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS,
20103d392b26STheodore Ts'o 	 MOPT_SET},
201199c880deSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
20120f0672ffSHarshad Shirwadkar 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
20130f0672ffSHarshad Shirwadkar 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
20148016e29fSHarshad Shirwadkar 	{Opt_fc_debug_max_replay, 0, MOPT_GTE0},
20158016e29fSHarshad Shirwadkar #endif
201626092bf5STheodore Ts'o 	{Opt_err, 0, 0}
201726092bf5STheodore Ts'o };
201826092bf5STheodore Ts'o 
2019c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
2020c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
2021c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
2022c83ad55eSGabriel Krisman Bertazi 	char *name;
2023c83ad55eSGabriel Krisman Bertazi 	char *version;
2024c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
2025c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
2026c83ad55eSGabriel Krisman Bertazi };
2027c83ad55eSGabriel Krisman Bertazi 
2028c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
2029c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
2030c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
2031c83ad55eSGabriel Krisman Bertazi {
2032c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
2033c83ad55eSGabriel Krisman Bertazi 	int i;
2034c83ad55eSGabriel Krisman Bertazi 
2035c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
2036c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
2037c83ad55eSGabriel Krisman Bertazi 			break;
2038c83ad55eSGabriel Krisman Bertazi 
2039c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
2040c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
2041c83ad55eSGabriel Krisman Bertazi 
2042c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
2043c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
2044c83ad55eSGabriel Krisman Bertazi 
2045c83ad55eSGabriel Krisman Bertazi 	return 0;
2046c83ad55eSGabriel Krisman Bertazi }
2047c83ad55eSGabriel Krisman Bertazi #endif
2048c83ad55eSGabriel Krisman Bertazi 
2049ed318a6cSEric Biggers static int ext4_set_test_dummy_encryption(struct super_block *sb,
2050ed318a6cSEric Biggers 					  const char *opt,
2051ed318a6cSEric Biggers 					  const substring_t *arg,
2052ed318a6cSEric Biggers 					  bool is_remount)
2053ed318a6cSEric Biggers {
2054ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
2055ed318a6cSEric Biggers 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2056ed318a6cSEric Biggers 	int err;
2057ed318a6cSEric Biggers 
2058ed318a6cSEric Biggers 	/*
2059ed318a6cSEric Biggers 	 * This mount option is just for testing, and it's not worthwhile to
2060ed318a6cSEric Biggers 	 * implement the extra complexity (e.g. RCU protection) that would be
2061ed318a6cSEric Biggers 	 * needed to allow it to be set or changed during remount.  We do allow
2062ed318a6cSEric Biggers 	 * it to be specified during remount, but only if there is no change.
2063ed318a6cSEric Biggers 	 */
2064ac4acb1fSEric Biggers 	if (is_remount && !sbi->s_dummy_enc_policy.policy) {
2065ed318a6cSEric Biggers 		ext4_msg(sb, KERN_WARNING,
2066ed318a6cSEric Biggers 			 "Can't set test_dummy_encryption on remount");
2067ed318a6cSEric Biggers 		return -1;
2068ed318a6cSEric Biggers 	}
2069c8c868abSEric Biggers 	err = fscrypt_set_test_dummy_encryption(sb, arg->from,
2070ac4acb1fSEric Biggers 						&sbi->s_dummy_enc_policy);
2071ed318a6cSEric Biggers 	if (err) {
2072ed318a6cSEric Biggers 		if (err == -EEXIST)
2073ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2074ed318a6cSEric Biggers 				 "Can't change test_dummy_encryption on remount");
2075ed318a6cSEric Biggers 		else if (err == -EINVAL)
2076ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2077ed318a6cSEric Biggers 				 "Value of option \"%s\" is unrecognized", opt);
2078ed318a6cSEric Biggers 		else
2079ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
2080ed318a6cSEric Biggers 				 "Error processing option \"%s\" [%d]",
2081ed318a6cSEric Biggers 				 opt, err);
2082ed318a6cSEric Biggers 		return -1;
2083ed318a6cSEric Biggers 	}
2084ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2085ed318a6cSEric Biggers #else
2086ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING,
2087ed318a6cSEric Biggers 		 "Test dummy encryption mount option ignored");
2088ed318a6cSEric Biggers #endif
2089ed318a6cSEric Biggers 	return 1;
2090ed318a6cSEric Biggers }
2091ed318a6cSEric Biggers 
209226092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
209326092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
209426092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
209526092bf5STheodore Ts'o {
209626092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
209726092bf5STheodore Ts'o 	const struct mount_opts *m;
209808cefc7aSEric W. Biederman 	kuid_t uid;
209908cefc7aSEric W. Biederman 	kgid_t gid;
210026092bf5STheodore Ts'o 	int arg = 0;
210126092bf5STheodore Ts'o 
210257f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
210357f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
210457f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
210557f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
210657f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
210757f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
210857f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
210957f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
211057f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
211157f73c2cSTheodore Ts'o #endif
211226092bf5STheodore Ts'o 	switch (token) {
2113f7048605STheodore Ts'o 	case Opt_noacl:
2114f7048605STheodore Ts'o 	case Opt_nouser_xattr:
2115f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
2116f7048605STheodore Ts'o 		break;
211726092bf5STheodore Ts'o 	case Opt_sb:
211826092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
211926092bf5STheodore Ts'o 	case Opt_removed:
21205f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
212126092bf5STheodore Ts'o 		return 1;
212226092bf5STheodore Ts'o 	case Opt_abort:
21239b5f6c9bSHarshad Shirwadkar 		ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
212426092bf5STheodore Ts'o 		return 1;
212526092bf5STheodore Ts'o 	case Opt_i_version:
2126357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
212726092bf5STheodore Ts'o 		return 1;
2128a26f4992STheodore Ts'o 	case Opt_lazytime:
21291751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
2130a26f4992STheodore Ts'o 		return 1;
2131a26f4992STheodore Ts'o 	case Opt_nolazytime:
21321751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
2133a26f4992STheodore Ts'o 		return 1;
21344f74d15fSEric Biggers 	case Opt_inlinecrypt:
21354f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
21364f74d15fSEric Biggers 		sb->s_flags |= SB_INLINECRYPT;
21374f74d15fSEric Biggers #else
21384f74d15fSEric Biggers 		ext4_msg(sb, KERN_ERR, "inline encryption not supported");
21394f74d15fSEric Biggers #endif
21404f74d15fSEric Biggers 		return 1;
214126092bf5STheodore Ts'o 	}
214226092bf5STheodore Ts'o 
21435f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
21445f3633e3SJan Kara 		if (token == m->token)
21455f3633e3SJan Kara 			break;
21465f3633e3SJan Kara 
21475f3633e3SJan Kara 	if (m->token == Opt_err) {
21485f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
21495f3633e3SJan Kara 			 "or missing value", opt);
21505f3633e3SJan Kara 		return -1;
21515f3633e3SJan Kara 	}
21525f3633e3SJan Kara 
21538dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
21548dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
21558dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
21568dc0aa8cSTheodore Ts'o 		return -1;
21578dc0aa8cSTheodore Ts'o 	}
21588dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
21598dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
21608dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
21618dc0aa8cSTheodore Ts'o 		return -1;
21628dc0aa8cSTheodore Ts'o 	}
21638dc0aa8cSTheodore Ts'o 
2164ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
21650efb3b23SJan Kara 		return -1;
216626092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
216726092bf5STheodore Ts'o 		return -1;
2168c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
2169c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
217026092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
21711e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
21721e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
2173c93cf2d7SDmitry Monakhov 		} else
2174c93cf2d7SDmitry Monakhov 			return -1;
2175c93cf2d7SDmitry Monakhov 	}
217626092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
217726092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
217826092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
217926092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
218026092bf5STheodore Ts'o 			 "options when quota turned on");
218126092bf5STheodore Ts'o 		return -1;
218226092bf5STheodore Ts'o 	}
218326092bf5STheodore Ts'o 
218426092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
218526092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
218626092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
218726092bf5STheodore Ts'o 		if (arg == 0)
218826092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
21899ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
21909ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
21919ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
21929ba55543Szhangyi (F) 				 "must be smaller than %d",
21939ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
21949ba55543Szhangyi (F) 			return -1;
21959ba55543Szhangyi (F) 		}
219626092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
2197670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
21989803387cSTheodore Ts'o 		if ((arg & 1) ||
21999803387cSTheodore Ts'o 		    (arg < 4) ||
22009803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
22019803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
22029803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
22039803387cSTheodore Ts'o 			return -1;
22049803387cSTheodore Ts'o 		}
2205670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
220626092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
220726092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
220826092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
220926092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
221026092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
2211e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2212e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
2213e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2214e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
221526092bf5STheodore Ts'o 			return -1;
221626092bf5STheodore Ts'o 		}
221726092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
221826092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
221926092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
222026092bf5STheodore Ts'o 		if (!args->from)
222126092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
222226092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
2223df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
2224df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
22258016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
22268016e29fSHarshad Shirwadkar 	} else if (token == Opt_fc_debug_max_replay) {
22278016e29fSHarshad Shirwadkar 		sbi->s_fc_debug_max_replay = arg;
22288016e29fSHarshad Shirwadkar #endif
222926092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
223026092bf5STheodore Ts'o 		sbi->s_stripe = arg;
22310efb3b23SJan Kara 	} else if (token == Opt_resuid) {
22320efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
22330efb3b23SJan Kara 		if (!uid_valid(uid)) {
22345f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
22350efb3b23SJan Kara 			return -1;
22360efb3b23SJan Kara 		}
22370efb3b23SJan Kara 		sbi->s_resuid = uid;
22380efb3b23SJan Kara 	} else if (token == Opt_resgid) {
22390efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
22400efb3b23SJan Kara 		if (!gid_valid(gid)) {
22415f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
22420efb3b23SJan Kara 			return -1;
22430efb3b23SJan Kara 		}
22440efb3b23SJan Kara 		sbi->s_resgid = gid;
22450efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
22460efb3b23SJan Kara 		if (is_remount) {
22470efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
22480efb3b23SJan Kara 				 "Cannot specify journal on remount");
22490efb3b23SJan Kara 			return -1;
22500efb3b23SJan Kara 		}
22510efb3b23SJan Kara 		*journal_devnum = arg;
2252ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2253ad4eec61SEric Sandeen 		char *journal_path;
2254ad4eec61SEric Sandeen 		struct inode *journal_inode;
2255ad4eec61SEric Sandeen 		struct path path;
2256ad4eec61SEric Sandeen 		int error;
2257ad4eec61SEric Sandeen 
2258ad4eec61SEric Sandeen 		if (is_remount) {
2259ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2260ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2261ad4eec61SEric Sandeen 			return -1;
2262ad4eec61SEric Sandeen 		}
2263ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2264ad4eec61SEric Sandeen 		if (!journal_path) {
2265ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2266ad4eec61SEric Sandeen 				"journal device string");
2267ad4eec61SEric Sandeen 			return -1;
2268ad4eec61SEric Sandeen 		}
2269ad4eec61SEric Sandeen 
2270ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2271ad4eec61SEric Sandeen 		if (error) {
2272ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2273ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2274ad4eec61SEric Sandeen 			kfree(journal_path);
2275ad4eec61SEric Sandeen 			return -1;
2276ad4eec61SEric Sandeen 		}
2277ad4eec61SEric Sandeen 
22782b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2279ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2280ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2281ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2282ad4eec61SEric Sandeen 			path_put(&path);
2283ad4eec61SEric Sandeen 			kfree(journal_path);
2284ad4eec61SEric Sandeen 			return -1;
2285ad4eec61SEric Sandeen 		}
2286ad4eec61SEric Sandeen 
2287ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2288ad4eec61SEric Sandeen 		path_put(&path);
2289ad4eec61SEric Sandeen 		kfree(journal_path);
22900efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
22910efb3b23SJan Kara 		if (arg > 7) {
22925f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
22930efb3b23SJan Kara 				 " (must be 0-7)");
22940efb3b23SJan Kara 			return -1;
22950efb3b23SJan Kara 		}
22960efb3b23SJan Kara 		*journal_ioprio =
22970efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
22986ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2299ed318a6cSEric Biggers 		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2300ed318a6cSEric Biggers 						      is_remount);
230126092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
230226092bf5STheodore Ts'o 		if (is_remount) {
230326092bf5STheodore Ts'o 			if (!sbi->s_journal)
230426092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
23055f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
230626092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
230726092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
230826092bf5STheodore Ts'o 				return -1;
230926092bf5STheodore Ts'o 			}
231026092bf5STheodore Ts'o 		} else {
231126092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
231226092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
231326092bf5STheodore Ts'o 		}
231426092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
231526092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
231626092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
231726092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
23185f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
23195f3633e3SJan Kara 				 "quota options when quota turned on");
232026092bf5STheodore Ts'o 			return -1;
232126092bf5STheodore Ts'o 		}
2322e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2323c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2324c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2325262b4662SJan Kara 				 "when QUOTA feature is enabled");
2326c325a67cSTheodore Ts'o 			return 1;
2327262b4662SJan Kara 		}
232826092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
232926092bf5STheodore Ts'o #endif
23309cb20f94SIra Weiny 	} else if (token == Opt_dax || token == Opt_dax_always ||
23319cb20f94SIra Weiny 		   token == Opt_dax_inode || token == Opt_dax_never) {
2332ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
23339cb20f94SIra Weiny 		switch (token) {
23349cb20f94SIra Weiny 		case Opt_dax:
23359cb20f94SIra Weiny 		case Opt_dax_always:
2336829b37b8STheodore Ts'o 			if (is_remount &&
2337829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2338829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2339829b37b8STheodore Ts'o 			fail_dax_change_remount:
2340829b37b8STheodore Ts'o 				ext4_msg(sb, KERN_ERR, "can't change "
2341829b37b8STheodore Ts'o 					 "dax mount option while remounting");
2342829b37b8STheodore Ts'o 				return -1;
2343829b37b8STheodore Ts'o 			}
2344829b37b8STheodore Ts'o 			if (is_remount &&
2345829b37b8STheodore Ts'o 			    (test_opt(sb, DATA_FLAGS) ==
2346829b37b8STheodore Ts'o 			     EXT4_MOUNT_JOURNAL_DATA)) {
2347829b37b8STheodore Ts'o 				    ext4_msg(sb, KERN_ERR, "can't mount with "
2348829b37b8STheodore Ts'o 					     "both data=journal and dax");
2349829b37b8STheodore Ts'o 				    return -1;
2350829b37b8STheodore Ts'o 			}
2351ef83b6e8SDan Williams 			ext4_msg(sb, KERN_WARNING,
2352ef83b6e8SDan Williams 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
23539cb20f94SIra Weiny 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
23549cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
23559cb20f94SIra Weiny 			break;
23569cb20f94SIra Weiny 		case Opt_dax_never:
2357829b37b8STheodore Ts'o 			if (is_remount &&
2358829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2359829b37b8STheodore Ts'o 			     (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2360829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
23619cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
23629cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
23639cb20f94SIra Weiny 			break;
23649cb20f94SIra Weiny 		case Opt_dax_inode:
2365829b37b8STheodore Ts'o 			if (is_remount &&
2366829b37b8STheodore Ts'o 			    ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2367829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2368829b37b8STheodore Ts'o 			     !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2369829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
23709cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
23719cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
23729cb20f94SIra Weiny 			/* Strictly for printing options */
23739cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
23749cb20f94SIra Weiny 			break;
23759cb20f94SIra Weiny 		}
2376ef83b6e8SDan Williams #else
2377923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
23789cb20f94SIra Weiny 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
23799cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2380923ae0ffSRoss Zwisler 		return -1;
2381923ae0ffSRoss Zwisler #endif
23827915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
23837915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
23847915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
23857915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
238626092bf5STheodore Ts'o 	} else {
238726092bf5STheodore Ts'o 		if (!args->from)
238826092bf5STheodore Ts'o 			arg = 1;
238926092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
239026092bf5STheodore Ts'o 			arg = !arg;
239126092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
239226092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
239326092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
239426092bf5STheodore Ts'o 			WARN_ON(1);
239526092bf5STheodore Ts'o 			return -1;
239626092bf5STheodore Ts'o 		}
2397995a3ed6SHarshad Shirwadkar 		if (m->flags & MOPT_2) {
2398995a3ed6SHarshad Shirwadkar 			if (arg != 0)
2399995a3ed6SHarshad Shirwadkar 				sbi->s_mount_opt2 |= m->mount_opt;
2400995a3ed6SHarshad Shirwadkar 			else
2401995a3ed6SHarshad Shirwadkar 				sbi->s_mount_opt2 &= ~m->mount_opt;
2402995a3ed6SHarshad Shirwadkar 		} else {
240326092bf5STheodore Ts'o 			if (arg != 0)
240426092bf5STheodore Ts'o 				sbi->s_mount_opt |= m->mount_opt;
240526092bf5STheodore Ts'o 			else
240626092bf5STheodore Ts'o 				sbi->s_mount_opt &= ~m->mount_opt;
240726092bf5STheodore Ts'o 		}
2408995a3ed6SHarshad Shirwadkar 	}
240926092bf5STheodore Ts'o 	return 1;
241026092bf5STheodore Ts'o }
241126092bf5STheodore Ts'o 
2412ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2413c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2414b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2415661aa520SEric Sandeen 			 int is_remount)
2416ac27a0ecSDave Kleikamp {
24171e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
241833458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2419ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
242026092bf5STheodore Ts'o 	int token;
2421ac27a0ecSDave Kleikamp 
2422ac27a0ecSDave Kleikamp 	if (!options)
2423ac27a0ecSDave Kleikamp 		return 1;
2424ac27a0ecSDave Kleikamp 
2425ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2426ac27a0ecSDave Kleikamp 		if (!*p)
2427ac27a0ecSDave Kleikamp 			continue;
242815121c18SEric Sandeen 		/*
242915121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
243015121c18SEric Sandeen 		 * found; some options take optional arguments.
243115121c18SEric Sandeen 		 */
2432caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2433ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
243426092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
243526092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2436ac27a0ecSDave Kleikamp 			return 0;
2437ac27a0ecSDave Kleikamp 	}
2438ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
243949da9392SJan Kara 	/*
244049da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
244149da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
244249da9392SJan Kara 	 * to support legacy quotas in quota files.
244349da9392SJan Kara 	 */
244449da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
244549da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
244649da9392SJan Kara 			 "Cannot enable project quota enforcement.");
244749da9392SJan Kara 		return 0;
244849da9392SJan Kara 	}
244933458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
245033458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
245133458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
245233458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2453fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2454ac27a0ecSDave Kleikamp 
245533458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2456fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2457ac27a0ecSDave Kleikamp 
245856c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2459b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2460b31e1552SEric Sandeen 					"format mixing");
2461ac27a0ecSDave Kleikamp 			return 0;
2462ac27a0ecSDave Kleikamp 		}
2463ac27a0ecSDave Kleikamp 
2464ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2465b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2466b31e1552SEric Sandeen 					"not specified");
2467ac27a0ecSDave Kleikamp 			return 0;
2468ac27a0ecSDave Kleikamp 		}
2469ac27a0ecSDave Kleikamp 	}
2470ac27a0ecSDave Kleikamp #endif
2471626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2472626b035bSRitesh Harjani 		int blocksize =
2473626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2474626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2475626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2476626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2477626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2478626b035bSRitesh Harjani 	}
2479ac27a0ecSDave Kleikamp 	return 1;
2480ac27a0ecSDave Kleikamp }
2481ac27a0ecSDave Kleikamp 
24822adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
24832adf6da8STheodore Ts'o 					   struct super_block *sb)
24842adf6da8STheodore Ts'o {
24852adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
24862adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
248733458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
24882adf6da8STheodore Ts'o 
24892adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
24902adf6da8STheodore Ts'o 		char *fmtname = "";
24912adf6da8STheodore Ts'o 
24922adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
24932adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
24942adf6da8STheodore Ts'o 			fmtname = "vfsold";
24952adf6da8STheodore Ts'o 			break;
24962adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
24972adf6da8STheodore Ts'o 			fmtname = "vfsv0";
24982adf6da8STheodore Ts'o 			break;
24992adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
25002adf6da8STheodore Ts'o 			fmtname = "vfsv1";
25012adf6da8STheodore Ts'o 			break;
25022adf6da8STheodore Ts'o 		}
25032adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
25042adf6da8STheodore Ts'o 	}
25052adf6da8STheodore Ts'o 
250633458eabSTheodore Ts'o 	rcu_read_lock();
250733458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
250833458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
250933458eabSTheodore Ts'o 	if (usr_qf_name)
251033458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
251133458eabSTheodore Ts'o 	if (grp_qf_name)
251233458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
251333458eabSTheodore Ts'o 	rcu_read_unlock();
25142adf6da8STheodore Ts'o #endif
25152adf6da8STheodore Ts'o }
25162adf6da8STheodore Ts'o 
25175a916be1STheodore Ts'o static const char *token2str(int token)
25185a916be1STheodore Ts'o {
251950df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
25205a916be1STheodore Ts'o 
25215a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
25225a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
25235a916be1STheodore Ts'o 			break;
25245a916be1STheodore Ts'o 	return t->pattern;
25255a916be1STheodore Ts'o }
25265a916be1STheodore Ts'o 
25272adf6da8STheodore Ts'o /*
25282adf6da8STheodore Ts'o  * Show an option if
25292adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
25302adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
25312adf6da8STheodore Ts'o  */
253266acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
253366acdcf4STheodore Ts'o 			      int nodefs)
25342adf6da8STheodore Ts'o {
25352adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
25362adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
253768afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
25385a916be1STheodore Ts'o 	const struct mount_opts *m;
253966acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
25402adf6da8STheodore Ts'o 
254166acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
254266acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
25432adf6da8STheodore Ts'o 
25442adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
25455a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
25465a916be1STheodore Ts'o 
25475a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
25485a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
25495a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
25509cb20f94SIra Weiny 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
25515a916be1STheodore Ts'o 			continue;
255268afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
25535a916be1STheodore Ts'o 			continue; /* skip if same as the default */
25545a916be1STheodore Ts'o 		if ((want_set &&
25555a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
25565a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
25575a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
25585a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
25595a916be1STheodore Ts'o 	}
25605a916be1STheodore Ts'o 
256108cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
25625a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
256308cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
256408cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
256508cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
25665a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
256708cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
256808cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
256966acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
25705a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
25715a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
25722adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
25735a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
25742adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
25755a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
257666acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
25775a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
257866acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
25795a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
258066acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
25815a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2582357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
25835a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
258466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
25855a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
258668afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
258768afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
25882adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
25895a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
25902adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
25915a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
25922adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
25935a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
25945a916be1STheodore Ts'o 	}
259566acdcf4STheodore Ts'o 	if (nodefs ||
259666acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
25975a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
25982adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
25992adf6da8STheodore Ts'o 
2600ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
260166acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
26025a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2603df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2604df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
26057915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
26067915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2607ed318a6cSEric Biggers 
2608ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
26092adf6da8STheodore Ts'o 
26104f74d15fSEric Biggers 	if (sb->s_flags & SB_INLINECRYPT)
26114f74d15fSEric Biggers 		SEQ_OPTS_PUTS("inlinecrypt");
26124f74d15fSEric Biggers 
26139cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
26149cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
26159cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
26169cb20f94SIra Weiny 		else
26179cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
26189cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
26199cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
26209cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
26219cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
26229cb20f94SIra Weiny 	}
26232adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
26242adf6da8STheodore Ts'o 	return 0;
26252adf6da8STheodore Ts'o }
26262adf6da8STheodore Ts'o 
262766acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
262866acdcf4STheodore Ts'o {
262966acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
263066acdcf4STheodore Ts'o }
263166acdcf4STheodore Ts'o 
2632ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
263366acdcf4STheodore Ts'o {
263466acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
263566acdcf4STheodore Ts'o 	int rc;
263666acdcf4STheodore Ts'o 
2637bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
263866acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
263966acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
264066acdcf4STheodore Ts'o 	return rc;
264166acdcf4STheodore Ts'o }
264266acdcf4STheodore Ts'o 
2643617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2644ac27a0ecSDave Kleikamp 			    int read_only)
2645ac27a0ecSDave Kleikamp {
2646617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2647c89128a0SJaegeuk Kim 	int err = 0;
2648ac27a0ecSDave Kleikamp 
2649617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2650b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2651b31e1552SEric Sandeen 			 "forcing read-only mode");
2652c89128a0SJaegeuk Kim 		err = -EROFS;
26535adaccacSyangerkun 		goto done;
2654ac27a0ecSDave Kleikamp 	}
2655ac27a0ecSDave Kleikamp 	if (read_only)
2656281b5995STheodore Ts'o 		goto done;
2657617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2658b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2659b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2660c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2661b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2662b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2663b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2664ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2665ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2666ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2667b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2668b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2669b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2670ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
26716a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
26726a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2673b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2674b31e1552SEric Sandeen 			 "warning: checktime reached, "
2675b31e1552SEric Sandeen 			 "running e2fsck is recommended");
26760390131bSFrank Mayhar 	if (!sbi->s_journal)
2677216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2678ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2679617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2680e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
26816a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
26820390131bSFrank Mayhar 	if (sbi->s_journal)
2683e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2684ac27a0ecSDave Kleikamp 
26854392fbc4SJan Kara 	err = ext4_commit_super(sb);
2686281b5995STheodore Ts'o done:
2687ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2688a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2689a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2690ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2691ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2692617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2693617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2694a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2695ac27a0ecSDave Kleikamp 
26967abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2697c89128a0SJaegeuk Kim 	return err;
2698ac27a0ecSDave Kleikamp }
2699ac27a0ecSDave Kleikamp 
2700117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2701117fff10STheodore Ts'o {
2702117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
27037c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
270437b0b6b8SDan Carpenter 	int size, i, j;
2705117fff10STheodore Ts'o 
2706117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2707117fff10STheodore Ts'o 		return 0;
2708117fff10STheodore Ts'o 
2709117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2710117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2711117fff10STheodore Ts'o 		return 0;
2712117fff10STheodore Ts'o 
27137c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
27147c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2715117fff10STheodore Ts'o 	if (!new_groups) {
27167c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
27177c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2718117fff10STheodore Ts'o 		return -ENOMEM;
2719117fff10STheodore Ts'o 	}
27207c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
27217c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
27227c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
27237c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
27247c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
272537b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
272637b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
27277c990728SSuraj Jitindar Singh 			kvfree(new_groups);
27287c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
27297c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
27307c990728SSuraj Jitindar Singh 			return -ENOMEM;
2731117fff10STheodore Ts'o 		}
27327c990728SSuraj Jitindar Singh 	}
27337c990728SSuraj Jitindar Singh 	rcu_read_lock();
27347c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
27357c990728SSuraj Jitindar Singh 	if (old_groups)
27367c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
27377c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
27387c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
27397c990728SSuraj Jitindar Singh 	rcu_read_unlock();
27407c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
27417c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
27427c990728SSuraj Jitindar Singh 	if (old_groups)
27437c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2744117fff10STheodore Ts'o 	return 0;
2745117fff10STheodore Ts'o }
2746117fff10STheodore Ts'o 
2747772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2748772cb7c8SJose R. Santos {
2749772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2750772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
27517c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2752772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2753117fff10STheodore Ts'o 	int i, err;
2754772cb7c8SJose R. Santos 
2755503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2756d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2757772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2758772cb7c8SJose R. Santos 		return 1;
2759772cb7c8SJose R. Santos 	}
2760772cb7c8SJose R. Santos 
2761117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2762117fff10STheodore Ts'o 	if (err)
2763772cb7c8SJose R. Santos 		goto failed;
2764772cb7c8SJose R. Santos 
2765772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
276688b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2767772cb7c8SJose R. Santos 
2768772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
27697c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
27707c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
277190ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
27727c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
27737c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2774772cb7c8SJose R. Santos 	}
2775772cb7c8SJose R. Santos 
2776772cb7c8SJose R. Santos 	return 1;
2777772cb7c8SJose R. Santos failed:
2778772cb7c8SJose R. Santos 	return 0;
2779772cb7c8SJose R. Santos }
2780772cb7c8SJose R. Santos 
2781e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2782717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2783717d50e4SAndreas Dilger {
2784b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2785717d50e4SAndreas Dilger 	__u16 crc = 0;
2786717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2787e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2788717d50e4SAndreas Dilger 
27899aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2790feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2791feb0ab32SDarrick J. Wong 		__u32 csum32;
2792b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2793feb0ab32SDarrick J. Wong 
2794feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2795feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2796b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2797b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2798b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2799b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2800b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2801b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2802b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2803feb0ab32SDarrick J. Wong 
2804feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2805feb0ab32SDarrick J. Wong 		goto out;
2806feb0ab32SDarrick J. Wong 	}
2807feb0ab32SDarrick J. Wong 
2808feb0ab32SDarrick J. Wong 	/* old crc16 code */
2809e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2810813d32f9SDarrick J. Wong 		return 0;
2811813d32f9SDarrick J. Wong 
2812717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2813717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2814717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2815717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2816717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2817e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2818717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2819717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2820717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2821717d50e4SAndreas Dilger 				offset);
2822717d50e4SAndreas Dilger 
2823feb0ab32SDarrick J. Wong out:
2824717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2825717d50e4SAndreas Dilger }
2826717d50e4SAndreas Dilger 
2827feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2828717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2829717d50e4SAndreas Dilger {
2830feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2831e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2832717d50e4SAndreas Dilger 		return 0;
2833717d50e4SAndreas Dilger 
2834717d50e4SAndreas Dilger 	return 1;
2835717d50e4SAndreas Dilger }
2836717d50e4SAndreas Dilger 
2837feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2838feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2839feb0ab32SDarrick J. Wong {
2840feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2841feb0ab32SDarrick J. Wong 		return;
2842e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2843feb0ab32SDarrick J. Wong }
2844feb0ab32SDarrick J. Wong 
2845ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2846bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2847829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2848bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2849ac27a0ecSDave Kleikamp {
2850617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2851617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2852617ba13bSMingming Cao 	ext4_fsblk_t last_block;
285344de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2854bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2855bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2856bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2857ce421581SJose R. Santos 	int flexbg_flag = 0;
2858bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2859ac27a0ecSDave Kleikamp 
2860e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2861ce421581SJose R. Santos 		flexbg_flag = 1;
2862ce421581SJose R. Santos 
2863617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2864ac27a0ecSDave Kleikamp 
2865197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2866197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2867197cd65aSAkinobu Mita 
2868ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2869bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2870ac27a0ecSDave Kleikamp 		else
2871ac27a0ecSDave Kleikamp 			last_block = first_block +
2872617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2873ac27a0ecSDave Kleikamp 
2874bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2875bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2876bfff6873SLukas Czerner 			grp = i;
2877bfff6873SLukas Czerner 
28788fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2879829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2880829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2881829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2882829fa70dSTheodore Ts'o 				 "superblock", i);
288318db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
288418db4b4eSTheodore Ts'o 				return 0;
2885829fa70dSTheodore Ts'o 		}
288677260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
288777260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
288877260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
288977260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
289077260807STheodore Ts'o 				 "block group descriptors", i);
289177260807STheodore Ts'o 			if (!sb_rdonly(sb))
289277260807STheodore Ts'o 				return 0;
289377260807STheodore Ts'o 		}
28942b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2895b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2896a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2897b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2898ac27a0ecSDave Kleikamp 			return 0;
2899ac27a0ecSDave Kleikamp 		}
29008fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2901829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2902829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2903829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2904829fa70dSTheodore Ts'o 				 "superblock", i);
290518db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
290618db4b4eSTheodore Ts'o 				return 0;
2907829fa70dSTheodore Ts'o 		}
290877260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
290977260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
291077260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
291177260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
291277260807STheodore Ts'o 				 "block group descriptors", i);
291377260807STheodore Ts'o 			if (!sb_rdonly(sb))
291477260807STheodore Ts'o 				return 0;
291577260807STheodore Ts'o 		}
29162b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2917b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2918a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2919b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2920ac27a0ecSDave Kleikamp 			return 0;
2921ac27a0ecSDave Kleikamp 		}
29228fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2923829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2924829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2925829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2926829fa70dSTheodore Ts'o 				 "superblock", i);
292718db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
292818db4b4eSTheodore Ts'o 				return 0;
2929829fa70dSTheodore Ts'o 		}
293077260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
293177260807STheodore Ts'o 		    inode_table <= last_bg_block) {
293277260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
293377260807STheodore Ts'o 				 "Inode table for group %u overlaps "
293477260807STheodore Ts'o 				 "block group descriptors", i);
293577260807STheodore Ts'o 			if (!sb_rdonly(sb))
293677260807STheodore Ts'o 				return 0;
293777260807STheodore Ts'o 		}
2938bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
29392b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2940b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2941a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2942b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2943ac27a0ecSDave Kleikamp 			return 0;
2944ac27a0ecSDave Kleikamp 		}
2945955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2946feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2947b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2948b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2949e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2950fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2951bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2952955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2953717d50e4SAndreas Dilger 				return 0;
2954717d50e4SAndreas Dilger 			}
29557ee1ec4cSLi Zefan 		}
2956955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2957ce421581SJose R. Santos 		if (!flexbg_flag)
2958617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2959ac27a0ecSDave Kleikamp 	}
2960bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2961bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2962ac27a0ecSDave Kleikamp 	return 1;
2963ac27a0ecSDave Kleikamp }
2964ac27a0ecSDave Kleikamp 
2965617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2966ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2967ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2968ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2969ac27a0ecSDave Kleikamp  *
2970ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2971ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2972ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2973ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2974ac27a0ecSDave Kleikamp  *
2975ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2976ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2977ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2978617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2979ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2980ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2981ac27a0ecSDave Kleikamp  */
2982617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2983617ba13bSMingming Cao 				struct ext4_super_block *es)
2984ac27a0ecSDave Kleikamp {
2985ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
29862c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2987ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
298895f1fda4Szhangyi (F) 	int quota_update = 0;
2989ac27a0ecSDave Kleikamp 	int i;
2990ac27a0ecSDave Kleikamp #endif
2991ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2992ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2993ac27a0ecSDave Kleikamp 		return;
2994ac27a0ecSDave Kleikamp 	}
2995ac27a0ecSDave Kleikamp 
2996a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2997b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2998b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2999a8f48a95SEric Sandeen 		return;
3000a8f48a95SEric Sandeen 	}
3001a8f48a95SEric Sandeen 
3002d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
3003d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
3004d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
3005d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
3006d39195c3SAmir Goldstein 		return;
3007d39195c3SAmir Goldstein 	}
3008d39195c3SAmir Goldstein 
3009617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
3010c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
30111751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
301284474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
3013ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
3014ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
3015c25f9bc6SEric Sandeen 		}
3016ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
3017ac27a0ecSDave Kleikamp 		return;
3018ac27a0ecSDave Kleikamp 	}
3019ac27a0ecSDave Kleikamp 
30201751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
3021b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
30221751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
3023ac27a0ecSDave Kleikamp 	}
3024ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
3025ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
30261751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
302795f1fda4Szhangyi (F) 
302895f1fda4Szhangyi (F) 	/*
302995f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
303095f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
303195f1fda4Szhangyi (F) 	 */
30321751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
303395f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
303495f1fda4Szhangyi (F) 
303595f1fda4Szhangyi (F) 		if (!ret)
303695f1fda4Szhangyi (F) 			quota_update = 1;
303795f1fda4Szhangyi (F) 		else
303895f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
303995f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
304095f1fda4Szhangyi (F) 	}
304195f1fda4Szhangyi (F) 
304295f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
3043a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
3044617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
3045617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
304695f1fda4Szhangyi (F) 
304795f1fda4Szhangyi (F) 			if (!ret)
304895f1fda4Szhangyi (F) 				quota_update = 1;
304995f1fda4Szhangyi (F) 			else
3050b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
3051b31e1552SEric Sandeen 					"Cannot turn on journaled "
305295f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
3053ac27a0ecSDave Kleikamp 		}
3054ac27a0ecSDave Kleikamp 	}
3055ac27a0ecSDave Kleikamp #endif
3056ac27a0ecSDave Kleikamp 
3057ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
3058ac27a0ecSDave Kleikamp 		struct inode *inode;
3059ac27a0ecSDave Kleikamp 
3060c65d5c6cSVegard Nossum 		/*
3061c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
3062c65d5c6cSVegard Nossum 		 * so, skip the rest.
3063c65d5c6cSVegard Nossum 		 */
3064c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
3065c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
3066c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
3067c65d5c6cSVegard Nossum 			break;
3068c65d5c6cSVegard Nossum 		}
3069c65d5c6cSVegard Nossum 
307097bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
307197bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
3072ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
3073ac27a0ecSDave Kleikamp 			break;
3074ac27a0ecSDave Kleikamp 		}
3075ac27a0ecSDave Kleikamp 
3076617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
3077871a2931SChristoph Hellwig 		dquot_initialize(inode);
3078ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
3079566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
3080b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
3081b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
308246e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
3083e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
3084ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
30855955102cSAl Viro 			inode_lock(inode);
308655f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
30872c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
30882c98eb5eSTheodore Ts'o 			if (ret)
30892c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
30905955102cSAl Viro 			inode_unlock(inode);
3091ac27a0ecSDave Kleikamp 			nr_truncates++;
3092ac27a0ecSDave Kleikamp 		} else {
3093566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
3094b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
3095b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
309646e665e9SHarvey Harrison 					__func__, inode->i_ino);
3097ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
3098ac27a0ecSDave Kleikamp 				  inode->i_ino);
3099ac27a0ecSDave Kleikamp 			nr_orphans++;
3100ac27a0ecSDave Kleikamp 		}
3101ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
3102ac27a0ecSDave Kleikamp 	}
3103ac27a0ecSDave Kleikamp 
3104ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
3105ac27a0ecSDave Kleikamp 
3106ac27a0ecSDave Kleikamp 	if (nr_orphans)
3107b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
3108b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
3109ac27a0ecSDave Kleikamp 	if (nr_truncates)
3110b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
3111b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
3112ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
311395f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
311495f1fda4Szhangyi (F) 	if (quota_update) {
3115a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
311695f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
3117287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
3118ac27a0ecSDave Kleikamp 		}
311995f1fda4Szhangyi (F) 	}
3120ac27a0ecSDave Kleikamp #endif
31211751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
3122ac27a0ecSDave Kleikamp }
31230b8e58a1SAndreas Dilger 
3124cd2291a4SEric Sandeen /*
3125cd2291a4SEric Sandeen  * Maximal extent format file size.
3126cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
3127cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
3128cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
3129cd2291a4SEric Sandeen  * so that won't be a limiting factor.
3130cd2291a4SEric Sandeen  *
3131f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
3132f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
3133f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
3134f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
3135f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3136f17722f9SLukas Czerner  *
3137cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3138cd2291a4SEric Sandeen  */
3139f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
3140cd2291a4SEric Sandeen {
3141cd2291a4SEric Sandeen 	loff_t res;
3142cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
3143cd2291a4SEric Sandeen 
314472deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
314572deb455SChristoph Hellwig 
314672deb455SChristoph Hellwig 	if (!has_huge_files) {
3147cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
3148cd2291a4SEric Sandeen 
3149cd2291a4SEric Sandeen 		/* total blocks in file system block size */
3150cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
3151cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
3152cd2291a4SEric Sandeen 	}
3153cd2291a4SEric Sandeen 
3154f17722f9SLukas Czerner 	/*
3155f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
3156f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
3157f17722f9SLukas Czerner 	 * size
3158f17722f9SLukas Czerner 	 */
3159f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
3160cd2291a4SEric Sandeen 	res <<= blkbits;
3161cd2291a4SEric Sandeen 
3162cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
3163cd2291a4SEric Sandeen 	if (res > upper_limit)
3164cd2291a4SEric Sandeen 		res = upper_limit;
3165cd2291a4SEric Sandeen 
3166cd2291a4SEric Sandeen 	return res;
3167cd2291a4SEric Sandeen }
3168ac27a0ecSDave Kleikamp 
3169ac27a0ecSDave Kleikamp /*
3170cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
31710fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
31720fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
3173ac27a0ecSDave Kleikamp  */
3174f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
3175ac27a0ecSDave Kleikamp {
3176617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
31770fc1b451SAneesh Kumar K.V 	int meta_blocks;
31780fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
31790b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
31800b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
31810b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
31820b8e58a1SAndreas Dilger 	 *
31830b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
31840b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
31850fc1b451SAneesh Kumar K.V 	 */
31860fc1b451SAneesh Kumar K.V 
318772deb455SChristoph Hellwig 	if (!has_huge_files) {
31880fc1b451SAneesh Kumar K.V 		/*
318972deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
319072deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
319172deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
31920fc1b451SAneesh Kumar K.V 		 */
31930fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
31940fc1b451SAneesh Kumar K.V 
31950fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
31960fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
31970fc1b451SAneesh Kumar K.V 
31980fc1b451SAneesh Kumar K.V 	} else {
31998180a562SAneesh Kumar K.V 		/*
32008180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
32018180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
32028180a562SAneesh Kumar K.V 		 * represent total number of blocks in
32038180a562SAneesh Kumar K.V 		 * file system block size
32048180a562SAneesh Kumar K.V 		 */
32050fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
32060fc1b451SAneesh Kumar K.V 
32070fc1b451SAneesh Kumar K.V 	}
32080fc1b451SAneesh Kumar K.V 
32090fc1b451SAneesh Kumar K.V 	/* indirect blocks */
32100fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
32110fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
32120fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
32130fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
32140fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
32150fc1b451SAneesh Kumar K.V 
32160fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
32170fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
3218ac27a0ecSDave Kleikamp 
3219ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
3220ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
3221ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
3222ac27a0ecSDave Kleikamp 	res <<= bits;
3223ac27a0ecSDave Kleikamp 	if (res > upper_limit)
3224ac27a0ecSDave Kleikamp 		res = upper_limit;
32250fc1b451SAneesh Kumar K.V 
32260fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
32270fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
32280fc1b451SAneesh Kumar K.V 
3229ac27a0ecSDave Kleikamp 	return res;
3230ac27a0ecSDave Kleikamp }
3231ac27a0ecSDave Kleikamp 
3232617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
323370bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3234ac27a0ecSDave Kleikamp {
3235617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3236fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3237ac27a0ecSDave Kleikamp 	int has_super = 0;
3238ac27a0ecSDave Kleikamp 
3239ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3240ac27a0ecSDave Kleikamp 
3241e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
324270bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3243ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3244617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3245ac27a0ecSDave Kleikamp 		has_super = 1;
32460b8e58a1SAndreas Dilger 
3247bd63f6b0SDarrick J. Wong 	/*
3248bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3249bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3250bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3251bd63f6b0SDarrick J. Wong 	 * compensate.
3252bd63f6b0SDarrick J. Wong 	 */
3253bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
325449598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3255bd63f6b0SDarrick J. Wong 		has_super++;
3256bd63f6b0SDarrick J. Wong 
3257617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3258ac27a0ecSDave Kleikamp }
3259ac27a0ecSDave Kleikamp 
3260c9de560dSAlex Tomas /**
3261c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3262c9de560dSAlex Tomas  * @sbi: In memory super block info
3263c9de560dSAlex Tomas  *
3264c9de560dSAlex Tomas  * If we have specified it via mount option, then
3265c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3266c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3267c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3268c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3269c9de560dSAlex Tomas  *
3270c9de560dSAlex Tomas  */
3271c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3272c9de560dSAlex Tomas {
3273c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3274c9de560dSAlex Tomas 	unsigned long stripe_width =
3275c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
32763eb08658SDan Ehrenberg 	int ret;
3277c9de560dSAlex Tomas 
3278c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
32793eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
32805469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
32813eb08658SDan Ehrenberg 		ret = stripe_width;
32825469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
32833eb08658SDan Ehrenberg 		ret = stride;
32843eb08658SDan Ehrenberg 	else
32853eb08658SDan Ehrenberg 		ret = 0;
3286c9de560dSAlex Tomas 
32873eb08658SDan Ehrenberg 	/*
32883eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
32893eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
32903eb08658SDan Ehrenberg 	 */
32913eb08658SDan Ehrenberg 	if (ret <= 1)
32923eb08658SDan Ehrenberg 		ret = 0;
3293c9de560dSAlex Tomas 
32943eb08658SDan Ehrenberg 	return ret;
3295c9de560dSAlex Tomas }
3296ac27a0ecSDave Kleikamp 
3297a13fb1a4SEric Sandeen /*
3298a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3299a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3300a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3301a13fb1a4SEric Sandeen  * 0 if it cannot be.
3302a13fb1a4SEric Sandeen  */
3303a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3304a13fb1a4SEric Sandeen {
3305e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3306a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3307a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3308a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3309a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3310a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3311a13fb1a4SEric Sandeen 		return 0;
3312a13fb1a4SEric Sandeen 	}
3313a13fb1a4SEric Sandeen 
3314c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3315c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3316c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3317c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3318c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3319c83ad55eSGabriel Krisman Bertazi 		return 0;
3320c83ad55eSGabriel Krisman Bertazi 	}
3321c83ad55eSGabriel Krisman Bertazi #endif
3322c83ad55eSGabriel Krisman Bertazi 
3323a13fb1a4SEric Sandeen 	if (readonly)
3324a13fb1a4SEric Sandeen 		return 1;
3325a13fb1a4SEric Sandeen 
3326e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
33272cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
33281751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
33292cb5cc8bSDarrick J. Wong 		return 1;
33302cb5cc8bSDarrick J. Wong 	}
33312cb5cc8bSDarrick J. Wong 
3332a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3333e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3334a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3335a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3336a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3337a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3338a13fb1a4SEric Sandeen 		return 0;
3339a13fb1a4SEric Sandeen 	}
3340e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3341bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3342bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3343bab08ab9STheodore Ts'o 			 "extents feature\n");
3344bab08ab9STheodore Ts'o 		return 0;
3345bab08ab9STheodore Ts'o 	}
33467c319d32SAditya Kali 
33479db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3348d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3349d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
33507c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3351d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3352689c958cSLi Xi 		return 0;
3353689c958cSLi Xi 	}
33547c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3355a13fb1a4SEric Sandeen 	return 1;
3356a13fb1a4SEric Sandeen }
3357a13fb1a4SEric Sandeen 
335866e61a9eSTheodore Ts'o /*
335966e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
336066e61a9eSTheodore Ts'o  * on the file system
336166e61a9eSTheodore Ts'o  */
3362235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
336366e61a9eSTheodore Ts'o {
3364235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3365235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3366235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
336766e61a9eSTheodore Ts'o 
336866e61a9eSTheodore Ts'o 	if (es->s_error_count)
3369ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3370ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
337166e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
337266e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
33736a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
33746a0678a7SArnd Bergmann 		       sb->s_id,
33756a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
337666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
337766e61a9eSTheodore Ts'o 		       es->s_first_error_func,
337866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
337966e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3380651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
338166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
338266e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3383651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
338466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3385651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
338666e61a9eSTheodore Ts'o 	}
338766e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
33886a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
33896a0678a7SArnd Bergmann 		       sb->s_id,
33906a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
339166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
339266e61a9eSTheodore Ts'o 		       es->s_last_error_func,
339366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
339466e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3395651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
339666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
339766e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3398651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
339966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3400651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
340166e61a9eSTheodore Ts'o 	}
340266e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
340366e61a9eSTheodore Ts'o }
340466e61a9eSTheodore Ts'o 
3405bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3406bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3407bfff6873SLukas Czerner {
3408bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
34093d392b26STheodore Ts'o 	struct super_block *sb = elr->lr_super;
34103d392b26STheodore Ts'o 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
34113d392b26STheodore Ts'o 	ext4_group_t group = elr->lr_next_group;
3412bfff6873SLukas Czerner 	unsigned long timeout = 0;
34133d392b26STheodore Ts'o 	unsigned int prefetch_ios = 0;
3414bfff6873SLukas Czerner 	int ret = 0;
3415bfff6873SLukas Czerner 
34163d392b26STheodore Ts'o 	if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
34173d392b26STheodore Ts'o 		elr->lr_next_group = ext4_mb_prefetch(sb, group,
34183d392b26STheodore Ts'o 				EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
34193d392b26STheodore Ts'o 		if (prefetch_ios)
34203d392b26STheodore Ts'o 			ext4_mb_prefetch_fini(sb, elr->lr_next_group,
34213d392b26STheodore Ts'o 					      prefetch_ios);
34223d392b26STheodore Ts'o 		trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
34233d392b26STheodore Ts'o 					    prefetch_ios);
34243d392b26STheodore Ts'o 		if (group >= elr->lr_next_group) {
34253d392b26STheodore Ts'o 			ret = 1;
34263d392b26STheodore Ts'o 			if (elr->lr_first_not_zeroed != ngroups &&
34273d392b26STheodore Ts'o 			    !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
34283d392b26STheodore Ts'o 				elr->lr_next_group = elr->lr_first_not_zeroed;
34293d392b26STheodore Ts'o 				elr->lr_mode = EXT4_LI_MODE_ITABLE;
34303d392b26STheodore Ts'o 				ret = 0;
34313d392b26STheodore Ts'o 			}
34323d392b26STheodore Ts'o 		}
34333d392b26STheodore Ts'o 		return ret;
34343d392b26STheodore Ts'o 	}
3435bfff6873SLukas Czerner 
34363d392b26STheodore Ts'o 	for (; group < ngroups; group++) {
3437bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3438bfff6873SLukas Czerner 		if (!gdp) {
3439bfff6873SLukas Czerner 			ret = 1;
3440bfff6873SLukas Czerner 			break;
3441bfff6873SLukas Czerner 		}
3442bfff6873SLukas Czerner 
3443bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3444bfff6873SLukas Czerner 			break;
3445bfff6873SLukas Czerner 	}
3446bfff6873SLukas Czerner 
34477f511862STheodore Ts'o 	if (group >= ngroups)
3448bfff6873SLukas Czerner 		ret = 1;
3449bfff6873SLukas Czerner 
3450bfff6873SLukas Czerner 	if (!ret) {
3451bfff6873SLukas Czerner 		timeout = jiffies;
3452bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3453bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
34543d392b26STheodore Ts'o 		trace_ext4_lazy_itable_init(sb, group);
3455bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
345651ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
34573d392b26STheodore Ts'o 				EXT4_SB(elr->lr_super)->s_li_wait_mult;
3458bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3459bfff6873SLukas Czerner 		}
3460bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3461bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3462bfff6873SLukas Czerner 	}
3463bfff6873SLukas Czerner 	return ret;
3464bfff6873SLukas Czerner }
3465bfff6873SLukas Czerner 
3466bfff6873SLukas Czerner /*
3467bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
34684ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3469bfff6873SLukas Czerner  */
3470bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3471bfff6873SLukas Czerner {
3472bfff6873SLukas Czerner 	if (!elr)
3473bfff6873SLukas Czerner 		return;
3474bfff6873SLukas Czerner 
3475bfff6873SLukas Czerner 	list_del(&elr->lr_request);
34763d392b26STheodore Ts'o 	EXT4_SB(elr->lr_super)->s_li_request = NULL;
3477bfff6873SLukas Czerner 	kfree(elr);
3478bfff6873SLukas Czerner }
3479bfff6873SLukas Czerner 
3480bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3481bfff6873SLukas Czerner {
34821bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
34831bb933fbSLukas Czerner 	if (!ext4_li_info) {
34841bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3485bfff6873SLukas Czerner 		return;
34861bb933fbSLukas Czerner 	}
3487bfff6873SLukas Czerner 
3488bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
34891bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3490bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
34911bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3492bfff6873SLukas Czerner }
3493bfff6873SLukas Czerner 
34948f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
34958f1f7453SEric Sandeen 
3496bfff6873SLukas Czerner /*
3497bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3498bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3499bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3500bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3501bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3502bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3503bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3504bfff6873SLukas Czerner  */
3505bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3506bfff6873SLukas Czerner {
3507bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3508bfff6873SLukas Czerner 	struct list_head *pos, *n;
3509bfff6873SLukas Czerner 	struct ext4_li_request *elr;
35104ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3511bfff6873SLukas Czerner 
3512bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3513bfff6873SLukas Czerner 
3514bfff6873SLukas Czerner cont_thread:
3515bfff6873SLukas Czerner 	while (true) {
3516bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3517bfff6873SLukas Czerner 
3518bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3519bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3520bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3521bfff6873SLukas Czerner 			goto exit_thread;
3522bfff6873SLukas Czerner 		}
3523bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3524e22834f0SDmitry Monakhov 			int err = 0;
3525e22834f0SDmitry Monakhov 			int progress = 0;
3526bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3527bfff6873SLukas Czerner 					 lr_request);
3528bfff6873SLukas Czerner 
3529e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3530e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3531e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3532e22834f0SDmitry Monakhov 				continue;
3533e22834f0SDmitry Monakhov 			}
3534e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3535e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3536e22834f0SDmitry Monakhov 					progress = 1;
3537e22834f0SDmitry Monakhov 					/*
3538e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3539e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3540e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3541e22834f0SDmitry Monakhov 					 */
3542e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3543e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3544e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3545e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3546e22834f0SDmitry Monakhov 					n = pos->next;
3547e22834f0SDmitry Monakhov 				}
3548e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3549e22834f0SDmitry Monakhov 			}
3550b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3551e22834f0SDmitry Monakhov 			if (err) {
3552bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3553bfff6873SLukas Czerner 				continue;
3554bfff6873SLukas Czerner 			}
3555e22834f0SDmitry Monakhov 			if (!progress) {
3556e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3557e22834f0SDmitry Monakhov 					(prandom_u32()
3558e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3559b2c78cd0STheodore Ts'o 			}
3560bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3561bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3562bfff6873SLukas Czerner 		}
3563bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3564bfff6873SLukas Czerner 
3565a0acae0eSTejun Heo 		try_to_freeze();
3566bfff6873SLukas Czerner 
35674ed5c033SLukas Czerner 		cur = jiffies;
35684ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3569f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3570bfff6873SLukas Czerner 			cond_resched();
3571bfff6873SLukas Czerner 			continue;
3572bfff6873SLukas Czerner 		}
3573bfff6873SLukas Czerner 
35744ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
35754ed5c033SLukas Czerner 
35768f1f7453SEric Sandeen 		if (kthread_should_stop()) {
35778f1f7453SEric Sandeen 			ext4_clear_request_list();
35788f1f7453SEric Sandeen 			goto exit_thread;
35798f1f7453SEric Sandeen 		}
3580bfff6873SLukas Czerner 	}
3581bfff6873SLukas Czerner 
3582bfff6873SLukas Czerner exit_thread:
3583bfff6873SLukas Czerner 	/*
3584bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3585bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3586bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3587bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3588bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3589bfff6873SLukas Czerner 	 * new one.
3590bfff6873SLukas Czerner 	 */
3591bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3592bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3593bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3594bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3595bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3596bfff6873SLukas Czerner 		goto cont_thread;
3597bfff6873SLukas Czerner 	}
3598bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3599bfff6873SLukas Czerner 	kfree(ext4_li_info);
3600bfff6873SLukas Czerner 	ext4_li_info = NULL;
3601bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3602bfff6873SLukas Czerner 
3603bfff6873SLukas Czerner 	return 0;
3604bfff6873SLukas Czerner }
3605bfff6873SLukas Czerner 
3606bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3607bfff6873SLukas Czerner {
3608bfff6873SLukas Czerner 	struct list_head *pos, *n;
3609bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3610bfff6873SLukas Czerner 
3611bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3612bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3613bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3614bfff6873SLukas Czerner 				 lr_request);
3615bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3616bfff6873SLukas Czerner 	}
3617bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3618bfff6873SLukas Czerner }
3619bfff6873SLukas Czerner 
3620bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3621bfff6873SLukas Czerner {
36228f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
36238f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
36248f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
36258f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3626bfff6873SLukas Czerner 		ext4_clear_request_list();
3627bfff6873SLukas Czerner 		kfree(ext4_li_info);
3628bfff6873SLukas Czerner 		ext4_li_info = NULL;
362992b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3630bfff6873SLukas Czerner 				 "initialization thread\n",
3631bfff6873SLukas Czerner 				 err);
3632bfff6873SLukas Czerner 		return err;
3633bfff6873SLukas Czerner 	}
3634bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3635bfff6873SLukas Czerner 	return 0;
3636bfff6873SLukas Czerner }
3637bfff6873SLukas Czerner 
3638bfff6873SLukas Czerner /*
3639bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3640bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3641bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3642bfff6873SLukas Czerner  * groups and return total number of groups.
3643bfff6873SLukas Czerner  */
3644bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3645bfff6873SLukas Czerner {
3646bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3647bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3648bfff6873SLukas Czerner 
36498844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
36508844618dSTheodore Ts'o 		return ngroups;
36518844618dSTheodore Ts'o 
3652bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3653bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3654bfff6873SLukas Czerner 		if (!gdp)
3655bfff6873SLukas Czerner 			continue;
3656bfff6873SLukas Czerner 
365750122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3658bfff6873SLukas Czerner 			break;
3659bfff6873SLukas Czerner 	}
3660bfff6873SLukas Czerner 
3661bfff6873SLukas Czerner 	return group;
3662bfff6873SLukas Czerner }
3663bfff6873SLukas Czerner 
3664bfff6873SLukas Czerner static int ext4_li_info_new(void)
3665bfff6873SLukas Czerner {
3666bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3667bfff6873SLukas Czerner 
3668bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3669bfff6873SLukas Czerner 	if (!eli)
3670bfff6873SLukas Czerner 		return -ENOMEM;
3671bfff6873SLukas Czerner 
3672bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3673bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3674bfff6873SLukas Czerner 
3675bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3676bfff6873SLukas Czerner 
3677bfff6873SLukas Czerner 	ext4_li_info = eli;
3678bfff6873SLukas Czerner 
3679bfff6873SLukas Czerner 	return 0;
3680bfff6873SLukas Czerner }
3681bfff6873SLukas Czerner 
3682bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3683bfff6873SLukas Czerner 					    ext4_group_t start)
3684bfff6873SLukas Czerner {
3685bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3686bfff6873SLukas Czerner 
3687bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3688bfff6873SLukas Czerner 	if (!elr)
3689bfff6873SLukas Czerner 		return NULL;
3690bfff6873SLukas Czerner 
3691bfff6873SLukas Czerner 	elr->lr_super = sb;
36923d392b26STheodore Ts'o 	elr->lr_first_not_zeroed = start;
36933d392b26STheodore Ts'o 	if (test_opt(sb, PREFETCH_BLOCK_BITMAPS))
36943d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
36953d392b26STheodore Ts'o 	else {
36963d392b26STheodore Ts'o 		elr->lr_mode = EXT4_LI_MODE_ITABLE;
3697bfff6873SLukas Czerner 		elr->lr_next_group = start;
36983d392b26STheodore Ts'o 	}
3699bfff6873SLukas Czerner 
3700bfff6873SLukas Czerner 	/*
3701bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3702bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3703bfff6873SLukas Czerner 	 * better.
3704bfff6873SLukas Czerner 	 */
3705dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3706dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3707bfff6873SLukas Czerner 	return elr;
3708bfff6873SLukas Czerner }
3709bfff6873SLukas Czerner 
37107f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3711bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3712bfff6873SLukas Czerner {
3713bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
37147f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
371549598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
37166c5a6cb9SAndrew Morton 	int ret = 0;
3717bfff6873SLukas Czerner 
37187f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
371951ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
372051ce6511SLukas Czerner 		/*
372151ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
372251ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
372351ce6511SLukas Czerner 		 */
372451ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
37257f511862STheodore Ts'o 		goto out;
372651ce6511SLukas Czerner 	}
3727bfff6873SLukas Czerner 
37283d392b26STheodore Ts'o 	if (!test_opt(sb, PREFETCH_BLOCK_BITMAPS) &&
37293d392b26STheodore Ts'o 	    (first_not_zeroed == ngroups || sb_rdonly(sb) ||
37303d392b26STheodore Ts'o 	     !test_opt(sb, INIT_INODE_TABLE)))
37317f511862STheodore Ts'o 		goto out;
3732bfff6873SLukas Czerner 
3733bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
37347f511862STheodore Ts'o 	if (!elr) {
37357f511862STheodore Ts'o 		ret = -ENOMEM;
37367f511862STheodore Ts'o 		goto out;
37377f511862STheodore Ts'o 	}
3738bfff6873SLukas Czerner 
3739bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3740bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3741bfff6873SLukas Czerner 		if (ret)
3742bfff6873SLukas Czerner 			goto out;
3743bfff6873SLukas Czerner 	}
3744bfff6873SLukas Czerner 
3745bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3746bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3747bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3748bfff6873SLukas Czerner 
3749bfff6873SLukas Czerner 	sbi->s_li_request = elr;
375046e4690bSTao Ma 	/*
375146e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
375246e4690bSTao Ma 	 * the request_list and the removal and free of it is
375346e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
375446e4690bSTao Ma 	 */
375546e4690bSTao Ma 	elr = NULL;
3756bfff6873SLukas Czerner 
3757bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3758bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3759bfff6873SLukas Czerner 		if (ret)
3760bfff6873SLukas Czerner 			goto out;
3761bfff6873SLukas Czerner 	}
3762bfff6873SLukas Czerner out:
3763bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3764beed5ecbSNicolas Kaiser 	if (ret)
3765bfff6873SLukas Czerner 		kfree(elr);
3766bfff6873SLukas Czerner 	return ret;
3767bfff6873SLukas Czerner }
3768bfff6873SLukas Czerner 
3769bfff6873SLukas Czerner /*
3770bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3771bfff6873SLukas Czerner  * module unload.
3772bfff6873SLukas Czerner  */
3773bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3774bfff6873SLukas Czerner {
3775bfff6873SLukas Czerner 	/*
3776bfff6873SLukas Czerner 	 * If thread exited earlier
3777bfff6873SLukas Czerner 	 * there's nothing to be done.
3778bfff6873SLukas Czerner 	 */
37798f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3780bfff6873SLukas Czerner 		return;
3781bfff6873SLukas Czerner 
37828f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3783bfff6873SLukas Czerner }
3784bfff6873SLukas Czerner 
378525ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
378625ed6e8aSDarrick J. Wong {
378725ed6e8aSDarrick J. Wong 	int ret = 1;
378825ed6e8aSDarrick J. Wong 	int compat, incompat;
378925ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
379025ed6e8aSDarrick J. Wong 
37919aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3792db9ee220SDarrick J. Wong 		/* journal checksum v3 */
379325ed6e8aSDarrick J. Wong 		compat = 0;
3794db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
379525ed6e8aSDarrick J. Wong 	} else {
379625ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
379725ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
379825ed6e8aSDarrick J. Wong 		incompat = 0;
379925ed6e8aSDarrick J. Wong 	}
380025ed6e8aSDarrick J. Wong 
3801feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3802feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3803feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3804feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
380525ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
380625ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
380725ed6e8aSDarrick J. Wong 				compat, 0,
380825ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
380925ed6e8aSDarrick J. Wong 				incompat);
381025ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
381125ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
381225ed6e8aSDarrick J. Wong 				compat, 0,
381325ed6e8aSDarrick J. Wong 				incompat);
381425ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
381525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
381625ed6e8aSDarrick J. Wong 	} else {
3817feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3818feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
381925ed6e8aSDarrick J. Wong 	}
382025ed6e8aSDarrick J. Wong 
382125ed6e8aSDarrick J. Wong 	return ret;
382225ed6e8aSDarrick J. Wong }
382325ed6e8aSDarrick J. Wong 
3824952fc18eSTheodore Ts'o /*
3825952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3826952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3827952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3828952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3829952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3830952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3831952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3832952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3833952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3834952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3835952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3836952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3837952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3838952fc18eSTheodore Ts'o  */
3839952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3840952fc18eSTheodore Ts'o 			  char *buf)
3841952fc18eSTheodore Ts'o {
3842952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3843952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3844952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3845952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3846952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3847952fc18eSTheodore Ts'o 
3848e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
38490548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
38500548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
38510548bbb8STheodore Ts'o 
3852952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3853952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3854952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3855952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3856952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3857952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3858952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3859952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3860952fc18eSTheodore Ts'o 			count++;
3861952fc18eSTheodore Ts'o 		}
3862952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3863952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3864952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3865952fc18eSTheodore Ts'o 			count++;
3866952fc18eSTheodore Ts'o 		}
3867952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3868952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3869952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3870952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3871952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3872952fc18eSTheodore Ts'o 				count++;
3873952fc18eSTheodore Ts'o 			}
3874952fc18eSTheodore Ts'o 		if (i != grp)
3875952fc18eSTheodore Ts'o 			continue;
3876952fc18eSTheodore Ts'o 		s = 0;
3877952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3878952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3879952fc18eSTheodore Ts'o 			count++;
3880952fc18eSTheodore Ts'o 		}
3881c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3882c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3883c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3884c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3885c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3886952fc18eSTheodore Ts'o 		}
3887c48ae41bSTheodore Ts'o 		count += j;
3888c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3889c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3890952fc18eSTheodore Ts'o 	}
3891952fc18eSTheodore Ts'o 	if (!count)
3892952fc18eSTheodore Ts'o 		return 0;
3893952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3894952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3895952fc18eSTheodore Ts'o }
3896952fc18eSTheodore Ts'o 
3897952fc18eSTheodore Ts'o /*
3898952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3899952fc18eSTheodore Ts'o  */
3900952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3901952fc18eSTheodore Ts'o {
3902952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3903952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
39043c816dedSEric Whitney 	struct inode *j_inode;
39053c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3906952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3907952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
39084fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3909952fc18eSTheodore Ts'o 
3910952fc18eSTheodore Ts'o 	if (!buf)
3911952fc18eSTheodore Ts'o 		return -ENOMEM;
3912952fc18eSTheodore Ts'o 
3913952fc18eSTheodore Ts'o 	/*
3914952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3915952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3916952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3917952fc18eSTheodore Ts'o 	 */
3918952fc18eSTheodore Ts'o 
3919952fc18eSTheodore Ts'o 	/*
3920952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3921952fc18eSTheodore Ts'o 	 */
3922952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3923952fc18eSTheodore Ts'o 
3924952fc18eSTheodore Ts'o 	/*
3925952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3926952fc18eSTheodore Ts'o 	 */
3927952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3928952fc18eSTheodore Ts'o 		int blks;
3929952fc18eSTheodore Ts'o 
3930952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3931952fc18eSTheodore Ts'o 		overhead += blks;
3932952fc18eSTheodore Ts'o 		if (blks)
3933952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3934952fc18eSTheodore Ts'o 		cond_resched();
3935952fc18eSTheodore Ts'o 	}
39363c816dedSEric Whitney 
39373c816dedSEric Whitney 	/*
39383c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
39393c816dedSEric Whitney 	 * loaded or not
39403c816dedSEric Whitney 	 */
3941ee7ed3aaSChunguang Xu 	if (sbi->s_journal && !sbi->s_journal_bdev)
3942ede7dc7fSHarshad Shirwadkar 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
3943f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3944f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
39453c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
39463c816dedSEric Whitney 		if (j_inode) {
39473c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
39483c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
39493c816dedSEric Whitney 			iput(j_inode);
39503c816dedSEric Whitney 		} else {
39513c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
39523c816dedSEric Whitney 		}
39533c816dedSEric Whitney 	}
3954952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3955952fc18eSTheodore Ts'o 	smp_wmb();
3956952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3957952fc18eSTheodore Ts'o 	return 0;
3958952fc18eSTheodore Ts'o }
3959952fc18eSTheodore Ts'o 
3960b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
396127dd4385SLukas Czerner {
396227dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3963b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
396427dd4385SLukas Czerner 
396527dd4385SLukas Czerner 	/*
396630fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
396730fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
396830fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
396930fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
397030fac0f7SJan Kara 	 */
3971e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3972b5799018STheodore Ts'o 		return;
397330fac0f7SJan Kara 	/*
397427dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
397527dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
397627dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3977556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
397827dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
397927dd4385SLukas Czerner 	 * very rare.
398027dd4385SLukas Czerner 	 */
3981b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3982b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
398327dd4385SLukas Czerner 
398427dd4385SLukas Czerner 	do_div(resv_clusters, 50);
398527dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
398627dd4385SLukas Czerner 
3987b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
398827dd4385SLukas Czerner }
398927dd4385SLukas Czerner 
3990ca9b404fSRoman Anufriev static const char *ext4_quota_mode(struct super_block *sb)
3991ca9b404fSRoman Anufriev {
3992ca9b404fSRoman Anufriev #ifdef CONFIG_QUOTA
3993ca9b404fSRoman Anufriev 	if (!ext4_quota_capable(sb))
3994ca9b404fSRoman Anufriev 		return "none";
3995ca9b404fSRoman Anufriev 
3996ca9b404fSRoman Anufriev 	if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
3997ca9b404fSRoman Anufriev 		return "journalled";
3998ca9b404fSRoman Anufriev 	else
3999ca9b404fSRoman Anufriev 		return "writeback";
4000ca9b404fSRoman Anufriev #else
4001ca9b404fSRoman Anufriev 	return "disabled";
4002ca9b404fSRoman Anufriev #endif
4003ca9b404fSRoman Anufriev }
4004ca9b404fSRoman Anufriev 
4005617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
4006ac27a0ecSDave Kleikamp {
40075e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
4008d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
40091d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
4010617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
40115aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
40127c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
4013617ba13bSMingming Cao 	ext4_fsblk_t block;
4014617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
401570bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
4016ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
4017ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
4018ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
4019ac27a0ecSDave Kleikamp 	struct inode *root;
40200390131bSFrank Mayhar 	const char *descr;
4021dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
4022281b5995STheodore Ts'o 	int blocksize, clustersize;
40234ec11028STheodore Ts'o 	unsigned int db_count;
40244ec11028STheodore Ts'o 	unsigned int i;
4025ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
4026bd81d8eeSLaurent Vivier 	__u64 blocks_count;
402707aa2ea1SLukas Czerner 	int err = 0;
4028b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4029bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
4030ac27a0ecSDave Kleikamp 
40315aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
40325aee0f8aSTheodore Ts'o 		goto out_free_base;
4033705895b6SPekka Enberg 
4034aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
4035705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
4036705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
40375aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
40385aee0f8aSTheodore Ts'o 		goto out_free_base;
40395aee0f8aSTheodore Ts'o 
4040ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
40412c0544b2STheodore Ts'o 	sbi->s_sb = sb;
4042240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
4043d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
4044f613dfcbSTheodore Ts'o 	sbi->s_sectors_written_start =
40458446fe92SChristoph Hellwig 		part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
4046ac27a0ecSDave Kleikamp 
40479f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
4048ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
40499f6200bbSTheodore Ts'o 
405007aa2ea1SLukas Czerner 	/* -EINVAL is default */
4051dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
4052617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
4053ac27a0ecSDave Kleikamp 	if (!blocksize) {
4054b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
4055ac27a0ecSDave Kleikamp 		goto out_fail;
4056ac27a0ecSDave Kleikamp 	}
4057ac27a0ecSDave Kleikamp 
4058ac27a0ecSDave Kleikamp 	/*
4059617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
4060ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
4061ac27a0ecSDave Kleikamp 	 */
4062617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
406370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
406470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4065ac27a0ecSDave Kleikamp 	} else {
406670bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
4067ac27a0ecSDave Kleikamp 	}
4068ac27a0ecSDave Kleikamp 
40698394a6abSzhangyi (F) 	bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
40708394a6abSzhangyi (F) 	if (IS_ERR(bh)) {
4071b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
40728394a6abSzhangyi (F) 		ret = PTR_ERR(bh);
4073ac27a0ecSDave Kleikamp 		goto out_fail;
4074ac27a0ecSDave Kleikamp 	}
4075ac27a0ecSDave Kleikamp 	/*
4076ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
4077617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
4078ac27a0ecSDave Kleikamp 	 */
40792716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4080ac27a0ecSDave Kleikamp 	sbi->s_es = es;
4081ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
4082617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
4083617ba13bSMingming Cao 		goto cantfind_ext4;
4084afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
4085ac27a0ecSDave Kleikamp 
4086feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
4087e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
4088e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
4089363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
4090feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
4091feb0ab32SDarrick J. Wong 
4092d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
4093d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
4094d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4095d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
4096d25425f8SDarrick J. Wong 		silent = 1;
4097d25425f8SDarrick J. Wong 		goto cantfind_ext4;
4098d25425f8SDarrick J. Wong 	}
4099d25425f8SDarrick J. Wong 
41000441984aSDarrick J. Wong 	/* Load the checksum driver */
41010441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
41020441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
41030441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
41040441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
41050441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
41060441984aSDarrick J. Wong 		goto failed_mount;
41070441984aSDarrick J. Wong 	}
41080441984aSDarrick J. Wong 
4109a9c47317SDarrick J. Wong 	/* Check superblock checksum */
4110a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
4111a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4112a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
4113a9c47317SDarrick J. Wong 		silent = 1;
41146a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
4115a9c47317SDarrick J. Wong 		goto cantfind_ext4;
4116a9c47317SDarrick J. Wong 	}
4117a9c47317SDarrick J. Wong 
4118a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
4119e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
41208c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4121dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4122a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4123a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
4124a9c47317SDarrick J. Wong 
4125ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
4126ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4127fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
4128617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
4129fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
413087f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4131fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
4132617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
4133fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
4134ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
4135fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
413603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
4137fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
41382e7842b8SHugh Dickins #endif
4139995a3ed6SHarshad Shirwadkar 	if (ext4_has_feature_fast_commit(sb))
4140995a3ed6SHarshad Shirwadkar 		set_opt2(sb, JOURNAL_FAST_COMMIT);
414198c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
414298c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
414398c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
414498c1a759SDarrick J. Wong 
4145617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4146fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
4147617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4148fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
4149617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4150fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
4151ac27a0ecSDave Kleikamp 
4152617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
4153fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
4154bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
4155fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
4156bb4f397aSAneesh Kumar K.V 	else
4157fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
415845f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
4159fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
41608b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
4161fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
4162ac27a0ecSDave Kleikamp 
416308cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
416408cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
416530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
416630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
416730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
4168ac27a0ecSDave Kleikamp 
41698b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4170fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
4171ac27a0ecSDave Kleikamp 
41721e2462f9SMingming Cao 	/*
4173dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
4174dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
4175dd919b98SAneesh Kumar K.V 	 */
4176bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
41778b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4178fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
4179dd919b98SAneesh Kumar K.V 
418051ce6511SLukas Czerner 	/*
418151ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
418251ce6511SLukas Czerner 	 * no mount option specified.
418351ce6511SLukas Czerner 	 */
418451ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
418551ce6511SLukas Czerner 
4186c9200760STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
4187c9200760STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4188c9200760STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4189c9200760STheodore Ts'o 			 "Invalid log block size: %u",
4190c9200760STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4191c9200760STheodore Ts'o 		goto failed_mount;
4192c9200760STheodore Ts'o 	}
4193c9200760STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4194c9200760STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4195c9200760STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4196c9200760STheodore Ts'o 			 "Invalid log cluster size: %u",
4197c9200760STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4198c9200760STheodore Ts'o 		goto failed_mount;
4199c9200760STheodore Ts'o 	}
4200c9200760STheodore Ts'o 
4201c9200760STheodore Ts'o 	blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
4202626b035bSRitesh Harjani 
4203626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
4204626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
4205626b035bSRitesh Harjani 
42069803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
42079803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
42089803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
42099803387cSTheodore Ts'o 	} else {
42109803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
42119803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
42129803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
42139803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
42149803387cSTheodore Ts'o 				 sbi->s_first_ino);
42159803387cSTheodore Ts'o 			goto failed_mount;
42169803387cSTheodore Ts'o 		}
42179803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
42189803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
42199803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
42209803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
42219803387cSTheodore Ts'o 			       "unsupported inode size: %d",
42229803387cSTheodore Ts'o 			       sbi->s_inode_size);
42234f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
42249803387cSTheodore Ts'o 			goto failed_mount;
42259803387cSTheodore Ts'o 		}
42269803387cSTheodore Ts'o 		/*
42279803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
42289803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
42299803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
42309803387cSTheodore Ts'o 		 * for all three.
42319803387cSTheodore Ts'o 		 */
42329803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
42339803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
42349803387cSTheodore Ts'o 			sb->s_time_gran = 1;
42359803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
42369803387cSTheodore Ts'o 		} else {
42379803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
42389803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
42399803387cSTheodore Ts'o 		}
42409803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
42419803387cSTheodore Ts'o 	}
42429803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
42439803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
42449803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
42459803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
42469803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
42479803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
42489803387cSTheodore Ts'o 
42499803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
42509803387cSTheodore Ts'o 			if (v > max) {
42519803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
42529803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
42539803387cSTheodore Ts'o 				goto failed_mount;
42549803387cSTheodore Ts'o 			}
42559803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
42569803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
42579803387cSTheodore Ts'o 
42589803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
42599803387cSTheodore Ts'o 			if (v > max) {
42609803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
42619803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
42629803387cSTheodore Ts'o 				goto failed_mount;
42639803387cSTheodore Ts'o 			}
42649803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
42659803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
42669803387cSTheodore Ts'o 		}
42679803387cSTheodore Ts'o 	}
42689803387cSTheodore Ts'o 
42695aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
42705aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
42715aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
42725aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
42735aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
42745aee0f8aSTheodore Ts'o 			goto failed_mount;
42755aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
42765aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
42778b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
42788b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
42795aee0f8aSTheodore Ts'o 				 s_mount_opts);
42805aee0f8aSTheodore Ts'o 		}
42815aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
42828b67f04aSTheodore Ts'o 	}
42835a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
4284b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
4285661aa520SEric Sandeen 			   &journal_ioprio, 0))
4286ac27a0ecSDave Kleikamp 		goto failed_mount;
4287ac27a0ecSDave Kleikamp 
4288c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4289f8f4acb6SDaniel Rosenberg 	if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
4290c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
4291c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
4292c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
4293c83ad55eSGabriel Krisman Bertazi 
4294c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
4295c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4296c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
4297c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4298c83ad55eSGabriel Krisman Bertazi 		}
4299c83ad55eSGabriel Krisman Bertazi 
4300c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
4301c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
4302c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4303c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
4304c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4305c83ad55eSGabriel Krisman Bertazi 		}
4306c83ad55eSGabriel Krisman Bertazi 
4307c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
4308c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
4309c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4310c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
4311c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4312c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4313c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4314c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4315c83ad55eSGabriel Krisman Bertazi 		}
4316c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4317c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4318c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4319c83ad55eSGabriel Krisman Bertazi 
4320f8f4acb6SDaniel Rosenberg 		sb->s_encoding = encoding;
4321f8f4acb6SDaniel Rosenberg 		sb->s_encoding_flags = encoding_flags;
4322c83ad55eSGabriel Krisman Bertazi 	}
4323c83ad55eSGabriel Krisman Bertazi #endif
4324c83ad55eSGabriel Krisman Bertazi 
432556889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4326556e0319SHarshad Shirwadkar 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, O_DIRECT and fast_commit support!\n");
4327781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4328244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
4329556e0319SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
433056889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
433156889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
433256889787STheodore Ts'o 				 "both data=journal and delalloc");
433356889787STheodore Ts'o 			goto failed_mount;
433456889787STheodore Ts'o 		}
4335fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4336923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4337923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4338923ae0ffSRoss Zwisler 			goto failed_mount;
4339923ae0ffSRoss Zwisler 		}
434073b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
434173b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
434273b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
434373b92a2aSSergey Karamov 				 "instead of data journaling mode");
434473b92a2aSSergey Karamov 		}
434556889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
434656889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4347001e4a87STejun Heo 	} else {
4348001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
434956889787STheodore Ts'o 	}
435056889787STheodore Ts'o 
43511751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
43521751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4353ac27a0ecSDave Kleikamp 
4354617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4355e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4356e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4357e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4358b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4359b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4360b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4361469108ffSTheodore Tso 
4362ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4363ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4364e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4365ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4366ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4367ed3654ebSTheodore Ts'o 			goto failed_mount;
4368ed3654ebSTheodore Ts'o 		}
4369dec214d0STahsin Erdogan 
4370dec214d0STahsin Erdogan 		/*
4371dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4372dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4373dec214d0STahsin Erdogan 		 */
4374dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4375dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4376dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4377dec214d0STahsin Erdogan 			goto failed_mount;
4378dec214d0STahsin Erdogan 		}
4379ed3654ebSTheodore Ts'o 	}
4380ed3654ebSTheodore Ts'o 
43812035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
43822035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
43832035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
43842035e776STheodore Ts'o 				 "using the ext4 subsystem");
43852035e776STheodore Ts'o 		else {
43860d9366d6SEric Sandeen 			/*
43870d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
43880d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
43890d9366d6SEric Sandeen 			 */
43900d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
43910d9366d6SEric Sandeen 				goto failed_mount;
43922035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
43932035e776STheodore Ts'o 				 "to feature incompatibilities");
43942035e776STheodore Ts'o 			goto failed_mount;
43952035e776STheodore Ts'o 		}
43962035e776STheodore Ts'o 	}
43972035e776STheodore Ts'o 
43982035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
43992035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
44002035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
44012035e776STheodore Ts'o 				 "using the ext4 subsystem");
44022035e776STheodore Ts'o 		else {
44030d9366d6SEric Sandeen 			/*
44040d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
44050d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
44060d9366d6SEric Sandeen 			 */
44070d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
44080d9366d6SEric Sandeen 				goto failed_mount;
44092035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
44102035e776STheodore Ts'o 				 "to feature incompatibilities");
44112035e776STheodore Ts'o 			goto failed_mount;
44122035e776STheodore Ts'o 		}
44132035e776STheodore Ts'o 	}
44142035e776STheodore Ts'o 
4415469108ffSTheodore Tso 	/*
4416ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4417ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4418ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4419ac27a0ecSDave Kleikamp 	 */
4420bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4421ac27a0ecSDave Kleikamp 		goto failed_mount;
4422a13fb1a4SEric Sandeen 
44235b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
44245b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44255b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
44265b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
44275b9554dcSTheodore Ts'o 		goto failed_mount;
44285b9554dcSTheodore Ts'o 	}
44295b9554dcSTheodore Ts'o 
4430a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4431a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4432a8ab6d38SIra Weiny 
4433fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4434559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4435559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4436559db4c6SRoss Zwisler 					" that may contain inline data");
4437361d24d4SEric Sandeen 			goto failed_mount;
4438559db4c6SRoss Zwisler 		}
4439a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
444024f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4441361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4442361d24d4SEric Sandeen 			goto failed_mount;
444324f3478dSDan Williams 		}
4444923ae0ffSRoss Zwisler 	}
4445923ae0ffSRoss Zwisler 
4446e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
44476ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
44486ddb2447STheodore Ts'o 			 es->s_encryption_level);
44496ddb2447STheodore Ts'o 		goto failed_mount;
44506ddb2447STheodore Ts'o 	}
44516ddb2447STheodore Ts'o 
4452ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4453ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4454ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4455b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4456ce40733cSAneesh Kumar K.V 					blocksize);
4457ac27a0ecSDave Kleikamp 			goto failed_mount;
4458ac27a0ecSDave Kleikamp 		}
4459ac27a0ecSDave Kleikamp 
4460ac27a0ecSDave Kleikamp 		brelse(bh);
446170bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
446270bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
44638394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
44648394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4465b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4466b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
44678394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
44688394a6abSzhangyi (F) 			bh = NULL;
4469ac27a0ecSDave Kleikamp 			goto failed_mount;
4470ac27a0ecSDave Kleikamp 		}
44712716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4472ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4473617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4474b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4475b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4476ac27a0ecSDave Kleikamp 			goto failed_mount;
4477ac27a0ecSDave Kleikamp 		}
4478ac27a0ecSDave Kleikamp 	}
4479ac27a0ecSDave Kleikamp 
4480e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4481f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4482f287a1a5STheodore Ts'o 						      has_huge_files);
4483f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4484ac27a0ecSDave Kleikamp 
44850d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4486e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
44878fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
44880d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4489d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4490b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4491b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
44920d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
44930d1ee42fSAlexandre Ratchov 			goto failed_mount;
44940d1ee42fSAlexandre Ratchov 		}
44950d1ee42fSAlexandre Ratchov 	} else
44960d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
44970b8e58a1SAndreas Dilger 
4498ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4499ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
45000b8e58a1SAndreas Dilger 
4501617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4502ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4503617ba13bSMingming Cao 		goto cantfind_ext4;
4504cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4505cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4506cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4507b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4508cd6bb35bSTheodore Ts'o 		goto failed_mount;
4509cd6bb35bSTheodore Ts'o 	}
4510ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4511ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
45120d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4513ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4514ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4515e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4516e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
45170b8e58a1SAndreas Dilger 
4518ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4519ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4520ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4521e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4522f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4523f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4524f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4525f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4526f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4527bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
452823301410STheodore Ts'o 				es->s_flags |=
452923301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4530f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4531f99b2589STheodore Ts'o #else
4532bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
453323301410STheodore Ts'o 				es->s_flags |=
453423301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4535f99b2589STheodore Ts'o #endif
4536f99b2589STheodore Ts'o 		}
453723301410STheodore Ts'o 	}
4538ac27a0ecSDave Kleikamp 
4539281b5995STheodore Ts'o 	/* Handle clustersize */
4540281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4541ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4542281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4543281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4544281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4545281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4546281b5995STheodore Ts'o 			goto failed_mount;
4547281b5995STheodore Ts'o 		}
4548281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4549281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4550281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4551281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4552281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4553281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4554281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4555281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4556281b5995STheodore Ts'o 			goto failed_mount;
4557281b5995STheodore Ts'o 		}
4558281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4559281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4560281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4561281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4562281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4563281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4564281b5995STheodore Ts'o 			goto failed_mount;
4565281b5995STheodore Ts'o 		}
4566281b5995STheodore Ts'o 	} else {
4567281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4568bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4569bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4570bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4571bfe0a5f4STheodore Ts'o 			goto failed_mount;
4572281b5995STheodore Ts'o 		}
4573ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4574b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4575b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4576ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4577ac27a0ecSDave Kleikamp 			goto failed_mount;
4578ac27a0ecSDave Kleikamp 		}
4579281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4580281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4581281b5995STheodore Ts'o 	}
4582281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4583281b5995STheodore Ts'o 
4584960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4585960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4586960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4587960fd856STheodore Ts'o 
4588bf43d84bSEric Sandeen 	/*
4589bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4590bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4591bf43d84bSEric Sandeen 	 */
45925a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
459330ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
45945a9ae68aSDarrick J. Wong 	if (err) {
4595b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4596bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4597ac27a0ecSDave Kleikamp 		goto failed_mount;
4598ac27a0ecSDave Kleikamp 	}
4599ac27a0ecSDave Kleikamp 
4600617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4601617ba13bSMingming Cao 		goto cantfind_ext4;
4602e7c95593SEric Sandeen 
46030f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
46040f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
46050f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4606b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4607b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
46080f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
46090f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
46100f2ddca6SFrom: Thiemo Nagel 	}
46110f2ddca6SFrom: Thiemo Nagel 
46124ec11028STheodore Ts'o 	/*
46134ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
46144ec11028STheodore Ts'o 	 * of the filesystem.
46154ec11028STheodore Ts'o 	 */
46164ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4617b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4618b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4619e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
46204ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4621e7c95593SEric Sandeen 		goto failed_mount;
4622e7c95593SEric Sandeen 	}
4623bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4624bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4625bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4626bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4627bfe0a5f4STheodore Ts'o 		goto failed_mount;
4628bfe0a5f4STheodore Ts'o 	}
4629bfe0a5f4STheodore Ts'o 
4630bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4631bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4632bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4633bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
46344ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4635df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
46364ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4637df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
46384ec11028STheodore Ts'o 		       ext4_blocks_count(es),
46394ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
46404ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
46414ec11028STheodore Ts'o 		goto failed_mount;
46424ec11028STheodore Ts'o 	}
4643bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4644fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4645fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
46469e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
46479e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
46489e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
46499e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
46509e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
46519e463084STheodore Ts'o 		ret = -EINVAL;
46529e463084STheodore Ts'o 		goto failed_mount;
46539e463084STheodore Ts'o 	}
4654617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4655617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
46563a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
46572ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
46583a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
46593a4b77cdSEryu Guan 				 "first meta block group too large: %u "
46603a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
46613a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
46623a4b77cdSEryu Guan 			goto failed_mount;
46633a4b77cdSEryu Guan 		}
46643a4b77cdSEryu Guan 	}
46651d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
46661d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4667f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
46681d0c3924STheodore Ts'o 					  GFP_KERNEL));
4669ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4670b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
46712cde417dSTheodore Ts'o 		ret = -ENOMEM;
4672ac27a0ecSDave Kleikamp 		goto failed_mount;
4673ac27a0ecSDave Kleikamp 	}
4674ac27a0ecSDave Kleikamp 
4675705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4676ac27a0ecSDave Kleikamp 
467785c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
467885c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
467985c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
46805df1d412Szhangyi (F) 		ext4_sb_breadahead_unmovable(sb, block);
468185c8f176SAndrew Perepechko 	}
468285c8f176SAndrew Perepechko 
4683ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
46841d0c3924STheodore Ts'o 		struct buffer_head *bh;
46851d0c3924STheodore Ts'o 
468670bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
46878394a6abSzhangyi (F) 		bh = ext4_sb_bread_unmovable(sb, block);
46888394a6abSzhangyi (F) 		if (IS_ERR(bh)) {
4689b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4690b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4691ac27a0ecSDave Kleikamp 			db_count = i;
46928394a6abSzhangyi (F) 			ret = PTR_ERR(bh);
4693ac27a0ecSDave Kleikamp 			goto failed_mount2;
4694ac27a0ecSDave Kleikamp 		}
46951d0c3924STheodore Ts'o 		rcu_read_lock();
46961d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
46971d0c3924STheodore Ts'o 		rcu_read_unlock();
4698ac27a0ecSDave Kleikamp 	}
469944de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4700829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4701b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
47026a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4703f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4704ac27a0ecSDave Kleikamp 	}
4705772cb7c8SJose R. Santos 
4706235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
4707c92dc856SJan Kara 	spin_lock_init(&sbi->s_error_lock);
4708c92dc856SJan Kara 	INIT_WORK(&sbi->s_error_work, flush_stashed_error_work);
470904496411STao Ma 
4710a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4711eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4712ce7e010aSTheodore Ts'o 		goto failed_mount3;
4713ce7e010aSTheodore Ts'o 
4714c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
471567a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4716c9de560dSAlex Tomas 
4717f9ae9cf5STheodore Ts'o 	/*
4718f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4719f9ae9cf5STheodore Ts'o 	 */
4720f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4721617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4722617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4723643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4724a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4725ffcc4182SEric Biggers #endif
4726c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4727c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4728c93d8f88SEric Biggers #endif
4729ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4730617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4731e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
47321fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4733262b4662SJan Kara 	else
4734262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4735689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4736ac27a0ecSDave Kleikamp #endif
473785787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4738f2fa2ffcSAneesh Kumar K.V 
4739ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
47403b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4741ac27a0ecSDave Kleikamp 
4742aa75f4d3SHarshad Shirwadkar 	/* Initialize fast commit stuff */
4743aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_subtid, 0);
4744aa75f4d3SHarshad Shirwadkar 	atomic_set(&sbi->s_fc_ineligible_updates, 0);
4745aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4746aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4747aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4748aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4749aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bytes = 0;
47509b5f6c9bSHarshad Shirwadkar 	ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
47519b5f6c9bSHarshad Shirwadkar 	ext4_clear_mount_flag(sb, EXT4_MF_FC_COMMITTING);
4752aa75f4d3SHarshad Shirwadkar 	spin_lock_init(&sbi->s_fc_lock);
4753aa75f4d3SHarshad Shirwadkar 	memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
47548016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions = NULL;
47558016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_size = 0;
47568016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_used = 0;
47578016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_regions_valid = 0;
47588016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes = NULL;
47598016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
47608016e29fSHarshad Shirwadkar 	sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4761aa75f4d3SHarshad Shirwadkar 
4762ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4763ac27a0ecSDave Kleikamp 
4764ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4765e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4766ac27a0ecSDave Kleikamp 
4767bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4768c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
476950460fe8SDarrick J. Wong 			goto failed_mount3a;
4770c5e06d10SJohann Lombardi 
4771ac27a0ecSDave Kleikamp 	/*
4772ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4773ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4774ac27a0ecSDave Kleikamp 	 */
4775e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
47764753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
47774753d8a2STheodore Ts'o 		if (err)
477850460fe8SDarrick J. Wong 			goto failed_mount3a;
4779bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4780e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4781b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4782b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4783744692dcSJiaying Zhang 		goto failed_mount_wq;
4784ac27a0ecSDave Kleikamp 	} else {
47851e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
47861e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
47871e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
47881e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
47891e381f60SDmitry Monakhov 			goto failed_mount_wq;
47901e381f60SDmitry Monakhov 		}
47911e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
47921e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
47931e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
47941e381f60SDmitry Monakhov 			goto failed_mount_wq;
47951e381f60SDmitry Monakhov 		}
47961e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
47971e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
47981e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
47991e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
48001e381f60SDmitry Monakhov 			goto failed_mount_wq;
48011e381f60SDmitry Monakhov 		}
48021e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
48031e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
48041e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
48051e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
48061e381f60SDmitry Monakhov 			goto failed_mount_wq;
48071e381f60SDmitry Monakhov 		}
480850b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
48091e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4810fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
4811995a3ed6SHarshad Shirwadkar 		clear_opt2(sb, JOURNAL_FAST_COMMIT);
48120390131bSFrank Mayhar 		sbi->s_journal = NULL;
48130390131bSFrank Mayhar 		needs_recovery = 0;
48140390131bSFrank Mayhar 		goto no_journal;
4815ac27a0ecSDave Kleikamp 	}
4816ac27a0ecSDave Kleikamp 
4817e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4818eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4819eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4820b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4821744692dcSJiaying Zhang 		goto failed_mount_wq;
4822eb40a09cSJose R. Santos 	}
4823eb40a09cSJose R. Santos 
482425ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
482525ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
482625ed6e8aSDarrick J. Wong 			 "feature set");
482725ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4828d4da6c9cSLinus Torvalds 	}
4829818d276cSGirish Shilamkar 
4830a1e5e465SHarshad Shirwadkar 	if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4831a1e5e465SHarshad Shirwadkar 		!jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4832a1e5e465SHarshad Shirwadkar 					  JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4833a1e5e465SHarshad Shirwadkar 		ext4_msg(sb, KERN_ERR,
4834a1e5e465SHarshad Shirwadkar 			"Failed to set fast commit journal feature");
4835a1e5e465SHarshad Shirwadkar 		goto failed_mount_wq;
4836a1e5e465SHarshad Shirwadkar 	}
4837a1e5e465SHarshad Shirwadkar 
4838ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4839ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4840ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4841ac27a0ecSDave Kleikamp 	case 0:
4842ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
484363f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
484463f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
484563f57933SAndrew Morton 		 */
4846dab291afSMingming Cao 		if (jbd2_journal_check_available_features
484727f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4848fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
484927f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
485027f394a7STyson Nottingham 		} else {
4851fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
485227f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
485327f394a7STyson Nottingham 		}
4854ac27a0ecSDave Kleikamp 		break;
4855ac27a0ecSDave Kleikamp 
4856617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4857617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4858dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4859dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4860b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4861b31e1552SEric Sandeen 			       "requested data journaling mode");
4862744692dcSJiaying Zhang 			goto failed_mount_wq;
4863ac27a0ecSDave Kleikamp 		}
48645a150bdeSGustavo A. R. Silva 		break;
4865ac27a0ecSDave Kleikamp 	default:
4866ac27a0ecSDave Kleikamp 		break;
4867ac27a0ecSDave Kleikamp 	}
4868ab04df78SJan Kara 
4869ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4870ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4871ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4872ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4873ab04df78SJan Kara 		goto failed_mount_wq;
4874ab04df78SJan Kara 	}
4875ab04df78SJan Kara 
4876b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4877ac27a0ecSDave Kleikamp 
4878342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_submit_inode_data_buffers =
4879afb585a9SMauricio Faria de Oliveira 		ext4_journal_submit_inode_data_buffers;
4880342af94eSMauricio Faria de Oliveira 	sbi->s_journal->j_finish_inode_data_buffers =
4881afb585a9SMauricio Faria de Oliveira 		ext4_journal_finish_inode_data_buffers;
488218aadd47SBobi Jam 
4883ce7e010aSTheodore Ts'o no_journal:
4884cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
488547387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
488647387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4887cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4888cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
48899c191f70ST Makphaibulchoke 			goto failed_mount_wq;
48909c191f70ST Makphaibulchoke 		}
48919c191f70ST Makphaibulchoke 
4892dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4893dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4894dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4895dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4896dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4897dec214d0STahsin Erdogan 				goto failed_mount_wq;
4898dec214d0STahsin Erdogan 			}
4899dec214d0STahsin Erdogan 		}
4900cdb7ee4cSTahsin Erdogan 	}
4901dec214d0STahsin Erdogan 
4902c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4903c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4904c93d8f88SEric Biggers 		goto failed_mount_wq;
4905c93d8f88SEric Biggers 	}
4906c93d8f88SEric Biggers 
4907bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4908e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4909e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
49104392fbc4SJan Kara 		ext4_commit_super(sb);
49116ddb2447STheodore Ts'o 	}
49126ddb2447STheodore Ts'o 
4913fd89d5f2STejun Heo 	/*
4914952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4915952fc18eSTheodore Ts'o 	 * superblock if present.
4916952fc18eSTheodore Ts'o 	 */
4917952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4918952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4919952fc18eSTheodore Ts'o 	else {
492007aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
492107aa2ea1SLukas Czerner 		if (err)
4922952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4923952fc18eSTheodore Ts'o 	}
4924952fc18eSTheodore Ts'o 
4925952fc18eSTheodore Ts'o 	/*
4926fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4927fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4928fd89d5f2STejun Heo 	 */
49292e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
49302e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
49312e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
49322e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
493307aa2ea1SLukas Czerner 		ret = -ENOMEM;
49342e8fa54eSJan Kara 		goto failed_mount4;
49352e8fa54eSJan Kara 	}
49362e8fa54eSJan Kara 
4937ac27a0ecSDave Kleikamp 	/*
4938dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4939ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4940ac27a0ecSDave Kleikamp 	 */
4941ac27a0ecSDave Kleikamp 
49428a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
49431d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4944b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
49451d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
494632a9bb57SManish Katiyar 		root = NULL;
4947ac27a0ecSDave Kleikamp 		goto failed_mount4;
4948ac27a0ecSDave Kleikamp 	}
4949ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4950b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
495194bf608aSAl Viro 		iput(root);
4952ac27a0ecSDave Kleikamp 		goto failed_mount4;
4953ac27a0ecSDave Kleikamp 	}
4954b886ee3eSGabriel Krisman Bertazi 
495548fde701SAl Viro 	sb->s_root = d_make_root(root);
49561d1fe1eeSDavid Howells 	if (!sb->s_root) {
4957b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
49581d1fe1eeSDavid Howells 		ret = -ENOMEM;
49591d1fe1eeSDavid Howells 		goto failed_mount4;
49601d1fe1eeSDavid Howells 	}
4961ac27a0ecSDave Kleikamp 
4962c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4963c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
49641751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4965c89128a0SJaegeuk Kim 		ret = 0;
4966c89128a0SJaegeuk Kim 	} else if (ret)
4967c89128a0SJaegeuk Kim 		goto failed_mount4a;
4968ef7f3835SKalpak Shah 
4969b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
497027dd4385SLukas Czerner 
49710f5bde1dSJan Kara 	if (test_opt(sb, BLOCK_VALIDITY)) {
49726fd058f7STheodore Ts'o 		err = ext4_setup_system_zone(sb);
49736fd058f7STheodore Ts'o 		if (err) {
4974b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "failed to initialize system "
4975fbe845ddSCurt Wohlgemuth 				 "zone (%d)", err);
4976f9ae9cf5STheodore Ts'o 			goto failed_mount4a;
4977f9ae9cf5STheodore Ts'o 		}
49780f5bde1dSJan Kara 	}
49798016e29fSHarshad Shirwadkar 	ext4_fc_replay_cleanup(sb);
4980f9ae9cf5STheodore Ts'o 
4981f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4982f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4983f9ae9cf5STheodore Ts'o 	if (err) {
4984f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4985f9ae9cf5STheodore Ts'o 			 err);
4986dcf2d804STao Ma 		goto failed_mount5;
4987c2774d84SAneesh Kumar K.V 	}
4988c2774d84SAneesh Kumar K.V 
4989027f14f5STheodore Ts'o 	/*
4990027f14f5STheodore Ts'o 	 * We can only set up the journal commit callback once
4991027f14f5STheodore Ts'o 	 * mballoc is initialized
4992027f14f5STheodore Ts'o 	 */
4993027f14f5STheodore Ts'o 	if (sbi->s_journal)
4994027f14f5STheodore Ts'o 		sbi->s_journal->j_commit_callback =
4995027f14f5STheodore Ts'o 			ext4_journal_commit_callback;
4996027f14f5STheodore Ts'o 
4997d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4998d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4999d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
5000908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
5001908c7f19STejun Heo 				  GFP_KERNEL);
5002d5e03cbbSTheodore Ts'o 	if (!err) {
5003d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
5004d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
5005908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
5006908c7f19STejun Heo 					  GFP_KERNEL);
5007d5e03cbbSTheodore Ts'o 	}
5008d5e03cbbSTheodore Ts'o 	if (!err)
5009d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
5010908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
5011d5e03cbbSTheodore Ts'o 	if (!err)
5012908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
5013908c7f19STejun Heo 					  GFP_KERNEL);
5014c8585c6fSDaeho Jeong 	if (!err)
5015bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
5016c8585c6fSDaeho Jeong 
5017d5e03cbbSTheodore Ts'o 	if (err) {
5018d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
5019d5e03cbbSTheodore Ts'o 		goto failed_mount6;
5020d5e03cbbSTheodore Ts'o 	}
5021d5e03cbbSTheodore Ts'o 
5022e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
5023d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
5024d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
5025d5e03cbbSTheodore Ts'o 			       "unable to initialize "
5026d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
5027d5e03cbbSTheodore Ts'o 			goto failed_mount6;
5028d5e03cbbSTheodore Ts'o 		}
5029d5e03cbbSTheodore Ts'o 
5030bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
5031bfff6873SLukas Czerner 	if (err)
5032dcf2d804STao Ma 		goto failed_mount6;
5033bfff6873SLukas Czerner 
5034b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
5035dcf2d804STao Ma 	if (err)
5036dcf2d804STao Ma 		goto failed_mount7;
50373197ebdbSTheodore Ts'o 
50389b2ff357SJan Kara #ifdef CONFIG_QUOTA
50399b2ff357SJan Kara 	/* Enable quota usage during mount. */
5040bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
50419b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
50429b2ff357SJan Kara 		if (err)
50439b2ff357SJan Kara 			goto failed_mount8;
50449b2ff357SJan Kara 	}
50459b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
50469b2ff357SJan Kara 
5047bc71726cSzhangyi (F) 	/*
5048bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
5049bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
5050bc71726cSzhangyi (F) 	 */
5051bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
5052bc71726cSzhangyi (F) 	errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5053bc71726cSzhangyi (F) 				 &sbi->s_bdev_wb_err);
5054bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
5055617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5056617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
5057617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
50580390131bSFrank Mayhar 	if (needs_recovery) {
5059b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
506011215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
506111215630SJan Kara 		if (err)
506211215630SJan Kara 			goto failed_mount8;
50630390131bSFrank Mayhar 	}
50640390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
50650390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
50660390131bSFrank Mayhar 			descr = " journalled data mode";
50670390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
50680390131bSFrank Mayhar 			descr = " ordered data mode";
50690390131bSFrank Mayhar 		else
50700390131bSFrank Mayhar 			descr = " writeback data mode";
50710390131bSFrank Mayhar 	} else
50720390131bSFrank Mayhar 		descr = "out journal";
50730390131bSFrank Mayhar 
507479add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
507579add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
507679add3a3SLukas Czerner 		if (!blk_queue_discard(q))
507779add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
507879add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
507979add3a3SLukas Czerner 				 "the device does not support discard");
508079add3a3SLukas Czerner 	}
508179add3a3SLukas Czerner 
5082e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
5083d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
5084ca9b404fSRoman Anufriev 			 "Opts: %.*s%s%s. Quota mode: %s.", descr,
50855aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
50865aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
5087ca9b404fSRoman Anufriev 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data,
5088ca9b404fSRoman Anufriev 			 ext4_quota_mode(sb));
5089ac27a0ecSDave Kleikamp 
509066e61a9eSTheodore Ts'o 	if (es->s_error_count)
509166e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
5092ac27a0ecSDave Kleikamp 
5093efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5094efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5095efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5096efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
50971cf006edSDmitry Monakhov 	atomic_set(&sbi->s_warning_count, 0);
50981cf006edSDmitry Monakhov 	atomic_set(&sbi->s_msg_count, 0);
5099efbed4dcSTheodore Ts'o 
5100d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5101ac27a0ecSDave Kleikamp 	return 0;
5102ac27a0ecSDave Kleikamp 
5103617ba13bSMingming Cao cantfind_ext4:
5104ac27a0ecSDave Kleikamp 	if (!silent)
5105b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5106ac27a0ecSDave Kleikamp 	goto failed_mount;
5107ac27a0ecSDave Kleikamp 
510872ba7450STheodore Ts'o failed_mount8:
5109ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
5110cb8d53d2SEric Biggers 	kobject_put(&sbi->s_kobj);
5111dcf2d804STao Ma failed_mount7:
5112dcf2d804STao Ma 	ext4_unregister_li_request(sb);
5113dcf2d804STao Ma failed_mount6:
5114f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
51157c990728SSuraj Jitindar Singh 	rcu_read_lock();
51167c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
51177c990728SSuraj Jitindar Singh 	if (flex_groups) {
51187c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
51197c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
51207c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
51217c990728SSuraj Jitindar Singh 	}
51227c990728SSuraj Jitindar Singh 	rcu_read_unlock();
5123d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
5124d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
5125d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
5126d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
5127bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
512800764937SAzat Khuzhin failed_mount5:
5129f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
5130f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
5131f9ae9cf5STheodore Ts'o failed_mount4a:
513294bf608aSAl Viro 	dput(sb->s_root);
513332a9bb57SManish Katiyar 	sb->s_root = NULL;
513494bf608aSAl Viro failed_mount4:
5135b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
51362e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
51372e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
51384c0425ffSMingming Cao failed_mount_wq:
5139dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5140dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
514150c15df6SChengguang Xu 
514247387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
514347387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
514450c15df6SChengguang Xu 
51450390131bSFrank Mayhar 	if (sbi->s_journal) {
5146dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
514747b4a50bSJan Kara 		sbi->s_journal = NULL;
51480390131bSFrank Mayhar 	}
514950460fe8SDarrick J. Wong failed_mount3a:
5150d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
5151eb68d0e2SZheng Liu failed_mount3:
51529105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
5153c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
5154c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
5155c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
5156ac27a0ecSDave Kleikamp failed_mount2:
51571d0c3924STheodore Ts'o 	rcu_read_lock();
51581d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
5159ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
51601d0c3924STheodore Ts'o 		brelse(group_desc[i]);
51611d0c3924STheodore Ts'o 	kvfree(group_desc);
51621d0c3924STheodore Ts'o 	rcu_read_unlock();
5163ac27a0ecSDave Kleikamp failed_mount:
51640441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
51650441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
5166c83ad55eSGabriel Krisman Bertazi 
5167c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
5168f8f4acb6SDaniel Rosenberg 	utf8_unload(sb->s_encoding);
5169c83ad55eSGabriel Krisman Bertazi #endif
5170c83ad55eSGabriel Krisman Bertazi 
5171ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5172a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
51730ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
5174ac27a0ecSDave Kleikamp #endif
5175ac4acb1fSEric Biggers 	fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5176617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
5177ac27a0ecSDave Kleikamp 	brelse(bh);
5178ac27a0ecSDave Kleikamp out_fail:
5179ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
5180f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
51815aee0f8aSTheodore Ts'o out_free_base:
5182ac27a0ecSDave Kleikamp 	kfree(sbi);
5183d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
51845e405595SDan Williams 	fs_put_dax(dax_dev);
518507aa2ea1SLukas Czerner 	return err ? err : ret;
5186ac27a0ecSDave Kleikamp }
5187ac27a0ecSDave Kleikamp 
5188ac27a0ecSDave Kleikamp /*
5189ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
5190ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
5191ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
5192ac27a0ecSDave Kleikamp  */
5193617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
5194ac27a0ecSDave Kleikamp {
5195617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5196ac27a0ecSDave Kleikamp 
5197ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
519830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
519930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
52006866d7b3SHarshad Shirwadkar 	ext4_fc_init(sb, journal);
5201ac27a0ecSDave Kleikamp 
5202a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
5203ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
5204dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
5205ac27a0ecSDave Kleikamp 	else
5206dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
52075bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
52085bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
52095bf5683aSHidehiro Kawai 	else
52105bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
5211a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
5212ac27a0ecSDave Kleikamp }
5213ac27a0ecSDave Kleikamp 
5214c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
5215ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
5216ac27a0ecSDave Kleikamp {
5217ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
5218ac27a0ecSDave Kleikamp 
5219c6cb7e77SEric Whitney 	/*
5220c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
5221c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
5222c6cb7e77SEric Whitney 	 * will try to delete it.
5223c6cb7e77SEric Whitney 	 */
52248a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
52251d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
5226b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
5227ac27a0ecSDave Kleikamp 		return NULL;
5228ac27a0ecSDave Kleikamp 	}
5229ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
5230ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
5231ac27a0ecSDave Kleikamp 		iput(journal_inode);
5232b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
5233ac27a0ecSDave Kleikamp 		return NULL;
5234ac27a0ecSDave Kleikamp 	}
5235ac27a0ecSDave Kleikamp 
5236e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
5237ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
52381d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
5239b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
5240ac27a0ecSDave Kleikamp 		iput(journal_inode);
5241ac27a0ecSDave Kleikamp 		return NULL;
5242ac27a0ecSDave Kleikamp 	}
5243c6cb7e77SEric Whitney 	return journal_inode;
5244c6cb7e77SEric Whitney }
5245c6cb7e77SEric Whitney 
5246c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
5247c6cb7e77SEric Whitney 				   unsigned int journal_inum)
5248c6cb7e77SEric Whitney {
5249c6cb7e77SEric Whitney 	struct inode *journal_inode;
5250c6cb7e77SEric Whitney 	journal_t *journal;
5251c6cb7e77SEric Whitney 
525211215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
525311215630SJan Kara 		return NULL;
5254c6cb7e77SEric Whitney 
5255c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
5256c6cb7e77SEric Whitney 	if (!journal_inode)
5257c6cb7e77SEric Whitney 		return NULL;
5258ac27a0ecSDave Kleikamp 
5259dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
5260ac27a0ecSDave Kleikamp 	if (!journal) {
5261b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
5262ac27a0ecSDave Kleikamp 		iput(journal_inode);
5263ac27a0ecSDave Kleikamp 		return NULL;
5264ac27a0ecSDave Kleikamp 	}
5265ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5266617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5267ac27a0ecSDave Kleikamp 	return journal;
5268ac27a0ecSDave Kleikamp }
5269ac27a0ecSDave Kleikamp 
5270617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
5271ac27a0ecSDave Kleikamp 				       dev_t j_dev)
5272ac27a0ecSDave Kleikamp {
5273ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5274ac27a0ecSDave Kleikamp 	journal_t *journal;
5275617ba13bSMingming Cao 	ext4_fsblk_t start;
5276617ba13bSMingming Cao 	ext4_fsblk_t len;
5277ac27a0ecSDave Kleikamp 	int hblock, blocksize;
5278617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
5279ac27a0ecSDave Kleikamp 	unsigned long offset;
5280617ba13bSMingming Cao 	struct ext4_super_block *es;
5281ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5282ac27a0ecSDave Kleikamp 
528311215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
528411215630SJan Kara 		return NULL;
52850390131bSFrank Mayhar 
5286b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5287ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5288ac27a0ecSDave Kleikamp 		return NULL;
5289ac27a0ecSDave Kleikamp 
5290ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5291e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5292ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5293b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5294b31e1552SEric Sandeen 			"blocksize too small for journal device");
5295ac27a0ecSDave Kleikamp 		goto out_bdev;
5296ac27a0ecSDave Kleikamp 	}
5297ac27a0ecSDave Kleikamp 
5298617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5299617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5300ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5301ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5302b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5303b31e1552SEric Sandeen 		       "external journal");
5304ac27a0ecSDave Kleikamp 		goto out_bdev;
5305ac27a0ecSDave Kleikamp 	}
5306ac27a0ecSDave Kleikamp 
53072716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5308617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5309ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5310617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5311b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5312b31e1552SEric Sandeen 					"bad superblock");
5313ac27a0ecSDave Kleikamp 		brelse(bh);
5314ac27a0ecSDave Kleikamp 		goto out_bdev;
5315ac27a0ecSDave Kleikamp 	}
5316ac27a0ecSDave Kleikamp 
5317df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5318df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5319df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5320df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5321df4763beSDarrick J. Wong 				       "corrupt superblock");
5322df4763beSDarrick J. Wong 		brelse(bh);
5323df4763beSDarrick J. Wong 		goto out_bdev;
5324df4763beSDarrick J. Wong 	}
5325df4763beSDarrick J. Wong 
5326617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5327b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5328ac27a0ecSDave Kleikamp 		brelse(bh);
5329ac27a0ecSDave Kleikamp 		goto out_bdev;
5330ac27a0ecSDave Kleikamp 	}
5331ac27a0ecSDave Kleikamp 
5332bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5333ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5334ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5335ac27a0ecSDave Kleikamp 
5336dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5337ac27a0ecSDave Kleikamp 					start, len, blocksize);
5338ac27a0ecSDave Kleikamp 	if (!journal) {
5339b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5340ac27a0ecSDave Kleikamp 		goto out_bdev;
5341ac27a0ecSDave Kleikamp 	}
5342ac27a0ecSDave Kleikamp 	journal->j_private = sb;
53432d069c08Szhangyi (F) 	if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
5344b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5345ac27a0ecSDave Kleikamp 		goto out_journal;
5346ac27a0ecSDave Kleikamp 	}
5347ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5348b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5349b31e1552SEric Sandeen 					"user (unsupported) - %d",
5350ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5351ac27a0ecSDave Kleikamp 		goto out_journal;
5352ac27a0ecSDave Kleikamp 	}
5353ee7ed3aaSChunguang Xu 	EXT4_SB(sb)->s_journal_bdev = bdev;
5354617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5355ac27a0ecSDave Kleikamp 	return journal;
53560b8e58a1SAndreas Dilger 
5357ac27a0ecSDave Kleikamp out_journal:
5358dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5359ac27a0ecSDave Kleikamp out_bdev:
5360617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5361ac27a0ecSDave Kleikamp 	return NULL;
5362ac27a0ecSDave Kleikamp }
5363ac27a0ecSDave Kleikamp 
5364617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5365617ba13bSMingming Cao 			     struct ext4_super_block *es,
5366ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5367ac27a0ecSDave Kleikamp {
5368ac27a0ecSDave Kleikamp 	journal_t *journal;
5369ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5370ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5371ac27a0ecSDave Kleikamp 	int err = 0;
5372ac27a0ecSDave Kleikamp 	int really_read_only;
5373273108faSLukas Czerner 	int journal_dev_ro;
5374ac27a0ecSDave Kleikamp 
537511215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
537611215630SJan Kara 		return -EFSCORRUPTED;
53770390131bSFrank Mayhar 
5378ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5379ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5380b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5381b31e1552SEric Sandeen 			"numbers have changed");
5382ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5383ac27a0ecSDave Kleikamp 	} else
5384ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5385ac27a0ecSDave Kleikamp 
5386273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5387273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5388273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5389273108faSLukas Czerner 		return -EINVAL;
5390273108faSLukas Czerner 	}
5391273108faSLukas Czerner 
5392273108faSLukas Czerner 	if (journal_inum) {
5393273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5394273108faSLukas Czerner 		if (!journal)
5395273108faSLukas Czerner 			return -EINVAL;
5396273108faSLukas Czerner 	} else {
5397273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5398273108faSLukas Czerner 		if (!journal)
5399273108faSLukas Czerner 			return -EINVAL;
5400273108faSLukas Czerner 	}
5401273108faSLukas Czerner 
5402273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5403273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5404273108faSLukas Czerner 
5405273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5406273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5407273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5408273108faSLukas Czerner 		err = -EROFS;
5409273108faSLukas Czerner 		goto err_out;
5410273108faSLukas Czerner 	}
5411ac27a0ecSDave Kleikamp 
5412ac27a0ecSDave Kleikamp 	/*
5413ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5414ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5415ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5416ac27a0ecSDave Kleikamp 	 */
5417e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5418bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5419b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5420b31e1552SEric Sandeen 					"required on readonly filesystem");
5421ac27a0ecSDave Kleikamp 			if (really_read_only) {
5422b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5423d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5424d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5425273108faSLukas Czerner 				err = -EROFS;
5426273108faSLukas Czerner 				goto err_out;
5427ac27a0ecSDave Kleikamp 			}
5428b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5429b31e1552SEric Sandeen 			       "be enabled during recovery");
5430ac27a0ecSDave Kleikamp 		}
5431ac27a0ecSDave Kleikamp 	}
5432ac27a0ecSDave Kleikamp 
543390576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5434b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
54354776004fSTheodore Ts'o 
5436e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5437dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
54381c13d5c0STheodore Ts'o 	if (!err) {
54391c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
54401c13d5c0STheodore Ts'o 		if (save)
54411c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
54421c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5443dab291afSMingming Cao 		err = jbd2_journal_load(journal);
54441c13d5c0STheodore Ts'o 		if (save)
54451c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
54461c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
54471c13d5c0STheodore Ts'o 		kfree(save);
54481c13d5c0STheodore Ts'o 	}
5449ac27a0ecSDave Kleikamp 
5450ac27a0ecSDave Kleikamp 	if (err) {
5451b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5452273108faSLukas Czerner 		goto err_out;
5453ac27a0ecSDave Kleikamp 	}
5454ac27a0ecSDave Kleikamp 
5455617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
545611215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
545711215630SJan Kara 	if (err) {
545811215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
545911215630SJan Kara 		jbd2_journal_destroy(journal);
546011215630SJan Kara 		return err;
546111215630SJan Kara 	}
5462ac27a0ecSDave Kleikamp 
5463c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5464ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5465ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5466ac27a0ecSDave Kleikamp 
5467ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
54684392fbc4SJan Kara 		ext4_commit_super(sb);
5469ac27a0ecSDave Kleikamp 	}
5470ac27a0ecSDave Kleikamp 
5471ac27a0ecSDave Kleikamp 	return 0;
5472273108faSLukas Czerner 
5473273108faSLukas Czerner err_out:
5474273108faSLukas Czerner 	jbd2_journal_destroy(journal);
5475273108faSLukas Czerner 	return err;
5476ac27a0ecSDave Kleikamp }
5477ac27a0ecSDave Kleikamp 
54782d01ddc8SJan Kara /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
54792d01ddc8SJan Kara static void ext4_update_super(struct super_block *sb)
5480ac27a0ecSDave Kleikamp {
5481c92dc856SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5482e92ad03fSJan Kara 	struct ext4_super_block *es = sbi->s_es;
5483e92ad03fSJan Kara 	struct buffer_head *sbh = sbi->s_sbh;
5484ac27a0ecSDave Kleikamp 
548505c2c00fSJan Kara 	lock_buffer(sbh);
5486a17712c8SJon Derrick 	/*
548771290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
548871290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
548971290b36STheodore Ts'o 	 * write time when we are mounting the root file system
549071290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
549171290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
549271290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
549371290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
549471290b36STheodore Ts'o 	 * to complain and force a full file system check.
549571290b36STheodore Ts'o 	 */
54961751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
54976a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5498afc32f7eSTheodore Ts'o 	es->s_kbytes_written =
5499e92ad03fSJan Kara 		cpu_to_le64(sbi->s_kbytes_written +
55008446fe92SChristoph Hellwig 		    ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
5501e92ad03fSJan Kara 		      sbi->s_sectors_written_start) >> 1));
5502e92ad03fSJan Kara 	if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
550357042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
5504e92ad03fSJan Kara 			EXT4_C2B(sbi, percpu_counter_sum_positive(
5505e92ad03fSJan Kara 				&sbi->s_freeclusters_counter)));
5506e92ad03fSJan Kara 	if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
55077f93cff9STheodore Ts'o 		es->s_free_inodes_count =
55087f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
5509e92ad03fSJan Kara 				&sbi->s_freeinodes_counter));
5510c92dc856SJan Kara 	/* Copy error information to the on-disk superblock */
5511c92dc856SJan Kara 	spin_lock(&sbi->s_error_lock);
5512c92dc856SJan Kara 	if (sbi->s_add_error_count > 0) {
5513c92dc856SJan Kara 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5514c92dc856SJan Kara 		if (!es->s_first_error_time && !es->s_first_error_time_hi) {
5515c92dc856SJan Kara 			__ext4_update_tstamp(&es->s_first_error_time,
5516c92dc856SJan Kara 					     &es->s_first_error_time_hi,
5517c92dc856SJan Kara 					     sbi->s_first_error_time);
5518c92dc856SJan Kara 			strncpy(es->s_first_error_func, sbi->s_first_error_func,
5519c92dc856SJan Kara 				sizeof(es->s_first_error_func));
5520c92dc856SJan Kara 			es->s_first_error_line =
5521c92dc856SJan Kara 				cpu_to_le32(sbi->s_first_error_line);
5522c92dc856SJan Kara 			es->s_first_error_ino =
5523c92dc856SJan Kara 				cpu_to_le32(sbi->s_first_error_ino);
5524c92dc856SJan Kara 			es->s_first_error_block =
5525c92dc856SJan Kara 				cpu_to_le64(sbi->s_first_error_block);
5526c92dc856SJan Kara 			es->s_first_error_errcode =
5527c92dc856SJan Kara 				ext4_errno_to_code(sbi->s_first_error_code);
5528c92dc856SJan Kara 		}
5529c92dc856SJan Kara 		__ext4_update_tstamp(&es->s_last_error_time,
5530c92dc856SJan Kara 				     &es->s_last_error_time_hi,
5531c92dc856SJan Kara 				     sbi->s_last_error_time);
5532c92dc856SJan Kara 		strncpy(es->s_last_error_func, sbi->s_last_error_func,
5533c92dc856SJan Kara 			sizeof(es->s_last_error_func));
5534c92dc856SJan Kara 		es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
5535c92dc856SJan Kara 		es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
5536c92dc856SJan Kara 		es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
5537c92dc856SJan Kara 		es->s_last_error_errcode =
5538c92dc856SJan Kara 				ext4_errno_to_code(sbi->s_last_error_code);
5539c92dc856SJan Kara 		/*
5540c92dc856SJan Kara 		 * Start the daily error reporting function if it hasn't been
5541c92dc856SJan Kara 		 * started already
5542c92dc856SJan Kara 		 */
5543c92dc856SJan Kara 		if (!es->s_error_count)
5544c92dc856SJan Kara 			mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
5545c92dc856SJan Kara 		le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
5546c92dc856SJan Kara 		sbi->s_add_error_count = 0;
5547c92dc856SJan Kara 	}
5548c92dc856SJan Kara 	spin_unlock(&sbi->s_error_lock);
5549c92dc856SJan Kara 
555006db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
55512d01ddc8SJan Kara 	unlock_buffer(sbh);
55522d01ddc8SJan Kara }
55532d01ddc8SJan Kara 
55542d01ddc8SJan Kara static int ext4_commit_super(struct super_block *sb)
55552d01ddc8SJan Kara {
55562d01ddc8SJan Kara 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
55572d01ddc8SJan Kara 	int error = 0;
55582d01ddc8SJan Kara 
55592d01ddc8SJan Kara 	if (!sbh || block_device_ejected(sb))
55602d01ddc8SJan Kara 		return error;
55612d01ddc8SJan Kara 
55622d01ddc8SJan Kara 	ext4_update_super(sb);
55632d01ddc8SJan Kara 
5564e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
55654743f839SPranay Kr. Srivastava 		/*
55664743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
55674743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
55684743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
55694743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
55704743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
55714743f839SPranay Kr. Srivastava 		 * write and hope for the best.
55724743f839SPranay Kr. Srivastava 		 */
55734743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
55744743f839SPranay Kr. Srivastava 		       "superblock detected");
55754743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
55764743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
55774743f839SPranay Kr. Srivastava 	}
55782d01ddc8SJan Kara 	BUFFER_TRACE(sbh, "marking dirty");
5579ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5580564bc402SDaeho Jeong 	error = __sync_dirty_buffer(sbh,
558100473374SJan Kara 		REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5582c89128a0SJaegeuk Kim 	if (buffer_write_io_error(sbh)) {
5583b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error while writing "
5584b31e1552SEric Sandeen 		       "superblock");
5585914258bfSTheodore Ts'o 		clear_buffer_write_io_error(sbh);
5586914258bfSTheodore Ts'o 		set_buffer_uptodate(sbh);
5587914258bfSTheodore Ts'o 	}
5588c4be0c1dSTakashi Sato 	return error;
5589ac27a0ecSDave Kleikamp }
5590ac27a0ecSDave Kleikamp 
5591ac27a0ecSDave Kleikamp /*
5592ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5593ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5594ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5595ac27a0ecSDave Kleikamp  */
559611215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
5597617ba13bSMingming Cao 				       struct ext4_super_block *es)
5598ac27a0ecSDave Kleikamp {
559911215630SJan Kara 	int err;
5600617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5601ac27a0ecSDave Kleikamp 
5602e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
560311215630SJan Kara 		if (journal != NULL) {
560411215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
560511215630SJan Kara 				   "mounted!");
560611215630SJan Kara 			return -EFSCORRUPTED;
560711215630SJan Kara 		}
560811215630SJan Kara 		return 0;
56090390131bSFrank Mayhar 	}
5610dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
561111215630SJan Kara 	err = jbd2_journal_flush(journal);
561211215630SJan Kara 	if (err < 0)
56137ffe1ea8SHidehiro Kawai 		goto out;
56147ffe1ea8SHidehiro Kawai 
5615bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5616e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
56174392fbc4SJan Kara 		ext4_commit_super(sb);
5618ac27a0ecSDave Kleikamp 	}
56197ffe1ea8SHidehiro Kawai out:
5620dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
562111215630SJan Kara 	return err;
5622ac27a0ecSDave Kleikamp }
5623ac27a0ecSDave Kleikamp 
5624ac27a0ecSDave Kleikamp /*
5625ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5626ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5627ac27a0ecSDave Kleikamp  * main filesystem now.
5628ac27a0ecSDave Kleikamp  */
562911215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
5630617ba13bSMingming Cao 				   struct ext4_super_block *es)
5631ac27a0ecSDave Kleikamp {
5632ac27a0ecSDave Kleikamp 	journal_t *journal;
5633ac27a0ecSDave Kleikamp 	int j_errno;
5634ac27a0ecSDave Kleikamp 	const char *errstr;
5635ac27a0ecSDave Kleikamp 
563611215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
563711215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
563811215630SJan Kara 		return -EFSCORRUPTED;
563911215630SJan Kara 	}
56400390131bSFrank Mayhar 
5641617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5642ac27a0ecSDave Kleikamp 
5643ac27a0ecSDave Kleikamp 	/*
5644ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5645617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5646ac27a0ecSDave Kleikamp 	 */
5647ac27a0ecSDave Kleikamp 
5648dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5649ac27a0ecSDave Kleikamp 	if (j_errno) {
5650ac27a0ecSDave Kleikamp 		char nbuf[16];
5651ac27a0ecSDave Kleikamp 
5652617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
565312062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5654ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
565512062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5656ac27a0ecSDave Kleikamp 
5657617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5658617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
56594392fbc4SJan Kara 		ext4_commit_super(sb);
5660ac27a0ecSDave Kleikamp 
5661dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5662d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5663ac27a0ecSDave Kleikamp 	}
566411215630SJan Kara 	return 0;
5665ac27a0ecSDave Kleikamp }
5666ac27a0ecSDave Kleikamp 
5667ac27a0ecSDave Kleikamp /*
5668ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5669ac27a0ecSDave Kleikamp  * and wait on the commit.
5670ac27a0ecSDave Kleikamp  */
5671617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5672ac27a0ecSDave Kleikamp {
5673ac27a0ecSDave Kleikamp 	journal_t *journal;
5674ac27a0ecSDave Kleikamp 
5675bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5676ac27a0ecSDave Kleikamp 		return 0;
5677ac27a0ecSDave Kleikamp 
5678617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5679b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5680ac27a0ecSDave Kleikamp }
5681ac27a0ecSDave Kleikamp 
5682617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5683ac27a0ecSDave Kleikamp {
568414ce0cb4STheodore Ts'o 	int ret = 0;
56859eddacf9SJan Kara 	tid_t target;
568606a407f1SDmitry Monakhov 	bool needs_barrier = false;
56878d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5688ac27a0ecSDave Kleikamp 
568949598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
56900db1ff22STheodore Ts'o 		return 0;
56910db1ff22STheodore Ts'o 
56929bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
56932e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5694a1177825SJan Kara 	/*
5695a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5696a1177825SJan Kara 	 * no dirty dquots
5697a1177825SJan Kara 	 */
5698a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
569906a407f1SDmitry Monakhov 	/*
570006a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
570106a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
570206a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
570306a407f1SDmitry Monakhov 	 */
5704bda32530STheodore Ts'o 	if (sbi->s_journal) {
570506a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
570606a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
570706a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
570806a407f1SDmitry Monakhov 			needs_barrier = true;
570906a407f1SDmitry Monakhov 
57108d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5711ac27a0ecSDave Kleikamp 			if (wait)
5712bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5713bda32530STheodore Ts'o 							   target);
57140390131bSFrank Mayhar 		}
5715bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5716bda32530STheodore Ts'o 		needs_barrier = true;
571706a407f1SDmitry Monakhov 	if (needs_barrier) {
571806a407f1SDmitry Monakhov 		int err;
5719c6bf3f0eSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev);
572006a407f1SDmitry Monakhov 		if (!ret)
572106a407f1SDmitry Monakhov 			ret = err;
572206a407f1SDmitry Monakhov 	}
572306a407f1SDmitry Monakhov 
572406a407f1SDmitry Monakhov 	return ret;
572506a407f1SDmitry Monakhov }
572606a407f1SDmitry Monakhov 
5727ac27a0ecSDave Kleikamp /*
5728ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5729ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5730be4f27d3SYongqiang Yang  *
5731be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
57328e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
57338e8ad8a5SJan Kara  * modifications.
5734ac27a0ecSDave Kleikamp  */
5735c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5736ac27a0ecSDave Kleikamp {
5737c4be0c1dSTakashi Sato 	int error = 0;
5738c4be0c1dSTakashi Sato 	journal_t *journal;
5739ac27a0ecSDave Kleikamp 
5740bc98a42cSDavid Howells 	if (sb_rdonly(sb))
57419ca92389STheodore Ts'o 		return 0;
57429ca92389STheodore Ts'o 
5743c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5744ac27a0ecSDave Kleikamp 
5745bb044576STheodore Ts'o 	if (journal) {
5746ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5747dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
57487ffe1ea8SHidehiro Kawai 
57497ffe1ea8SHidehiro Kawai 		/*
5750bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5751bb044576STheodore Ts'o 		 * flush the journal.
57527ffe1ea8SHidehiro Kawai 		 */
5753c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
57546b0310fbSEric Sandeen 		if (error < 0)
57556b0310fbSEric Sandeen 			goto out;
5756ac27a0ecSDave Kleikamp 
5757ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5758e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5759c642dc9eSEric Sandeen 	}
5760c642dc9eSEric Sandeen 
57614392fbc4SJan Kara 	error = ext4_commit_super(sb);
57626b0310fbSEric Sandeen out:
5763bb044576STheodore Ts'o 	if (journal)
57648e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5765bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
57666b0310fbSEric Sandeen 	return error;
5767ac27a0ecSDave Kleikamp }
5768ac27a0ecSDave Kleikamp 
5769ac27a0ecSDave Kleikamp /*
5770ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5771ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5772ac27a0ecSDave Kleikamp  */
5773c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5774ac27a0ecSDave Kleikamp {
5775bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
57769ca92389STheodore Ts'o 		return 0;
57779ca92389STheodore Ts'o 
5778c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
57799ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5780e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5781c642dc9eSEric Sandeen 	}
5782c642dc9eSEric Sandeen 
57834392fbc4SJan Kara 	ext4_commit_super(sb);
5784c4be0c1dSTakashi Sato 	return 0;
5785ac27a0ecSDave Kleikamp }
5786ac27a0ecSDave Kleikamp 
5787673c6100STheodore Ts'o /*
5788673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5789673c6100STheodore Ts'o  */
5790673c6100STheodore Ts'o struct ext4_mount_options {
5791673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5792a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
579308cefc7aSEric W. Biederman 	kuid_t s_resuid;
579408cefc7aSEric W. Biederman 	kgid_t s_resgid;
5795673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5796673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5797673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5798673c6100STheodore Ts'o 	int s_jquota_fmt;
5799a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5800673c6100STheodore Ts'o #endif
5801673c6100STheodore Ts'o };
5802673c6100STheodore Ts'o 
5803617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5804ac27a0ecSDave Kleikamp {
5805617ba13bSMingming Cao 	struct ext4_super_block *es;
5806617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5807f25391ebSLukas Czerner 	unsigned long old_sb_flags, vfs_flags;
5808617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5809c79d967dSChristoph Hellwig 	int enable_quota = 0;
58108a266467STheodore Ts'o 	ext4_group_t g;
5811b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5812c5e06d10SJohann Lombardi 	int err = 0;
5813ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
581403dafb5fSChen Gang 	int i, j;
581533458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5816ac27a0ecSDave Kleikamp #endif
5817d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5818ac27a0ecSDave Kleikamp 
581921ac738eSChengguang Xu 	if (data && !orig_data)
582021ac738eSChengguang Xu 		return -ENOMEM;
582121ac738eSChengguang Xu 
5822ac27a0ecSDave Kleikamp 	/* Store the original options */
5823ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5824ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5825a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5826ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5827ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5828ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
582930773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
583030773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5831ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5832ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5833a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
583403dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
583533458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
583633458eabSTheodore Ts'o 
583733458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
583803dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
583903dafb5fSChen Gang 				for (j = 0; j < i; j++)
584003dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
58413e36a163SWei Yongjun 				kfree(orig_data);
584203dafb5fSChen Gang 				return -ENOMEM;
584303dafb5fSChen Gang 			}
584403dafb5fSChen Gang 		} else
584503dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5846ac27a0ecSDave Kleikamp #endif
5847b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5848b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5849ac27a0ecSDave Kleikamp 
5850f25391ebSLukas Czerner 	/*
5851f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
5852f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
5853f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from *flags to s_flags
5854f25391ebSLukas Czerner 	 */
5855f25391ebSLukas Czerner 	vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5856f25391ebSLukas Czerner 	sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5857f25391ebSLukas Czerner 
5858661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5859ac27a0ecSDave Kleikamp 		err = -EINVAL;
5860ac27a0ecSDave Kleikamp 		goto restore_opts;
5861ac27a0ecSDave Kleikamp 	}
5862ac27a0ecSDave Kleikamp 
58636b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
58646b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
58656b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
58662d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
58672d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5868c6d3d56dSDarrick J. Wong 	}
5869c6d3d56dSDarrick J. Wong 
58706ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
58716ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
58726ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
58736ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
58746ae6514bSPiotr Sarna 			err = -EINVAL;
58756ae6514bSPiotr Sarna 			goto restore_opts;
58766ae6514bSPiotr Sarna 		}
58776ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
58786ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
58796ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
58806ae6514bSPiotr Sarna 			err = -EINVAL;
58816ae6514bSPiotr Sarna 			goto restore_opts;
58826ae6514bSPiotr Sarna 		}
5883ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5884ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5885ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5886ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5887ab04df78SJan Kara 			err = -EINVAL;
5888ab04df78SJan Kara 			goto restore_opts;
5889ab04df78SJan Kara 		}
5890923ae0ffSRoss Zwisler 	}
5891923ae0ffSRoss Zwisler 
5892cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5893cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5894cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5895cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5896cdb7ee4cSTahsin Erdogan 	}
5897cdb7ee4cSTahsin Erdogan 
58989b5f6c9bSHarshad Shirwadkar 	if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
589954d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5900ac27a0ecSDave Kleikamp 
59011751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
59021751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5903ac27a0ecSDave Kleikamp 
5904ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5905ac27a0ecSDave Kleikamp 
5906b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5907617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5908b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5909b3881f74STheodore Ts'o 	}
5910ac27a0ecSDave Kleikamp 
5911c92dc856SJan Kara 	/* Flush outstanding errors before changing fs state */
5912c92dc856SJan Kara 	flush_work(&sbi->s_error_work);
5913c92dc856SJan Kara 
59141751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
59159b5f6c9bSHarshad Shirwadkar 		if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) {
5916ac27a0ecSDave Kleikamp 			err = -EROFS;
5917ac27a0ecSDave Kleikamp 			goto restore_opts;
5918ac27a0ecSDave Kleikamp 		}
5919ac27a0ecSDave Kleikamp 
59201751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
592138c03b34STheodore Ts'o 			err = sync_filesystem(sb);
592238c03b34STheodore Ts'o 			if (err < 0)
592338c03b34STheodore Ts'o 				goto restore_opts;
59240f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
59250f0dd62fSChristoph Hellwig 			if (err < 0)
5926c79d967dSChristoph Hellwig 				goto restore_opts;
5927c79d967dSChristoph Hellwig 
5928ac27a0ecSDave Kleikamp 			/*
5929ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5930ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5931ac27a0ecSDave Kleikamp 			 */
59321751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5933ac27a0ecSDave Kleikamp 
5934ac27a0ecSDave Kleikamp 			/*
5935ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5936ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5937ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5938ac27a0ecSDave Kleikamp 			 */
5939617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5940617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5941ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5942ac27a0ecSDave Kleikamp 
594311215630SJan Kara 			if (sbi->s_journal) {
594411215630SJan Kara 				/*
594511215630SJan Kara 				 * We let remount-ro finish even if marking fs
594611215630SJan Kara 				 * as clean failed...
594711215630SJan Kara 				 */
5948617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
594911215630SJan Kara 			}
59502dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
59512dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5952ac27a0ecSDave Kleikamp 		} else {
5953a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5954e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
59552cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5956ac27a0ecSDave Kleikamp 				err = -EROFS;
5957ac27a0ecSDave Kleikamp 				goto restore_opts;
5958ac27a0ecSDave Kleikamp 			}
5959ead6596bSEric Sandeen 			/*
59608a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
59610b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
59628a266467STheodore Ts'o 			 */
59638a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
59648a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
59658a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
59668a266467STheodore Ts'o 
5967feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5968b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5969b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5970e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
59718a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
59726a797d27SDarrick J. Wong 					err = -EFSBADCRC;
59738a266467STheodore Ts'o 					goto restore_opts;
59748a266467STheodore Ts'o 				}
59758a266467STheodore Ts'o 			}
59768a266467STheodore Ts'o 
59778a266467STheodore Ts'o 			/*
5978ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5979ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5980ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5981ead6596bSEric Sandeen 			 */
5982ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5983b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5984ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5985ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5986b31e1552SEric Sandeen 				       "umount/remount instead");
5987ead6596bSEric Sandeen 				err = -EINVAL;
5988ead6596bSEric Sandeen 				goto restore_opts;
5989ead6596bSEric Sandeen 			}
5990ead6596bSEric Sandeen 
5991ac27a0ecSDave Kleikamp 			/*
5992ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5993ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5994ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5995ac27a0ecSDave Kleikamp 			 * the partition.)
5996ac27a0ecSDave Kleikamp 			 */
599711215630SJan Kara 			if (sbi->s_journal) {
599811215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
599911215630SJan Kara 				if (err)
600011215630SJan Kara 					goto restore_opts;
600111215630SJan Kara 			}
6002ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
6003c89128a0SJaegeuk Kim 
6004c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
6005c89128a0SJaegeuk Kim 			if (err)
6006c89128a0SJaegeuk Kim 				goto restore_opts;
6007c89128a0SJaegeuk Kim 
60081751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
6009e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
6010c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
6011c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
6012c5e06d10SJohann Lombardi 					err = -EROFS;
6013c5e06d10SJohann Lombardi 					goto restore_opts;
6014c5e06d10SJohann Lombardi 				}
6015c79d967dSChristoph Hellwig 			enable_quota = 1;
6016ac27a0ecSDave Kleikamp 		}
6017ac27a0ecSDave Kleikamp 	}
6018bfff6873SLukas Czerner 
6019bfff6873SLukas Czerner 	/*
6020bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
6021bfff6873SLukas Czerner 	 * current settings
6022bfff6873SLukas Czerner 	 */
6023bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
6024bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
6025bfff6873SLukas Czerner 	else {
6026bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
6027bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
6028bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
6029bfff6873SLukas Czerner 	}
6030bfff6873SLukas Czerner 
60310f5bde1dSJan Kara 	/*
60320f5bde1dSJan Kara 	 * Handle creation of system zone data early because it can fail.
60330f5bde1dSJan Kara 	 * Releasing of existing data is done when we are sure remount will
60340f5bde1dSJan Kara 	 * succeed.
60350f5bde1dSJan Kara 	 */
6036dd0db94fSChunguang Xu 	if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
6037d176b1f6SJan Kara 		err = ext4_setup_system_zone(sb);
6038d176b1f6SJan Kara 		if (err)
6039d176b1f6SJan Kara 			goto restore_opts;
60400f5bde1dSJan Kara 	}
6041d176b1f6SJan Kara 
6042c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
60434392fbc4SJan Kara 		err = ext4_commit_super(sb);
6044c89128a0SJaegeuk Kim 		if (err)
6045c89128a0SJaegeuk Kim 			goto restore_opts;
6046c89128a0SJaegeuk Kim 	}
60470390131bSFrank Mayhar 
6048ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6049ac27a0ecSDave Kleikamp 	/* Release old quota file names */
6050a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
6051ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
60527c319d32SAditya Kali 	if (enable_quota) {
60537c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
60540f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
6055e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
60567c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
605707724f98STheodore Ts'o 			if (err)
60587c319d32SAditya Kali 				goto restore_opts;
60597c319d32SAditya Kali 		}
60607c319d32SAditya Kali 	}
60617c319d32SAditya Kali #endif
6062dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
60630f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6064d4c402d9SCurt Wohlgemuth 
6065f25391ebSLukas Czerner 	/*
6066f25391ebSLukas Czerner 	 * Some options can be enabled by ext4 and/or by VFS mount flag
6067f25391ebSLukas Czerner 	 * either way we need to make sure it matches in both *flags and
6068f25391ebSLukas Czerner 	 * s_flags. Copy those selected flags from s_flags to *flags
6069f25391ebSLukas Czerner 	 */
6070f25391ebSLukas Czerner 	*flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
6071f25391ebSLukas Czerner 
6072ca9b404fSRoman Anufriev 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s. Quota mode: %s.",
6073ca9b404fSRoman Anufriev 		 orig_data, ext4_quota_mode(sb));
6074d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
6075ac27a0ecSDave Kleikamp 	return 0;
60760b8e58a1SAndreas Dilger 
6077ac27a0ecSDave Kleikamp restore_opts:
6078ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
6079ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
6080a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
6081ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
6082ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
6083ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
608430773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
608530773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
6086dd0db94fSChunguang Xu 	if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
60870f5bde1dSJan Kara 		ext4_release_system_zone(sb);
6088ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6089ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
6090a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
609133458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
609233458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
6093ac27a0ecSDave Kleikamp 	}
609433458eabSTheodore Ts'o 	synchronize_rcu();
609533458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
609633458eabSTheodore Ts'o 		kfree(to_free[i]);
6097ac27a0ecSDave Kleikamp #endif
6098d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
6099ac27a0ecSDave Kleikamp 	return err;
6100ac27a0ecSDave Kleikamp }
6101ac27a0ecSDave Kleikamp 
6102689c958cSLi Xi #ifdef CONFIG_QUOTA
6103689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
6104689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
6105689c958cSLi Xi {
6106689c958cSLi Xi 	struct kqid qid;
6107689c958cSLi Xi 	struct dquot *dquot;
6108689c958cSLi Xi 	u64 limit;
6109689c958cSLi Xi 	u64 curblock;
6110689c958cSLi Xi 
6111689c958cSLi Xi 	qid = make_kqid_projid(projid);
6112689c958cSLi Xi 	dquot = dqget(sb, qid);
6113689c958cSLi Xi 	if (IS_ERR(dquot))
6114689c958cSLi Xi 		return PTR_ERR(dquot);
61157b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
6116689c958cSLi Xi 
6117a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6118a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
611957c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
612057c32ea4SChengguang Xu 
6121689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
6122f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
6123f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
6124689c958cSLi Xi 		buf->f_blocks = limit;
6125689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
6126689c958cSLi Xi 			(buf->f_blocks > curblock) ?
6127689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
6128689c958cSLi Xi 	}
6129689c958cSLi Xi 
6130a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6131a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
6132689c958cSLi Xi 	if (limit && buf->f_files > limit) {
6133689c958cSLi Xi 		buf->f_files = limit;
6134689c958cSLi Xi 		buf->f_ffree =
6135689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6136689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6137689c958cSLi Xi 	}
6138689c958cSLi Xi 
61397b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
6140689c958cSLi Xi 	dqput(dquot);
6141689c958cSLi Xi 	return 0;
6142689c958cSLi Xi }
6143689c958cSLi Xi #endif
6144689c958cSLi Xi 
6145617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
6146ac27a0ecSDave Kleikamp {
6147ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
6148617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
6149617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
615027dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
6151d02a9391SKazuya Mio 	s64 bfree;
615227dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
6153ac27a0ecSDave Kleikamp 
6154952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
6155952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
6156ac27a0ecSDave Kleikamp 
6157617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
6158ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
6159b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
616057042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
616157042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
6162d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
616357042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
616427dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
616527dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
616627dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
6167ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
6168ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
616952d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
6170617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
6171*9591c3a3SAmir Goldstein 	buf->f_fsid = uuid_to_fsid(es->s_uuid);
61720b8e58a1SAndreas Dilger 
6173689c958cSLi Xi #ifdef CONFIG_QUOTA
6174689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6175689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
6176689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6177689c958cSLi Xi #endif
6178ac27a0ecSDave Kleikamp 	return 0;
6179ac27a0ecSDave Kleikamp }
6180ac27a0ecSDave Kleikamp 
6181ac27a0ecSDave Kleikamp 
6182ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
6183ac27a0ecSDave Kleikamp 
6184bc8230eeSJan Kara /*
6185bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
6186bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
6187bc8230eeSJan Kara  */
6188ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
6189ac27a0ecSDave Kleikamp {
61904c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
6191ac27a0ecSDave Kleikamp }
6192ac27a0ecSDave Kleikamp 
6193617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
6194ac27a0ecSDave Kleikamp {
6195ac27a0ecSDave Kleikamp 	int ret, err;
6196ac27a0ecSDave Kleikamp 	handle_t *handle;
6197ac27a0ecSDave Kleikamp 	struct inode *inode;
6198ac27a0ecSDave Kleikamp 
6199ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
62009924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
6201617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
6202ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6203ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6204ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
6205617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6206ac27a0ecSDave Kleikamp 	if (!ret)
6207ac27a0ecSDave Kleikamp 		ret = err;
6208ac27a0ecSDave Kleikamp 	return ret;
6209ac27a0ecSDave Kleikamp }
6210ac27a0ecSDave Kleikamp 
6211617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
6212ac27a0ecSDave Kleikamp {
6213ac27a0ecSDave Kleikamp 	int ret, err;
6214ac27a0ecSDave Kleikamp 	handle_t *handle;
6215ac27a0ecSDave Kleikamp 
62169924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6217617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
6218ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6219ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6220ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
6221617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6222ac27a0ecSDave Kleikamp 	if (!ret)
6223ac27a0ecSDave Kleikamp 		ret = err;
6224ac27a0ecSDave Kleikamp 	return ret;
6225ac27a0ecSDave Kleikamp }
6226ac27a0ecSDave Kleikamp 
6227617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
6228ac27a0ecSDave Kleikamp {
6229ac27a0ecSDave Kleikamp 	int ret, err;
6230ac27a0ecSDave Kleikamp 	handle_t *handle;
6231ac27a0ecSDave Kleikamp 
62329924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
6233617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
62349c3013e9SJan Kara 	if (IS_ERR(handle)) {
62359c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
62369c3013e9SJan Kara 		dquot_release(dquot);
6237ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
62389c3013e9SJan Kara 	}
6239ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
6240617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6241ac27a0ecSDave Kleikamp 	if (!ret)
6242ac27a0ecSDave Kleikamp 		ret = err;
6243ac27a0ecSDave Kleikamp 	return ret;
6244ac27a0ecSDave Kleikamp }
6245ac27a0ecSDave Kleikamp 
6246617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
6247ac27a0ecSDave Kleikamp {
6248262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
6249262b4662SJan Kara 
6250f177ee08SRoman Anufriev 	if (ext4_is_quota_journalled(sb)) {
6251ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
6252617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
6253ac27a0ecSDave Kleikamp 	} else {
6254ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
6255ac27a0ecSDave Kleikamp 	}
6256ac27a0ecSDave Kleikamp }
6257ac27a0ecSDave Kleikamp 
6258617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
6259ac27a0ecSDave Kleikamp {
6260ac27a0ecSDave Kleikamp 	int ret, err;
6261ac27a0ecSDave Kleikamp 	handle_t *handle;
6262ac27a0ecSDave Kleikamp 
6263ac27a0ecSDave Kleikamp 	/* Data block + inode block */
62642b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
6265ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
6266ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
6267ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
6268617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
6269ac27a0ecSDave Kleikamp 	if (!ret)
6270ac27a0ecSDave Kleikamp 		ret = err;
6271ac27a0ecSDave Kleikamp 	return ret;
6272ac27a0ecSDave Kleikamp }
6273ac27a0ecSDave Kleikamp 
6274ac27a0ecSDave Kleikamp /*
6275ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
6276ac27a0ecSDave Kleikamp  * the quota file and such...
6277ac27a0ecSDave Kleikamp  */
6278617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
6279ac27a0ecSDave Kleikamp {
628033458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
6281617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
6282ac27a0ecSDave Kleikamp }
6283ac27a0ecSDave Kleikamp 
6284daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6285daf647d2STheodore Ts'o {
6286daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
6287daf647d2STheodore Ts'o 
6288daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
6289daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
6290daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
6291daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
6292daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
6293daf647d2STheodore Ts'o 	 */
6294daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
6295daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
6296daf647d2STheodore Ts'o }
6297daf647d2STheodore Ts'o 
6298ac27a0ecSDave Kleikamp /*
6299ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
6300ac27a0ecSDave Kleikamp  */
6301617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
63028c54ca9cSAl Viro 			 const struct path *path)
6303ac27a0ecSDave Kleikamp {
6304ac27a0ecSDave Kleikamp 	int err;
6305ac27a0ecSDave Kleikamp 
6306ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
6307ac27a0ecSDave Kleikamp 		return -EINVAL;
63080623543bSJan Kara 
6309ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
6310d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
6311ac27a0ecSDave Kleikamp 		return -EXDEV;
6312e0770e91SJan Kara 
6313e0770e91SJan Kara 	/* Quota already enabled for this file? */
6314e0770e91SJan Kara 	if (IS_NOQUOTA(d_inode(path->dentry)))
6315e0770e91SJan Kara 		return -EBUSY;
6316e0770e91SJan Kara 
63170623543bSJan Kara 	/* Journaling quota? */
63180623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
63192b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
6320f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
6321b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
6322b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
6323b31e1552SEric Sandeen 				"Journaled quota will not work");
632491389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
632591389240SJan Kara 	} else {
632691389240SJan Kara 		/*
632791389240SJan Kara 		 * Clear the flag just in case mount options changed since
632891389240SJan Kara 		 * last time.
632991389240SJan Kara 		 */
633091389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
63310623543bSJan Kara 	}
63320623543bSJan Kara 
63330623543bSJan Kara 	/*
63340623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
63350623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
63360623543bSJan Kara 	 */
63370390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
63382b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
63390623543bSJan Kara 		/*
63400623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
63410623543bSJan Kara 		 * otherwise be livelocked...
63420623543bSJan Kara 		 */
63430623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
63447ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
63450623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6346f00c9e44SJan Kara 		if (err)
63477ffe1ea8SHidehiro Kawai 			return err;
63487ffe1ea8SHidehiro Kawai 	}
6349957153fcSJan Kara 
6350daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6351daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
6352957153fcSJan Kara 	if (err) {
6353daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
6354daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
6355957153fcSJan Kara 	} else {
6356957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6357957153fcSJan Kara 		handle_t *handle;
6358957153fcSJan Kara 
635961a92987SJan Kara 		/*
636061a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
636161a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
636261a92987SJan Kara 		 * are already enabled and this is not a hard failure.
636361a92987SJan Kara 		 */
6364957153fcSJan Kara 		inode_lock(inode);
6365957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6366957153fcSJan Kara 		if (IS_ERR(handle))
6367957153fcSJan Kara 			goto unlock_inode;
6368957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6369957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6370957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
63714209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6372957153fcSJan Kara 		ext4_journal_stop(handle);
6373957153fcSJan Kara 	unlock_inode:
6374957153fcSJan Kara 		inode_unlock(inode);
6375957153fcSJan Kara 	}
6376daf647d2STheodore Ts'o 	return err;
6377ac27a0ecSDave Kleikamp }
6378ac27a0ecSDave Kleikamp 
63797c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
63807c319d32SAditya Kali 			     unsigned int flags)
63817c319d32SAditya Kali {
63827c319d32SAditya Kali 	int err;
63837c319d32SAditya Kali 	struct inode *qf_inode;
6384a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
63857c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6386689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6387689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
63887c319d32SAditya Kali 	};
63897c319d32SAditya Kali 
6390e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
63917c319d32SAditya Kali 
63927c319d32SAditya Kali 	if (!qf_inums[type])
63937c319d32SAditya Kali 		return -EPERM;
63947c319d32SAditya Kali 
63958a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
63967c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
63977c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
63987c319d32SAditya Kali 		return PTR_ERR(qf_inode);
63997c319d32SAditya Kali 	}
64007c319d32SAditya Kali 
6401bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6402bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6403daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
64047212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6405daf647d2STheodore Ts'o 	if (err)
6406daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
640761157b24SPan Bian 	iput(qf_inode);
64087c319d32SAditya Kali 
64097c319d32SAditya Kali 	return err;
64107c319d32SAditya Kali }
64117c319d32SAditya Kali 
64127c319d32SAditya Kali /* Enable usage tracking for all quota types. */
64137c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
64147c319d32SAditya Kali {
64157c319d32SAditya Kali 	int type, err = 0;
6416a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
64177c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6418689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6419689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
64207c319d32SAditya Kali 	};
642149da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
642249da9392SJan Kara 		test_opt(sb, USRQUOTA),
642349da9392SJan Kara 		test_opt(sb, GRPQUOTA),
642449da9392SJan Kara 		test_opt(sb, PRJQUOTA),
642549da9392SJan Kara 	};
64267c319d32SAditya Kali 
642791389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6428a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
64297c319d32SAditya Kali 		if (qf_inums[type]) {
64307c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
643149da9392SJan Kara 				DQUOT_USAGE_ENABLED |
643249da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
64337c319d32SAditya Kali 			if (err) {
64347c319d32SAditya Kali 				ext4_warning(sb,
643572ba7450STheodore Ts'o 					"Failed to enable quota tracking "
643672ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
643772ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
64387f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
64397f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
64407f144fd0SJunichi Uekawa 
64417c319d32SAditya Kali 				return err;
64427c319d32SAditya Kali 			}
64437c319d32SAditya Kali 		}
64447c319d32SAditya Kali 	}
64457c319d32SAditya Kali 	return 0;
64467c319d32SAditya Kali }
64477c319d32SAditya Kali 
6448ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6449ca0e05e4SDmitry Monakhov {
645021f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
645121f97697SJan Kara 	handle_t *handle;
6452957153fcSJan Kara 	int err;
645321f97697SJan Kara 
645487009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
645587009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
645687009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6457ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6458ca0e05e4SDmitry Monakhov 
6459957153fcSJan Kara 	if (!inode || !igrab(inode))
64600b268590SAmir Goldstein 		goto out;
64610b268590SAmir Goldstein 
6462957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6463964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6464957153fcSJan Kara 		goto out_put;
6465957153fcSJan Kara 
6466957153fcSJan Kara 	inode_lock(inode);
646761a92987SJan Kara 	/*
646861a92987SJan Kara 	 * Update modification times of quota files when userspace can
646961a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
647061a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
647161a92987SJan Kara 	 */
64729924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
64734209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
64744209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6475957153fcSJan Kara 		goto out_unlock;
64764209ae12SHarshad Shirwadkar 	}
6477957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6478957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6479eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
64804209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
648121f97697SJan Kara 	ext4_journal_stop(handle);
6482957153fcSJan Kara out_unlock:
6483957153fcSJan Kara 	inode_unlock(inode);
6484957153fcSJan Kara out_put:
6485964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6486957153fcSJan Kara 	iput(inode);
6487957153fcSJan Kara 	return err;
648821f97697SJan Kara out:
6489ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6490ca0e05e4SDmitry Monakhov }
6491ca0e05e4SDmitry Monakhov 
6492ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6493ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6494ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6495ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6496617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6497ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6498ac27a0ecSDave Kleikamp {
6499ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6500725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6501ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6502ac27a0ecSDave Kleikamp 	int tocopy;
6503ac27a0ecSDave Kleikamp 	size_t toread;
6504ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6505ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6506ac27a0ecSDave Kleikamp 
6507ac27a0ecSDave Kleikamp 	if (off > i_size)
6508ac27a0ecSDave Kleikamp 		return 0;
6509ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6510ac27a0ecSDave Kleikamp 		len = i_size-off;
6511ac27a0ecSDave Kleikamp 	toread = len;
6512ac27a0ecSDave Kleikamp 	while (toread > 0) {
6513ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6514ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
65151c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
65161c215028STheodore Ts'o 		if (IS_ERR(bh))
65171c215028STheodore Ts'o 			return PTR_ERR(bh);
6518ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6519ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6520ac27a0ecSDave Kleikamp 		else
6521ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6522ac27a0ecSDave Kleikamp 		brelse(bh);
6523ac27a0ecSDave Kleikamp 		offset = 0;
6524ac27a0ecSDave Kleikamp 		toread -= tocopy;
6525ac27a0ecSDave Kleikamp 		data += tocopy;
6526ac27a0ecSDave Kleikamp 		blk++;
6527ac27a0ecSDave Kleikamp 	}
6528ac27a0ecSDave Kleikamp 	return len;
6529ac27a0ecSDave Kleikamp }
6530ac27a0ecSDave Kleikamp 
6531ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6532ac27a0ecSDave Kleikamp  * enough credits) */
6533617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6534ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6535ac27a0ecSDave Kleikamp {
6536ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6537725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
65384209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6539c5e298aeSTheodore Ts'o 	int retries = 0;
6540ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6541ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6542ac27a0ecSDave Kleikamp 
65430390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6544b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6545b31e1552SEric Sandeen 			" cancelled because transaction is not started",
65469c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
65479c3013e9SJan Kara 		return -EIO;
65489c3013e9SJan Kara 	}
654967eeb568SDmitry Monakhov 	/*
655067eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
655167eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
655267eeb568SDmitry Monakhov 	 */
655367eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
655467eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
655567eeb568SDmitry Monakhov 			" cancelled because not block aligned",
655667eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
655767eeb568SDmitry Monakhov 		return -EIO;
655867eeb568SDmitry Monakhov 	}
655967eeb568SDmitry Monakhov 
6560c5e298aeSTheodore Ts'o 	do {
6561c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6562c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6563c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
656445586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6565c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
65661c215028STheodore Ts'o 	if (IS_ERR(bh))
65671c215028STheodore Ts'o 		return PTR_ERR(bh);
6568ac27a0ecSDave Kleikamp 	if (!bh)
6569ac27a0ecSDave Kleikamp 		goto out;
65705d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6571617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6572ac27a0ecSDave Kleikamp 	if (err) {
6573ac27a0ecSDave Kleikamp 		brelse(bh);
65741c215028STheodore Ts'o 		return err;
6575ac27a0ecSDave Kleikamp 	}
6576ac27a0ecSDave Kleikamp 	lock_buffer(bh);
657767eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6578ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6579ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
65800390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6581ac27a0ecSDave Kleikamp 	brelse(bh);
6582ac27a0ecSDave Kleikamp out:
658367eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
658467eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6585617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
65864209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
65874209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
65884209ae12SHarshad Shirwadkar 			err = err2;
658921f97697SJan Kara 	}
65904209ae12SHarshad Shirwadkar 	return err ? err : len;
6591ac27a0ecSDave Kleikamp }
6592ac27a0ecSDave Kleikamp #endif
6593ac27a0ecSDave Kleikamp 
6594152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6595152a0836SAl Viro 		       const char *dev_name, void *data)
6596ac27a0ecSDave Kleikamp {
6597152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6598ac27a0ecSDave Kleikamp }
6599ac27a0ecSDave Kleikamp 
6600c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
660124b58424STheodore Ts'o static inline void register_as_ext2(void)
660224b58424STheodore Ts'o {
660324b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
660424b58424STheodore Ts'o 	if (err)
660524b58424STheodore Ts'o 		printk(KERN_WARNING
660624b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
660724b58424STheodore Ts'o }
660824b58424STheodore Ts'o 
660924b58424STheodore Ts'o static inline void unregister_as_ext2(void)
661024b58424STheodore Ts'o {
661124b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
661224b58424STheodore Ts'o }
66132035e776STheodore Ts'o 
66142035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
66152035e776STheodore Ts'o {
6616e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
66172035e776STheodore Ts'o 		return 0;
6618bc98a42cSDavid Howells 	if (sb_rdonly(sb))
66192035e776STheodore Ts'o 		return 1;
6620e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
66212035e776STheodore Ts'o 		return 0;
66222035e776STheodore Ts'o 	return 1;
66232035e776STheodore Ts'o }
662424b58424STheodore Ts'o #else
662524b58424STheodore Ts'o static inline void register_as_ext2(void) { }
662624b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
66272035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
662824b58424STheodore Ts'o #endif
662924b58424STheodore Ts'o 
663024b58424STheodore Ts'o static inline void register_as_ext3(void)
663124b58424STheodore Ts'o {
663224b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
663324b58424STheodore Ts'o 	if (err)
663424b58424STheodore Ts'o 		printk(KERN_WARNING
663524b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
663624b58424STheodore Ts'o }
663724b58424STheodore Ts'o 
663824b58424STheodore Ts'o static inline void unregister_as_ext3(void)
663924b58424STheodore Ts'o {
664024b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
664124b58424STheodore Ts'o }
66422035e776STheodore Ts'o 
66432035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
66442035e776STheodore Ts'o {
6645e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
66462035e776STheodore Ts'o 		return 0;
6647e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
66482035e776STheodore Ts'o 		return 0;
6649bc98a42cSDavid Howells 	if (sb_rdonly(sb))
66502035e776STheodore Ts'o 		return 1;
6651e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
66522035e776STheodore Ts'o 		return 0;
66532035e776STheodore Ts'o 	return 1;
66542035e776STheodore Ts'o }
665524b58424STheodore Ts'o 
665603010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6657ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
665803010a33STheodore Ts'o 	.name		= "ext4",
6659152a0836SAl Viro 	.mount		= ext4_mount,
6660ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
666114f3db55SChristian Brauner 	.fs_flags	= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
6662ac27a0ecSDave Kleikamp };
66637f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6664ac27a0ecSDave Kleikamp 
6665e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6666e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6667e9e3bcecSEric Sandeen 
66685dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6669ac27a0ecSDave Kleikamp {
6670e9e3bcecSEric Sandeen 	int i, err;
6671c9de560dSAlex Tomas 
6672e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
667307c0c5d8SAl Viro 	ext4_li_info = NULL;
667407c0c5d8SAl Viro 
66759a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
667612e9b892SDmitry Monakhov 	ext4_check_flag_values();
6677e9e3bcecSEric Sandeen 
6678e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6679e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6680e9e3bcecSEric Sandeen 
668151865fdaSZheng Liu 	err = ext4_init_es();
66826fd058f7STheodore Ts'o 	if (err)
66836fd058f7STheodore Ts'o 		return err;
668451865fdaSZheng Liu 
66851dc0aa46SEric Whitney 	err = ext4_init_pending();
66861dc0aa46SEric Whitney 	if (err)
668722cfe4b4SEric Biggers 		goto out7;
668822cfe4b4SEric Biggers 
668922cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
669022cfe4b4SEric Biggers 	if (err)
66911dc0aa46SEric Whitney 		goto out6;
66921dc0aa46SEric Whitney 
669351865fdaSZheng Liu 	err = ext4_init_pageio();
669451865fdaSZheng Liu 	if (err)
6695b5799018STheodore Ts'o 		goto out5;
669651865fdaSZheng Liu 
66975dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6698bd2d0210STheodore Ts'o 	if (err)
6699b5799018STheodore Ts'o 		goto out4;
6700857ac889SLukas Czerner 
6701b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6702dd68314cSTheodore Ts'o 	if (err)
6703b5799018STheodore Ts'o 		goto out3;
6704857ac889SLukas Czerner 
67055dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6706ac27a0ecSDave Kleikamp 	if (err)
6707c9de560dSAlex Tomas 		goto out2;
6708ac27a0ecSDave Kleikamp 	err = init_inodecache();
6709ac27a0ecSDave Kleikamp 	if (err)
6710ac27a0ecSDave Kleikamp 		goto out1;
6711aa75f4d3SHarshad Shirwadkar 
6712aa75f4d3SHarshad Shirwadkar 	err = ext4_fc_init_dentry_cache();
6713aa75f4d3SHarshad Shirwadkar 	if (err)
6714aa75f4d3SHarshad Shirwadkar 		goto out05;
6715aa75f4d3SHarshad Shirwadkar 
671624b58424STheodore Ts'o 	register_as_ext3();
67172035e776STheodore Ts'o 	register_as_ext2();
671803010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6719ac27a0ecSDave Kleikamp 	if (err)
6720ac27a0ecSDave Kleikamp 		goto out;
6721bfff6873SLukas Czerner 
6722ac27a0ecSDave Kleikamp 	return 0;
6723ac27a0ecSDave Kleikamp out:
672424b58424STheodore Ts'o 	unregister_as_ext2();
672524b58424STheodore Ts'o 	unregister_as_ext3();
6726aa75f4d3SHarshad Shirwadkar out05:
6727ac27a0ecSDave Kleikamp 	destroy_inodecache();
6728ac27a0ecSDave Kleikamp out1:
67295dabfc78STheodore Ts'o 	ext4_exit_mballoc();
67309c191f70ST Makphaibulchoke out2:
6731b5799018STheodore Ts'o 	ext4_exit_sysfs();
6732b5799018STheodore Ts'o out3:
6733dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6734b5799018STheodore Ts'o out4:
67355dabfc78STheodore Ts'o 	ext4_exit_pageio();
6736b5799018STheodore Ts'o out5:
673722cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
67381dc0aa46SEric Whitney out6:
673922cfe4b4SEric Biggers 	ext4_exit_pending();
674022cfe4b4SEric Biggers out7:
674151865fdaSZheng Liu 	ext4_exit_es();
674251865fdaSZheng Liu 
6743ac27a0ecSDave Kleikamp 	return err;
6744ac27a0ecSDave Kleikamp }
6745ac27a0ecSDave Kleikamp 
67465dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6747ac27a0ecSDave Kleikamp {
6748bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
674924b58424STheodore Ts'o 	unregister_as_ext2();
675024b58424STheodore Ts'o 	unregister_as_ext3();
675103010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6752ac27a0ecSDave Kleikamp 	destroy_inodecache();
67535dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6754b5799018STheodore Ts'o 	ext4_exit_sysfs();
67555dabfc78STheodore Ts'o 	ext4_exit_system_zone();
67565dabfc78STheodore Ts'o 	ext4_exit_pageio();
675722cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6758dd12ed14SEric Sandeen 	ext4_exit_es();
67591dc0aa46SEric Whitney 	ext4_exit_pending();
6760ac27a0ecSDave Kleikamp }
6761ac27a0ecSDave Kleikamp 
6762ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
676383982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6764ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
67657ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
67665dabfc78STheodore Ts'o module_init(ext4_init_fs)
67675dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6768