xref: /linux/fs/ext4/super.c (revision 2c1d0e3631e5732dba98ef49ac0bec1388776793)
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 	}
702*2c1d0e36SJan 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 
11107ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
11117ff9c073STheodore Ts'o 	return drop;
11127ff9c073STheodore Ts'o }
11137ff9c073STheodore Ts'o 
1114fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head)
1115fa0d7e3dSNick Piggin {
1116fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
1117fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1118fa0d7e3dSNick Piggin }
1119fa0d7e3dSNick Piggin 
1120617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1121ac27a0ecSDave Kleikamp {
11229f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1123b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1124b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1125b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
11269f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
11279f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
11289f7dd93dSVasily Averin 				true);
11299f7dd93dSVasily Averin 		dump_stack();
11309f7dd93dSVasily Averin 	}
1131fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, ext4_i_callback);
1132ac27a0ecSDave Kleikamp }
1133ac27a0ecSDave Kleikamp 
113451cc5068SAlexey Dobriyan static void init_once(void *foo)
1135ac27a0ecSDave Kleikamp {
1136617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1137ac27a0ecSDave Kleikamp 
1138ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1139ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
11400e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1141ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1142ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1143ac27a0ecSDave Kleikamp }
1144ac27a0ecSDave Kleikamp 
1145e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1146ac27a0ecSDave Kleikamp {
1147f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1148f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1149f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1150f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1151f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1152f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
115320c2df83SPaul Mundt 				init_once);
1154617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1155ac27a0ecSDave Kleikamp 		return -ENOMEM;
1156ac27a0ecSDave Kleikamp 	return 0;
1157ac27a0ecSDave Kleikamp }
1158ac27a0ecSDave Kleikamp 
1159ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1160ac27a0ecSDave Kleikamp {
11618c0a8537SKirill A. Shutemov 	/*
11628c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
11638c0a8537SKirill A. Shutemov 	 * destroy cache.
11648c0a8537SKirill A. Shutemov 	 */
11658c0a8537SKirill A. Shutemov 	rcu_barrier();
1166617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1167ac27a0ecSDave Kleikamp }
1168ac27a0ecSDave Kleikamp 
11690930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1170ac27a0ecSDave Kleikamp {
11710930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1172dbd5768fSJan Kara 	clear_inode(inode);
11739f754758SChristoph Hellwig 	dquot_drop(inode);
1174c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
117551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
11768aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
11778aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
11788aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
11798aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
11808aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
11818aefcd55STheodore Ts'o 	}
11823d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1183ac27a0ecSDave Kleikamp }
1184ac27a0ecSDave Kleikamp 
11851b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
11861b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1187ac27a0ecSDave Kleikamp {
1188ac27a0ecSDave Kleikamp 	struct inode *inode;
1189ac27a0ecSDave Kleikamp 
11908a363970STheodore Ts'o 	/*
1191ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1192ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1193ac27a0ecSDave Kleikamp 	 */
11948a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
11951d1fe1eeSDavid Howells 	if (IS_ERR(inode))
11961d1fe1eeSDavid Howells 		return ERR_CAST(inode);
11971d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1198ac27a0ecSDave Kleikamp 		iput(inode);
1199ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1200ac27a0ecSDave Kleikamp 	}
12011b961ac0SChristoph Hellwig 
12021b961ac0SChristoph Hellwig 	return inode;
1203ac27a0ecSDave Kleikamp }
12041b961ac0SChristoph Hellwig 
12051b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
12061b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12071b961ac0SChristoph Hellwig {
12081b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
12091b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
12101b961ac0SChristoph Hellwig }
12111b961ac0SChristoph Hellwig 
12121b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
12131b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12141b961ac0SChristoph Hellwig {
12151b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
12161b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1217ac27a0ecSDave Kleikamp }
1218ac27a0ecSDave Kleikamp 
1219fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1220fde87268STheodore Ts'o {
1221fde87268STheodore Ts'o 	struct writeback_control wbc = {
1222fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1223fde87268STheodore Ts'o 	};
1224fde87268STheodore Ts'o 
1225fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1226fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1227fde87268STheodore Ts'o }
1228fde87268STheodore Ts'o 
1229c39a7f84SToshiyuki Okajima /*
1230c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1231c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1232c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1233c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1234c39a7f84SToshiyuki Okajima  */
12350b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
12360b8e58a1SAndreas Dilger 				 gfp_t wait)
1237c39a7f84SToshiyuki Okajima {
1238c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1239c39a7f84SToshiyuki Okajima 
1240c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1241c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1242c39a7f84SToshiyuki Okajima 		return 0;
1243c39a7f84SToshiyuki Okajima 	if (journal)
1244c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1245d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1246c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1247c39a7f84SToshiyuki Okajima }
1248c39a7f84SToshiyuki Okajima 
1249643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1250a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1251a7550b30SJaegeuk Kim {
1252a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1253a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1254a7550b30SJaegeuk Kim }
1255a7550b30SJaegeuk Kim 
1256a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1257a7550b30SJaegeuk Kim 							void *fs_data)
1258a7550b30SJaegeuk Kim {
12592f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1260c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1261a7550b30SJaegeuk Kim 
12629ce0151aSEric Biggers 	/*
12639ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
12649ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
12659ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
12669ce0151aSEric Biggers 	 * the filename "lost+found" itself.
12679ce0151aSEric Biggers 	 */
12689ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
12699ce0151aSEric Biggers 		return -EPERM;
1270a7550b30SJaegeuk Kim 
12717d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
12727d3e06a8SRoss Zwisler 		return -EINVAL;
12737d3e06a8SRoss Zwisler 
127494840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
127594840e3cSEric Biggers 	if (res)
127694840e3cSEric Biggers 		return res;
127794840e3cSEric Biggers 
12782f8f5e76SEric Biggers 	/*
12792f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
12802f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
12812f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
12822f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
12832f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
12842f8f5e76SEric Biggers 	 */
12852f8f5e76SEric Biggers 
12862f8f5e76SEric Biggers 	if (handle) {
12872f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
12882f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
12892f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
12902f8f5e76SEric Biggers 					    ctx, len, 0);
1291a7550b30SJaegeuk Kim 		if (!res) {
1292a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1293a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1294a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1295a3caa24bSJan Kara 			/*
12962ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
12972ee6a576SEric Biggers 			 * S_DAX may be disabled
1298a3caa24bSJan Kara 			 */
1299a3caa24bSJan Kara 			ext4_set_inode_flags(inode);
1300a7550b30SJaegeuk Kim 		}
1301a7550b30SJaegeuk Kim 		return res;
1302a7550b30SJaegeuk Kim 	}
1303a7550b30SJaegeuk Kim 
1304b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1305b8cb5a54STahsin Erdogan 	if (res)
1306b8cb5a54STahsin Erdogan 		return res;
13072f8f5e76SEric Biggers retry:
1308af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1309af65207cSTahsin Erdogan 				     &credits);
1310dec214d0STahsin Erdogan 	if (res)
1311dec214d0STahsin Erdogan 		return res;
1312dec214d0STahsin Erdogan 
1313c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1314a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1315a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1316a7550b30SJaegeuk Kim 
13172f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
13182f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13192f8f5e76SEric Biggers 				    ctx, len, 0);
1320a7550b30SJaegeuk Kim 	if (!res) {
1321a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
13222ee6a576SEric Biggers 		/*
13232ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13242ee6a576SEric Biggers 		 * S_DAX may be disabled
13252ee6a576SEric Biggers 		 */
1326a3caa24bSJan Kara 		ext4_set_inode_flags(inode);
1327a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1328a7550b30SJaegeuk Kim 		if (res)
1329a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1330a7550b30SJaegeuk Kim 	}
1331a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
13322f8f5e76SEric Biggers 
13332f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
13342f8f5e76SEric Biggers 		goto retry;
1335a7550b30SJaegeuk Kim 	if (!res)
1336a7550b30SJaegeuk Kim 		res = res2;
1337a7550b30SJaegeuk Kim 	return res;
1338a7550b30SJaegeuk Kim }
1339a7550b30SJaegeuk Kim 
1340c250b7ddSEric Biggers static bool ext4_dummy_context(struct inode *inode)
1341a7550b30SJaegeuk Kim {
1342a7550b30SJaegeuk Kim 	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1343a7550b30SJaegeuk Kim }
1344a7550b30SJaegeuk Kim 
13456f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1346a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1347a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1348a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1349a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1350a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1351e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1352a7550b30SJaegeuk Kim };
1353a7550b30SJaegeuk Kim #endif
1354a7550b30SJaegeuk Kim 
1355ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1356d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1357689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1358ac27a0ecSDave Kleikamp 
1359617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1360617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1361617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1362617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1363617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
13646f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
13658c54ca9cSAl Viro 			 const struct path *path);
1366617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1367617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1368ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1369617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1370ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
13717c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
13727c319d32SAditya Kali 			     unsigned int flags);
13737c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
13748f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
1375ac27a0ecSDave Kleikamp 
137696c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
137796c7e0d9SJan Kara {
137896c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
137996c7e0d9SJan Kara }
138096c7e0d9SJan Kara 
138161e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
138260e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1383617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1384617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1385617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1386617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1387a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1388a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1389a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1390040cb378SLi Xi 	.get_projid		= ext4_get_projid,
13917a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
13928f0e8746STheodore Ts'o 	.get_next_id		= ext4_get_next_id,
1393ac27a0ecSDave Kleikamp };
1394ac27a0ecSDave Kleikamp 
13950d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1396617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1397ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1398287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
13990a240339SJan Kara 	.get_state	= dquot_get_state,
1400287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1401287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
14026332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
14036332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1404ac27a0ecSDave Kleikamp };
1405ac27a0ecSDave Kleikamp #endif
1406ac27a0ecSDave Kleikamp 
1407ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1408617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
1409617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1410617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1411617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
14127ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
14130930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1414617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1415617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1416c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1417c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1418617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1419617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1420617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1421ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1422617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1423617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
142496c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1425ac27a0ecSDave Kleikamp #endif
1426c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1427ac27a0ecSDave Kleikamp };
1428ac27a0ecSDave Kleikamp 
142939655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
14301b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
14311b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1432617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1433fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1434ac27a0ecSDave Kleikamp };
1435ac27a0ecSDave Kleikamp 
1436ac27a0ecSDave Kleikamp enum {
1437ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1438ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
143972578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1440ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
144172578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1442ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1443ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1444ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
14456ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1446ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
14475a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1448ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
144949da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
1450327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1451327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1452670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
14531449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
14545328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1455744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1456fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1457cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1458ac27a0ecSDave Kleikamp };
1459ac27a0ecSDave Kleikamp 
1460a447c093SSteven Whitehouse static const match_table_t tokens = {
1461ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1462ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1463ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1464ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1465ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1466ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1467ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1468ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1469ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1470ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1471ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1472ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1473ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1474ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
147572578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
147672578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1477ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1478ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1479ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1480ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1481e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
14825a916be1STheodore Ts'o 	{Opt_noload, "noload"},
148372578c33STheodore Ts'o 	{Opt_removed, "nobh"},
148472578c33STheodore Ts'o 	{Opt_removed, "bh"},
1485ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
148630773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
148730773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1488ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1489ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1490818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1491c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1492818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1493ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1494ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1495ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1496ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
14975bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
14985bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1499ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1500ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1501ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1502ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1503ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1504ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15055a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1506ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1507ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1508ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1509ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
151049da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1511ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
151206705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
151306705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
151425ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1515923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1516c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
151764769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1518327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1519327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1520a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1521a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1522670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1523dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
152436ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
152536ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15266fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15276fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1528240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1529b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1530afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
153106705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
153206705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1533744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1534744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
15355328e635SEric Sandeen 	{Opt_discard, "discard"},
15365328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1537fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1538fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1539fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1540df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
15416ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1542cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1543cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1544c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1545c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1546c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1547c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1548c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1549f3f12faaSJosef Bacik 	{Opt_err, NULL},
1550ac27a0ecSDave Kleikamp };
1551ac27a0ecSDave Kleikamp 
1552617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1553ac27a0ecSDave Kleikamp {
1554617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1555ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1556ac27a0ecSDave Kleikamp 
1557ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1558ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
15590b8e58a1SAndreas Dilger 
1560ac27a0ecSDave Kleikamp 	options += 3;
15610b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1562ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1563ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
15644776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1565ac27a0ecSDave Kleikamp 		       (char *) *data);
1566ac27a0ecSDave Kleikamp 		return 1;
1567ac27a0ecSDave Kleikamp 	}
1568ac27a0ecSDave Kleikamp 	if (*options == ',')
1569ac27a0ecSDave Kleikamp 		options++;
1570ac27a0ecSDave Kleikamp 	*data = (void *) options;
15710b8e58a1SAndreas Dilger 
1572ac27a0ecSDave Kleikamp 	return sb_block;
1573ac27a0ecSDave Kleikamp }
1574ac27a0ecSDave Kleikamp 
1575b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1576d6006186SEric Biggers static const char deprecated_msg[] =
1577d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1578437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1579b3881f74STheodore Ts'o 
158056c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
158156c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
158256c50f11SDmitry Monakhov {
158356c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
158433458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
158503dafb5fSChen Gang 	int ret = -1;
158656c50f11SDmitry Monakhov 
158733458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
158856c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
158956c50f11SDmitry Monakhov 			"Cannot change journaled "
159056c50f11SDmitry Monakhov 			"quota options when quota turned on");
159157f73c2cSTheodore Ts'o 		return -1;
159256c50f11SDmitry Monakhov 	}
1593e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1594c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1595c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1596c325a67cSTheodore Ts'o 		return 1;
1597262b4662SJan Kara 	}
159856c50f11SDmitry Monakhov 	qname = match_strdup(args);
159956c50f11SDmitry Monakhov 	if (!qname) {
160056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
160156c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
160257f73c2cSTheodore Ts'o 		return -1;
160356c50f11SDmitry Monakhov 	}
160433458eabSTheodore Ts'o 	if (old_qname) {
160533458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
160603dafb5fSChen Gang 			ret = 1;
160703dafb5fSChen Gang 		else
160856c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
160903dafb5fSChen Gang 				 "%s quota file already specified",
161003dafb5fSChen Gang 				 QTYPE2NAME(qtype));
161103dafb5fSChen Gang 		goto errout;
161256c50f11SDmitry Monakhov 	}
161303dafb5fSChen Gang 	if (strchr(qname, '/')) {
161456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
161556c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
161603dafb5fSChen Gang 		goto errout;
161756c50f11SDmitry Monakhov 	}
161833458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1619fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
162056c50f11SDmitry Monakhov 	return 1;
162103dafb5fSChen Gang errout:
162203dafb5fSChen Gang 	kfree(qname);
162303dafb5fSChen Gang 	return ret;
162456c50f11SDmitry Monakhov }
162556c50f11SDmitry Monakhov 
162656c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
162756c50f11SDmitry Monakhov {
162856c50f11SDmitry Monakhov 
162956c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
163033458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
163156c50f11SDmitry Monakhov 
163233458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
163356c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
163456c50f11SDmitry Monakhov 			" when quota turned on");
163557f73c2cSTheodore Ts'o 		return -1;
163656c50f11SDmitry Monakhov 	}
163733458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
163833458eabSTheodore Ts'o 	synchronize_rcu();
163933458eabSTheodore Ts'o 	kfree(old_qname);
164056c50f11SDmitry Monakhov 	return 1;
164156c50f11SDmitry Monakhov }
164256c50f11SDmitry Monakhov #endif
164356c50f11SDmitry Monakhov 
164426092bf5STheodore Ts'o #define MOPT_SET	0x0001
164526092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
164626092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
164726092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
164826092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
164926092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
165026092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
165126092bf5STheodore Ts'o #define MOPT_Q		0
165226092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
165326092bf5STheodore Ts'o #else
165426092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
165526092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
165626092bf5STheodore Ts'o #endif
165726092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
16588dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
16598dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
16608dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1661ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
166226092bf5STheodore Ts'o 
166326092bf5STheodore Ts'o static const struct mount_opts {
166426092bf5STheodore Ts'o 	int	token;
166526092bf5STheodore Ts'o 	int	mount_opt;
166626092bf5STheodore Ts'o 	int	flags;
166726092bf5STheodore Ts'o } ext4_mount_opts[] = {
166826092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
166926092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
167026092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
167126092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
167226092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
167326092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
16748dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
16758dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
16768dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
16778dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
167826092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
167926092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
16808dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
16818dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
16828dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
168359d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1684327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1685327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1686c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1687c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
16888dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
16891e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
169026092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
16918dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
16921e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
16938dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
169426092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
169526092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
169626092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
16978dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
16987915a861SAles Novak 	 MOPT_NO_EXT2},
16998dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17007915a861SAles Novak 	 MOPT_NO_EXT2},
170126092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
170226092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
170326092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
170426092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
170526092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
170626092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
170726092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
170826092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
170926092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
171026092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1711923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
171226092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17130efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17140efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17155ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17165ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17175ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17188dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17198dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17208dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17218dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
172226092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
172326092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
172426092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
172526092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
172626092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
172726092bf5STheodore Ts'o #else
172826092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
172926092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
173026092bf5STheodore Ts'o #endif
173126092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
173226092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1733670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
173426092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
173526092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
173626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
173726092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
173826092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
173949da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
174049da9392SJan Kara 							MOPT_SET | MOPT_Q},
174126092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
174249da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
174349da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
174426092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
174526092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
174626092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
174726092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
174826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
174926092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
175026092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1751df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
17526ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1753cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
175426092bf5STheodore Ts'o 	{Opt_err, 0, 0}
175526092bf5STheodore Ts'o };
175626092bf5STheodore Ts'o 
1757c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1758c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1759c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1760c83ad55eSGabriel Krisman Bertazi 	char *name;
1761c83ad55eSGabriel Krisman Bertazi 	char *version;
1762c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1763c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1764c83ad55eSGabriel Krisman Bertazi };
1765c83ad55eSGabriel Krisman Bertazi 
1766c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1767c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1768c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1769c83ad55eSGabriel Krisman Bertazi {
1770c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1771c83ad55eSGabriel Krisman Bertazi 	int i;
1772c83ad55eSGabriel Krisman Bertazi 
1773c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1774c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1775c83ad55eSGabriel Krisman Bertazi 			break;
1776c83ad55eSGabriel Krisman Bertazi 
1777c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1778c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1779c83ad55eSGabriel Krisman Bertazi 
1780c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1781c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1782c83ad55eSGabriel Krisman Bertazi 
1783c83ad55eSGabriel Krisman Bertazi 	return 0;
1784c83ad55eSGabriel Krisman Bertazi }
1785c83ad55eSGabriel Krisman Bertazi #endif
1786c83ad55eSGabriel Krisman Bertazi 
178726092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
178826092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
178926092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
179026092bf5STheodore Ts'o {
179126092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
179226092bf5STheodore Ts'o 	const struct mount_opts *m;
179308cefc7aSEric W. Biederman 	kuid_t uid;
179408cefc7aSEric W. Biederman 	kgid_t gid;
179526092bf5STheodore Ts'o 	int arg = 0;
179626092bf5STheodore Ts'o 
179757f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
179857f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
179957f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
180057f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
180157f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
180257f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
180357f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
180457f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
180557f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
180657f73c2cSTheodore Ts'o #endif
180726092bf5STheodore Ts'o 	switch (token) {
1808f7048605STheodore Ts'o 	case Opt_noacl:
1809f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1810f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1811f7048605STheodore Ts'o 		break;
181226092bf5STheodore Ts'o 	case Opt_sb:
181326092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
181426092bf5STheodore Ts'o 	case Opt_removed:
18155f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
181626092bf5STheodore Ts'o 		return 1;
181726092bf5STheodore Ts'o 	case Opt_abort:
181826092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
181926092bf5STheodore Ts'o 		return 1;
182026092bf5STheodore Ts'o 	case Opt_i_version:
1821357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
182226092bf5STheodore Ts'o 		return 1;
1823a26f4992STheodore Ts'o 	case Opt_lazytime:
18241751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1825a26f4992STheodore Ts'o 		return 1;
1826a26f4992STheodore Ts'o 	case Opt_nolazytime:
18271751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1828a26f4992STheodore Ts'o 		return 1;
182926092bf5STheodore Ts'o 	}
183026092bf5STheodore Ts'o 
18315f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
18325f3633e3SJan Kara 		if (token == m->token)
18335f3633e3SJan Kara 			break;
18345f3633e3SJan Kara 
18355f3633e3SJan Kara 	if (m->token == Opt_err) {
18365f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
18375f3633e3SJan Kara 			 "or missing value", opt);
18385f3633e3SJan Kara 		return -1;
18395f3633e3SJan Kara 	}
18405f3633e3SJan Kara 
18418dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
18428dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
18438dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
18448dc0aa8cSTheodore Ts'o 		return -1;
18458dc0aa8cSTheodore Ts'o 	}
18468dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
18478dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
18488dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
18498dc0aa8cSTheodore Ts'o 		return -1;
18508dc0aa8cSTheodore Ts'o 	}
18518dc0aa8cSTheodore Ts'o 
1852ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
18530efb3b23SJan Kara 		return -1;
185426092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
185526092bf5STheodore Ts'o 		return -1;
1856c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1857c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
185826092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
18591e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
18601e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1861c93cf2d7SDmitry Monakhov 		} else
1862c93cf2d7SDmitry Monakhov 			return -1;
1863c93cf2d7SDmitry Monakhov 	}
186426092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
186526092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
186626092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
186726092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
186826092bf5STheodore Ts'o 			 "options when quota turned on");
186926092bf5STheodore Ts'o 		return -1;
187026092bf5STheodore Ts'o 	}
187126092bf5STheodore Ts'o 
187226092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
187326092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
187426092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
187526092bf5STheodore Ts'o 		if (arg == 0)
187626092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
187726092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1878670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
1879670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
188026092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
188126092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
188226092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
188326092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
188426092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1885e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1886e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1887e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1888e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
188926092bf5STheodore Ts'o 			return -1;
189026092bf5STheodore Ts'o 		}
189126092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
189226092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
189326092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
189426092bf5STheodore Ts'o 		if (!args->from)
189526092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
189626092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1897df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1898df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
189926092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
190026092bf5STheodore Ts'o 		sbi->s_stripe = arg;
19010efb3b23SJan Kara 	} else if (token == Opt_resuid) {
19020efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
19030efb3b23SJan Kara 		if (!uid_valid(uid)) {
19045f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
19050efb3b23SJan Kara 			return -1;
19060efb3b23SJan Kara 		}
19070efb3b23SJan Kara 		sbi->s_resuid = uid;
19080efb3b23SJan Kara 	} else if (token == Opt_resgid) {
19090efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
19100efb3b23SJan Kara 		if (!gid_valid(gid)) {
19115f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
19120efb3b23SJan Kara 			return -1;
19130efb3b23SJan Kara 		}
19140efb3b23SJan Kara 		sbi->s_resgid = gid;
19150efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
19160efb3b23SJan Kara 		if (is_remount) {
19170efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
19180efb3b23SJan Kara 				 "Cannot specify journal on remount");
19190efb3b23SJan Kara 			return -1;
19200efb3b23SJan Kara 		}
19210efb3b23SJan Kara 		*journal_devnum = arg;
1922ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
1923ad4eec61SEric Sandeen 		char *journal_path;
1924ad4eec61SEric Sandeen 		struct inode *journal_inode;
1925ad4eec61SEric Sandeen 		struct path path;
1926ad4eec61SEric Sandeen 		int error;
1927ad4eec61SEric Sandeen 
1928ad4eec61SEric Sandeen 		if (is_remount) {
1929ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
1930ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
1931ad4eec61SEric Sandeen 			return -1;
1932ad4eec61SEric Sandeen 		}
1933ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
1934ad4eec61SEric Sandeen 		if (!journal_path) {
1935ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1936ad4eec61SEric Sandeen 				"journal device string");
1937ad4eec61SEric Sandeen 			return -1;
1938ad4eec61SEric Sandeen 		}
1939ad4eec61SEric Sandeen 
1940ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1941ad4eec61SEric Sandeen 		if (error) {
1942ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
1943ad4eec61SEric Sandeen 				"journal device path: error %d", error);
1944ad4eec61SEric Sandeen 			kfree(journal_path);
1945ad4eec61SEric Sandeen 			return -1;
1946ad4eec61SEric Sandeen 		}
1947ad4eec61SEric Sandeen 
19482b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
1949ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
1950ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1951ad4eec61SEric Sandeen 				"is not a block device", journal_path);
1952ad4eec61SEric Sandeen 			path_put(&path);
1953ad4eec61SEric Sandeen 			kfree(journal_path);
1954ad4eec61SEric Sandeen 			return -1;
1955ad4eec61SEric Sandeen 		}
1956ad4eec61SEric Sandeen 
1957ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1958ad4eec61SEric Sandeen 		path_put(&path);
1959ad4eec61SEric Sandeen 		kfree(journal_path);
19600efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
19610efb3b23SJan Kara 		if (arg > 7) {
19625f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
19630efb3b23SJan Kara 				 " (must be 0-7)");
19640efb3b23SJan Kara 			return -1;
19650efb3b23SJan Kara 		}
19660efb3b23SJan Kara 		*journal_ioprio =
19670efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
19686ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
1969643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
19706ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
19716ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
19726ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
19736ddb2447STheodore Ts'o #else
19746ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
19756ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
19766ddb2447STheodore Ts'o #endif
197726092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
197826092bf5STheodore Ts'o 		if (is_remount) {
197926092bf5STheodore Ts'o 			if (!sbi->s_journal)
198026092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
19815f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
198226092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
198326092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
198426092bf5STheodore Ts'o 				return -1;
198526092bf5STheodore Ts'o 			}
198626092bf5STheodore Ts'o 		} else {
198726092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
198826092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
198926092bf5STheodore Ts'o 		}
199026092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
199126092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
199226092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
199326092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
19945f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
19955f3633e3SJan Kara 				 "quota options when quota turned on");
199626092bf5STheodore Ts'o 			return -1;
199726092bf5STheodore Ts'o 		}
1998e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
1999c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2000c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2001262b4662SJan Kara 				 "when QUOTA feature is enabled");
2002c325a67cSTheodore Ts'o 			return 1;
2003262b4662SJan Kara 		}
200426092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
200526092bf5STheodore Ts'o #endif
2006923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
2007ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2008ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
2009ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2010ef83b6e8SDan Williams 		sbi->s_mount_opt |= m->mount_opt;
2011ef83b6e8SDan Williams #else
2012923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2013923ae0ffSRoss Zwisler 		return -1;
2014923ae0ffSRoss Zwisler #endif
20157915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
20167915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
20177915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
20187915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
201926092bf5STheodore Ts'o 	} else {
202026092bf5STheodore Ts'o 		if (!args->from)
202126092bf5STheodore Ts'o 			arg = 1;
202226092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
202326092bf5STheodore Ts'o 			arg = !arg;
202426092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
202526092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
202626092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
202726092bf5STheodore Ts'o 			WARN_ON(1);
202826092bf5STheodore Ts'o 			return -1;
202926092bf5STheodore Ts'o 		}
203026092bf5STheodore Ts'o 		if (arg != 0)
203126092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
203226092bf5STheodore Ts'o 		else
203326092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
203426092bf5STheodore Ts'o 	}
203526092bf5STheodore Ts'o 	return 1;
203626092bf5STheodore Ts'o }
203726092bf5STheodore Ts'o 
2038ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2039c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2040b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2041661aa520SEric Sandeen 			 int is_remount)
2042ac27a0ecSDave Kleikamp {
2043617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
204433458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2045ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
204626092bf5STheodore Ts'o 	int token;
2047ac27a0ecSDave Kleikamp 
2048ac27a0ecSDave Kleikamp 	if (!options)
2049ac27a0ecSDave Kleikamp 		return 1;
2050ac27a0ecSDave Kleikamp 
2051ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2052ac27a0ecSDave Kleikamp 		if (!*p)
2053ac27a0ecSDave Kleikamp 			continue;
205415121c18SEric Sandeen 		/*
205515121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
205615121c18SEric Sandeen 		 * found; some options take optional arguments.
205715121c18SEric Sandeen 		 */
2058caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2059ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
206026092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
206126092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2062ac27a0ecSDave Kleikamp 			return 0;
2063ac27a0ecSDave Kleikamp 	}
2064ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
206549da9392SJan Kara 	/*
206649da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
206749da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
206849da9392SJan Kara 	 * to support legacy quotas in quota files.
206949da9392SJan Kara 	 */
207049da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
207149da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
207249da9392SJan Kara 			 "Cannot enable project quota enforcement.");
207349da9392SJan Kara 		return 0;
207449da9392SJan Kara 	}
207533458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
207633458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
207733458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
207833458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2079fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2080ac27a0ecSDave Kleikamp 
208133458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2082fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2083ac27a0ecSDave Kleikamp 
208456c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2085b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2086b31e1552SEric Sandeen 					"format mixing");
2087ac27a0ecSDave Kleikamp 			return 0;
2088ac27a0ecSDave Kleikamp 		}
2089ac27a0ecSDave Kleikamp 
2090ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2091b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2092b31e1552SEric Sandeen 					"not specified");
2093ac27a0ecSDave Kleikamp 			return 0;
2094ac27a0ecSDave Kleikamp 		}
2095ac27a0ecSDave Kleikamp 	}
2096ac27a0ecSDave Kleikamp #endif
2097261cb20cSJan Kara 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2098261cb20cSJan Kara 		int blocksize =
2099261cb20cSJan Kara 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2100261cb20cSJan Kara 
210109cbfeafSKirill A. Shutemov 		if (blocksize < PAGE_SIZE) {
2102261cb20cSJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
2103261cb20cSJan Kara 				 "dioread_nolock if block size != PAGE_SIZE");
2104261cb20cSJan Kara 			return 0;
2105261cb20cSJan Kara 		}
2106261cb20cSJan Kara 	}
2107ac27a0ecSDave Kleikamp 	return 1;
2108ac27a0ecSDave Kleikamp }
2109ac27a0ecSDave Kleikamp 
21102adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
21112adf6da8STheodore Ts'o 					   struct super_block *sb)
21122adf6da8STheodore Ts'o {
21132adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
21142adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
211533458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
21162adf6da8STheodore Ts'o 
21172adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
21182adf6da8STheodore Ts'o 		char *fmtname = "";
21192adf6da8STheodore Ts'o 
21202adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
21212adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
21222adf6da8STheodore Ts'o 			fmtname = "vfsold";
21232adf6da8STheodore Ts'o 			break;
21242adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
21252adf6da8STheodore Ts'o 			fmtname = "vfsv0";
21262adf6da8STheodore Ts'o 			break;
21272adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
21282adf6da8STheodore Ts'o 			fmtname = "vfsv1";
21292adf6da8STheodore Ts'o 			break;
21302adf6da8STheodore Ts'o 		}
21312adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
21322adf6da8STheodore Ts'o 	}
21332adf6da8STheodore Ts'o 
213433458eabSTheodore Ts'o 	rcu_read_lock();
213533458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
213633458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
213733458eabSTheodore Ts'o 	if (usr_qf_name)
213833458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
213933458eabSTheodore Ts'o 	if (grp_qf_name)
214033458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
214133458eabSTheodore Ts'o 	rcu_read_unlock();
21422adf6da8STheodore Ts'o #endif
21432adf6da8STheodore Ts'o }
21442adf6da8STheodore Ts'o 
21455a916be1STheodore Ts'o static const char *token2str(int token)
21465a916be1STheodore Ts'o {
214750df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
21485a916be1STheodore Ts'o 
21495a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
21505a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
21515a916be1STheodore Ts'o 			break;
21525a916be1STheodore Ts'o 	return t->pattern;
21535a916be1STheodore Ts'o }
21545a916be1STheodore Ts'o 
21552adf6da8STheodore Ts'o /*
21562adf6da8STheodore Ts'o  * Show an option if
21572adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
21582adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
21592adf6da8STheodore Ts'o  */
216066acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
216166acdcf4STheodore Ts'o 			      int nodefs)
21622adf6da8STheodore Ts'o {
21632adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
21642adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
216568afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
21665a916be1STheodore Ts'o 	const struct mount_opts *m;
216766acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
21682adf6da8STheodore Ts'o 
216966acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
217066acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
21712adf6da8STheodore Ts'o 
21722adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
21735a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
21745a916be1STheodore Ts'o 
21755a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
21765a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
21775a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
21785a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
21795a916be1STheodore Ts'o 			continue;
218068afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
21815a916be1STheodore Ts'o 			continue; /* skip if same as the default */
21825a916be1STheodore Ts'o 		if ((want_set &&
21835a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
21845a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
21855a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
21865a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
21875a916be1STheodore Ts'o 	}
21885a916be1STheodore Ts'o 
218908cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
21905a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
219108cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
219208cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
219308cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
21945a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
219508cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
219608cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
219766acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
21985a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
21995a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
22002adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
22015a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
22022adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
22035a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
220466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
22055a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
220666acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
22075a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
220866acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
22095a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2210357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
22115a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
221266acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
22135a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
221468afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
221568afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
22162adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
22175a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
22182adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
22195a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
22202adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
22215a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
22225a916be1STheodore Ts'o 	}
222366acdcf4STheodore Ts'o 	if (nodefs ||
222466acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
22255a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
22262adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
22272adf6da8STheodore Ts'o 
2228ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
222966acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
22305a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2231df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2232df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
22337915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
22347915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2235338affb5SEric Biggers 	if (DUMMY_ENCRYPTION_ENABLED(sbi))
2236338affb5SEric Biggers 		SEQ_OPTS_PUTS("test_dummy_encryption");
22372adf6da8STheodore Ts'o 
22382adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
22392adf6da8STheodore Ts'o 	return 0;
22402adf6da8STheodore Ts'o }
22412adf6da8STheodore Ts'o 
224266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
224366acdcf4STheodore Ts'o {
224466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
224566acdcf4STheodore Ts'o }
224666acdcf4STheodore Ts'o 
2247ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
224866acdcf4STheodore Ts'o {
224966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
225066acdcf4STheodore Ts'o 	int rc;
225166acdcf4STheodore Ts'o 
2252bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
225366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
225466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
225566acdcf4STheodore Ts'o 	return rc;
225666acdcf4STheodore Ts'o }
225766acdcf4STheodore Ts'o 
2258617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2259ac27a0ecSDave Kleikamp 			    int read_only)
2260ac27a0ecSDave Kleikamp {
2261617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2262c89128a0SJaegeuk Kim 	int err = 0;
2263ac27a0ecSDave Kleikamp 
2264617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2265b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2266b31e1552SEric Sandeen 			 "forcing read-only mode");
2267c89128a0SJaegeuk Kim 		err = -EROFS;
2268ac27a0ecSDave Kleikamp 	}
2269ac27a0ecSDave Kleikamp 	if (read_only)
2270281b5995STheodore Ts'o 		goto done;
2271617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2272b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2273b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2274c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2275b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2276b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2277b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2278ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2279ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2280ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2281b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2282b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2283b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2284ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
22856a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
22866a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2287b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2288b31e1552SEric Sandeen 			 "warning: checktime reached, "
2289b31e1552SEric Sandeen 			 "running e2fsck is recommended");
22900390131bSFrank Mayhar 	if (!sbi->s_journal)
2291216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2292ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2293617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2294e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
22956a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
22960390131bSFrank Mayhar 	if (sbi->s_journal)
2297e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2298ac27a0ecSDave Kleikamp 
2299c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2300281b5995STheodore Ts'o done:
2301ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2302a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2303a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2304ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2305ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2306617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2307617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2308a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2309ac27a0ecSDave Kleikamp 
23107abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2311c89128a0SJaegeuk Kim 	return err;
2312ac27a0ecSDave Kleikamp }
2313ac27a0ecSDave Kleikamp 
2314117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2315117fff10STheodore Ts'o {
2316117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2317117fff10STheodore Ts'o 	struct flex_groups *new_groups;
2318117fff10STheodore Ts'o 	int size;
2319117fff10STheodore Ts'o 
2320117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2321117fff10STheodore Ts'o 		return 0;
2322117fff10STheodore Ts'o 
2323117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2324117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2325117fff10STheodore Ts'o 		return 0;
2326117fff10STheodore Ts'o 
2327117fff10STheodore Ts'o 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
2328a7c3e901SMichal Hocko 	new_groups = kvzalloc(size, GFP_KERNEL);
2329117fff10STheodore Ts'o 	if (!new_groups) {
2330117fff10STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
2331117fff10STheodore Ts'o 			 size / (int) sizeof(struct flex_groups));
2332117fff10STheodore Ts'o 		return -ENOMEM;
2333117fff10STheodore Ts'o 	}
2334117fff10STheodore Ts'o 
2335117fff10STheodore Ts'o 	if (sbi->s_flex_groups) {
2336117fff10STheodore Ts'o 		memcpy(new_groups, sbi->s_flex_groups,
2337117fff10STheodore Ts'o 		       (sbi->s_flex_groups_allocated *
2338117fff10STheodore Ts'o 			sizeof(struct flex_groups)));
2339b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
2340117fff10STheodore Ts'o 	}
2341117fff10STheodore Ts'o 	sbi->s_flex_groups = new_groups;
2342117fff10STheodore Ts'o 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
2343117fff10STheodore Ts'o 	return 0;
2344117fff10STheodore Ts'o }
2345117fff10STheodore Ts'o 
2346772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2347772cb7c8SJose R. Santos {
2348772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2349772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
2350772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2351117fff10STheodore Ts'o 	int i, err;
2352772cb7c8SJose R. Santos 
2353503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2354d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2355772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2356772cb7c8SJose R. Santos 		return 1;
2357772cb7c8SJose R. Santos 	}
2358772cb7c8SJose R. Santos 
2359117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2360117fff10STheodore Ts'o 	if (err)
2361772cb7c8SJose R. Santos 		goto failed;
2362772cb7c8SJose R. Santos 
2363772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
236488b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2365772cb7c8SJose R. Santos 
2366772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
23677ad9bb65STheodore Ts'o 		atomic_add(ext4_free_inodes_count(sb, gdp),
23687ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_inodes);
236990ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
237024aaa8efSTheodore Ts'o 			     &sbi->s_flex_groups[flex_group].free_clusters);
23717ad9bb65STheodore Ts'o 		atomic_add(ext4_used_dirs_count(sb, gdp),
23727ad9bb65STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].used_dirs);
2373772cb7c8SJose R. Santos 	}
2374772cb7c8SJose R. Santos 
2375772cb7c8SJose R. Santos 	return 1;
2376772cb7c8SJose R. Santos failed:
2377772cb7c8SJose R. Santos 	return 0;
2378772cb7c8SJose R. Santos }
2379772cb7c8SJose R. Santos 
2380e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2381717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2382717d50e4SAndreas Dilger {
2383b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2384717d50e4SAndreas Dilger 	__u16 crc = 0;
2385717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2386e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2387717d50e4SAndreas Dilger 
23889aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2389feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2390feb0ab32SDarrick J. Wong 		__u32 csum32;
2391b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2392feb0ab32SDarrick J. Wong 
2393feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2394feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2395b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2396b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2397b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2398b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2399b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2400b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2401b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2402feb0ab32SDarrick J. Wong 
2403feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2404feb0ab32SDarrick J. Wong 		goto out;
2405feb0ab32SDarrick J. Wong 	}
2406feb0ab32SDarrick J. Wong 
2407feb0ab32SDarrick J. Wong 	/* old crc16 code */
2408e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2409813d32f9SDarrick J. Wong 		return 0;
2410813d32f9SDarrick J. Wong 
2411717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2412717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2413717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2414717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2415717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2416e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2417717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2418717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2419717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2420717d50e4SAndreas Dilger 				offset);
2421717d50e4SAndreas Dilger 
2422feb0ab32SDarrick J. Wong out:
2423717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2424717d50e4SAndreas Dilger }
2425717d50e4SAndreas Dilger 
2426feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2427717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2428717d50e4SAndreas Dilger {
2429feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2430e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2431717d50e4SAndreas Dilger 		return 0;
2432717d50e4SAndreas Dilger 
2433717d50e4SAndreas Dilger 	return 1;
2434717d50e4SAndreas Dilger }
2435717d50e4SAndreas Dilger 
2436feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2437feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2438feb0ab32SDarrick J. Wong {
2439feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2440feb0ab32SDarrick J. Wong 		return;
2441e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2442feb0ab32SDarrick J. Wong }
2443feb0ab32SDarrick J. Wong 
2444ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2445bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2446829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2447bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2448ac27a0ecSDave Kleikamp {
2449617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2450617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2451617ba13bSMingming Cao 	ext4_fsblk_t last_block;
245244de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2453bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2454bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2455bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2456ce421581SJose R. Santos 	int flexbg_flag = 0;
2457bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2458ac27a0ecSDave Kleikamp 
2459e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2460ce421581SJose R. Santos 		flexbg_flag = 1;
2461ce421581SJose R. Santos 
2462617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2463ac27a0ecSDave Kleikamp 
2464197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2465197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2466197cd65aSAkinobu Mita 
2467ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2468bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2469ac27a0ecSDave Kleikamp 		else
2470ac27a0ecSDave Kleikamp 			last_block = first_block +
2471617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2472ac27a0ecSDave Kleikamp 
2473bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2474bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2475bfff6873SLukas Czerner 			grp = i;
2476bfff6873SLukas Czerner 
24778fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2478829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2479829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2480829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2481829fa70dSTheodore Ts'o 				 "superblock", i);
248218db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
248318db4b4eSTheodore Ts'o 				return 0;
2484829fa70dSTheodore Ts'o 		}
248577260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
248677260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
248777260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
248877260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
248977260807STheodore Ts'o 				 "block group descriptors", i);
249077260807STheodore Ts'o 			if (!sb_rdonly(sb))
249177260807STheodore Ts'o 				return 0;
249277260807STheodore Ts'o 		}
24932b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2494b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2495a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2496b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2497ac27a0ecSDave Kleikamp 			return 0;
2498ac27a0ecSDave Kleikamp 		}
24998fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2500829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2501829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2502829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2503829fa70dSTheodore Ts'o 				 "superblock", i);
250418db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
250518db4b4eSTheodore Ts'o 				return 0;
2506829fa70dSTheodore Ts'o 		}
250777260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
250877260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
250977260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
251077260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
251177260807STheodore Ts'o 				 "block group descriptors", i);
251277260807STheodore Ts'o 			if (!sb_rdonly(sb))
251377260807STheodore Ts'o 				return 0;
251477260807STheodore Ts'o 		}
25152b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2516b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2517a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2518b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2519ac27a0ecSDave Kleikamp 			return 0;
2520ac27a0ecSDave Kleikamp 		}
25218fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2522829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2523829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2524829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2525829fa70dSTheodore Ts'o 				 "superblock", i);
252618db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
252718db4b4eSTheodore Ts'o 				return 0;
2528829fa70dSTheodore Ts'o 		}
252977260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
253077260807STheodore Ts'o 		    inode_table <= last_bg_block) {
253177260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
253277260807STheodore Ts'o 				 "Inode table for group %u overlaps "
253377260807STheodore Ts'o 				 "block group descriptors", i);
253477260807STheodore Ts'o 			if (!sb_rdonly(sb))
253577260807STheodore Ts'o 				return 0;
253677260807STheodore Ts'o 		}
2537bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
25382b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2539b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2540a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2541b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2542ac27a0ecSDave Kleikamp 			return 0;
2543ac27a0ecSDave Kleikamp 		}
2544955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2545feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2546b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2547b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2548e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2549fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2550bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2551955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2552717d50e4SAndreas Dilger 				return 0;
2553717d50e4SAndreas Dilger 			}
25547ee1ec4cSLi Zefan 		}
2555955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2556ce421581SJose R. Santos 		if (!flexbg_flag)
2557617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2558ac27a0ecSDave Kleikamp 	}
2559bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2560bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2561ac27a0ecSDave Kleikamp 	return 1;
2562ac27a0ecSDave Kleikamp }
2563ac27a0ecSDave Kleikamp 
2564617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2565ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2566ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2567ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2568ac27a0ecSDave Kleikamp  *
2569ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2570ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2571ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2572ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2573ac27a0ecSDave Kleikamp  *
2574ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2575ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2576ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2577617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2578ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2579ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2580ac27a0ecSDave Kleikamp  */
2581617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2582617ba13bSMingming Cao 				struct ext4_super_block *es)
2583ac27a0ecSDave Kleikamp {
2584ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
25852c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2586ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
258795f1fda4Szhangyi (F) 	int quota_update = 0;
2588ac27a0ecSDave Kleikamp 	int i;
2589ac27a0ecSDave Kleikamp #endif
2590ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2591ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2592ac27a0ecSDave Kleikamp 		return;
2593ac27a0ecSDave Kleikamp 	}
2594ac27a0ecSDave Kleikamp 
2595a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2596b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2597b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2598a8f48a95SEric Sandeen 		return;
2599a8f48a95SEric Sandeen 	}
2600a8f48a95SEric Sandeen 
2601d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2602d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2603d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2604d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2605d39195c3SAmir Goldstein 		return;
2606d39195c3SAmir Goldstein 	}
2607d39195c3SAmir Goldstein 
2608617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2609c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
26101751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
261184474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2612ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2613ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2614c25f9bc6SEric Sandeen 		}
2615ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2616ac27a0ecSDave Kleikamp 		return;
2617ac27a0ecSDave Kleikamp 	}
2618ac27a0ecSDave Kleikamp 
26191751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2620b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
26211751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2622ac27a0ecSDave Kleikamp 	}
2623ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2624ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
26251751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
262695f1fda4Szhangyi (F) 
262795f1fda4Szhangyi (F) 	/*
262895f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
262995f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
263095f1fda4Szhangyi (F) 	 */
26311751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
263295f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
263395f1fda4Szhangyi (F) 
263495f1fda4Szhangyi (F) 		if (!ret)
263595f1fda4Szhangyi (F) 			quota_update = 1;
263695f1fda4Szhangyi (F) 		else
263795f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
263895f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
263995f1fda4Szhangyi (F) 	}
264095f1fda4Szhangyi (F) 
264195f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2642a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2643617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2644617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
264595f1fda4Szhangyi (F) 
264695f1fda4Szhangyi (F) 			if (!ret)
264795f1fda4Szhangyi (F) 				quota_update = 1;
264895f1fda4Szhangyi (F) 			else
2649b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2650b31e1552SEric Sandeen 					"Cannot turn on journaled "
265195f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2652ac27a0ecSDave Kleikamp 		}
2653ac27a0ecSDave Kleikamp 	}
2654ac27a0ecSDave Kleikamp #endif
2655ac27a0ecSDave Kleikamp 
2656ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2657ac27a0ecSDave Kleikamp 		struct inode *inode;
2658ac27a0ecSDave Kleikamp 
2659c65d5c6cSVegard Nossum 		/*
2660c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2661c65d5c6cSVegard Nossum 		 * so, skip the rest.
2662c65d5c6cSVegard Nossum 		 */
2663c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2664c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2665c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2666c65d5c6cSVegard Nossum 			break;
2667c65d5c6cSVegard Nossum 		}
2668c65d5c6cSVegard Nossum 
266997bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
267097bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2671ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2672ac27a0ecSDave Kleikamp 			break;
2673ac27a0ecSDave Kleikamp 		}
2674ac27a0ecSDave Kleikamp 
2675617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2676871a2931SChristoph Hellwig 		dquot_initialize(inode);
2677ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2678566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2679b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2680b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
268146e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2682e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2683ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
26845955102cSAl Viro 			inode_lock(inode);
268555f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
26862c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
26872c98eb5eSTheodore Ts'o 			if (ret)
26882c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
26895955102cSAl Viro 			inode_unlock(inode);
2690ac27a0ecSDave Kleikamp 			nr_truncates++;
2691ac27a0ecSDave Kleikamp 		} else {
2692566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2693b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2694b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
269546e665e9SHarvey Harrison 					__func__, inode->i_ino);
2696ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2697ac27a0ecSDave Kleikamp 				  inode->i_ino);
2698ac27a0ecSDave Kleikamp 			nr_orphans++;
2699ac27a0ecSDave Kleikamp 		}
2700ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2701ac27a0ecSDave Kleikamp 	}
2702ac27a0ecSDave Kleikamp 
2703ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2704ac27a0ecSDave Kleikamp 
2705ac27a0ecSDave Kleikamp 	if (nr_orphans)
2706b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2707b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2708ac27a0ecSDave Kleikamp 	if (nr_truncates)
2709b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2710b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2711ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
271295f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
271395f1fda4Szhangyi (F) 	if (quota_update) {
2714a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
271595f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2716287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2717ac27a0ecSDave Kleikamp 		}
271895f1fda4Szhangyi (F) 	}
2719ac27a0ecSDave Kleikamp #endif
27201751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2721ac27a0ecSDave Kleikamp }
27220b8e58a1SAndreas Dilger 
2723cd2291a4SEric Sandeen /*
2724cd2291a4SEric Sandeen  * Maximal extent format file size.
2725cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2726cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2727cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2728cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2729cd2291a4SEric Sandeen  *
2730f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2731f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2732f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2733f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2734f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2735f17722f9SLukas Czerner  *
2736cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2737cd2291a4SEric Sandeen  */
2738f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2739cd2291a4SEric Sandeen {
2740cd2291a4SEric Sandeen 	loff_t res;
2741cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2742cd2291a4SEric Sandeen 
2743cd2291a4SEric Sandeen 	/* small i_blocks in vfs inode? */
2744f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2745cd2291a4SEric Sandeen 		/*
274690c699a9SBartlomiej Zolnierkiewicz 		 * CONFIG_LBDAF is not enabled implies the inode
2747cd2291a4SEric Sandeen 		 * i_block represent total blocks in 512 bytes
2748cd2291a4SEric Sandeen 		 * 32 == size of vfs inode i_blocks * 8
2749cd2291a4SEric Sandeen 		 */
2750cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2751cd2291a4SEric Sandeen 
2752cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2753cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2754cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2755cd2291a4SEric Sandeen 	}
2756cd2291a4SEric Sandeen 
2757f17722f9SLukas Czerner 	/*
2758f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2759f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2760f17722f9SLukas Czerner 	 * size
2761f17722f9SLukas Czerner 	 */
2762f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2763cd2291a4SEric Sandeen 	res <<= blkbits;
2764cd2291a4SEric Sandeen 
2765cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2766cd2291a4SEric Sandeen 	if (res > upper_limit)
2767cd2291a4SEric Sandeen 		res = upper_limit;
2768cd2291a4SEric Sandeen 
2769cd2291a4SEric Sandeen 	return res;
2770cd2291a4SEric Sandeen }
2771ac27a0ecSDave Kleikamp 
2772ac27a0ecSDave Kleikamp /*
2773cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
27740fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
27750fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2776ac27a0ecSDave Kleikamp  */
2777f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2778ac27a0ecSDave Kleikamp {
2779617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
27800fc1b451SAneesh Kumar K.V 	int meta_blocks;
27810fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
27820b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
27830b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
27840b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
27850b8e58a1SAndreas Dilger 	 *
27860b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
27870b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
27880fc1b451SAneesh Kumar K.V 	 */
27890fc1b451SAneesh Kumar K.V 
2790f287a1a5STheodore Ts'o 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
27910fc1b451SAneesh Kumar K.V 		/*
279290c699a9SBartlomiej Zolnierkiewicz 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
27930b8e58a1SAndreas Dilger 		 * the inode i_block field represents total file blocks in
27940b8e58a1SAndreas Dilger 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
27950fc1b451SAneesh Kumar K.V 		 */
27960fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
27970fc1b451SAneesh Kumar K.V 
27980fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
27990fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
28000fc1b451SAneesh Kumar K.V 
28010fc1b451SAneesh Kumar K.V 	} else {
28028180a562SAneesh Kumar K.V 		/*
28038180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
28048180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
28058180a562SAneesh Kumar K.V 		 * represent total number of blocks in
28068180a562SAneesh Kumar K.V 		 * file system block size
28078180a562SAneesh Kumar K.V 		 */
28080fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
28090fc1b451SAneesh Kumar K.V 
28100fc1b451SAneesh Kumar K.V 	}
28110fc1b451SAneesh Kumar K.V 
28120fc1b451SAneesh Kumar K.V 	/* indirect blocks */
28130fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
28140fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
28150fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
28160fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
28170fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
28180fc1b451SAneesh Kumar K.V 
28190fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
28200fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2821ac27a0ecSDave Kleikamp 
2822ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2823ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2824ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2825ac27a0ecSDave Kleikamp 	res <<= bits;
2826ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2827ac27a0ecSDave Kleikamp 		res = upper_limit;
28280fc1b451SAneesh Kumar K.V 
28290fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
28300fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
28310fc1b451SAneesh Kumar K.V 
2832ac27a0ecSDave Kleikamp 	return res;
2833ac27a0ecSDave Kleikamp }
2834ac27a0ecSDave Kleikamp 
2835617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
283670bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2837ac27a0ecSDave Kleikamp {
2838617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2839fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2840ac27a0ecSDave Kleikamp 	int has_super = 0;
2841ac27a0ecSDave Kleikamp 
2842ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2843ac27a0ecSDave Kleikamp 
2844e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
284570bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2846ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2847617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2848ac27a0ecSDave Kleikamp 		has_super = 1;
28490b8e58a1SAndreas Dilger 
2850bd63f6b0SDarrick J. Wong 	/*
2851bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2852bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2853bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2854bd63f6b0SDarrick J. Wong 	 * compensate.
2855bd63f6b0SDarrick J. Wong 	 */
2856bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
285749598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
2858bd63f6b0SDarrick J. Wong 		has_super++;
2859bd63f6b0SDarrick J. Wong 
2860617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2861ac27a0ecSDave Kleikamp }
2862ac27a0ecSDave Kleikamp 
2863c9de560dSAlex Tomas /**
2864c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2865c9de560dSAlex Tomas  * @sbi: In memory super block info
2866c9de560dSAlex Tomas  *
2867c9de560dSAlex Tomas  * If we have specified it via mount option, then
2868c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2869c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2870c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2871c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2872c9de560dSAlex Tomas  *
2873c9de560dSAlex Tomas  */
2874c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2875c9de560dSAlex Tomas {
2876c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2877c9de560dSAlex Tomas 	unsigned long stripe_width =
2878c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
28793eb08658SDan Ehrenberg 	int ret;
2880c9de560dSAlex Tomas 
2881c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
28823eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
28835469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
28843eb08658SDan Ehrenberg 		ret = stripe_width;
28855469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
28863eb08658SDan Ehrenberg 		ret = stride;
28873eb08658SDan Ehrenberg 	else
28883eb08658SDan Ehrenberg 		ret = 0;
2889c9de560dSAlex Tomas 
28903eb08658SDan Ehrenberg 	/*
28913eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
28923eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
28933eb08658SDan Ehrenberg 	 */
28943eb08658SDan Ehrenberg 	if (ret <= 1)
28953eb08658SDan Ehrenberg 		ret = 0;
2896c9de560dSAlex Tomas 
28973eb08658SDan Ehrenberg 	return ret;
2898c9de560dSAlex Tomas }
2899ac27a0ecSDave Kleikamp 
2900a13fb1a4SEric Sandeen /*
2901a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2902a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2903a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2904a13fb1a4SEric Sandeen  * 0 if it cannot be.
2905a13fb1a4SEric Sandeen  */
2906a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2907a13fb1a4SEric Sandeen {
2908e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2909a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
2910a13fb1a4SEric Sandeen 			"Couldn't mount because of "
2911a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
2912a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2913a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
2914a13fb1a4SEric Sandeen 		return 0;
2915a13fb1a4SEric Sandeen 	}
2916a13fb1a4SEric Sandeen 
2917c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
2918c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
2919c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
2920c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
2921c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
2922c83ad55eSGabriel Krisman Bertazi 		return 0;
2923c83ad55eSGabriel Krisman Bertazi 	}
2924c83ad55eSGabriel Krisman Bertazi #endif
2925c83ad55eSGabriel Krisman Bertazi 
2926a13fb1a4SEric Sandeen 	if (readonly)
2927a13fb1a4SEric Sandeen 		return 1;
2928a13fb1a4SEric Sandeen 
2929e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
29302cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
29311751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
29322cb5cc8bSDarrick J. Wong 		return 1;
29332cb5cc8bSDarrick J. Wong 	}
29342cb5cc8bSDarrick J. Wong 
2935a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
2936e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
2937a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2938a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
2939a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2940a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2941a13fb1a4SEric Sandeen 		return 0;
2942a13fb1a4SEric Sandeen 	}
2943a13fb1a4SEric Sandeen 	/*
2944a13fb1a4SEric Sandeen 	 * Large file size enabled file system can only be mounted
2945a13fb1a4SEric Sandeen 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2946a13fb1a4SEric Sandeen 	 */
2947e2b911c5SDarrick J. Wong 	if (ext4_has_feature_huge_file(sb)) {
2948a13fb1a4SEric Sandeen 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2949a13fb1a4SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2950a13fb1a4SEric Sandeen 				 "cannot be mounted RDWR without "
2951a13fb1a4SEric Sandeen 				 "CONFIG_LBDAF");
2952a13fb1a4SEric Sandeen 			return 0;
2953a13fb1a4SEric Sandeen 		}
2954a13fb1a4SEric Sandeen 	}
2955e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
2956bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
2957bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
2958bab08ab9STheodore Ts'o 			 "extents feature\n");
2959bab08ab9STheodore Ts'o 		return 0;
2960bab08ab9STheodore Ts'o 	}
29617c319d32SAditya Kali 
29627c319d32SAditya Kali #ifndef CONFIG_QUOTA
2963e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) && !readonly) {
29647c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
29657c319d32SAditya Kali 			 "Filesystem with quota feature cannot be mounted RDWR "
29667c319d32SAditya Kali 			 "without CONFIG_QUOTA");
29677c319d32SAditya Kali 		return 0;
29687c319d32SAditya Kali 	}
2969689c958cSLi Xi 	if (ext4_has_feature_project(sb) && !readonly) {
2970689c958cSLi Xi 		ext4_msg(sb, KERN_ERR,
2971689c958cSLi Xi 			 "Filesystem with project quota feature cannot be mounted RDWR "
2972689c958cSLi Xi 			 "without CONFIG_QUOTA");
2973689c958cSLi Xi 		return 0;
2974689c958cSLi Xi 	}
29757c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
2976a13fb1a4SEric Sandeen 	return 1;
2977a13fb1a4SEric Sandeen }
2978a13fb1a4SEric Sandeen 
297966e61a9eSTheodore Ts'o /*
298066e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
298166e61a9eSTheodore Ts'o  * on the file system
298266e61a9eSTheodore Ts'o  */
2983235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
298466e61a9eSTheodore Ts'o {
2985235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
2986235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
2987235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
298866e61a9eSTheodore Ts'o 
298966e61a9eSTheodore Ts'o 	if (es->s_error_count)
2990ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2991ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
299266e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
299366e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
29946a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
29956a0678a7SArnd Bergmann 		       sb->s_id,
29966a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
299766e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
299866e61a9eSTheodore Ts'o 		       es->s_first_error_func,
299966e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
300066e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3001651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
300266e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
300366e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3004651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
300566e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3006651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
300766e61a9eSTheodore Ts'o 	}
300866e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
30096a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
30106a0678a7SArnd Bergmann 		       sb->s_id,
30116a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
301266e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
301366e61a9eSTheodore Ts'o 		       es->s_last_error_func,
301466e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
301566e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3016651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
301766e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
301866e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3019651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
302066e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3021651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
302266e61a9eSTheodore Ts'o 	}
302366e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
302466e61a9eSTheodore Ts'o }
302566e61a9eSTheodore Ts'o 
3026bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3027bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3028bfff6873SLukas Czerner {
3029bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3030bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3031bfff6873SLukas Czerner 	struct super_block *sb;
3032bfff6873SLukas Czerner 	unsigned long timeout = 0;
3033bfff6873SLukas Czerner 	int ret = 0;
3034bfff6873SLukas Czerner 
3035bfff6873SLukas Czerner 	sb = elr->lr_super;
3036bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3037bfff6873SLukas Czerner 
3038bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3039bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3040bfff6873SLukas Czerner 		if (!gdp) {
3041bfff6873SLukas Czerner 			ret = 1;
3042bfff6873SLukas Czerner 			break;
3043bfff6873SLukas Czerner 		}
3044bfff6873SLukas Czerner 
3045bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3046bfff6873SLukas Czerner 			break;
3047bfff6873SLukas Czerner 	}
3048bfff6873SLukas Czerner 
30497f511862STheodore Ts'o 	if (group >= ngroups)
3050bfff6873SLukas Czerner 		ret = 1;
3051bfff6873SLukas Czerner 
3052bfff6873SLukas Czerner 	if (!ret) {
3053bfff6873SLukas Czerner 		timeout = jiffies;
3054bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3055bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3056bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
305751ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
305851ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3059bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3060bfff6873SLukas Czerner 		}
3061bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3062bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3063bfff6873SLukas Czerner 	}
3064bfff6873SLukas Czerner 	return ret;
3065bfff6873SLukas Czerner }
3066bfff6873SLukas Czerner 
3067bfff6873SLukas Czerner /*
3068bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
30694ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3070bfff6873SLukas Czerner  */
3071bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3072bfff6873SLukas Czerner {
3073bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3074bfff6873SLukas Czerner 
3075bfff6873SLukas Czerner 	if (!elr)
3076bfff6873SLukas Czerner 		return;
3077bfff6873SLukas Czerner 
3078bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3079bfff6873SLukas Czerner 
3080bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3081bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3082bfff6873SLukas Czerner 	kfree(elr);
3083bfff6873SLukas Czerner }
3084bfff6873SLukas Czerner 
3085bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3086bfff6873SLukas Czerner {
30871bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
30881bb933fbSLukas Czerner 	if (!ext4_li_info) {
30891bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3090bfff6873SLukas Czerner 		return;
30911bb933fbSLukas Czerner 	}
3092bfff6873SLukas Czerner 
3093bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
30941bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3095bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
30961bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3097bfff6873SLukas Czerner }
3098bfff6873SLukas Czerner 
30998f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
31008f1f7453SEric Sandeen 
3101bfff6873SLukas Czerner /*
3102bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3103bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3104bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3105bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3106bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3107bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3108bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3109bfff6873SLukas Czerner  */
3110bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3111bfff6873SLukas Czerner {
3112bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3113bfff6873SLukas Czerner 	struct list_head *pos, *n;
3114bfff6873SLukas Czerner 	struct ext4_li_request *elr;
31154ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3116bfff6873SLukas Czerner 
3117bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3118bfff6873SLukas Czerner 
3119bfff6873SLukas Czerner cont_thread:
3120bfff6873SLukas Czerner 	while (true) {
3121bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3122bfff6873SLukas Czerner 
3123bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3124bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3125bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3126bfff6873SLukas Czerner 			goto exit_thread;
3127bfff6873SLukas Czerner 		}
3128bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3129e22834f0SDmitry Monakhov 			int err = 0;
3130e22834f0SDmitry Monakhov 			int progress = 0;
3131bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3132bfff6873SLukas Czerner 					 lr_request);
3133bfff6873SLukas Czerner 
3134e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3135e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3136e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3137e22834f0SDmitry Monakhov 				continue;
3138e22834f0SDmitry Monakhov 			}
3139e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3140e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3141e22834f0SDmitry Monakhov 					progress = 1;
3142e22834f0SDmitry Monakhov 					/*
3143e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3144e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3145e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3146e22834f0SDmitry Monakhov 					 */
3147e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3148e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3149e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3150e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3151e22834f0SDmitry Monakhov 					n = pos->next;
3152e22834f0SDmitry Monakhov 				}
3153e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3154e22834f0SDmitry Monakhov 			}
3155b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3156e22834f0SDmitry Monakhov 			if (err) {
3157bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3158bfff6873SLukas Czerner 				continue;
3159bfff6873SLukas Czerner 			}
3160e22834f0SDmitry Monakhov 			if (!progress) {
3161e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3162e22834f0SDmitry Monakhov 					(prandom_u32()
3163e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3164b2c78cd0STheodore Ts'o 			}
3165bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3166bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3167bfff6873SLukas Czerner 		}
3168bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3169bfff6873SLukas Czerner 
3170a0acae0eSTejun Heo 		try_to_freeze();
3171bfff6873SLukas Czerner 
31724ed5c033SLukas Czerner 		cur = jiffies;
31734ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3174f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3175bfff6873SLukas Czerner 			cond_resched();
3176bfff6873SLukas Czerner 			continue;
3177bfff6873SLukas Czerner 		}
3178bfff6873SLukas Czerner 
31794ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
31804ed5c033SLukas Czerner 
31818f1f7453SEric Sandeen 		if (kthread_should_stop()) {
31828f1f7453SEric Sandeen 			ext4_clear_request_list();
31838f1f7453SEric Sandeen 			goto exit_thread;
31848f1f7453SEric Sandeen 		}
3185bfff6873SLukas Czerner 	}
3186bfff6873SLukas Czerner 
3187bfff6873SLukas Czerner exit_thread:
3188bfff6873SLukas Czerner 	/*
3189bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3190bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3191bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3192bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3193bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3194bfff6873SLukas Czerner 	 * new one.
3195bfff6873SLukas Czerner 	 */
3196bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3197bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3198bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3199bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3200bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3201bfff6873SLukas Czerner 		goto cont_thread;
3202bfff6873SLukas Czerner 	}
3203bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3204bfff6873SLukas Czerner 	kfree(ext4_li_info);
3205bfff6873SLukas Czerner 	ext4_li_info = NULL;
3206bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3207bfff6873SLukas Czerner 
3208bfff6873SLukas Czerner 	return 0;
3209bfff6873SLukas Czerner }
3210bfff6873SLukas Czerner 
3211bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3212bfff6873SLukas Czerner {
3213bfff6873SLukas Czerner 	struct list_head *pos, *n;
3214bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3215bfff6873SLukas Czerner 
3216bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3217bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3218bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3219bfff6873SLukas Czerner 				 lr_request);
3220bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3221bfff6873SLukas Czerner 	}
3222bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3223bfff6873SLukas Czerner }
3224bfff6873SLukas Czerner 
3225bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3226bfff6873SLukas Czerner {
32278f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
32288f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
32298f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
32308f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3231bfff6873SLukas Czerner 		ext4_clear_request_list();
3232bfff6873SLukas Czerner 		kfree(ext4_li_info);
3233bfff6873SLukas Czerner 		ext4_li_info = NULL;
323492b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3235bfff6873SLukas Czerner 				 "initialization thread\n",
3236bfff6873SLukas Czerner 				 err);
3237bfff6873SLukas Czerner 		return err;
3238bfff6873SLukas Czerner 	}
3239bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3240bfff6873SLukas Czerner 	return 0;
3241bfff6873SLukas Czerner }
3242bfff6873SLukas Czerner 
3243bfff6873SLukas Czerner /*
3244bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3245bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3246bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3247bfff6873SLukas Czerner  * groups and return total number of groups.
3248bfff6873SLukas Czerner  */
3249bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3250bfff6873SLukas Czerner {
3251bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3252bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3253bfff6873SLukas Czerner 
32548844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
32558844618dSTheodore Ts'o 		return ngroups;
32568844618dSTheodore Ts'o 
3257bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3258bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3259bfff6873SLukas Czerner 		if (!gdp)
3260bfff6873SLukas Czerner 			continue;
3261bfff6873SLukas Czerner 
326250122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3263bfff6873SLukas Czerner 			break;
3264bfff6873SLukas Czerner 	}
3265bfff6873SLukas Czerner 
3266bfff6873SLukas Czerner 	return group;
3267bfff6873SLukas Czerner }
3268bfff6873SLukas Czerner 
3269bfff6873SLukas Czerner static int ext4_li_info_new(void)
3270bfff6873SLukas Czerner {
3271bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3272bfff6873SLukas Czerner 
3273bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3274bfff6873SLukas Czerner 	if (!eli)
3275bfff6873SLukas Czerner 		return -ENOMEM;
3276bfff6873SLukas Czerner 
3277bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3278bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3279bfff6873SLukas Czerner 
3280bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3281bfff6873SLukas Czerner 
3282bfff6873SLukas Czerner 	ext4_li_info = eli;
3283bfff6873SLukas Czerner 
3284bfff6873SLukas Czerner 	return 0;
3285bfff6873SLukas Czerner }
3286bfff6873SLukas Czerner 
3287bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3288bfff6873SLukas Czerner 					    ext4_group_t start)
3289bfff6873SLukas Czerner {
3290bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3291bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3292bfff6873SLukas Czerner 
3293bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3294bfff6873SLukas Czerner 	if (!elr)
3295bfff6873SLukas Czerner 		return NULL;
3296bfff6873SLukas Czerner 
3297bfff6873SLukas Czerner 	elr->lr_super = sb;
3298bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3299bfff6873SLukas Czerner 	elr->lr_next_group = start;
3300bfff6873SLukas Czerner 
3301bfff6873SLukas Czerner 	/*
3302bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3303bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3304bfff6873SLukas Czerner 	 * better.
3305bfff6873SLukas Czerner 	 */
3306dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3307dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3308bfff6873SLukas Czerner 	return elr;
3309bfff6873SLukas Czerner }
3310bfff6873SLukas Czerner 
33117f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3312bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3313bfff6873SLukas Czerner {
3314bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
33157f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
331649598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
33176c5a6cb9SAndrew Morton 	int ret = 0;
3318bfff6873SLukas Czerner 
33197f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
332051ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
332151ce6511SLukas Czerner 		/*
332251ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
332351ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
332451ce6511SLukas Czerner 		 */
332551ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
33267f511862STheodore Ts'o 		goto out;
332751ce6511SLukas Czerner 	}
3328bfff6873SLukas Czerner 
3329bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
333055ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
33317f511862STheodore Ts'o 		goto out;
3332bfff6873SLukas Czerner 
3333bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
33347f511862STheodore Ts'o 	if (!elr) {
33357f511862STheodore Ts'o 		ret = -ENOMEM;
33367f511862STheodore Ts'o 		goto out;
33377f511862STheodore Ts'o 	}
3338bfff6873SLukas Czerner 
3339bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3340bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3341bfff6873SLukas Czerner 		if (ret)
3342bfff6873SLukas Czerner 			goto out;
3343bfff6873SLukas Czerner 	}
3344bfff6873SLukas Czerner 
3345bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3346bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3347bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3348bfff6873SLukas Czerner 
3349bfff6873SLukas Czerner 	sbi->s_li_request = elr;
335046e4690bSTao Ma 	/*
335146e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
335246e4690bSTao Ma 	 * the request_list and the removal and free of it is
335346e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
335446e4690bSTao Ma 	 */
335546e4690bSTao Ma 	elr = NULL;
3356bfff6873SLukas Czerner 
3357bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3358bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3359bfff6873SLukas Czerner 		if (ret)
3360bfff6873SLukas Czerner 			goto out;
3361bfff6873SLukas Czerner 	}
3362bfff6873SLukas Czerner out:
3363bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3364beed5ecbSNicolas Kaiser 	if (ret)
3365bfff6873SLukas Czerner 		kfree(elr);
3366bfff6873SLukas Czerner 	return ret;
3367bfff6873SLukas Czerner }
3368bfff6873SLukas Czerner 
3369bfff6873SLukas Czerner /*
3370bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3371bfff6873SLukas Czerner  * module unload.
3372bfff6873SLukas Czerner  */
3373bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3374bfff6873SLukas Czerner {
3375bfff6873SLukas Czerner 	/*
3376bfff6873SLukas Czerner 	 * If thread exited earlier
3377bfff6873SLukas Czerner 	 * there's nothing to be done.
3378bfff6873SLukas Czerner 	 */
33798f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3380bfff6873SLukas Czerner 		return;
3381bfff6873SLukas Czerner 
33828f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3383bfff6873SLukas Czerner }
3384bfff6873SLukas Czerner 
338525ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
338625ed6e8aSDarrick J. Wong {
338725ed6e8aSDarrick J. Wong 	int ret = 1;
338825ed6e8aSDarrick J. Wong 	int compat, incompat;
338925ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
339025ed6e8aSDarrick J. Wong 
33919aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3392db9ee220SDarrick J. Wong 		/* journal checksum v3 */
339325ed6e8aSDarrick J. Wong 		compat = 0;
3394db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
339525ed6e8aSDarrick J. Wong 	} else {
339625ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
339725ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
339825ed6e8aSDarrick J. Wong 		incompat = 0;
339925ed6e8aSDarrick J. Wong 	}
340025ed6e8aSDarrick J. Wong 
3401feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3402feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3403feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3404feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
340525ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
340625ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
340725ed6e8aSDarrick J. Wong 				compat, 0,
340825ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
340925ed6e8aSDarrick J. Wong 				incompat);
341025ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
341125ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
341225ed6e8aSDarrick J. Wong 				compat, 0,
341325ed6e8aSDarrick J. Wong 				incompat);
341425ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
341525ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
341625ed6e8aSDarrick J. Wong 	} else {
3417feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3418feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
341925ed6e8aSDarrick J. Wong 	}
342025ed6e8aSDarrick J. Wong 
342125ed6e8aSDarrick J. Wong 	return ret;
342225ed6e8aSDarrick J. Wong }
342325ed6e8aSDarrick J. Wong 
3424952fc18eSTheodore Ts'o /*
3425952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3426952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3427952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3428952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3429952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3430952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3431952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3432952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3433952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3434952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3435952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3436952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3437952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3438952fc18eSTheodore Ts'o  */
3439952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3440952fc18eSTheodore Ts'o 			  char *buf)
3441952fc18eSTheodore Ts'o {
3442952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3443952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3444952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3445952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3446952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3447952fc18eSTheodore Ts'o 
3448e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
34490548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
34500548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
34510548bbb8STheodore Ts'o 
3452952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3453952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3454952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3455952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3456952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3457952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3458952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3459952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3460952fc18eSTheodore Ts'o 			count++;
3461952fc18eSTheodore Ts'o 		}
3462952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3463952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3464952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3465952fc18eSTheodore Ts'o 			count++;
3466952fc18eSTheodore Ts'o 		}
3467952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3468952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3469952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3470952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3471952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3472952fc18eSTheodore Ts'o 				count++;
3473952fc18eSTheodore Ts'o 			}
3474952fc18eSTheodore Ts'o 		if (i != grp)
3475952fc18eSTheodore Ts'o 			continue;
3476952fc18eSTheodore Ts'o 		s = 0;
3477952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3478952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3479952fc18eSTheodore Ts'o 			count++;
3480952fc18eSTheodore Ts'o 		}
3481c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3482c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3483c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3484c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3485c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3486952fc18eSTheodore Ts'o 		}
3487c48ae41bSTheodore Ts'o 		count += j;
3488c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3489c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3490952fc18eSTheodore Ts'o 	}
3491952fc18eSTheodore Ts'o 	if (!count)
3492952fc18eSTheodore Ts'o 		return 0;
3493952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3494952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3495952fc18eSTheodore Ts'o }
3496952fc18eSTheodore Ts'o 
3497952fc18eSTheodore Ts'o /*
3498952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3499952fc18eSTheodore Ts'o  */
3500952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3501952fc18eSTheodore Ts'o {
3502952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3503952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
35043c816dedSEric Whitney 	struct inode *j_inode;
35053c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3506952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3507952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
35084fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3509952fc18eSTheodore Ts'o 
3510952fc18eSTheodore Ts'o 	if (!buf)
3511952fc18eSTheodore Ts'o 		return -ENOMEM;
3512952fc18eSTheodore Ts'o 
3513952fc18eSTheodore Ts'o 	/*
3514952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3515952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3516952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3517952fc18eSTheodore Ts'o 	 */
3518952fc18eSTheodore Ts'o 
3519952fc18eSTheodore Ts'o 	/*
3520952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3521952fc18eSTheodore Ts'o 	 */
3522952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3523952fc18eSTheodore Ts'o 
3524952fc18eSTheodore Ts'o 	/*
3525952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3526952fc18eSTheodore Ts'o 	 */
3527952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3528952fc18eSTheodore Ts'o 		int blks;
3529952fc18eSTheodore Ts'o 
3530952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3531952fc18eSTheodore Ts'o 		overhead += blks;
3532952fc18eSTheodore Ts'o 		if (blks)
3533952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3534952fc18eSTheodore Ts'o 		cond_resched();
3535952fc18eSTheodore Ts'o 	}
35363c816dedSEric Whitney 
35373c816dedSEric Whitney 	/*
35383c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
35393c816dedSEric Whitney 	 * loaded or not
35403c816dedSEric Whitney 	 */
3541b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3542810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
35433c816dedSEric Whitney 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
35443c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
35453c816dedSEric Whitney 		if (j_inode) {
35463c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
35473c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
35483c816dedSEric Whitney 			iput(j_inode);
35493c816dedSEric Whitney 		} else {
35503c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
35513c816dedSEric Whitney 		}
35523c816dedSEric Whitney 	}
3553952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3554952fc18eSTheodore Ts'o 	smp_wmb();
3555952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3556952fc18eSTheodore Ts'o 	return 0;
3557952fc18eSTheodore Ts'o }
3558952fc18eSTheodore Ts'o 
35597bc04c5cSBarret Rhoden static void ext4_clamp_want_extra_isize(struct super_block *sb)
35607bc04c5cSBarret Rhoden {
35617bc04c5cSBarret Rhoden 	struct ext4_sb_info *sbi = EXT4_SB(sb);
35627bc04c5cSBarret Rhoden 	struct ext4_super_block *es = sbi->s_es;
35637bc04c5cSBarret Rhoden 
35647bc04c5cSBarret Rhoden 	/* determine the minimum size of new large inodes, if present */
35657bc04c5cSBarret Rhoden 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE &&
35667bc04c5cSBarret Rhoden 	    sbi->s_want_extra_isize == 0) {
35677bc04c5cSBarret Rhoden 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
35687bc04c5cSBarret Rhoden 						     EXT4_GOOD_OLD_INODE_SIZE;
35697bc04c5cSBarret Rhoden 		if (ext4_has_feature_extra_isize(sb)) {
35707bc04c5cSBarret Rhoden 			if (sbi->s_want_extra_isize <
35717bc04c5cSBarret Rhoden 			    le16_to_cpu(es->s_want_extra_isize))
35727bc04c5cSBarret Rhoden 				sbi->s_want_extra_isize =
35737bc04c5cSBarret Rhoden 					le16_to_cpu(es->s_want_extra_isize);
35747bc04c5cSBarret Rhoden 			if (sbi->s_want_extra_isize <
35757bc04c5cSBarret Rhoden 			    le16_to_cpu(es->s_min_extra_isize))
35767bc04c5cSBarret Rhoden 				sbi->s_want_extra_isize =
35777bc04c5cSBarret Rhoden 					le16_to_cpu(es->s_min_extra_isize);
35787bc04c5cSBarret Rhoden 		}
35797bc04c5cSBarret Rhoden 	}
35807bc04c5cSBarret Rhoden 	/* Check if enough inode space is available */
35817bc04c5cSBarret Rhoden 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
35827bc04c5cSBarret Rhoden 							sbi->s_inode_size) {
35837bc04c5cSBarret Rhoden 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
35847bc04c5cSBarret Rhoden 						       EXT4_GOOD_OLD_INODE_SIZE;
35857bc04c5cSBarret Rhoden 		ext4_msg(sb, KERN_INFO,
35867bc04c5cSBarret Rhoden 			 "required extra inode space not available");
35877bc04c5cSBarret Rhoden 	}
35887bc04c5cSBarret Rhoden }
35897bc04c5cSBarret Rhoden 
3590b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
359127dd4385SLukas Czerner {
359227dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3593b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
359427dd4385SLukas Czerner 
359527dd4385SLukas Czerner 	/*
359630fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
359730fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
359830fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
359930fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
360030fac0f7SJan Kara 	 */
3601e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3602b5799018STheodore Ts'o 		return;
360330fac0f7SJan Kara 	/*
360427dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
360527dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
360627dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3607556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
360827dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
360927dd4385SLukas Czerner 	 * very rare.
361027dd4385SLukas Czerner 	 */
3611b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3612b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
361327dd4385SLukas Czerner 
361427dd4385SLukas Czerner 	do_div(resv_clusters, 50);
361527dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
361627dd4385SLukas Czerner 
3617b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
361827dd4385SLukas Czerner }
361927dd4385SLukas Czerner 
3620617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3621ac27a0ecSDave Kleikamp {
36225e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3623d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
3624ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
3625617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
36265aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3627617ba13bSMingming Cao 	ext4_fsblk_t block;
3628617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
362970bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3630ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3631ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3632ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3633ac27a0ecSDave Kleikamp 	struct inode *root;
36340390131bSFrank Mayhar 	const char *descr;
3635dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3636281b5995STheodore Ts'o 	int blocksize, clustersize;
36374ec11028STheodore Ts'o 	unsigned int db_count;
36384ec11028STheodore Ts'o 	unsigned int i;
3639281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3640bd81d8eeSLaurent Vivier 	__u64 blocks_count;
364107aa2ea1SLukas Czerner 	int err = 0;
3642b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3643bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3644ac27a0ecSDave Kleikamp 
36455aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
36465aee0f8aSTheodore Ts'o 		goto out_free_base;
3647705895b6SPekka Enberg 
3648aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3649705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3650705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
36515aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
36525aee0f8aSTheodore Ts'o 		goto out_free_base;
36535aee0f8aSTheodore Ts'o 
3654ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
36552c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3656240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3657d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3658f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3659f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3660dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3661ac27a0ecSDave Kleikamp 
36629f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3663ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
36649f6200bbSTheodore Ts'o 
366507aa2ea1SLukas Czerner 	/* -EINVAL is default */
3666dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3667617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3668ac27a0ecSDave Kleikamp 	if (!blocksize) {
3669b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3670ac27a0ecSDave Kleikamp 		goto out_fail;
3671ac27a0ecSDave Kleikamp 	}
3672ac27a0ecSDave Kleikamp 
3673ac27a0ecSDave Kleikamp 	/*
3674617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3675ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3676ac27a0ecSDave Kleikamp 	 */
3677617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
367870bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
367970bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3680ac27a0ecSDave Kleikamp 	} else {
368170bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3682ac27a0ecSDave Kleikamp 	}
3683ac27a0ecSDave Kleikamp 
3684a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3685b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3686ac27a0ecSDave Kleikamp 		goto out_fail;
3687ac27a0ecSDave Kleikamp 	}
3688ac27a0ecSDave Kleikamp 	/*
3689ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3690617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3691ac27a0ecSDave Kleikamp 	 */
36922716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3693ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3694ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3695617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3696617ba13bSMingming Cao 		goto cantfind_ext4;
3697afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3698ac27a0ecSDave Kleikamp 
3699feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3700e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3701e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3702363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3703feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3704feb0ab32SDarrick J. Wong 
3705d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3706d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3707d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3708d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3709d25425f8SDarrick J. Wong 		silent = 1;
3710d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3711d25425f8SDarrick J. Wong 	}
3712d25425f8SDarrick J. Wong 
37130441984aSDarrick J. Wong 	/* Load the checksum driver */
37140441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
37150441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
37160441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
37170441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
37180441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
37190441984aSDarrick J. Wong 		goto failed_mount;
37200441984aSDarrick J. Wong 	}
37210441984aSDarrick J. Wong 
3722a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3723a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3724a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3725a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3726a9c47317SDarrick J. Wong 		silent = 1;
37276a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3728a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3729a9c47317SDarrick J. Wong 	}
3730a9c47317SDarrick J. Wong 
3731a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3732e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
37338c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3734dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3735a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3736a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3737a9c47317SDarrick J. Wong 
3738ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3739ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3740fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3741617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3742fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
374387f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3744fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3745617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3746fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3747ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3748fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
374903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3750fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
37512e7842b8SHugh Dickins #endif
375298c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
375398c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
375498c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
375598c1a759SDarrick J. Wong 
3756617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3757fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3758617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3759fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3760617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3761fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3762ac27a0ecSDave Kleikamp 
3763617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3764fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3765bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3766fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3767bb4f397aSAneesh Kumar K.V 	else
3768fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
376945f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3770fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
37718b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3772fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3773ac27a0ecSDave Kleikamp 
377408cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
377508cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
377630773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
377730773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
377830773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3779ac27a0ecSDave Kleikamp 
37808b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3781fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3782ac27a0ecSDave Kleikamp 
37831e2462f9SMingming Cao 	/*
3784dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3785dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3786dd919b98SAneesh Kumar K.V 	 */
3787bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
37888b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3789fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3790dd919b98SAneesh Kumar K.V 
379151ce6511SLukas Czerner 	/*
379251ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
379351ce6511SLukas Czerner 	 * no mount option specified.
379451ce6511SLukas Czerner 	 */
379551ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
379651ce6511SLukas Czerner 
37975aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
37985aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
37995aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
38005aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
38015aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
38025aee0f8aSTheodore Ts'o 			goto failed_mount;
38035aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
38045aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
38058b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
38068b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
38075aee0f8aSTheodore Ts'o 				 s_mount_opts);
38085aee0f8aSTheodore Ts'o 		}
38095aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
38108b67f04aSTheodore Ts'o 	}
38115a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3812b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3813661aa520SEric Sandeen 			   &journal_ioprio, 0))
3814ac27a0ecSDave Kleikamp 		goto failed_mount;
3815ac27a0ecSDave Kleikamp 
3816c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3817c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
3818c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
3819c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
3820c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
3821c83ad55eSGabriel Krisman Bertazi 
3822c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
3823c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3824c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
3825c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3826c83ad55eSGabriel Krisman Bertazi 		}
3827c83ad55eSGabriel Krisman Bertazi 
3828c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
3829c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
3830c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3831c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
3832c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3833c83ad55eSGabriel Krisman Bertazi 		}
3834c83ad55eSGabriel Krisman Bertazi 
3835c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
3836c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
3837c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3838c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
3839c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
3840c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
3841c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
3842c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3843c83ad55eSGabriel Krisman Bertazi 		}
3844c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
3845c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
3846c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
3847c83ad55eSGabriel Krisman Bertazi 
3848c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
3849c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
3850c83ad55eSGabriel Krisman Bertazi 	}
3851c83ad55eSGabriel Krisman Bertazi #endif
3852c83ad55eSGabriel Krisman Bertazi 
385356889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
385456889787STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
385556889787STheodore Ts'o 			    "with data=journal disables delayed "
385656889787STheodore Ts'o 			    "allocation and O_DIRECT support!\n");
385756889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
385856889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
385956889787STheodore Ts'o 				 "both data=journal and delalloc");
386056889787STheodore Ts'o 			goto failed_mount;
386156889787STheodore Ts'o 		}
386256889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
386356889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
38646ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
386556889787STheodore Ts'o 			goto failed_mount;
386656889787STheodore Ts'o 		}
3867923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3868923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3869923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3870923ae0ffSRoss Zwisler 			goto failed_mount;
3871923ae0ffSRoss Zwisler 		}
387273b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
387373b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
387473b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
387573b92a2aSSergey Karamov 				 "instead of data journaling mode");
387673b92a2aSSergey Karamov 		}
387756889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
387856889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3879001e4a87STejun Heo 	} else {
3880001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
388156889787STheodore Ts'o 	}
388256889787STheodore Ts'o 
38831751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
38841751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
3885ac27a0ecSDave Kleikamp 
3886617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3887e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
3888e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
3889e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
3890b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
3891b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
3892b31e1552SEric Sandeen 		       "running e2fsck is recommended");
3893469108ffSTheodore Tso 
3894ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3895ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
3896e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
3897ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
3898ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
3899ed3654ebSTheodore Ts'o 			goto failed_mount;
3900ed3654ebSTheodore Ts'o 		}
3901dec214d0STahsin Erdogan 
3902dec214d0STahsin Erdogan 		/*
3903dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
3904dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
3905dec214d0STahsin Erdogan 		 */
3906dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
3907dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
3908dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
3909dec214d0STahsin Erdogan 			goto failed_mount;
3910dec214d0STahsin Erdogan 		}
3911ed3654ebSTheodore Ts'o 	}
3912ed3654ebSTheodore Ts'o 
39132035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
39142035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
39152035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
39162035e776STheodore Ts'o 				 "using the ext4 subsystem");
39172035e776STheodore Ts'o 		else {
39180d9366d6SEric Sandeen 			/*
39190d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
39200d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
39210d9366d6SEric Sandeen 			 */
39220d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
39230d9366d6SEric Sandeen 				goto failed_mount;
39242035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
39252035e776STheodore Ts'o 				 "to feature incompatibilities");
39262035e776STheodore Ts'o 			goto failed_mount;
39272035e776STheodore Ts'o 		}
39282035e776STheodore Ts'o 	}
39292035e776STheodore Ts'o 
39302035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
39312035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
39322035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
39332035e776STheodore Ts'o 				 "using the ext4 subsystem");
39342035e776STheodore Ts'o 		else {
39350d9366d6SEric Sandeen 			/*
39360d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
39370d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
39380d9366d6SEric Sandeen 			 */
39390d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
39400d9366d6SEric Sandeen 				goto failed_mount;
39412035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
39422035e776STheodore Ts'o 				 "to feature incompatibilities");
39432035e776STheodore Ts'o 			goto failed_mount;
39442035e776STheodore Ts'o 		}
39452035e776STheodore Ts'o 	}
39462035e776STheodore Ts'o 
3947469108ffSTheodore Tso 	/*
3948ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
3949ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
3950ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3951ac27a0ecSDave Kleikamp 	 */
3952bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
3953ac27a0ecSDave Kleikamp 		goto failed_mount;
3954a13fb1a4SEric Sandeen 
3955261cb20cSJan Kara 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3956617ba13bSMingming Cao 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3957617ba13bSMingming Cao 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3958b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
39598cdf3372STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
39608cdf3372STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
39618cdf3372STheodore Ts'o 		goto failed_mount;
39628cdf3372STheodore Ts'o 	}
39638cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
39648cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
39658cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39668cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
39678cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
3968ac27a0ecSDave Kleikamp 		goto failed_mount;
3969ac27a0ecSDave Kleikamp 	}
3970bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
3971bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
3972bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3973bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
3974bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
3975bfe0a5f4STheodore Ts'o 		goto failed_mount;
3976bfe0a5f4STheodore Ts'o 	}
3977ac27a0ecSDave Kleikamp 
39785b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
39795b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39805b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
39815b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
39825b9554dcSTheodore Ts'o 		goto failed_mount;
39835b9554dcSTheodore Ts'o 	}
39845b9554dcSTheodore Ts'o 
3985923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
3986559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
3987559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
3988559db4c6SRoss Zwisler 					" that may contain inline data");
3989361d24d4SEric Sandeen 			goto failed_mount;
3990559db4c6SRoss Zwisler 		}
399180660f20SDave Jiang 		if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
399224f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
3993361d24d4SEric Sandeen 				"DAX unsupported by block device.");
3994361d24d4SEric Sandeen 			goto failed_mount;
399524f3478dSDan Williams 		}
3996923ae0ffSRoss Zwisler 	}
3997923ae0ffSRoss Zwisler 
3998e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
39996ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
40006ddb2447STheodore Ts'o 			 es->s_encryption_level);
40016ddb2447STheodore Ts'o 		goto failed_mount;
40026ddb2447STheodore Ts'o 	}
40036ddb2447STheodore Ts'o 
4004ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4005ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4006ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4007b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4008ce40733cSAneesh Kumar K.V 					blocksize);
4009ac27a0ecSDave Kleikamp 			goto failed_mount;
4010ac27a0ecSDave Kleikamp 		}
4011ac27a0ecSDave Kleikamp 
4012ac27a0ecSDave Kleikamp 		brelse(bh);
401370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
401470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4015a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4016ac27a0ecSDave Kleikamp 		if (!bh) {
4017b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4018b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4019ac27a0ecSDave Kleikamp 			goto failed_mount;
4020ac27a0ecSDave Kleikamp 		}
40212716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4022ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4023617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4024b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4025b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4026ac27a0ecSDave Kleikamp 			goto failed_mount;
4027ac27a0ecSDave Kleikamp 		}
4028ac27a0ecSDave Kleikamp 	}
4029ac27a0ecSDave Kleikamp 
4030e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4031f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4032f287a1a5STheodore Ts'o 						      has_huge_files);
4033f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4034ac27a0ecSDave Kleikamp 
4035617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4036617ba13bSMingming Cao 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4037617ba13bSMingming Cao 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4038ac27a0ecSDave Kleikamp 	} else {
4039ac27a0ecSDave Kleikamp 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4040ac27a0ecSDave Kleikamp 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4041c37e9e01STheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4042c37e9e01STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4043c37e9e01STheodore Ts'o 				 sbi->s_first_ino);
4044c37e9e01STheodore Ts'o 			goto failed_mount;
4045c37e9e01STheodore Ts'o 		}
4046617ba13bSMingming Cao 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
40471330593eSVignesh Babu 		    (!is_power_of_2(sbi->s_inode_size)) ||
4048ac27a0ecSDave Kleikamp 		    (sbi->s_inode_size > blocksize)) {
4049b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4050b31e1552SEric Sandeen 			       "unsupported inode size: %d",
4051ac27a0ecSDave Kleikamp 			       sbi->s_inode_size);
4052ac27a0ecSDave Kleikamp 			goto failed_mount;
4053ac27a0ecSDave Kleikamp 		}
4054ef7f3835SKalpak Shah 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
4055ef7f3835SKalpak Shah 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
4056ac27a0ecSDave Kleikamp 	}
40570b8e58a1SAndreas Dilger 
40580d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4059e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
40608fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
40610d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4062d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4063b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4064b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
40650d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
40660d1ee42fSAlexandre Ratchov 			goto failed_mount;
40670d1ee42fSAlexandre Ratchov 		}
40680d1ee42fSAlexandre Ratchov 	} else
40690d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
40700b8e58a1SAndreas Dilger 
4071ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4072ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
40730b8e58a1SAndreas Dilger 
4074617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4075ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4076617ba13bSMingming Cao 		goto cantfind_ext4;
4077cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4078cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4079cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4080cd6bb35bSTheodore Ts'o 			 sbi->s_blocks_per_group);
4081cd6bb35bSTheodore Ts'o 		goto failed_mount;
4082cd6bb35bSTheodore Ts'o 	}
4083ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4084ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
40850d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4086ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4087ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4088e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4089e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
40900b8e58a1SAndreas Dilger 
4091ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4092ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4093ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4094e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4095f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4096f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4097f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4098f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4099f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4100bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
410123301410STheodore Ts'o 				es->s_flags |=
410223301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4103f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4104f99b2589STheodore Ts'o #else
4105bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
410623301410STheodore Ts'o 				es->s_flags |=
410723301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4108f99b2589STheodore Ts'o #endif
4109f99b2589STheodore Ts'o 		}
411023301410STheodore Ts'o 	}
4111ac27a0ecSDave Kleikamp 
4112281b5995STheodore Ts'o 	/* Handle clustersize */
4113281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4114e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
4115281b5995STheodore Ts'o 	if (has_bigalloc) {
4116281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4117281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4118281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4119281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4120281b5995STheodore Ts'o 			goto failed_mount;
4121281b5995STheodore Ts'o 		}
4122281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4123281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4124281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4125281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4126281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4127281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4128281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4129281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4130281b5995STheodore Ts'o 			goto failed_mount;
4131281b5995STheodore Ts'o 		}
4132281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4133281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4134281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4135281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4136281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4137281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4138281b5995STheodore Ts'o 			goto failed_mount;
4139281b5995STheodore Ts'o 		}
4140281b5995STheodore Ts'o 	} else {
4141281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4142bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4143bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4144bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4145bfe0a5f4STheodore Ts'o 			goto failed_mount;
4146281b5995STheodore Ts'o 		}
4147ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4148b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4149b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4150ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4151ac27a0ecSDave Kleikamp 			goto failed_mount;
4152ac27a0ecSDave Kleikamp 		}
4153281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4154281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4155281b5995STheodore Ts'o 	}
4156281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4157281b5995STheodore Ts'o 
4158960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4159960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4160960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4161960fd856STheodore Ts'o 
4162bf43d84bSEric Sandeen 	/*
4163bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4164bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4165bf43d84bSEric Sandeen 	 */
41665a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
416730ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
41685a9ae68aSDarrick J. Wong 	if (err) {
4169b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4170bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4171ac27a0ecSDave Kleikamp 		if (sizeof(sector_t) < 8)
417290c699a9SBartlomiej Zolnierkiewicz 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
4173ac27a0ecSDave Kleikamp 		goto failed_mount;
4174ac27a0ecSDave Kleikamp 	}
4175ac27a0ecSDave Kleikamp 
4176617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4177617ba13bSMingming Cao 		goto cantfind_ext4;
4178e7c95593SEric Sandeen 
41790f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
41800f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
41810f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4182b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4183b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
41840f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
41850f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
41860f2ddca6SFrom: Thiemo Nagel 	}
41870f2ddca6SFrom: Thiemo Nagel 
41884ec11028STheodore Ts'o 	/*
41894ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
41904ec11028STheodore Ts'o 	 * of the filesystem.
41914ec11028STheodore Ts'o 	 */
41924ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4193b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4194b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4195e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
41964ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4197e7c95593SEric Sandeen 		goto failed_mount;
4198e7c95593SEric Sandeen 	}
4199bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4200bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4201bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4202bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4203bfe0a5f4STheodore Ts'o 		goto failed_mount;
4204bfe0a5f4STheodore Ts'o 	}
4205bfe0a5f4STheodore Ts'o 
4206bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4207bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4208bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4209bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
42104ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4211b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
42124ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4213b31e1552SEric Sandeen 		       "blocks per group %lu)", sbi->s_groups_count,
42144ec11028STheodore Ts'o 		       ext4_blocks_count(es),
42154ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
42164ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
42174ec11028STheodore Ts'o 		goto failed_mount;
42184ec11028STheodore Ts'o 	}
4219bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4220fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4221fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
42229e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
42239e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
42249e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
42259e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
42269e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
42279e463084STheodore Ts'o 		ret = -EINVAL;
42289e463084STheodore Ts'o 		goto failed_mount;
42299e463084STheodore Ts'o 	}
4230617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4231617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
42323a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
42332ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
42343a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
42353a4b77cdSEryu Guan 				 "first meta block group too large: %u "
42363a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
42373a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
42383a4b77cdSEryu Guan 			goto failed_mount;
42393a4b77cdSEryu Guan 		}
42403a4b77cdSEryu Guan 	}
4241344476e1SKees Cook 	sbi->s_group_desc = kvmalloc_array(db_count,
4242f18a5f21STheodore Ts'o 					   sizeof(struct buffer_head *),
4243ac27a0ecSDave Kleikamp 					   GFP_KERNEL);
4244ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4245b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
42462cde417dSTheodore Ts'o 		ret = -ENOMEM;
4247ac27a0ecSDave Kleikamp 		goto failed_mount;
4248ac27a0ecSDave Kleikamp 	}
4249ac27a0ecSDave Kleikamp 
4250705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4251ac27a0ecSDave Kleikamp 
425285c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
425385c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
425485c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
425585c8f176SAndrew Perepechko 		sb_breadahead(sb, block);
425685c8f176SAndrew Perepechko 	}
425785c8f176SAndrew Perepechko 
4258ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
425970bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
4260a8ac900bSGioh Kim 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
4261ac27a0ecSDave Kleikamp 		if (!sbi->s_group_desc[i]) {
4262b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4263b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4264ac27a0ecSDave Kleikamp 			db_count = i;
4265ac27a0ecSDave Kleikamp 			goto failed_mount2;
4266ac27a0ecSDave Kleikamp 		}
4267ac27a0ecSDave Kleikamp 	}
426844de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4269829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4270b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
42716a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4272f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4273ac27a0ecSDave Kleikamp 	}
4274772cb7c8SJose R. Santos 
4275235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
427604496411STao Ma 
4277a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4278eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4279ce7e010aSTheodore Ts'o 		goto failed_mount3;
4280ce7e010aSTheodore Ts'o 
4281c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
428267a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4283c9de560dSAlex Tomas 
4284f9ae9cf5STheodore Ts'o 	/*
4285f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4286f9ae9cf5STheodore Ts'o 	 */
4287f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4288617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4289617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4290643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4291a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4292ffcc4182SEric Biggers #endif
4293ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4294617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4295e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
42961fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4297262b4662SJan Kara 	else
4298262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4299689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4300ac27a0ecSDave Kleikamp #endif
430185787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4302f2fa2ffcSAneesh Kumar K.V 
4303ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
43043b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4305ac27a0ecSDave Kleikamp 
4306ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4307ac27a0ecSDave Kleikamp 
4308ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4309e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4310ac27a0ecSDave Kleikamp 
4311bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4312c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
431350460fe8SDarrick J. Wong 			goto failed_mount3a;
4314c5e06d10SJohann Lombardi 
4315ac27a0ecSDave Kleikamp 	/*
4316ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4317ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4318ac27a0ecSDave Kleikamp 	 */
4319e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
43204753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
43214753d8a2STheodore Ts'o 		if (err)
432250460fe8SDarrick J. Wong 			goto failed_mount3a;
4323bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4324e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4325b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4326b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4327744692dcSJiaying Zhang 		goto failed_mount_wq;
4328ac27a0ecSDave Kleikamp 	} else {
43291e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
43301e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
43311e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43321e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
43331e381f60SDmitry Monakhov 			goto failed_mount_wq;
43341e381f60SDmitry Monakhov 		}
43351e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
43361e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43371e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
43381e381f60SDmitry Monakhov 			goto failed_mount_wq;
43391e381f60SDmitry Monakhov 		}
43401e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
43411e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43421e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
43431e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
43441e381f60SDmitry Monakhov 			goto failed_mount_wq;
43451e381f60SDmitry Monakhov 		}
43461e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
43471e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
43481e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
43491e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
43501e381f60SDmitry Monakhov 			goto failed_mount_wq;
43511e381f60SDmitry Monakhov 		}
435250b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
43531e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4354fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
43550390131bSFrank Mayhar 		sbi->s_journal = NULL;
43560390131bSFrank Mayhar 		needs_recovery = 0;
43570390131bSFrank Mayhar 		goto no_journal;
4358ac27a0ecSDave Kleikamp 	}
4359ac27a0ecSDave Kleikamp 
4360e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4361eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4362eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4363b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4364744692dcSJiaying Zhang 		goto failed_mount_wq;
4365eb40a09cSJose R. Santos 	}
4366eb40a09cSJose R. Santos 
436725ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
436825ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
436925ed6e8aSDarrick J. Wong 			 "feature set");
437025ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4371d4da6c9cSLinus Torvalds 	}
4372818d276cSGirish Shilamkar 
4373ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4374ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4375ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4376ac27a0ecSDave Kleikamp 	case 0:
4377ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
437863f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
437963f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
438063f57933SAndrew Morton 		 */
4381dab291afSMingming Cao 		if (jbd2_journal_check_available_features
438227f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4383fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
438427f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
438527f394a7STyson Nottingham 		} else {
4386fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
438727f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
438827f394a7STyson Nottingham 		}
4389ac27a0ecSDave Kleikamp 		break;
4390ac27a0ecSDave Kleikamp 
4391617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4392617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4393dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4394dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4395b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4396b31e1552SEric Sandeen 			       "requested data journaling mode");
4397744692dcSJiaying Zhang 			goto failed_mount_wq;
4398ac27a0ecSDave Kleikamp 		}
4399ac27a0ecSDave Kleikamp 	default:
4400ac27a0ecSDave Kleikamp 		break;
4401ac27a0ecSDave Kleikamp 	}
4402ab04df78SJan Kara 
4403ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4404ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4405ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4406ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4407ab04df78SJan Kara 		goto failed_mount_wq;
4408ab04df78SJan Kara 	}
4409ab04df78SJan Kara 
4410b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4411ac27a0ecSDave Kleikamp 
441218aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
441318aadd47SBobi Jam 
4414ce7e010aSTheodore Ts'o no_journal:
4415cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
441647387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
441747387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4418cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4419cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
44209c191f70ST Makphaibulchoke 			goto failed_mount_wq;
44219c191f70ST Makphaibulchoke 		}
44229c191f70ST Makphaibulchoke 
4423dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4424dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4425dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4426dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4427dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4428dec214d0STahsin Erdogan 				goto failed_mount_wq;
4429dec214d0STahsin Erdogan 			}
4430dec214d0STahsin Erdogan 		}
4431cdb7ee4cSTahsin Erdogan 	}
4432dec214d0STahsin Erdogan 
4433e2b911c5SDarrick J. Wong 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
443409cbfeafSKirill A. Shutemov 	    (blocksize != PAGE_SIZE)) {
44351cb767cdSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
44361cb767cdSTheodore Ts'o 			 "Unsupported blocksize for fs encryption");
44371cb767cdSTheodore Ts'o 		goto failed_mount_wq;
44381cb767cdSTheodore Ts'o 	}
44391cb767cdSTheodore Ts'o 
4440bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4441e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4442e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
44436ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
44446ddb2447STheodore Ts'o 	}
44456ddb2447STheodore Ts'o 
4446fd89d5f2STejun Heo 	/*
4447952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4448952fc18eSTheodore Ts'o 	 * superblock if present.
4449952fc18eSTheodore Ts'o 	 */
4450952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4451952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4452952fc18eSTheodore Ts'o 	else {
445307aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
445407aa2ea1SLukas Czerner 		if (err)
4455952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4456952fc18eSTheodore Ts'o 	}
4457952fc18eSTheodore Ts'o 
4458952fc18eSTheodore Ts'o 	/*
4459fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4460fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4461fd89d5f2STejun Heo 	 */
44622e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
44632e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
44642e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
44652e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
446607aa2ea1SLukas Czerner 		ret = -ENOMEM;
44672e8fa54eSJan Kara 		goto failed_mount4;
44682e8fa54eSJan Kara 	}
44692e8fa54eSJan Kara 
4470ac27a0ecSDave Kleikamp 	/*
4471dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4472ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4473ac27a0ecSDave Kleikamp 	 */
4474ac27a0ecSDave Kleikamp 
44758a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
44761d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4477b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
44781d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
447932a9bb57SManish Katiyar 		root = NULL;
4480ac27a0ecSDave Kleikamp 		goto failed_mount4;
4481ac27a0ecSDave Kleikamp 	}
4482ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4483b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
448494bf608aSAl Viro 		iput(root);
4485ac27a0ecSDave Kleikamp 		goto failed_mount4;
4486ac27a0ecSDave Kleikamp 	}
4487b886ee3eSGabriel Krisman Bertazi 
4488b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4489b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4490b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4491b886ee3eSGabriel Krisman Bertazi #endif
4492b886ee3eSGabriel Krisman Bertazi 
449348fde701SAl Viro 	sb->s_root = d_make_root(root);
44941d1fe1eeSDavid Howells 	if (!sb->s_root) {
4495b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
44961d1fe1eeSDavid Howells 		ret = -ENOMEM;
44971d1fe1eeSDavid Howells 		goto failed_mount4;
44981d1fe1eeSDavid Howells 	}
4499ac27a0ecSDave Kleikamp 
4500c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4501c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
45021751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4503c89128a0SJaegeuk Kim 		ret = 0;
4504c89128a0SJaegeuk Kim 	} else if (ret)
4505c89128a0SJaegeuk Kim 		goto failed_mount4a;
4506ef7f3835SKalpak Shah 
45077bc04c5cSBarret Rhoden 	ext4_clamp_want_extra_isize(sb);
4508ef7f3835SKalpak Shah 
4509b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
451027dd4385SLukas Czerner 
45116fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
45126fd058f7STheodore Ts'o 	if (err) {
4513b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4514fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4515f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4516f9ae9cf5STheodore Ts'o 	}
4517f9ae9cf5STheodore Ts'o 
4518f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4519f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4520f9ae9cf5STheodore Ts'o 	if (err) {
4521f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4522f9ae9cf5STheodore Ts'o 			 err);
4523dcf2d804STao Ma 		goto failed_mount5;
4524c2774d84SAneesh Kumar K.V 	}
4525c2774d84SAneesh Kumar K.V 
4526d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4527d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4528d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
45294274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4530908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4531908c7f19STejun Heo 				  GFP_KERNEL);
4532d5e03cbbSTheodore Ts'o 	if (!err) {
4533d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4534d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
45354274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4536908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4537908c7f19STejun Heo 					  GFP_KERNEL);
4538d5e03cbbSTheodore Ts'o 	}
4539d5e03cbbSTheodore Ts'o 	if (!err)
4540d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4541908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4542d5e03cbbSTheodore Ts'o 	if (!err)
4543908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4544908c7f19STejun Heo 					  GFP_KERNEL);
4545c8585c6fSDaeho Jeong 	if (!err)
4546c8585c6fSDaeho Jeong 		err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
4547c8585c6fSDaeho Jeong 
4548d5e03cbbSTheodore Ts'o 	if (err) {
4549d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4550d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4551d5e03cbbSTheodore Ts'o 	}
4552d5e03cbbSTheodore Ts'o 
4553e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4554d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4555d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4556d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4557d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4558d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4559d5e03cbbSTheodore Ts'o 		}
4560d5e03cbbSTheodore Ts'o 
4561bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4562bfff6873SLukas Czerner 	if (err)
4563dcf2d804STao Ma 		goto failed_mount6;
4564bfff6873SLukas Czerner 
4565b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4566dcf2d804STao Ma 	if (err)
4567dcf2d804STao Ma 		goto failed_mount7;
45683197ebdbSTheodore Ts'o 
45699b2ff357SJan Kara #ifdef CONFIG_QUOTA
45709b2ff357SJan Kara 	/* Enable quota usage during mount. */
4571bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
45729b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
45739b2ff357SJan Kara 		if (err)
45749b2ff357SJan Kara 			goto failed_mount8;
45759b2ff357SJan Kara 	}
45769b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
45779b2ff357SJan Kara 
4578617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4579617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4580617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
45810390131bSFrank Mayhar 	if (needs_recovery) {
4582b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4583617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
45840390131bSFrank Mayhar 	}
45850390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
45860390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
45870390131bSFrank Mayhar 			descr = " journalled data mode";
45880390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
45890390131bSFrank Mayhar 			descr = " ordered data mode";
45900390131bSFrank Mayhar 		else
45910390131bSFrank Mayhar 			descr = " writeback data mode";
45920390131bSFrank Mayhar 	} else
45930390131bSFrank Mayhar 		descr = "out journal";
45940390131bSFrank Mayhar 
459579add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
459679add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
459779add3a3SLukas Czerner 		if (!blk_queue_discard(q))
459879add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
459979add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
460079add3a3SLukas Czerner 				 "the device does not support discard");
460179add3a3SLukas Czerner 	}
460279add3a3SLukas Czerner 
4603e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4604d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
46055aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
46065aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
46075aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
46088b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4609ac27a0ecSDave Kleikamp 
461066e61a9eSTheodore Ts'o 	if (es->s_error_count)
461166e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4612ac27a0ecSDave Kleikamp 
4613efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4614efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4615efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4616efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4617efbed4dcSTheodore Ts'o 
4618d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4619ac27a0ecSDave Kleikamp 	return 0;
4620ac27a0ecSDave Kleikamp 
4621617ba13bSMingming Cao cantfind_ext4:
4622ac27a0ecSDave Kleikamp 	if (!silent)
4623b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4624ac27a0ecSDave Kleikamp 	goto failed_mount;
4625ac27a0ecSDave Kleikamp 
462672ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
462772ba7450STheodore Ts'o failed_mount8:
4628ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
462972ba7450STheodore Ts'o #endif
4630dcf2d804STao Ma failed_mount7:
4631dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4632dcf2d804STao Ma failed_mount6:
4633f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
4634d5e03cbbSTheodore Ts'o 	if (sbi->s_flex_groups)
4635b93b41d4SAl Viro 		kvfree(sbi->s_flex_groups);
4636d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4637d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4638d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4639d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4640af18e35bSVasily Averin 	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
464100764937SAzat Khuzhin failed_mount5:
4642f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4643f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4644f9ae9cf5STheodore Ts'o failed_mount4a:
464594bf608aSAl Viro 	dput(sb->s_root);
464632a9bb57SManish Katiyar 	sb->s_root = NULL;
464794bf608aSAl Viro failed_mount4:
4648b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
46492e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
46502e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
46514c0425ffSMingming Cao failed_mount_wq:
4652dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4653dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
465450c15df6SChengguang Xu 
465547387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
465647387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
465750c15df6SChengguang Xu 
46580390131bSFrank Mayhar 	if (sbi->s_journal) {
4659dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
466047b4a50bSJan Kara 		sbi->s_journal = NULL;
46610390131bSFrank Mayhar 	}
466250460fe8SDarrick J. Wong failed_mount3a:
4663d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4664eb68d0e2SZheng Liu failed_mount3:
46659105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4666c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4667c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4668ac27a0ecSDave Kleikamp failed_mount2:
4669ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
4670ac27a0ecSDave Kleikamp 		brelse(sbi->s_group_desc[i]);
4671b93b41d4SAl Viro 	kvfree(sbi->s_group_desc);
4672ac27a0ecSDave Kleikamp failed_mount:
46730441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
46740441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4675c83ad55eSGabriel Krisman Bertazi 
4676c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4677c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4678c83ad55eSGabriel Krisman Bertazi #endif
4679c83ad55eSGabriel Krisman Bertazi 
4680ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4681a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
46820ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4683ac27a0ecSDave Kleikamp #endif
4684617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4685ac27a0ecSDave Kleikamp 	brelse(bh);
4686ac27a0ecSDave Kleikamp out_fail:
4687ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4688f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
46895aee0f8aSTheodore Ts'o out_free_base:
4690ac27a0ecSDave Kleikamp 	kfree(sbi);
4691d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
46925e405595SDan Williams 	fs_put_dax(dax_dev);
469307aa2ea1SLukas Czerner 	return err ? err : ret;
4694ac27a0ecSDave Kleikamp }
4695ac27a0ecSDave Kleikamp 
4696ac27a0ecSDave Kleikamp /*
4697ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4698ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4699ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4700ac27a0ecSDave Kleikamp  */
4701617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4702ac27a0ecSDave Kleikamp {
4703617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4704ac27a0ecSDave Kleikamp 
4705ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
470630773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
470730773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4708ac27a0ecSDave Kleikamp 
4709a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4710ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4711dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4712ac27a0ecSDave Kleikamp 	else
4713dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
47145bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
47155bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
47165bf5683aSHidehiro Kawai 	else
47175bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4718a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4719ac27a0ecSDave Kleikamp }
4720ac27a0ecSDave Kleikamp 
4721c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4722ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4723ac27a0ecSDave Kleikamp {
4724ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4725ac27a0ecSDave Kleikamp 
4726c6cb7e77SEric Whitney 	/*
4727c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4728c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4729c6cb7e77SEric Whitney 	 * will try to delete it.
4730c6cb7e77SEric Whitney 	 */
47318a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
47321d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4733b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4734ac27a0ecSDave Kleikamp 		return NULL;
4735ac27a0ecSDave Kleikamp 	}
4736ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4737ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4738ac27a0ecSDave Kleikamp 		iput(journal_inode);
4739b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4740ac27a0ecSDave Kleikamp 		return NULL;
4741ac27a0ecSDave Kleikamp 	}
4742ac27a0ecSDave Kleikamp 
4743e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4744ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
47451d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4746b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4747ac27a0ecSDave Kleikamp 		iput(journal_inode);
4748ac27a0ecSDave Kleikamp 		return NULL;
4749ac27a0ecSDave Kleikamp 	}
4750c6cb7e77SEric Whitney 	return journal_inode;
4751c6cb7e77SEric Whitney }
4752c6cb7e77SEric Whitney 
4753c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4754c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4755c6cb7e77SEric Whitney {
4756c6cb7e77SEric Whitney 	struct inode *journal_inode;
4757c6cb7e77SEric Whitney 	journal_t *journal;
4758c6cb7e77SEric Whitney 
4759c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4760c6cb7e77SEric Whitney 
4761c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4762c6cb7e77SEric Whitney 	if (!journal_inode)
4763c6cb7e77SEric Whitney 		return NULL;
4764ac27a0ecSDave Kleikamp 
4765dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4766ac27a0ecSDave Kleikamp 	if (!journal) {
4767b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4768ac27a0ecSDave Kleikamp 		iput(journal_inode);
4769ac27a0ecSDave Kleikamp 		return NULL;
4770ac27a0ecSDave Kleikamp 	}
4771ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4772617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4773ac27a0ecSDave Kleikamp 	return journal;
4774ac27a0ecSDave Kleikamp }
4775ac27a0ecSDave Kleikamp 
4776617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4777ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4778ac27a0ecSDave Kleikamp {
4779ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4780ac27a0ecSDave Kleikamp 	journal_t *journal;
4781617ba13bSMingming Cao 	ext4_fsblk_t start;
4782617ba13bSMingming Cao 	ext4_fsblk_t len;
4783ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4784617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4785ac27a0ecSDave Kleikamp 	unsigned long offset;
4786617ba13bSMingming Cao 	struct ext4_super_block *es;
4787ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4788ac27a0ecSDave Kleikamp 
4789e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
47900390131bSFrank Mayhar 
4791b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4792ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4793ac27a0ecSDave Kleikamp 		return NULL;
4794ac27a0ecSDave Kleikamp 
4795ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4796e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4797ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4798b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4799b31e1552SEric Sandeen 			"blocksize too small for journal device");
4800ac27a0ecSDave Kleikamp 		goto out_bdev;
4801ac27a0ecSDave Kleikamp 	}
4802ac27a0ecSDave Kleikamp 
4803617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4804617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4805ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4806ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4807b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4808b31e1552SEric Sandeen 		       "external journal");
4809ac27a0ecSDave Kleikamp 		goto out_bdev;
4810ac27a0ecSDave Kleikamp 	}
4811ac27a0ecSDave Kleikamp 
48122716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4813617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4814ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4815617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4816b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4817b31e1552SEric Sandeen 					"bad superblock");
4818ac27a0ecSDave Kleikamp 		brelse(bh);
4819ac27a0ecSDave Kleikamp 		goto out_bdev;
4820ac27a0ecSDave Kleikamp 	}
4821ac27a0ecSDave Kleikamp 
4822df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4823df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4824df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4825df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4826df4763beSDarrick J. Wong 				       "corrupt superblock");
4827df4763beSDarrick J. Wong 		brelse(bh);
4828df4763beSDarrick J. Wong 		goto out_bdev;
4829df4763beSDarrick J. Wong 	}
4830df4763beSDarrick J. Wong 
4831617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4832b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4833ac27a0ecSDave Kleikamp 		brelse(bh);
4834ac27a0ecSDave Kleikamp 		goto out_bdev;
4835ac27a0ecSDave Kleikamp 	}
4836ac27a0ecSDave Kleikamp 
4837bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4838ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4839ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4840ac27a0ecSDave Kleikamp 
4841dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4842ac27a0ecSDave Kleikamp 					start, len, blocksize);
4843ac27a0ecSDave Kleikamp 	if (!journal) {
4844b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4845ac27a0ecSDave Kleikamp 		goto out_bdev;
4846ac27a0ecSDave Kleikamp 	}
4847ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4848dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4849ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4850ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4851b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4852ac27a0ecSDave Kleikamp 		goto out_journal;
4853ac27a0ecSDave Kleikamp 	}
4854ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4855b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4856b31e1552SEric Sandeen 					"user (unsupported) - %d",
4857ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4858ac27a0ecSDave Kleikamp 		goto out_journal;
4859ac27a0ecSDave Kleikamp 	}
4860617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4861617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4862ac27a0ecSDave Kleikamp 	return journal;
48630b8e58a1SAndreas Dilger 
4864ac27a0ecSDave Kleikamp out_journal:
4865dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4866ac27a0ecSDave Kleikamp out_bdev:
4867617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4868ac27a0ecSDave Kleikamp 	return NULL;
4869ac27a0ecSDave Kleikamp }
4870ac27a0ecSDave Kleikamp 
4871617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4872617ba13bSMingming Cao 			     struct ext4_super_block *es,
4873ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4874ac27a0ecSDave Kleikamp {
4875ac27a0ecSDave Kleikamp 	journal_t *journal;
4876ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4877ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4878ac27a0ecSDave Kleikamp 	int err = 0;
4879ac27a0ecSDave Kleikamp 	int really_read_only;
4880ac27a0ecSDave Kleikamp 
4881e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
48820390131bSFrank Mayhar 
4883ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4884ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4885b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4886b31e1552SEric Sandeen 			"numbers have changed");
4887ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4888ac27a0ecSDave Kleikamp 	} else
4889ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4890ac27a0ecSDave Kleikamp 
4891ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4892ac27a0ecSDave Kleikamp 
4893ac27a0ecSDave Kleikamp 	/*
4894ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4895ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4896ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4897ac27a0ecSDave Kleikamp 	 */
4898e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4899bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
4900b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4901b31e1552SEric Sandeen 					"required on readonly filesystem");
4902ac27a0ecSDave Kleikamp 			if (really_read_only) {
4903b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4904d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
4905d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
4906ac27a0ecSDave Kleikamp 				return -EROFS;
4907ac27a0ecSDave Kleikamp 			}
4908b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
4909b31e1552SEric Sandeen 			       "be enabled during recovery");
4910ac27a0ecSDave Kleikamp 		}
4911ac27a0ecSDave Kleikamp 	}
4912ac27a0ecSDave Kleikamp 
4913ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
4914b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4915b31e1552SEric Sandeen 		       "and inode journals!");
4916ac27a0ecSDave Kleikamp 		return -EINVAL;
4917ac27a0ecSDave Kleikamp 	}
4918ac27a0ecSDave Kleikamp 
4919ac27a0ecSDave Kleikamp 	if (journal_inum) {
4920617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4921ac27a0ecSDave Kleikamp 			return -EINVAL;
4922ac27a0ecSDave Kleikamp 	} else {
4923617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4924ac27a0ecSDave Kleikamp 			return -EINVAL;
4925ac27a0ecSDave Kleikamp 	}
4926ac27a0ecSDave Kleikamp 
492790576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
4928b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
49294776004fSTheodore Ts'o 
4930e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
4931dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
49321c13d5c0STheodore Ts'o 	if (!err) {
49331c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
49341c13d5c0STheodore Ts'o 		if (save)
49351c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
49361c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4937dab291afSMingming Cao 		err = jbd2_journal_load(journal);
49381c13d5c0STheodore Ts'o 		if (save)
49391c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
49401c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
49411c13d5c0STheodore Ts'o 		kfree(save);
49421c13d5c0STheodore Ts'o 	}
4943ac27a0ecSDave Kleikamp 
4944ac27a0ecSDave Kleikamp 	if (err) {
4945b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
4946dab291afSMingming Cao 		jbd2_journal_destroy(journal);
4947ac27a0ecSDave Kleikamp 		return err;
4948ac27a0ecSDave Kleikamp 	}
4949ac27a0ecSDave Kleikamp 
4950617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
4951617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
4952ac27a0ecSDave Kleikamp 
4953c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
4954ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4955ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4956ac27a0ecSDave Kleikamp 
4957ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
4958e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
4959ac27a0ecSDave Kleikamp 	}
4960ac27a0ecSDave Kleikamp 
4961ac27a0ecSDave Kleikamp 	return 0;
4962ac27a0ecSDave Kleikamp }
4963ac27a0ecSDave Kleikamp 
4964e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
4965ac27a0ecSDave Kleikamp {
4966e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4967617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4968c4be0c1dSTakashi Sato 	int error = 0;
4969ac27a0ecSDave Kleikamp 
4970bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
4971c4be0c1dSTakashi Sato 		return error;
4972a17712c8SJon Derrick 
4973a17712c8SJon Derrick 	/*
4974a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
4975a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
4976a17712c8SJon Derrick 	 */
4977a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
4978a17712c8SJon Derrick 		return error;
4979a17712c8SJon Derrick 
498071290b36STheodore Ts'o 	/*
498171290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
498271290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
498371290b36STheodore Ts'o 	 * write time when we are mounting the root file system
498471290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
498571290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
498671290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
498771290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
498871290b36STheodore Ts'o 	 * to complain and force a full file system check.
498971290b36STheodore Ts'o 	 */
49901751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
49916a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
4992f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
4993afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
4994afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4995dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
4996dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
4997afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4998f613dfcbSTheodore Ts'o 	else
4999f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5000f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5001d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
500257042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
500357042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
500457042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5005d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
50067f93cff9STheodore Ts'o 		es->s_free_inodes_count =
50077f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
50085d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5009ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
501006db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
50111566a48aSTheodore Ts'o 	if (sync)
50124743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5013e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
50144743f839SPranay Kr. Srivastava 		/*
50154743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
50164743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
50174743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
50184743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
50194743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
50204743f839SPranay Kr. Srivastava 		 * write and hope for the best.
50214743f839SPranay Kr. Srivastava 		 */
50224743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
50234743f839SPranay Kr. Srivastava 		       "superblock detected");
50244743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
50254743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
50264743f839SPranay Kr. Srivastava 	}
5027ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5028914258bfSTheodore Ts'o 	if (sync) {
50291566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5030564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
503100473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5032c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5033b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5034b31e1552SEric Sandeen 			       "superblock");
5035914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5036914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5037914258bfSTheodore Ts'o 		}
5038914258bfSTheodore Ts'o 	}
5039c4be0c1dSTakashi Sato 	return error;
5040ac27a0ecSDave Kleikamp }
5041ac27a0ecSDave Kleikamp 
5042ac27a0ecSDave Kleikamp /*
5043ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5044ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5045ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5046ac27a0ecSDave Kleikamp  */
5047617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5048617ba13bSMingming Cao 					struct ext4_super_block *es)
5049ac27a0ecSDave Kleikamp {
5050617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5051ac27a0ecSDave Kleikamp 
5052e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
50530390131bSFrank Mayhar 		BUG_ON(journal != NULL);
50540390131bSFrank Mayhar 		return;
50550390131bSFrank Mayhar 	}
5056dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
50577ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
50587ffe1ea8SHidehiro Kawai 		goto out;
50597ffe1ea8SHidehiro Kawai 
5060bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5061e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5062e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5063ac27a0ecSDave Kleikamp 	}
50647ffe1ea8SHidehiro Kawai 
50657ffe1ea8SHidehiro Kawai out:
5066dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5067ac27a0ecSDave Kleikamp }
5068ac27a0ecSDave Kleikamp 
5069ac27a0ecSDave Kleikamp /*
5070ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5071ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5072ac27a0ecSDave Kleikamp  * main filesystem now.
5073ac27a0ecSDave Kleikamp  */
5074617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5075617ba13bSMingming Cao 				   struct ext4_super_block *es)
5076ac27a0ecSDave Kleikamp {
5077ac27a0ecSDave Kleikamp 	journal_t *journal;
5078ac27a0ecSDave Kleikamp 	int j_errno;
5079ac27a0ecSDave Kleikamp 	const char *errstr;
5080ac27a0ecSDave Kleikamp 
5081e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
50820390131bSFrank Mayhar 
5083617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5084ac27a0ecSDave Kleikamp 
5085ac27a0ecSDave Kleikamp 	/*
5086ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5087617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5088ac27a0ecSDave Kleikamp 	 */
5089ac27a0ecSDave Kleikamp 
5090dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5091ac27a0ecSDave Kleikamp 	if (j_errno) {
5092ac27a0ecSDave Kleikamp 		char nbuf[16];
5093ac27a0ecSDave Kleikamp 
5094617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
509512062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5096ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
509712062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5098ac27a0ecSDave Kleikamp 
5099617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5100617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5101e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5102ac27a0ecSDave Kleikamp 
5103dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5104d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5105ac27a0ecSDave Kleikamp 	}
5106ac27a0ecSDave Kleikamp }
5107ac27a0ecSDave Kleikamp 
5108ac27a0ecSDave Kleikamp /*
5109ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5110ac27a0ecSDave Kleikamp  * and wait on the commit.
5111ac27a0ecSDave Kleikamp  */
5112617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5113ac27a0ecSDave Kleikamp {
5114ac27a0ecSDave Kleikamp 	journal_t *journal;
5115ac27a0ecSDave Kleikamp 
5116bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5117ac27a0ecSDave Kleikamp 		return 0;
5118ac27a0ecSDave Kleikamp 
5119617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5120b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5121ac27a0ecSDave Kleikamp }
5122ac27a0ecSDave Kleikamp 
5123617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5124ac27a0ecSDave Kleikamp {
512514ce0cb4STheodore Ts'o 	int ret = 0;
51269eddacf9SJan Kara 	tid_t target;
512706a407f1SDmitry Monakhov 	bool needs_barrier = false;
51288d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5129ac27a0ecSDave Kleikamp 
513049598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
51310db1ff22STheodore Ts'o 		return 0;
51320db1ff22STheodore Ts'o 
51339bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
51342e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5135a1177825SJan Kara 	/*
5136a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5137a1177825SJan Kara 	 * no dirty dquots
5138a1177825SJan Kara 	 */
5139a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
514006a407f1SDmitry Monakhov 	/*
514106a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
514206a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
514306a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
514406a407f1SDmitry Monakhov 	 */
5145bda32530STheodore Ts'o 	if (sbi->s_journal) {
514606a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
514706a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
514806a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
514906a407f1SDmitry Monakhov 			needs_barrier = true;
515006a407f1SDmitry Monakhov 
51518d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5152ac27a0ecSDave Kleikamp 			if (wait)
5153bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5154bda32530STheodore Ts'o 							   target);
51550390131bSFrank Mayhar 		}
5156bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5157bda32530STheodore Ts'o 		needs_barrier = true;
515806a407f1SDmitry Monakhov 	if (needs_barrier) {
515906a407f1SDmitry Monakhov 		int err;
516006a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
516106a407f1SDmitry Monakhov 		if (!ret)
516206a407f1SDmitry Monakhov 			ret = err;
516306a407f1SDmitry Monakhov 	}
516406a407f1SDmitry Monakhov 
516506a407f1SDmitry Monakhov 	return ret;
516606a407f1SDmitry Monakhov }
516706a407f1SDmitry Monakhov 
5168ac27a0ecSDave Kleikamp /*
5169ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5170ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5171be4f27d3SYongqiang Yang  *
5172be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
51738e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
51748e8ad8a5SJan Kara  * modifications.
5175ac27a0ecSDave Kleikamp  */
5176c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5177ac27a0ecSDave Kleikamp {
5178c4be0c1dSTakashi Sato 	int error = 0;
5179c4be0c1dSTakashi Sato 	journal_t *journal;
5180ac27a0ecSDave Kleikamp 
5181bc98a42cSDavid Howells 	if (sb_rdonly(sb))
51829ca92389STheodore Ts'o 		return 0;
51839ca92389STheodore Ts'o 
5184c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5185ac27a0ecSDave Kleikamp 
5186bb044576STheodore Ts'o 	if (journal) {
5187ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5188dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
51897ffe1ea8SHidehiro Kawai 
51907ffe1ea8SHidehiro Kawai 		/*
5191bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5192bb044576STheodore Ts'o 		 * flush the journal.
51937ffe1ea8SHidehiro Kawai 		 */
5194c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
51956b0310fbSEric Sandeen 		if (error < 0)
51966b0310fbSEric Sandeen 			goto out;
5197ac27a0ecSDave Kleikamp 
5198ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5199e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5200c642dc9eSEric Sandeen 	}
5201c642dc9eSEric Sandeen 
5202e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
52036b0310fbSEric Sandeen out:
5204bb044576STheodore Ts'o 	if (journal)
52058e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5206bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
52076b0310fbSEric Sandeen 	return error;
5208ac27a0ecSDave Kleikamp }
5209ac27a0ecSDave Kleikamp 
5210ac27a0ecSDave Kleikamp /*
5211ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5212ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5213ac27a0ecSDave Kleikamp  */
5214c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5215ac27a0ecSDave Kleikamp {
5216bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
52179ca92389STheodore Ts'o 		return 0;
52189ca92389STheodore Ts'o 
5219c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
52209ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5221e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5222c642dc9eSEric Sandeen 	}
5223c642dc9eSEric Sandeen 
5224e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5225c4be0c1dSTakashi Sato 	return 0;
5226ac27a0ecSDave Kleikamp }
5227ac27a0ecSDave Kleikamp 
5228673c6100STheodore Ts'o /*
5229673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5230673c6100STheodore Ts'o  */
5231673c6100STheodore Ts'o struct ext4_mount_options {
5232673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5233a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
523408cefc7aSEric W. Biederman 	kuid_t s_resuid;
523508cefc7aSEric W. Biederman 	kgid_t s_resgid;
5236673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5237673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5238673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5239673c6100STheodore Ts'o 	int s_jquota_fmt;
5240a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5241673c6100STheodore Ts'o #endif
5242673c6100STheodore Ts'o };
5243673c6100STheodore Ts'o 
5244617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5245ac27a0ecSDave Kleikamp {
5246617ba13bSMingming Cao 	struct ext4_super_block *es;
5247617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5248ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5249617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5250c79d967dSChristoph Hellwig 	int enable_quota = 0;
52518a266467STheodore Ts'o 	ext4_group_t g;
5252b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5253c5e06d10SJohann Lombardi 	int err = 0;
5254ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
525503dafb5fSChen Gang 	int i, j;
525633458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5257ac27a0ecSDave Kleikamp #endif
5258d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5259ac27a0ecSDave Kleikamp 
526021ac738eSChengguang Xu 	if (data && !orig_data)
526121ac738eSChengguang Xu 		return -ENOMEM;
526221ac738eSChengguang Xu 
5263ac27a0ecSDave Kleikamp 	/* Store the original options */
5264ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5265ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5266a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5267ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5268ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5269ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
527030773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
527130773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5272ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5273ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5274a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
527503dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
527633458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
527733458eabSTheodore Ts'o 
527833458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
527903dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
528003dafb5fSChen Gang 				for (j = 0; j < i; j++)
528103dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
52823e36a163SWei Yongjun 				kfree(orig_data);
528303dafb5fSChen Gang 				return -ENOMEM;
528403dafb5fSChen Gang 			}
528503dafb5fSChen Gang 		} else
528603dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5287ac27a0ecSDave Kleikamp #endif
5288b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5289b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5290ac27a0ecSDave Kleikamp 
5291661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5292ac27a0ecSDave Kleikamp 		err = -EINVAL;
5293ac27a0ecSDave Kleikamp 		goto restore_opts;
5294ac27a0ecSDave Kleikamp 	}
5295ac27a0ecSDave Kleikamp 
52967bc04c5cSBarret Rhoden 	ext4_clamp_want_extra_isize(sb);
52977bc04c5cSBarret Rhoden 
52986b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
52996b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
53006b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
53012d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
53022d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5303c6d3d56dSDarrick J. Wong 	}
5304c6d3d56dSDarrick J. Wong 
53056ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
53066ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
53076ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53086ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
53096ae6514bSPiotr Sarna 			err = -EINVAL;
53106ae6514bSPiotr Sarna 			goto restore_opts;
53116ae6514bSPiotr Sarna 		}
53126ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
53136ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
53146ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
53156ae6514bSPiotr Sarna 			err = -EINVAL;
53166ae6514bSPiotr Sarna 			goto restore_opts;
53176ae6514bSPiotr Sarna 		}
5318923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
5319923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5320923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5321923ae0ffSRoss Zwisler 			err = -EINVAL;
5322923ae0ffSRoss Zwisler 			goto restore_opts;
5323923ae0ffSRoss Zwisler 		}
5324ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5325ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5326ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5327ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5328ab04df78SJan Kara 			err = -EINVAL;
5329ab04df78SJan Kara 			goto restore_opts;
5330ab04df78SJan Kara 		}
5331923ae0ffSRoss Zwisler 	}
5332923ae0ffSRoss Zwisler 
5333cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5334cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5335cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5336cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5337cdb7ee4cSTahsin Erdogan 	}
5338cdb7ee4cSTahsin Erdogan 
5339923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
5340923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5341923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
5342923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
53436ae6514bSPiotr Sarna 	}
53446ae6514bSPiotr Sarna 
53454ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
5346c67d859eSTheodore Ts'o 		ext4_abort(sb, "Abort forced by user");
5347ac27a0ecSDave Kleikamp 
53481751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
53491751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5350ac27a0ecSDave Kleikamp 
5351ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5352ac27a0ecSDave Kleikamp 
5353b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5354617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5355b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5356b3881f74STheodore Ts'o 	}
5357ac27a0ecSDave Kleikamp 
53581751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
53591751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5360a2fd66d0STheodore Ts'o 
53611751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
53624ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5363ac27a0ecSDave Kleikamp 			err = -EROFS;
5364ac27a0ecSDave Kleikamp 			goto restore_opts;
5365ac27a0ecSDave Kleikamp 		}
5366ac27a0ecSDave Kleikamp 
53671751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
536838c03b34STheodore Ts'o 			err = sync_filesystem(sb);
536938c03b34STheodore Ts'o 			if (err < 0)
537038c03b34STheodore Ts'o 				goto restore_opts;
53710f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
53720f0dd62fSChristoph Hellwig 			if (err < 0)
5373c79d967dSChristoph Hellwig 				goto restore_opts;
5374c79d967dSChristoph Hellwig 
5375ac27a0ecSDave Kleikamp 			/*
5376ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5377ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5378ac27a0ecSDave Kleikamp 			 */
53791751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5380ac27a0ecSDave Kleikamp 
5381ac27a0ecSDave Kleikamp 			/*
5382ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5383ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5384ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5385ac27a0ecSDave Kleikamp 			 */
5386617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5387617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5388ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5389ac27a0ecSDave Kleikamp 
5390a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5391617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
53922dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
53932dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5394ac27a0ecSDave Kleikamp 		} else {
5395a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5396e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
53972cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5398ac27a0ecSDave Kleikamp 				err = -EROFS;
5399ac27a0ecSDave Kleikamp 				goto restore_opts;
5400ac27a0ecSDave Kleikamp 			}
5401ead6596bSEric Sandeen 			/*
54028a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
54030b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
54048a266467STheodore Ts'o 			 */
54058a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
54068a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
54078a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
54088a266467STheodore Ts'o 
5409feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5410b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5411b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5412e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
54138a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
54146a797d27SDarrick J. Wong 					err = -EFSBADCRC;
54158a266467STheodore Ts'o 					goto restore_opts;
54168a266467STheodore Ts'o 				}
54178a266467STheodore Ts'o 			}
54188a266467STheodore Ts'o 
54198a266467STheodore Ts'o 			/*
5420ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5421ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5422ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5423ead6596bSEric Sandeen 			 */
5424ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5425b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5426ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5427ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5428b31e1552SEric Sandeen 				       "umount/remount instead");
5429ead6596bSEric Sandeen 				err = -EINVAL;
5430ead6596bSEric Sandeen 				goto restore_opts;
5431ead6596bSEric Sandeen 			}
5432ead6596bSEric Sandeen 
5433ac27a0ecSDave Kleikamp 			/*
5434ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5435ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5436ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5437ac27a0ecSDave Kleikamp 			 * the partition.)
5438ac27a0ecSDave Kleikamp 			 */
54390390131bSFrank Mayhar 			if (sbi->s_journal)
5440617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5441ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5442c89128a0SJaegeuk Kim 
5443c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5444c89128a0SJaegeuk Kim 			if (err)
5445c89128a0SJaegeuk Kim 				goto restore_opts;
5446c89128a0SJaegeuk Kim 
54471751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5448e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5449c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5450c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5451c5e06d10SJohann Lombardi 					err = -EROFS;
5452c5e06d10SJohann Lombardi 					goto restore_opts;
5453c5e06d10SJohann Lombardi 				}
5454c79d967dSChristoph Hellwig 			enable_quota = 1;
5455ac27a0ecSDave Kleikamp 		}
5456ac27a0ecSDave Kleikamp 	}
5457bfff6873SLukas Czerner 
5458bfff6873SLukas Czerner 	/*
5459bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5460bfff6873SLukas Czerner 	 * current settings
5461bfff6873SLukas Czerner 	 */
5462bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5463bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5464bfff6873SLukas Czerner 	else {
5465bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5466bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5467bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5468bfff6873SLukas Czerner 	}
5469bfff6873SLukas Czerner 
54706fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5471c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5472c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5473c89128a0SJaegeuk Kim 		if (err)
5474c89128a0SJaegeuk Kim 			goto restore_opts;
5475c89128a0SJaegeuk Kim 	}
54760390131bSFrank Mayhar 
5477ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5478ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5479a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5480ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
54817c319d32SAditya Kali 	if (enable_quota) {
54827c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
54830f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5484e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
54857c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
548607724f98STheodore Ts'o 			if (err)
54877c319d32SAditya Kali 				goto restore_opts;
54887c319d32SAditya Kali 		}
54897c319d32SAditya Kali 	}
54907c319d32SAditya Kali #endif
5491d4c402d9SCurt Wohlgemuth 
54921751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5493d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5494d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5495ac27a0ecSDave Kleikamp 	return 0;
54960b8e58a1SAndreas Dilger 
5497ac27a0ecSDave Kleikamp restore_opts:
5498ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5499ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5500a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5501ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5502ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5503ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
550430773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
550530773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5506ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5507ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5508a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
550933458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
551033458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5511ac27a0ecSDave Kleikamp 	}
551233458eabSTheodore Ts'o 	synchronize_rcu();
551333458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
551433458eabSTheodore Ts'o 		kfree(to_free[i]);
5515ac27a0ecSDave Kleikamp #endif
5516d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5517ac27a0ecSDave Kleikamp 	return err;
5518ac27a0ecSDave Kleikamp }
5519ac27a0ecSDave Kleikamp 
5520689c958cSLi Xi #ifdef CONFIG_QUOTA
5521689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5522689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5523689c958cSLi Xi {
5524689c958cSLi Xi 	struct kqid qid;
5525689c958cSLi Xi 	struct dquot *dquot;
5526689c958cSLi Xi 	u64 limit;
5527689c958cSLi Xi 	u64 curblock;
5528689c958cSLi Xi 
5529689c958cSLi Xi 	qid = make_kqid_projid(projid);
5530689c958cSLi Xi 	dquot = dqget(sb, qid);
5531689c958cSLi Xi 	if (IS_ERR(dquot))
5532689c958cSLi Xi 		return PTR_ERR(dquot);
55337b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5534689c958cSLi Xi 
5535689c958cSLi Xi 	limit = (dquot->dq_dqb.dqb_bsoftlimit ?
5536689c958cSLi Xi 		 dquot->dq_dqb.dqb_bsoftlimit :
5537689c958cSLi Xi 		 dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
5538689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5539f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5540f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5541689c958cSLi Xi 		buf->f_blocks = limit;
5542689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5543689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5544689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5545689c958cSLi Xi 	}
5546689c958cSLi Xi 
5547689c958cSLi Xi 	limit = dquot->dq_dqb.dqb_isoftlimit ?
5548689c958cSLi Xi 		dquot->dq_dqb.dqb_isoftlimit :
5549689c958cSLi Xi 		dquot->dq_dqb.dqb_ihardlimit;
5550689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5551689c958cSLi Xi 		buf->f_files = limit;
5552689c958cSLi Xi 		buf->f_ffree =
5553689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5554689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5555689c958cSLi Xi 	}
5556689c958cSLi Xi 
55577b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5558689c958cSLi Xi 	dqput(dquot);
5559689c958cSLi Xi 	return 0;
5560689c958cSLi Xi }
5561689c958cSLi Xi #endif
5562689c958cSLi Xi 
5563617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5564ac27a0ecSDave Kleikamp {
5565ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5566617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5567617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
556827dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5569960cc398SPekka Enberg 	u64 fsid;
5570d02a9391SKazuya Mio 	s64 bfree;
557127dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5572ac27a0ecSDave Kleikamp 
5573952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5574952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5575ac27a0ecSDave Kleikamp 
5576617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5577ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5578b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
557957042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
558057042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5581d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
558257042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
558327dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
558427dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
558527dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5586ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5587ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
558852d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5589617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5590960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5591960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5592960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5593960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
55940b8e58a1SAndreas Dilger 
5595689c958cSLi Xi #ifdef CONFIG_QUOTA
5596689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5597689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5598689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5599689c958cSLi Xi #endif
5600ac27a0ecSDave Kleikamp 	return 0;
5601ac27a0ecSDave Kleikamp }
5602ac27a0ecSDave Kleikamp 
5603ac27a0ecSDave Kleikamp 
5604ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5605ac27a0ecSDave Kleikamp 
5606bc8230eeSJan Kara /*
5607bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5608bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5609bc8230eeSJan Kara  */
5610ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5611ac27a0ecSDave Kleikamp {
56124c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5613ac27a0ecSDave Kleikamp }
5614ac27a0ecSDave Kleikamp 
5615617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5616ac27a0ecSDave Kleikamp {
5617ac27a0ecSDave Kleikamp 	int ret, err;
5618ac27a0ecSDave Kleikamp 	handle_t *handle;
5619ac27a0ecSDave Kleikamp 	struct inode *inode;
5620ac27a0ecSDave Kleikamp 
5621ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
56229924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5623617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5624ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5625ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5626ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5627617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5628ac27a0ecSDave Kleikamp 	if (!ret)
5629ac27a0ecSDave Kleikamp 		ret = err;
5630ac27a0ecSDave Kleikamp 	return ret;
5631ac27a0ecSDave Kleikamp }
5632ac27a0ecSDave Kleikamp 
5633617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5634ac27a0ecSDave Kleikamp {
5635ac27a0ecSDave Kleikamp 	int ret, err;
5636ac27a0ecSDave Kleikamp 	handle_t *handle;
5637ac27a0ecSDave Kleikamp 
56389924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5639617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5640ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5641ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5642ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5643617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5644ac27a0ecSDave Kleikamp 	if (!ret)
5645ac27a0ecSDave Kleikamp 		ret = err;
5646ac27a0ecSDave Kleikamp 	return ret;
5647ac27a0ecSDave Kleikamp }
5648ac27a0ecSDave Kleikamp 
5649617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5650ac27a0ecSDave Kleikamp {
5651ac27a0ecSDave Kleikamp 	int ret, err;
5652ac27a0ecSDave Kleikamp 	handle_t *handle;
5653ac27a0ecSDave Kleikamp 
56549924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5655617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
56569c3013e9SJan Kara 	if (IS_ERR(handle)) {
56579c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
56589c3013e9SJan Kara 		dquot_release(dquot);
5659ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
56609c3013e9SJan Kara 	}
5661ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5662617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5663ac27a0ecSDave Kleikamp 	if (!ret)
5664ac27a0ecSDave Kleikamp 		ret = err;
5665ac27a0ecSDave Kleikamp 	return ret;
5666ac27a0ecSDave Kleikamp }
5667ac27a0ecSDave Kleikamp 
5668617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5669ac27a0ecSDave Kleikamp {
5670262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5671262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5672262b4662SJan Kara 
56732c8be6b2SJan Kara 	/* Are we journaling quotas? */
5674e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5675262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5676ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5677617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5678ac27a0ecSDave Kleikamp 	} else {
5679ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5680ac27a0ecSDave Kleikamp 	}
5681ac27a0ecSDave Kleikamp }
5682ac27a0ecSDave Kleikamp 
5683617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5684ac27a0ecSDave Kleikamp {
5685ac27a0ecSDave Kleikamp 	int ret, err;
5686ac27a0ecSDave Kleikamp 	handle_t *handle;
5687ac27a0ecSDave Kleikamp 
5688ac27a0ecSDave Kleikamp 	/* Data block + inode block */
56892b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5690ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5691ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5692ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5693617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5694ac27a0ecSDave Kleikamp 	if (!ret)
5695ac27a0ecSDave Kleikamp 		ret = err;
5696ac27a0ecSDave Kleikamp 	return ret;
5697ac27a0ecSDave Kleikamp }
5698ac27a0ecSDave Kleikamp 
5699ac27a0ecSDave Kleikamp /*
5700ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5701ac27a0ecSDave Kleikamp  * the quota file and such...
5702ac27a0ecSDave Kleikamp  */
5703617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5704ac27a0ecSDave Kleikamp {
570533458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5706617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5707ac27a0ecSDave Kleikamp }
5708ac27a0ecSDave Kleikamp 
5709daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5710daf647d2STheodore Ts'o {
5711daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5712daf647d2STheodore Ts'o 
5713daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5714daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5715daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5716daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5717daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5718daf647d2STheodore Ts'o 	 */
5719daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5720daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5721daf647d2STheodore Ts'o }
5722daf647d2STheodore Ts'o 
5723ac27a0ecSDave Kleikamp /*
5724ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5725ac27a0ecSDave Kleikamp  */
5726617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
57278c54ca9cSAl Viro 			 const struct path *path)
5728ac27a0ecSDave Kleikamp {
5729ac27a0ecSDave Kleikamp 	int err;
5730ac27a0ecSDave Kleikamp 
5731ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5732ac27a0ecSDave Kleikamp 		return -EINVAL;
57330623543bSJan Kara 
5734ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5735d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5736ac27a0ecSDave Kleikamp 		return -EXDEV;
57370623543bSJan Kara 	/* Journaling quota? */
57380623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
57392b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5740f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5741b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5742b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5743b31e1552SEric Sandeen 				"Journaled quota will not work");
574491389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
574591389240SJan Kara 	} else {
574691389240SJan Kara 		/*
574791389240SJan Kara 		 * Clear the flag just in case mount options changed since
574891389240SJan Kara 		 * last time.
574991389240SJan Kara 		 */
575091389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
57510623543bSJan Kara 	}
57520623543bSJan Kara 
57530623543bSJan Kara 	/*
57540623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
57550623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
57560623543bSJan Kara 	 */
57570390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
57582b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
57590623543bSJan Kara 		/*
57600623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
57610623543bSJan Kara 		 * otherwise be livelocked...
57620623543bSJan Kara 		 */
57630623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
57647ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
57650623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5766f00c9e44SJan Kara 		if (err)
57677ffe1ea8SHidehiro Kawai 			return err;
57687ffe1ea8SHidehiro Kawai 	}
5769957153fcSJan Kara 
5770daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5771daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5772957153fcSJan Kara 	if (err) {
5773daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5774daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5775957153fcSJan Kara 	} else {
5776957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5777957153fcSJan Kara 		handle_t *handle;
5778957153fcSJan Kara 
577961a92987SJan Kara 		/*
578061a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
578161a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
578261a92987SJan Kara 		 * are already enabled and this is not a hard failure.
578361a92987SJan Kara 		 */
5784957153fcSJan Kara 		inode_lock(inode);
5785957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5786957153fcSJan Kara 		if (IS_ERR(handle))
5787957153fcSJan Kara 			goto unlock_inode;
5788957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5789957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5790957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5791957153fcSJan Kara 		ext4_mark_inode_dirty(handle, inode);
5792957153fcSJan Kara 		ext4_journal_stop(handle);
5793957153fcSJan Kara 	unlock_inode:
5794957153fcSJan Kara 		inode_unlock(inode);
5795957153fcSJan Kara 	}
5796daf647d2STheodore Ts'o 	return err;
5797ac27a0ecSDave Kleikamp }
5798ac27a0ecSDave Kleikamp 
57997c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
58007c319d32SAditya Kali 			     unsigned int flags)
58017c319d32SAditya Kali {
58027c319d32SAditya Kali 	int err;
58037c319d32SAditya Kali 	struct inode *qf_inode;
5804a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58057c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5806689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5807689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58087c319d32SAditya Kali 	};
58097c319d32SAditya Kali 
5810e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
58117c319d32SAditya Kali 
58127c319d32SAditya Kali 	if (!qf_inums[type])
58137c319d32SAditya Kali 		return -EPERM;
58147c319d32SAditya Kali 
58158a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
58167c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
58177c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
58187c319d32SAditya Kali 		return PTR_ERR(qf_inode);
58197c319d32SAditya Kali 	}
58207c319d32SAditya Kali 
5821bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5822bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5823daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
58247c319d32SAditya Kali 	err = dquot_enable(qf_inode, type, format_id, flags);
5825daf647d2STheodore Ts'o 	if (err)
5826daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
582761157b24SPan Bian 	iput(qf_inode);
58287c319d32SAditya Kali 
58297c319d32SAditya Kali 	return err;
58307c319d32SAditya Kali }
58317c319d32SAditya Kali 
58327c319d32SAditya Kali /* Enable usage tracking for all quota types. */
58337c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
58347c319d32SAditya Kali {
58357c319d32SAditya Kali 	int type, err = 0;
5836a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58377c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5838689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5839689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
58407c319d32SAditya Kali 	};
584149da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
584249da9392SJan Kara 		test_opt(sb, USRQUOTA),
584349da9392SJan Kara 		test_opt(sb, GRPQUOTA),
584449da9392SJan Kara 		test_opt(sb, PRJQUOTA),
584549da9392SJan Kara 	};
58467c319d32SAditya Kali 
584791389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5848a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
58497c319d32SAditya Kali 		if (qf_inums[type]) {
58507c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
585149da9392SJan Kara 				DQUOT_USAGE_ENABLED |
585249da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
58537c319d32SAditya Kali 			if (err) {
58547c319d32SAditya Kali 				ext4_warning(sb,
585572ba7450STheodore Ts'o 					"Failed to enable quota tracking "
585672ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
585772ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
58587f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
58597f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
58607f144fd0SJunichi Uekawa 
58617c319d32SAditya Kali 				return err;
58627c319d32SAditya Kali 			}
58637c319d32SAditya Kali 		}
58647c319d32SAditya Kali 	}
58657c319d32SAditya Kali 	return 0;
58667c319d32SAditya Kali }
58677c319d32SAditya Kali 
5868ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5869ca0e05e4SDmitry Monakhov {
587021f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
587121f97697SJan Kara 	handle_t *handle;
5872957153fcSJan Kara 	int err;
587321f97697SJan Kara 
587487009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
587587009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
587687009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5877ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5878ca0e05e4SDmitry Monakhov 
5879957153fcSJan Kara 	if (!inode || !igrab(inode))
58800b268590SAmir Goldstein 		goto out;
58810b268590SAmir Goldstein 
5882957153fcSJan Kara 	err = dquot_quota_off(sb, type);
5883964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
5884957153fcSJan Kara 		goto out_put;
5885957153fcSJan Kara 
5886957153fcSJan Kara 	inode_lock(inode);
588761a92987SJan Kara 	/*
588861a92987SJan Kara 	 * Update modification times of quota files when userspace can
588961a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
589061a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
589161a92987SJan Kara 	 */
58929924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
589321f97697SJan Kara 	if (IS_ERR(handle))
5894957153fcSJan Kara 		goto out_unlock;
5895957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
5896957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
5897eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
589821f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
589921f97697SJan Kara 	ext4_journal_stop(handle);
5900957153fcSJan Kara out_unlock:
5901957153fcSJan Kara 	inode_unlock(inode);
5902957153fcSJan Kara out_put:
5903964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
5904957153fcSJan Kara 	iput(inode);
5905957153fcSJan Kara 	return err;
590621f97697SJan Kara out:
5907ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
5908ca0e05e4SDmitry Monakhov }
5909ca0e05e4SDmitry Monakhov 
5910ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
5911ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
5912ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
5913ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
5914617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5915ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
5916ac27a0ecSDave Kleikamp {
5917ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5918725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5919ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
5920ac27a0ecSDave Kleikamp 	int tocopy;
5921ac27a0ecSDave Kleikamp 	size_t toread;
5922ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5923ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
5924ac27a0ecSDave Kleikamp 
5925ac27a0ecSDave Kleikamp 	if (off > i_size)
5926ac27a0ecSDave Kleikamp 		return 0;
5927ac27a0ecSDave Kleikamp 	if (off+len > i_size)
5928ac27a0ecSDave Kleikamp 		len = i_size-off;
5929ac27a0ecSDave Kleikamp 	toread = len;
5930ac27a0ecSDave Kleikamp 	while (toread > 0) {
5931ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
5932ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
59331c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
59341c215028STheodore Ts'o 		if (IS_ERR(bh))
59351c215028STheodore Ts'o 			return PTR_ERR(bh);
5936ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
5937ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
5938ac27a0ecSDave Kleikamp 		else
5939ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
5940ac27a0ecSDave Kleikamp 		brelse(bh);
5941ac27a0ecSDave Kleikamp 		offset = 0;
5942ac27a0ecSDave Kleikamp 		toread -= tocopy;
5943ac27a0ecSDave Kleikamp 		data += tocopy;
5944ac27a0ecSDave Kleikamp 		blk++;
5945ac27a0ecSDave Kleikamp 	}
5946ac27a0ecSDave Kleikamp 	return len;
5947ac27a0ecSDave Kleikamp }
5948ac27a0ecSDave Kleikamp 
5949ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
5950ac27a0ecSDave Kleikamp  * enough credits) */
5951617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
5952ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
5953ac27a0ecSDave Kleikamp {
5954ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
5955725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
59561c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
5957c5e298aeSTheodore Ts'o 	int retries = 0;
5958ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
5959ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
5960ac27a0ecSDave Kleikamp 
59610390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
5962b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5963b31e1552SEric Sandeen 			" cancelled because transaction is not started",
59649c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
59659c3013e9SJan Kara 		return -EIO;
59669c3013e9SJan Kara 	}
596767eeb568SDmitry Monakhov 	/*
596867eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
596967eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
597067eeb568SDmitry Monakhov 	 */
597167eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
597267eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
597367eeb568SDmitry Monakhov 			" cancelled because not block aligned",
597467eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
597567eeb568SDmitry Monakhov 		return -EIO;
597667eeb568SDmitry Monakhov 	}
597767eeb568SDmitry Monakhov 
5978c5e298aeSTheodore Ts'o 	do {
5979c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
5980c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
5981c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
5982c5e298aeSTheodore Ts'o 	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
5983c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
59841c215028STheodore Ts'o 	if (IS_ERR(bh))
59851c215028STheodore Ts'o 		return PTR_ERR(bh);
5986ac27a0ecSDave Kleikamp 	if (!bh)
5987ac27a0ecSDave Kleikamp 		goto out;
59885d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
5989617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
5990ac27a0ecSDave Kleikamp 	if (err) {
5991ac27a0ecSDave Kleikamp 		brelse(bh);
59921c215028STheodore Ts'o 		return err;
5993ac27a0ecSDave Kleikamp 	}
5994ac27a0ecSDave Kleikamp 	lock_buffer(bh);
599567eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
5996ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
5997ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
59980390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5999ac27a0ecSDave Kleikamp 	brelse(bh);
6000ac27a0ecSDave Kleikamp out:
600167eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
600267eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6003617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
6004617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
600521f97697SJan Kara 	}
600667eeb568SDmitry Monakhov 	return len;
6007ac27a0ecSDave Kleikamp }
6008ac27a0ecSDave Kleikamp 
60098f0e8746STheodore Ts'o static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
60108f0e8746STheodore Ts'o {
60118f0e8746STheodore Ts'o 	const struct quota_format_ops	*ops;
60128f0e8746STheodore Ts'o 
60138f0e8746STheodore Ts'o 	if (!sb_has_quota_loaded(sb, qid->type))
60148f0e8746STheodore Ts'o 		return -ESRCH;
60158f0e8746STheodore Ts'o 	ops = sb_dqopt(sb)->ops[qid->type];
60168f0e8746STheodore Ts'o 	if (!ops || !ops->get_next_id)
60178f0e8746STheodore Ts'o 		return -ENOSYS;
60188f0e8746STheodore Ts'o 	return dquot_get_next_id(sb, qid);
60198f0e8746STheodore Ts'o }
6020ac27a0ecSDave Kleikamp #endif
6021ac27a0ecSDave Kleikamp 
6022152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6023152a0836SAl Viro 		       const char *dev_name, void *data)
6024ac27a0ecSDave Kleikamp {
6025152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6026ac27a0ecSDave Kleikamp }
6027ac27a0ecSDave Kleikamp 
6028c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
602924b58424STheodore Ts'o static inline void register_as_ext2(void)
603024b58424STheodore Ts'o {
603124b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
603224b58424STheodore Ts'o 	if (err)
603324b58424STheodore Ts'o 		printk(KERN_WARNING
603424b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
603524b58424STheodore Ts'o }
603624b58424STheodore Ts'o 
603724b58424STheodore Ts'o static inline void unregister_as_ext2(void)
603824b58424STheodore Ts'o {
603924b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
604024b58424STheodore Ts'o }
60412035e776STheodore Ts'o 
60422035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
60432035e776STheodore Ts'o {
6044e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
60452035e776STheodore Ts'o 		return 0;
6046bc98a42cSDavid Howells 	if (sb_rdonly(sb))
60472035e776STheodore Ts'o 		return 1;
6048e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
60492035e776STheodore Ts'o 		return 0;
60502035e776STheodore Ts'o 	return 1;
60512035e776STheodore Ts'o }
605224b58424STheodore Ts'o #else
605324b58424STheodore Ts'o static inline void register_as_ext2(void) { }
605424b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
60552035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
605624b58424STheodore Ts'o #endif
605724b58424STheodore Ts'o 
605824b58424STheodore Ts'o static inline void register_as_ext3(void)
605924b58424STheodore Ts'o {
606024b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
606124b58424STheodore Ts'o 	if (err)
606224b58424STheodore Ts'o 		printk(KERN_WARNING
606324b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
606424b58424STheodore Ts'o }
606524b58424STheodore Ts'o 
606624b58424STheodore Ts'o static inline void unregister_as_ext3(void)
606724b58424STheodore Ts'o {
606824b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
606924b58424STheodore Ts'o }
60702035e776STheodore Ts'o 
60712035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
60722035e776STheodore Ts'o {
6073e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
60742035e776STheodore Ts'o 		return 0;
6075e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
60762035e776STheodore Ts'o 		return 0;
6077bc98a42cSDavid Howells 	if (sb_rdonly(sb))
60782035e776STheodore Ts'o 		return 1;
6079e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
60802035e776STheodore Ts'o 		return 0;
60812035e776STheodore Ts'o 	return 1;
60822035e776STheodore Ts'o }
608324b58424STheodore Ts'o 
608403010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6085ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
608603010a33STheodore Ts'o 	.name		= "ext4",
6087152a0836SAl Viro 	.mount		= ext4_mount,
6088ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6089ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6090ac27a0ecSDave Kleikamp };
60917f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6092ac27a0ecSDave Kleikamp 
6093e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6094e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6095e9e3bcecSEric Sandeen 
60965dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6097ac27a0ecSDave Kleikamp {
6098e9e3bcecSEric Sandeen 	int i, err;
6099c9de560dSAlex Tomas 
6100e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
610107c0c5d8SAl Viro 	ext4_li_info = NULL;
610207c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
610307c0c5d8SAl Viro 
61049a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
610512e9b892SDmitry Monakhov 	ext4_check_flag_values();
6106e9e3bcecSEric Sandeen 
6107e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6108e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6109e9e3bcecSEric Sandeen 
611051865fdaSZheng Liu 	err = ext4_init_es();
61116fd058f7STheodore Ts'o 	if (err)
61126fd058f7STheodore Ts'o 		return err;
611351865fdaSZheng Liu 
61141dc0aa46SEric Whitney 	err = ext4_init_pending();
61151dc0aa46SEric Whitney 	if (err)
61161dc0aa46SEric Whitney 		goto out6;
61171dc0aa46SEric Whitney 
611851865fdaSZheng Liu 	err = ext4_init_pageio();
611951865fdaSZheng Liu 	if (err)
6120b5799018STheodore Ts'o 		goto out5;
612151865fdaSZheng Liu 
61225dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6123bd2d0210STheodore Ts'o 	if (err)
6124b5799018STheodore Ts'o 		goto out4;
6125857ac889SLukas Czerner 
6126b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6127dd68314cSTheodore Ts'o 	if (err)
6128b5799018STheodore Ts'o 		goto out3;
6129857ac889SLukas Czerner 
61305dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6131ac27a0ecSDave Kleikamp 	if (err)
6132c9de560dSAlex Tomas 		goto out2;
6133ac27a0ecSDave Kleikamp 	err = init_inodecache();
6134ac27a0ecSDave Kleikamp 	if (err)
6135ac27a0ecSDave Kleikamp 		goto out1;
613624b58424STheodore Ts'o 	register_as_ext3();
61372035e776STheodore Ts'o 	register_as_ext2();
613803010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6139ac27a0ecSDave Kleikamp 	if (err)
6140ac27a0ecSDave Kleikamp 		goto out;
6141bfff6873SLukas Czerner 
6142ac27a0ecSDave Kleikamp 	return 0;
6143ac27a0ecSDave Kleikamp out:
614424b58424STheodore Ts'o 	unregister_as_ext2();
614524b58424STheodore Ts'o 	unregister_as_ext3();
6146ac27a0ecSDave Kleikamp 	destroy_inodecache();
6147ac27a0ecSDave Kleikamp out1:
61485dabfc78STheodore Ts'o 	ext4_exit_mballoc();
61499c191f70ST Makphaibulchoke out2:
6150b5799018STheodore Ts'o 	ext4_exit_sysfs();
6151b5799018STheodore Ts'o out3:
6152dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6153b5799018STheodore Ts'o out4:
61545dabfc78STheodore Ts'o 	ext4_exit_pageio();
6155b5799018STheodore Ts'o out5:
61561dc0aa46SEric Whitney 	ext4_exit_pending();
61571dc0aa46SEric Whitney out6:
615851865fdaSZheng Liu 	ext4_exit_es();
615951865fdaSZheng Liu 
6160ac27a0ecSDave Kleikamp 	return err;
6161ac27a0ecSDave Kleikamp }
6162ac27a0ecSDave Kleikamp 
61635dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6164ac27a0ecSDave Kleikamp {
6165bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
616624b58424STheodore Ts'o 	unregister_as_ext2();
616724b58424STheodore Ts'o 	unregister_as_ext3();
616803010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6169ac27a0ecSDave Kleikamp 	destroy_inodecache();
61705dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6171b5799018STheodore Ts'o 	ext4_exit_sysfs();
61725dabfc78STheodore Ts'o 	ext4_exit_system_zone();
61735dabfc78STheodore Ts'o 	ext4_exit_pageio();
6174dd12ed14SEric Sandeen 	ext4_exit_es();
61751dc0aa46SEric Whitney 	ext4_exit_pending();
6176ac27a0ecSDave Kleikamp }
6177ac27a0ecSDave Kleikamp 
6178ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
617983982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6180ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
61817ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
61825dabfc78STheodore Ts'o module_init(ext4_init_fs)
61835dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6184