xref: /linux/fs/ext4/super.c (revision 1e1a76ed9a1c090e1ac030ed73612e5fe2262f2d)
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>
46ac27a0ecSDave Kleikamp 
47bfff6873SLukas Czerner #include <linux/kthread.h>
48bfff6873SLukas Czerner #include <linux/freezer.h>
49bfff6873SLukas Czerner 
503dcf5451SChristoph Hellwig #include "ext4.h"
514a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
523dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
53ac27a0ecSDave Kleikamp #include "xattr.h"
54ac27a0ecSDave Kleikamp #include "acl.h"
553661d286STheodore Ts'o #include "mballoc.h"
560c9ec4beSDarrick J. Wong #include "fsmap.h"
57ac27a0ecSDave Kleikamp 
589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
599bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
609bffad1eSTheodore Ts'o 
610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
620b75a840SLukas Czerner static struct mutex ext4_li_mtx;
63e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
649f6200bbSTheodore Ts'o 
65617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
66ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
672adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
68e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
69617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
70617ba13bSMingming Cao 					struct ext4_super_block *es);
71617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
72617ba13bSMingming Cao 				   struct ext4_super_block *es);
73617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
74617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
75617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
76c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79152a0836SAl Viro 		       const char *dev_name, void *data);
802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
858f1f7453SEric Sandeen static void ext4_clear_request_list(void);
86c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
87c6cb7e77SEric Whitney 					    unsigned int journal_inum);
88ac27a0ecSDave Kleikamp 
89e74031fdSJan Kara /*
90e74031fdSJan Kara  * Lock ordering
91e74031fdSJan Kara  *
92e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
93e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
94e74031fdSJan Kara  *
95e74031fdSJan Kara  * page fault path:
96e74031fdSJan Kara  * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
97e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
98e74031fdSJan Kara  *
99e74031fdSJan Kara  * buffered write path:
100e74031fdSJan Kara  * sb_start_write -> i_mutex -> mmap_sem
101e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
102e74031fdSJan Kara  *   i_data_sem (rw)
103e74031fdSJan Kara  *
104e74031fdSJan Kara  * truncate:
1051d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock
1061d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start ->
1071d39834fSNikolay Borisov  *   i_data_sem (rw)
108e74031fdSJan Kara  *
109e74031fdSJan Kara  * direct IO:
1101d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> mmap_sem
1111d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
112e74031fdSJan Kara  *
113e74031fdSJan Kara  * writepages:
114e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
115e74031fdSJan Kara  */
116e74031fdSJan Kara 
117c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1182035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1192035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1202035e776STheodore Ts'o 	.name		= "ext2",
1212035e776STheodore Ts'o 	.mount		= ext4_mount,
1222035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1232035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1242035e776STheodore Ts'o };
1257f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
126fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1272035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1282035e776STheodore Ts'o #else
1292035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1302035e776STheodore Ts'o #endif
1312035e776STheodore Ts'o 
1322035e776STheodore Ts'o 
133ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
134ba69f9abSJan Kara 	.owner		= THIS_MODULE,
135ba69f9abSJan Kara 	.name		= "ext3",
136152a0836SAl Viro 	.mount		= ext4_mount,
137ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
138ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
139ba69f9abSJan Kara };
1407f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
141fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
142ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
143bd81d8eeSLaurent Vivier 
144fb265c9cSTheodore Ts'o /*
145fb265c9cSTheodore Ts'o  * This works like sb_bread() except it uses ERR_PTR for error
146fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
147fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
148fb265c9cSTheodore Ts'o  * return.
149fb265c9cSTheodore Ts'o  */
150fb265c9cSTheodore Ts'o struct buffer_head *
151fb265c9cSTheodore Ts'o ext4_sb_bread(struct super_block *sb, sector_t block, int op_flags)
152fb265c9cSTheodore Ts'o {
153fb265c9cSTheodore Ts'o 	struct buffer_head *bh = sb_getblk(sb, block);
154fb265c9cSTheodore Ts'o 
155fb265c9cSTheodore Ts'o 	if (bh == NULL)
156fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
157fb265c9cSTheodore Ts'o 	if (buffer_uptodate(bh))
158fb265c9cSTheodore Ts'o 		return bh;
159fb265c9cSTheodore Ts'o 	ll_rw_block(REQ_OP_READ, REQ_META | op_flags, 1, &bh);
160fb265c9cSTheodore Ts'o 	wait_on_buffer(bh);
161fb265c9cSTheodore Ts'o 	if (buffer_uptodate(bh))
162fb265c9cSTheodore Ts'o 		return bh;
163fb265c9cSTheodore Ts'o 	put_bh(bh);
164fb265c9cSTheodore Ts'o 	return ERR_PTR(-EIO);
165fb265c9cSTheodore Ts'o }
166fb265c9cSTheodore Ts'o 
167d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb,
168d25425f8SDarrick J. Wong 				 struct ext4_super_block *es)
169d25425f8SDarrick J. Wong {
170e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_metadata_csum(sb))
171d25425f8SDarrick J. Wong 		return 1;
172d25425f8SDarrick J. Wong 
173d25425f8SDarrick J. Wong 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
174d25425f8SDarrick J. Wong }
175d25425f8SDarrick J. Wong 
176a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb,
177a9c47317SDarrick J. Wong 				   struct ext4_super_block *es)
178a9c47317SDarrick J. Wong {
179a9c47317SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
180a9c47317SDarrick J. Wong 	int offset = offsetof(struct ext4_super_block, s_checksum);
181a9c47317SDarrick J. Wong 	__u32 csum;
182a9c47317SDarrick J. Wong 
183a9c47317SDarrick J. Wong 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
184a9c47317SDarrick J. Wong 
185a9c47317SDarrick J. Wong 	return cpu_to_le32(csum);
186a9c47317SDarrick J. Wong }
187a9c47317SDarrick J. Wong 
188c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb,
189a9c47317SDarrick J. Wong 				       struct ext4_super_block *es)
190a9c47317SDarrick J. Wong {
1919aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
192a9c47317SDarrick J. Wong 		return 1;
193a9c47317SDarrick J. Wong 
194a9c47317SDarrick J. Wong 	return es->s_checksum == ext4_superblock_csum(sb, es);
195a9c47317SDarrick J. Wong }
196a9c47317SDarrick J. Wong 
19706db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb)
198a9c47317SDarrick J. Wong {
19906db49e6STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
20006db49e6STheodore Ts'o 
2019aa5d32bSDmitry Monakhov 	if (!ext4_has_metadata_csum(sb))
202a9c47317SDarrick J. Wong 		return;
203a9c47317SDarrick J. Wong 
204a9c47317SDarrick J. Wong 	es->s_checksum = ext4_superblock_csum(sb, es);
205a9c47317SDarrick J. Wong }
206a9c47317SDarrick J. Wong 
2079933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags)
2089933fc0aSTheodore Ts'o {
2099933fc0aSTheodore Ts'o 	void *ret;
2109933fc0aSTheodore Ts'o 
2118be04b93SJoe Perches 	ret = kmalloc(size, flags | __GFP_NOWARN);
2129933fc0aSTheodore Ts'o 	if (!ret)
2139933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags, PAGE_KERNEL);
2149933fc0aSTheodore Ts'o 	return ret;
2159933fc0aSTheodore Ts'o }
2169933fc0aSTheodore Ts'o 
2179933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags)
2189933fc0aSTheodore Ts'o {
2199933fc0aSTheodore Ts'o 	void *ret;
2209933fc0aSTheodore Ts'o 
2218be04b93SJoe Perches 	ret = kzalloc(size, flags | __GFP_NOWARN);
2229933fc0aSTheodore Ts'o 	if (!ret)
2239933fc0aSTheodore Ts'o 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2249933fc0aSTheodore Ts'o 	return ret;
2259933fc0aSTheodore Ts'o }
2269933fc0aSTheodore Ts'o 
2278fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
2288fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
229bd81d8eeSLaurent Vivier {
2303a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
2318fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2328fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
233bd81d8eeSLaurent Vivier }
234bd81d8eeSLaurent Vivier 
2358fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
2368fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
237bd81d8eeSLaurent Vivier {
2385272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
2398fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2408fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
241bd81d8eeSLaurent Vivier }
242bd81d8eeSLaurent Vivier 
2438fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
2448fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
245bd81d8eeSLaurent Vivier {
2465272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
2478fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2488fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
249bd81d8eeSLaurent Vivier }
250bd81d8eeSLaurent Vivier 
251021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
252560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
253560671a0SAneesh Kumar K.V {
254560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
255560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
256560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
257560671a0SAneesh Kumar K.V }
258560671a0SAneesh Kumar K.V 
259560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
260560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
261560671a0SAneesh Kumar K.V {
262560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
263560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
264560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
265560671a0SAneesh Kumar K.V }
266560671a0SAneesh Kumar K.V 
267560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
268560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
269560671a0SAneesh Kumar K.V {
270560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
271560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
272560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
273560671a0SAneesh Kumar K.V }
274560671a0SAneesh Kumar K.V 
275560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
276560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
277560671a0SAneesh Kumar K.V {
278560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
279560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
280560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
281560671a0SAneesh Kumar K.V }
282560671a0SAneesh Kumar K.V 
2838fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2848fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
285bd81d8eeSLaurent Vivier {
2863a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2878fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2888fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
289bd81d8eeSLaurent Vivier }
290bd81d8eeSLaurent Vivier 
2918fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2928fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
293bd81d8eeSLaurent Vivier {
2945272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2958fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2968fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
297bd81d8eeSLaurent Vivier }
298bd81d8eeSLaurent Vivier 
2998fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
3008fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
301bd81d8eeSLaurent Vivier {
3025272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
3038fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
3048fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
305bd81d8eeSLaurent Vivier }
306bd81d8eeSLaurent Vivier 
307021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
308560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
309560671a0SAneesh Kumar K.V {
310560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
311560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
312560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
313560671a0SAneesh Kumar K.V }
314560671a0SAneesh Kumar K.V 
315560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
316560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
317560671a0SAneesh Kumar K.V {
318560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
319560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
320560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
321560671a0SAneesh Kumar K.V }
322560671a0SAneesh Kumar K.V 
323560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
324560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
325560671a0SAneesh Kumar K.V {
326560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
327560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
328560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
329560671a0SAneesh Kumar K.V }
330560671a0SAneesh Kumar K.V 
331560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
332560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
333560671a0SAneesh Kumar K.V {
334560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
335560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
336560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
337560671a0SAneesh Kumar K.V }
338560671a0SAneesh Kumar K.V 
3396a0678a7SArnd Bergmann static void __ext4_update_tstamp(__le32 *lo, __u8 *hi)
3406a0678a7SArnd Bergmann {
3416a0678a7SArnd Bergmann 	time64_t now = ktime_get_real_seconds();
3426a0678a7SArnd Bergmann 
3436a0678a7SArnd Bergmann 	now = clamp_val(now, 0, (1ull << 40) - 1);
3446a0678a7SArnd Bergmann 
3456a0678a7SArnd Bergmann 	*lo = cpu_to_le32(lower_32_bits(now));
3466a0678a7SArnd Bergmann 	*hi = upper_32_bits(now);
3476a0678a7SArnd Bergmann }
3486a0678a7SArnd Bergmann 
3496a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
3506a0678a7SArnd Bergmann {
3516a0678a7SArnd Bergmann 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
3526a0678a7SArnd Bergmann }
3536a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \
3546a0678a7SArnd Bergmann 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
3556a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \
3566a0678a7SArnd Bergmann 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
357d3d1faf6SCurt Wohlgemuth 
3581c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func,
3591c13d5c0STheodore Ts'o 			    unsigned int line)
3601c13d5c0STheodore Ts'o {
3611c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
3621c13d5c0STheodore Ts'o 
3631c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3641b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
3651b46617bSTheodore Ts'o 		return;
3661c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3676a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_last_error_time);
3681c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3691c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
3701c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
3711c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
3726a0678a7SArnd Bergmann 		es->s_first_error_time_hi = es->s_last_error_time_hi;
3731c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
3741c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
3751c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
3761c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
3771c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
3781c13d5c0STheodore Ts'o 	}
37966e61a9eSTheodore Ts'o 	/*
38066e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
38166e61a9eSTheodore Ts'o 	 * started already
38266e61a9eSTheodore Ts'o 	 */
38366e61a9eSTheodore Ts'o 	if (!es->s_error_count)
38466e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
385ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
3861c13d5c0STheodore Ts'o }
3871c13d5c0STheodore Ts'o 
3881c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func,
3891c13d5c0STheodore Ts'o 			    unsigned int line)
3901c13d5c0STheodore Ts'o {
3911c13d5c0STheodore Ts'o 	__save_error_info(sb, func, line);
3921c13d5c0STheodore Ts'o 	ext4_commit_super(sb, 1);
3931c13d5c0STheodore Ts'o }
3941c13d5c0STheodore Ts'o 
395bdfe0cbdSTheodore Ts'o /*
396bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
397bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
398bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
399bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
400bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
401bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
402bdfe0cbdSTheodore Ts'o  */
403bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
404bdfe0cbdSTheodore Ts'o {
405bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
406bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
407bdfe0cbdSTheodore Ts'o 
408bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
409bdfe0cbdSTheodore Ts'o }
410bdfe0cbdSTheodore Ts'o 
41118aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
41218aadd47SBobi Jam {
41318aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
41418aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
41518aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
4165d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
41718aadd47SBobi Jam 
4185d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
419a0154344SDaeho Jeong 
420a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
421a0154344SDaeho Jeong 
42218aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
4235d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
4245d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
4255d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
42618aadd47SBobi Jam 		list_del_init(&jce->jce_list);
42718aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
42818aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
42918aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
43018aadd47SBobi Jam 	}
43118aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
43218aadd47SBobi Jam }
4331c13d5c0STheodore Ts'o 
4341dc1097fSJan Kara static bool system_going_down(void)
4351dc1097fSJan Kara {
4361dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4371dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
4381dc1097fSJan Kara }
4391dc1097fSJan Kara 
440ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
441ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
442ac27a0ecSDave Kleikamp  *
443ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
444617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
445ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
446ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
447ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
448ac27a0ecSDave Kleikamp  * write out the superblock safely.
449ac27a0ecSDave Kleikamp  *
450dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
451d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
452ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
453ac27a0ecSDave Kleikamp  */
454ac27a0ecSDave Kleikamp 
455617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
456ac27a0ecSDave Kleikamp {
457327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
458327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
459327eaf73STheodore Ts'o 
460bc98a42cSDavid Howells 	if (sb_rdonly(sb))
461ac27a0ecSDave Kleikamp 		return;
462ac27a0ecSDave Kleikamp 
463ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
464617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
465ac27a0ecSDave Kleikamp 
4664ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
467ac27a0ecSDave Kleikamp 		if (journal)
468dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
469ac27a0ecSDave Kleikamp 	}
4701dc1097fSJan Kara 	/*
4711dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
4721dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
4731dc1097fSJan Kara 	 * disabled.
4741dc1097fSJan Kara 	 */
4751dc1097fSJan Kara 	if (test_opt(sb, ERRORS_RO) || system_going_down()) {
476b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
4774418e141SDmitry Monakhov 		/*
4784418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
4794418e141SDmitry Monakhov 		 * before ->s_flags update
4804418e141SDmitry Monakhov 		 */
4814418e141SDmitry Monakhov 		smp_wmb();
4821751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4831dc1097fSJan Kara 	} else if (test_opt(sb, ERRORS_PANIC)) {
4844327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
4854327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
4864327ba52SDaeho Jeong 			return;
487617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
488ac27a0ecSDave Kleikamp 			sb->s_id);
489ac27a0ecSDave Kleikamp 	}
4904327ba52SDaeho Jeong }
491ac27a0ecSDave Kleikamp 
492efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
493efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
494efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
495efbed4dcSTheodore Ts'o 
49612062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
497c398eda0STheodore Ts'o 		  unsigned int line, const char *fmt, ...)
498ac27a0ecSDave Kleikamp {
4990ff2ea7dSJoe Perches 	struct va_format vaf;
500ac27a0ecSDave Kleikamp 	va_list args;
501ac27a0ecSDave Kleikamp 
5020db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
5030db1ff22STheodore Ts'o 		return;
5040db1ff22STheodore Ts'o 
505ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
506efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
507ac27a0ecSDave Kleikamp 		va_start(args, fmt);
5080ff2ea7dSJoe Perches 		vaf.fmt = fmt;
5090ff2ea7dSJoe Perches 		vaf.va = &args;
510efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
511efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
5120ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
513ac27a0ecSDave Kleikamp 		va_end(args);
514efbed4dcSTheodore Ts'o 	}
515f3fc0210STheodore Ts'o 	save_error_info(sb, function, line);
516617ba13bSMingming Cao 	ext4_handle_error(sb);
517ac27a0ecSDave Kleikamp }
518ac27a0ecSDave Kleikamp 
519e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
520c398eda0STheodore Ts'o 			unsigned int line, ext4_fsblk_t block,
521273df556SFrank Mayhar 			const char *fmt, ...)
522273df556SFrank Mayhar {
523273df556SFrank Mayhar 	va_list args;
524f7c21177STheodore Ts'o 	struct va_format vaf;
5251c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
526273df556SFrank Mayhar 
5270db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5280db1ff22STheodore Ts'o 		return;
5290db1ff22STheodore Ts'o 
530ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
5311c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
5321c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
533efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
534273df556SFrank Mayhar 		va_start(args, fmt);
535f7c21177STheodore Ts'o 		vaf.fmt = fmt;
536f7c21177STheodore Ts'o 		vaf.va = &args;
537c398eda0STheodore Ts'o 		if (block)
538d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
539d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
540d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
541d9ee81daSJoe Perches 			       block, current->comm, &vaf);
542d9ee81daSJoe Perches 		else
543d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
544d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
545d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
546d9ee81daSJoe Perches 			       current->comm, &vaf);
547273df556SFrank Mayhar 		va_end(args);
548efbed4dcSTheodore Ts'o 	}
549efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
550273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
551273df556SFrank Mayhar }
552273df556SFrank Mayhar 
553e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
554f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
555f7c21177STheodore Ts'o 		       const char *fmt, ...)
556273df556SFrank Mayhar {
557273df556SFrank Mayhar 	va_list args;
558f7c21177STheodore Ts'o 	struct va_format vaf;
5591c13d5c0STheodore Ts'o 	struct ext4_super_block *es;
560496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
561273df556SFrank Mayhar 	char pathname[80], *path;
562273df556SFrank Mayhar 
5630db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5640db1ff22STheodore Ts'o 		return;
5650db1ff22STheodore Ts'o 
566ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
5671c13d5c0STheodore Ts'o 	es = EXT4_SB(inode->i_sb)->s_es;
5681c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
569efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
5709bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
571f9a62d09SDan Carpenter 		if (IS_ERR(path))
572273df556SFrank Mayhar 			path = "(unknown)";
573f7c21177STheodore Ts'o 		va_start(args, fmt);
574f7c21177STheodore Ts'o 		vaf.fmt = fmt;
575f7c21177STheodore Ts'o 		vaf.va = &args;
576d9ee81daSJoe Perches 		if (block)
577d9ee81daSJoe Perches 			printk(KERN_CRIT
578d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
579d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
580d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
581d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
582d9ee81daSJoe Perches 		else
583d9ee81daSJoe Perches 			printk(KERN_CRIT
584d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
585d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
586d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
587d9ee81daSJoe Perches 			       current->comm, path, &vaf);
588273df556SFrank Mayhar 		va_end(args);
589efbed4dcSTheodore Ts'o 	}
590efbed4dcSTheodore Ts'o 	save_error_info(inode->i_sb, function, line);
591273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
592273df556SFrank Mayhar }
593273df556SFrank Mayhar 
594722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
595ac27a0ecSDave Kleikamp 			      char nbuf[16])
596ac27a0ecSDave Kleikamp {
597ac27a0ecSDave Kleikamp 	char *errstr = NULL;
598ac27a0ecSDave Kleikamp 
599ac27a0ecSDave Kleikamp 	switch (errno) {
6006a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
6016a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
6026a797d27SDarrick J. Wong 		break;
6036a797d27SDarrick J. Wong 	case -EFSBADCRC:
6046a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
6056a797d27SDarrick J. Wong 		break;
606ac27a0ecSDave Kleikamp 	case -EIO:
607ac27a0ecSDave Kleikamp 		errstr = "IO failure";
608ac27a0ecSDave Kleikamp 		break;
609ac27a0ecSDave Kleikamp 	case -ENOMEM:
610ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
611ac27a0ecSDave Kleikamp 		break;
612ac27a0ecSDave Kleikamp 	case -EROFS:
61378f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
61478f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
615ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
616ac27a0ecSDave Kleikamp 		else
617ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
618ac27a0ecSDave Kleikamp 		break;
619ac27a0ecSDave Kleikamp 	default:
620ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
621ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
622ac27a0ecSDave Kleikamp 		 * NULL. */
623ac27a0ecSDave Kleikamp 		if (nbuf) {
624ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
625ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
626ac27a0ecSDave Kleikamp 				errstr = nbuf;
627ac27a0ecSDave Kleikamp 		}
628ac27a0ecSDave Kleikamp 		break;
629ac27a0ecSDave Kleikamp 	}
630ac27a0ecSDave Kleikamp 
631ac27a0ecSDave Kleikamp 	return errstr;
632ac27a0ecSDave Kleikamp }
633ac27a0ecSDave Kleikamp 
634617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
635ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
636ac27a0ecSDave Kleikamp 
637c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
638c398eda0STheodore Ts'o 		      unsigned int line, int errno)
639ac27a0ecSDave Kleikamp {
640ac27a0ecSDave Kleikamp 	char nbuf[16];
641ac27a0ecSDave Kleikamp 	const char *errstr;
642ac27a0ecSDave Kleikamp 
6430db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6440db1ff22STheodore Ts'o 		return;
6450db1ff22STheodore Ts'o 
646ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
647ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
648ac27a0ecSDave Kleikamp 	 * an error. */
649bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
650ac27a0ecSDave Kleikamp 		return;
651ac27a0ecSDave Kleikamp 
652efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
653617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
654c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
655c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
656efbed4dcSTheodore Ts'o 	}
657ac27a0ecSDave Kleikamp 
658efbed4dcSTheodore Ts'o 	save_error_info(sb, function, line);
659617ba13bSMingming Cao 	ext4_handle_error(sb);
660ac27a0ecSDave Kleikamp }
661ac27a0ecSDave Kleikamp 
662ac27a0ecSDave Kleikamp /*
663617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
664ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
665ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
666ac27a0ecSDave Kleikamp  *
667ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
668ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
669ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
670ac27a0ecSDave Kleikamp  */
671ac27a0ecSDave Kleikamp 
672c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
673c398eda0STheodore Ts'o 		unsigned int line, const char *fmt, ...)
674ac27a0ecSDave Kleikamp {
675651e1c3bSJoe Perches 	struct va_format vaf;
676ac27a0ecSDave Kleikamp 	va_list args;
677ac27a0ecSDave Kleikamp 
6780db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6790db1ff22STheodore Ts'o 		return;
6800db1ff22STheodore Ts'o 
6811c13d5c0STheodore Ts'o 	save_error_info(sb, function, line);
682ac27a0ecSDave Kleikamp 	va_start(args, fmt);
683651e1c3bSJoe Perches 	vaf.fmt = fmt;
684651e1c3bSJoe Perches 	vaf.va = &args;
685651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
686651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
687ac27a0ecSDave Kleikamp 	va_end(args);
688ac27a0ecSDave Kleikamp 
689bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
690b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
6914ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
6924418e141SDmitry Monakhov 		/*
6934418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
6944418e141SDmitry Monakhov 		 * before ->s_flags update
6954418e141SDmitry Monakhov 		 */
6964418e141SDmitry Monakhov 		smp_wmb();
6971751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
698ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
699dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
7001c13d5c0STheodore Ts'o 		save_error_info(sb, function, line);
7011c13d5c0STheodore Ts'o 	}
7022c1d0e36SJan Kara 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
7034327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
7044327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
7054327ba52SDaeho Jeong 			return;
7061c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
707ac27a0ecSDave Kleikamp 	}
7084327ba52SDaeho Jeong }
709ac27a0ecSDave Kleikamp 
710e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
711e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
712b31e1552SEric Sandeen {
7130ff2ea7dSJoe Perches 	struct va_format vaf;
714b31e1552SEric Sandeen 	va_list args;
715b31e1552SEric Sandeen 
716efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
717efbed4dcSTheodore Ts'o 		return;
718efbed4dcSTheodore Ts'o 
719b31e1552SEric Sandeen 	va_start(args, fmt);
7200ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7210ff2ea7dSJoe Perches 	vaf.va = &args;
7220ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
723b31e1552SEric Sandeen 	va_end(args);
724b31e1552SEric Sandeen }
725b31e1552SEric Sandeen 
726b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
727b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
728b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
729b03a2f7eSAndreas Dilger 
73012062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
731c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
732ac27a0ecSDave Kleikamp {
7330ff2ea7dSJoe Perches 	struct va_format vaf;
734ac27a0ecSDave Kleikamp 	va_list args;
735ac27a0ecSDave Kleikamp 
736b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
737efbed4dcSTheodore Ts'o 		return;
738efbed4dcSTheodore Ts'o 
739ac27a0ecSDave Kleikamp 	va_start(args, fmt);
7400ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7410ff2ea7dSJoe Perches 	vaf.va = &args;
7420ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
7430ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
744ac27a0ecSDave Kleikamp 	va_end(args);
745ac27a0ecSDave Kleikamp }
746ac27a0ecSDave Kleikamp 
747b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
748b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
749b03a2f7eSAndreas Dilger {
750b03a2f7eSAndreas Dilger 	struct va_format vaf;
751b03a2f7eSAndreas Dilger 	va_list args;
752b03a2f7eSAndreas Dilger 
753b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
754b03a2f7eSAndreas Dilger 		return;
755b03a2f7eSAndreas Dilger 
756b03a2f7eSAndreas Dilger 	va_start(args, fmt);
757b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
758b03a2f7eSAndreas Dilger 	vaf.va = &args;
759b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
760b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
761b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
762b03a2f7eSAndreas Dilger 	va_end(args);
763b03a2f7eSAndreas Dilger }
764b03a2f7eSAndreas Dilger 
765e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
766e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
767e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
768e29136f8STheodore Ts'o 			     const char *fmt, ...)
7695d1b1b3fSAneesh Kumar K.V __releases(bitlock)
7705d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
7715d1b1b3fSAneesh Kumar K.V {
7720ff2ea7dSJoe Perches 	struct va_format vaf;
7735d1b1b3fSAneesh Kumar K.V 	va_list args;
7745d1b1b3fSAneesh Kumar K.V 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
7755d1b1b3fSAneesh Kumar K.V 
7760db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7770db1ff22STheodore Ts'o 		return;
7780db1ff22STheodore Ts'o 
779ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
7801c13d5c0STheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
7811c13d5c0STheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
7821c13d5c0STheodore Ts'o 	__save_error_info(sb, function, line);
7830ff2ea7dSJoe Perches 
784efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
7855d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
7860ff2ea7dSJoe Perches 		vaf.fmt = fmt;
7870ff2ea7dSJoe Perches 		vaf.va = &args;
78821149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
789e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
790e29136f8STheodore Ts'o 		if (ino)
7910ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
792e29136f8STheodore Ts'o 		if (block)
793efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
794efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
7950ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
7965d1b1b3fSAneesh Kumar K.V 		va_end(args);
797efbed4dcSTheodore Ts'o 	}
7985d1b1b3fSAneesh Kumar K.V 
799327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
800327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
801327eaf73STheodore Ts'o 
8025d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
803e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
8045d1b1b3fSAneesh Kumar K.V 		return;
8055d1b1b3fSAneesh Kumar K.V 	}
8061c13d5c0STheodore Ts'o 
8075d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
80806f29cc8SZhouyi Zhou 	ext4_commit_super(sb, 1);
8095d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
8105d1b1b3fSAneesh Kumar K.V 	/*
8115d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
8125d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
8135d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
8145d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
8155d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
81625985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
8175d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
8185d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
8195d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
8205d1b1b3fSAneesh Kumar K.V 	 */
8215d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
8225d1b1b3fSAneesh Kumar K.V 	return;
8235d1b1b3fSAneesh Kumar K.V }
8245d1b1b3fSAneesh Kumar K.V 
825db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
826db79e6d1SWang Shilong 				     ext4_group_t group,
827db79e6d1SWang Shilong 				     unsigned int flags)
828db79e6d1SWang Shilong {
829db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
830db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
831db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
8329af0b3d1SWang Shilong 	int ret;
833db79e6d1SWang Shilong 
8349af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
8359af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
8369af0b3d1SWang Shilong 					    &grp->bb_state);
8379af0b3d1SWang Shilong 		if (!ret)
838db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
839db79e6d1SWang Shilong 					   grp->bb_free);
840db79e6d1SWang Shilong 	}
841db79e6d1SWang Shilong 
8429af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
8439af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
8449af0b3d1SWang Shilong 					    &grp->bb_state);
8459af0b3d1SWang Shilong 		if (!ret && gdp) {
846db79e6d1SWang Shilong 			int count;
847db79e6d1SWang Shilong 
848db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
849db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
850db79e6d1SWang Shilong 					   count);
851db79e6d1SWang Shilong 		}
852db79e6d1SWang Shilong 	}
853db79e6d1SWang Shilong }
854db79e6d1SWang Shilong 
855617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
856ac27a0ecSDave Kleikamp {
857617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
858ac27a0ecSDave Kleikamp 
859617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
860ac27a0ecSDave Kleikamp 		return;
861ac27a0ecSDave Kleikamp 
86212062dddSEric Sandeen 	ext4_warning(sb,
863ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
864ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
865617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
866ac27a0ecSDave Kleikamp 
867617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
868617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
869617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
870ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
871ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
872ac27a0ecSDave Kleikamp 
873ac27a0ecSDave Kleikamp 	/*
874ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
875ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
876ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
877ac27a0ecSDave Kleikamp 	 */
878ac27a0ecSDave Kleikamp }
879ac27a0ecSDave Kleikamp 
880ac27a0ecSDave Kleikamp /*
881ac27a0ecSDave Kleikamp  * Open the external journal device
882ac27a0ecSDave Kleikamp  */
883b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
884ac27a0ecSDave Kleikamp {
885ac27a0ecSDave Kleikamp 	struct block_device *bdev;
886ac27a0ecSDave Kleikamp 	char b[BDEVNAME_SIZE];
887ac27a0ecSDave Kleikamp 
888d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
889ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
890ac27a0ecSDave Kleikamp 		goto fail;
891ac27a0ecSDave Kleikamp 	return bdev;
892ac27a0ecSDave Kleikamp 
893ac27a0ecSDave Kleikamp fail:
894b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
895ac27a0ecSDave Kleikamp 			__bdevname(dev, b), PTR_ERR(bdev));
896ac27a0ecSDave Kleikamp 	return NULL;
897ac27a0ecSDave Kleikamp }
898ac27a0ecSDave Kleikamp 
899ac27a0ecSDave Kleikamp /*
900ac27a0ecSDave Kleikamp  * Release the journal device
901ac27a0ecSDave Kleikamp  */
9024385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
903ac27a0ecSDave Kleikamp {
9044385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
905ac27a0ecSDave Kleikamp }
906ac27a0ecSDave Kleikamp 
9074385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
908ac27a0ecSDave Kleikamp {
909ac27a0ecSDave Kleikamp 	struct block_device *bdev;
910ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
911ac27a0ecSDave Kleikamp 	if (bdev) {
9124385bab1SAl Viro 		ext4_blkdev_put(bdev);
913ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
914ac27a0ecSDave Kleikamp 	}
915ac27a0ecSDave Kleikamp }
916ac27a0ecSDave Kleikamp 
917ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
918ac27a0ecSDave Kleikamp {
919617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
920ac27a0ecSDave Kleikamp }
921ac27a0ecSDave Kleikamp 
922617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
923ac27a0ecSDave Kleikamp {
924ac27a0ecSDave Kleikamp 	struct list_head *l;
925ac27a0ecSDave Kleikamp 
926b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
927ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
928ac27a0ecSDave Kleikamp 
929ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
930ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
931ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
932ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
933ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
934ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
935ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
936ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
937ac27a0ecSDave Kleikamp 	}
938ac27a0ecSDave Kleikamp }
939ac27a0ecSDave Kleikamp 
940957153fcSJan Kara #ifdef CONFIG_QUOTA
941957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
942957153fcSJan Kara 
943957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
944957153fcSJan Kara {
945957153fcSJan Kara 	int type;
946957153fcSJan Kara 
947957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
948957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
949957153fcSJan Kara 		ext4_quota_off(sb, type);
950957153fcSJan Kara }
95133458eabSTheodore Ts'o 
95233458eabSTheodore Ts'o /*
95333458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
95433458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
95533458eabSTheodore Ts'o  */
95633458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
95733458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
95833458eabSTheodore Ts'o 				int type)
95933458eabSTheodore Ts'o {
96033458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
96133458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
96233458eabSTheodore Ts'o }
963957153fcSJan Kara #else
964957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
965957153fcSJan Kara {
966957153fcSJan Kara }
967957153fcSJan Kara #endif
968957153fcSJan Kara 
969617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
970ac27a0ecSDave Kleikamp {
971617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
972617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
97397abd7d4STheodore Ts'o 	int aborted = 0;
974ef2cabf7SHidehiro Kawai 	int i, err;
975ac27a0ecSDave Kleikamp 
976857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
977957153fcSJan Kara 	ext4_quota_off_umount(sb);
978e0ccfd95SChristoph Hellwig 
9792e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
9804c0425ffSMingming Cao 
9810390131bSFrank Mayhar 	if (sbi->s_journal) {
98297abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
983ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
98447b4a50bSJan Kara 		sbi->s_journal = NULL;
98597abd7d4STheodore Ts'o 		if ((err < 0) && !aborted)
986c67d859eSTheodore Ts'o 			ext4_abort(sb, "Couldn't clean up the journal");
9870390131bSFrank Mayhar 	}
988d4edac31SJosef Bacik 
989ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
990d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
9919105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
992d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
993d4edac31SJosef Bacik 	ext4_mb_release(sb);
994d4edac31SJosef Bacik 	ext4_ext_release(sb);
995d4edac31SJosef Bacik 
996bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
997e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
998ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
999ac27a0ecSDave Kleikamp 	}
1000bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
1001a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
1002a8e25a83SArtem Bityutskiy 
1003ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
1004ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
1005b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
1006b93b41d4SAl Viro 	kvfree(sbi->s_flex_groups);
100757042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1008ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1009ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
101057042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1011c8585c6fSDaeho Jeong 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
1012ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1013a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
101433458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1015ac27a0ecSDave Kleikamp #endif
1016ac27a0ecSDave Kleikamp 
1017ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1018ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1019ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1020ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1021ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1022ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1023ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
1024ac27a0ecSDave Kleikamp 
102589d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1026f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1027ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
1028ac27a0ecSDave Kleikamp 		/*
1029ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1030ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1031ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1032ac27a0ecSDave Kleikamp 		 */
1033ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
1034f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
1035617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1036ac27a0ecSDave Kleikamp 	}
103750c15df6SChengguang Xu 
1038dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1039dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
104050c15df6SChengguang Xu 
104147387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
104247387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
104350c15df6SChengguang Xu 
1044c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1045c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
10469060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1047ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
10483197ebdbSTheodore Ts'o 	/*
10493197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
10503197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
10513197ebdbSTheodore Ts'o 	 */
10523197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
10533197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
10540441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
10550441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1056705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
10575e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1058c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1059c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
1060c83ad55eSGabriel Krisman Bertazi #endif
1061ac27a0ecSDave Kleikamp 	kfree(sbi);
1062ac27a0ecSDave Kleikamp }
1063ac27a0ecSDave Kleikamp 
1064e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1065ac27a0ecSDave Kleikamp 
1066ac27a0ecSDave Kleikamp /*
1067ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1068ac27a0ecSDave Kleikamp  */
1069617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1070ac27a0ecSDave Kleikamp {
1071617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1072ac27a0ecSDave Kleikamp 
1073e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1074ac27a0ecSDave Kleikamp 	if (!ei)
1075ac27a0ecSDave Kleikamp 		return NULL;
10760b8e58a1SAndreas Dilger 
1077ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1078202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1079c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
1080c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
10819a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
10829a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1083edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1084eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1085edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1086dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1087d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
10889d0be502STheodore Ts'o 	ei->i_da_metadata_calc_len = 0;
10897e731bc9STheodore Ts'o 	ei->i_da_metadata_calc_last_lblock = 0;
1090d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
10911dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1092a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1093a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
109496c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1095a9e7f447SDmitry Monakhov #endif
10968aefcd55STheodore Ts'o 	ei->jinode = NULL;
10972e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1098744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1099b436b9beSJan Kara 	ei->i_sync_tid = 0;
1100b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1101e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
11022e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1103ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1104ac27a0ecSDave Kleikamp }
1105ac27a0ecSDave Kleikamp 
11067ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
11077ff9c073STheodore Ts'o {
11087ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
11097ff9c073STheodore Ts'o 
111029b3692eSEric Biggers 	if (!drop)
111129b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
111229b3692eSEric Biggers 
11137ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
11147ff9c073STheodore Ts'o 	return drop;
11157ff9c073STheodore Ts'o }
11167ff9c073STheodore Ts'o 
111794053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1118fa0d7e3dSNick Piggin {
11192c58d548SEric Biggers 	fscrypt_free_inode(inode);
1120fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1121fa0d7e3dSNick Piggin }
1122fa0d7e3dSNick Piggin 
1123617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1124ac27a0ecSDave Kleikamp {
11259f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1126b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1127b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1128b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
11299f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
11309f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
11319f7dd93dSVasily Averin 				true);
11329f7dd93dSVasily Averin 		dump_stack();
11339f7dd93dSVasily Averin 	}
1134ac27a0ecSDave Kleikamp }
1135ac27a0ecSDave Kleikamp 
113651cc5068SAlexey Dobriyan static void init_once(void *foo)
1137ac27a0ecSDave Kleikamp {
1138617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1139ac27a0ecSDave Kleikamp 
1140ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1141ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
11420e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1143ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1144ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1145ac27a0ecSDave Kleikamp }
1146ac27a0ecSDave Kleikamp 
1147e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1148ac27a0ecSDave Kleikamp {
1149f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1150f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1151f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1152f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1153f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1154f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
115520c2df83SPaul Mundt 				init_once);
1156617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1157ac27a0ecSDave Kleikamp 		return -ENOMEM;
1158ac27a0ecSDave Kleikamp 	return 0;
1159ac27a0ecSDave Kleikamp }
1160ac27a0ecSDave Kleikamp 
1161ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1162ac27a0ecSDave Kleikamp {
11638c0a8537SKirill A. Shutemov 	/*
11648c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
11658c0a8537SKirill A. Shutemov 	 * destroy cache.
11668c0a8537SKirill A. Shutemov 	 */
11678c0a8537SKirill A. Shutemov 	rcu_barrier();
1168617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1169ac27a0ecSDave Kleikamp }
1170ac27a0ecSDave Kleikamp 
11710930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1172ac27a0ecSDave Kleikamp {
11730930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1174dbd5768fSJan Kara 	clear_inode(inode);
11759f754758SChristoph Hellwig 	dquot_drop(inode);
1176c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
117751865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
11788aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
11798aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
11808aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
11818aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
11828aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
11838aefcd55STheodore Ts'o 	}
11843d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1185c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1186ac27a0ecSDave Kleikamp }
1187ac27a0ecSDave Kleikamp 
11881b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
11891b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1190ac27a0ecSDave Kleikamp {
1191ac27a0ecSDave Kleikamp 	struct inode *inode;
1192ac27a0ecSDave Kleikamp 
11938a363970STheodore Ts'o 	/*
1194ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1195ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1196ac27a0ecSDave Kleikamp 	 */
11978a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
11981d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11991d1fe1eeSDavid Howells 		return ERR_CAST(inode);
12001d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1201ac27a0ecSDave Kleikamp 		iput(inode);
1202ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1203ac27a0ecSDave Kleikamp 	}
12041b961ac0SChristoph Hellwig 
12051b961ac0SChristoph Hellwig 	return inode;
1206ac27a0ecSDave Kleikamp }
12071b961ac0SChristoph Hellwig 
12081b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
12091b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12101b961ac0SChristoph Hellwig {
12111b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
12121b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
12131b961ac0SChristoph Hellwig }
12141b961ac0SChristoph Hellwig 
12151b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
12161b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12171b961ac0SChristoph Hellwig {
12181b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
12191b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1220ac27a0ecSDave Kleikamp }
1221ac27a0ecSDave Kleikamp 
1222fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1223fde87268STheodore Ts'o {
1224fde87268STheodore Ts'o 	struct writeback_control wbc = {
1225fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1226fde87268STheodore Ts'o 	};
1227fde87268STheodore Ts'o 
1228fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1229fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1230fde87268STheodore Ts'o }
1231fde87268STheodore Ts'o 
1232c39a7f84SToshiyuki Okajima /*
1233c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1234c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1235c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1236c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1237c39a7f84SToshiyuki Okajima  */
12380b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
12390b8e58a1SAndreas Dilger 				 gfp_t wait)
1240c39a7f84SToshiyuki Okajima {
1241c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1242c39a7f84SToshiyuki Okajima 
1243c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1244c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1245c39a7f84SToshiyuki Okajima 		return 0;
1246c39a7f84SToshiyuki Okajima 	if (journal)
1247c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1248d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1249c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1250c39a7f84SToshiyuki Okajima }
1251c39a7f84SToshiyuki Okajima 
1252643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1253a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1254a7550b30SJaegeuk Kim {
1255a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1256a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1257a7550b30SJaegeuk Kim }
1258a7550b30SJaegeuk Kim 
1259a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1260a7550b30SJaegeuk Kim 							void *fs_data)
1261a7550b30SJaegeuk Kim {
12622f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1263c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1264a7550b30SJaegeuk Kim 
12659ce0151aSEric Biggers 	/*
12669ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
12679ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
12689ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
12699ce0151aSEric Biggers 	 * the filename "lost+found" itself.
12709ce0151aSEric Biggers 	 */
12719ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
12729ce0151aSEric Biggers 		return -EPERM;
1273a7550b30SJaegeuk Kim 
12747d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
12757d3e06a8SRoss Zwisler 		return -EINVAL;
12767d3e06a8SRoss Zwisler 
127794840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
127894840e3cSEric Biggers 	if (res)
127994840e3cSEric Biggers 		return res;
128094840e3cSEric Biggers 
12812f8f5e76SEric Biggers 	/*
12822f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
12832f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
12842f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
12852f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
12862f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
12872f8f5e76SEric Biggers 	 */
12882f8f5e76SEric Biggers 
12892f8f5e76SEric Biggers 	if (handle) {
12902f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
12912f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
12922f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
12932f8f5e76SEric Biggers 					    ctx, len, 0);
1294a7550b30SJaegeuk Kim 		if (!res) {
1295a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1296a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1297a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1298a3caa24bSJan Kara 			/*
12992ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13002ee6a576SEric Biggers 			 * S_DAX may be disabled
1301a3caa24bSJan Kara 			 */
1302a3caa24bSJan Kara 			ext4_set_inode_flags(inode);
1303a7550b30SJaegeuk Kim 		}
1304a7550b30SJaegeuk Kim 		return res;
1305a7550b30SJaegeuk Kim 	}
1306a7550b30SJaegeuk Kim 
1307b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1308b8cb5a54STahsin Erdogan 	if (res)
1309b8cb5a54STahsin Erdogan 		return res;
13102f8f5e76SEric Biggers retry:
1311af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1312af65207cSTahsin Erdogan 				     &credits);
1313dec214d0STahsin Erdogan 	if (res)
1314dec214d0STahsin Erdogan 		return res;
1315dec214d0STahsin Erdogan 
1316c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1317a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1318a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1319a7550b30SJaegeuk Kim 
13202f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
13212f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13222f8f5e76SEric Biggers 				    ctx, len, 0);
1323a7550b30SJaegeuk Kim 	if (!res) {
1324a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
13252ee6a576SEric Biggers 		/*
13262ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13272ee6a576SEric Biggers 		 * S_DAX may be disabled
13282ee6a576SEric Biggers 		 */
1329a3caa24bSJan Kara 		ext4_set_inode_flags(inode);
1330a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1331a7550b30SJaegeuk Kim 		if (res)
1332a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1333a7550b30SJaegeuk Kim 	}
1334a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
13352f8f5e76SEric Biggers 
13362f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
13372f8f5e76SEric Biggers 		goto retry;
1338a7550b30SJaegeuk Kim 	if (!res)
1339a7550b30SJaegeuk Kim 		res = res2;
1340a7550b30SJaegeuk Kim 	return res;
1341a7550b30SJaegeuk Kim }
1342a7550b30SJaegeuk Kim 
1343c250b7ddSEric Biggers static bool ext4_dummy_context(struct inode *inode)
1344a7550b30SJaegeuk Kim {
1345a7550b30SJaegeuk Kim 	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1346a7550b30SJaegeuk Kim }
1347a7550b30SJaegeuk Kim 
13486f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1349a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1350a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1351a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1352a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1353a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1354e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1355a7550b30SJaegeuk Kim };
1356a7550b30SJaegeuk Kim #endif
1357a7550b30SJaegeuk Kim 
1358ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1359d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1360689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1361ac27a0ecSDave Kleikamp 
1362617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1363617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1364617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1365617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1366617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
13676f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
13688c54ca9cSAl Viro 			 const struct path *path);
1369617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1370617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1371ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1372617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1373ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
13747c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
13757c319d32SAditya Kali 			     unsigned int flags);
13767c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
13778f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
1378ac27a0ecSDave Kleikamp 
137996c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
138096c7e0d9SJan Kara {
138196c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
138296c7e0d9SJan Kara }
138396c7e0d9SJan Kara 
138461e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
138560e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1386617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1387617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1388617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1389617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1390a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1391a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1392a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1393040cb378SLi Xi 	.get_projid		= ext4_get_projid,
13947a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
13958f0e8746STheodore Ts'o 	.get_next_id		= ext4_get_next_id,
1396ac27a0ecSDave Kleikamp };
1397ac27a0ecSDave Kleikamp 
13980d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1399617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1400ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1401287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
14020a240339SJan Kara 	.get_state	= dquot_get_state,
1403287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1404287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
14056332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
14066332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1407ac27a0ecSDave Kleikamp };
1408ac27a0ecSDave Kleikamp #endif
1409ac27a0ecSDave Kleikamp 
1410ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1411617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
141294053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1413617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1414617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1415617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
14167ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
14170930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1418617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1419617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1420c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1421c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1422617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1423617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1424617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1425ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1426617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1427617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
142896c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1429ac27a0ecSDave Kleikamp #endif
1430c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1431ac27a0ecSDave Kleikamp };
1432ac27a0ecSDave Kleikamp 
143339655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
14341b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
14351b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1436617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1437fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1438ac27a0ecSDave Kleikamp };
1439ac27a0ecSDave Kleikamp 
1440ac27a0ecSDave Kleikamp enum {
1441ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1442ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
144372578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1444ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
144572578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1446ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1447ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1448ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
14496ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1450ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
14515a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1452ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
145349da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
1454327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1455327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1456670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
14571449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
14585328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1459744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1460fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1461cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1462ac27a0ecSDave Kleikamp };
1463ac27a0ecSDave Kleikamp 
1464a447c093SSteven Whitehouse static const match_table_t tokens = {
1465ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1466ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1467ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1468ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1469ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1470ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1471ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1472ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1473ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1474ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1475ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1476ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1477ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1478ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
147972578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
148072578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1481ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1482ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1483ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1484ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1485e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
14865a916be1STheodore Ts'o 	{Opt_noload, "noload"},
148772578c33STheodore Ts'o 	{Opt_removed, "nobh"},
148872578c33STheodore Ts'o 	{Opt_removed, "bh"},
1489ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
149030773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
149130773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1492ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1493ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1494818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1495c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1496818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1497ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1498ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1499ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1500ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
15015bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
15025bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1503ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1504ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1505ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1506ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1507ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1508ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15095a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1510ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1511ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1512ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1513ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
151449da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1515ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
151606705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
151706705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
151825ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1519923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1520c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
152164769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1522327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1523327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1524a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1525a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1526670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1527dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
152836ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
152936ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15306fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15316fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1532240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1533b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1534afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
153506705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
153606705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1537744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1538744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
15395328e635SEric Sandeen 	{Opt_discard, "discard"},
15405328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1541fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1542fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1543fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1544df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
15456ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1546cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1547cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1548c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1549c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1550c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1551c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1552c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1553f3f12faaSJosef Bacik 	{Opt_err, NULL},
1554ac27a0ecSDave Kleikamp };
1555ac27a0ecSDave Kleikamp 
1556617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1557ac27a0ecSDave Kleikamp {
1558617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1559ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1560ac27a0ecSDave Kleikamp 
1561ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1562ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
15630b8e58a1SAndreas Dilger 
1564ac27a0ecSDave Kleikamp 	options += 3;
15650b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1566ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1567ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
15684776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1569ac27a0ecSDave Kleikamp 		       (char *) *data);
1570ac27a0ecSDave Kleikamp 		return 1;
1571ac27a0ecSDave Kleikamp 	}
1572ac27a0ecSDave Kleikamp 	if (*options == ',')
1573ac27a0ecSDave Kleikamp 		options++;
1574ac27a0ecSDave Kleikamp 	*data = (void *) options;
15750b8e58a1SAndreas Dilger 
1576ac27a0ecSDave Kleikamp 	return sb_block;
1577ac27a0ecSDave Kleikamp }
1578ac27a0ecSDave Kleikamp 
1579b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1580d6006186SEric Biggers static const char deprecated_msg[] =
1581d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1582437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1583b3881f74STheodore Ts'o 
158456c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
158556c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
158656c50f11SDmitry Monakhov {
158756c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
158833458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
158903dafb5fSChen Gang 	int ret = -1;
159056c50f11SDmitry Monakhov 
159133458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
159256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
159356c50f11SDmitry Monakhov 			"Cannot change journaled "
159456c50f11SDmitry Monakhov 			"quota options when quota turned on");
159557f73c2cSTheodore Ts'o 		return -1;
159656c50f11SDmitry Monakhov 	}
1597e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1598c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1599c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1600c325a67cSTheodore Ts'o 		return 1;
1601262b4662SJan Kara 	}
160256c50f11SDmitry Monakhov 	qname = match_strdup(args);
160356c50f11SDmitry Monakhov 	if (!qname) {
160456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
160556c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
160657f73c2cSTheodore Ts'o 		return -1;
160756c50f11SDmitry Monakhov 	}
160833458eabSTheodore Ts'o 	if (old_qname) {
160933458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
161003dafb5fSChen Gang 			ret = 1;
161103dafb5fSChen Gang 		else
161256c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
161303dafb5fSChen Gang 				 "%s quota file already specified",
161403dafb5fSChen Gang 				 QTYPE2NAME(qtype));
161503dafb5fSChen Gang 		goto errout;
161656c50f11SDmitry Monakhov 	}
161703dafb5fSChen Gang 	if (strchr(qname, '/')) {
161856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
161956c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
162003dafb5fSChen Gang 		goto errout;
162156c50f11SDmitry Monakhov 	}
162233458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1623fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
162456c50f11SDmitry Monakhov 	return 1;
162503dafb5fSChen Gang errout:
162603dafb5fSChen Gang 	kfree(qname);
162703dafb5fSChen Gang 	return ret;
162856c50f11SDmitry Monakhov }
162956c50f11SDmitry Monakhov 
163056c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
163156c50f11SDmitry Monakhov {
163256c50f11SDmitry Monakhov 
163356c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
163433458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
163556c50f11SDmitry Monakhov 
163633458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
163756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
163856c50f11SDmitry Monakhov 			" when quota turned on");
163957f73c2cSTheodore Ts'o 		return -1;
164056c50f11SDmitry Monakhov 	}
164133458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
164233458eabSTheodore Ts'o 	synchronize_rcu();
164333458eabSTheodore Ts'o 	kfree(old_qname);
164456c50f11SDmitry Monakhov 	return 1;
164556c50f11SDmitry Monakhov }
164656c50f11SDmitry Monakhov #endif
164756c50f11SDmitry Monakhov 
164826092bf5STheodore Ts'o #define MOPT_SET	0x0001
164926092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
165026092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
165126092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
165226092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
165326092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
165426092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
165526092bf5STheodore Ts'o #define MOPT_Q		0
165626092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
165726092bf5STheodore Ts'o #else
165826092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
165926092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
166026092bf5STheodore Ts'o #endif
166126092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
16628dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
16638dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
16648dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1665ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
166626092bf5STheodore Ts'o 
166726092bf5STheodore Ts'o static const struct mount_opts {
166826092bf5STheodore Ts'o 	int	token;
166926092bf5STheodore Ts'o 	int	mount_opt;
167026092bf5STheodore Ts'o 	int	flags;
167126092bf5STheodore Ts'o } ext4_mount_opts[] = {
167226092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
167326092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
167426092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
167526092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
167626092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
167726092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
16788dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
16798dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
16808dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
16818dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
168226092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
168326092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
16848dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
16858dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
16868dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
168759d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1688327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1689327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1690c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1691c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
16928dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
16931e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
169426092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
16958dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
16961e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
16978dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
169826092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
169926092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
170026092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17018dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17027915a861SAles Novak 	 MOPT_NO_EXT2},
17038dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17047915a861SAles Novak 	 MOPT_NO_EXT2},
170526092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
170626092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
170726092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
170826092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
170926092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
171026092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
171126092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
171226092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
171326092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
171426092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1715923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
171626092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17170efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17180efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17195ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17205ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17215ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17228dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17238dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17248dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17258dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
172626092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
172726092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
172826092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
172926092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
173026092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
173126092bf5STheodore Ts'o #else
173226092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
173326092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
173426092bf5STheodore Ts'o #endif
173526092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
173626092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1737670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
173826092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
173926092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
174026092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
174126092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
174226092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
174349da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
174449da9392SJan Kara 							MOPT_SET | MOPT_Q},
174526092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
174649da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
174749da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
174826092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
174926092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
175026092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
175126092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
175226092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
175326092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
175426092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1755df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
17566ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1757cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
175826092bf5STheodore Ts'o 	{Opt_err, 0, 0}
175926092bf5STheodore Ts'o };
176026092bf5STheodore Ts'o 
1761c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1762c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1763c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1764c83ad55eSGabriel Krisman Bertazi 	char *name;
1765c83ad55eSGabriel Krisman Bertazi 	char *version;
1766c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1767c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1768c83ad55eSGabriel Krisman Bertazi };
1769c83ad55eSGabriel Krisman Bertazi 
1770c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1771c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1772c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1773c83ad55eSGabriel Krisman Bertazi {
1774c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1775c83ad55eSGabriel Krisman Bertazi 	int i;
1776c83ad55eSGabriel Krisman Bertazi 
1777c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1778c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1779c83ad55eSGabriel Krisman Bertazi 			break;
1780c83ad55eSGabriel Krisman Bertazi 
1781c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1782c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1783c83ad55eSGabriel Krisman Bertazi 
1784c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1785c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1786c83ad55eSGabriel Krisman Bertazi 
1787c83ad55eSGabriel Krisman Bertazi 	return 0;
1788c83ad55eSGabriel Krisman Bertazi }
1789c83ad55eSGabriel Krisman Bertazi #endif
1790c83ad55eSGabriel Krisman Bertazi 
179126092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
179226092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
179326092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
179426092bf5STheodore Ts'o {
179526092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
179626092bf5STheodore Ts'o 	const struct mount_opts *m;
179708cefc7aSEric W. Biederman 	kuid_t uid;
179808cefc7aSEric W. Biederman 	kgid_t gid;
179926092bf5STheodore Ts'o 	int arg = 0;
180026092bf5STheodore Ts'o 
180157f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
180257f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
180357f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
180457f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
180557f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
180657f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
180757f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
180857f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
180957f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
181057f73c2cSTheodore Ts'o #endif
181126092bf5STheodore Ts'o 	switch (token) {
1812f7048605STheodore Ts'o 	case Opt_noacl:
1813f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1814f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1815f7048605STheodore Ts'o 		break;
181626092bf5STheodore Ts'o 	case Opt_sb:
181726092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
181826092bf5STheodore Ts'o 	case Opt_removed:
18195f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
182026092bf5STheodore Ts'o 		return 1;
182126092bf5STheodore Ts'o 	case Opt_abort:
182226092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
182326092bf5STheodore Ts'o 		return 1;
182426092bf5STheodore Ts'o 	case Opt_i_version:
1825357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
182626092bf5STheodore Ts'o 		return 1;
1827a26f4992STheodore Ts'o 	case Opt_lazytime:
18281751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1829a26f4992STheodore Ts'o 		return 1;
1830a26f4992STheodore Ts'o 	case Opt_nolazytime:
18311751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1832a26f4992STheodore Ts'o 		return 1;
183326092bf5STheodore Ts'o 	}
183426092bf5STheodore Ts'o 
18355f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
18365f3633e3SJan Kara 		if (token == m->token)
18375f3633e3SJan Kara 			break;
18385f3633e3SJan Kara 
18395f3633e3SJan Kara 	if (m->token == Opt_err) {
18405f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
18415f3633e3SJan Kara 			 "or missing value", opt);
18425f3633e3SJan Kara 		return -1;
18435f3633e3SJan Kara 	}
18445f3633e3SJan Kara 
18458dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
18468dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
18478dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
18488dc0aa8cSTheodore Ts'o 		return -1;
18498dc0aa8cSTheodore Ts'o 	}
18508dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
18518dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
18528dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
18538dc0aa8cSTheodore Ts'o 		return -1;
18548dc0aa8cSTheodore Ts'o 	}
18558dc0aa8cSTheodore Ts'o 
1856ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
18570efb3b23SJan Kara 		return -1;
185826092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
185926092bf5STheodore Ts'o 		return -1;
1860c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1861c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
186226092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
18631e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
18641e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1865c93cf2d7SDmitry Monakhov 		} else
1866c93cf2d7SDmitry Monakhov 			return -1;
1867c93cf2d7SDmitry Monakhov 	}
186826092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
186926092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
187026092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
187126092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
187226092bf5STheodore Ts'o 			 "options when quota turned on");
187326092bf5STheodore Ts'o 		return -1;
187426092bf5STheodore Ts'o 	}
187526092bf5STheodore Ts'o 
187626092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
187726092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
187826092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
187926092bf5STheodore Ts'o 		if (arg == 0)
188026092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
18819ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
18829ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
18839ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
18849ba55543Szhangyi (F) 				 "must be smaller than %d",
18859ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
18869ba55543Szhangyi (F) 			return -1;
18879ba55543Szhangyi (F) 		}
188826092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1889670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
1890670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
189126092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
189226092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
189326092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
189426092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
189526092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1896e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1897e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1898e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1899e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
190026092bf5STheodore Ts'o 			return -1;
190126092bf5STheodore Ts'o 		}
190226092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
190326092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
190426092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
190526092bf5STheodore Ts'o 		if (!args->from)
190626092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
190726092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1908df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1909df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
191026092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
191126092bf5STheodore Ts'o 		sbi->s_stripe = arg;
19120efb3b23SJan Kara 	} else if (token == Opt_resuid) {
19130efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
19140efb3b23SJan Kara 		if (!uid_valid(uid)) {
19155f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
19160efb3b23SJan Kara 			return -1;
19170efb3b23SJan Kara 		}
19180efb3b23SJan Kara 		sbi->s_resuid = uid;
19190efb3b23SJan Kara 	} else if (token == Opt_resgid) {
19200efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
19210efb3b23SJan Kara 		if (!gid_valid(gid)) {
19225f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
19230efb3b23SJan Kara 			return -1;
19240efb3b23SJan Kara 		}
19250efb3b23SJan Kara 		sbi->s_resgid = gid;
19260efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
19270efb3b23SJan Kara 		if (is_remount) {
19280efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
19290efb3b23SJan Kara 				 "Cannot specify journal on remount");
19300efb3b23SJan Kara 			return -1;
19310efb3b23SJan Kara 		}
19320efb3b23SJan Kara 		*journal_devnum = arg;
1933ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1934ad4eec61SEric Sandeen 		char *journal_path;
1935ad4eec61SEric Sandeen 		struct inode *journal_inode;
1936ad4eec61SEric Sandeen 		struct path path;
1937ad4eec61SEric Sandeen 		int error;
1938ad4eec61SEric Sandeen 
1939ad4eec61SEric Sandeen 		if (is_remount) {
1940ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1941ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1942ad4eec61SEric Sandeen 			return -1;
1943ad4eec61SEric Sandeen 		}
1944ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1945ad4eec61SEric Sandeen 		if (!journal_path) {
1946ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1947ad4eec61SEric Sandeen 				"journal device string");
1948ad4eec61SEric Sandeen 			return -1;
1949ad4eec61SEric Sandeen 		}
1950ad4eec61SEric Sandeen 
1951ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1952ad4eec61SEric Sandeen 		if (error) {
1953ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1954ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1955ad4eec61SEric Sandeen 			kfree(journal_path);
1956ad4eec61SEric Sandeen 			return -1;
1957ad4eec61SEric Sandeen 		}
1958ad4eec61SEric Sandeen 
19592b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
1960ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1961ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1962ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1963ad4eec61SEric Sandeen 			path_put(&path);
1964ad4eec61SEric Sandeen 			kfree(journal_path);
1965ad4eec61SEric Sandeen 			return -1;
1966ad4eec61SEric Sandeen 		}
1967ad4eec61SEric Sandeen 
1968ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1969ad4eec61SEric Sandeen 		path_put(&path);
1970ad4eec61SEric Sandeen 		kfree(journal_path);
19710efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
19720efb3b23SJan Kara 		if (arg > 7) {
19735f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
19740efb3b23SJan Kara 				 " (must be 0-7)");
19750efb3b23SJan Kara 			return -1;
19760efb3b23SJan Kara 		}
19770efb3b23SJan Kara 		*journal_ioprio =
19780efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
19796ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
1980643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
19816ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
19826ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
19836ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
19846ddb2447STheodore Ts'o #else
19856ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
19866ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
19876ddb2447STheodore Ts'o #endif
198826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
198926092bf5STheodore Ts'o 		if (is_remount) {
199026092bf5STheodore Ts'o 			if (!sbi->s_journal)
199126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
19925f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
199326092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
199426092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
199526092bf5STheodore Ts'o 				return -1;
199626092bf5STheodore Ts'o 			}
199726092bf5STheodore Ts'o 		} else {
199826092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
199926092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
200026092bf5STheodore Ts'o 		}
200126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
200226092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
200326092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
200426092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
20055f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
20065f3633e3SJan Kara 				 "quota options when quota turned on");
200726092bf5STheodore Ts'o 			return -1;
200826092bf5STheodore Ts'o 		}
2009e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2010c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2011c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2012262b4662SJan Kara 				 "when QUOTA feature is enabled");
2013c325a67cSTheodore Ts'o 			return 1;
2014262b4662SJan Kara 		}
201526092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
201626092bf5STheodore Ts'o #endif
2017923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
2018ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2019ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
2020ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2021ef83b6e8SDan Williams 		sbi->s_mount_opt |= m->mount_opt;
2022ef83b6e8SDan Williams #else
2023923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2024923ae0ffSRoss Zwisler 		return -1;
2025923ae0ffSRoss Zwisler #endif
20267915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
20277915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
20287915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
20297915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
203026092bf5STheodore Ts'o 	} else {
203126092bf5STheodore Ts'o 		if (!args->from)
203226092bf5STheodore Ts'o 			arg = 1;
203326092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
203426092bf5STheodore Ts'o 			arg = !arg;
203526092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
203626092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
203726092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
203826092bf5STheodore Ts'o 			WARN_ON(1);
203926092bf5STheodore Ts'o 			return -1;
204026092bf5STheodore Ts'o 		}
204126092bf5STheodore Ts'o 		if (arg != 0)
204226092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
204326092bf5STheodore Ts'o 		else
204426092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
204526092bf5STheodore Ts'o 	}
204626092bf5STheodore Ts'o 	return 1;
204726092bf5STheodore Ts'o }
204826092bf5STheodore Ts'o 
2049ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2050c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2051b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2052661aa520SEric Sandeen 			 int is_remount)
2053ac27a0ecSDave Kleikamp {
2054*1e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
205533458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2056ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
205726092bf5STheodore Ts'o 	int token;
2058ac27a0ecSDave Kleikamp 
2059ac27a0ecSDave Kleikamp 	if (!options)
2060ac27a0ecSDave Kleikamp 		return 1;
2061ac27a0ecSDave Kleikamp 
2062ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2063ac27a0ecSDave Kleikamp 		if (!*p)
2064ac27a0ecSDave Kleikamp 			continue;
206515121c18SEric Sandeen 		/*
206615121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
206715121c18SEric Sandeen 		 * found; some options take optional arguments.
206815121c18SEric Sandeen 		 */
2069caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2070ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
207126092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
207226092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2073ac27a0ecSDave Kleikamp 			return 0;
2074ac27a0ecSDave Kleikamp 	}
2075ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
207649da9392SJan Kara 	/*
207749da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
207849da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
207949da9392SJan Kara 	 * to support legacy quotas in quota files.
208049da9392SJan Kara 	 */
208149da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
208249da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
208349da9392SJan Kara 			 "Cannot enable project quota enforcement.");
208449da9392SJan Kara 		return 0;
208549da9392SJan Kara 	}
208633458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
208733458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
208833458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
208933458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2090fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2091ac27a0ecSDave Kleikamp 
209233458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2093fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2094ac27a0ecSDave Kleikamp 
209556c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2096b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2097b31e1552SEric Sandeen 					"format mixing");
2098ac27a0ecSDave Kleikamp 			return 0;
2099ac27a0ecSDave Kleikamp 		}
2100ac27a0ecSDave Kleikamp 
2101ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2102b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2103b31e1552SEric Sandeen 					"not specified");
2104ac27a0ecSDave Kleikamp 			return 0;
2105ac27a0ecSDave Kleikamp 		}
2106ac27a0ecSDave Kleikamp 	}
2107ac27a0ecSDave Kleikamp #endif
2108ac27a0ecSDave Kleikamp 	return 1;
2109ac27a0ecSDave Kleikamp }
2110ac27a0ecSDave Kleikamp 
21112adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
21122adf6da8STheodore Ts'o 					   struct super_block *sb)
21132adf6da8STheodore Ts'o {
21142adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
21152adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
211633458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
21172adf6da8STheodore Ts'o 
21182adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
21192adf6da8STheodore Ts'o 		char *fmtname = "";
21202adf6da8STheodore Ts'o 
21212adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
21222adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
21232adf6da8STheodore Ts'o 			fmtname = "vfsold";
21242adf6da8STheodore Ts'o 			break;
21252adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
21262adf6da8STheodore Ts'o 			fmtname = "vfsv0";
21272adf6da8STheodore Ts'o 			break;
21282adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
21292adf6da8STheodore Ts'o 			fmtname = "vfsv1";
21302adf6da8STheodore Ts'o 			break;
21312adf6da8STheodore Ts'o 		}
21322adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
21332adf6da8STheodore Ts'o 	}
21342adf6da8STheodore Ts'o 
213533458eabSTheodore Ts'o 	rcu_read_lock();
213633458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
213733458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
213833458eabSTheodore Ts'o 	if (usr_qf_name)
213933458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
214033458eabSTheodore Ts'o 	if (grp_qf_name)
214133458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
214233458eabSTheodore Ts'o 	rcu_read_unlock();
21432adf6da8STheodore Ts'o #endif
21442adf6da8STheodore Ts'o }
21452adf6da8STheodore Ts'o 
21465a916be1STheodore Ts'o static const char *token2str(int token)
21475a916be1STheodore Ts'o {
214850df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
21495a916be1STheodore Ts'o 
21505a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
21515a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
21525a916be1STheodore Ts'o 			break;
21535a916be1STheodore Ts'o 	return t->pattern;
21545a916be1STheodore Ts'o }
21555a916be1STheodore Ts'o 
21562adf6da8STheodore Ts'o /*
21572adf6da8STheodore Ts'o  * Show an option if
21582adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
21592adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
21602adf6da8STheodore Ts'o  */
216166acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
216266acdcf4STheodore Ts'o 			      int nodefs)
21632adf6da8STheodore Ts'o {
21642adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
21652adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
216668afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
21675a916be1STheodore Ts'o 	const struct mount_opts *m;
216866acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
21692adf6da8STheodore Ts'o 
217066acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
217166acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
21722adf6da8STheodore Ts'o 
21732adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
21745a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
21755a916be1STheodore Ts'o 
21765a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
21775a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
21785a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
21795a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
21805a916be1STheodore Ts'o 			continue;
218168afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
21825a916be1STheodore Ts'o 			continue; /* skip if same as the default */
21835a916be1STheodore Ts'o 		if ((want_set &&
21845a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
21855a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
21865a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
21875a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
21885a916be1STheodore Ts'o 	}
21895a916be1STheodore Ts'o 
219008cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
21915a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
219208cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
219308cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
219408cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
21955a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
219608cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
219708cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
219866acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
21995a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
22005a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
22012adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
22025a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
22032adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
22045a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
220566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
22065a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
220766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
22085a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
220966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
22105a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2211357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
22125a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
221366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
22145a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
221568afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
221668afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
22172adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
22185a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
22192adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
22205a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
22212adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
22225a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
22235a916be1STheodore Ts'o 	}
222466acdcf4STheodore Ts'o 	if (nodefs ||
222566acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
22265a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
22272adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
22282adf6da8STheodore Ts'o 
2229ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
223066acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
22315a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2232df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2233df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
22347915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
22357915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2236338affb5SEric Biggers 	if (DUMMY_ENCRYPTION_ENABLED(sbi))
2237338affb5SEric Biggers 		SEQ_OPTS_PUTS("test_dummy_encryption");
22382adf6da8STheodore Ts'o 
22392adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
22402adf6da8STheodore Ts'o 	return 0;
22412adf6da8STheodore Ts'o }
22422adf6da8STheodore Ts'o 
224366acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
224466acdcf4STheodore Ts'o {
224566acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
224666acdcf4STheodore Ts'o }
224766acdcf4STheodore Ts'o 
2248ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
224966acdcf4STheodore Ts'o {
225066acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
225166acdcf4STheodore Ts'o 	int rc;
225266acdcf4STheodore Ts'o 
2253bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
225466acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
225566acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
225666acdcf4STheodore Ts'o 	return rc;
225766acdcf4STheodore Ts'o }
225866acdcf4STheodore Ts'o 
2259617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2260ac27a0ecSDave Kleikamp 			    int read_only)
2261ac27a0ecSDave Kleikamp {
2262617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2263c89128a0SJaegeuk Kim 	int err = 0;
2264ac27a0ecSDave Kleikamp 
2265617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2266b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2267b31e1552SEric Sandeen 			 "forcing read-only mode");
2268c89128a0SJaegeuk Kim 		err = -EROFS;
2269ac27a0ecSDave Kleikamp 	}
2270ac27a0ecSDave Kleikamp 	if (read_only)
2271281b5995STheodore Ts'o 		goto done;
2272617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2273b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2274b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2275c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2276b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2277b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2278b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2279ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2280ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2281ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2282b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2283b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2284b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2285ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
22866a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
22876a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2288b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2289b31e1552SEric Sandeen 			 "warning: checktime reached, "
2290b31e1552SEric Sandeen 			 "running e2fsck is recommended");
22910390131bSFrank Mayhar 	if (!sbi->s_journal)
2292216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2293ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2294617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2295e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
22966a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
22970390131bSFrank Mayhar 	if (sbi->s_journal)
2298e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2299ac27a0ecSDave Kleikamp 
2300c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2301281b5995STheodore Ts'o done:
2302ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2303a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2304a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2305ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2306ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2307617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2308617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2309a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2310ac27a0ecSDave Kleikamp 
23117abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2312c89128a0SJaegeuk Kim 	return err;
2313ac27a0ecSDave Kleikamp }
2314ac27a0ecSDave Kleikamp 
2315117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2316117fff10STheodore Ts'o {
2317117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2318117fff10STheodore Ts'o 	struct flex_groups *new_groups;
2319117fff10STheodore Ts'o 	int size;
2320117fff10STheodore Ts'o 
2321117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2322117fff10STheodore Ts'o 		return 0;
2323117fff10STheodore Ts'o 
2324117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2325117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2326117fff10STheodore Ts'o 		return 0;
2327117fff10STheodore Ts'o 
2328117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2329a7c3e901SMichal Hocko 	new_groups = kvzalloc(size, GFP_KERNEL);
2330117fff10STheodore Ts'o 	if (!new_groups) {
2331117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2332117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
2333117fff10STheodore Ts'o 		return -ENOMEM;
2334117fff10STheodore Ts'o 	}
2335117fff10STheodore Ts'o 
2336117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
2337117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
2338117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
2339117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
2340b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
2341117fff10STheodore Ts'o 	}
2342117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
2343117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
2344117fff10STheodore Ts'o 	return 0;
2345117fff10STheodore Ts'o }
2346117fff10STheodore Ts'o 
2347772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2348772cb7c8SJose R. Santos {
2349772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2350772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2351772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2352117fff10STheodore Ts'o 	int i, err;
2353772cb7c8SJose R. Santos 
2354503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2355d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2356772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2357772cb7c8SJose R. Santos 		return 1;
2358772cb7c8SJose R. Santos 	}
2359772cb7c8SJose R. Santos 
2360117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2361117fff10STheodore Ts'o 	if (err)
2362772cb7c8SJose R. Santos 		goto failed;
2363772cb7c8SJose R. Santos 
2364772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
236588b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2366772cb7c8SJose R. Santos 
2367772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
23687ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
23697ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
237090ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
237124aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
23727ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
23737ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2374772cb7c8SJose R. Santos 	}
2375772cb7c8SJose R. Santos 
2376772cb7c8SJose R. Santos 	return 1;
2377772cb7c8SJose R. Santos failed:
2378772cb7c8SJose R. Santos 	return 0;
2379772cb7c8SJose R. Santos }
2380772cb7c8SJose R. Santos 
2381e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2382717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2383717d50e4SAndreas Dilger {
2384b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2385717d50e4SAndreas Dilger 	__u16 crc = 0;
2386717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2387e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2388717d50e4SAndreas Dilger 
23899aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2390feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2391feb0ab32SDarrick J. Wong 		__u32 csum32;
2392b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2393feb0ab32SDarrick J. Wong 
2394feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2395feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2396b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2397b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2398b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2399b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2400b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2401b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2402b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2403feb0ab32SDarrick J. Wong 
2404feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2405feb0ab32SDarrick J. Wong 		goto out;
2406feb0ab32SDarrick J. Wong 	}
2407feb0ab32SDarrick J. Wong 
2408feb0ab32SDarrick J. Wong 	/* old crc16 code */
2409e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2410813d32f9SDarrick J. Wong 		return 0;
2411813d32f9SDarrick J. Wong 
2412717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2413717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2414717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2415717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2416717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2417e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2418717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2419717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2420717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2421717d50e4SAndreas Dilger 				offset);
2422717d50e4SAndreas Dilger 
2423feb0ab32SDarrick J. Wong out:
2424717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2425717d50e4SAndreas Dilger }
2426717d50e4SAndreas Dilger 
2427feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2428717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2429717d50e4SAndreas Dilger {
2430feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2431e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2432717d50e4SAndreas Dilger 		return 0;
2433717d50e4SAndreas Dilger 
2434717d50e4SAndreas Dilger 	return 1;
2435717d50e4SAndreas Dilger }
2436717d50e4SAndreas Dilger 
2437feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2438feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2439feb0ab32SDarrick J. Wong {
2440feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2441feb0ab32SDarrick J. Wong 		return;
2442e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2443feb0ab32SDarrick J. Wong }
2444feb0ab32SDarrick J. Wong 
2445ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2446bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2447829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2448bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2449ac27a0ecSDave Kleikamp {
2450617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2451617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2452617ba13bSMingming Cao 	ext4_fsblk_t last_block;
245344de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2454bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2455bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2456bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2457ce421581SJose R. Santos 	int flexbg_flag = 0;
2458bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2459ac27a0ecSDave Kleikamp 
2460e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2461ce421581SJose R. Santos 		flexbg_flag = 1;
2462ce421581SJose R. Santos 
2463617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2464ac27a0ecSDave Kleikamp 
2465197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2466197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2467197cd65aSAkinobu Mita 
2468ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2469bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2470ac27a0ecSDave Kleikamp 		else
2471ac27a0ecSDave Kleikamp 			last_block = first_block +
2472617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2473ac27a0ecSDave Kleikamp 
2474bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2475bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2476bfff6873SLukas Czerner 			grp = i;
2477bfff6873SLukas Czerner 
24788fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2479829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2480829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2481829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2482829fa70dSTheodore Ts'o 				 "superblock", i);
248318db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
248418db4b4eSTheodore Ts'o 				return 0;
2485829fa70dSTheodore Ts'o 		}
248677260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
248777260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
248877260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
248977260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
249077260807STheodore Ts'o 				 "block group descriptors", i);
249177260807STheodore Ts'o 			if (!sb_rdonly(sb))
249277260807STheodore Ts'o 				return 0;
249377260807STheodore Ts'o 		}
24942b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2495b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2496a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2497b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2498ac27a0ecSDave Kleikamp 			return 0;
2499ac27a0ecSDave Kleikamp 		}
25008fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2501829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2502829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2503829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2504829fa70dSTheodore Ts'o 				 "superblock", i);
250518db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
250618db4b4eSTheodore Ts'o 				return 0;
2507829fa70dSTheodore Ts'o 		}
250877260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
250977260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
251077260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
251177260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
251277260807STheodore Ts'o 				 "block group descriptors", i);
251377260807STheodore Ts'o 			if (!sb_rdonly(sb))
251477260807STheodore Ts'o 				return 0;
251577260807STheodore Ts'o 		}
25162b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2517b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2518a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2519b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2520ac27a0ecSDave Kleikamp 			return 0;
2521ac27a0ecSDave Kleikamp 		}
25228fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2523829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2524829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2525829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2526829fa70dSTheodore Ts'o 				 "superblock", i);
252718db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
252818db4b4eSTheodore Ts'o 				return 0;
2529829fa70dSTheodore Ts'o 		}
253077260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
253177260807STheodore Ts'o 		    inode_table <= last_bg_block) {
253277260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
253377260807STheodore Ts'o 				 "Inode table for group %u overlaps "
253477260807STheodore Ts'o 				 "block group descriptors", i);
253577260807STheodore Ts'o 			if (!sb_rdonly(sb))
253677260807STheodore Ts'o 				return 0;
253777260807STheodore Ts'o 		}
2538bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
25392b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2540b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2541a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2542b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2543ac27a0ecSDave Kleikamp 			return 0;
2544ac27a0ecSDave Kleikamp 		}
2545955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2546feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2547b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2548b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2549e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2550fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2551bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2552955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2553717d50e4SAndreas Dilger 				return 0;
2554717d50e4SAndreas Dilger 			}
25557ee1ec4cSLi Zefan 		}
2556955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2557ce421581SJose R. Santos 		if (!flexbg_flag)
2558617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2559ac27a0ecSDave Kleikamp 	}
2560bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2561bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2562ac27a0ecSDave Kleikamp 	return 1;
2563ac27a0ecSDave Kleikamp }
2564ac27a0ecSDave Kleikamp 
2565617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2566ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2567ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2568ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2569ac27a0ecSDave Kleikamp  *
2570ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2571ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2572ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2573ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2574ac27a0ecSDave Kleikamp  *
2575ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2576ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2577ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2578617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2579ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2580ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2581ac27a0ecSDave Kleikamp  */
2582617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2583617ba13bSMingming Cao 				struct ext4_super_block *es)
2584ac27a0ecSDave Kleikamp {
2585ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
25862c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2587ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
258895f1fda4Szhangyi (F) 	int quota_update = 0;
2589ac27a0ecSDave Kleikamp 	int i;
2590ac27a0ecSDave Kleikamp #endif
2591ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2592ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2593ac27a0ecSDave Kleikamp 		return;
2594ac27a0ecSDave Kleikamp 	}
2595ac27a0ecSDave Kleikamp 
2596a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2597b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2598b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2599a8f48a95SEric Sandeen 		return;
2600a8f48a95SEric Sandeen 	}
2601a8f48a95SEric Sandeen 
2602d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2603d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2604d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2605d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2606d39195c3SAmir Goldstein 		return;
2607d39195c3SAmir Goldstein 	}
2608d39195c3SAmir Goldstein 
2609617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2610c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
26111751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
261284474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2613ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2614ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2615c25f9bc6SEric Sandeen 		}
2616ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2617ac27a0ecSDave Kleikamp 		return;
2618ac27a0ecSDave Kleikamp 	}
2619ac27a0ecSDave Kleikamp 
26201751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2621b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
26221751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2623ac27a0ecSDave Kleikamp 	}
2624ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2625ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
26261751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
262795f1fda4Szhangyi (F) 
262895f1fda4Szhangyi (F) 	/*
262995f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
263095f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
263195f1fda4Szhangyi (F) 	 */
26321751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
263395f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
263495f1fda4Szhangyi (F) 
263595f1fda4Szhangyi (F) 		if (!ret)
263695f1fda4Szhangyi (F) 			quota_update = 1;
263795f1fda4Szhangyi (F) 		else
263895f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
263995f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
264095f1fda4Szhangyi (F) 	}
264195f1fda4Szhangyi (F) 
264295f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2643a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2644617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2645617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
264695f1fda4Szhangyi (F) 
264795f1fda4Szhangyi (F) 			if (!ret)
264895f1fda4Szhangyi (F) 				quota_update = 1;
264995f1fda4Szhangyi (F) 			else
2650b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2651b31e1552SEric Sandeen 					"Cannot turn on journaled "
265295f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2653ac27a0ecSDave Kleikamp 		}
2654ac27a0ecSDave Kleikamp 	}
2655ac27a0ecSDave Kleikamp #endif
2656ac27a0ecSDave Kleikamp 
2657ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2658ac27a0ecSDave Kleikamp 		struct inode *inode;
2659ac27a0ecSDave Kleikamp 
2660c65d5c6cSVegard Nossum 		/*
2661c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2662c65d5c6cSVegard Nossum 		 * so, skip the rest.
2663c65d5c6cSVegard Nossum 		 */
2664c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2665c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2666c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2667c65d5c6cSVegard Nossum 			break;
2668c65d5c6cSVegard Nossum 		}
2669c65d5c6cSVegard Nossum 
267097bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
267197bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2672ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2673ac27a0ecSDave Kleikamp 			break;
2674ac27a0ecSDave Kleikamp 		}
2675ac27a0ecSDave Kleikamp 
2676617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2677871a2931SChristoph Hellwig 		dquot_initialize(inode);
2678ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2679566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2680b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2681b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
268246e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2683e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2684ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
26855955102cSAl Viro 			inode_lock(inode);
268655f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
26872c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
26882c98eb5eSTheodore Ts'o 			if (ret)
26892c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
26905955102cSAl Viro 			inode_unlock(inode);
2691ac27a0ecSDave Kleikamp 			nr_truncates++;
2692ac27a0ecSDave Kleikamp 		} else {
2693566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2694b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2695b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
269646e665e9SHarvey Harrison 					__func__, inode->i_ino);
2697ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2698ac27a0ecSDave Kleikamp 				  inode->i_ino);
2699ac27a0ecSDave Kleikamp 			nr_orphans++;
2700ac27a0ecSDave Kleikamp 		}
2701ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2702ac27a0ecSDave Kleikamp 	}
2703ac27a0ecSDave Kleikamp 
2704ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2705ac27a0ecSDave Kleikamp 
2706ac27a0ecSDave Kleikamp 	if (nr_orphans)
2707b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2708b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2709ac27a0ecSDave Kleikamp 	if (nr_truncates)
2710b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2711b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2712ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
271395f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
271495f1fda4Szhangyi (F) 	if (quota_update) {
2715a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
271695f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2717287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2718ac27a0ecSDave Kleikamp 		}
271995f1fda4Szhangyi (F) 	}
2720ac27a0ecSDave Kleikamp #endif
27211751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2722ac27a0ecSDave Kleikamp }
27230b8e58a1SAndreas Dilger 
2724cd2291a4SEric Sandeen /*
2725cd2291a4SEric Sandeen  * Maximal extent format file size.
2726cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2727cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2728cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2729cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2730cd2291a4SEric Sandeen  *
2731f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2732f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2733f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2734f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2735f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2736f17722f9SLukas Czerner  *
2737cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2738cd2291a4SEric Sandeen  */
2739f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2740cd2291a4SEric Sandeen {
2741cd2291a4SEric Sandeen 	loff_t res;
2742cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2743cd2291a4SEric Sandeen 
274472deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
274572deb455SChristoph Hellwig 
274672deb455SChristoph Hellwig 	if (!has_huge_files) {
2747cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2748cd2291a4SEric Sandeen 
2749cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2750cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2751cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2752cd2291a4SEric Sandeen 	}
2753cd2291a4SEric Sandeen 
2754f17722f9SLukas Czerner 	/*
2755f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2756f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2757f17722f9SLukas Czerner 	 * size
2758f17722f9SLukas Czerner 	 */
2759f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2760cd2291a4SEric Sandeen 	res <<= blkbits;
2761cd2291a4SEric Sandeen 
2762cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2763cd2291a4SEric Sandeen 	if (res > upper_limit)
2764cd2291a4SEric Sandeen 		res = upper_limit;
2765cd2291a4SEric Sandeen 
2766cd2291a4SEric Sandeen 	return res;
2767cd2291a4SEric Sandeen }
2768ac27a0ecSDave Kleikamp 
2769ac27a0ecSDave Kleikamp /*
2770cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
27710fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
27720fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2773ac27a0ecSDave Kleikamp  */
2774f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2775ac27a0ecSDave Kleikamp {
2776617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
27770fc1b451SAneesh Kumar K.V 	int meta_blocks;
27780fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
27790b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
27800b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
27810b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
27820b8e58a1SAndreas Dilger 	 *
27830b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
27840b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
27850fc1b451SAneesh Kumar K.V 	 */
27860fc1b451SAneesh Kumar K.V 
278772deb455SChristoph Hellwig 	if (!has_huge_files) {
27880fc1b451SAneesh Kumar K.V 		/*
278972deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
279072deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
279172deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
27920fc1b451SAneesh Kumar K.V 		 */
27930fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
27940fc1b451SAneesh Kumar K.V 
27950fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
27960fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
27970fc1b451SAneesh Kumar K.V 
27980fc1b451SAneesh Kumar K.V 	} else {
27998180a562SAneesh Kumar K.V 		/*
28008180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
28018180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
28028180a562SAneesh Kumar K.V 		 * represent total number of blocks in
28038180a562SAneesh Kumar K.V 		 * file system block size
28048180a562SAneesh Kumar K.V 		 */
28050fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
28060fc1b451SAneesh Kumar K.V 
28070fc1b451SAneesh Kumar K.V 	}
28080fc1b451SAneesh Kumar K.V 
28090fc1b451SAneesh Kumar K.V 	/* indirect blocks */
28100fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
28110fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
28120fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
28130fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
28140fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
28150fc1b451SAneesh Kumar K.V 
28160fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
28170fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2818ac27a0ecSDave Kleikamp 
2819ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2820ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2821ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2822ac27a0ecSDave Kleikamp 	res <<= bits;
2823ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2824ac27a0ecSDave Kleikamp 		res = upper_limit;
28250fc1b451SAneesh Kumar K.V 
28260fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
28270fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
28280fc1b451SAneesh Kumar K.V 
2829ac27a0ecSDave Kleikamp 	return res;
2830ac27a0ecSDave Kleikamp }
2831ac27a0ecSDave Kleikamp 
2832617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
283370bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2834ac27a0ecSDave Kleikamp {
2835617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2836fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2837ac27a0ecSDave Kleikamp 	int has_super = 0;
2838ac27a0ecSDave Kleikamp 
2839ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2840ac27a0ecSDave Kleikamp 
2841e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
284270bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2843ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2844617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2845ac27a0ecSDave Kleikamp 		has_super = 1;
28460b8e58a1SAndreas Dilger 
2847bd63f6b0SDarrick J. Wong 	/*
2848bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2849bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2850bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2851bd63f6b0SDarrick J. Wong 	 * compensate.
2852bd63f6b0SDarrick J. Wong 	 */
2853bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
285449598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
2855bd63f6b0SDarrick J. Wong 		has_super++;
2856bd63f6b0SDarrick J. Wong 
2857617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2858ac27a0ecSDave Kleikamp }
2859ac27a0ecSDave Kleikamp 
2860c9de560dSAlex Tomas /**
2861c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2862c9de560dSAlex Tomas  * @sbi: In memory super block info
2863c9de560dSAlex Tomas  *
2864c9de560dSAlex Tomas  * If we have specified it via mount option, then
2865c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2866c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2867c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2868c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2869c9de560dSAlex Tomas  *
2870c9de560dSAlex Tomas  */
2871c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2872c9de560dSAlex Tomas {
2873c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2874c9de560dSAlex Tomas 	unsigned long stripe_width =
2875c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
28763eb08658SDan Ehrenberg 	int ret;
2877c9de560dSAlex Tomas 
2878c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
28793eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
28805469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
28813eb08658SDan Ehrenberg 		ret = stripe_width;
28825469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
28833eb08658SDan Ehrenberg 		ret = stride;
28843eb08658SDan Ehrenberg 	else
28853eb08658SDan Ehrenberg 		ret = 0;
2886c9de560dSAlex Tomas 
28873eb08658SDan Ehrenberg 	/*
28883eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
28893eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
28903eb08658SDan Ehrenberg 	 */
28913eb08658SDan Ehrenberg 	if (ret <= 1)
28923eb08658SDan Ehrenberg 		ret = 0;
2893c9de560dSAlex Tomas 
28943eb08658SDan Ehrenberg 	return ret;
2895c9de560dSAlex Tomas }
2896ac27a0ecSDave Kleikamp 
2897a13fb1a4SEric Sandeen /*
2898a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2899a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2900a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2901a13fb1a4SEric Sandeen  * 0 if it cannot be.
2902a13fb1a4SEric Sandeen  */
2903a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2904a13fb1a4SEric Sandeen {
2905e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2906a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2907a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2908a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2909a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2910a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2911a13fb1a4SEric Sandeen 		return 0;
2912a13fb1a4SEric Sandeen 	}
2913a13fb1a4SEric Sandeen 
2914c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
2915c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
2916c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
2917c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
2918c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
2919c83ad55eSGabriel Krisman Bertazi 		return 0;
2920c83ad55eSGabriel Krisman Bertazi 	}
2921c83ad55eSGabriel Krisman Bertazi #endif
2922c83ad55eSGabriel Krisman Bertazi 
2923a13fb1a4SEric Sandeen 	if (readonly)
2924a13fb1a4SEric Sandeen 		return 1;
2925a13fb1a4SEric Sandeen 
2926e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
29272cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
29281751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
29292cb5cc8bSDarrick J. Wong 		return 1;
29302cb5cc8bSDarrick J. Wong 	}
29312cb5cc8bSDarrick J. Wong 
2932a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2933e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2934a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2935a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2936a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2937a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2938a13fb1a4SEric Sandeen 		return 0;
2939a13fb1a4SEric Sandeen 	}
2940e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
2941bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2942bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2943bab08ab9STheodore Ts'o 			 "extents feature\n");
2944bab08ab9STheodore Ts'o 		return 0;
2945bab08ab9STheodore Ts'o 	}
29467c319d32SAditya Kali 
29477c319d32SAditya Kali #ifndef CONFIG_QUOTA
2948e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
29497c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
29507c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
29517c319d32SAditya Kali 			 "without CONFIG_QUOTA");
29527c319d32SAditya Kali 		return 0;
29537c319d32SAditya Kali 	}
2954689c958cSLi Xi 	if (ext4_has_feature_project(sb) && !readonly) {
2955689c958cSLi Xi 		ext4_msg(sb, KERN_ERR,
2956689c958cSLi Xi 			 "Filesystem with project quota feature cannot be mounted RDWR "
2957689c958cSLi Xi 			 "without CONFIG_QUOTA");
2958689c958cSLi Xi 		return 0;
2959689c958cSLi Xi 	}
29607c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2961a13fb1a4SEric Sandeen 	return 1;
2962a13fb1a4SEric Sandeen }
2963a13fb1a4SEric Sandeen 
296466e61a9eSTheodore Ts'o /*
296566e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
296666e61a9eSTheodore Ts'o  * on the file system
296766e61a9eSTheodore Ts'o  */
2968235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
296966e61a9eSTheodore Ts'o {
2970235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
2971235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
2972235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
297366e61a9eSTheodore Ts'o 
297466e61a9eSTheodore Ts'o 	if (es->s_error_count)
2975ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2976ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
297766e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
297866e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
29796a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
29806a0678a7SArnd Bergmann 		       sb->s_id,
29816a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
298266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
298366e61a9eSTheodore Ts'o 		       es->s_first_error_func,
298466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
298566e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
2986651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
298766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
298866e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
2989651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
299066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
2991651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
299266e61a9eSTheodore Ts'o 	}
299366e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
29946a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
29956a0678a7SArnd Bergmann 		       sb->s_id,
29966a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
299766e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
299866e61a9eSTheodore Ts'o 		       es->s_last_error_func,
299966e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
300066e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3001651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
300266e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
300366e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3004651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
300566e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3006651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
300766e61a9eSTheodore Ts'o 	}
300866e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
300966e61a9eSTheodore Ts'o }
301066e61a9eSTheodore Ts'o 
3011bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3012bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3013bfff6873SLukas Czerner {
3014bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3015bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3016bfff6873SLukas Czerner 	struct super_block *sb;
3017bfff6873SLukas Czerner 	unsigned long timeout = 0;
3018bfff6873SLukas Czerner 	int ret = 0;
3019bfff6873SLukas Czerner 
3020bfff6873SLukas Czerner 	sb = elr->lr_super;
3021bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3022bfff6873SLukas Czerner 
3023bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3024bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3025bfff6873SLukas Czerner 		if (!gdp) {
3026bfff6873SLukas Czerner 			ret = 1;
3027bfff6873SLukas Czerner 			break;
3028bfff6873SLukas Czerner 		}
3029bfff6873SLukas Czerner 
3030bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3031bfff6873SLukas Czerner 			break;
3032bfff6873SLukas Czerner 	}
3033bfff6873SLukas Czerner 
30347f511862STheodore Ts'o 	if (group >= ngroups)
3035bfff6873SLukas Czerner 		ret = 1;
3036bfff6873SLukas Czerner 
3037bfff6873SLukas Czerner 	if (!ret) {
3038bfff6873SLukas Czerner 		timeout = jiffies;
3039bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3040bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3041bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
304251ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
304351ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3044bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3045bfff6873SLukas Czerner 		}
3046bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3047bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3048bfff6873SLukas Czerner 	}
3049bfff6873SLukas Czerner 	return ret;
3050bfff6873SLukas Czerner }
3051bfff6873SLukas Czerner 
3052bfff6873SLukas Czerner /*
3053bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
30544ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3055bfff6873SLukas Czerner  */
3056bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3057bfff6873SLukas Czerner {
3058bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3059bfff6873SLukas Czerner 
3060bfff6873SLukas Czerner 	if (!elr)
3061bfff6873SLukas Czerner 		return;
3062bfff6873SLukas Czerner 
3063bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3064bfff6873SLukas Czerner 
3065bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3066bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3067bfff6873SLukas Czerner 	kfree(elr);
3068bfff6873SLukas Czerner }
3069bfff6873SLukas Czerner 
3070bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3071bfff6873SLukas Czerner {
30721bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
30731bb933fbSLukas Czerner 	if (!ext4_li_info) {
30741bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3075bfff6873SLukas Czerner 		return;
30761bb933fbSLukas Czerner 	}
3077bfff6873SLukas Czerner 
3078bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
30791bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3080bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
30811bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3082bfff6873SLukas Czerner }
3083bfff6873SLukas Czerner 
30848f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
30858f1f7453SEric Sandeen 
3086bfff6873SLukas Czerner /*
3087bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3088bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3089bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3090bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3091bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3092bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3093bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3094bfff6873SLukas Czerner  */
3095bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3096bfff6873SLukas Czerner {
3097bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3098bfff6873SLukas Czerner 	struct list_head *pos, *n;
3099bfff6873SLukas Czerner 	struct ext4_li_request *elr;
31004ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3101bfff6873SLukas Czerner 
3102bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3103bfff6873SLukas Czerner 
3104bfff6873SLukas Czerner cont_thread:
3105bfff6873SLukas Czerner 	while (true) {
3106bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3107bfff6873SLukas Czerner 
3108bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3109bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3110bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3111bfff6873SLukas Czerner 			goto exit_thread;
3112bfff6873SLukas Czerner 		}
3113bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3114e22834f0SDmitry Monakhov 			int err = 0;
3115e22834f0SDmitry Monakhov 			int progress = 0;
3116bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3117bfff6873SLukas Czerner 					 lr_request);
3118bfff6873SLukas Czerner 
3119e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3120e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3121e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3122e22834f0SDmitry Monakhov 				continue;
3123e22834f0SDmitry Monakhov 			}
3124e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3125e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3126e22834f0SDmitry Monakhov 					progress = 1;
3127e22834f0SDmitry Monakhov 					/*
3128e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3129e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3130e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3131e22834f0SDmitry Monakhov 					 */
3132e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3133e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3134e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3135e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3136e22834f0SDmitry Monakhov 					n = pos->next;
3137e22834f0SDmitry Monakhov 				}
3138e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3139e22834f0SDmitry Monakhov 			}
3140b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3141e22834f0SDmitry Monakhov 			if (err) {
3142bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3143bfff6873SLukas Czerner 				continue;
3144bfff6873SLukas Czerner 			}
3145e22834f0SDmitry Monakhov 			if (!progress) {
3146e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3147e22834f0SDmitry Monakhov 					(prandom_u32()
3148e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3149b2c78cd0STheodore Ts'o 			}
3150bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3151bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3152bfff6873SLukas Czerner 		}
3153bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3154bfff6873SLukas Czerner 
3155a0acae0eSTejun Heo 		try_to_freeze();
3156bfff6873SLukas Czerner 
31574ed5c033SLukas Czerner 		cur = jiffies;
31584ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3159f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3160bfff6873SLukas Czerner 			cond_resched();
3161bfff6873SLukas Czerner 			continue;
3162bfff6873SLukas Czerner 		}
3163bfff6873SLukas Czerner 
31644ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
31654ed5c033SLukas Czerner 
31668f1f7453SEric Sandeen 		if (kthread_should_stop()) {
31678f1f7453SEric Sandeen 			ext4_clear_request_list();
31688f1f7453SEric Sandeen 			goto exit_thread;
31698f1f7453SEric Sandeen 		}
3170bfff6873SLukas Czerner 	}
3171bfff6873SLukas Czerner 
3172bfff6873SLukas Czerner exit_thread:
3173bfff6873SLukas Czerner 	/*
3174bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3175bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3176bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3177bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3178bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3179bfff6873SLukas Czerner 	 * new one.
3180bfff6873SLukas Czerner 	 */
3181bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3182bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3183bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3184bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3185bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3186bfff6873SLukas Czerner 		goto cont_thread;
3187bfff6873SLukas Czerner 	}
3188bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3189bfff6873SLukas Czerner 	kfree(ext4_li_info);
3190bfff6873SLukas Czerner 	ext4_li_info = NULL;
3191bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3192bfff6873SLukas Czerner 
3193bfff6873SLukas Czerner 	return 0;
3194bfff6873SLukas Czerner }
3195bfff6873SLukas Czerner 
3196bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3197bfff6873SLukas Czerner {
3198bfff6873SLukas Czerner 	struct list_head *pos, *n;
3199bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3200bfff6873SLukas Czerner 
3201bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3202bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3203bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3204bfff6873SLukas Czerner 				 lr_request);
3205bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3206bfff6873SLukas Czerner 	}
3207bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3208bfff6873SLukas Czerner }
3209bfff6873SLukas Czerner 
3210bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3211bfff6873SLukas Czerner {
32128f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
32138f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
32148f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
32158f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3216bfff6873SLukas Czerner 		ext4_clear_request_list();
3217bfff6873SLukas Czerner 		kfree(ext4_li_info);
3218bfff6873SLukas Czerner 		ext4_li_info = NULL;
321992b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3220bfff6873SLukas Czerner 				 "initialization thread\n",
3221bfff6873SLukas Czerner 				 err);
3222bfff6873SLukas Czerner 		return err;
3223bfff6873SLukas Czerner 	}
3224bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3225bfff6873SLukas Czerner 	return 0;
3226bfff6873SLukas Czerner }
3227bfff6873SLukas Czerner 
3228bfff6873SLukas Czerner /*
3229bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3230bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3231bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3232bfff6873SLukas Czerner  * groups and return total number of groups.
3233bfff6873SLukas Czerner  */
3234bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3235bfff6873SLukas Czerner {
3236bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3237bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3238bfff6873SLukas Czerner 
32398844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
32408844618dSTheodore Ts'o 		return ngroups;
32418844618dSTheodore Ts'o 
3242bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3243bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3244bfff6873SLukas Czerner 		if (!gdp)
3245bfff6873SLukas Czerner 			continue;
3246bfff6873SLukas Czerner 
324750122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3248bfff6873SLukas Czerner 			break;
3249bfff6873SLukas Czerner 	}
3250bfff6873SLukas Czerner 
3251bfff6873SLukas Czerner 	return group;
3252bfff6873SLukas Czerner }
3253bfff6873SLukas Czerner 
3254bfff6873SLukas Czerner static int ext4_li_info_new(void)
3255bfff6873SLukas Czerner {
3256bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3257bfff6873SLukas Czerner 
3258bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3259bfff6873SLukas Czerner 	if (!eli)
3260bfff6873SLukas Czerner 		return -ENOMEM;
3261bfff6873SLukas Czerner 
3262bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3263bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3264bfff6873SLukas Czerner 
3265bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3266bfff6873SLukas Czerner 
3267bfff6873SLukas Czerner 	ext4_li_info = eli;
3268bfff6873SLukas Czerner 
3269bfff6873SLukas Czerner 	return 0;
3270bfff6873SLukas Czerner }
3271bfff6873SLukas Czerner 
3272bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3273bfff6873SLukas Czerner 					    ext4_group_t start)
3274bfff6873SLukas Czerner {
3275bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3276bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3277bfff6873SLukas Czerner 
3278bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3279bfff6873SLukas Czerner 	if (!elr)
3280bfff6873SLukas Czerner 		return NULL;
3281bfff6873SLukas Czerner 
3282bfff6873SLukas Czerner 	elr->lr_super = sb;
3283bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3284bfff6873SLukas Czerner 	elr->lr_next_group = start;
3285bfff6873SLukas Czerner 
3286bfff6873SLukas Czerner 	/*
3287bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3288bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3289bfff6873SLukas Czerner 	 * better.
3290bfff6873SLukas Czerner 	 */
3291dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3292dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3293bfff6873SLukas Czerner 	return elr;
3294bfff6873SLukas Czerner }
3295bfff6873SLukas Czerner 
32967f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3297bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3298bfff6873SLukas Czerner {
3299bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
33007f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
330149598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
33026c5a6cb9SAndrew Morton 	int ret = 0;
3303bfff6873SLukas Czerner 
33047f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
330551ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
330651ce6511SLukas Czerner 		/*
330751ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
330851ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
330951ce6511SLukas Czerner 		 */
331051ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
33117f511862STheodore Ts'o 		goto out;
331251ce6511SLukas Czerner 	}
3313bfff6873SLukas Czerner 
3314bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
331555ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
33167f511862STheodore Ts'o 		goto out;
3317bfff6873SLukas Czerner 
3318bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
33197f511862STheodore Ts'o 	if (!elr) {
33207f511862STheodore Ts'o 		ret = -ENOMEM;
33217f511862STheodore Ts'o 		goto out;
33227f511862STheodore Ts'o 	}
3323bfff6873SLukas Czerner 
3324bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3325bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3326bfff6873SLukas Czerner 		if (ret)
3327bfff6873SLukas Czerner 			goto out;
3328bfff6873SLukas Czerner 	}
3329bfff6873SLukas Czerner 
3330bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3331bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3332bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3333bfff6873SLukas Czerner 
3334bfff6873SLukas Czerner 	sbi->s_li_request = elr;
333546e4690bSTao Ma 	/*
333646e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
333746e4690bSTao Ma 	 * the request_list and the removal and free of it is
333846e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
333946e4690bSTao Ma 	 */
334046e4690bSTao Ma 	elr = NULL;
3341bfff6873SLukas Czerner 
3342bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3343bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3344bfff6873SLukas Czerner 		if (ret)
3345bfff6873SLukas Czerner 			goto out;
3346bfff6873SLukas Czerner 	}
3347bfff6873SLukas Czerner out:
3348bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3349beed5ecbSNicolas Kaiser 	if (ret)
3350bfff6873SLukas Czerner 		kfree(elr);
3351bfff6873SLukas Czerner 	return ret;
3352bfff6873SLukas Czerner }
3353bfff6873SLukas Czerner 
3354bfff6873SLukas Czerner /*
3355bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3356bfff6873SLukas Czerner  * module unload.
3357bfff6873SLukas Czerner  */
3358bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3359bfff6873SLukas Czerner {
3360bfff6873SLukas Czerner 	/*
3361bfff6873SLukas Czerner 	 * If thread exited earlier
3362bfff6873SLukas Czerner 	 * there's nothing to be done.
3363bfff6873SLukas Czerner 	 */
33648f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3365bfff6873SLukas Czerner 		return;
3366bfff6873SLukas Czerner 
33678f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3368bfff6873SLukas Czerner }
3369bfff6873SLukas Czerner 
337025ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
337125ed6e8aSDarrick J. Wong {
337225ed6e8aSDarrick J. Wong 	int ret = 1;
337325ed6e8aSDarrick J. Wong 	int compat, incompat;
337425ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
337525ed6e8aSDarrick J. Wong 
33769aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3377db9ee220SDarrick J. Wong 		/* journal checksum v3 */
337825ed6e8aSDarrick J. Wong 		compat = 0;
3379db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
338025ed6e8aSDarrick J. Wong 	} else {
338125ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
338225ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
338325ed6e8aSDarrick J. Wong 		incompat = 0;
338425ed6e8aSDarrick J. Wong 	}
338525ed6e8aSDarrick J. Wong 
3386feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3387feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3388feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3389feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
339025ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
339125ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
339225ed6e8aSDarrick J. Wong 				compat, 0,
339325ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
339425ed6e8aSDarrick J. Wong 				incompat);
339525ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
339625ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
339725ed6e8aSDarrick J. Wong 				compat, 0,
339825ed6e8aSDarrick J. Wong 				incompat);
339925ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
340025ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
340125ed6e8aSDarrick J. Wong 	} else {
3402feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3403feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
340425ed6e8aSDarrick J. Wong 	}
340525ed6e8aSDarrick J. Wong 
340625ed6e8aSDarrick J. Wong 	return ret;
340725ed6e8aSDarrick J. Wong }
340825ed6e8aSDarrick J. Wong 
3409952fc18eSTheodore Ts'o /*
3410952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3411952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3412952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3413952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3414952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3415952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3416952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3417952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3418952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3419952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3420952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3421952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3422952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3423952fc18eSTheodore Ts'o  */
3424952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3425952fc18eSTheodore Ts'o 			  char *buf)
3426952fc18eSTheodore Ts'o {
3427952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3428952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3429952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3430952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3431952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3432952fc18eSTheodore Ts'o 
3433e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
34340548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
34350548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
34360548bbb8STheodore Ts'o 
3437952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3438952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3439952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3440952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3441952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3442952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3443952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3444952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3445952fc18eSTheodore Ts'o 			count++;
3446952fc18eSTheodore Ts'o 		}
3447952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3448952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3449952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3450952fc18eSTheodore Ts'o 			count++;
3451952fc18eSTheodore Ts'o 		}
3452952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3453952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3454952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3455952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3456952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3457952fc18eSTheodore Ts'o 				count++;
3458952fc18eSTheodore Ts'o 			}
3459952fc18eSTheodore Ts'o 		if (i != grp)
3460952fc18eSTheodore Ts'o 			continue;
3461952fc18eSTheodore Ts'o 		s = 0;
3462952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3463952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3464952fc18eSTheodore Ts'o 			count++;
3465952fc18eSTheodore Ts'o 		}
3466c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3467c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3468c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3469c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3470c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3471952fc18eSTheodore Ts'o 		}
3472c48ae41bSTheodore Ts'o 		count += j;
3473c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3474c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3475952fc18eSTheodore Ts'o 	}
3476952fc18eSTheodore Ts'o 	if (!count)
3477952fc18eSTheodore Ts'o 		return 0;
3478952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3479952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3480952fc18eSTheodore Ts'o }
3481952fc18eSTheodore Ts'o 
3482952fc18eSTheodore Ts'o /*
3483952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3484952fc18eSTheodore Ts'o  */
3485952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3486952fc18eSTheodore Ts'o {
3487952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3488952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
34893c816dedSEric Whitney 	struct inode *j_inode;
34903c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3491952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3492952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
34934fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3494952fc18eSTheodore Ts'o 
3495952fc18eSTheodore Ts'o 	if (!buf)
3496952fc18eSTheodore Ts'o 		return -ENOMEM;
3497952fc18eSTheodore Ts'o 
3498952fc18eSTheodore Ts'o 	/*
3499952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3500952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3501952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3502952fc18eSTheodore Ts'o 	 */
3503952fc18eSTheodore Ts'o 
3504952fc18eSTheodore Ts'o 	/*
3505952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3506952fc18eSTheodore Ts'o 	 */
3507952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3508952fc18eSTheodore Ts'o 
3509952fc18eSTheodore Ts'o 	/*
3510952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3511952fc18eSTheodore Ts'o 	 */
3512952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3513952fc18eSTheodore Ts'o 		int blks;
3514952fc18eSTheodore Ts'o 
3515952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3516952fc18eSTheodore Ts'o 		overhead += blks;
3517952fc18eSTheodore Ts'o 		if (blks)
3518952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3519952fc18eSTheodore Ts'o 		cond_resched();
3520952fc18eSTheodore Ts'o 	}
35213c816dedSEric Whitney 
35223c816dedSEric Whitney 	/*
35233c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
35243c816dedSEric Whitney 	 * loaded or not
35253c816dedSEric Whitney 	 */
3526b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3527810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
35283c816dedSEric Whitney 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
35293c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
35303c816dedSEric Whitney 		if (j_inode) {
35313c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
35323c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
35333c816dedSEric Whitney 			iput(j_inode);
35343c816dedSEric Whitney 		} else {
35353c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
35363c816dedSEric Whitney 		}
35373c816dedSEric Whitney 	}
3538952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3539952fc18eSTheodore Ts'o 	smp_wmb();
3540952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3541952fc18eSTheodore Ts'o 	return 0;
3542952fc18eSTheodore Ts'o }
3543952fc18eSTheodore Ts'o 
35447bc04c5cSBarret Rhoden static void ext4_clamp_want_extra_isize(struct super_block *sb)
35457bc04c5cSBarret Rhoden {
35467bc04c5cSBarret Rhoden 	struct ext4_sb_info *sbi = EXT4_SB(sb);
35477bc04c5cSBarret Rhoden 	struct ext4_super_block *es = sbi->s_es;
35487bc04c5cSBarret Rhoden 
35497bc04c5cSBarret Rhoden 	/* determine the minimum size of new large inodes, if present */
35507bc04c5cSBarret Rhoden 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE &&
35517bc04c5cSBarret Rhoden 	    sbi->s_want_extra_isize == 0) {
35527bc04c5cSBarret Rhoden 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
35537bc04c5cSBarret Rhoden 						     EXT4_GOOD_OLD_INODE_SIZE;
35547bc04c5cSBarret Rhoden 		if (ext4_has_feature_extra_isize(sb)) {
35557bc04c5cSBarret Rhoden 			if (sbi->s_want_extra_isize <
35567bc04c5cSBarret Rhoden 			    le16_to_cpu(es->s_want_extra_isize))
35577bc04c5cSBarret Rhoden 				sbi->s_want_extra_isize =
35587bc04c5cSBarret Rhoden 					le16_to_cpu(es->s_want_extra_isize);
35597bc04c5cSBarret Rhoden 			if (sbi->s_want_extra_isize <
35607bc04c5cSBarret Rhoden 			    le16_to_cpu(es->s_min_extra_isize))
35617bc04c5cSBarret Rhoden 				sbi->s_want_extra_isize =
35627bc04c5cSBarret Rhoden 					le16_to_cpu(es->s_min_extra_isize);
35637bc04c5cSBarret Rhoden 		}
35647bc04c5cSBarret Rhoden 	}
35657bc04c5cSBarret Rhoden 	/* Check if enough inode space is available */
35667bc04c5cSBarret Rhoden 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
35677bc04c5cSBarret Rhoden 							sbi->s_inode_size) {
35687bc04c5cSBarret Rhoden 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
35697bc04c5cSBarret Rhoden 						       EXT4_GOOD_OLD_INODE_SIZE;
35707bc04c5cSBarret Rhoden 		ext4_msg(sb, KERN_INFO,
35717bc04c5cSBarret Rhoden 			 "required extra inode space not available");
35727bc04c5cSBarret Rhoden 	}
35737bc04c5cSBarret Rhoden }
35747bc04c5cSBarret Rhoden 
3575b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
357627dd4385SLukas Czerner {
357727dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3578b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
357927dd4385SLukas Czerner 
358027dd4385SLukas Czerner 	/*
358130fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
358230fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
358330fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
358430fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
358530fac0f7SJan Kara 	 */
3586e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3587b5799018STheodore Ts'o 		return;
358830fac0f7SJan Kara 	/*
358927dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
359027dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
359127dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3592556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
359327dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
359427dd4385SLukas Czerner 	 * very rare.
359527dd4385SLukas Czerner 	 */
3596b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3597b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
359827dd4385SLukas Czerner 
359927dd4385SLukas Czerner 	do_div(resv_clusters, 50);
360027dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
360127dd4385SLukas Czerner 
3602b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
360327dd4385SLukas Czerner }
360427dd4385SLukas Czerner 
3605617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3606ac27a0ecSDave Kleikamp {
36075e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3608d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3609ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3610617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
36115aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3612617ba13bSMingming Cao 	ext4_fsblk_t block;
3613617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
361470bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3615ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3616ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3617ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3618ac27a0ecSDave Kleikamp 	struct inode *root;
36190390131bSFrank Mayhar 	const char *descr;
3620dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3621281b5995STheodore Ts'o 	int blocksize, clustersize;
36224ec11028STheodore Ts'o 	unsigned int db_count;
36234ec11028STheodore Ts'o 	unsigned int i;
3624281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3625bd81d8eeSLaurent Vivier 	__u64 blocks_count;
362607aa2ea1SLukas Czerner 	int err = 0;
3627b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3628bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3629ac27a0ecSDave Kleikamp 
36305aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
36315aee0f8aSTheodore Ts'o 		goto out_free_base;
3632705895b6SPekka Enberg 
3633aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3634705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3635705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
36365aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
36375aee0f8aSTheodore Ts'o 		goto out_free_base;
36385aee0f8aSTheodore Ts'o 
3639ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
36402c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3641240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3642d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3643f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3644f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3645dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3646ac27a0ecSDave Kleikamp 
36479f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3648ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
36499f6200bbSTheodore Ts'o 
365007aa2ea1SLukas Czerner 	/* -EINVAL is default */
3651dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3652617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3653ac27a0ecSDave Kleikamp 	if (!blocksize) {
3654b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3655ac27a0ecSDave Kleikamp 		goto out_fail;
3656ac27a0ecSDave Kleikamp 	}
3657ac27a0ecSDave Kleikamp 
3658ac27a0ecSDave Kleikamp 	/*
3659617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3660ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3661ac27a0ecSDave Kleikamp 	 */
3662617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
366370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
366470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3665ac27a0ecSDave Kleikamp 	} else {
366670bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3667ac27a0ecSDave Kleikamp 	}
3668ac27a0ecSDave Kleikamp 
3669a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3670b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3671ac27a0ecSDave Kleikamp 		goto out_fail;
3672ac27a0ecSDave Kleikamp 	}
3673ac27a0ecSDave Kleikamp 	/*
3674ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3675617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3676ac27a0ecSDave Kleikamp 	 */
36772716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3678ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3679ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3680617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3681617ba13bSMingming Cao 		goto cantfind_ext4;
3682afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3683ac27a0ecSDave Kleikamp 
3684feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3685e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3686e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3687363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3688feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3689feb0ab32SDarrick J. Wong 
3690d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3691d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3692d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3693d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3694d25425f8SDarrick J. Wong 		silent = 1;
3695d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3696d25425f8SDarrick J. Wong 	}
3697d25425f8SDarrick J. Wong 
36980441984aSDarrick J. Wong 	/* Load the checksum driver */
36990441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
37000441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
37010441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
37020441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
37030441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
37040441984aSDarrick J. Wong 		goto failed_mount;
37050441984aSDarrick J. Wong 	}
37060441984aSDarrick J. Wong 
3707a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3708a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3709a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3710a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3711a9c47317SDarrick J. Wong 		silent = 1;
37126a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3713a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3714a9c47317SDarrick J. Wong 	}
3715a9c47317SDarrick J. Wong 
3716a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3717e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
37188c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3719dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3720a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3721a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3722a9c47317SDarrick J. Wong 
3723ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3724ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3725fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3726617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3727fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
372887f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3729fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3730617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3731fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3732ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3733fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
373403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3735fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
37362e7842b8SHugh Dickins #endif
373798c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
373898c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
373998c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
374098c1a759SDarrick J. Wong 
3741617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3742fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3743617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3744fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3745617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3746fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3747ac27a0ecSDave Kleikamp 
3748617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3749fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3750bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3751fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3752bb4f397aSAneesh Kumar K.V 	else
3753fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
375445f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3755fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
37568b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3757fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3758ac27a0ecSDave Kleikamp 
375908cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
376008cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
376130773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
376230773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
376330773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3764ac27a0ecSDave Kleikamp 
37658b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3766fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3767ac27a0ecSDave Kleikamp 
37681e2462f9SMingming Cao 	/*
3769dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3770dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3771dd919b98SAneesh Kumar K.V 	 */
3772bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
37738b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3774fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3775dd919b98SAneesh Kumar K.V 
377651ce6511SLukas Czerner 	/*
377751ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
377851ce6511SLukas Czerner 	 * no mount option specified.
377951ce6511SLukas Czerner 	 */
378051ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
378151ce6511SLukas Czerner 
37825aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
37835aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
37845aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
37855aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
37865aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
37875aee0f8aSTheodore Ts'o 			goto failed_mount;
37885aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
37895aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
37908b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
37918b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
37925aee0f8aSTheodore Ts'o 				 s_mount_opts);
37935aee0f8aSTheodore Ts'o 		}
37945aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
37958b67f04aSTheodore Ts'o 	}
37965a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3797b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3798661aa520SEric Sandeen 			   &journal_ioprio, 0))
3799ac27a0ecSDave Kleikamp 		goto failed_mount;
3800ac27a0ecSDave Kleikamp 
3801c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3802c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
3803c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
3804c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
3805c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
3806c83ad55eSGabriel Krisman Bertazi 
3807c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
3808c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3809c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
3810c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3811c83ad55eSGabriel Krisman Bertazi 		}
3812c83ad55eSGabriel Krisman Bertazi 
3813c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
3814c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
3815c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3816c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
3817c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3818c83ad55eSGabriel Krisman Bertazi 		}
3819c83ad55eSGabriel Krisman Bertazi 
3820c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
3821c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
3822c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3823c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
3824c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
3825c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
3826c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
3827c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3828c83ad55eSGabriel Krisman Bertazi 		}
3829c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
3830c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
3831c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
3832c83ad55eSGabriel Krisman Bertazi 
3833c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
3834c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
3835c83ad55eSGabriel Krisman Bertazi 	}
3836c83ad55eSGabriel Krisman Bertazi #endif
3837c83ad55eSGabriel Krisman Bertazi 
383856889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
383956889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
384056889787STheodore Ts'o 			    "with data=journal disables delayed "
384156889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
384256889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
384356889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
384456889787STheodore Ts'o 				 "both data=journal and delalloc");
384556889787STheodore Ts'o 			goto failed_mount;
384656889787STheodore Ts'o 		}
384756889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
384856889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
38496ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
385056889787STheodore Ts'o 			goto failed_mount;
385156889787STheodore Ts'o 		}
3852923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3853923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3854923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3855923ae0ffSRoss Zwisler 			goto failed_mount;
3856923ae0ffSRoss Zwisler 		}
385773b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
385873b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
385973b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
386073b92a2aSSergey Karamov 				 "instead of data journaling mode");
386173b92a2aSSergey Karamov 		}
386256889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
386356889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3864001e4a87STejun Heo 	} else {
3865001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
386656889787STheodore Ts'o 	}
386756889787STheodore Ts'o 
38681751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
38691751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
3870ac27a0ecSDave Kleikamp 
3871617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3872e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3873e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3874e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3875b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3876b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3877b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3878469108ffSTheodore Tso 
3879ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3880ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3881e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3882ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3883ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3884ed3654ebSTheodore Ts'o 			goto failed_mount;
3885ed3654ebSTheodore Ts'o 		}
3886dec214d0STahsin Erdogan 
3887dec214d0STahsin Erdogan 		/*
3888dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
3889dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
3890dec214d0STahsin Erdogan 		 */
3891dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
3892dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
3893dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
3894dec214d0STahsin Erdogan 			goto failed_mount;
3895dec214d0STahsin Erdogan 		}
3896ed3654ebSTheodore Ts'o 	}
3897ed3654ebSTheodore Ts'o 
38982035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
38992035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
39002035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
39012035e776STheodore Ts'o 				 "using the ext4 subsystem");
39022035e776STheodore Ts'o 		else {
39030d9366d6SEric Sandeen 			/*
39040d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
39050d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
39060d9366d6SEric Sandeen 			 */
39070d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
39080d9366d6SEric Sandeen 				goto failed_mount;
39092035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
39102035e776STheodore Ts'o 				 "to feature incompatibilities");
39112035e776STheodore Ts'o 			goto failed_mount;
39122035e776STheodore Ts'o 		}
39132035e776STheodore Ts'o 	}
39142035e776STheodore Ts'o 
39152035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
39162035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
39172035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
39182035e776STheodore Ts'o 				 "using the ext4 subsystem");
39192035e776STheodore Ts'o 		else {
39200d9366d6SEric Sandeen 			/*
39210d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
39220d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
39230d9366d6SEric Sandeen 			 */
39240d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
39250d9366d6SEric Sandeen 				goto failed_mount;
39262035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
39272035e776STheodore Ts'o 				 "to feature incompatibilities");
39282035e776STheodore Ts'o 			goto failed_mount;
39292035e776STheodore Ts'o 		}
39302035e776STheodore Ts'o 	}
39312035e776STheodore Ts'o 
3932469108ffSTheodore Tso 	/*
3933ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3934ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3935ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3936ac27a0ecSDave Kleikamp 	 */
3937bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
3938ac27a0ecSDave Kleikamp 		goto failed_mount;
3939a13fb1a4SEric Sandeen 
3940261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3941617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3942617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3943b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
39448cdf3372STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
39458cdf3372STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
39468cdf3372STheodore Ts'o 		goto failed_mount;
39478cdf3372STheodore Ts'o 	}
39488cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
39498cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
39508cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39518cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
39528cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
3953ac27a0ecSDave Kleikamp 		goto failed_mount;
3954ac27a0ecSDave Kleikamp 	}
3955bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
3956bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
3957bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3958bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
3959bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
3960bfe0a5f4STheodore Ts'o 		goto failed_mount;
3961bfe0a5f4STheodore Ts'o 	}
3962ac27a0ecSDave Kleikamp 
39635b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
39645b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39655b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
39665b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
39675b9554dcSTheodore Ts'o 		goto failed_mount;
39685b9554dcSTheodore Ts'o 	}
39695b9554dcSTheodore Ts'o 
3970923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
3971559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
3972559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
3973559db4c6SRoss Zwisler 					" that may contain inline data");
3974361d24d4SEric Sandeen 			goto failed_mount;
3975559db4c6SRoss Zwisler 		}
397680660f20SDave Jiang 		if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
397724f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
3978361d24d4SEric Sandeen 				"DAX unsupported by block device.");
3979361d24d4SEric Sandeen 			goto failed_mount;
398024f3478dSDan Williams 		}
3981923ae0ffSRoss Zwisler 	}
3982923ae0ffSRoss Zwisler 
3983e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
39846ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
39856ddb2447STheodore Ts'o 			 es->s_encryption_level);
39866ddb2447STheodore Ts'o 		goto failed_mount;
39876ddb2447STheodore Ts'o 	}
39886ddb2447STheodore Ts'o 
3989ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
3990ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
3991ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
3992b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3993ce40733cSAneesh Kumar K.V 					blocksize);
3994ac27a0ecSDave Kleikamp 			goto failed_mount;
3995ac27a0ecSDave Kleikamp 		}
3996ac27a0ecSDave Kleikamp 
3997ac27a0ecSDave Kleikamp 		brelse(bh);
399870bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
399970bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4000a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4001ac27a0ecSDave Kleikamp 		if (!bh) {
4002b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4003b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4004ac27a0ecSDave Kleikamp 			goto failed_mount;
4005ac27a0ecSDave Kleikamp 		}
40062716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4007ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4008617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4009b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4010b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4011ac27a0ecSDave Kleikamp 			goto failed_mount;
4012ac27a0ecSDave Kleikamp 		}
4013ac27a0ecSDave Kleikamp 	}
4014ac27a0ecSDave Kleikamp 
4015e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4016f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4017f287a1a5STheodore Ts'o 						      has_huge_files);
4018f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4019ac27a0ecSDave Kleikamp 
4020617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4021617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4022617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4023ac27a0ecSDave Kleikamp 	} else {
4024ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4025ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4026c37e9e01STheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4027c37e9e01STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4028c37e9e01STheodore Ts'o 				 sbi->s_first_ino);
4029c37e9e01STheodore Ts'o 			goto failed_mount;
4030c37e9e01STheodore Ts'o 		}
4031617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
40321330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
4033ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
4034b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4035b31e1552SEric Sandeen 			       "unsupported inode size: %d",
4036ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
4037ac27a0ecSDave Kleikamp 			goto failed_mount;
4038ac27a0ecSDave Kleikamp 		}
40394881c497SDeepa Dinamani 		/*
40404881c497SDeepa Dinamani 		 * i_atime_extra is the last extra field available for [acm]times in
40414881c497SDeepa Dinamani 		 * struct ext4_inode. Checking for that field should suffice to ensure
40424881c497SDeepa Dinamani 		 * we have extra space for all three.
40434881c497SDeepa Dinamani 		 */
40444881c497SDeepa Dinamani 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
40454881c497SDeepa Dinamani 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
40464881c497SDeepa Dinamani 			sb->s_time_gran = 1;
40474881c497SDeepa Dinamani 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
40484881c497SDeepa Dinamani 		} else {
40494881c497SDeepa Dinamani 			sb->s_time_gran = NSEC_PER_SEC;
40504881c497SDeepa Dinamani 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
40514881c497SDeepa Dinamani 		}
40524881c497SDeepa Dinamani 
40534881c497SDeepa Dinamani 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
4054ac27a0ecSDave Kleikamp 	}
40550b8e58a1SAndreas Dilger 
40560d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4057e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
40588fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
40590d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4060d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4061b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4062b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
40630d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
40640d1ee42fSAlexandre Ratchov 			goto failed_mount;
40650d1ee42fSAlexandre Ratchov 		}
40660d1ee42fSAlexandre Ratchov 	} else
40670d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
40680b8e58a1SAndreas Dilger 
4069ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4070ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
40710b8e58a1SAndreas Dilger 
4072617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4073ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4074617ba13bSMingming Cao 		goto cantfind_ext4;
4075cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4076cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4077cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4078cd6bb35bSTheodore Ts'o 			 sbi->s_blocks_per_group);
4079cd6bb35bSTheodore Ts'o 		goto failed_mount;
4080cd6bb35bSTheodore Ts'o 	}
4081ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4082ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
40830d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4084ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4085ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4086e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4087e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
40880b8e58a1SAndreas Dilger 
4089ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4090ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4091ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4092e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4093f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4094f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4095f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4096f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4097f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4098bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
409923301410STheodore Ts'o 				es->s_flags |=
410023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4101f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4102f99b2589STheodore Ts'o #else
4103bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
410423301410STheodore Ts'o 				es->s_flags |=
410523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4106f99b2589STheodore Ts'o #endif
4107f99b2589STheodore Ts'o 		}
410823301410STheodore Ts'o 	}
4109ac27a0ecSDave Kleikamp 
4110281b5995STheodore Ts'o 	/* Handle clustersize */
4111281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4112e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
4113281b5995STheodore Ts'o 	if (has_bigalloc) {
4114281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4115281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4116281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4117281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4118281b5995STheodore Ts'o 			goto failed_mount;
4119281b5995STheodore Ts'o 		}
4120281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4121281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4122281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4123281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4124281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4125281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4126281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4127281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4128281b5995STheodore Ts'o 			goto failed_mount;
4129281b5995STheodore Ts'o 		}
4130281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4131281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4132281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4133281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4134281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4135281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4136281b5995STheodore Ts'o 			goto failed_mount;
4137281b5995STheodore Ts'o 		}
4138281b5995STheodore Ts'o 	} else {
4139281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4140bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4141bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4142bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4143bfe0a5f4STheodore Ts'o 			goto failed_mount;
4144281b5995STheodore Ts'o 		}
4145ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4146b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4147b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4148ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4149ac27a0ecSDave Kleikamp 			goto failed_mount;
4150ac27a0ecSDave Kleikamp 		}
4151281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4152281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4153281b5995STheodore Ts'o 	}
4154281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4155281b5995STheodore Ts'o 
4156960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4157960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4158960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4159960fd856STheodore Ts'o 
4160bf43d84bSEric Sandeen 	/*
4161bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4162bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4163bf43d84bSEric Sandeen 	 */
41645a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
416530ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
41665a9ae68aSDarrick J. Wong 	if (err) {
4167b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4168bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4169ac27a0ecSDave Kleikamp 		goto failed_mount;
4170ac27a0ecSDave Kleikamp 	}
4171ac27a0ecSDave Kleikamp 
4172617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4173617ba13bSMingming Cao 		goto cantfind_ext4;
4174e7c95593SEric Sandeen 
41750f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
41760f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
41770f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4178b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4179b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
41800f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
41810f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
41820f2ddca6SFrom: Thiemo Nagel 	}
41830f2ddca6SFrom: Thiemo Nagel 
41844ec11028STheodore Ts'o 	/*
41854ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
41864ec11028STheodore Ts'o 	 * of the filesystem.
41874ec11028STheodore Ts'o 	 */
41884ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4189b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4190b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4191e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
41924ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4193e7c95593SEric Sandeen 		goto failed_mount;
4194e7c95593SEric Sandeen 	}
4195bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4196bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4197bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4198bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4199bfe0a5f4STheodore Ts'o 		goto failed_mount;
4200bfe0a5f4STheodore Ts'o 	}
4201bfe0a5f4STheodore Ts'o 
4202bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4203bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4204bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4205bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
42064ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4207b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
42084ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4209b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
42104ec11028STheodore Ts'o 		       ext4_blocks_count(es),
42114ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
42124ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
42134ec11028STheodore Ts'o 		goto failed_mount;
42144ec11028STheodore Ts'o 	}
4215bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4216fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4217fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
42189e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
42199e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
42209e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
42219e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
42229e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
42239e463084STheodore Ts'o 		ret = -EINVAL;
42249e463084STheodore Ts'o 		goto failed_mount;
42259e463084STheodore Ts'o 	}
4226617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4227617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
42283a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
42292ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
42303a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
42313a4b77cdSEryu Guan 				 "first meta block group too large: %u "
42323a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
42333a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
42343a4b77cdSEryu Guan 			goto failed_mount;
42353a4b77cdSEryu Guan 		}
42363a4b77cdSEryu Guan 	}
4237344476e1SKees Cook 	sbi->s_group_desc = kvmalloc_array(db_count,
4238f18a5f21STheodore Ts'o 					   sizeof(struct buffer_head *),
4239ac27a0ecSDave Kleikamp 					   GFP_KERNEL);
4240ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4241b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
42422cde417dSTheodore Ts'o 		ret = -ENOMEM;
4243ac27a0ecSDave Kleikamp 		goto failed_mount;
4244ac27a0ecSDave Kleikamp 	}
4245ac27a0ecSDave Kleikamp 
4246705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4247ac27a0ecSDave Kleikamp 
424885c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
424985c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
425085c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
425185c8f176SAndrew Perepechko 		sb_breadahead(sb, block);
425285c8f176SAndrew Perepechko 	}
425385c8f176SAndrew Perepechko 
4254ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
425570bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
4256a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
4257ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
4258b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4259b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4260ac27a0ecSDave Kleikamp 			db_count = i;
4261ac27a0ecSDave Kleikamp 			goto failed_mount2;
4262ac27a0ecSDave Kleikamp 		}
4263ac27a0ecSDave Kleikamp 	}
426444de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4265829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4266b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
42676a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4268f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4269ac27a0ecSDave Kleikamp 	}
4270772cb7c8SJose R. Santos 
4271235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
427204496411STao Ma 
4273a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4274eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4275ce7e010aSTheodore Ts'o 		goto failed_mount3;
4276ce7e010aSTheodore Ts'o 
4277c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
427867a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4279c9de560dSAlex Tomas 
4280f9ae9cf5STheodore Ts'o 	/*
4281f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4282f9ae9cf5STheodore Ts'o 	 */
4283f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4284617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4285617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4286643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4287a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4288ffcc4182SEric Biggers #endif
4289c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4290c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4291c93d8f88SEric Biggers #endif
4292ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4293617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4294e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
42951fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4296262b4662SJan Kara 	else
4297262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4298689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4299ac27a0ecSDave Kleikamp #endif
430085787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4301f2fa2ffcSAneesh Kumar K.V 
4302ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
43033b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4304ac27a0ecSDave Kleikamp 
4305ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4306ac27a0ecSDave Kleikamp 
4307ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4308e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4309ac27a0ecSDave Kleikamp 
4310bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4311c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
431250460fe8SDarrick J. Wong 			goto failed_mount3a;
4313c5e06d10SJohann Lombardi 
4314ac27a0ecSDave Kleikamp 	/*
4315ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4316ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4317ac27a0ecSDave Kleikamp 	 */
4318e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
43194753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
43204753d8a2STheodore Ts'o 		if (err)
432150460fe8SDarrick J. Wong 			goto failed_mount3a;
4322bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4323e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4324b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4325b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4326744692dcSJiaying Zhang 		goto failed_mount_wq;
4327ac27a0ecSDave Kleikamp 	} else {
43281e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
43291e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
43301e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43311e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
43321e381f60SDmitry Monakhov 			goto failed_mount_wq;
43331e381f60SDmitry Monakhov 		}
43341e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
43351e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43361e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
43371e381f60SDmitry Monakhov 			goto failed_mount_wq;
43381e381f60SDmitry Monakhov 		}
43391e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
43401e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43411e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
43421e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
43431e381f60SDmitry Monakhov 			goto failed_mount_wq;
43441e381f60SDmitry Monakhov 		}
43451e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
43461e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
43471e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43481e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
43491e381f60SDmitry Monakhov 			goto failed_mount_wq;
43501e381f60SDmitry Monakhov 		}
435150b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
43521e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4353fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
43540390131bSFrank Mayhar 		sbi->s_journal = NULL;
43550390131bSFrank Mayhar 		needs_recovery = 0;
43560390131bSFrank Mayhar 		goto no_journal;
4357ac27a0ecSDave Kleikamp 	}
4358ac27a0ecSDave Kleikamp 
4359e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4360eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4361eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4362b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4363744692dcSJiaying Zhang 		goto failed_mount_wq;
4364eb40a09cSJose R. Santos 	}
4365eb40a09cSJose R. Santos 
436625ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
436725ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
436825ed6e8aSDarrick J. Wong 			 "feature set");
436925ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4370d4da6c9cSLinus Torvalds 	}
4371818d276cSGirish Shilamkar 
4372ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4373ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4374ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4375ac27a0ecSDave Kleikamp 	case 0:
4376ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
437763f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
437863f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
437963f57933SAndrew Morton 		 */
4380dab291afSMingming Cao 		if (jbd2_journal_check_available_features
438127f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4382fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
438327f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
438427f394a7STyson Nottingham 		} else {
4385fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
438627f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
438727f394a7STyson Nottingham 		}
4388ac27a0ecSDave Kleikamp 		break;
4389ac27a0ecSDave Kleikamp 
4390617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4391617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4392dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4393dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4394b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4395b31e1552SEric Sandeen 			       "requested data journaling mode");
4396744692dcSJiaying Zhang 			goto failed_mount_wq;
4397ac27a0ecSDave Kleikamp 		}
4398ac27a0ecSDave Kleikamp 	default:
4399ac27a0ecSDave Kleikamp 		break;
4400ac27a0ecSDave Kleikamp 	}
4401ab04df78SJan Kara 
4402ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4403ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4404ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4405ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4406ab04df78SJan Kara 		goto failed_mount_wq;
4407ab04df78SJan Kara 	}
4408ab04df78SJan Kara 
4409b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4410ac27a0ecSDave Kleikamp 
441118aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
441218aadd47SBobi Jam 
4413ce7e010aSTheodore Ts'o no_journal:
4414cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
441547387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
441647387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4417cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4418cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
44199c191f70ST Makphaibulchoke 			goto failed_mount_wq;
44209c191f70ST Makphaibulchoke 		}
44219c191f70ST Makphaibulchoke 
4422dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4423dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4424dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4425dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4426dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4427dec214d0STahsin Erdogan 				goto failed_mount_wq;
4428dec214d0STahsin Erdogan 			}
4429dec214d0STahsin Erdogan 		}
4430cdb7ee4cSTahsin Erdogan 	}
4431dec214d0STahsin Erdogan 
4432c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4433c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4434c93d8f88SEric Biggers 		goto failed_mount_wq;
4435c93d8f88SEric Biggers 	}
4436c93d8f88SEric Biggers 
4437bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4438e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4439e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
44406ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
44416ddb2447STheodore Ts'o 	}
44426ddb2447STheodore Ts'o 
4443fd89d5f2STejun Heo 	/*
4444952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4445952fc18eSTheodore Ts'o 	 * superblock if present.
4446952fc18eSTheodore Ts'o 	 */
4447952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4448952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4449952fc18eSTheodore Ts'o 	else {
445007aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
445107aa2ea1SLukas Czerner 		if (err)
4452952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4453952fc18eSTheodore Ts'o 	}
4454952fc18eSTheodore Ts'o 
4455952fc18eSTheodore Ts'o 	/*
4456fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4457fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4458fd89d5f2STejun Heo 	 */
44592e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
44602e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
44612e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
44622e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
446307aa2ea1SLukas Czerner 		ret = -ENOMEM;
44642e8fa54eSJan Kara 		goto failed_mount4;
44652e8fa54eSJan Kara 	}
44662e8fa54eSJan Kara 
4467ac27a0ecSDave Kleikamp 	/*
4468dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4469ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4470ac27a0ecSDave Kleikamp 	 */
4471ac27a0ecSDave Kleikamp 
44728a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
44731d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4474b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
44751d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
447632a9bb57SManish Katiyar 		root = NULL;
4477ac27a0ecSDave Kleikamp 		goto failed_mount4;
4478ac27a0ecSDave Kleikamp 	}
4479ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4480b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
448194bf608aSAl Viro 		iput(root);
4482ac27a0ecSDave Kleikamp 		goto failed_mount4;
4483ac27a0ecSDave Kleikamp 	}
4484b886ee3eSGabriel Krisman Bertazi 
4485b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4486b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4487b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4488b886ee3eSGabriel Krisman Bertazi #endif
4489b886ee3eSGabriel Krisman Bertazi 
449048fde701SAl Viro 	sb->s_root = d_make_root(root);
44911d1fe1eeSDavid Howells 	if (!sb->s_root) {
4492b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
44931d1fe1eeSDavid Howells 		ret = -ENOMEM;
44941d1fe1eeSDavid Howells 		goto failed_mount4;
44951d1fe1eeSDavid Howells 	}
4496ac27a0ecSDave Kleikamp 
4497c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4498c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
44991751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4500c89128a0SJaegeuk Kim 		ret = 0;
4501c89128a0SJaegeuk Kim 	} else if (ret)
4502c89128a0SJaegeuk Kim 		goto failed_mount4a;
4503ef7f3835SKalpak Shah 
45047bc04c5cSBarret Rhoden 	ext4_clamp_want_extra_isize(sb);
4505ef7f3835SKalpak Shah 
4506b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
450727dd4385SLukas Czerner 
45086fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
45096fd058f7STheodore Ts'o 	if (err) {
4510b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4511fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4512f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4513f9ae9cf5STheodore Ts'o 	}
4514f9ae9cf5STheodore Ts'o 
4515f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4516f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4517f9ae9cf5STheodore Ts'o 	if (err) {
4518f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4519f9ae9cf5STheodore Ts'o 			 err);
4520dcf2d804STao Ma 		goto failed_mount5;
4521c2774d84SAneesh Kumar K.V 	}
4522c2774d84SAneesh Kumar K.V 
4523d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4524d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4525d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
45264274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4527908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4528908c7f19STejun Heo 				  GFP_KERNEL);
4529d5e03cbbSTheodore Ts'o 	if (!err) {
4530d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4531d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
45324274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4533908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4534908c7f19STejun Heo 					  GFP_KERNEL);
4535d5e03cbbSTheodore Ts'o 	}
4536d5e03cbbSTheodore Ts'o 	if (!err)
4537d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4538908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4539d5e03cbbSTheodore Ts'o 	if (!err)
4540908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4541908c7f19STejun Heo 					  GFP_KERNEL);
4542c8585c6fSDaeho Jeong 	if (!err)
4543c8585c6fSDaeho Jeong 		err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
4544c8585c6fSDaeho Jeong 
4545d5e03cbbSTheodore Ts'o 	if (err) {
4546d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4547d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4548d5e03cbbSTheodore Ts'o 	}
4549d5e03cbbSTheodore Ts'o 
4550e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4551d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4552d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4553d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4554d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4555d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4556d5e03cbbSTheodore Ts'o 		}
4557d5e03cbbSTheodore Ts'o 
4558bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4559bfff6873SLukas Czerner 	if (err)
4560dcf2d804STao Ma 		goto failed_mount6;
4561bfff6873SLukas Czerner 
4562b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4563dcf2d804STao Ma 	if (err)
4564dcf2d804STao Ma 		goto failed_mount7;
45653197ebdbSTheodore Ts'o 
45669b2ff357SJan Kara #ifdef CONFIG_QUOTA
45679b2ff357SJan Kara 	/* Enable quota usage during mount. */
4568bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
45699b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
45709b2ff357SJan Kara 		if (err)
45719b2ff357SJan Kara 			goto failed_mount8;
45729b2ff357SJan Kara 	}
45739b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
45749b2ff357SJan Kara 
4575617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4576617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4577617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
45780390131bSFrank Mayhar 	if (needs_recovery) {
4579b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4580617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
45810390131bSFrank Mayhar 	}
45820390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
45830390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
45840390131bSFrank Mayhar 			descr = " journalled data mode";
45850390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
45860390131bSFrank Mayhar 			descr = " ordered data mode";
45870390131bSFrank Mayhar 		else
45880390131bSFrank Mayhar 			descr = " writeback data mode";
45890390131bSFrank Mayhar 	} else
45900390131bSFrank Mayhar 		descr = "out journal";
45910390131bSFrank Mayhar 
459279add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
459379add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
459479add3a3SLukas Czerner 		if (!blk_queue_discard(q))
459579add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
459679add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
459779add3a3SLukas Czerner 				 "the device does not support discard");
459879add3a3SLukas Czerner 	}
459979add3a3SLukas Czerner 
4600e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4601d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
46025aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
46035aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
46045aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
46058b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4606ac27a0ecSDave Kleikamp 
460766e61a9eSTheodore Ts'o 	if (es->s_error_count)
460866e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4609ac27a0ecSDave Kleikamp 
4610efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4611efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4612efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4613efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4614efbed4dcSTheodore Ts'o 
4615d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4616ac27a0ecSDave Kleikamp 	return 0;
4617ac27a0ecSDave Kleikamp 
4618617ba13bSMingming Cao cantfind_ext4:
4619ac27a0ecSDave Kleikamp 	if (!silent)
4620b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4621ac27a0ecSDave Kleikamp 	goto failed_mount;
4622ac27a0ecSDave Kleikamp 
462372ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
462472ba7450STheodore Ts'o failed_mount8:
4625ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
462672ba7450STheodore Ts'o #endif
4627dcf2d804STao Ma failed_mount7:
4628dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4629dcf2d804STao Ma failed_mount6:
4630f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4631d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4632b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4633d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4634d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4635d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4636d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4637af18e35bSVasily Averin 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
463800764937SAzat Khuzhin failed_mount5:
4639f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4640f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4641f9ae9cf5STheodore Ts'o failed_mount4a:
464294bf608aSAl Viro 	dput(sb->s_root);
464332a9bb57SManish Katiyar 	sb->s_root = NULL;
464494bf608aSAl Viro failed_mount4:
4645b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
46462e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
46472e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
46484c0425ffSMingming Cao failed_mount_wq:
4649dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4650dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
465150c15df6SChengguang Xu 
465247387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
465347387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
465450c15df6SChengguang Xu 
46550390131bSFrank Mayhar 	if (sbi->s_journal) {
4656dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
465747b4a50bSJan Kara 		sbi->s_journal = NULL;
46580390131bSFrank Mayhar 	}
465950460fe8SDarrick J. Wong failed_mount3a:
4660d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4661eb68d0e2SZheng Liu failed_mount3:
46629105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4663c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4664c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4665ac27a0ecSDave Kleikamp failed_mount2:
4666ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4667ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4668b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4669ac27a0ecSDave Kleikamp failed_mount:
46700441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
46710441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4672c83ad55eSGabriel Krisman Bertazi 
4673c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4674c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4675c83ad55eSGabriel Krisman Bertazi #endif
4676c83ad55eSGabriel Krisman Bertazi 
4677ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4678a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
46790ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4680ac27a0ecSDave Kleikamp #endif
4681617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4682ac27a0ecSDave Kleikamp 	brelse(bh);
4683ac27a0ecSDave Kleikamp out_fail:
4684ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4685f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
46865aee0f8aSTheodore Ts'o out_free_base:
4687ac27a0ecSDave Kleikamp 	kfree(sbi);
4688d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
46895e405595SDan Williams 	fs_put_dax(dax_dev);
469007aa2ea1SLukas Czerner 	return err ? err : ret;
4691ac27a0ecSDave Kleikamp }
4692ac27a0ecSDave Kleikamp 
4693ac27a0ecSDave Kleikamp /*
4694ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4695ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4696ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4697ac27a0ecSDave Kleikamp  */
4698617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4699ac27a0ecSDave Kleikamp {
4700617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4701ac27a0ecSDave Kleikamp 
4702ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
470330773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
470430773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4705ac27a0ecSDave Kleikamp 
4706a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4707ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4708dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4709ac27a0ecSDave Kleikamp 	else
4710dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
47115bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
47125bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
47135bf5683aSHidehiro Kawai 	else
47145bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4715a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4716ac27a0ecSDave Kleikamp }
4717ac27a0ecSDave Kleikamp 
4718c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4719ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4720ac27a0ecSDave Kleikamp {
4721ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4722ac27a0ecSDave Kleikamp 
4723c6cb7e77SEric Whitney 	/*
4724c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4725c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4726c6cb7e77SEric Whitney 	 * will try to delete it.
4727c6cb7e77SEric Whitney 	 */
47288a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
47291d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4730b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4731ac27a0ecSDave Kleikamp 		return NULL;
4732ac27a0ecSDave Kleikamp 	}
4733ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4734ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4735ac27a0ecSDave Kleikamp 		iput(journal_inode);
4736b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4737ac27a0ecSDave Kleikamp 		return NULL;
4738ac27a0ecSDave Kleikamp 	}
4739ac27a0ecSDave Kleikamp 
4740e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4741ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
47421d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4743b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4744ac27a0ecSDave Kleikamp 		iput(journal_inode);
4745ac27a0ecSDave Kleikamp 		return NULL;
4746ac27a0ecSDave Kleikamp 	}
4747c6cb7e77SEric Whitney 	return journal_inode;
4748c6cb7e77SEric Whitney }
4749c6cb7e77SEric Whitney 
4750c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4751c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4752c6cb7e77SEric Whitney {
4753c6cb7e77SEric Whitney 	struct inode *journal_inode;
4754c6cb7e77SEric Whitney 	journal_t *journal;
4755c6cb7e77SEric Whitney 
4756c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4757c6cb7e77SEric Whitney 
4758c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4759c6cb7e77SEric Whitney 	if (!journal_inode)
4760c6cb7e77SEric Whitney 		return NULL;
4761ac27a0ecSDave Kleikamp 
4762dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4763ac27a0ecSDave Kleikamp 	if (!journal) {
4764b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4765ac27a0ecSDave Kleikamp 		iput(journal_inode);
4766ac27a0ecSDave Kleikamp 		return NULL;
4767ac27a0ecSDave Kleikamp 	}
4768ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4769617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4770ac27a0ecSDave Kleikamp 	return journal;
4771ac27a0ecSDave Kleikamp }
4772ac27a0ecSDave Kleikamp 
4773617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4774ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4775ac27a0ecSDave Kleikamp {
4776ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4777ac27a0ecSDave Kleikamp 	journal_t *journal;
4778617ba13bSMingming Cao 	ext4_fsblk_t start;
4779617ba13bSMingming Cao 	ext4_fsblk_t len;
4780ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4781617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4782ac27a0ecSDave Kleikamp 	unsigned long offset;
4783617ba13bSMingming Cao 	struct ext4_super_block *es;
4784ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4785ac27a0ecSDave Kleikamp 
4786e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
47870390131bSFrank Mayhar 
4788b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4789ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4790ac27a0ecSDave Kleikamp 		return NULL;
4791ac27a0ecSDave Kleikamp 
4792ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4793e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4794ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4795b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4796b31e1552SEric Sandeen 			"blocksize too small for journal device");
4797ac27a0ecSDave Kleikamp 		goto out_bdev;
4798ac27a0ecSDave Kleikamp 	}
4799ac27a0ecSDave Kleikamp 
4800617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4801617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4802ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4803ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4804b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4805b31e1552SEric Sandeen 		       "external journal");
4806ac27a0ecSDave Kleikamp 		goto out_bdev;
4807ac27a0ecSDave Kleikamp 	}
4808ac27a0ecSDave Kleikamp 
48092716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4810617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4811ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4812617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4813b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4814b31e1552SEric Sandeen 					"bad superblock");
4815ac27a0ecSDave Kleikamp 		brelse(bh);
4816ac27a0ecSDave Kleikamp 		goto out_bdev;
4817ac27a0ecSDave Kleikamp 	}
4818ac27a0ecSDave Kleikamp 
4819df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4820df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4821df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4822df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4823df4763beSDarrick J. Wong 				       "corrupt superblock");
4824df4763beSDarrick J. Wong 		brelse(bh);
4825df4763beSDarrick J. Wong 		goto out_bdev;
4826df4763beSDarrick J. Wong 	}
4827df4763beSDarrick J. Wong 
4828617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4829b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4830ac27a0ecSDave Kleikamp 		brelse(bh);
4831ac27a0ecSDave Kleikamp 		goto out_bdev;
4832ac27a0ecSDave Kleikamp 	}
4833ac27a0ecSDave Kleikamp 
4834bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4835ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4836ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4837ac27a0ecSDave Kleikamp 
4838dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4839ac27a0ecSDave Kleikamp 					start, len, blocksize);
4840ac27a0ecSDave Kleikamp 	if (!journal) {
4841b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4842ac27a0ecSDave Kleikamp 		goto out_bdev;
4843ac27a0ecSDave Kleikamp 	}
4844ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4845dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4846ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4847ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4848b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4849ac27a0ecSDave Kleikamp 		goto out_journal;
4850ac27a0ecSDave Kleikamp 	}
4851ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4852b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4853b31e1552SEric Sandeen 					"user (unsupported) - %d",
4854ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4855ac27a0ecSDave Kleikamp 		goto out_journal;
4856ac27a0ecSDave Kleikamp 	}
4857617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4858617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4859ac27a0ecSDave Kleikamp 	return journal;
48600b8e58a1SAndreas Dilger 
4861ac27a0ecSDave Kleikamp out_journal:
4862dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4863ac27a0ecSDave Kleikamp out_bdev:
4864617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4865ac27a0ecSDave Kleikamp 	return NULL;
4866ac27a0ecSDave Kleikamp }
4867ac27a0ecSDave Kleikamp 
4868617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4869617ba13bSMingming Cao 			     struct ext4_super_block *es,
4870ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4871ac27a0ecSDave Kleikamp {
4872ac27a0ecSDave Kleikamp 	journal_t *journal;
4873ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4874ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4875ac27a0ecSDave Kleikamp 	int err = 0;
4876ac27a0ecSDave Kleikamp 	int really_read_only;
4877ac27a0ecSDave Kleikamp 
4878e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
48790390131bSFrank Mayhar 
4880ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4881ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4882b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4883b31e1552SEric Sandeen 			"numbers have changed");
4884ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4885ac27a0ecSDave Kleikamp 	} else
4886ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4887ac27a0ecSDave Kleikamp 
4888ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4889ac27a0ecSDave Kleikamp 
4890ac27a0ecSDave Kleikamp 	/*
4891ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4892ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4893ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4894ac27a0ecSDave Kleikamp 	 */
4895e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4896bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
4897b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4898b31e1552SEric Sandeen 					"required on readonly filesystem");
4899ac27a0ecSDave Kleikamp 			if (really_read_only) {
4900b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4901d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
4902d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
4903ac27a0ecSDave Kleikamp 				return -EROFS;
4904ac27a0ecSDave Kleikamp 			}
4905b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4906b31e1552SEric Sandeen 			       "be enabled during recovery");
4907ac27a0ecSDave Kleikamp 		}
4908ac27a0ecSDave Kleikamp 	}
4909ac27a0ecSDave Kleikamp 
4910ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4911b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4912b31e1552SEric Sandeen 		       "and inode journals!");
4913ac27a0ecSDave Kleikamp 		return -EINVAL;
4914ac27a0ecSDave Kleikamp 	}
4915ac27a0ecSDave Kleikamp 
4916ac27a0ecSDave Kleikamp 	if (journal_inum) {
4917617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4918ac27a0ecSDave Kleikamp 			return -EINVAL;
4919ac27a0ecSDave Kleikamp 	} else {
4920617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4921ac27a0ecSDave Kleikamp 			return -EINVAL;
4922ac27a0ecSDave Kleikamp 	}
4923ac27a0ecSDave Kleikamp 
492490576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4925b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
49264776004fSTheodore Ts'o 
4927e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4928dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
49291c13d5c0STheodore Ts'o 	if (!err) {
49301c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
49311c13d5c0STheodore Ts'o 		if (save)
49321c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
49331c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4934dab291afSMingming Cao 		err = jbd2_journal_load(journal);
49351c13d5c0STheodore Ts'o 		if (save)
49361c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
49371c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
49381c13d5c0STheodore Ts'o 		kfree(save);
49391c13d5c0STheodore Ts'o 	}
4940ac27a0ecSDave Kleikamp 
4941ac27a0ecSDave Kleikamp 	if (err) {
4942b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4943dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4944ac27a0ecSDave Kleikamp 		return err;
4945ac27a0ecSDave Kleikamp 	}
4946ac27a0ecSDave Kleikamp 
4947617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4948617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4949ac27a0ecSDave Kleikamp 
4950c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4951ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4952ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4953ac27a0ecSDave Kleikamp 
4954ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4955e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4956ac27a0ecSDave Kleikamp 	}
4957ac27a0ecSDave Kleikamp 
4958ac27a0ecSDave Kleikamp 	return 0;
4959ac27a0ecSDave Kleikamp }
4960ac27a0ecSDave Kleikamp 
4961e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4962ac27a0ecSDave Kleikamp {
4963e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4964617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4965c4be0c1dSTakashi Sato 	int error = 0;
4966ac27a0ecSDave Kleikamp 
4967bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4968c4be0c1dSTakashi Sato 		return error;
4969a17712c8SJon Derrick 
4970a17712c8SJon Derrick 	/*
4971a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
4972a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
4973a17712c8SJon Derrick 	 */
4974a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
4975a17712c8SJon Derrick 		return error;
4976a17712c8SJon Derrick 
497771290b36STheodore Ts'o 	/*
497871290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
497971290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
498071290b36STheodore Ts'o 	 * write time when we are mounting the root file system
498171290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
498271290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
498371290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
498471290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
498571290b36STheodore Ts'o 	 * to complain and force a full file system check.
498671290b36STheodore Ts'o 	 */
49871751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
49886a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
4989f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4990afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4991afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4992dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
4993dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
4994afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4995f613dfcbSTheodore Ts'o 	else
4996f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
4997f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
4998d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
499957042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
500057042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
500157042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5002d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
50037f93cff9STheodore Ts'o 		es->s_free_inodes_count =
50047f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
50055d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5006ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
500706db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
50081566a48aSTheodore Ts'o 	if (sync)
50094743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5010e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
50114743f839SPranay Kr. Srivastava 		/*
50124743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
50134743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
50144743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
50154743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
50164743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
50174743f839SPranay Kr. Srivastava 		 * write and hope for the best.
50184743f839SPranay Kr. Srivastava 		 */
50194743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
50204743f839SPranay Kr. Srivastava 		       "superblock detected");
50214743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
50224743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
50234743f839SPranay Kr. Srivastava 	}
5024ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5025914258bfSTheodore Ts'o 	if (sync) {
50261566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5027564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
502800473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5029c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5030b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5031b31e1552SEric Sandeen 			       "superblock");
5032914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5033914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5034914258bfSTheodore Ts'o 		}
5035914258bfSTheodore Ts'o 	}
5036c4be0c1dSTakashi Sato 	return error;
5037ac27a0ecSDave Kleikamp }
5038ac27a0ecSDave Kleikamp 
5039ac27a0ecSDave Kleikamp /*
5040ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5041ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5042ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5043ac27a0ecSDave Kleikamp  */
5044617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5045617ba13bSMingming Cao 					struct ext4_super_block *es)
5046ac27a0ecSDave Kleikamp {
5047617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5048ac27a0ecSDave Kleikamp 
5049e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
50500390131bSFrank Mayhar 		BUG_ON(journal != NULL);
50510390131bSFrank Mayhar 		return;
50520390131bSFrank Mayhar 	}
5053dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
50547ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
50557ffe1ea8SHidehiro Kawai 		goto out;
50567ffe1ea8SHidehiro Kawai 
5057bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5058e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5059e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5060ac27a0ecSDave Kleikamp 	}
50617ffe1ea8SHidehiro Kawai 
50627ffe1ea8SHidehiro Kawai out:
5063dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5064ac27a0ecSDave Kleikamp }
5065ac27a0ecSDave Kleikamp 
5066ac27a0ecSDave Kleikamp /*
5067ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5068ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5069ac27a0ecSDave Kleikamp  * main filesystem now.
5070ac27a0ecSDave Kleikamp  */
5071617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5072617ba13bSMingming Cao 				   struct ext4_super_block *es)
5073ac27a0ecSDave Kleikamp {
5074ac27a0ecSDave Kleikamp 	journal_t *journal;
5075ac27a0ecSDave Kleikamp 	int j_errno;
5076ac27a0ecSDave Kleikamp 	const char *errstr;
5077ac27a0ecSDave Kleikamp 
5078e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
50790390131bSFrank Mayhar 
5080617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5081ac27a0ecSDave Kleikamp 
5082ac27a0ecSDave Kleikamp 	/*
5083ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5084617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5085ac27a0ecSDave Kleikamp 	 */
5086ac27a0ecSDave Kleikamp 
5087dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5088ac27a0ecSDave Kleikamp 	if (j_errno) {
5089ac27a0ecSDave Kleikamp 		char nbuf[16];
5090ac27a0ecSDave Kleikamp 
5091617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
509212062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5093ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
509412062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5095ac27a0ecSDave Kleikamp 
5096617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5097617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5098e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5099ac27a0ecSDave Kleikamp 
5100dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5101d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5102ac27a0ecSDave Kleikamp 	}
5103ac27a0ecSDave Kleikamp }
5104ac27a0ecSDave Kleikamp 
5105ac27a0ecSDave Kleikamp /*
5106ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5107ac27a0ecSDave Kleikamp  * and wait on the commit.
5108ac27a0ecSDave Kleikamp  */
5109617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5110ac27a0ecSDave Kleikamp {
5111ac27a0ecSDave Kleikamp 	journal_t *journal;
5112ac27a0ecSDave Kleikamp 
5113bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5114ac27a0ecSDave Kleikamp 		return 0;
5115ac27a0ecSDave Kleikamp 
5116617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5117b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5118ac27a0ecSDave Kleikamp }
5119ac27a0ecSDave Kleikamp 
5120617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5121ac27a0ecSDave Kleikamp {
512214ce0cb4STheodore Ts'o 	int ret = 0;
51239eddacf9SJan Kara 	tid_t target;
512406a407f1SDmitry Monakhov 	bool needs_barrier = false;
51258d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5126ac27a0ecSDave Kleikamp 
512749598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
51280db1ff22STheodore Ts'o 		return 0;
51290db1ff22STheodore Ts'o 
51309bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
51312e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5132a1177825SJan Kara 	/*
5133a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5134a1177825SJan Kara 	 * no dirty dquots
5135a1177825SJan Kara 	 */
5136a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
513706a407f1SDmitry Monakhov 	/*
513806a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
513906a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
514006a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
514106a407f1SDmitry Monakhov 	 */
5142bda32530STheodore Ts'o 	if (sbi->s_journal) {
514306a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
514406a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
514506a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
514606a407f1SDmitry Monakhov 			needs_barrier = true;
514706a407f1SDmitry Monakhov 
51488d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5149ac27a0ecSDave Kleikamp 			if (wait)
5150bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5151bda32530STheodore Ts'o 							   target);
51520390131bSFrank Mayhar 		}
5153bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5154bda32530STheodore Ts'o 		needs_barrier = true;
515506a407f1SDmitry Monakhov 	if (needs_barrier) {
515606a407f1SDmitry Monakhov 		int err;
515706a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
515806a407f1SDmitry Monakhov 		if (!ret)
515906a407f1SDmitry Monakhov 			ret = err;
516006a407f1SDmitry Monakhov 	}
516106a407f1SDmitry Monakhov 
516206a407f1SDmitry Monakhov 	return ret;
516306a407f1SDmitry Monakhov }
516406a407f1SDmitry Monakhov 
5165ac27a0ecSDave Kleikamp /*
5166ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5167ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5168be4f27d3SYongqiang Yang  *
5169be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
51708e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
51718e8ad8a5SJan Kara  * modifications.
5172ac27a0ecSDave Kleikamp  */
5173c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5174ac27a0ecSDave Kleikamp {
5175c4be0c1dSTakashi Sato 	int error = 0;
5176c4be0c1dSTakashi Sato 	journal_t *journal;
5177ac27a0ecSDave Kleikamp 
5178bc98a42cSDavid Howells 	if (sb_rdonly(sb))
51799ca92389STheodore Ts'o 		return 0;
51809ca92389STheodore Ts'o 
5181c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5182ac27a0ecSDave Kleikamp 
5183bb044576STheodore Ts'o 	if (journal) {
5184ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5185dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
51867ffe1ea8SHidehiro Kawai 
51877ffe1ea8SHidehiro Kawai 		/*
5188bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5189bb044576STheodore Ts'o 		 * flush the journal.
51907ffe1ea8SHidehiro Kawai 		 */
5191c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
51926b0310fbSEric Sandeen 		if (error < 0)
51936b0310fbSEric Sandeen 			goto out;
5194ac27a0ecSDave Kleikamp 
5195ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5196e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5197c642dc9eSEric Sandeen 	}
5198c642dc9eSEric Sandeen 
5199e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
52006b0310fbSEric Sandeen out:
5201bb044576STheodore Ts'o 	if (journal)
52028e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5203bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
52046b0310fbSEric Sandeen 	return error;
5205ac27a0ecSDave Kleikamp }
5206ac27a0ecSDave Kleikamp 
5207ac27a0ecSDave Kleikamp /*
5208ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5209ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5210ac27a0ecSDave Kleikamp  */
5211c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5212ac27a0ecSDave Kleikamp {
5213bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
52149ca92389STheodore Ts'o 		return 0;
52159ca92389STheodore Ts'o 
5216c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
52179ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5218e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5219c642dc9eSEric Sandeen 	}
5220c642dc9eSEric Sandeen 
5221e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5222c4be0c1dSTakashi Sato 	return 0;
5223ac27a0ecSDave Kleikamp }
5224ac27a0ecSDave Kleikamp 
5225673c6100STheodore Ts'o /*
5226673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5227673c6100STheodore Ts'o  */
5228673c6100STheodore Ts'o struct ext4_mount_options {
5229673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5230a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
523108cefc7aSEric W. Biederman 	kuid_t s_resuid;
523208cefc7aSEric W. Biederman 	kgid_t s_resgid;
5233673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5234673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5235673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5236673c6100STheodore Ts'o 	int s_jquota_fmt;
5237a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5238673c6100STheodore Ts'o #endif
5239673c6100STheodore Ts'o };
5240673c6100STheodore Ts'o 
5241617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5242ac27a0ecSDave Kleikamp {
5243617ba13bSMingming Cao 	struct ext4_super_block *es;
5244617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5245ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5246617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5247c79d967dSChristoph Hellwig 	int enable_quota = 0;
52488a266467STheodore Ts'o 	ext4_group_t g;
5249b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5250c5e06d10SJohann Lombardi 	int err = 0;
5251ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
525203dafb5fSChen Gang 	int i, j;
525333458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5254ac27a0ecSDave Kleikamp #endif
5255d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5256ac27a0ecSDave Kleikamp 
525721ac738eSChengguang Xu 	if (data && !orig_data)
525821ac738eSChengguang Xu 		return -ENOMEM;
525921ac738eSChengguang Xu 
5260ac27a0ecSDave Kleikamp 	/* Store the original options */
5261ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5262ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5263a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5264ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5265ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5266ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
526730773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
526830773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5269ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5270ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5271a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
527203dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
527333458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
527433458eabSTheodore Ts'o 
527533458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
527603dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
527703dafb5fSChen Gang 				for (j = 0; j < i; j++)
527803dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
52793e36a163SWei Yongjun 				kfree(orig_data);
528003dafb5fSChen Gang 				return -ENOMEM;
528103dafb5fSChen Gang 			}
528203dafb5fSChen Gang 		} else
528303dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5284ac27a0ecSDave Kleikamp #endif
5285b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5286b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5287ac27a0ecSDave Kleikamp 
5288661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5289ac27a0ecSDave Kleikamp 		err = -EINVAL;
5290ac27a0ecSDave Kleikamp 		goto restore_opts;
5291ac27a0ecSDave Kleikamp 	}
5292ac27a0ecSDave Kleikamp 
52937bc04c5cSBarret Rhoden 	ext4_clamp_want_extra_isize(sb);
52947bc04c5cSBarret Rhoden 
52956b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
52966b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
52976b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
52982d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
52992d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5300c6d3d56dSDarrick J. Wong 	}
5301c6d3d56dSDarrick J. Wong 
53026ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
53036ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
53046ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53056ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
53066ae6514bSPiotr Sarna 			err = -EINVAL;
53076ae6514bSPiotr Sarna 			goto restore_opts;
53086ae6514bSPiotr Sarna 		}
53096ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
53106ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53116ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
53126ae6514bSPiotr Sarna 			err = -EINVAL;
53136ae6514bSPiotr Sarna 			goto restore_opts;
53146ae6514bSPiotr Sarna 		}
5315923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
5316923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5317923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5318923ae0ffSRoss Zwisler 			err = -EINVAL;
5319923ae0ffSRoss Zwisler 			goto restore_opts;
5320923ae0ffSRoss Zwisler 		}
5321ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5322ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5323ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5324ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5325ab04df78SJan Kara 			err = -EINVAL;
5326ab04df78SJan Kara 			goto restore_opts;
5327ab04df78SJan Kara 		}
5328923ae0ffSRoss Zwisler 	}
5329923ae0ffSRoss Zwisler 
5330cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5331cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5332cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5333cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5334cdb7ee4cSTahsin Erdogan 	}
5335cdb7ee4cSTahsin Erdogan 
5336923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
5337923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5338923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
5339923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
53406ae6514bSPiotr Sarna 	}
53416ae6514bSPiotr Sarna 
53424ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
5343c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
5344ac27a0ecSDave Kleikamp 
53451751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
53461751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5347ac27a0ecSDave Kleikamp 
5348ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5349ac27a0ecSDave Kleikamp 
5350b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5351617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5352b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5353b3881f74STheodore Ts'o 	}
5354ac27a0ecSDave Kleikamp 
53551751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
53561751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5357a2fd66d0STheodore Ts'o 
53581751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
53594ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5360ac27a0ecSDave Kleikamp 			err = -EROFS;
5361ac27a0ecSDave Kleikamp 			goto restore_opts;
5362ac27a0ecSDave Kleikamp 		}
5363ac27a0ecSDave Kleikamp 
53641751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
536538c03b34STheodore Ts'o 			err = sync_filesystem(sb);
536638c03b34STheodore Ts'o 			if (err < 0)
536738c03b34STheodore Ts'o 				goto restore_opts;
53680f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
53690f0dd62fSChristoph Hellwig 			if (err < 0)
5370c79d967dSChristoph Hellwig 				goto restore_opts;
5371c79d967dSChristoph Hellwig 
5372ac27a0ecSDave Kleikamp 			/*
5373ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5374ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5375ac27a0ecSDave Kleikamp 			 */
53761751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5377ac27a0ecSDave Kleikamp 
5378ac27a0ecSDave Kleikamp 			/*
5379ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5380ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5381ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5382ac27a0ecSDave Kleikamp 			 */
5383617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5384617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5385ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5386ac27a0ecSDave Kleikamp 
5387a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5388617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
53892dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
53902dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5391ac27a0ecSDave Kleikamp 		} else {
5392a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5393e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
53942cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5395ac27a0ecSDave Kleikamp 				err = -EROFS;
5396ac27a0ecSDave Kleikamp 				goto restore_opts;
5397ac27a0ecSDave Kleikamp 			}
5398ead6596bSEric Sandeen 			/*
53998a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
54000b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
54018a266467STheodore Ts'o 			 */
54028a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
54038a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
54048a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
54058a266467STheodore Ts'o 
5406feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5407b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5408b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5409e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
54108a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
54116a797d27SDarrick J. Wong 					err = -EFSBADCRC;
54128a266467STheodore Ts'o 					goto restore_opts;
54138a266467STheodore Ts'o 				}
54148a266467STheodore Ts'o 			}
54158a266467STheodore Ts'o 
54168a266467STheodore Ts'o 			/*
5417ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5418ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5419ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5420ead6596bSEric Sandeen 			 */
5421ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5422b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5423ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5424ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5425b31e1552SEric Sandeen 				       "umount/remount instead");
5426ead6596bSEric Sandeen 				err = -EINVAL;
5427ead6596bSEric Sandeen 				goto restore_opts;
5428ead6596bSEric Sandeen 			}
5429ead6596bSEric Sandeen 
5430ac27a0ecSDave Kleikamp 			/*
5431ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5432ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5433ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5434ac27a0ecSDave Kleikamp 			 * the partition.)
5435ac27a0ecSDave Kleikamp 			 */
54360390131bSFrank Mayhar 			if (sbi->s_journal)
5437617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5438ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5439c89128a0SJaegeuk Kim 
5440c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5441c89128a0SJaegeuk Kim 			if (err)
5442c89128a0SJaegeuk Kim 				goto restore_opts;
5443c89128a0SJaegeuk Kim 
54441751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5445e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5446c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5447c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5448c5e06d10SJohann Lombardi 					err = -EROFS;
5449c5e06d10SJohann Lombardi 					goto restore_opts;
5450c5e06d10SJohann Lombardi 				}
5451c79d967dSChristoph Hellwig 			enable_quota = 1;
5452ac27a0ecSDave Kleikamp 		}
5453ac27a0ecSDave Kleikamp 	}
5454bfff6873SLukas Czerner 
5455bfff6873SLukas Czerner 	/*
5456bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5457bfff6873SLukas Czerner 	 * current settings
5458bfff6873SLukas Czerner 	 */
5459bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5460bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5461bfff6873SLukas Czerner 	else {
5462bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5463bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5464bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5465bfff6873SLukas Czerner 	}
5466bfff6873SLukas Czerner 
54676fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5468c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5469c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5470c89128a0SJaegeuk Kim 		if (err)
5471c89128a0SJaegeuk Kim 			goto restore_opts;
5472c89128a0SJaegeuk Kim 	}
54730390131bSFrank Mayhar 
5474ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5475ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5476a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5477ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
54787c319d32SAditya Kali 	if (enable_quota) {
54797c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
54800f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5481e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
54827c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
548307724f98STheodore Ts'o 			if (err)
54847c319d32SAditya Kali 				goto restore_opts;
54857c319d32SAditya Kali 		}
54867c319d32SAditya Kali 	}
54877c319d32SAditya Kali #endif
5488d4c402d9SCurt Wohlgemuth 
54891751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5490d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5491d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5492ac27a0ecSDave Kleikamp 	return 0;
54930b8e58a1SAndreas Dilger 
5494ac27a0ecSDave Kleikamp restore_opts:
5495ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5496ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5497a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5498ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5499ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5500ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
550130773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
550230773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5503ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5504ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5505a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
550633458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
550733458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5508ac27a0ecSDave Kleikamp 	}
550933458eabSTheodore Ts'o 	synchronize_rcu();
551033458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
551133458eabSTheodore Ts'o 		kfree(to_free[i]);
5512ac27a0ecSDave Kleikamp #endif
5513d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5514ac27a0ecSDave Kleikamp 	return err;
5515ac27a0ecSDave Kleikamp }
5516ac27a0ecSDave Kleikamp 
5517689c958cSLi Xi #ifdef CONFIG_QUOTA
5518689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5519689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5520689c958cSLi Xi {
5521689c958cSLi Xi 	struct kqid qid;
5522689c958cSLi Xi 	struct dquot *dquot;
5523689c958cSLi Xi 	u64 limit;
5524689c958cSLi Xi 	u64 curblock;
5525689c958cSLi Xi 
5526689c958cSLi Xi 	qid = make_kqid_projid(projid);
5527689c958cSLi Xi 	dquot = dqget(sb, qid);
5528689c958cSLi Xi 	if (IS_ERR(dquot))
5529689c958cSLi Xi 		return PTR_ERR(dquot);
55307b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5531689c958cSLi Xi 
5532689c958cSLi Xi 	limit = (dquot->dq_dqb.dqb_bsoftlimit ?
5533689c958cSLi Xi 		 dquot->dq_dqb.dqb_bsoftlimit :
5534689c958cSLi Xi 		 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
5535689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5536f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5537f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5538689c958cSLi Xi 		buf->f_blocks = limit;
5539689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5540689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5541689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5542689c958cSLi Xi 	}
5543689c958cSLi Xi 
5544689c958cSLi Xi 	limit = dquot->dq_dqb.dqb_isoftlimit ?
5545689c958cSLi Xi 		dquot->dq_dqb.dqb_isoftlimit :
5546689c958cSLi Xi 		dquot->dq_dqb.dqb_ihardlimit;
5547689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5548689c958cSLi Xi 		buf->f_files = limit;
5549689c958cSLi Xi 		buf->f_ffree =
5550689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5551689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5552689c958cSLi Xi 	}
5553689c958cSLi Xi 
55547b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5555689c958cSLi Xi 	dqput(dquot);
5556689c958cSLi Xi 	return 0;
5557689c958cSLi Xi }
5558689c958cSLi Xi #endif
5559689c958cSLi Xi 
5560617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5561ac27a0ecSDave Kleikamp {
5562ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5563617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5564617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
556527dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5566960cc398SPekka Enberg 	u64 fsid;
5567d02a9391SKazuya Mio 	s64 bfree;
556827dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5569ac27a0ecSDave Kleikamp 
5570952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5571952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5572ac27a0ecSDave Kleikamp 
5573617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5574ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5575b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
557657042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
557757042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5578d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
557957042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
558027dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
558127dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
558227dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5583ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5584ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
558552d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5586617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5587960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5588960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5589960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5590960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
55910b8e58a1SAndreas Dilger 
5592689c958cSLi Xi #ifdef CONFIG_QUOTA
5593689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5594689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5595689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5596689c958cSLi Xi #endif
5597ac27a0ecSDave Kleikamp 	return 0;
5598ac27a0ecSDave Kleikamp }
5599ac27a0ecSDave Kleikamp 
5600ac27a0ecSDave Kleikamp 
5601ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5602ac27a0ecSDave Kleikamp 
5603bc8230eeSJan Kara /*
5604bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5605bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5606bc8230eeSJan Kara  */
5607ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5608ac27a0ecSDave Kleikamp {
56094c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5610ac27a0ecSDave Kleikamp }
5611ac27a0ecSDave Kleikamp 
5612617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5613ac27a0ecSDave Kleikamp {
5614ac27a0ecSDave Kleikamp 	int ret, err;
5615ac27a0ecSDave Kleikamp 	handle_t *handle;
5616ac27a0ecSDave Kleikamp 	struct inode *inode;
5617ac27a0ecSDave Kleikamp 
5618ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
56199924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5620617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5621ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5622ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5623ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5624617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5625ac27a0ecSDave Kleikamp 	if (!ret)
5626ac27a0ecSDave Kleikamp 		ret = err;
5627ac27a0ecSDave Kleikamp 	return ret;
5628ac27a0ecSDave Kleikamp }
5629ac27a0ecSDave Kleikamp 
5630617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5631ac27a0ecSDave Kleikamp {
5632ac27a0ecSDave Kleikamp 	int ret, err;
5633ac27a0ecSDave Kleikamp 	handle_t *handle;
5634ac27a0ecSDave Kleikamp 
56359924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5636617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5637ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5638ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5639ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5640617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5641ac27a0ecSDave Kleikamp 	if (!ret)
5642ac27a0ecSDave Kleikamp 		ret = err;
5643ac27a0ecSDave Kleikamp 	return ret;
5644ac27a0ecSDave Kleikamp }
5645ac27a0ecSDave Kleikamp 
5646617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5647ac27a0ecSDave Kleikamp {
5648ac27a0ecSDave Kleikamp 	int ret, err;
5649ac27a0ecSDave Kleikamp 	handle_t *handle;
5650ac27a0ecSDave Kleikamp 
56519924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5652617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
56539c3013e9SJan Kara 	if (IS_ERR(handle)) {
56549c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
56559c3013e9SJan Kara 		dquot_release(dquot);
5656ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
56579c3013e9SJan Kara 	}
5658ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5659617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5660ac27a0ecSDave Kleikamp 	if (!ret)
5661ac27a0ecSDave Kleikamp 		ret = err;
5662ac27a0ecSDave Kleikamp 	return ret;
5663ac27a0ecSDave Kleikamp }
5664ac27a0ecSDave Kleikamp 
5665617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5666ac27a0ecSDave Kleikamp {
5667262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5668262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5669262b4662SJan Kara 
56702c8be6b2SJan Kara 	/* Are we journaling quotas? */
5671e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5672262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5673ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5674617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5675ac27a0ecSDave Kleikamp 	} else {
5676ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5677ac27a0ecSDave Kleikamp 	}
5678ac27a0ecSDave Kleikamp }
5679ac27a0ecSDave Kleikamp 
5680617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5681ac27a0ecSDave Kleikamp {
5682ac27a0ecSDave Kleikamp 	int ret, err;
5683ac27a0ecSDave Kleikamp 	handle_t *handle;
5684ac27a0ecSDave Kleikamp 
5685ac27a0ecSDave Kleikamp 	/* Data block + inode block */
56862b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5687ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5688ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5689ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5690617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5691ac27a0ecSDave Kleikamp 	if (!ret)
5692ac27a0ecSDave Kleikamp 		ret = err;
5693ac27a0ecSDave Kleikamp 	return ret;
5694ac27a0ecSDave Kleikamp }
5695ac27a0ecSDave Kleikamp 
5696ac27a0ecSDave Kleikamp /*
5697ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5698ac27a0ecSDave Kleikamp  * the quota file and such...
5699ac27a0ecSDave Kleikamp  */
5700617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5701ac27a0ecSDave Kleikamp {
570233458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5703617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5704ac27a0ecSDave Kleikamp }
5705ac27a0ecSDave Kleikamp 
5706daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5707daf647d2STheodore Ts'o {
5708daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5709daf647d2STheodore Ts'o 
5710daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5711daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5712daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5713daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5714daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5715daf647d2STheodore Ts'o 	 */
5716daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5717daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5718daf647d2STheodore Ts'o }
5719daf647d2STheodore Ts'o 
5720ac27a0ecSDave Kleikamp /*
5721ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5722ac27a0ecSDave Kleikamp  */
5723617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
57248c54ca9cSAl Viro 			 const struct path *path)
5725ac27a0ecSDave Kleikamp {
5726ac27a0ecSDave Kleikamp 	int err;
5727ac27a0ecSDave Kleikamp 
5728ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5729ac27a0ecSDave Kleikamp 		return -EINVAL;
57300623543bSJan Kara 
5731ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5732d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5733ac27a0ecSDave Kleikamp 		return -EXDEV;
57340623543bSJan Kara 	/* Journaling quota? */
57350623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
57362b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5737f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5738b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5739b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5740b31e1552SEric Sandeen 				"Journaled quota will not work");
574191389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
574291389240SJan Kara 	} else {
574391389240SJan Kara 		/*
574491389240SJan Kara 		 * Clear the flag just in case mount options changed since
574591389240SJan Kara 		 * last time.
574691389240SJan Kara 		 */
574791389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
57480623543bSJan Kara 	}
57490623543bSJan Kara 
57500623543bSJan Kara 	/*
57510623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
57520623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
57530623543bSJan Kara 	 */
57540390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
57552b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
57560623543bSJan Kara 		/*
57570623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
57580623543bSJan Kara 		 * otherwise be livelocked...
57590623543bSJan Kara 		 */
57600623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
57617ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
57620623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5763f00c9e44SJan Kara 		if (err)
57647ffe1ea8SHidehiro Kawai 			return err;
57657ffe1ea8SHidehiro Kawai 	}
5766957153fcSJan Kara 
5767daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5768daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5769957153fcSJan Kara 	if (err) {
5770daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5771daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5772957153fcSJan Kara 	} else {
5773957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5774957153fcSJan Kara 		handle_t *handle;
5775957153fcSJan Kara 
577661a92987SJan Kara 		/*
577761a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
577861a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
577961a92987SJan Kara 		 * are already enabled and this is not a hard failure.
578061a92987SJan Kara 		 */
5781957153fcSJan Kara 		inode_lock(inode);
5782957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5783957153fcSJan Kara 		if (IS_ERR(handle))
5784957153fcSJan Kara 			goto unlock_inode;
5785957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5786957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5787957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5788957153fcSJan Kara 		ext4_mark_inode_dirty(handle, inode);
5789957153fcSJan Kara 		ext4_journal_stop(handle);
5790957153fcSJan Kara 	unlock_inode:
5791957153fcSJan Kara 		inode_unlock(inode);
5792957153fcSJan Kara 	}
5793daf647d2STheodore Ts'o 	return err;
5794ac27a0ecSDave Kleikamp }
5795ac27a0ecSDave Kleikamp 
57967c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
57977c319d32SAditya Kali 			     unsigned int flags)
57987c319d32SAditya Kali {
57997c319d32SAditya Kali 	int err;
58007c319d32SAditya Kali 	struct inode *qf_inode;
5801a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58027c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5803689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5804689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58057c319d32SAditya Kali 	};
58067c319d32SAditya Kali 
5807e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
58087c319d32SAditya Kali 
58097c319d32SAditya Kali 	if (!qf_inums[type])
58107c319d32SAditya Kali 		return -EPERM;
58117c319d32SAditya Kali 
58128a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
58137c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
58147c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
58157c319d32SAditya Kali 		return PTR_ERR(qf_inode);
58167c319d32SAditya Kali 	}
58177c319d32SAditya Kali 
5818bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5819bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5820daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
58217c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
5822daf647d2STheodore Ts'o 	if (err)
5823daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
582461157b24SPan Bian 	iput(qf_inode);
58257c319d32SAditya Kali 
58267c319d32SAditya Kali 	return err;
58277c319d32SAditya Kali }
58287c319d32SAditya Kali 
58297c319d32SAditya Kali /* Enable usage tracking for all quota types. */
58307c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
58317c319d32SAditya Kali {
58327c319d32SAditya Kali 	int type, err = 0;
5833a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58347c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5835689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5836689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58377c319d32SAditya Kali 	};
583849da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
583949da9392SJan Kara 		test_opt(sb, USRQUOTA),
584049da9392SJan Kara 		test_opt(sb, GRPQUOTA),
584149da9392SJan Kara 		test_opt(sb, PRJQUOTA),
584249da9392SJan Kara 	};
58437c319d32SAditya Kali 
584491389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5845a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
58467c319d32SAditya Kali 		if (qf_inums[type]) {
58477c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
584849da9392SJan Kara 				DQUOT_USAGE_ENABLED |
584949da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
58507c319d32SAditya Kali 			if (err) {
58517c319d32SAditya Kali 				ext4_warning(sb,
585272ba7450STheodore Ts'o 					"Failed to enable quota tracking "
585372ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
585472ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
58557f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
58567f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
58577f144fd0SJunichi Uekawa 
58587c319d32SAditya Kali 				return err;
58597c319d32SAditya Kali 			}
58607c319d32SAditya Kali 		}
58617c319d32SAditya Kali 	}
58627c319d32SAditya Kali 	return 0;
58637c319d32SAditya Kali }
58647c319d32SAditya Kali 
5865ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5866ca0e05e4SDmitry Monakhov {
586721f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
586821f97697SJan Kara 	handle_t *handle;
5869957153fcSJan Kara 	int err;
587021f97697SJan Kara 
587187009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
587287009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
587387009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5874ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5875ca0e05e4SDmitry Monakhov 
5876957153fcSJan Kara 	if (!inode || !igrab(inode))
58770b268590SAmir Goldstein 		goto out;
58780b268590SAmir Goldstein 
5879957153fcSJan Kara 	err = dquot_quota_off(sb, type);
5880964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
5881957153fcSJan Kara 		goto out_put;
5882957153fcSJan Kara 
5883957153fcSJan Kara 	inode_lock(inode);
588461a92987SJan Kara 	/*
588561a92987SJan Kara 	 * Update modification times of quota files when userspace can
588661a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
588761a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
588861a92987SJan Kara 	 */
58899924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
589021f97697SJan Kara 	if (IS_ERR(handle))
5891957153fcSJan Kara 		goto out_unlock;
5892957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
5893957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
5894eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
589521f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
589621f97697SJan Kara 	ext4_journal_stop(handle);
5897957153fcSJan Kara out_unlock:
5898957153fcSJan Kara 	inode_unlock(inode);
5899957153fcSJan Kara out_put:
5900964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
5901957153fcSJan Kara 	iput(inode);
5902957153fcSJan Kara 	return err;
590321f97697SJan Kara out:
5904ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5905ca0e05e4SDmitry Monakhov }
5906ca0e05e4SDmitry Monakhov 
5907ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5908ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5909ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5910ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5911617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5912ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5913ac27a0ecSDave Kleikamp {
5914ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5915725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5916ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5917ac27a0ecSDave Kleikamp 	int tocopy;
5918ac27a0ecSDave Kleikamp 	size_t toread;
5919ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5920ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5921ac27a0ecSDave Kleikamp 
5922ac27a0ecSDave Kleikamp 	if (off > i_size)
5923ac27a0ecSDave Kleikamp 		return 0;
5924ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5925ac27a0ecSDave Kleikamp 		len = i_size-off;
5926ac27a0ecSDave Kleikamp 	toread = len;
5927ac27a0ecSDave Kleikamp 	while (toread > 0) {
5928ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5929ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
59301c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
59311c215028STheodore Ts'o 		if (IS_ERR(bh))
59321c215028STheodore Ts'o 			return PTR_ERR(bh);
5933ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5934ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5935ac27a0ecSDave Kleikamp 		else
5936ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5937ac27a0ecSDave Kleikamp 		brelse(bh);
5938ac27a0ecSDave Kleikamp 		offset = 0;
5939ac27a0ecSDave Kleikamp 		toread -= tocopy;
5940ac27a0ecSDave Kleikamp 		data += tocopy;
5941ac27a0ecSDave Kleikamp 		blk++;
5942ac27a0ecSDave Kleikamp 	}
5943ac27a0ecSDave Kleikamp 	return len;
5944ac27a0ecSDave Kleikamp }
5945ac27a0ecSDave Kleikamp 
5946ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5947ac27a0ecSDave Kleikamp  * enough credits) */
5948617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5949ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5950ac27a0ecSDave Kleikamp {
5951ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5952725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
59531c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5954c5e298aeSTheodore Ts'o 	int retries = 0;
5955ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5956ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5957ac27a0ecSDave Kleikamp 
59580390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5959b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5960b31e1552SEric Sandeen 			" cancelled because transaction is not started",
59619c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
59629c3013e9SJan Kara 		return -EIO;
59639c3013e9SJan Kara 	}
596467eeb568SDmitry Monakhov 	/*
596567eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
596667eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
596767eeb568SDmitry Monakhov 	 */
596867eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
596967eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
597067eeb568SDmitry Monakhov 			" cancelled because not block aligned",
597167eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
597267eeb568SDmitry Monakhov 		return -EIO;
597367eeb568SDmitry Monakhov 	}
597467eeb568SDmitry Monakhov 
5975c5e298aeSTheodore Ts'o 	do {
5976c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
5977c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
5978c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
5979c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
5980c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
59811c215028STheodore Ts'o 	if (IS_ERR(bh))
59821c215028STheodore Ts'o 		return PTR_ERR(bh);
5983ac27a0ecSDave Kleikamp 	if (!bh)
5984ac27a0ecSDave Kleikamp 		goto out;
59855d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
5986617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5987ac27a0ecSDave Kleikamp 	if (err) {
5988ac27a0ecSDave Kleikamp 		brelse(bh);
59891c215028STheodore Ts'o 		return err;
5990ac27a0ecSDave Kleikamp 	}
5991ac27a0ecSDave Kleikamp 	lock_buffer(bh);
599267eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5993ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5994ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
59950390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5996ac27a0ecSDave Kleikamp 	brelse(bh);
5997ac27a0ecSDave Kleikamp out:
599867eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
599967eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6000617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
6001617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
600221f97697SJan Kara 	}
600367eeb568SDmitry Monakhov 	return len;
6004ac27a0ecSDave Kleikamp }
6005ac27a0ecSDave Kleikamp 
60068f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
60078f0e8746STheodore Ts'o {
60088f0e8746STheodore Ts'o 	const struct quota_format_ops	*ops;
60098f0e8746STheodore Ts'o 
60108f0e8746STheodore Ts'o 	if (!sb_has_quota_loaded(sb, qid->type))
60118f0e8746STheodore Ts'o 		return -ESRCH;
60128f0e8746STheodore Ts'o 	ops = sb_dqopt(sb)->ops[qid->type];
60138f0e8746STheodore Ts'o 	if (!ops || !ops->get_next_id)
60148f0e8746STheodore Ts'o 		return -ENOSYS;
60158f0e8746STheodore Ts'o 	return dquot_get_next_id(sb, qid);
60168f0e8746STheodore Ts'o }
6017ac27a0ecSDave Kleikamp #endif
6018ac27a0ecSDave Kleikamp 
6019152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6020152a0836SAl Viro 		       const char *dev_name, void *data)
6021ac27a0ecSDave Kleikamp {
6022152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6023ac27a0ecSDave Kleikamp }
6024ac27a0ecSDave Kleikamp 
6025c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
602624b58424STheodore Ts'o static inline void register_as_ext2(void)
602724b58424STheodore Ts'o {
602824b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
602924b58424STheodore Ts'o 	if (err)
603024b58424STheodore Ts'o 		printk(KERN_WARNING
603124b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
603224b58424STheodore Ts'o }
603324b58424STheodore Ts'o 
603424b58424STheodore Ts'o static inline void unregister_as_ext2(void)
603524b58424STheodore Ts'o {
603624b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
603724b58424STheodore Ts'o }
60382035e776STheodore Ts'o 
60392035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
60402035e776STheodore Ts'o {
6041e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
60422035e776STheodore Ts'o 		return 0;
6043bc98a42cSDavid Howells 	if (sb_rdonly(sb))
60442035e776STheodore Ts'o 		return 1;
6045e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
60462035e776STheodore Ts'o 		return 0;
60472035e776STheodore Ts'o 	return 1;
60482035e776STheodore Ts'o }
604924b58424STheodore Ts'o #else
605024b58424STheodore Ts'o static inline void register_as_ext2(void) { }
605124b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
60522035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
605324b58424STheodore Ts'o #endif
605424b58424STheodore Ts'o 
605524b58424STheodore Ts'o static inline void register_as_ext3(void)
605624b58424STheodore Ts'o {
605724b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
605824b58424STheodore Ts'o 	if (err)
605924b58424STheodore Ts'o 		printk(KERN_WARNING
606024b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
606124b58424STheodore Ts'o }
606224b58424STheodore Ts'o 
606324b58424STheodore Ts'o static inline void unregister_as_ext3(void)
606424b58424STheodore Ts'o {
606524b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
606624b58424STheodore Ts'o }
60672035e776STheodore Ts'o 
60682035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
60692035e776STheodore Ts'o {
6070e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
60712035e776STheodore Ts'o 		return 0;
6072e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
60732035e776STheodore Ts'o 		return 0;
6074bc98a42cSDavid Howells 	if (sb_rdonly(sb))
60752035e776STheodore Ts'o 		return 1;
6076e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
60772035e776STheodore Ts'o 		return 0;
60782035e776STheodore Ts'o 	return 1;
60792035e776STheodore Ts'o }
608024b58424STheodore Ts'o 
608103010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6082ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
608303010a33STheodore Ts'o 	.name		= "ext4",
6084152a0836SAl Viro 	.mount		= ext4_mount,
6085ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6086ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6087ac27a0ecSDave Kleikamp };
60887f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6089ac27a0ecSDave Kleikamp 
6090e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6091e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6092e9e3bcecSEric Sandeen 
60935dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6094ac27a0ecSDave Kleikamp {
6095e9e3bcecSEric Sandeen 	int i, err;
6096c9de560dSAlex Tomas 
6097e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
609807c0c5d8SAl Viro 	ext4_li_info = NULL;
609907c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
610007c0c5d8SAl Viro 
61019a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
610212e9b892SDmitry Monakhov 	ext4_check_flag_values();
6103e9e3bcecSEric Sandeen 
6104e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6105e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6106e9e3bcecSEric Sandeen 
610751865fdaSZheng Liu 	err = ext4_init_es();
61086fd058f7STheodore Ts'o 	if (err)
61096fd058f7STheodore Ts'o 		return err;
611051865fdaSZheng Liu 
61111dc0aa46SEric Whitney 	err = ext4_init_pending();
61121dc0aa46SEric Whitney 	if (err)
611322cfe4b4SEric Biggers 		goto out7;
611422cfe4b4SEric Biggers 
611522cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
611622cfe4b4SEric Biggers 	if (err)
61171dc0aa46SEric Whitney 		goto out6;
61181dc0aa46SEric Whitney 
611951865fdaSZheng Liu 	err = ext4_init_pageio();
612051865fdaSZheng Liu 	if (err)
6121b5799018STheodore Ts'o 		goto out5;
612251865fdaSZheng Liu 
61235dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6124bd2d0210STheodore Ts'o 	if (err)
6125b5799018STheodore Ts'o 		goto out4;
6126857ac889SLukas Czerner 
6127b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6128dd68314cSTheodore Ts'o 	if (err)
6129b5799018STheodore Ts'o 		goto out3;
6130857ac889SLukas Czerner 
61315dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6132ac27a0ecSDave Kleikamp 	if (err)
6133c9de560dSAlex Tomas 		goto out2;
6134ac27a0ecSDave Kleikamp 	err = init_inodecache();
6135ac27a0ecSDave Kleikamp 	if (err)
6136ac27a0ecSDave Kleikamp 		goto out1;
613724b58424STheodore Ts'o 	register_as_ext3();
61382035e776STheodore Ts'o 	register_as_ext2();
613903010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6140ac27a0ecSDave Kleikamp 	if (err)
6141ac27a0ecSDave Kleikamp 		goto out;
6142bfff6873SLukas Czerner 
6143ac27a0ecSDave Kleikamp 	return 0;
6144ac27a0ecSDave Kleikamp out:
614524b58424STheodore Ts'o 	unregister_as_ext2();
614624b58424STheodore Ts'o 	unregister_as_ext3();
6147ac27a0ecSDave Kleikamp 	destroy_inodecache();
6148ac27a0ecSDave Kleikamp out1:
61495dabfc78STheodore Ts'o 	ext4_exit_mballoc();
61509c191f70ST Makphaibulchoke out2:
6151b5799018STheodore Ts'o 	ext4_exit_sysfs();
6152b5799018STheodore Ts'o out3:
6153dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6154b5799018STheodore Ts'o out4:
61555dabfc78STheodore Ts'o 	ext4_exit_pageio();
6156b5799018STheodore Ts'o out5:
615722cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
61581dc0aa46SEric Whitney out6:
615922cfe4b4SEric Biggers 	ext4_exit_pending();
616022cfe4b4SEric Biggers out7:
616151865fdaSZheng Liu 	ext4_exit_es();
616251865fdaSZheng Liu 
6163ac27a0ecSDave Kleikamp 	return err;
6164ac27a0ecSDave Kleikamp }
6165ac27a0ecSDave Kleikamp 
61665dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6167ac27a0ecSDave Kleikamp {
6168bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
616924b58424STheodore Ts'o 	unregister_as_ext2();
617024b58424STheodore Ts'o 	unregister_as_ext3();
617103010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6172ac27a0ecSDave Kleikamp 	destroy_inodecache();
61735dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6174b5799018STheodore Ts'o 	ext4_exit_sysfs();
61755dabfc78STheodore Ts'o 	ext4_exit_system_zone();
61765dabfc78STheodore Ts'o 	ext4_exit_pageio();
617722cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6178dd12ed14SEric Sandeen 	ext4_exit_es();
61791dc0aa46SEric Whitney 	ext4_exit_pending();
6180ac27a0ecSDave Kleikamp }
6181ac27a0ecSDave Kleikamp 
6182ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
618383982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6184ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
61857ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
61865dabfc78STheodore Ts'o module_init(ext4_init_fs)
61875dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6188