xref: /linux/fs/ext4/super.c (revision b925acb8f83d7ce3582b34018c05aae1be666abf)
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 
1348*b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1349*b925acb8SEric Biggers {
1350*b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1351*b925acb8SEric Biggers }
1352*b925acb8SEric Biggers 
1353*b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1354*b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1355*b925acb8SEric Biggers {
1356*b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1357*b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1358*b925acb8SEric Biggers }
1359*b925acb8SEric Biggers 
13606f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1361a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1362a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1363a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1364a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1365a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1366e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1367*b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1368*b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1369a7550b30SJaegeuk Kim };
1370a7550b30SJaegeuk Kim #endif
1371a7550b30SJaegeuk Kim 
1372ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1373d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1374689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1375ac27a0ecSDave Kleikamp 
1376617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1377617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1378617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1379617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1380617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
13816f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
13828c54ca9cSAl Viro 			 const struct path *path);
1383617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1384617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1385ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1386617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1387ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
13887c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
13897c319d32SAditya Kali 			     unsigned int flags);
13907c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
13918f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
1392ac27a0ecSDave Kleikamp 
139396c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
139496c7e0d9SJan Kara {
139596c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
139696c7e0d9SJan Kara }
139796c7e0d9SJan Kara 
139861e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
139960e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1400617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1401617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1402617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1403617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1404a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1405a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1406a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1407040cb378SLi Xi 	.get_projid		= ext4_get_projid,
14087a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
14098f0e8746STheodore Ts'o 	.get_next_id		= ext4_get_next_id,
1410ac27a0ecSDave Kleikamp };
1411ac27a0ecSDave Kleikamp 
14120d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1413617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1414ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1415287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
14160a240339SJan Kara 	.get_state	= dquot_get_state,
1417287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1418287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
14196332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
14206332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1421ac27a0ecSDave Kleikamp };
1422ac27a0ecSDave Kleikamp #endif
1423ac27a0ecSDave Kleikamp 
1424ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1425617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
142694053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1427617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1428617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1429617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
14307ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
14310930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1432617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1433617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1434c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1435c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1436617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1437617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1438617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1439ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1440617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1441617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
144296c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1443ac27a0ecSDave Kleikamp #endif
1444c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1445ac27a0ecSDave Kleikamp };
1446ac27a0ecSDave Kleikamp 
144739655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
14481b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
14491b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1450617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1451fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1452ac27a0ecSDave Kleikamp };
1453ac27a0ecSDave Kleikamp 
1454ac27a0ecSDave Kleikamp enum {
1455ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1456ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
145772578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1458ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
145972578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1460ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1461ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1462ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
14636ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1464ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
14655a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1466ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
146749da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
1468327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1469327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1470670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
14711449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
14725328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1473744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1474fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1475cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1476ac27a0ecSDave Kleikamp };
1477ac27a0ecSDave Kleikamp 
1478a447c093SSteven Whitehouse static const match_table_t tokens = {
1479ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1480ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1481ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1482ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1483ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1484ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1485ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1486ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1487ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1488ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1489ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1490ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1491ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1492ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
149372578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
149472578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1495ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1496ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1497ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1498ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1499e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
15005a916be1STheodore Ts'o 	{Opt_noload, "noload"},
150172578c33STheodore Ts'o 	{Opt_removed, "nobh"},
150272578c33STheodore Ts'o 	{Opt_removed, "bh"},
1503ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
150430773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
150530773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1506ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1507ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1508818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1509c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1510818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1511ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1512ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1513ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1514ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
15155bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
15165bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1517ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1518ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1519ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1520ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1521ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1522ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15235a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1524ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1525ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1526ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1527ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
152849da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1529ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
153006705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
153106705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
153225ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1533923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1534c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
153564769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1536327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1537327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1538a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1539a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1540670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1541dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
154236ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
154336ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15446fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15456fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1546240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1547b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1548afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
154906705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
155006705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1551744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1552744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
15535328e635SEric Sandeen 	{Opt_discard, "discard"},
15545328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1555fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1556fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1557fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1558df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
15596ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1560cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1561cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1562c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1563c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1564c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1565c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1566c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1567f3f12faaSJosef Bacik 	{Opt_err, NULL},
1568ac27a0ecSDave Kleikamp };
1569ac27a0ecSDave Kleikamp 
1570617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1571ac27a0ecSDave Kleikamp {
1572617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1573ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1574ac27a0ecSDave Kleikamp 
1575ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1576ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
15770b8e58a1SAndreas Dilger 
1578ac27a0ecSDave Kleikamp 	options += 3;
15790b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1580ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1581ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
15824776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1583ac27a0ecSDave Kleikamp 		       (char *) *data);
1584ac27a0ecSDave Kleikamp 		return 1;
1585ac27a0ecSDave Kleikamp 	}
1586ac27a0ecSDave Kleikamp 	if (*options == ',')
1587ac27a0ecSDave Kleikamp 		options++;
1588ac27a0ecSDave Kleikamp 	*data = (void *) options;
15890b8e58a1SAndreas Dilger 
1590ac27a0ecSDave Kleikamp 	return sb_block;
1591ac27a0ecSDave Kleikamp }
1592ac27a0ecSDave Kleikamp 
1593b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1594d6006186SEric Biggers static const char deprecated_msg[] =
1595d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1596437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1597b3881f74STheodore Ts'o 
159856c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
159956c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
160056c50f11SDmitry Monakhov {
160156c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
160233458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
160303dafb5fSChen Gang 	int ret = -1;
160456c50f11SDmitry Monakhov 
160533458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
160656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
160756c50f11SDmitry Monakhov 			"Cannot change journaled "
160856c50f11SDmitry Monakhov 			"quota options when quota turned on");
160957f73c2cSTheodore Ts'o 		return -1;
161056c50f11SDmitry Monakhov 	}
1611e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1612c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1613c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1614c325a67cSTheodore Ts'o 		return 1;
1615262b4662SJan Kara 	}
161656c50f11SDmitry Monakhov 	qname = match_strdup(args);
161756c50f11SDmitry Monakhov 	if (!qname) {
161856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
161956c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
162057f73c2cSTheodore Ts'o 		return -1;
162156c50f11SDmitry Monakhov 	}
162233458eabSTheodore Ts'o 	if (old_qname) {
162333458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
162403dafb5fSChen Gang 			ret = 1;
162503dafb5fSChen Gang 		else
162656c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
162703dafb5fSChen Gang 				 "%s quota file already specified",
162803dafb5fSChen Gang 				 QTYPE2NAME(qtype));
162903dafb5fSChen Gang 		goto errout;
163056c50f11SDmitry Monakhov 	}
163103dafb5fSChen Gang 	if (strchr(qname, '/')) {
163256c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
163356c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
163403dafb5fSChen Gang 		goto errout;
163556c50f11SDmitry Monakhov 	}
163633458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1637fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
163856c50f11SDmitry Monakhov 	return 1;
163903dafb5fSChen Gang errout:
164003dafb5fSChen Gang 	kfree(qname);
164103dafb5fSChen Gang 	return ret;
164256c50f11SDmitry Monakhov }
164356c50f11SDmitry Monakhov 
164456c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
164556c50f11SDmitry Monakhov {
164656c50f11SDmitry Monakhov 
164756c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
164833458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
164956c50f11SDmitry Monakhov 
165033458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
165156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
165256c50f11SDmitry Monakhov 			" when quota turned on");
165357f73c2cSTheodore Ts'o 		return -1;
165456c50f11SDmitry Monakhov 	}
165533458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
165633458eabSTheodore Ts'o 	synchronize_rcu();
165733458eabSTheodore Ts'o 	kfree(old_qname);
165856c50f11SDmitry Monakhov 	return 1;
165956c50f11SDmitry Monakhov }
166056c50f11SDmitry Monakhov #endif
166156c50f11SDmitry Monakhov 
166226092bf5STheodore Ts'o #define MOPT_SET	0x0001
166326092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
166426092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
166526092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
166626092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
166726092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
166826092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
166926092bf5STheodore Ts'o #define MOPT_Q		0
167026092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
167126092bf5STheodore Ts'o #else
167226092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
167326092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
167426092bf5STheodore Ts'o #endif
167526092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
16768dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
16778dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
16788dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1679ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
168026092bf5STheodore Ts'o 
168126092bf5STheodore Ts'o static const struct mount_opts {
168226092bf5STheodore Ts'o 	int	token;
168326092bf5STheodore Ts'o 	int	mount_opt;
168426092bf5STheodore Ts'o 	int	flags;
168526092bf5STheodore Ts'o } ext4_mount_opts[] = {
168626092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
168726092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
168826092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
168926092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
169026092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
169126092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
16928dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
16938dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
16948dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
16958dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
169626092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
169726092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
16988dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
16998dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17008dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
170159d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1702327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1703327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1704c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1705c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
17068dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
17071e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
170826092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
17098dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
17101e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17118dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
171226092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
171326092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
171426092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17158dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17167915a861SAles Novak 	 MOPT_NO_EXT2},
17178dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17187915a861SAles Novak 	 MOPT_NO_EXT2},
171926092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
172026092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
172126092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
172226092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
172326092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
172426092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
172526092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
172626092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
172726092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
172826092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1729923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
173026092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17310efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17320efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17335ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17345ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17355ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17368dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17378dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17388dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17398dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
174026092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
174126092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
174226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
174326092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
174426092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
174526092bf5STheodore Ts'o #else
174626092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
174726092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
174826092bf5STheodore Ts'o #endif
174926092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
175026092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1751670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
175226092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
175326092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
175426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
175526092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
175626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
175749da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
175849da9392SJan Kara 							MOPT_SET | MOPT_Q},
175926092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
176049da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
176149da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
176226092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
176326092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
176426092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
176526092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
176626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
176726092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
176826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1769df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
17706ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1771cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
177226092bf5STheodore Ts'o 	{Opt_err, 0, 0}
177326092bf5STheodore Ts'o };
177426092bf5STheodore Ts'o 
1775c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1776c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1777c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1778c83ad55eSGabriel Krisman Bertazi 	char *name;
1779c83ad55eSGabriel Krisman Bertazi 	char *version;
1780c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1781c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1782c83ad55eSGabriel Krisman Bertazi };
1783c83ad55eSGabriel Krisman Bertazi 
1784c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1785c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1786c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1787c83ad55eSGabriel Krisman Bertazi {
1788c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1789c83ad55eSGabriel Krisman Bertazi 	int i;
1790c83ad55eSGabriel Krisman Bertazi 
1791c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1792c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1793c83ad55eSGabriel Krisman Bertazi 			break;
1794c83ad55eSGabriel Krisman Bertazi 
1795c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1796c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1797c83ad55eSGabriel Krisman Bertazi 
1798c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1799c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1800c83ad55eSGabriel Krisman Bertazi 
1801c83ad55eSGabriel Krisman Bertazi 	return 0;
1802c83ad55eSGabriel Krisman Bertazi }
1803c83ad55eSGabriel Krisman Bertazi #endif
1804c83ad55eSGabriel Krisman Bertazi 
180526092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
180626092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
180726092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
180826092bf5STheodore Ts'o {
180926092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
181026092bf5STheodore Ts'o 	const struct mount_opts *m;
181108cefc7aSEric W. Biederman 	kuid_t uid;
181208cefc7aSEric W. Biederman 	kgid_t gid;
181326092bf5STheodore Ts'o 	int arg = 0;
181426092bf5STheodore Ts'o 
181557f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
181657f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
181757f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
181857f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
181957f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
182057f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
182157f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
182257f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
182357f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
182457f73c2cSTheodore Ts'o #endif
182526092bf5STheodore Ts'o 	switch (token) {
1826f7048605STheodore Ts'o 	case Opt_noacl:
1827f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1828f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1829f7048605STheodore Ts'o 		break;
183026092bf5STheodore Ts'o 	case Opt_sb:
183126092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
183226092bf5STheodore Ts'o 	case Opt_removed:
18335f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
183426092bf5STheodore Ts'o 		return 1;
183526092bf5STheodore Ts'o 	case Opt_abort:
183626092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
183726092bf5STheodore Ts'o 		return 1;
183826092bf5STheodore Ts'o 	case Opt_i_version:
1839357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
184026092bf5STheodore Ts'o 		return 1;
1841a26f4992STheodore Ts'o 	case Opt_lazytime:
18421751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1843a26f4992STheodore Ts'o 		return 1;
1844a26f4992STheodore Ts'o 	case Opt_nolazytime:
18451751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1846a26f4992STheodore Ts'o 		return 1;
184726092bf5STheodore Ts'o 	}
184826092bf5STheodore Ts'o 
18495f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
18505f3633e3SJan Kara 		if (token == m->token)
18515f3633e3SJan Kara 			break;
18525f3633e3SJan Kara 
18535f3633e3SJan Kara 	if (m->token == Opt_err) {
18545f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
18555f3633e3SJan Kara 			 "or missing value", opt);
18565f3633e3SJan Kara 		return -1;
18575f3633e3SJan Kara 	}
18585f3633e3SJan Kara 
18598dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
18608dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
18618dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
18628dc0aa8cSTheodore Ts'o 		return -1;
18638dc0aa8cSTheodore Ts'o 	}
18648dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
18658dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
18668dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
18678dc0aa8cSTheodore Ts'o 		return -1;
18688dc0aa8cSTheodore Ts'o 	}
18698dc0aa8cSTheodore Ts'o 
1870ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
18710efb3b23SJan Kara 		return -1;
187226092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
187326092bf5STheodore Ts'o 		return -1;
1874c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1875c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
187626092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
18771e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
18781e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1879c93cf2d7SDmitry Monakhov 		} else
1880c93cf2d7SDmitry Monakhov 			return -1;
1881c93cf2d7SDmitry Monakhov 	}
188226092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
188326092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
188426092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
188526092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
188626092bf5STheodore Ts'o 			 "options when quota turned on");
188726092bf5STheodore Ts'o 		return -1;
188826092bf5STheodore Ts'o 	}
188926092bf5STheodore Ts'o 
189026092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
189126092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
189226092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
189326092bf5STheodore Ts'o 		if (arg == 0)
189426092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
18959ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
18969ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
18979ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
18989ba55543Szhangyi (F) 				 "must be smaller than %d",
18999ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
19009ba55543Szhangyi (F) 			return -1;
19019ba55543Szhangyi (F) 		}
190226092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1903670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
1904670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
190526092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
190626092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
190726092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
190826092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
190926092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1910e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1911e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1912e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1913e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
191426092bf5STheodore Ts'o 			return -1;
191526092bf5STheodore Ts'o 		}
191626092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
191726092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
191826092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
191926092bf5STheodore Ts'o 		if (!args->from)
192026092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
192126092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1922df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1923df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
192426092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
192526092bf5STheodore Ts'o 		sbi->s_stripe = arg;
19260efb3b23SJan Kara 	} else if (token == Opt_resuid) {
19270efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
19280efb3b23SJan Kara 		if (!uid_valid(uid)) {
19295f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
19300efb3b23SJan Kara 			return -1;
19310efb3b23SJan Kara 		}
19320efb3b23SJan Kara 		sbi->s_resuid = uid;
19330efb3b23SJan Kara 	} else if (token == Opt_resgid) {
19340efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
19350efb3b23SJan Kara 		if (!gid_valid(gid)) {
19365f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
19370efb3b23SJan Kara 			return -1;
19380efb3b23SJan Kara 		}
19390efb3b23SJan Kara 		sbi->s_resgid = gid;
19400efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
19410efb3b23SJan Kara 		if (is_remount) {
19420efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
19430efb3b23SJan Kara 				 "Cannot specify journal on remount");
19440efb3b23SJan Kara 			return -1;
19450efb3b23SJan Kara 		}
19460efb3b23SJan Kara 		*journal_devnum = arg;
1947ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1948ad4eec61SEric Sandeen 		char *journal_path;
1949ad4eec61SEric Sandeen 		struct inode *journal_inode;
1950ad4eec61SEric Sandeen 		struct path path;
1951ad4eec61SEric Sandeen 		int error;
1952ad4eec61SEric Sandeen 
1953ad4eec61SEric Sandeen 		if (is_remount) {
1954ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1955ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1956ad4eec61SEric Sandeen 			return -1;
1957ad4eec61SEric Sandeen 		}
1958ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1959ad4eec61SEric Sandeen 		if (!journal_path) {
1960ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1961ad4eec61SEric Sandeen 				"journal device string");
1962ad4eec61SEric Sandeen 			return -1;
1963ad4eec61SEric Sandeen 		}
1964ad4eec61SEric Sandeen 
1965ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1966ad4eec61SEric Sandeen 		if (error) {
1967ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1968ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1969ad4eec61SEric Sandeen 			kfree(journal_path);
1970ad4eec61SEric Sandeen 			return -1;
1971ad4eec61SEric Sandeen 		}
1972ad4eec61SEric Sandeen 
19732b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
1974ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1975ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1976ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1977ad4eec61SEric Sandeen 			path_put(&path);
1978ad4eec61SEric Sandeen 			kfree(journal_path);
1979ad4eec61SEric Sandeen 			return -1;
1980ad4eec61SEric Sandeen 		}
1981ad4eec61SEric Sandeen 
1982ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1983ad4eec61SEric Sandeen 		path_put(&path);
1984ad4eec61SEric Sandeen 		kfree(journal_path);
19850efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
19860efb3b23SJan Kara 		if (arg > 7) {
19875f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
19880efb3b23SJan Kara 				 " (must be 0-7)");
19890efb3b23SJan Kara 			return -1;
19900efb3b23SJan Kara 		}
19910efb3b23SJan Kara 		*journal_ioprio =
19920efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
19936ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
1994643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
19956ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
19966ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
19976ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
19986ddb2447STheodore Ts'o #else
19996ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20006ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
20016ddb2447STheodore Ts'o #endif
200226092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
200326092bf5STheodore Ts'o 		if (is_remount) {
200426092bf5STheodore Ts'o 			if (!sbi->s_journal)
200526092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
20065f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
200726092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
200826092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
200926092bf5STheodore Ts'o 				return -1;
201026092bf5STheodore Ts'o 			}
201126092bf5STheodore Ts'o 		} else {
201226092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
201326092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
201426092bf5STheodore Ts'o 		}
201526092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
201626092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
201726092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
201826092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
20195f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
20205f3633e3SJan Kara 				 "quota options when quota turned on");
202126092bf5STheodore Ts'o 			return -1;
202226092bf5STheodore Ts'o 		}
2023e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2024c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2025c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2026262b4662SJan Kara 				 "when QUOTA feature is enabled");
2027c325a67cSTheodore Ts'o 			return 1;
2028262b4662SJan Kara 		}
202926092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
203026092bf5STheodore Ts'o #endif
2031923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
2032ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2033ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
2034ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2035ef83b6e8SDan Williams 		sbi->s_mount_opt |= m->mount_opt;
2036ef83b6e8SDan Williams #else
2037923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2038923ae0ffSRoss Zwisler 		return -1;
2039923ae0ffSRoss Zwisler #endif
20407915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
20417915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
20427915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
20437915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
204426092bf5STheodore Ts'o 	} else {
204526092bf5STheodore Ts'o 		if (!args->from)
204626092bf5STheodore Ts'o 			arg = 1;
204726092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
204826092bf5STheodore Ts'o 			arg = !arg;
204926092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
205026092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
205126092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
205226092bf5STheodore Ts'o 			WARN_ON(1);
205326092bf5STheodore Ts'o 			return -1;
205426092bf5STheodore Ts'o 		}
205526092bf5STheodore Ts'o 		if (arg != 0)
205626092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
205726092bf5STheodore Ts'o 		else
205826092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
205926092bf5STheodore Ts'o 	}
206026092bf5STheodore Ts'o 	return 1;
206126092bf5STheodore Ts'o }
206226092bf5STheodore Ts'o 
2063ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2064c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2065b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2066661aa520SEric Sandeen 			 int is_remount)
2067ac27a0ecSDave Kleikamp {
2068617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
206933458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2070ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
207126092bf5STheodore Ts'o 	int token;
2072ac27a0ecSDave Kleikamp 
2073ac27a0ecSDave Kleikamp 	if (!options)
2074ac27a0ecSDave Kleikamp 		return 1;
2075ac27a0ecSDave Kleikamp 
2076ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2077ac27a0ecSDave Kleikamp 		if (!*p)
2078ac27a0ecSDave Kleikamp 			continue;
207915121c18SEric Sandeen 		/*
208015121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
208115121c18SEric Sandeen 		 * found; some options take optional arguments.
208215121c18SEric Sandeen 		 */
2083caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2084ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
208526092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
208626092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2087ac27a0ecSDave Kleikamp 			return 0;
2088ac27a0ecSDave Kleikamp 	}
2089ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
209049da9392SJan Kara 	/*
209149da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
209249da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
209349da9392SJan Kara 	 * to support legacy quotas in quota files.
209449da9392SJan Kara 	 */
209549da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
209649da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
209749da9392SJan Kara 			 "Cannot enable project quota enforcement.");
209849da9392SJan Kara 		return 0;
209949da9392SJan Kara 	}
210033458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
210133458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
210233458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
210333458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2104fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2105ac27a0ecSDave Kleikamp 
210633458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2107fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2108ac27a0ecSDave Kleikamp 
210956c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2110b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2111b31e1552SEric Sandeen 					"format mixing");
2112ac27a0ecSDave Kleikamp 			return 0;
2113ac27a0ecSDave Kleikamp 		}
2114ac27a0ecSDave Kleikamp 
2115ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2116b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2117b31e1552SEric Sandeen 					"not specified");
2118ac27a0ecSDave Kleikamp 			return 0;
2119ac27a0ecSDave Kleikamp 		}
2120ac27a0ecSDave Kleikamp 	}
2121ac27a0ecSDave Kleikamp #endif
2122261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2123261cb20cSJan Kara 		int blocksize =
2124261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2125261cb20cSJan Kara 
212609cbfeafSKirill A. Shutemov 		if (blocksize < PAGE_SIZE) {
2127261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
2128261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
2129261cb20cSJan Kara 			return 0;
2130261cb20cSJan Kara 		}
2131261cb20cSJan Kara 	}
2132ac27a0ecSDave Kleikamp 	return 1;
2133ac27a0ecSDave Kleikamp }
2134ac27a0ecSDave Kleikamp 
21352adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
21362adf6da8STheodore Ts'o 					   struct super_block *sb)
21372adf6da8STheodore Ts'o {
21382adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
21392adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
214033458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
21412adf6da8STheodore Ts'o 
21422adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
21432adf6da8STheodore Ts'o 		char *fmtname = "";
21442adf6da8STheodore Ts'o 
21452adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
21462adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
21472adf6da8STheodore Ts'o 			fmtname = "vfsold";
21482adf6da8STheodore Ts'o 			break;
21492adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
21502adf6da8STheodore Ts'o 			fmtname = "vfsv0";
21512adf6da8STheodore Ts'o 			break;
21522adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
21532adf6da8STheodore Ts'o 			fmtname = "vfsv1";
21542adf6da8STheodore Ts'o 			break;
21552adf6da8STheodore Ts'o 		}
21562adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
21572adf6da8STheodore Ts'o 	}
21582adf6da8STheodore Ts'o 
215933458eabSTheodore Ts'o 	rcu_read_lock();
216033458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
216133458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
216233458eabSTheodore Ts'o 	if (usr_qf_name)
216333458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
216433458eabSTheodore Ts'o 	if (grp_qf_name)
216533458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
216633458eabSTheodore Ts'o 	rcu_read_unlock();
21672adf6da8STheodore Ts'o #endif
21682adf6da8STheodore Ts'o }
21692adf6da8STheodore Ts'o 
21705a916be1STheodore Ts'o static const char *token2str(int token)
21715a916be1STheodore Ts'o {
217250df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
21735a916be1STheodore Ts'o 
21745a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
21755a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
21765a916be1STheodore Ts'o 			break;
21775a916be1STheodore Ts'o 	return t->pattern;
21785a916be1STheodore Ts'o }
21795a916be1STheodore Ts'o 
21802adf6da8STheodore Ts'o /*
21812adf6da8STheodore Ts'o  * Show an option if
21822adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
21832adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
21842adf6da8STheodore Ts'o  */
218566acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
218666acdcf4STheodore Ts'o 			      int nodefs)
21872adf6da8STheodore Ts'o {
21882adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
21892adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
219068afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
21915a916be1STheodore Ts'o 	const struct mount_opts *m;
219266acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
21932adf6da8STheodore Ts'o 
219466acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
219566acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
21962adf6da8STheodore Ts'o 
21972adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
21985a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
21995a916be1STheodore Ts'o 
22005a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
22015a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
22025a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
22035a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
22045a916be1STheodore Ts'o 			continue;
220568afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
22065a916be1STheodore Ts'o 			continue; /* skip if same as the default */
22075a916be1STheodore Ts'o 		if ((want_set &&
22085a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
22095a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
22105a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
22115a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
22125a916be1STheodore Ts'o 	}
22135a916be1STheodore Ts'o 
221408cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
22155a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
221608cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
221708cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
221808cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
22195a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
222008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
222108cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
222266acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
22235a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
22245a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
22252adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
22265a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
22272adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
22285a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
222966acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
22305a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
223166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
22325a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
223366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
22345a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2235357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
22365a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
223766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
22385a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
223968afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
224068afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
22412adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
22425a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
22432adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
22445a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
22452adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
22465a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
22475a916be1STheodore Ts'o 	}
224866acdcf4STheodore Ts'o 	if (nodefs ||
224966acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
22505a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
22512adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
22522adf6da8STheodore Ts'o 
2253ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
225466acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
22555a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2256df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2257df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
22587915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
22597915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2260338affb5SEric Biggers 	if (DUMMY_ENCRYPTION_ENABLED(sbi))
2261338affb5SEric Biggers 		SEQ_OPTS_PUTS("test_dummy_encryption");
22622adf6da8STheodore Ts'o 
22632adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
22642adf6da8STheodore Ts'o 	return 0;
22652adf6da8STheodore Ts'o }
22662adf6da8STheodore Ts'o 
226766acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
226866acdcf4STheodore Ts'o {
226966acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
227066acdcf4STheodore Ts'o }
227166acdcf4STheodore Ts'o 
2272ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
227366acdcf4STheodore Ts'o {
227466acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
227566acdcf4STheodore Ts'o 	int rc;
227666acdcf4STheodore Ts'o 
2277bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
227866acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
227966acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
228066acdcf4STheodore Ts'o 	return rc;
228166acdcf4STheodore Ts'o }
228266acdcf4STheodore Ts'o 
2283617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2284ac27a0ecSDave Kleikamp 			    int read_only)
2285ac27a0ecSDave Kleikamp {
2286617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2287c89128a0SJaegeuk Kim 	int err = 0;
2288ac27a0ecSDave Kleikamp 
2289617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2290b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2291b31e1552SEric Sandeen 			 "forcing read-only mode");
2292c89128a0SJaegeuk Kim 		err = -EROFS;
2293ac27a0ecSDave Kleikamp 	}
2294ac27a0ecSDave Kleikamp 	if (read_only)
2295281b5995STheodore Ts'o 		goto done;
2296617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2297b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2298b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2299c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2300b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2301b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2302b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2303ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2304ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2305ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2306b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2307b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2308b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2309ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
23106a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
23116a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2312b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2313b31e1552SEric Sandeen 			 "warning: checktime reached, "
2314b31e1552SEric Sandeen 			 "running e2fsck is recommended");
23150390131bSFrank Mayhar 	if (!sbi->s_journal)
2316216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2317ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2318617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2319e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
23206a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
23210390131bSFrank Mayhar 	if (sbi->s_journal)
2322e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2323ac27a0ecSDave Kleikamp 
2324c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2325281b5995STheodore Ts'o done:
2326ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2327a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2328a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2329ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2330ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2331617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2332617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2333a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2334ac27a0ecSDave Kleikamp 
23357abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2336c89128a0SJaegeuk Kim 	return err;
2337ac27a0ecSDave Kleikamp }
2338ac27a0ecSDave Kleikamp 
2339117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2340117fff10STheodore Ts'o {
2341117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2342117fff10STheodore Ts'o 	struct flex_groups *new_groups;
2343117fff10STheodore Ts'o 	int size;
2344117fff10STheodore Ts'o 
2345117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2346117fff10STheodore Ts'o 		return 0;
2347117fff10STheodore Ts'o 
2348117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2349117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2350117fff10STheodore Ts'o 		return 0;
2351117fff10STheodore Ts'o 
2352117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2353a7c3e901SMichal Hocko 	new_groups = kvzalloc(size, GFP_KERNEL);
2354117fff10STheodore Ts'o 	if (!new_groups) {
2355117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2356117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
2357117fff10STheodore Ts'o 		return -ENOMEM;
2358117fff10STheodore Ts'o 	}
2359117fff10STheodore Ts'o 
2360117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
2361117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
2362117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
2363117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
2364b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
2365117fff10STheodore Ts'o 	}
2366117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
2367117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
2368117fff10STheodore Ts'o 	return 0;
2369117fff10STheodore Ts'o }
2370117fff10STheodore Ts'o 
2371772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2372772cb7c8SJose R. Santos {
2373772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2374772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2375772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2376117fff10STheodore Ts'o 	int i, err;
2377772cb7c8SJose R. Santos 
2378503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2379d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2380772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2381772cb7c8SJose R. Santos 		return 1;
2382772cb7c8SJose R. Santos 	}
2383772cb7c8SJose R. Santos 
2384117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2385117fff10STheodore Ts'o 	if (err)
2386772cb7c8SJose R. Santos 		goto failed;
2387772cb7c8SJose R. Santos 
2388772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
238988b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2390772cb7c8SJose R. Santos 
2391772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
23927ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
23937ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
239490ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
239524aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
23967ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
23977ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2398772cb7c8SJose R. Santos 	}
2399772cb7c8SJose R. Santos 
2400772cb7c8SJose R. Santos 	return 1;
2401772cb7c8SJose R. Santos failed:
2402772cb7c8SJose R. Santos 	return 0;
2403772cb7c8SJose R. Santos }
2404772cb7c8SJose R. Santos 
2405e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2406717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2407717d50e4SAndreas Dilger {
2408b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2409717d50e4SAndreas Dilger 	__u16 crc = 0;
2410717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2411e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2412717d50e4SAndreas Dilger 
24139aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2414feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2415feb0ab32SDarrick J. Wong 		__u32 csum32;
2416b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2417feb0ab32SDarrick J. Wong 
2418feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2419feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2420b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2421b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2422b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2423b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2424b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2425b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2426b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2427feb0ab32SDarrick J. Wong 
2428feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2429feb0ab32SDarrick J. Wong 		goto out;
2430feb0ab32SDarrick J. Wong 	}
2431feb0ab32SDarrick J. Wong 
2432feb0ab32SDarrick J. Wong 	/* old crc16 code */
2433e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2434813d32f9SDarrick J. Wong 		return 0;
2435813d32f9SDarrick J. Wong 
2436717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2437717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2438717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2439717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2440717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2441e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2442717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2443717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2444717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2445717d50e4SAndreas Dilger 				offset);
2446717d50e4SAndreas Dilger 
2447feb0ab32SDarrick J. Wong out:
2448717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2449717d50e4SAndreas Dilger }
2450717d50e4SAndreas Dilger 
2451feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2452717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2453717d50e4SAndreas Dilger {
2454feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2455e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2456717d50e4SAndreas Dilger 		return 0;
2457717d50e4SAndreas Dilger 
2458717d50e4SAndreas Dilger 	return 1;
2459717d50e4SAndreas Dilger }
2460717d50e4SAndreas Dilger 
2461feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2462feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2463feb0ab32SDarrick J. Wong {
2464feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2465feb0ab32SDarrick J. Wong 		return;
2466e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2467feb0ab32SDarrick J. Wong }
2468feb0ab32SDarrick J. Wong 
2469ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2470bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2471829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2472bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2473ac27a0ecSDave Kleikamp {
2474617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2475617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2476617ba13bSMingming Cao 	ext4_fsblk_t last_block;
247744de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2478bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2479bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2480bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2481ce421581SJose R. Santos 	int flexbg_flag = 0;
2482bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2483ac27a0ecSDave Kleikamp 
2484e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2485ce421581SJose R. Santos 		flexbg_flag = 1;
2486ce421581SJose R. Santos 
2487617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2488ac27a0ecSDave Kleikamp 
2489197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2490197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2491197cd65aSAkinobu Mita 
2492ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2493bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2494ac27a0ecSDave Kleikamp 		else
2495ac27a0ecSDave Kleikamp 			last_block = first_block +
2496617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2497ac27a0ecSDave Kleikamp 
2498bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2499bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2500bfff6873SLukas Czerner 			grp = i;
2501bfff6873SLukas Czerner 
25028fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2503829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2504829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2505829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2506829fa70dSTheodore Ts'o 				 "superblock", i);
250718db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
250818db4b4eSTheodore Ts'o 				return 0;
2509829fa70dSTheodore Ts'o 		}
251077260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
251177260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
251277260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
251377260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
251477260807STheodore Ts'o 				 "block group descriptors", i);
251577260807STheodore Ts'o 			if (!sb_rdonly(sb))
251677260807STheodore Ts'o 				return 0;
251777260807STheodore Ts'o 		}
25182b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2519b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2520a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2521b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2522ac27a0ecSDave Kleikamp 			return 0;
2523ac27a0ecSDave Kleikamp 		}
25248fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2525829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2526829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2527829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2528829fa70dSTheodore Ts'o 				 "superblock", i);
252918db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
253018db4b4eSTheodore Ts'o 				return 0;
2531829fa70dSTheodore Ts'o 		}
253277260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
253377260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
253477260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
253577260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
253677260807STheodore Ts'o 				 "block group descriptors", i);
253777260807STheodore Ts'o 			if (!sb_rdonly(sb))
253877260807STheodore Ts'o 				return 0;
253977260807STheodore Ts'o 		}
25402b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2541b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2542a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2543b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2544ac27a0ecSDave Kleikamp 			return 0;
2545ac27a0ecSDave Kleikamp 		}
25468fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2547829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2548829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2549829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2550829fa70dSTheodore Ts'o 				 "superblock", i);
255118db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
255218db4b4eSTheodore Ts'o 				return 0;
2553829fa70dSTheodore Ts'o 		}
255477260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
255577260807STheodore Ts'o 		    inode_table <= last_bg_block) {
255677260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
255777260807STheodore Ts'o 				 "Inode table for group %u overlaps "
255877260807STheodore Ts'o 				 "block group descriptors", i);
255977260807STheodore Ts'o 			if (!sb_rdonly(sb))
256077260807STheodore Ts'o 				return 0;
256177260807STheodore Ts'o 		}
2562bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
25632b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2564b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2565a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2566b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2567ac27a0ecSDave Kleikamp 			return 0;
2568ac27a0ecSDave Kleikamp 		}
2569955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2570feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2571b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2572b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2573e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2574fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2575bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2576955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2577717d50e4SAndreas Dilger 				return 0;
2578717d50e4SAndreas Dilger 			}
25797ee1ec4cSLi Zefan 		}
2580955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2581ce421581SJose R. Santos 		if (!flexbg_flag)
2582617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2583ac27a0ecSDave Kleikamp 	}
2584bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2585bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2586ac27a0ecSDave Kleikamp 	return 1;
2587ac27a0ecSDave Kleikamp }
2588ac27a0ecSDave Kleikamp 
2589617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2590ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2591ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2592ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2593ac27a0ecSDave Kleikamp  *
2594ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2595ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2596ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2597ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2598ac27a0ecSDave Kleikamp  *
2599ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2600ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2601ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2602617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2603ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2604ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2605ac27a0ecSDave Kleikamp  */
2606617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2607617ba13bSMingming Cao 				struct ext4_super_block *es)
2608ac27a0ecSDave Kleikamp {
2609ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
26102c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2611ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
261295f1fda4Szhangyi (F) 	int quota_update = 0;
2613ac27a0ecSDave Kleikamp 	int i;
2614ac27a0ecSDave Kleikamp #endif
2615ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2616ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2617ac27a0ecSDave Kleikamp 		return;
2618ac27a0ecSDave Kleikamp 	}
2619ac27a0ecSDave Kleikamp 
2620a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2621b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2622b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2623a8f48a95SEric Sandeen 		return;
2624a8f48a95SEric Sandeen 	}
2625a8f48a95SEric Sandeen 
2626d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2627d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2628d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2629d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2630d39195c3SAmir Goldstein 		return;
2631d39195c3SAmir Goldstein 	}
2632d39195c3SAmir Goldstein 
2633617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2634c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
26351751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
263684474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2637ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2638ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2639c25f9bc6SEric Sandeen 		}
2640ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2641ac27a0ecSDave Kleikamp 		return;
2642ac27a0ecSDave Kleikamp 	}
2643ac27a0ecSDave Kleikamp 
26441751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2645b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
26461751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2647ac27a0ecSDave Kleikamp 	}
2648ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2649ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
26501751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
265195f1fda4Szhangyi (F) 
265295f1fda4Szhangyi (F) 	/*
265395f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
265495f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
265595f1fda4Szhangyi (F) 	 */
26561751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
265795f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
265895f1fda4Szhangyi (F) 
265995f1fda4Szhangyi (F) 		if (!ret)
266095f1fda4Szhangyi (F) 			quota_update = 1;
266195f1fda4Szhangyi (F) 		else
266295f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
266395f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
266495f1fda4Szhangyi (F) 	}
266595f1fda4Szhangyi (F) 
266695f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2667a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2668617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2669617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
267095f1fda4Szhangyi (F) 
267195f1fda4Szhangyi (F) 			if (!ret)
267295f1fda4Szhangyi (F) 				quota_update = 1;
267395f1fda4Szhangyi (F) 			else
2674b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2675b31e1552SEric Sandeen 					"Cannot turn on journaled "
267695f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2677ac27a0ecSDave Kleikamp 		}
2678ac27a0ecSDave Kleikamp 	}
2679ac27a0ecSDave Kleikamp #endif
2680ac27a0ecSDave Kleikamp 
2681ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2682ac27a0ecSDave Kleikamp 		struct inode *inode;
2683ac27a0ecSDave Kleikamp 
2684c65d5c6cSVegard Nossum 		/*
2685c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2686c65d5c6cSVegard Nossum 		 * so, skip the rest.
2687c65d5c6cSVegard Nossum 		 */
2688c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2689c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2690c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2691c65d5c6cSVegard Nossum 			break;
2692c65d5c6cSVegard Nossum 		}
2693c65d5c6cSVegard Nossum 
269497bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
269597bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2696ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2697ac27a0ecSDave Kleikamp 			break;
2698ac27a0ecSDave Kleikamp 		}
2699ac27a0ecSDave Kleikamp 
2700617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2701871a2931SChristoph Hellwig 		dquot_initialize(inode);
2702ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2703566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2704b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2705b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
270646e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2707e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2708ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
27095955102cSAl Viro 			inode_lock(inode);
271055f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
27112c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
27122c98eb5eSTheodore Ts'o 			if (ret)
27132c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
27145955102cSAl Viro 			inode_unlock(inode);
2715ac27a0ecSDave Kleikamp 			nr_truncates++;
2716ac27a0ecSDave Kleikamp 		} else {
2717566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2718b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2719b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
272046e665e9SHarvey Harrison 					__func__, inode->i_ino);
2721ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2722ac27a0ecSDave Kleikamp 				  inode->i_ino);
2723ac27a0ecSDave Kleikamp 			nr_orphans++;
2724ac27a0ecSDave Kleikamp 		}
2725ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2726ac27a0ecSDave Kleikamp 	}
2727ac27a0ecSDave Kleikamp 
2728ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2729ac27a0ecSDave Kleikamp 
2730ac27a0ecSDave Kleikamp 	if (nr_orphans)
2731b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2732b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2733ac27a0ecSDave Kleikamp 	if (nr_truncates)
2734b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2735b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2736ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
273795f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
273895f1fda4Szhangyi (F) 	if (quota_update) {
2739a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
274095f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2741287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2742ac27a0ecSDave Kleikamp 		}
274395f1fda4Szhangyi (F) 	}
2744ac27a0ecSDave Kleikamp #endif
27451751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2746ac27a0ecSDave Kleikamp }
27470b8e58a1SAndreas Dilger 
2748cd2291a4SEric Sandeen /*
2749cd2291a4SEric Sandeen  * Maximal extent format file size.
2750cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2751cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2752cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2753cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2754cd2291a4SEric Sandeen  *
2755f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2756f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2757f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2758f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2759f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2760f17722f9SLukas Czerner  *
2761cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2762cd2291a4SEric Sandeen  */
2763f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2764cd2291a4SEric Sandeen {
2765cd2291a4SEric Sandeen 	loff_t res;
2766cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2767cd2291a4SEric Sandeen 
276872deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
276972deb455SChristoph Hellwig 
277072deb455SChristoph Hellwig 	if (!has_huge_files) {
2771cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2772cd2291a4SEric Sandeen 
2773cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2774cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2775cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2776cd2291a4SEric Sandeen 	}
2777cd2291a4SEric Sandeen 
2778f17722f9SLukas Czerner 	/*
2779f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2780f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2781f17722f9SLukas Czerner 	 * size
2782f17722f9SLukas Czerner 	 */
2783f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2784cd2291a4SEric Sandeen 	res <<= blkbits;
2785cd2291a4SEric Sandeen 
2786cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2787cd2291a4SEric Sandeen 	if (res > upper_limit)
2788cd2291a4SEric Sandeen 		res = upper_limit;
2789cd2291a4SEric Sandeen 
2790cd2291a4SEric Sandeen 	return res;
2791cd2291a4SEric Sandeen }
2792ac27a0ecSDave Kleikamp 
2793ac27a0ecSDave Kleikamp /*
2794cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
27950fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
27960fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2797ac27a0ecSDave Kleikamp  */
2798f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2799ac27a0ecSDave Kleikamp {
2800617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
28010fc1b451SAneesh Kumar K.V 	int meta_blocks;
28020fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
28030b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
28040b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
28050b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
28060b8e58a1SAndreas Dilger 	 *
28070b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
28080b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
28090fc1b451SAneesh Kumar K.V 	 */
28100fc1b451SAneesh Kumar K.V 
281172deb455SChristoph Hellwig 	if (!has_huge_files) {
28120fc1b451SAneesh Kumar K.V 		/*
281372deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
281472deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
281572deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
28160fc1b451SAneesh Kumar K.V 		 */
28170fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
28180fc1b451SAneesh Kumar K.V 
28190fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
28200fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
28210fc1b451SAneesh Kumar K.V 
28220fc1b451SAneesh Kumar K.V 	} else {
28238180a562SAneesh Kumar K.V 		/*
28248180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
28258180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
28268180a562SAneesh Kumar K.V 		 * represent total number of blocks in
28278180a562SAneesh Kumar K.V 		 * file system block size
28288180a562SAneesh Kumar K.V 		 */
28290fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
28300fc1b451SAneesh Kumar K.V 
28310fc1b451SAneesh Kumar K.V 	}
28320fc1b451SAneesh Kumar K.V 
28330fc1b451SAneesh Kumar K.V 	/* indirect blocks */
28340fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
28350fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
28360fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
28370fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
28380fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
28390fc1b451SAneesh Kumar K.V 
28400fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
28410fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2842ac27a0ecSDave Kleikamp 
2843ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2844ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2845ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2846ac27a0ecSDave Kleikamp 	res <<= bits;
2847ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2848ac27a0ecSDave Kleikamp 		res = upper_limit;
28490fc1b451SAneesh Kumar K.V 
28500fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
28510fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
28520fc1b451SAneesh Kumar K.V 
2853ac27a0ecSDave Kleikamp 	return res;
2854ac27a0ecSDave Kleikamp }
2855ac27a0ecSDave Kleikamp 
2856617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
285770bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2858ac27a0ecSDave Kleikamp {
2859617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2860fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2861ac27a0ecSDave Kleikamp 	int has_super = 0;
2862ac27a0ecSDave Kleikamp 
2863ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2864ac27a0ecSDave Kleikamp 
2865e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
286670bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2867ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2868617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2869ac27a0ecSDave Kleikamp 		has_super = 1;
28700b8e58a1SAndreas Dilger 
2871bd63f6b0SDarrick J. Wong 	/*
2872bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2873bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2874bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2875bd63f6b0SDarrick J. Wong 	 * compensate.
2876bd63f6b0SDarrick J. Wong 	 */
2877bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
287849598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
2879bd63f6b0SDarrick J. Wong 		has_super++;
2880bd63f6b0SDarrick J. Wong 
2881617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2882ac27a0ecSDave Kleikamp }
2883ac27a0ecSDave Kleikamp 
2884c9de560dSAlex Tomas /**
2885c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2886c9de560dSAlex Tomas  * @sbi: In memory super block info
2887c9de560dSAlex Tomas  *
2888c9de560dSAlex Tomas  * If we have specified it via mount option, then
2889c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2890c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2891c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2892c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2893c9de560dSAlex Tomas  *
2894c9de560dSAlex Tomas  */
2895c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2896c9de560dSAlex Tomas {
2897c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2898c9de560dSAlex Tomas 	unsigned long stripe_width =
2899c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
29003eb08658SDan Ehrenberg 	int ret;
2901c9de560dSAlex Tomas 
2902c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
29033eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
29045469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
29053eb08658SDan Ehrenberg 		ret = stripe_width;
29065469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
29073eb08658SDan Ehrenberg 		ret = stride;
29083eb08658SDan Ehrenberg 	else
29093eb08658SDan Ehrenberg 		ret = 0;
2910c9de560dSAlex Tomas 
29113eb08658SDan Ehrenberg 	/*
29123eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
29133eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
29143eb08658SDan Ehrenberg 	 */
29153eb08658SDan Ehrenberg 	if (ret <= 1)
29163eb08658SDan Ehrenberg 		ret = 0;
2917c9de560dSAlex Tomas 
29183eb08658SDan Ehrenberg 	return ret;
2919c9de560dSAlex Tomas }
2920ac27a0ecSDave Kleikamp 
2921a13fb1a4SEric Sandeen /*
2922a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2923a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2924a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2925a13fb1a4SEric Sandeen  * 0 if it cannot be.
2926a13fb1a4SEric Sandeen  */
2927a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2928a13fb1a4SEric Sandeen {
2929e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2930a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2931a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2932a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2933a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2934a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2935a13fb1a4SEric Sandeen 		return 0;
2936a13fb1a4SEric Sandeen 	}
2937a13fb1a4SEric Sandeen 
2938c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
2939c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
2940c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
2941c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
2942c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
2943c83ad55eSGabriel Krisman Bertazi 		return 0;
2944c83ad55eSGabriel Krisman Bertazi 	}
2945c83ad55eSGabriel Krisman Bertazi #endif
2946c83ad55eSGabriel Krisman Bertazi 
2947a13fb1a4SEric Sandeen 	if (readonly)
2948a13fb1a4SEric Sandeen 		return 1;
2949a13fb1a4SEric Sandeen 
2950e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
29512cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
29521751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
29532cb5cc8bSDarrick J. Wong 		return 1;
29542cb5cc8bSDarrick J. Wong 	}
29552cb5cc8bSDarrick J. Wong 
2956a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2957e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2958a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2959a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2960a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2961a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2962a13fb1a4SEric Sandeen 		return 0;
2963a13fb1a4SEric Sandeen 	}
2964e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
2965bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2966bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2967bab08ab9STheodore Ts'o 			 "extents feature\n");
2968bab08ab9STheodore Ts'o 		return 0;
2969bab08ab9STheodore Ts'o 	}
29707c319d32SAditya Kali 
29717c319d32SAditya Kali #ifndef CONFIG_QUOTA
2972e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
29737c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
29747c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
29757c319d32SAditya Kali 			 "without CONFIG_QUOTA");
29767c319d32SAditya Kali 		return 0;
29777c319d32SAditya Kali 	}
2978689c958cSLi Xi 	if (ext4_has_feature_project(sb) && !readonly) {
2979689c958cSLi Xi 		ext4_msg(sb, KERN_ERR,
2980689c958cSLi Xi 			 "Filesystem with project quota feature cannot be mounted RDWR "
2981689c958cSLi Xi 			 "without CONFIG_QUOTA");
2982689c958cSLi Xi 		return 0;
2983689c958cSLi Xi 	}
29847c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2985a13fb1a4SEric Sandeen 	return 1;
2986a13fb1a4SEric Sandeen }
2987a13fb1a4SEric Sandeen 
298866e61a9eSTheodore Ts'o /*
298966e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
299066e61a9eSTheodore Ts'o  * on the file system
299166e61a9eSTheodore Ts'o  */
2992235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
299366e61a9eSTheodore Ts'o {
2994235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
2995235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
2996235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
299766e61a9eSTheodore Ts'o 
299866e61a9eSTheodore Ts'o 	if (es->s_error_count)
2999ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3000ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
300166e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
300266e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
30036a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
30046a0678a7SArnd Bergmann 		       sb->s_id,
30056a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
300666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
300766e61a9eSTheodore Ts'o 		       es->s_first_error_func,
300866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
300966e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3010651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
301166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
301266e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3013651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
301466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3015651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
301666e61a9eSTheodore Ts'o 	}
301766e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
30186a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
30196a0678a7SArnd Bergmann 		       sb->s_id,
30206a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
302166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
302266e61a9eSTheodore Ts'o 		       es->s_last_error_func,
302366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
302466e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3025651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
302666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
302766e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3028651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
302966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3030651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
303166e61a9eSTheodore Ts'o 	}
303266e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
303366e61a9eSTheodore Ts'o }
303466e61a9eSTheodore Ts'o 
3035bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3036bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3037bfff6873SLukas Czerner {
3038bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3039bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3040bfff6873SLukas Czerner 	struct super_block *sb;
3041bfff6873SLukas Czerner 	unsigned long timeout = 0;
3042bfff6873SLukas Czerner 	int ret = 0;
3043bfff6873SLukas Czerner 
3044bfff6873SLukas Czerner 	sb = elr->lr_super;
3045bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3046bfff6873SLukas Czerner 
3047bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3048bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3049bfff6873SLukas Czerner 		if (!gdp) {
3050bfff6873SLukas Czerner 			ret = 1;
3051bfff6873SLukas Czerner 			break;
3052bfff6873SLukas Czerner 		}
3053bfff6873SLukas Czerner 
3054bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3055bfff6873SLukas Czerner 			break;
3056bfff6873SLukas Czerner 	}
3057bfff6873SLukas Czerner 
30587f511862STheodore Ts'o 	if (group >= ngroups)
3059bfff6873SLukas Czerner 		ret = 1;
3060bfff6873SLukas Czerner 
3061bfff6873SLukas Czerner 	if (!ret) {
3062bfff6873SLukas Czerner 		timeout = jiffies;
3063bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3064bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3065bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
306651ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
306751ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3068bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3069bfff6873SLukas Czerner 		}
3070bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3071bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3072bfff6873SLukas Czerner 	}
3073bfff6873SLukas Czerner 	return ret;
3074bfff6873SLukas Czerner }
3075bfff6873SLukas Czerner 
3076bfff6873SLukas Czerner /*
3077bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
30784ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3079bfff6873SLukas Czerner  */
3080bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3081bfff6873SLukas Czerner {
3082bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3083bfff6873SLukas Czerner 
3084bfff6873SLukas Czerner 	if (!elr)
3085bfff6873SLukas Czerner 		return;
3086bfff6873SLukas Czerner 
3087bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3088bfff6873SLukas Czerner 
3089bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3090bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3091bfff6873SLukas Czerner 	kfree(elr);
3092bfff6873SLukas Czerner }
3093bfff6873SLukas Czerner 
3094bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3095bfff6873SLukas Czerner {
30961bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
30971bb933fbSLukas Czerner 	if (!ext4_li_info) {
30981bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3099bfff6873SLukas Czerner 		return;
31001bb933fbSLukas Czerner 	}
3101bfff6873SLukas Czerner 
3102bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
31031bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3104bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
31051bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3106bfff6873SLukas Czerner }
3107bfff6873SLukas Czerner 
31088f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
31098f1f7453SEric Sandeen 
3110bfff6873SLukas Czerner /*
3111bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3112bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3113bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3114bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3115bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3116bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3117bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3118bfff6873SLukas Czerner  */
3119bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3120bfff6873SLukas Czerner {
3121bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3122bfff6873SLukas Czerner 	struct list_head *pos, *n;
3123bfff6873SLukas Czerner 	struct ext4_li_request *elr;
31244ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3125bfff6873SLukas Czerner 
3126bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3127bfff6873SLukas Czerner 
3128bfff6873SLukas Czerner cont_thread:
3129bfff6873SLukas Czerner 	while (true) {
3130bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3131bfff6873SLukas Czerner 
3132bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3133bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3134bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3135bfff6873SLukas Czerner 			goto exit_thread;
3136bfff6873SLukas Czerner 		}
3137bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3138e22834f0SDmitry Monakhov 			int err = 0;
3139e22834f0SDmitry Monakhov 			int progress = 0;
3140bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3141bfff6873SLukas Czerner 					 lr_request);
3142bfff6873SLukas Czerner 
3143e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3144e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3145e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3146e22834f0SDmitry Monakhov 				continue;
3147e22834f0SDmitry Monakhov 			}
3148e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3149e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3150e22834f0SDmitry Monakhov 					progress = 1;
3151e22834f0SDmitry Monakhov 					/*
3152e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3153e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3154e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3155e22834f0SDmitry Monakhov 					 */
3156e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3157e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3158e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3159e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3160e22834f0SDmitry Monakhov 					n = pos->next;
3161e22834f0SDmitry Monakhov 				}
3162e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3163e22834f0SDmitry Monakhov 			}
3164b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3165e22834f0SDmitry Monakhov 			if (err) {
3166bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3167bfff6873SLukas Czerner 				continue;
3168bfff6873SLukas Czerner 			}
3169e22834f0SDmitry Monakhov 			if (!progress) {
3170e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3171e22834f0SDmitry Monakhov 					(prandom_u32()
3172e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3173b2c78cd0STheodore Ts'o 			}
3174bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3175bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3176bfff6873SLukas Czerner 		}
3177bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3178bfff6873SLukas Czerner 
3179a0acae0eSTejun Heo 		try_to_freeze();
3180bfff6873SLukas Czerner 
31814ed5c033SLukas Czerner 		cur = jiffies;
31824ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3183f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3184bfff6873SLukas Czerner 			cond_resched();
3185bfff6873SLukas Czerner 			continue;
3186bfff6873SLukas Czerner 		}
3187bfff6873SLukas Czerner 
31884ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
31894ed5c033SLukas Czerner 
31908f1f7453SEric Sandeen 		if (kthread_should_stop()) {
31918f1f7453SEric Sandeen 			ext4_clear_request_list();
31928f1f7453SEric Sandeen 			goto exit_thread;
31938f1f7453SEric Sandeen 		}
3194bfff6873SLukas Czerner 	}
3195bfff6873SLukas Czerner 
3196bfff6873SLukas Czerner exit_thread:
3197bfff6873SLukas Czerner 	/*
3198bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3199bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3200bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3201bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3202bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3203bfff6873SLukas Czerner 	 * new one.
3204bfff6873SLukas Czerner 	 */
3205bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3206bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3207bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3208bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3209bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3210bfff6873SLukas Czerner 		goto cont_thread;
3211bfff6873SLukas Czerner 	}
3212bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3213bfff6873SLukas Czerner 	kfree(ext4_li_info);
3214bfff6873SLukas Czerner 	ext4_li_info = NULL;
3215bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3216bfff6873SLukas Czerner 
3217bfff6873SLukas Czerner 	return 0;
3218bfff6873SLukas Czerner }
3219bfff6873SLukas Czerner 
3220bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3221bfff6873SLukas Czerner {
3222bfff6873SLukas Czerner 	struct list_head *pos, *n;
3223bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3224bfff6873SLukas Czerner 
3225bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3226bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3227bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3228bfff6873SLukas Czerner 				 lr_request);
3229bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3230bfff6873SLukas Czerner 	}
3231bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3232bfff6873SLukas Czerner }
3233bfff6873SLukas Czerner 
3234bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3235bfff6873SLukas Czerner {
32368f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
32378f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
32388f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
32398f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3240bfff6873SLukas Czerner 		ext4_clear_request_list();
3241bfff6873SLukas Czerner 		kfree(ext4_li_info);
3242bfff6873SLukas Czerner 		ext4_li_info = NULL;
324392b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3244bfff6873SLukas Czerner 				 "initialization thread\n",
3245bfff6873SLukas Czerner 				 err);
3246bfff6873SLukas Czerner 		return err;
3247bfff6873SLukas Czerner 	}
3248bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3249bfff6873SLukas Czerner 	return 0;
3250bfff6873SLukas Czerner }
3251bfff6873SLukas Czerner 
3252bfff6873SLukas Czerner /*
3253bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3254bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3255bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3256bfff6873SLukas Czerner  * groups and return total number of groups.
3257bfff6873SLukas Czerner  */
3258bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3259bfff6873SLukas Czerner {
3260bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3261bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3262bfff6873SLukas Czerner 
32638844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
32648844618dSTheodore Ts'o 		return ngroups;
32658844618dSTheodore Ts'o 
3266bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3267bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3268bfff6873SLukas Czerner 		if (!gdp)
3269bfff6873SLukas Czerner 			continue;
3270bfff6873SLukas Czerner 
327150122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3272bfff6873SLukas Czerner 			break;
3273bfff6873SLukas Czerner 	}
3274bfff6873SLukas Czerner 
3275bfff6873SLukas Czerner 	return group;
3276bfff6873SLukas Czerner }
3277bfff6873SLukas Czerner 
3278bfff6873SLukas Czerner static int ext4_li_info_new(void)
3279bfff6873SLukas Czerner {
3280bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3281bfff6873SLukas Czerner 
3282bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3283bfff6873SLukas Czerner 	if (!eli)
3284bfff6873SLukas Czerner 		return -ENOMEM;
3285bfff6873SLukas Czerner 
3286bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3287bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3288bfff6873SLukas Czerner 
3289bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3290bfff6873SLukas Czerner 
3291bfff6873SLukas Czerner 	ext4_li_info = eli;
3292bfff6873SLukas Czerner 
3293bfff6873SLukas Czerner 	return 0;
3294bfff6873SLukas Czerner }
3295bfff6873SLukas Czerner 
3296bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3297bfff6873SLukas Czerner 					    ext4_group_t start)
3298bfff6873SLukas Czerner {
3299bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3300bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3301bfff6873SLukas Czerner 
3302bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3303bfff6873SLukas Czerner 	if (!elr)
3304bfff6873SLukas Czerner 		return NULL;
3305bfff6873SLukas Czerner 
3306bfff6873SLukas Czerner 	elr->lr_super = sb;
3307bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3308bfff6873SLukas Czerner 	elr->lr_next_group = start;
3309bfff6873SLukas Czerner 
3310bfff6873SLukas Czerner 	/*
3311bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3312bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3313bfff6873SLukas Czerner 	 * better.
3314bfff6873SLukas Czerner 	 */
3315dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3316dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3317bfff6873SLukas Czerner 	return elr;
3318bfff6873SLukas Czerner }
3319bfff6873SLukas Czerner 
33207f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3321bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3322bfff6873SLukas Czerner {
3323bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
33247f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
332549598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
33266c5a6cb9SAndrew Morton 	int ret = 0;
3327bfff6873SLukas Czerner 
33287f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
332951ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
333051ce6511SLukas Czerner 		/*
333151ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
333251ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
333351ce6511SLukas Czerner 		 */
333451ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
33357f511862STheodore Ts'o 		goto out;
333651ce6511SLukas Czerner 	}
3337bfff6873SLukas Czerner 
3338bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
333955ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
33407f511862STheodore Ts'o 		goto out;
3341bfff6873SLukas Czerner 
3342bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
33437f511862STheodore Ts'o 	if (!elr) {
33447f511862STheodore Ts'o 		ret = -ENOMEM;
33457f511862STheodore Ts'o 		goto out;
33467f511862STheodore Ts'o 	}
3347bfff6873SLukas Czerner 
3348bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3349bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3350bfff6873SLukas Czerner 		if (ret)
3351bfff6873SLukas Czerner 			goto out;
3352bfff6873SLukas Czerner 	}
3353bfff6873SLukas Czerner 
3354bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3355bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3356bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3357bfff6873SLukas Czerner 
3358bfff6873SLukas Czerner 	sbi->s_li_request = elr;
335946e4690bSTao Ma 	/*
336046e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
336146e4690bSTao Ma 	 * the request_list and the removal and free of it is
336246e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
336346e4690bSTao Ma 	 */
336446e4690bSTao Ma 	elr = NULL;
3365bfff6873SLukas Czerner 
3366bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3367bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3368bfff6873SLukas Czerner 		if (ret)
3369bfff6873SLukas Czerner 			goto out;
3370bfff6873SLukas Czerner 	}
3371bfff6873SLukas Czerner out:
3372bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3373beed5ecbSNicolas Kaiser 	if (ret)
3374bfff6873SLukas Czerner 		kfree(elr);
3375bfff6873SLukas Czerner 	return ret;
3376bfff6873SLukas Czerner }
3377bfff6873SLukas Czerner 
3378bfff6873SLukas Czerner /*
3379bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3380bfff6873SLukas Czerner  * module unload.
3381bfff6873SLukas Czerner  */
3382bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3383bfff6873SLukas Czerner {
3384bfff6873SLukas Czerner 	/*
3385bfff6873SLukas Czerner 	 * If thread exited earlier
3386bfff6873SLukas Czerner 	 * there's nothing to be done.
3387bfff6873SLukas Czerner 	 */
33888f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3389bfff6873SLukas Czerner 		return;
3390bfff6873SLukas Czerner 
33918f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3392bfff6873SLukas Czerner }
3393bfff6873SLukas Czerner 
339425ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
339525ed6e8aSDarrick J. Wong {
339625ed6e8aSDarrick J. Wong 	int ret = 1;
339725ed6e8aSDarrick J. Wong 	int compat, incompat;
339825ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
339925ed6e8aSDarrick J. Wong 
34009aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3401db9ee220SDarrick J. Wong 		/* journal checksum v3 */
340225ed6e8aSDarrick J. Wong 		compat = 0;
3403db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
340425ed6e8aSDarrick J. Wong 	} else {
340525ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
340625ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
340725ed6e8aSDarrick J. Wong 		incompat = 0;
340825ed6e8aSDarrick J. Wong 	}
340925ed6e8aSDarrick J. Wong 
3410feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3411feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3412feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3413feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
341425ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
341525ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
341625ed6e8aSDarrick J. Wong 				compat, 0,
341725ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
341825ed6e8aSDarrick J. Wong 				incompat);
341925ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
342025ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
342125ed6e8aSDarrick J. Wong 				compat, 0,
342225ed6e8aSDarrick J. Wong 				incompat);
342325ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
342425ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
342525ed6e8aSDarrick J. Wong 	} else {
3426feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3427feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
342825ed6e8aSDarrick J. Wong 	}
342925ed6e8aSDarrick J. Wong 
343025ed6e8aSDarrick J. Wong 	return ret;
343125ed6e8aSDarrick J. Wong }
343225ed6e8aSDarrick J. Wong 
3433952fc18eSTheodore Ts'o /*
3434952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3435952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3436952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3437952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3438952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3439952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3440952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3441952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3442952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3443952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3444952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3445952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3446952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3447952fc18eSTheodore Ts'o  */
3448952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3449952fc18eSTheodore Ts'o 			  char *buf)
3450952fc18eSTheodore Ts'o {
3451952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3452952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3453952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3454952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3455952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3456952fc18eSTheodore Ts'o 
3457e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
34580548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
34590548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
34600548bbb8STheodore Ts'o 
3461952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3462952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3463952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3464952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3465952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3466952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3467952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3468952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3469952fc18eSTheodore Ts'o 			count++;
3470952fc18eSTheodore Ts'o 		}
3471952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3472952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3473952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3474952fc18eSTheodore Ts'o 			count++;
3475952fc18eSTheodore Ts'o 		}
3476952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3477952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3478952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3479952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3480952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3481952fc18eSTheodore Ts'o 				count++;
3482952fc18eSTheodore Ts'o 			}
3483952fc18eSTheodore Ts'o 		if (i != grp)
3484952fc18eSTheodore Ts'o 			continue;
3485952fc18eSTheodore Ts'o 		s = 0;
3486952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3487952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3488952fc18eSTheodore Ts'o 			count++;
3489952fc18eSTheodore Ts'o 		}
3490c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3491c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3492c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3493c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3494c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3495952fc18eSTheodore Ts'o 		}
3496c48ae41bSTheodore Ts'o 		count += j;
3497c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3498c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3499952fc18eSTheodore Ts'o 	}
3500952fc18eSTheodore Ts'o 	if (!count)
3501952fc18eSTheodore Ts'o 		return 0;
3502952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3503952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3504952fc18eSTheodore Ts'o }
3505952fc18eSTheodore Ts'o 
3506952fc18eSTheodore Ts'o /*
3507952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3508952fc18eSTheodore Ts'o  */
3509952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3510952fc18eSTheodore Ts'o {
3511952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3512952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
35133c816dedSEric Whitney 	struct inode *j_inode;
35143c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3515952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3516952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
35174fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3518952fc18eSTheodore Ts'o 
3519952fc18eSTheodore Ts'o 	if (!buf)
3520952fc18eSTheodore Ts'o 		return -ENOMEM;
3521952fc18eSTheodore Ts'o 
3522952fc18eSTheodore Ts'o 	/*
3523952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3524952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3525952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3526952fc18eSTheodore Ts'o 	 */
3527952fc18eSTheodore Ts'o 
3528952fc18eSTheodore Ts'o 	/*
3529952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3530952fc18eSTheodore Ts'o 	 */
3531952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3532952fc18eSTheodore Ts'o 
3533952fc18eSTheodore Ts'o 	/*
3534952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3535952fc18eSTheodore Ts'o 	 */
3536952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3537952fc18eSTheodore Ts'o 		int blks;
3538952fc18eSTheodore Ts'o 
3539952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3540952fc18eSTheodore Ts'o 		overhead += blks;
3541952fc18eSTheodore Ts'o 		if (blks)
3542952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3543952fc18eSTheodore Ts'o 		cond_resched();
3544952fc18eSTheodore Ts'o 	}
35453c816dedSEric Whitney 
35463c816dedSEric Whitney 	/*
35473c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
35483c816dedSEric Whitney 	 * loaded or not
35493c816dedSEric Whitney 	 */
3550b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3551810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
35523c816dedSEric Whitney 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
35533c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
35543c816dedSEric Whitney 		if (j_inode) {
35553c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
35563c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
35573c816dedSEric Whitney 			iput(j_inode);
35583c816dedSEric Whitney 		} else {
35593c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
35603c816dedSEric Whitney 		}
35613c816dedSEric Whitney 	}
3562952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3563952fc18eSTheodore Ts'o 	smp_wmb();
3564952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3565952fc18eSTheodore Ts'o 	return 0;
3566952fc18eSTheodore Ts'o }
3567952fc18eSTheodore Ts'o 
35687bc04c5cSBarret Rhoden static void ext4_clamp_want_extra_isize(struct super_block *sb)
35697bc04c5cSBarret Rhoden {
35707bc04c5cSBarret Rhoden 	struct ext4_sb_info *sbi = EXT4_SB(sb);
35717bc04c5cSBarret Rhoden 	struct ext4_super_block *es = sbi->s_es;
35727bc04c5cSBarret Rhoden 
35737bc04c5cSBarret Rhoden 	/* determine the minimum size of new large inodes, if present */
35747bc04c5cSBarret Rhoden 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE &&
35757bc04c5cSBarret Rhoden 	    sbi->s_want_extra_isize == 0) {
35767bc04c5cSBarret Rhoden 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
35777bc04c5cSBarret Rhoden 						     EXT4_GOOD_OLD_INODE_SIZE;
35787bc04c5cSBarret Rhoden 		if (ext4_has_feature_extra_isize(sb)) {
35797bc04c5cSBarret Rhoden 			if (sbi->s_want_extra_isize <
35807bc04c5cSBarret Rhoden 			    le16_to_cpu(es->s_want_extra_isize))
35817bc04c5cSBarret Rhoden 				sbi->s_want_extra_isize =
35827bc04c5cSBarret Rhoden 					le16_to_cpu(es->s_want_extra_isize);
35837bc04c5cSBarret Rhoden 			if (sbi->s_want_extra_isize <
35847bc04c5cSBarret Rhoden 			    le16_to_cpu(es->s_min_extra_isize))
35857bc04c5cSBarret Rhoden 				sbi->s_want_extra_isize =
35867bc04c5cSBarret Rhoden 					le16_to_cpu(es->s_min_extra_isize);
35877bc04c5cSBarret Rhoden 		}
35887bc04c5cSBarret Rhoden 	}
35897bc04c5cSBarret Rhoden 	/* Check if enough inode space is available */
35907bc04c5cSBarret Rhoden 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
35917bc04c5cSBarret Rhoden 							sbi->s_inode_size) {
35927bc04c5cSBarret Rhoden 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
35937bc04c5cSBarret Rhoden 						       EXT4_GOOD_OLD_INODE_SIZE;
35947bc04c5cSBarret Rhoden 		ext4_msg(sb, KERN_INFO,
35957bc04c5cSBarret Rhoden 			 "required extra inode space not available");
35967bc04c5cSBarret Rhoden 	}
35977bc04c5cSBarret Rhoden }
35987bc04c5cSBarret Rhoden 
3599b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
360027dd4385SLukas Czerner {
360127dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3602b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
360327dd4385SLukas Czerner 
360427dd4385SLukas Czerner 	/*
360530fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
360630fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
360730fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
360830fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
360930fac0f7SJan Kara 	 */
3610e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3611b5799018STheodore Ts'o 		return;
361230fac0f7SJan Kara 	/*
361327dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
361427dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
361527dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3616556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
361727dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
361827dd4385SLukas Czerner 	 * very rare.
361927dd4385SLukas Czerner 	 */
3620b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3621b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
362227dd4385SLukas Czerner 
362327dd4385SLukas Czerner 	do_div(resv_clusters, 50);
362427dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
362527dd4385SLukas Czerner 
3626b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
362727dd4385SLukas Czerner }
362827dd4385SLukas Czerner 
3629617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3630ac27a0ecSDave Kleikamp {
36315e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3632d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3633ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3634617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
36355aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3636617ba13bSMingming Cao 	ext4_fsblk_t block;
3637617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
363870bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3639ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3640ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3641ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3642ac27a0ecSDave Kleikamp 	struct inode *root;
36430390131bSFrank Mayhar 	const char *descr;
3644dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3645281b5995STheodore Ts'o 	int blocksize, clustersize;
36464ec11028STheodore Ts'o 	unsigned int db_count;
36474ec11028STheodore Ts'o 	unsigned int i;
3648281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3649bd81d8eeSLaurent Vivier 	__u64 blocks_count;
365007aa2ea1SLukas Czerner 	int err = 0;
3651b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3652bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3653ac27a0ecSDave Kleikamp 
36545aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
36555aee0f8aSTheodore Ts'o 		goto out_free_base;
3656705895b6SPekka Enberg 
3657aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3658705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3659705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
36605aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
36615aee0f8aSTheodore Ts'o 		goto out_free_base;
36625aee0f8aSTheodore Ts'o 
3663ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
36642c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3665240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3666d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3667f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3668f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3669dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3670ac27a0ecSDave Kleikamp 
36719f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3672ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
36739f6200bbSTheodore Ts'o 
367407aa2ea1SLukas Czerner 	/* -EINVAL is default */
3675dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3676617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3677ac27a0ecSDave Kleikamp 	if (!blocksize) {
3678b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3679ac27a0ecSDave Kleikamp 		goto out_fail;
3680ac27a0ecSDave Kleikamp 	}
3681ac27a0ecSDave Kleikamp 
3682ac27a0ecSDave Kleikamp 	/*
3683617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3684ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3685ac27a0ecSDave Kleikamp 	 */
3686617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
368770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
368870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3689ac27a0ecSDave Kleikamp 	} else {
369070bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3691ac27a0ecSDave Kleikamp 	}
3692ac27a0ecSDave Kleikamp 
3693a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3694b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3695ac27a0ecSDave Kleikamp 		goto out_fail;
3696ac27a0ecSDave Kleikamp 	}
3697ac27a0ecSDave Kleikamp 	/*
3698ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3699617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3700ac27a0ecSDave Kleikamp 	 */
37012716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3702ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3703ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3704617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3705617ba13bSMingming Cao 		goto cantfind_ext4;
3706afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3707ac27a0ecSDave Kleikamp 
3708feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3709e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3710e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3711363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3712feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3713feb0ab32SDarrick J. Wong 
3714d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3715d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3716d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3717d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3718d25425f8SDarrick J. Wong 		silent = 1;
3719d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3720d25425f8SDarrick J. Wong 	}
3721d25425f8SDarrick J. Wong 
37220441984aSDarrick J. Wong 	/* Load the checksum driver */
37230441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
37240441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
37250441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
37260441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
37270441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
37280441984aSDarrick J. Wong 		goto failed_mount;
37290441984aSDarrick J. Wong 	}
37300441984aSDarrick J. Wong 
3731a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3732a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3733a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3734a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3735a9c47317SDarrick J. Wong 		silent = 1;
37366a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3737a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3738a9c47317SDarrick J. Wong 	}
3739a9c47317SDarrick J. Wong 
3740a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3741e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
37428c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3743dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3744a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3745a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3746a9c47317SDarrick J. Wong 
3747ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3748ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3749fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3750617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3751fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
375287f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3753fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3754617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3755fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3756ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3757fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
375803010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3759fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
37602e7842b8SHugh Dickins #endif
376198c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
376298c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
376398c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
376498c1a759SDarrick J. Wong 
3765617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3766fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3767617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3768fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3769617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3770fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3771ac27a0ecSDave Kleikamp 
3772617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3773fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3774bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3775fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3776bb4f397aSAneesh Kumar K.V 	else
3777fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
377845f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3779fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
37808b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3781fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3782ac27a0ecSDave Kleikamp 
378308cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
378408cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
378530773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
378630773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
378730773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3788ac27a0ecSDave Kleikamp 
37898b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3790fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3791ac27a0ecSDave Kleikamp 
37921e2462f9SMingming Cao 	/*
3793dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3794dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3795dd919b98SAneesh Kumar K.V 	 */
3796bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
37978b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3798fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3799dd919b98SAneesh Kumar K.V 
380051ce6511SLukas Czerner 	/*
380151ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
380251ce6511SLukas Czerner 	 * no mount option specified.
380351ce6511SLukas Czerner 	 */
380451ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
380551ce6511SLukas Czerner 
38065aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
38075aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
38085aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
38095aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
38105aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
38115aee0f8aSTheodore Ts'o 			goto failed_mount;
38125aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
38135aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
38148b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
38158b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
38165aee0f8aSTheodore Ts'o 				 s_mount_opts);
38175aee0f8aSTheodore Ts'o 		}
38185aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
38198b67f04aSTheodore Ts'o 	}
38205a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3821b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3822661aa520SEric Sandeen 			   &journal_ioprio, 0))
3823ac27a0ecSDave Kleikamp 		goto failed_mount;
3824ac27a0ecSDave Kleikamp 
3825c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3826c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
3827c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
3828c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
3829c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
3830c83ad55eSGabriel Krisman Bertazi 
3831c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
3832c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3833c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
3834c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3835c83ad55eSGabriel Krisman Bertazi 		}
3836c83ad55eSGabriel Krisman Bertazi 
3837c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
3838c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
3839c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3840c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
3841c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3842c83ad55eSGabriel Krisman Bertazi 		}
3843c83ad55eSGabriel Krisman Bertazi 
3844c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
3845c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
3846c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3847c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
3848c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
3849c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
3850c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
3851c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3852c83ad55eSGabriel Krisman Bertazi 		}
3853c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
3854c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
3855c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
3856c83ad55eSGabriel Krisman Bertazi 
3857c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
3858c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
3859c83ad55eSGabriel Krisman Bertazi 	}
3860c83ad55eSGabriel Krisman Bertazi #endif
3861c83ad55eSGabriel Krisman Bertazi 
386256889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
386356889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
386456889787STheodore Ts'o 			    "with data=journal disables delayed "
386556889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
386656889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
386756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
386856889787STheodore Ts'o 				 "both data=journal and delalloc");
386956889787STheodore Ts'o 			goto failed_mount;
387056889787STheodore Ts'o 		}
387156889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
387256889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
38736ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
387456889787STheodore Ts'o 			goto failed_mount;
387556889787STheodore Ts'o 		}
3876923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3877923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3878923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3879923ae0ffSRoss Zwisler 			goto failed_mount;
3880923ae0ffSRoss Zwisler 		}
388173b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
388273b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
388373b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
388473b92a2aSSergey Karamov 				 "instead of data journaling mode");
388573b92a2aSSergey Karamov 		}
388656889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
388756889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3888001e4a87STejun Heo 	} else {
3889001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
389056889787STheodore Ts'o 	}
389156889787STheodore Ts'o 
38921751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
38931751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
3894ac27a0ecSDave Kleikamp 
3895617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3896e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3897e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3898e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3899b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3900b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3901b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3902469108ffSTheodore Tso 
3903ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3904ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3905e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3906ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3907ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3908ed3654ebSTheodore Ts'o 			goto failed_mount;
3909ed3654ebSTheodore Ts'o 		}
3910dec214d0STahsin Erdogan 
3911dec214d0STahsin Erdogan 		/*
3912dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
3913dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
3914dec214d0STahsin Erdogan 		 */
3915dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
3916dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
3917dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
3918dec214d0STahsin Erdogan 			goto failed_mount;
3919dec214d0STahsin Erdogan 		}
3920ed3654ebSTheodore Ts'o 	}
3921ed3654ebSTheodore Ts'o 
39222035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
39232035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
39242035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
39252035e776STheodore Ts'o 				 "using the ext4 subsystem");
39262035e776STheodore Ts'o 		else {
39270d9366d6SEric Sandeen 			/*
39280d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
39290d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
39300d9366d6SEric Sandeen 			 */
39310d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
39320d9366d6SEric Sandeen 				goto failed_mount;
39332035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
39342035e776STheodore Ts'o 				 "to feature incompatibilities");
39352035e776STheodore Ts'o 			goto failed_mount;
39362035e776STheodore Ts'o 		}
39372035e776STheodore Ts'o 	}
39382035e776STheodore Ts'o 
39392035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
39402035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
39412035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
39422035e776STheodore Ts'o 				 "using the ext4 subsystem");
39432035e776STheodore Ts'o 		else {
39440d9366d6SEric Sandeen 			/*
39450d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
39460d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
39470d9366d6SEric Sandeen 			 */
39480d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
39490d9366d6SEric Sandeen 				goto failed_mount;
39502035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
39512035e776STheodore Ts'o 				 "to feature incompatibilities");
39522035e776STheodore Ts'o 			goto failed_mount;
39532035e776STheodore Ts'o 		}
39542035e776STheodore Ts'o 	}
39552035e776STheodore Ts'o 
3956469108ffSTheodore Tso 	/*
3957ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3958ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3959ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3960ac27a0ecSDave Kleikamp 	 */
3961bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
3962ac27a0ecSDave Kleikamp 		goto failed_mount;
3963a13fb1a4SEric Sandeen 
3964261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3965617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3966617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3967b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
39688cdf3372STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
39698cdf3372STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
39708cdf3372STheodore Ts'o 		goto failed_mount;
39718cdf3372STheodore Ts'o 	}
39728cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
39738cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
39748cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39758cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
39768cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
3977ac27a0ecSDave Kleikamp 		goto failed_mount;
3978ac27a0ecSDave Kleikamp 	}
3979bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
3980bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
3981bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3982bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
3983bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
3984bfe0a5f4STheodore Ts'o 		goto failed_mount;
3985bfe0a5f4STheodore Ts'o 	}
3986ac27a0ecSDave Kleikamp 
39875b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
39885b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39895b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
39905b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
39915b9554dcSTheodore Ts'o 		goto failed_mount;
39925b9554dcSTheodore Ts'o 	}
39935b9554dcSTheodore Ts'o 
3994923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
3995559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
3996559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
3997559db4c6SRoss Zwisler 					" that may contain inline data");
3998361d24d4SEric Sandeen 			goto failed_mount;
3999559db4c6SRoss Zwisler 		}
400080660f20SDave Jiang 		if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
400124f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4002361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4003361d24d4SEric Sandeen 			goto failed_mount;
400424f3478dSDan Williams 		}
4005923ae0ffSRoss Zwisler 	}
4006923ae0ffSRoss Zwisler 
4007e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
40086ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
40096ddb2447STheodore Ts'o 			 es->s_encryption_level);
40106ddb2447STheodore Ts'o 		goto failed_mount;
40116ddb2447STheodore Ts'o 	}
40126ddb2447STheodore Ts'o 
4013ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4014ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4015ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4016b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4017ce40733cSAneesh Kumar K.V 					blocksize);
4018ac27a0ecSDave Kleikamp 			goto failed_mount;
4019ac27a0ecSDave Kleikamp 		}
4020ac27a0ecSDave Kleikamp 
4021ac27a0ecSDave Kleikamp 		brelse(bh);
402270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
402370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4024a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4025ac27a0ecSDave Kleikamp 		if (!bh) {
4026b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4027b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4028ac27a0ecSDave Kleikamp 			goto failed_mount;
4029ac27a0ecSDave Kleikamp 		}
40302716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4031ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4032617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4033b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4034b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4035ac27a0ecSDave Kleikamp 			goto failed_mount;
4036ac27a0ecSDave Kleikamp 		}
4037ac27a0ecSDave Kleikamp 	}
4038ac27a0ecSDave Kleikamp 
4039e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4040f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4041f287a1a5STheodore Ts'o 						      has_huge_files);
4042f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4043ac27a0ecSDave Kleikamp 
4044617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4045617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4046617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4047ac27a0ecSDave Kleikamp 	} else {
4048ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4049ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4050c37e9e01STheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4051c37e9e01STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4052c37e9e01STheodore Ts'o 				 sbi->s_first_ino);
4053c37e9e01STheodore Ts'o 			goto failed_mount;
4054c37e9e01STheodore Ts'o 		}
4055617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
40561330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
4057ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
4058b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4059b31e1552SEric Sandeen 			       "unsupported inode size: %d",
4060ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
4061ac27a0ecSDave Kleikamp 			goto failed_mount;
4062ac27a0ecSDave Kleikamp 		}
40634881c497SDeepa Dinamani 		/*
40644881c497SDeepa Dinamani 		 * i_atime_extra is the last extra field available for [acm]times in
40654881c497SDeepa Dinamani 		 * struct ext4_inode. Checking for that field should suffice to ensure
40664881c497SDeepa Dinamani 		 * we have extra space for all three.
40674881c497SDeepa Dinamani 		 */
40684881c497SDeepa Dinamani 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
40694881c497SDeepa Dinamani 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
40704881c497SDeepa Dinamani 			sb->s_time_gran = 1;
40714881c497SDeepa Dinamani 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
40724881c497SDeepa Dinamani 		} else {
40734881c497SDeepa Dinamani 			sb->s_time_gran = NSEC_PER_SEC;
40744881c497SDeepa Dinamani 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
40754881c497SDeepa Dinamani 		}
40764881c497SDeepa Dinamani 
40774881c497SDeepa Dinamani 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
4078ac27a0ecSDave Kleikamp 	}
40790b8e58a1SAndreas Dilger 
40800d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4081e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
40828fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
40830d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4084d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4085b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4086b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
40870d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
40880d1ee42fSAlexandre Ratchov 			goto failed_mount;
40890d1ee42fSAlexandre Ratchov 		}
40900d1ee42fSAlexandre Ratchov 	} else
40910d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
40920b8e58a1SAndreas Dilger 
4093ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4094ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
40950b8e58a1SAndreas Dilger 
4096617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4097ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4098617ba13bSMingming Cao 		goto cantfind_ext4;
4099cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4100cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4101cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4102cd6bb35bSTheodore Ts'o 			 sbi->s_blocks_per_group);
4103cd6bb35bSTheodore Ts'o 		goto failed_mount;
4104cd6bb35bSTheodore Ts'o 	}
4105ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4106ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
41070d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4108ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4109ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4110e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4111e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
41120b8e58a1SAndreas Dilger 
4113ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4114ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4115ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4116e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4117f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4118f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4119f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4120f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4121f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4122bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
412323301410STheodore Ts'o 				es->s_flags |=
412423301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4125f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4126f99b2589STheodore Ts'o #else
4127bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
412823301410STheodore Ts'o 				es->s_flags |=
412923301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4130f99b2589STheodore Ts'o #endif
4131f99b2589STheodore Ts'o 		}
413223301410STheodore Ts'o 	}
4133ac27a0ecSDave Kleikamp 
4134281b5995STheodore Ts'o 	/* Handle clustersize */
4135281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4136e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
4137281b5995STheodore Ts'o 	if (has_bigalloc) {
4138281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4139281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4140281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4141281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4142281b5995STheodore Ts'o 			goto failed_mount;
4143281b5995STheodore Ts'o 		}
4144281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4145281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4146281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4147281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4148281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4149281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4150281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4151281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4152281b5995STheodore Ts'o 			goto failed_mount;
4153281b5995STheodore Ts'o 		}
4154281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4155281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4156281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4157281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4158281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4159281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4160281b5995STheodore Ts'o 			goto failed_mount;
4161281b5995STheodore Ts'o 		}
4162281b5995STheodore Ts'o 	} else {
4163281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4164bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4165bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4166bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4167bfe0a5f4STheodore Ts'o 			goto failed_mount;
4168281b5995STheodore Ts'o 		}
4169ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4170b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4171b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4172ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4173ac27a0ecSDave Kleikamp 			goto failed_mount;
4174ac27a0ecSDave Kleikamp 		}
4175281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4176281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4177281b5995STheodore Ts'o 	}
4178281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4179281b5995STheodore Ts'o 
4180960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4181960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4182960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4183960fd856STheodore Ts'o 
4184bf43d84bSEric Sandeen 	/*
4185bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4186bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4187bf43d84bSEric Sandeen 	 */
41885a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
418930ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
41905a9ae68aSDarrick J. Wong 	if (err) {
4191b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4192bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4193ac27a0ecSDave Kleikamp 		goto failed_mount;
4194ac27a0ecSDave Kleikamp 	}
4195ac27a0ecSDave Kleikamp 
4196617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4197617ba13bSMingming Cao 		goto cantfind_ext4;
4198e7c95593SEric Sandeen 
41990f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
42000f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
42010f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4202b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4203b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
42040f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
42050f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
42060f2ddca6SFrom: Thiemo Nagel 	}
42070f2ddca6SFrom: Thiemo Nagel 
42084ec11028STheodore Ts'o 	/*
42094ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
42104ec11028STheodore Ts'o 	 * of the filesystem.
42114ec11028STheodore Ts'o 	 */
42124ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4213b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4214b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4215e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
42164ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4217e7c95593SEric Sandeen 		goto failed_mount;
4218e7c95593SEric Sandeen 	}
4219bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4220bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4221bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4222bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4223bfe0a5f4STheodore Ts'o 		goto failed_mount;
4224bfe0a5f4STheodore Ts'o 	}
4225bfe0a5f4STheodore Ts'o 
4226bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4227bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4228bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4229bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
42304ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4231b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
42324ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4233b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
42344ec11028STheodore Ts'o 		       ext4_blocks_count(es),
42354ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
42364ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
42374ec11028STheodore Ts'o 		goto failed_mount;
42384ec11028STheodore Ts'o 	}
4239bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4240fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4241fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
42429e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
42439e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
42449e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
42459e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
42469e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
42479e463084STheodore Ts'o 		ret = -EINVAL;
42489e463084STheodore Ts'o 		goto failed_mount;
42499e463084STheodore Ts'o 	}
4250617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4251617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
42523a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
42532ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
42543a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
42553a4b77cdSEryu Guan 				 "first meta block group too large: %u "
42563a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
42573a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
42583a4b77cdSEryu Guan 			goto failed_mount;
42593a4b77cdSEryu Guan 		}
42603a4b77cdSEryu Guan 	}
4261344476e1SKees Cook 	sbi->s_group_desc = kvmalloc_array(db_count,
4262f18a5f21STheodore Ts'o 					   sizeof(struct buffer_head *),
4263ac27a0ecSDave Kleikamp 					   GFP_KERNEL);
4264ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4265b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
42662cde417dSTheodore Ts'o 		ret = -ENOMEM;
4267ac27a0ecSDave Kleikamp 		goto failed_mount;
4268ac27a0ecSDave Kleikamp 	}
4269ac27a0ecSDave Kleikamp 
4270705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4271ac27a0ecSDave Kleikamp 
427285c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
427385c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
427485c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
427585c8f176SAndrew Perepechko 		sb_breadahead(sb, block);
427685c8f176SAndrew Perepechko 	}
427785c8f176SAndrew Perepechko 
4278ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
427970bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
4280a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
4281ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
4282b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4283b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4284ac27a0ecSDave Kleikamp 			db_count = i;
4285ac27a0ecSDave Kleikamp 			goto failed_mount2;
4286ac27a0ecSDave Kleikamp 		}
4287ac27a0ecSDave Kleikamp 	}
428844de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4289829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4290b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
42916a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4292f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4293ac27a0ecSDave Kleikamp 	}
4294772cb7c8SJose R. Santos 
4295235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
429604496411STao Ma 
4297a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4298eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4299ce7e010aSTheodore Ts'o 		goto failed_mount3;
4300ce7e010aSTheodore Ts'o 
4301c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
430267a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4303c9de560dSAlex Tomas 
4304f9ae9cf5STheodore Ts'o 	/*
4305f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4306f9ae9cf5STheodore Ts'o 	 */
4307f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4308617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4309617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4310643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4311a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4312ffcc4182SEric Biggers #endif
4313c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4314c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4315c93d8f88SEric Biggers #endif
4316ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4317617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4318e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
43191fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4320262b4662SJan Kara 	else
4321262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4322689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4323ac27a0ecSDave Kleikamp #endif
432485787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4325f2fa2ffcSAneesh Kumar K.V 
4326ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
43273b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4328ac27a0ecSDave Kleikamp 
4329ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4330ac27a0ecSDave Kleikamp 
4331ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4332e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4333ac27a0ecSDave Kleikamp 
4334bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4335c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
433650460fe8SDarrick J. Wong 			goto failed_mount3a;
4337c5e06d10SJohann Lombardi 
4338ac27a0ecSDave Kleikamp 	/*
4339ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4340ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4341ac27a0ecSDave Kleikamp 	 */
4342e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
43434753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
43444753d8a2STheodore Ts'o 		if (err)
434550460fe8SDarrick J. Wong 			goto failed_mount3a;
4346bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4347e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4348b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4349b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4350744692dcSJiaying Zhang 		goto failed_mount_wq;
4351ac27a0ecSDave Kleikamp 	} else {
43521e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
43531e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
43541e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43551e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
43561e381f60SDmitry Monakhov 			goto failed_mount_wq;
43571e381f60SDmitry Monakhov 		}
43581e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
43591e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43601e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
43611e381f60SDmitry Monakhov 			goto failed_mount_wq;
43621e381f60SDmitry Monakhov 		}
43631e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
43641e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43651e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
43661e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
43671e381f60SDmitry Monakhov 			goto failed_mount_wq;
43681e381f60SDmitry Monakhov 		}
43691e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
43701e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
43711e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43721e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
43731e381f60SDmitry Monakhov 			goto failed_mount_wq;
43741e381f60SDmitry Monakhov 		}
437550b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
43761e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4377fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
43780390131bSFrank Mayhar 		sbi->s_journal = NULL;
43790390131bSFrank Mayhar 		needs_recovery = 0;
43800390131bSFrank Mayhar 		goto no_journal;
4381ac27a0ecSDave Kleikamp 	}
4382ac27a0ecSDave Kleikamp 
4383e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4384eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4385eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4386b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4387744692dcSJiaying Zhang 		goto failed_mount_wq;
4388eb40a09cSJose R. Santos 	}
4389eb40a09cSJose R. Santos 
439025ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
439125ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
439225ed6e8aSDarrick J. Wong 			 "feature set");
439325ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4394d4da6c9cSLinus Torvalds 	}
4395818d276cSGirish Shilamkar 
4396ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4397ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4398ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4399ac27a0ecSDave Kleikamp 	case 0:
4400ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
440163f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
440263f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
440363f57933SAndrew Morton 		 */
4404dab291afSMingming Cao 		if (jbd2_journal_check_available_features
440527f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4406fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
440727f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
440827f394a7STyson Nottingham 		} else {
4409fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
441027f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
441127f394a7STyson Nottingham 		}
4412ac27a0ecSDave Kleikamp 		break;
4413ac27a0ecSDave Kleikamp 
4414617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4415617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4416dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4417dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4418b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4419b31e1552SEric Sandeen 			       "requested data journaling mode");
4420744692dcSJiaying Zhang 			goto failed_mount_wq;
4421ac27a0ecSDave Kleikamp 		}
4422ac27a0ecSDave Kleikamp 	default:
4423ac27a0ecSDave Kleikamp 		break;
4424ac27a0ecSDave Kleikamp 	}
4425ab04df78SJan Kara 
4426ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4427ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4428ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4429ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4430ab04df78SJan Kara 		goto failed_mount_wq;
4431ab04df78SJan Kara 	}
4432ab04df78SJan Kara 
4433b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4434ac27a0ecSDave Kleikamp 
443518aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
443618aadd47SBobi Jam 
4437ce7e010aSTheodore Ts'o no_journal:
4438cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
443947387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
444047387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4441cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4442cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
44439c191f70ST Makphaibulchoke 			goto failed_mount_wq;
44449c191f70ST Makphaibulchoke 		}
44459c191f70ST Makphaibulchoke 
4446dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4447dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4448dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4449dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4450dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4451dec214d0STahsin Erdogan 				goto failed_mount_wq;
4452dec214d0STahsin Erdogan 			}
4453dec214d0STahsin Erdogan 		}
4454cdb7ee4cSTahsin Erdogan 	}
4455dec214d0STahsin Erdogan 
4456e2b911c5SDarrick J. Wong 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
445709cbfeafSKirill A. Shutemov 	    (blocksize != PAGE_SIZE)) {
44581cb767cdSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44591cb767cdSTheodore Ts'o 			 "Unsupported blocksize for fs encryption");
44601cb767cdSTheodore Ts'o 		goto failed_mount_wq;
44611cb767cdSTheodore Ts'o 	}
44621cb767cdSTheodore Ts'o 
4463c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4464c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4465c93d8f88SEric Biggers 		goto failed_mount_wq;
4466c93d8f88SEric Biggers 	}
4467c93d8f88SEric Biggers 
4468bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4469e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4470e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
44716ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
44726ddb2447STheodore Ts'o 	}
44736ddb2447STheodore Ts'o 
4474fd89d5f2STejun Heo 	/*
4475952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4476952fc18eSTheodore Ts'o 	 * superblock if present.
4477952fc18eSTheodore Ts'o 	 */
4478952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4479952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4480952fc18eSTheodore Ts'o 	else {
448107aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
448207aa2ea1SLukas Czerner 		if (err)
4483952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4484952fc18eSTheodore Ts'o 	}
4485952fc18eSTheodore Ts'o 
4486952fc18eSTheodore Ts'o 	/*
4487fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4488fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4489fd89d5f2STejun Heo 	 */
44902e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
44912e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
44922e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
44932e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
449407aa2ea1SLukas Czerner 		ret = -ENOMEM;
44952e8fa54eSJan Kara 		goto failed_mount4;
44962e8fa54eSJan Kara 	}
44972e8fa54eSJan Kara 
4498ac27a0ecSDave Kleikamp 	/*
4499dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4500ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4501ac27a0ecSDave Kleikamp 	 */
4502ac27a0ecSDave Kleikamp 
45038a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
45041d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4505b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
45061d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
450732a9bb57SManish Katiyar 		root = NULL;
4508ac27a0ecSDave Kleikamp 		goto failed_mount4;
4509ac27a0ecSDave Kleikamp 	}
4510ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4511b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
451294bf608aSAl Viro 		iput(root);
4513ac27a0ecSDave Kleikamp 		goto failed_mount4;
4514ac27a0ecSDave Kleikamp 	}
4515b886ee3eSGabriel Krisman Bertazi 
4516b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4517b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4518b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4519b886ee3eSGabriel Krisman Bertazi #endif
4520b886ee3eSGabriel Krisman Bertazi 
452148fde701SAl Viro 	sb->s_root = d_make_root(root);
45221d1fe1eeSDavid Howells 	if (!sb->s_root) {
4523b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
45241d1fe1eeSDavid Howells 		ret = -ENOMEM;
45251d1fe1eeSDavid Howells 		goto failed_mount4;
45261d1fe1eeSDavid Howells 	}
4527ac27a0ecSDave Kleikamp 
4528c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4529c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
45301751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4531c89128a0SJaegeuk Kim 		ret = 0;
4532c89128a0SJaegeuk Kim 	} else if (ret)
4533c89128a0SJaegeuk Kim 		goto failed_mount4a;
4534ef7f3835SKalpak Shah 
45357bc04c5cSBarret Rhoden 	ext4_clamp_want_extra_isize(sb);
4536ef7f3835SKalpak Shah 
4537b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
453827dd4385SLukas Czerner 
45396fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
45406fd058f7STheodore Ts'o 	if (err) {
4541b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4542fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4543f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4544f9ae9cf5STheodore Ts'o 	}
4545f9ae9cf5STheodore Ts'o 
4546f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4547f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4548f9ae9cf5STheodore Ts'o 	if (err) {
4549f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4550f9ae9cf5STheodore Ts'o 			 err);
4551dcf2d804STao Ma 		goto failed_mount5;
4552c2774d84SAneesh Kumar K.V 	}
4553c2774d84SAneesh Kumar K.V 
4554d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4555d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4556d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
45574274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4558908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4559908c7f19STejun Heo 				  GFP_KERNEL);
4560d5e03cbbSTheodore Ts'o 	if (!err) {
4561d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4562d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
45634274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4564908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4565908c7f19STejun Heo 					  GFP_KERNEL);
4566d5e03cbbSTheodore Ts'o 	}
4567d5e03cbbSTheodore Ts'o 	if (!err)
4568d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4569908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4570d5e03cbbSTheodore Ts'o 	if (!err)
4571908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4572908c7f19STejun Heo 					  GFP_KERNEL);
4573c8585c6fSDaeho Jeong 	if (!err)
4574c8585c6fSDaeho Jeong 		err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
4575c8585c6fSDaeho Jeong 
4576d5e03cbbSTheodore Ts'o 	if (err) {
4577d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4578d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4579d5e03cbbSTheodore Ts'o 	}
4580d5e03cbbSTheodore Ts'o 
4581e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4582d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4583d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4584d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4585d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4586d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4587d5e03cbbSTheodore Ts'o 		}
4588d5e03cbbSTheodore Ts'o 
4589bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4590bfff6873SLukas Czerner 	if (err)
4591dcf2d804STao Ma 		goto failed_mount6;
4592bfff6873SLukas Czerner 
4593b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4594dcf2d804STao Ma 	if (err)
4595dcf2d804STao Ma 		goto failed_mount7;
45963197ebdbSTheodore Ts'o 
45979b2ff357SJan Kara #ifdef CONFIG_QUOTA
45989b2ff357SJan Kara 	/* Enable quota usage during mount. */
4599bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
46009b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
46019b2ff357SJan Kara 		if (err)
46029b2ff357SJan Kara 			goto failed_mount8;
46039b2ff357SJan Kara 	}
46049b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
46059b2ff357SJan Kara 
4606617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4607617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4608617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
46090390131bSFrank Mayhar 	if (needs_recovery) {
4610b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4611617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
46120390131bSFrank Mayhar 	}
46130390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
46140390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
46150390131bSFrank Mayhar 			descr = " journalled data mode";
46160390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
46170390131bSFrank Mayhar 			descr = " ordered data mode";
46180390131bSFrank Mayhar 		else
46190390131bSFrank Mayhar 			descr = " writeback data mode";
46200390131bSFrank Mayhar 	} else
46210390131bSFrank Mayhar 		descr = "out journal";
46220390131bSFrank Mayhar 
462379add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
462479add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
462579add3a3SLukas Czerner 		if (!blk_queue_discard(q))
462679add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
462779add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
462879add3a3SLukas Czerner 				 "the device does not support discard");
462979add3a3SLukas Czerner 	}
463079add3a3SLukas Czerner 
4631e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4632d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
46335aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
46345aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
46355aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
46368b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4637ac27a0ecSDave Kleikamp 
463866e61a9eSTheodore Ts'o 	if (es->s_error_count)
463966e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4640ac27a0ecSDave Kleikamp 
4641efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4642efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4643efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4644efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4645efbed4dcSTheodore Ts'o 
4646d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4647ac27a0ecSDave Kleikamp 	return 0;
4648ac27a0ecSDave Kleikamp 
4649617ba13bSMingming Cao cantfind_ext4:
4650ac27a0ecSDave Kleikamp 	if (!silent)
4651b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4652ac27a0ecSDave Kleikamp 	goto failed_mount;
4653ac27a0ecSDave Kleikamp 
465472ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
465572ba7450STheodore Ts'o failed_mount8:
4656ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
465772ba7450STheodore Ts'o #endif
4658dcf2d804STao Ma failed_mount7:
4659dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4660dcf2d804STao Ma failed_mount6:
4661f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4662d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4663b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4664d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4665d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4666d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4667d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4668af18e35bSVasily Averin 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
466900764937SAzat Khuzhin failed_mount5:
4670f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4671f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4672f9ae9cf5STheodore Ts'o failed_mount4a:
467394bf608aSAl Viro 	dput(sb->s_root);
467432a9bb57SManish Katiyar 	sb->s_root = NULL;
467594bf608aSAl Viro failed_mount4:
4676b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
46772e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
46782e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
46794c0425ffSMingming Cao failed_mount_wq:
4680dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4681dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
468250c15df6SChengguang Xu 
468347387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
468447387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
468550c15df6SChengguang Xu 
46860390131bSFrank Mayhar 	if (sbi->s_journal) {
4687dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
468847b4a50bSJan Kara 		sbi->s_journal = NULL;
46890390131bSFrank Mayhar 	}
469050460fe8SDarrick J. Wong failed_mount3a:
4691d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4692eb68d0e2SZheng Liu failed_mount3:
46939105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4694c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4695c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4696ac27a0ecSDave Kleikamp failed_mount2:
4697ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4698ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4699b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4700ac27a0ecSDave Kleikamp failed_mount:
47010441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
47020441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4703c83ad55eSGabriel Krisman Bertazi 
4704c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4705c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4706c83ad55eSGabriel Krisman Bertazi #endif
4707c83ad55eSGabriel Krisman Bertazi 
4708ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4709a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
47100ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4711ac27a0ecSDave Kleikamp #endif
4712617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4713ac27a0ecSDave Kleikamp 	brelse(bh);
4714ac27a0ecSDave Kleikamp out_fail:
4715ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4716f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
47175aee0f8aSTheodore Ts'o out_free_base:
4718ac27a0ecSDave Kleikamp 	kfree(sbi);
4719d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
47205e405595SDan Williams 	fs_put_dax(dax_dev);
472107aa2ea1SLukas Czerner 	return err ? err : ret;
4722ac27a0ecSDave Kleikamp }
4723ac27a0ecSDave Kleikamp 
4724ac27a0ecSDave Kleikamp /*
4725ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4726ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4727ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4728ac27a0ecSDave Kleikamp  */
4729617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4730ac27a0ecSDave Kleikamp {
4731617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4732ac27a0ecSDave Kleikamp 
4733ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
473430773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
473530773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4736ac27a0ecSDave Kleikamp 
4737a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4738ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4739dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4740ac27a0ecSDave Kleikamp 	else
4741dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
47425bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
47435bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
47445bf5683aSHidehiro Kawai 	else
47455bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4746a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4747ac27a0ecSDave Kleikamp }
4748ac27a0ecSDave Kleikamp 
4749c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4750ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4751ac27a0ecSDave Kleikamp {
4752ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4753ac27a0ecSDave Kleikamp 
4754c6cb7e77SEric Whitney 	/*
4755c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4756c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4757c6cb7e77SEric Whitney 	 * will try to delete it.
4758c6cb7e77SEric Whitney 	 */
47598a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
47601d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4761b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4762ac27a0ecSDave Kleikamp 		return NULL;
4763ac27a0ecSDave Kleikamp 	}
4764ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4765ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4766ac27a0ecSDave Kleikamp 		iput(journal_inode);
4767b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4768ac27a0ecSDave Kleikamp 		return NULL;
4769ac27a0ecSDave Kleikamp 	}
4770ac27a0ecSDave Kleikamp 
4771e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4772ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
47731d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4774b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4775ac27a0ecSDave Kleikamp 		iput(journal_inode);
4776ac27a0ecSDave Kleikamp 		return NULL;
4777ac27a0ecSDave Kleikamp 	}
4778c6cb7e77SEric Whitney 	return journal_inode;
4779c6cb7e77SEric Whitney }
4780c6cb7e77SEric Whitney 
4781c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4782c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4783c6cb7e77SEric Whitney {
4784c6cb7e77SEric Whitney 	struct inode *journal_inode;
4785c6cb7e77SEric Whitney 	journal_t *journal;
4786c6cb7e77SEric Whitney 
4787c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4788c6cb7e77SEric Whitney 
4789c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4790c6cb7e77SEric Whitney 	if (!journal_inode)
4791c6cb7e77SEric Whitney 		return NULL;
4792ac27a0ecSDave Kleikamp 
4793dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4794ac27a0ecSDave Kleikamp 	if (!journal) {
4795b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4796ac27a0ecSDave Kleikamp 		iput(journal_inode);
4797ac27a0ecSDave Kleikamp 		return NULL;
4798ac27a0ecSDave Kleikamp 	}
4799ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4800617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4801ac27a0ecSDave Kleikamp 	return journal;
4802ac27a0ecSDave Kleikamp }
4803ac27a0ecSDave Kleikamp 
4804617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4805ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4806ac27a0ecSDave Kleikamp {
4807ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4808ac27a0ecSDave Kleikamp 	journal_t *journal;
4809617ba13bSMingming Cao 	ext4_fsblk_t start;
4810617ba13bSMingming Cao 	ext4_fsblk_t len;
4811ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4812617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4813ac27a0ecSDave Kleikamp 	unsigned long offset;
4814617ba13bSMingming Cao 	struct ext4_super_block *es;
4815ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4816ac27a0ecSDave Kleikamp 
4817e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
48180390131bSFrank Mayhar 
4819b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4820ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4821ac27a0ecSDave Kleikamp 		return NULL;
4822ac27a0ecSDave Kleikamp 
4823ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4824e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4825ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4826b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4827b31e1552SEric Sandeen 			"blocksize too small for journal device");
4828ac27a0ecSDave Kleikamp 		goto out_bdev;
4829ac27a0ecSDave Kleikamp 	}
4830ac27a0ecSDave Kleikamp 
4831617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4832617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4833ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4834ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4835b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4836b31e1552SEric Sandeen 		       "external journal");
4837ac27a0ecSDave Kleikamp 		goto out_bdev;
4838ac27a0ecSDave Kleikamp 	}
4839ac27a0ecSDave Kleikamp 
48402716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4841617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4842ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4843617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4844b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4845b31e1552SEric Sandeen 					"bad superblock");
4846ac27a0ecSDave Kleikamp 		brelse(bh);
4847ac27a0ecSDave Kleikamp 		goto out_bdev;
4848ac27a0ecSDave Kleikamp 	}
4849ac27a0ecSDave Kleikamp 
4850df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4851df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4852df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4853df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4854df4763beSDarrick J. Wong 				       "corrupt superblock");
4855df4763beSDarrick J. Wong 		brelse(bh);
4856df4763beSDarrick J. Wong 		goto out_bdev;
4857df4763beSDarrick J. Wong 	}
4858df4763beSDarrick J. Wong 
4859617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4860b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4861ac27a0ecSDave Kleikamp 		brelse(bh);
4862ac27a0ecSDave Kleikamp 		goto out_bdev;
4863ac27a0ecSDave Kleikamp 	}
4864ac27a0ecSDave Kleikamp 
4865bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4866ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4867ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4868ac27a0ecSDave Kleikamp 
4869dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4870ac27a0ecSDave Kleikamp 					start, len, blocksize);
4871ac27a0ecSDave Kleikamp 	if (!journal) {
4872b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4873ac27a0ecSDave Kleikamp 		goto out_bdev;
4874ac27a0ecSDave Kleikamp 	}
4875ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4876dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4877ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4878ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4879b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4880ac27a0ecSDave Kleikamp 		goto out_journal;
4881ac27a0ecSDave Kleikamp 	}
4882ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4883b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4884b31e1552SEric Sandeen 					"user (unsupported) - %d",
4885ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4886ac27a0ecSDave Kleikamp 		goto out_journal;
4887ac27a0ecSDave Kleikamp 	}
4888617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4889617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4890ac27a0ecSDave Kleikamp 	return journal;
48910b8e58a1SAndreas Dilger 
4892ac27a0ecSDave Kleikamp out_journal:
4893dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4894ac27a0ecSDave Kleikamp out_bdev:
4895617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4896ac27a0ecSDave Kleikamp 	return NULL;
4897ac27a0ecSDave Kleikamp }
4898ac27a0ecSDave Kleikamp 
4899617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4900617ba13bSMingming Cao 			     struct ext4_super_block *es,
4901ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4902ac27a0ecSDave Kleikamp {
4903ac27a0ecSDave Kleikamp 	journal_t *journal;
4904ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4905ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4906ac27a0ecSDave Kleikamp 	int err = 0;
4907ac27a0ecSDave Kleikamp 	int really_read_only;
4908ac27a0ecSDave Kleikamp 
4909e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
49100390131bSFrank Mayhar 
4911ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4912ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4913b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4914b31e1552SEric Sandeen 			"numbers have changed");
4915ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4916ac27a0ecSDave Kleikamp 	} else
4917ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4918ac27a0ecSDave Kleikamp 
4919ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4920ac27a0ecSDave Kleikamp 
4921ac27a0ecSDave Kleikamp 	/*
4922ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4923ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4924ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4925ac27a0ecSDave Kleikamp 	 */
4926e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4927bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
4928b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4929b31e1552SEric Sandeen 					"required on readonly filesystem");
4930ac27a0ecSDave Kleikamp 			if (really_read_only) {
4931b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4932d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
4933d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
4934ac27a0ecSDave Kleikamp 				return -EROFS;
4935ac27a0ecSDave Kleikamp 			}
4936b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4937b31e1552SEric Sandeen 			       "be enabled during recovery");
4938ac27a0ecSDave Kleikamp 		}
4939ac27a0ecSDave Kleikamp 	}
4940ac27a0ecSDave Kleikamp 
4941ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4942b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4943b31e1552SEric Sandeen 		       "and inode journals!");
4944ac27a0ecSDave Kleikamp 		return -EINVAL;
4945ac27a0ecSDave Kleikamp 	}
4946ac27a0ecSDave Kleikamp 
4947ac27a0ecSDave Kleikamp 	if (journal_inum) {
4948617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4949ac27a0ecSDave Kleikamp 			return -EINVAL;
4950ac27a0ecSDave Kleikamp 	} else {
4951617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4952ac27a0ecSDave Kleikamp 			return -EINVAL;
4953ac27a0ecSDave Kleikamp 	}
4954ac27a0ecSDave Kleikamp 
495590576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4956b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
49574776004fSTheodore Ts'o 
4958e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4959dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
49601c13d5c0STheodore Ts'o 	if (!err) {
49611c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
49621c13d5c0STheodore Ts'o 		if (save)
49631c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
49641c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4965dab291afSMingming Cao 		err = jbd2_journal_load(journal);
49661c13d5c0STheodore Ts'o 		if (save)
49671c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
49681c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
49691c13d5c0STheodore Ts'o 		kfree(save);
49701c13d5c0STheodore Ts'o 	}
4971ac27a0ecSDave Kleikamp 
4972ac27a0ecSDave Kleikamp 	if (err) {
4973b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4974dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4975ac27a0ecSDave Kleikamp 		return err;
4976ac27a0ecSDave Kleikamp 	}
4977ac27a0ecSDave Kleikamp 
4978617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4979617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4980ac27a0ecSDave Kleikamp 
4981c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4982ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4983ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4984ac27a0ecSDave Kleikamp 
4985ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4986e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4987ac27a0ecSDave Kleikamp 	}
4988ac27a0ecSDave Kleikamp 
4989ac27a0ecSDave Kleikamp 	return 0;
4990ac27a0ecSDave Kleikamp }
4991ac27a0ecSDave Kleikamp 
4992e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4993ac27a0ecSDave Kleikamp {
4994e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4995617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4996c4be0c1dSTakashi Sato 	int error = 0;
4997ac27a0ecSDave Kleikamp 
4998bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4999c4be0c1dSTakashi Sato 		return error;
5000a17712c8SJon Derrick 
5001a17712c8SJon Derrick 	/*
5002a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
5003a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
5004a17712c8SJon Derrick 	 */
5005a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
5006a17712c8SJon Derrick 		return error;
5007a17712c8SJon Derrick 
500871290b36STheodore Ts'o 	/*
500971290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
501071290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
501171290b36STheodore Ts'o 	 * write time when we are mounting the root file system
501271290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
501371290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
501471290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
501571290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
501671290b36STheodore Ts'o 	 * to complain and force a full file system check.
501771290b36STheodore Ts'o 	 */
50181751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
50196a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5020f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5021afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5022afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5023dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5024dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5025afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5026f613dfcbSTheodore Ts'o 	else
5027f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5028f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5029d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
503057042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
503157042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
503257042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5033d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
50347f93cff9STheodore Ts'o 		es->s_free_inodes_count =
50357f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
50365d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5037ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
503806db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
50391566a48aSTheodore Ts'o 	if (sync)
50404743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5041e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
50424743f839SPranay Kr. Srivastava 		/*
50434743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
50444743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
50454743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
50464743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
50474743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
50484743f839SPranay Kr. Srivastava 		 * write and hope for the best.
50494743f839SPranay Kr. Srivastava 		 */
50504743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
50514743f839SPranay Kr. Srivastava 		       "superblock detected");
50524743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
50534743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
50544743f839SPranay Kr. Srivastava 	}
5055ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5056914258bfSTheodore Ts'o 	if (sync) {
50571566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5058564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
505900473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5060c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5061b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5062b31e1552SEric Sandeen 			       "superblock");
5063914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5064914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5065914258bfSTheodore Ts'o 		}
5066914258bfSTheodore Ts'o 	}
5067c4be0c1dSTakashi Sato 	return error;
5068ac27a0ecSDave Kleikamp }
5069ac27a0ecSDave Kleikamp 
5070ac27a0ecSDave Kleikamp /*
5071ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5072ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5073ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5074ac27a0ecSDave Kleikamp  */
5075617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5076617ba13bSMingming Cao 					struct ext4_super_block *es)
5077ac27a0ecSDave Kleikamp {
5078617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5079ac27a0ecSDave Kleikamp 
5080e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
50810390131bSFrank Mayhar 		BUG_ON(journal != NULL);
50820390131bSFrank Mayhar 		return;
50830390131bSFrank Mayhar 	}
5084dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
50857ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
50867ffe1ea8SHidehiro Kawai 		goto out;
50877ffe1ea8SHidehiro Kawai 
5088bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5089e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5090e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5091ac27a0ecSDave Kleikamp 	}
50927ffe1ea8SHidehiro Kawai 
50937ffe1ea8SHidehiro Kawai out:
5094dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5095ac27a0ecSDave Kleikamp }
5096ac27a0ecSDave Kleikamp 
5097ac27a0ecSDave Kleikamp /*
5098ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5099ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5100ac27a0ecSDave Kleikamp  * main filesystem now.
5101ac27a0ecSDave Kleikamp  */
5102617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5103617ba13bSMingming Cao 				   struct ext4_super_block *es)
5104ac27a0ecSDave Kleikamp {
5105ac27a0ecSDave Kleikamp 	journal_t *journal;
5106ac27a0ecSDave Kleikamp 	int j_errno;
5107ac27a0ecSDave Kleikamp 	const char *errstr;
5108ac27a0ecSDave Kleikamp 
5109e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
51100390131bSFrank Mayhar 
5111617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5112ac27a0ecSDave Kleikamp 
5113ac27a0ecSDave Kleikamp 	/*
5114ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5115617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5116ac27a0ecSDave Kleikamp 	 */
5117ac27a0ecSDave Kleikamp 
5118dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5119ac27a0ecSDave Kleikamp 	if (j_errno) {
5120ac27a0ecSDave Kleikamp 		char nbuf[16];
5121ac27a0ecSDave Kleikamp 
5122617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
512312062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5124ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
512512062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5126ac27a0ecSDave Kleikamp 
5127617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5128617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5129e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5130ac27a0ecSDave Kleikamp 
5131dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5132d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5133ac27a0ecSDave Kleikamp 	}
5134ac27a0ecSDave Kleikamp }
5135ac27a0ecSDave Kleikamp 
5136ac27a0ecSDave Kleikamp /*
5137ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5138ac27a0ecSDave Kleikamp  * and wait on the commit.
5139ac27a0ecSDave Kleikamp  */
5140617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5141ac27a0ecSDave Kleikamp {
5142ac27a0ecSDave Kleikamp 	journal_t *journal;
5143ac27a0ecSDave Kleikamp 
5144bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5145ac27a0ecSDave Kleikamp 		return 0;
5146ac27a0ecSDave Kleikamp 
5147617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5148b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5149ac27a0ecSDave Kleikamp }
5150ac27a0ecSDave Kleikamp 
5151617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5152ac27a0ecSDave Kleikamp {
515314ce0cb4STheodore Ts'o 	int ret = 0;
51549eddacf9SJan Kara 	tid_t target;
515506a407f1SDmitry Monakhov 	bool needs_barrier = false;
51568d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5157ac27a0ecSDave Kleikamp 
515849598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
51590db1ff22STheodore Ts'o 		return 0;
51600db1ff22STheodore Ts'o 
51619bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
51622e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5163a1177825SJan Kara 	/*
5164a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5165a1177825SJan Kara 	 * no dirty dquots
5166a1177825SJan Kara 	 */
5167a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
516806a407f1SDmitry Monakhov 	/*
516906a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
517006a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
517106a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
517206a407f1SDmitry Monakhov 	 */
5173bda32530STheodore Ts'o 	if (sbi->s_journal) {
517406a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
517506a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
517606a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
517706a407f1SDmitry Monakhov 			needs_barrier = true;
517806a407f1SDmitry Monakhov 
51798d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5180ac27a0ecSDave Kleikamp 			if (wait)
5181bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5182bda32530STheodore Ts'o 							   target);
51830390131bSFrank Mayhar 		}
5184bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5185bda32530STheodore Ts'o 		needs_barrier = true;
518606a407f1SDmitry Monakhov 	if (needs_barrier) {
518706a407f1SDmitry Monakhov 		int err;
518806a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
518906a407f1SDmitry Monakhov 		if (!ret)
519006a407f1SDmitry Monakhov 			ret = err;
519106a407f1SDmitry Monakhov 	}
519206a407f1SDmitry Monakhov 
519306a407f1SDmitry Monakhov 	return ret;
519406a407f1SDmitry Monakhov }
519506a407f1SDmitry Monakhov 
5196ac27a0ecSDave Kleikamp /*
5197ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5198ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5199be4f27d3SYongqiang Yang  *
5200be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
52018e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
52028e8ad8a5SJan Kara  * modifications.
5203ac27a0ecSDave Kleikamp  */
5204c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5205ac27a0ecSDave Kleikamp {
5206c4be0c1dSTakashi Sato 	int error = 0;
5207c4be0c1dSTakashi Sato 	journal_t *journal;
5208ac27a0ecSDave Kleikamp 
5209bc98a42cSDavid Howells 	if (sb_rdonly(sb))
52109ca92389STheodore Ts'o 		return 0;
52119ca92389STheodore Ts'o 
5212c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5213ac27a0ecSDave Kleikamp 
5214bb044576STheodore Ts'o 	if (journal) {
5215ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5216dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
52177ffe1ea8SHidehiro Kawai 
52187ffe1ea8SHidehiro Kawai 		/*
5219bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5220bb044576STheodore Ts'o 		 * flush the journal.
52217ffe1ea8SHidehiro Kawai 		 */
5222c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
52236b0310fbSEric Sandeen 		if (error < 0)
52246b0310fbSEric Sandeen 			goto out;
5225ac27a0ecSDave Kleikamp 
5226ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5227e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5228c642dc9eSEric Sandeen 	}
5229c642dc9eSEric Sandeen 
5230e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
52316b0310fbSEric Sandeen out:
5232bb044576STheodore Ts'o 	if (journal)
52338e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5234bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
52356b0310fbSEric Sandeen 	return error;
5236ac27a0ecSDave Kleikamp }
5237ac27a0ecSDave Kleikamp 
5238ac27a0ecSDave Kleikamp /*
5239ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5240ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5241ac27a0ecSDave Kleikamp  */
5242c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5243ac27a0ecSDave Kleikamp {
5244bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
52459ca92389STheodore Ts'o 		return 0;
52469ca92389STheodore Ts'o 
5247c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
52489ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5249e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5250c642dc9eSEric Sandeen 	}
5251c642dc9eSEric Sandeen 
5252e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5253c4be0c1dSTakashi Sato 	return 0;
5254ac27a0ecSDave Kleikamp }
5255ac27a0ecSDave Kleikamp 
5256673c6100STheodore Ts'o /*
5257673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5258673c6100STheodore Ts'o  */
5259673c6100STheodore Ts'o struct ext4_mount_options {
5260673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5261a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
526208cefc7aSEric W. Biederman 	kuid_t s_resuid;
526308cefc7aSEric W. Biederman 	kgid_t s_resgid;
5264673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5265673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5266673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5267673c6100STheodore Ts'o 	int s_jquota_fmt;
5268a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5269673c6100STheodore Ts'o #endif
5270673c6100STheodore Ts'o };
5271673c6100STheodore Ts'o 
5272617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5273ac27a0ecSDave Kleikamp {
5274617ba13bSMingming Cao 	struct ext4_super_block *es;
5275617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5276ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5277617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5278c79d967dSChristoph Hellwig 	int enable_quota = 0;
52798a266467STheodore Ts'o 	ext4_group_t g;
5280b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5281c5e06d10SJohann Lombardi 	int err = 0;
5282ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
528303dafb5fSChen Gang 	int i, j;
528433458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5285ac27a0ecSDave Kleikamp #endif
5286d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5287ac27a0ecSDave Kleikamp 
528821ac738eSChengguang Xu 	if (data && !orig_data)
528921ac738eSChengguang Xu 		return -ENOMEM;
529021ac738eSChengguang Xu 
5291ac27a0ecSDave Kleikamp 	/* Store the original options */
5292ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5293ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5294a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5295ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5296ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5297ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
529830773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
529930773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5300ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5301ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5302a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
530303dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
530433458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
530533458eabSTheodore Ts'o 
530633458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
530703dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
530803dafb5fSChen Gang 				for (j = 0; j < i; j++)
530903dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
53103e36a163SWei Yongjun 				kfree(orig_data);
531103dafb5fSChen Gang 				return -ENOMEM;
531203dafb5fSChen Gang 			}
531303dafb5fSChen Gang 		} else
531403dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5315ac27a0ecSDave Kleikamp #endif
5316b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5317b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5318ac27a0ecSDave Kleikamp 
5319661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5320ac27a0ecSDave Kleikamp 		err = -EINVAL;
5321ac27a0ecSDave Kleikamp 		goto restore_opts;
5322ac27a0ecSDave Kleikamp 	}
5323ac27a0ecSDave Kleikamp 
53247bc04c5cSBarret Rhoden 	ext4_clamp_want_extra_isize(sb);
53257bc04c5cSBarret Rhoden 
53266b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
53276b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
53286b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
53292d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
53302d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5331c6d3d56dSDarrick J. Wong 	}
5332c6d3d56dSDarrick J. Wong 
53336ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
53346ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
53356ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53366ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
53376ae6514bSPiotr Sarna 			err = -EINVAL;
53386ae6514bSPiotr Sarna 			goto restore_opts;
53396ae6514bSPiotr Sarna 		}
53406ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
53416ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53426ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
53436ae6514bSPiotr Sarna 			err = -EINVAL;
53446ae6514bSPiotr Sarna 			goto restore_opts;
53456ae6514bSPiotr Sarna 		}
5346923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
5347923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5348923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5349923ae0ffSRoss Zwisler 			err = -EINVAL;
5350923ae0ffSRoss Zwisler 			goto restore_opts;
5351923ae0ffSRoss Zwisler 		}
5352ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5353ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5354ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5355ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5356ab04df78SJan Kara 			err = -EINVAL;
5357ab04df78SJan Kara 			goto restore_opts;
5358ab04df78SJan Kara 		}
5359923ae0ffSRoss Zwisler 	}
5360923ae0ffSRoss Zwisler 
5361cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5362cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5363cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5364cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5365cdb7ee4cSTahsin Erdogan 	}
5366cdb7ee4cSTahsin Erdogan 
5367923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
5368923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5369923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
5370923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
53716ae6514bSPiotr Sarna 	}
53726ae6514bSPiotr Sarna 
53734ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
5374c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
5375ac27a0ecSDave Kleikamp 
53761751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
53771751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5378ac27a0ecSDave Kleikamp 
5379ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5380ac27a0ecSDave Kleikamp 
5381b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5382617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5383b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5384b3881f74STheodore Ts'o 	}
5385ac27a0ecSDave Kleikamp 
53861751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
53871751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5388a2fd66d0STheodore Ts'o 
53891751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
53904ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5391ac27a0ecSDave Kleikamp 			err = -EROFS;
5392ac27a0ecSDave Kleikamp 			goto restore_opts;
5393ac27a0ecSDave Kleikamp 		}
5394ac27a0ecSDave Kleikamp 
53951751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
539638c03b34STheodore Ts'o 			err = sync_filesystem(sb);
539738c03b34STheodore Ts'o 			if (err < 0)
539838c03b34STheodore Ts'o 				goto restore_opts;
53990f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
54000f0dd62fSChristoph Hellwig 			if (err < 0)
5401c79d967dSChristoph Hellwig 				goto restore_opts;
5402c79d967dSChristoph Hellwig 
5403ac27a0ecSDave Kleikamp 			/*
5404ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5405ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5406ac27a0ecSDave Kleikamp 			 */
54071751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5408ac27a0ecSDave Kleikamp 
5409ac27a0ecSDave Kleikamp 			/*
5410ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5411ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5412ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5413ac27a0ecSDave Kleikamp 			 */
5414617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5415617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5416ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5417ac27a0ecSDave Kleikamp 
5418a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5419617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
54202dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
54212dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5422ac27a0ecSDave Kleikamp 		} else {
5423a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5424e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
54252cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5426ac27a0ecSDave Kleikamp 				err = -EROFS;
5427ac27a0ecSDave Kleikamp 				goto restore_opts;
5428ac27a0ecSDave Kleikamp 			}
5429ead6596bSEric Sandeen 			/*
54308a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
54310b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
54328a266467STheodore Ts'o 			 */
54338a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
54348a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
54358a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
54368a266467STheodore Ts'o 
5437feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5438b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5439b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5440e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
54418a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
54426a797d27SDarrick J. Wong 					err = -EFSBADCRC;
54438a266467STheodore Ts'o 					goto restore_opts;
54448a266467STheodore Ts'o 				}
54458a266467STheodore Ts'o 			}
54468a266467STheodore Ts'o 
54478a266467STheodore Ts'o 			/*
5448ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5449ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5450ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5451ead6596bSEric Sandeen 			 */
5452ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5453b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5454ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5455ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5456b31e1552SEric Sandeen 				       "umount/remount instead");
5457ead6596bSEric Sandeen 				err = -EINVAL;
5458ead6596bSEric Sandeen 				goto restore_opts;
5459ead6596bSEric Sandeen 			}
5460ead6596bSEric Sandeen 
5461ac27a0ecSDave Kleikamp 			/*
5462ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5463ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5464ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5465ac27a0ecSDave Kleikamp 			 * the partition.)
5466ac27a0ecSDave Kleikamp 			 */
54670390131bSFrank Mayhar 			if (sbi->s_journal)
5468617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5469ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5470c89128a0SJaegeuk Kim 
5471c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5472c89128a0SJaegeuk Kim 			if (err)
5473c89128a0SJaegeuk Kim 				goto restore_opts;
5474c89128a0SJaegeuk Kim 
54751751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5476e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5477c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5478c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5479c5e06d10SJohann Lombardi 					err = -EROFS;
5480c5e06d10SJohann Lombardi 					goto restore_opts;
5481c5e06d10SJohann Lombardi 				}
5482c79d967dSChristoph Hellwig 			enable_quota = 1;
5483ac27a0ecSDave Kleikamp 		}
5484ac27a0ecSDave Kleikamp 	}
5485bfff6873SLukas Czerner 
5486bfff6873SLukas Czerner 	/*
5487bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5488bfff6873SLukas Czerner 	 * current settings
5489bfff6873SLukas Czerner 	 */
5490bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5491bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5492bfff6873SLukas Czerner 	else {
5493bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5494bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5495bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5496bfff6873SLukas Czerner 	}
5497bfff6873SLukas Czerner 
54986fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5499c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5500c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5501c89128a0SJaegeuk Kim 		if (err)
5502c89128a0SJaegeuk Kim 			goto restore_opts;
5503c89128a0SJaegeuk Kim 	}
55040390131bSFrank Mayhar 
5505ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5506ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5507a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5508ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
55097c319d32SAditya Kali 	if (enable_quota) {
55107c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
55110f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5512e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
55137c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
551407724f98STheodore Ts'o 			if (err)
55157c319d32SAditya Kali 				goto restore_opts;
55167c319d32SAditya Kali 		}
55177c319d32SAditya Kali 	}
55187c319d32SAditya Kali #endif
5519d4c402d9SCurt Wohlgemuth 
55201751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5521d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5522d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5523ac27a0ecSDave Kleikamp 	return 0;
55240b8e58a1SAndreas Dilger 
5525ac27a0ecSDave Kleikamp restore_opts:
5526ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5527ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5528a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5529ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5530ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5531ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
553230773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
553330773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5534ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5535ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5536a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
553733458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
553833458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5539ac27a0ecSDave Kleikamp 	}
554033458eabSTheodore Ts'o 	synchronize_rcu();
554133458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
554233458eabSTheodore Ts'o 		kfree(to_free[i]);
5543ac27a0ecSDave Kleikamp #endif
5544d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5545ac27a0ecSDave Kleikamp 	return err;
5546ac27a0ecSDave Kleikamp }
5547ac27a0ecSDave Kleikamp 
5548689c958cSLi Xi #ifdef CONFIG_QUOTA
5549689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5550689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5551689c958cSLi Xi {
5552689c958cSLi Xi 	struct kqid qid;
5553689c958cSLi Xi 	struct dquot *dquot;
5554689c958cSLi Xi 	u64 limit;
5555689c958cSLi Xi 	u64 curblock;
5556689c958cSLi Xi 
5557689c958cSLi Xi 	qid = make_kqid_projid(projid);
5558689c958cSLi Xi 	dquot = dqget(sb, qid);
5559689c958cSLi Xi 	if (IS_ERR(dquot))
5560689c958cSLi Xi 		return PTR_ERR(dquot);
55617b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5562689c958cSLi Xi 
5563689c958cSLi Xi 	limit = (dquot->dq_dqb.dqb_bsoftlimit ?
5564689c958cSLi Xi 		 dquot->dq_dqb.dqb_bsoftlimit :
5565689c958cSLi Xi 		 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
5566689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5567f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5568f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5569689c958cSLi Xi 		buf->f_blocks = limit;
5570689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5571689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5572689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5573689c958cSLi Xi 	}
5574689c958cSLi Xi 
5575689c958cSLi Xi 	limit = dquot->dq_dqb.dqb_isoftlimit ?
5576689c958cSLi Xi 		dquot->dq_dqb.dqb_isoftlimit :
5577689c958cSLi Xi 		dquot->dq_dqb.dqb_ihardlimit;
5578689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5579689c958cSLi Xi 		buf->f_files = limit;
5580689c958cSLi Xi 		buf->f_ffree =
5581689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5582689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5583689c958cSLi Xi 	}
5584689c958cSLi Xi 
55857b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5586689c958cSLi Xi 	dqput(dquot);
5587689c958cSLi Xi 	return 0;
5588689c958cSLi Xi }
5589689c958cSLi Xi #endif
5590689c958cSLi Xi 
5591617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5592ac27a0ecSDave Kleikamp {
5593ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5594617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5595617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
559627dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5597960cc398SPekka Enberg 	u64 fsid;
5598d02a9391SKazuya Mio 	s64 bfree;
559927dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5600ac27a0ecSDave Kleikamp 
5601952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5602952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5603ac27a0ecSDave Kleikamp 
5604617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5605ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5606b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
560757042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
560857042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5609d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
561057042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
561127dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
561227dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
561327dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5614ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5615ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
561652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5617617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5618960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5619960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5620960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5621960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
56220b8e58a1SAndreas Dilger 
5623689c958cSLi Xi #ifdef CONFIG_QUOTA
5624689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5625689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5626689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5627689c958cSLi Xi #endif
5628ac27a0ecSDave Kleikamp 	return 0;
5629ac27a0ecSDave Kleikamp }
5630ac27a0ecSDave Kleikamp 
5631ac27a0ecSDave Kleikamp 
5632ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5633ac27a0ecSDave Kleikamp 
5634bc8230eeSJan Kara /*
5635bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5636bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5637bc8230eeSJan Kara  */
5638ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5639ac27a0ecSDave Kleikamp {
56404c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5641ac27a0ecSDave Kleikamp }
5642ac27a0ecSDave Kleikamp 
5643617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5644ac27a0ecSDave Kleikamp {
5645ac27a0ecSDave Kleikamp 	int ret, err;
5646ac27a0ecSDave Kleikamp 	handle_t *handle;
5647ac27a0ecSDave Kleikamp 	struct inode *inode;
5648ac27a0ecSDave Kleikamp 
5649ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
56509924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5651617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5652ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5653ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5654ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5655617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5656ac27a0ecSDave Kleikamp 	if (!ret)
5657ac27a0ecSDave Kleikamp 		ret = err;
5658ac27a0ecSDave Kleikamp 	return ret;
5659ac27a0ecSDave Kleikamp }
5660ac27a0ecSDave Kleikamp 
5661617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5662ac27a0ecSDave Kleikamp {
5663ac27a0ecSDave Kleikamp 	int ret, err;
5664ac27a0ecSDave Kleikamp 	handle_t *handle;
5665ac27a0ecSDave Kleikamp 
56669924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5667617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5668ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5669ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5670ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5671617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5672ac27a0ecSDave Kleikamp 	if (!ret)
5673ac27a0ecSDave Kleikamp 		ret = err;
5674ac27a0ecSDave Kleikamp 	return ret;
5675ac27a0ecSDave Kleikamp }
5676ac27a0ecSDave Kleikamp 
5677617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5678ac27a0ecSDave Kleikamp {
5679ac27a0ecSDave Kleikamp 	int ret, err;
5680ac27a0ecSDave Kleikamp 	handle_t *handle;
5681ac27a0ecSDave Kleikamp 
56829924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5683617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
56849c3013e9SJan Kara 	if (IS_ERR(handle)) {
56859c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
56869c3013e9SJan Kara 		dquot_release(dquot);
5687ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
56889c3013e9SJan Kara 	}
5689ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5690617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5691ac27a0ecSDave Kleikamp 	if (!ret)
5692ac27a0ecSDave Kleikamp 		ret = err;
5693ac27a0ecSDave Kleikamp 	return ret;
5694ac27a0ecSDave Kleikamp }
5695ac27a0ecSDave Kleikamp 
5696617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5697ac27a0ecSDave Kleikamp {
5698262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5699262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5700262b4662SJan Kara 
57012c8be6b2SJan Kara 	/* Are we journaling quotas? */
5702e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5703262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5704ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5705617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5706ac27a0ecSDave Kleikamp 	} else {
5707ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5708ac27a0ecSDave Kleikamp 	}
5709ac27a0ecSDave Kleikamp }
5710ac27a0ecSDave Kleikamp 
5711617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5712ac27a0ecSDave Kleikamp {
5713ac27a0ecSDave Kleikamp 	int ret, err;
5714ac27a0ecSDave Kleikamp 	handle_t *handle;
5715ac27a0ecSDave Kleikamp 
5716ac27a0ecSDave Kleikamp 	/* Data block + inode block */
57172b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5718ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5719ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5720ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5721617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5722ac27a0ecSDave Kleikamp 	if (!ret)
5723ac27a0ecSDave Kleikamp 		ret = err;
5724ac27a0ecSDave Kleikamp 	return ret;
5725ac27a0ecSDave Kleikamp }
5726ac27a0ecSDave Kleikamp 
5727ac27a0ecSDave Kleikamp /*
5728ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5729ac27a0ecSDave Kleikamp  * the quota file and such...
5730ac27a0ecSDave Kleikamp  */
5731617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5732ac27a0ecSDave Kleikamp {
573333458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5734617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5735ac27a0ecSDave Kleikamp }
5736ac27a0ecSDave Kleikamp 
5737daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5738daf647d2STheodore Ts'o {
5739daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5740daf647d2STheodore Ts'o 
5741daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5742daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5743daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5744daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5745daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5746daf647d2STheodore Ts'o 	 */
5747daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5748daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5749daf647d2STheodore Ts'o }
5750daf647d2STheodore Ts'o 
5751ac27a0ecSDave Kleikamp /*
5752ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5753ac27a0ecSDave Kleikamp  */
5754617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
57558c54ca9cSAl Viro 			 const struct path *path)
5756ac27a0ecSDave Kleikamp {
5757ac27a0ecSDave Kleikamp 	int err;
5758ac27a0ecSDave Kleikamp 
5759ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5760ac27a0ecSDave Kleikamp 		return -EINVAL;
57610623543bSJan Kara 
5762ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5763d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5764ac27a0ecSDave Kleikamp 		return -EXDEV;
57650623543bSJan Kara 	/* Journaling quota? */
57660623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
57672b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5768f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5769b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5770b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5771b31e1552SEric Sandeen 				"Journaled quota will not work");
577291389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
577391389240SJan Kara 	} else {
577491389240SJan Kara 		/*
577591389240SJan Kara 		 * Clear the flag just in case mount options changed since
577691389240SJan Kara 		 * last time.
577791389240SJan Kara 		 */
577891389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
57790623543bSJan Kara 	}
57800623543bSJan Kara 
57810623543bSJan Kara 	/*
57820623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
57830623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
57840623543bSJan Kara 	 */
57850390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
57862b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
57870623543bSJan Kara 		/*
57880623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
57890623543bSJan Kara 		 * otherwise be livelocked...
57900623543bSJan Kara 		 */
57910623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
57927ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
57930623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5794f00c9e44SJan Kara 		if (err)
57957ffe1ea8SHidehiro Kawai 			return err;
57967ffe1ea8SHidehiro Kawai 	}
5797957153fcSJan Kara 
5798daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5799daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5800957153fcSJan Kara 	if (err) {
5801daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5802daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5803957153fcSJan Kara 	} else {
5804957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5805957153fcSJan Kara 		handle_t *handle;
5806957153fcSJan Kara 
580761a92987SJan Kara 		/*
580861a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
580961a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
581061a92987SJan Kara 		 * are already enabled and this is not a hard failure.
581161a92987SJan Kara 		 */
5812957153fcSJan Kara 		inode_lock(inode);
5813957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5814957153fcSJan Kara 		if (IS_ERR(handle))
5815957153fcSJan Kara 			goto unlock_inode;
5816957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5817957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5818957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5819957153fcSJan Kara 		ext4_mark_inode_dirty(handle, inode);
5820957153fcSJan Kara 		ext4_journal_stop(handle);
5821957153fcSJan Kara 	unlock_inode:
5822957153fcSJan Kara 		inode_unlock(inode);
5823957153fcSJan Kara 	}
5824daf647d2STheodore Ts'o 	return err;
5825ac27a0ecSDave Kleikamp }
5826ac27a0ecSDave Kleikamp 
58277c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
58287c319d32SAditya Kali 			     unsigned int flags)
58297c319d32SAditya Kali {
58307c319d32SAditya Kali 	int err;
58317c319d32SAditya Kali 	struct inode *qf_inode;
5832a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58337c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5834689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5835689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58367c319d32SAditya Kali 	};
58377c319d32SAditya Kali 
5838e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
58397c319d32SAditya Kali 
58407c319d32SAditya Kali 	if (!qf_inums[type])
58417c319d32SAditya Kali 		return -EPERM;
58427c319d32SAditya Kali 
58438a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
58447c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
58457c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
58467c319d32SAditya Kali 		return PTR_ERR(qf_inode);
58477c319d32SAditya Kali 	}
58487c319d32SAditya Kali 
5849bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5850bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5851daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
58527c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
5853daf647d2STheodore Ts'o 	if (err)
5854daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
585561157b24SPan Bian 	iput(qf_inode);
58567c319d32SAditya Kali 
58577c319d32SAditya Kali 	return err;
58587c319d32SAditya Kali }
58597c319d32SAditya Kali 
58607c319d32SAditya Kali /* Enable usage tracking for all quota types. */
58617c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
58627c319d32SAditya Kali {
58637c319d32SAditya Kali 	int type, err = 0;
5864a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58657c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5866689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5867689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58687c319d32SAditya Kali 	};
586949da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
587049da9392SJan Kara 		test_opt(sb, USRQUOTA),
587149da9392SJan Kara 		test_opt(sb, GRPQUOTA),
587249da9392SJan Kara 		test_opt(sb, PRJQUOTA),
587349da9392SJan Kara 	};
58747c319d32SAditya Kali 
587591389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5876a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
58777c319d32SAditya Kali 		if (qf_inums[type]) {
58787c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
587949da9392SJan Kara 				DQUOT_USAGE_ENABLED |
588049da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
58817c319d32SAditya Kali 			if (err) {
58827c319d32SAditya Kali 				ext4_warning(sb,
588372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
588472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
588572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
58867f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
58877f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
58887f144fd0SJunichi Uekawa 
58897c319d32SAditya Kali 				return err;
58907c319d32SAditya Kali 			}
58917c319d32SAditya Kali 		}
58927c319d32SAditya Kali 	}
58937c319d32SAditya Kali 	return 0;
58947c319d32SAditya Kali }
58957c319d32SAditya Kali 
5896ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5897ca0e05e4SDmitry Monakhov {
589821f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
589921f97697SJan Kara 	handle_t *handle;
5900957153fcSJan Kara 	int err;
590121f97697SJan Kara 
590287009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
590387009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
590487009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5905ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5906ca0e05e4SDmitry Monakhov 
5907957153fcSJan Kara 	if (!inode || !igrab(inode))
59080b268590SAmir Goldstein 		goto out;
59090b268590SAmir Goldstein 
5910957153fcSJan Kara 	err = dquot_quota_off(sb, type);
5911964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
5912957153fcSJan Kara 		goto out_put;
5913957153fcSJan Kara 
5914957153fcSJan Kara 	inode_lock(inode);
591561a92987SJan Kara 	/*
591661a92987SJan Kara 	 * Update modification times of quota files when userspace can
591761a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
591861a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
591961a92987SJan Kara 	 */
59209924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
592121f97697SJan Kara 	if (IS_ERR(handle))
5922957153fcSJan Kara 		goto out_unlock;
5923957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
5924957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
5925eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
592621f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
592721f97697SJan Kara 	ext4_journal_stop(handle);
5928957153fcSJan Kara out_unlock:
5929957153fcSJan Kara 	inode_unlock(inode);
5930957153fcSJan Kara out_put:
5931964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
5932957153fcSJan Kara 	iput(inode);
5933957153fcSJan Kara 	return err;
593421f97697SJan Kara out:
5935ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5936ca0e05e4SDmitry Monakhov }
5937ca0e05e4SDmitry Monakhov 
5938ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5939ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5940ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5941ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5942617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5943ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5944ac27a0ecSDave Kleikamp {
5945ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5946725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5947ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5948ac27a0ecSDave Kleikamp 	int tocopy;
5949ac27a0ecSDave Kleikamp 	size_t toread;
5950ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5951ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5952ac27a0ecSDave Kleikamp 
5953ac27a0ecSDave Kleikamp 	if (off > i_size)
5954ac27a0ecSDave Kleikamp 		return 0;
5955ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5956ac27a0ecSDave Kleikamp 		len = i_size-off;
5957ac27a0ecSDave Kleikamp 	toread = len;
5958ac27a0ecSDave Kleikamp 	while (toread > 0) {
5959ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5960ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
59611c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
59621c215028STheodore Ts'o 		if (IS_ERR(bh))
59631c215028STheodore Ts'o 			return PTR_ERR(bh);
5964ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5965ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5966ac27a0ecSDave Kleikamp 		else
5967ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5968ac27a0ecSDave Kleikamp 		brelse(bh);
5969ac27a0ecSDave Kleikamp 		offset = 0;
5970ac27a0ecSDave Kleikamp 		toread -= tocopy;
5971ac27a0ecSDave Kleikamp 		data += tocopy;
5972ac27a0ecSDave Kleikamp 		blk++;
5973ac27a0ecSDave Kleikamp 	}
5974ac27a0ecSDave Kleikamp 	return len;
5975ac27a0ecSDave Kleikamp }
5976ac27a0ecSDave Kleikamp 
5977ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5978ac27a0ecSDave Kleikamp  * enough credits) */
5979617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5980ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5981ac27a0ecSDave Kleikamp {
5982ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5983725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
59841c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5985c5e298aeSTheodore Ts'o 	int retries = 0;
5986ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5987ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5988ac27a0ecSDave Kleikamp 
59890390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5990b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5991b31e1552SEric Sandeen 			" cancelled because transaction is not started",
59929c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
59939c3013e9SJan Kara 		return -EIO;
59949c3013e9SJan Kara 	}
599567eeb568SDmitry Monakhov 	/*
599667eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
599767eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
599867eeb568SDmitry Monakhov 	 */
599967eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
600067eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
600167eeb568SDmitry Monakhov 			" cancelled because not block aligned",
600267eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
600367eeb568SDmitry Monakhov 		return -EIO;
600467eeb568SDmitry Monakhov 	}
600567eeb568SDmitry Monakhov 
6006c5e298aeSTheodore Ts'o 	do {
6007c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6008c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6009c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
6010c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
6011c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
60121c215028STheodore Ts'o 	if (IS_ERR(bh))
60131c215028STheodore Ts'o 		return PTR_ERR(bh);
6014ac27a0ecSDave Kleikamp 	if (!bh)
6015ac27a0ecSDave Kleikamp 		goto out;
60165d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6017617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6018ac27a0ecSDave Kleikamp 	if (err) {
6019ac27a0ecSDave Kleikamp 		brelse(bh);
60201c215028STheodore Ts'o 		return err;
6021ac27a0ecSDave Kleikamp 	}
6022ac27a0ecSDave Kleikamp 	lock_buffer(bh);
602367eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6024ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6025ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
60260390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6027ac27a0ecSDave Kleikamp 	brelse(bh);
6028ac27a0ecSDave Kleikamp out:
602967eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
603067eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6031617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
6032617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
603321f97697SJan Kara 	}
603467eeb568SDmitry Monakhov 	return len;
6035ac27a0ecSDave Kleikamp }
6036ac27a0ecSDave Kleikamp 
60378f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
60388f0e8746STheodore Ts'o {
60398f0e8746STheodore Ts'o 	const struct quota_format_ops	*ops;
60408f0e8746STheodore Ts'o 
60418f0e8746STheodore Ts'o 	if (!sb_has_quota_loaded(sb, qid->type))
60428f0e8746STheodore Ts'o 		return -ESRCH;
60438f0e8746STheodore Ts'o 	ops = sb_dqopt(sb)->ops[qid->type];
60448f0e8746STheodore Ts'o 	if (!ops || !ops->get_next_id)
60458f0e8746STheodore Ts'o 		return -ENOSYS;
60468f0e8746STheodore Ts'o 	return dquot_get_next_id(sb, qid);
60478f0e8746STheodore Ts'o }
6048ac27a0ecSDave Kleikamp #endif
6049ac27a0ecSDave Kleikamp 
6050152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6051152a0836SAl Viro 		       const char *dev_name, void *data)
6052ac27a0ecSDave Kleikamp {
6053152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6054ac27a0ecSDave Kleikamp }
6055ac27a0ecSDave Kleikamp 
6056c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
605724b58424STheodore Ts'o static inline void register_as_ext2(void)
605824b58424STheodore Ts'o {
605924b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
606024b58424STheodore Ts'o 	if (err)
606124b58424STheodore Ts'o 		printk(KERN_WARNING
606224b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
606324b58424STheodore Ts'o }
606424b58424STheodore Ts'o 
606524b58424STheodore Ts'o static inline void unregister_as_ext2(void)
606624b58424STheodore Ts'o {
606724b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
606824b58424STheodore Ts'o }
60692035e776STheodore Ts'o 
60702035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
60712035e776STheodore Ts'o {
6072e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
60732035e776STheodore Ts'o 		return 0;
6074bc98a42cSDavid Howells 	if (sb_rdonly(sb))
60752035e776STheodore Ts'o 		return 1;
6076e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
60772035e776STheodore Ts'o 		return 0;
60782035e776STheodore Ts'o 	return 1;
60792035e776STheodore Ts'o }
608024b58424STheodore Ts'o #else
608124b58424STheodore Ts'o static inline void register_as_ext2(void) { }
608224b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
60832035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
608424b58424STheodore Ts'o #endif
608524b58424STheodore Ts'o 
608624b58424STheodore Ts'o static inline void register_as_ext3(void)
608724b58424STheodore Ts'o {
608824b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
608924b58424STheodore Ts'o 	if (err)
609024b58424STheodore Ts'o 		printk(KERN_WARNING
609124b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
609224b58424STheodore Ts'o }
609324b58424STheodore Ts'o 
609424b58424STheodore Ts'o static inline void unregister_as_ext3(void)
609524b58424STheodore Ts'o {
609624b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
609724b58424STheodore Ts'o }
60982035e776STheodore Ts'o 
60992035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
61002035e776STheodore Ts'o {
6101e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
61022035e776STheodore Ts'o 		return 0;
6103e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
61042035e776STheodore Ts'o 		return 0;
6105bc98a42cSDavid Howells 	if (sb_rdonly(sb))
61062035e776STheodore Ts'o 		return 1;
6107e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
61082035e776STheodore Ts'o 		return 0;
61092035e776STheodore Ts'o 	return 1;
61102035e776STheodore Ts'o }
611124b58424STheodore Ts'o 
611203010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6113ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
611403010a33STheodore Ts'o 	.name		= "ext4",
6115152a0836SAl Viro 	.mount		= ext4_mount,
6116ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6117ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6118ac27a0ecSDave Kleikamp };
61197f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6120ac27a0ecSDave Kleikamp 
6121e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6122e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6123e9e3bcecSEric Sandeen 
61245dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6125ac27a0ecSDave Kleikamp {
6126e9e3bcecSEric Sandeen 	int i, err;
6127c9de560dSAlex Tomas 
6128e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
612907c0c5d8SAl Viro 	ext4_li_info = NULL;
613007c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
613107c0c5d8SAl Viro 
61329a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
613312e9b892SDmitry Monakhov 	ext4_check_flag_values();
6134e9e3bcecSEric Sandeen 
6135e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6136e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6137e9e3bcecSEric Sandeen 
613851865fdaSZheng Liu 	err = ext4_init_es();
61396fd058f7STheodore Ts'o 	if (err)
61406fd058f7STheodore Ts'o 		return err;
614151865fdaSZheng Liu 
61421dc0aa46SEric Whitney 	err = ext4_init_pending();
61431dc0aa46SEric Whitney 	if (err)
614422cfe4b4SEric Biggers 		goto out7;
614522cfe4b4SEric Biggers 
614622cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
614722cfe4b4SEric Biggers 	if (err)
61481dc0aa46SEric Whitney 		goto out6;
61491dc0aa46SEric Whitney 
615051865fdaSZheng Liu 	err = ext4_init_pageio();
615151865fdaSZheng Liu 	if (err)
6152b5799018STheodore Ts'o 		goto out5;
615351865fdaSZheng Liu 
61545dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6155bd2d0210STheodore Ts'o 	if (err)
6156b5799018STheodore Ts'o 		goto out4;
6157857ac889SLukas Czerner 
6158b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6159dd68314cSTheodore Ts'o 	if (err)
6160b5799018STheodore Ts'o 		goto out3;
6161857ac889SLukas Czerner 
61625dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6163ac27a0ecSDave Kleikamp 	if (err)
6164c9de560dSAlex Tomas 		goto out2;
6165ac27a0ecSDave Kleikamp 	err = init_inodecache();
6166ac27a0ecSDave Kleikamp 	if (err)
6167ac27a0ecSDave Kleikamp 		goto out1;
616824b58424STheodore Ts'o 	register_as_ext3();
61692035e776STheodore Ts'o 	register_as_ext2();
617003010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6171ac27a0ecSDave Kleikamp 	if (err)
6172ac27a0ecSDave Kleikamp 		goto out;
6173bfff6873SLukas Czerner 
6174ac27a0ecSDave Kleikamp 	return 0;
6175ac27a0ecSDave Kleikamp out:
617624b58424STheodore Ts'o 	unregister_as_ext2();
617724b58424STheodore Ts'o 	unregister_as_ext3();
6178ac27a0ecSDave Kleikamp 	destroy_inodecache();
6179ac27a0ecSDave Kleikamp out1:
61805dabfc78STheodore Ts'o 	ext4_exit_mballoc();
61819c191f70ST Makphaibulchoke out2:
6182b5799018STheodore Ts'o 	ext4_exit_sysfs();
6183b5799018STheodore Ts'o out3:
6184dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6185b5799018STheodore Ts'o out4:
61865dabfc78STheodore Ts'o 	ext4_exit_pageio();
6187b5799018STheodore Ts'o out5:
618822cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
61891dc0aa46SEric Whitney out6:
619022cfe4b4SEric Biggers 	ext4_exit_pending();
619122cfe4b4SEric Biggers out7:
619251865fdaSZheng Liu 	ext4_exit_es();
619351865fdaSZheng Liu 
6194ac27a0ecSDave Kleikamp 	return err;
6195ac27a0ecSDave Kleikamp }
6196ac27a0ecSDave Kleikamp 
61975dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6198ac27a0ecSDave Kleikamp {
6199bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
620024b58424STheodore Ts'o 	unregister_as_ext2();
620124b58424STheodore Ts'o 	unregister_as_ext3();
620203010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6203ac27a0ecSDave Kleikamp 	destroy_inodecache();
62045dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6205b5799018STheodore Ts'o 	ext4_exit_sysfs();
62065dabfc78STheodore Ts'o 	ext4_exit_system_zone();
62075dabfc78STheodore Ts'o 	ext4_exit_pageio();
620822cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6209dd12ed14SEric Sandeen 	ext4_exit_es();
62101dc0aa46SEric Whitney 	ext4_exit_pending();
6211ac27a0ecSDave Kleikamp }
6212ac27a0ecSDave Kleikamp 
6213ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
621483982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6215ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
62167ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
62175dabfc78STheodore Ts'o module_init(ext4_init_fs)
62185dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6219