xref: /linux/fs/ext4/super.c (revision 4f74d15fe40833d4fb4f5ead7437aa73840596a5)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/super.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  from
11ac27a0ecSDave Kleikamp  *
12ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
13ac27a0ecSDave Kleikamp  *
14ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
15ac27a0ecSDave Kleikamp  *
16ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
17ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
18ac27a0ecSDave Kleikamp  */
19ac27a0ecSDave Kleikamp 
20ac27a0ecSDave Kleikamp #include <linux/module.h>
21ac27a0ecSDave Kleikamp #include <linux/string.h>
22ac27a0ecSDave Kleikamp #include <linux/fs.h>
23ac27a0ecSDave Kleikamp #include <linux/time.h>
24c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
2866114cadSTejun Heo #include <linux/backing-dev.h>
29ac27a0ecSDave Kleikamp #include <linux/parser.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.h>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ef510424SDan Williams #include <linux/dax.h>
427abc52c2SDan Magenheimer #include <linux/cleancache.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
44ee73f9a5SJeff Layton #include <linux/iversion.h>
45c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h>
46c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
47bfff6873SLukas Czerner #include <linux/kthread.h>
48bfff6873SLukas Czerner #include <linux/freezer.h>
49bfff6873SLukas Czerner 
503dcf5451SChristoph Hellwig #include "ext4.h"
514a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
523dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
53ac27a0ecSDave Kleikamp #include "xattr.h"
54ac27a0ecSDave Kleikamp #include "acl.h"
553661d286STheodore Ts'o #include "mballoc.h"
560c9ec4beSDarrick J. Wong #include "fsmap.h"
57ac27a0ecSDave Kleikamp 
589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
599bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
609bffad1eSTheodore Ts'o 
610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
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:
96c1e8d7c6SMichel Lespinasse  * mmap_lock -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
97e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
98e74031fdSJan Kara  *
99e74031fdSJan Kara  * buffered write path:
100c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
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:
110c1e8d7c6SMichel Lespinasse  * sb_start_write -> i_mutex -> mmap_lock
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);
157cf2834a5STheodore Ts'o 	if (ext4_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 
2078fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
2088fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
209bd81d8eeSLaurent Vivier {
2103a14589cSAneesh Kumar K.V 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
2118fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2128fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
213bd81d8eeSLaurent Vivier }
214bd81d8eeSLaurent Vivier 
2158fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
2168fadc143SAlexandre Ratchov 			       struct ext4_group_desc *bg)
217bd81d8eeSLaurent Vivier {
2185272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
2198fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2208fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
221bd81d8eeSLaurent Vivier }
222bd81d8eeSLaurent Vivier 
2238fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb,
2248fadc143SAlexandre Ratchov 			      struct ext4_group_desc *bg)
225bd81d8eeSLaurent Vivier {
2265272f837SAneesh Kumar K.V 	return le32_to_cpu(bg->bg_inode_table_lo) |
2278fadc143SAlexandre Ratchov 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
2288fadc143SAlexandre Ratchov 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
229bd81d8eeSLaurent Vivier }
230bd81d8eeSLaurent Vivier 
231021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb,
232560671a0SAneesh Kumar K.V 			       struct ext4_group_desc *bg)
233560671a0SAneesh Kumar K.V {
234560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
235560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
236560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
237560671a0SAneesh Kumar K.V }
238560671a0SAneesh Kumar K.V 
239560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb,
240560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
241560671a0SAneesh Kumar K.V {
242560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
243560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
244560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
245560671a0SAneesh Kumar K.V }
246560671a0SAneesh Kumar K.V 
247560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb,
248560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
249560671a0SAneesh Kumar K.V {
250560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
251560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
252560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
253560671a0SAneesh Kumar K.V }
254560671a0SAneesh Kumar K.V 
255560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb,
256560671a0SAneesh Kumar K.V 			      struct ext4_group_desc *bg)
257560671a0SAneesh Kumar K.V {
258560671a0SAneesh Kumar K.V 	return le16_to_cpu(bg->bg_itable_unused_lo) |
259560671a0SAneesh Kumar K.V 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
260560671a0SAneesh Kumar K.V 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
261560671a0SAneesh Kumar K.V }
262560671a0SAneesh Kumar K.V 
2638fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb,
2648fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
265bd81d8eeSLaurent Vivier {
2663a14589cSAneesh Kumar K.V 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
2678fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2688fadc143SAlexandre Ratchov 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
269bd81d8eeSLaurent Vivier }
270bd81d8eeSLaurent Vivier 
2718fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb,
2728fadc143SAlexandre Ratchov 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
273bd81d8eeSLaurent Vivier {
2745272f837SAneesh Kumar K.V 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
2758fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2768fadc143SAlexandre Ratchov 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
277bd81d8eeSLaurent Vivier }
278bd81d8eeSLaurent Vivier 
2798fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb,
2808fadc143SAlexandre Ratchov 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
281bd81d8eeSLaurent Vivier {
2825272f837SAneesh Kumar K.V 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
2838fadc143SAlexandre Ratchov 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
2848fadc143SAlexandre Ratchov 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
285bd81d8eeSLaurent Vivier }
286bd81d8eeSLaurent Vivier 
287021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb,
288560671a0SAneesh Kumar K.V 				  struct ext4_group_desc *bg, __u32 count)
289560671a0SAneesh Kumar K.V {
290560671a0SAneesh Kumar K.V 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
291560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
292560671a0SAneesh Kumar K.V 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
293560671a0SAneesh Kumar K.V }
294560671a0SAneesh Kumar K.V 
295560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb,
296560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
297560671a0SAneesh Kumar K.V {
298560671a0SAneesh Kumar K.V 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
299560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
300560671a0SAneesh Kumar K.V 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
301560671a0SAneesh Kumar K.V }
302560671a0SAneesh Kumar K.V 
303560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb,
304560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
305560671a0SAneesh Kumar K.V {
306560671a0SAneesh Kumar K.V 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
307560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
308560671a0SAneesh Kumar K.V 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
309560671a0SAneesh Kumar K.V }
310560671a0SAneesh Kumar K.V 
311560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb,
312560671a0SAneesh Kumar K.V 			  struct ext4_group_desc *bg, __u32 count)
313560671a0SAneesh Kumar K.V {
314560671a0SAneesh Kumar K.V 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
315560671a0SAneesh Kumar K.V 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
316560671a0SAneesh Kumar K.V 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
317560671a0SAneesh Kumar K.V }
318560671a0SAneesh Kumar K.V 
3196a0678a7SArnd Bergmann static void __ext4_update_tstamp(__le32 *lo, __u8 *hi)
3206a0678a7SArnd Bergmann {
3216a0678a7SArnd Bergmann 	time64_t now = ktime_get_real_seconds();
3226a0678a7SArnd Bergmann 
3236a0678a7SArnd Bergmann 	now = clamp_val(now, 0, (1ull << 40) - 1);
3246a0678a7SArnd Bergmann 
3256a0678a7SArnd Bergmann 	*lo = cpu_to_le32(lower_32_bits(now));
3266a0678a7SArnd Bergmann 	*hi = upper_32_bits(now);
3276a0678a7SArnd Bergmann }
3286a0678a7SArnd Bergmann 
3296a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
3306a0678a7SArnd Bergmann {
3316a0678a7SArnd Bergmann 	return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
3326a0678a7SArnd Bergmann }
3336a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \
3346a0678a7SArnd Bergmann 	__ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
3356a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \
3366a0678a7SArnd Bergmann 	__ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
337d3d1faf6SCurt Wohlgemuth 
33854d3adbcSTheodore Ts'o static void __save_error_info(struct super_block *sb, int error,
33954d3adbcSTheodore Ts'o 			      __u32 ino, __u64 block,
34054d3adbcSTheodore Ts'o 			      const char *func, unsigned int line)
3411c13d5c0STheodore Ts'o {
3421c13d5c0STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
34354d3adbcSTheodore Ts'o 	int err;
3441c13d5c0STheodore Ts'o 
3451c13d5c0STheodore Ts'o 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3461b46617bSTheodore Ts'o 	if (bdev_read_only(sb->s_bdev))
3471b46617bSTheodore Ts'o 		return;
3481c13d5c0STheodore Ts'o 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
3496a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_last_error_time);
3501c13d5c0STheodore Ts'o 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
3511c13d5c0STheodore Ts'o 	es->s_last_error_line = cpu_to_le32(line);
35254d3adbcSTheodore Ts'o 	es->s_last_error_ino = cpu_to_le32(ino);
35354d3adbcSTheodore Ts'o 	es->s_last_error_block = cpu_to_le64(block);
35454d3adbcSTheodore Ts'o 	switch (error) {
35554d3adbcSTheodore Ts'o 	case EIO:
35654d3adbcSTheodore Ts'o 		err = EXT4_ERR_EIO;
35754d3adbcSTheodore Ts'o 		break;
35854d3adbcSTheodore Ts'o 	case ENOMEM:
35954d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOMEM;
36054d3adbcSTheodore Ts'o 		break;
36154d3adbcSTheodore Ts'o 	case EFSBADCRC:
36254d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFSBADCRC;
36354d3adbcSTheodore Ts'o 		break;
36454d3adbcSTheodore Ts'o 	case 0:
36554d3adbcSTheodore Ts'o 	case EFSCORRUPTED:
36654d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFSCORRUPTED;
36754d3adbcSTheodore Ts'o 		break;
36854d3adbcSTheodore Ts'o 	case ENOSPC:
36954d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOSPC;
37054d3adbcSTheodore Ts'o 		break;
37154d3adbcSTheodore Ts'o 	case ENOKEY:
37254d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOKEY;
37354d3adbcSTheodore Ts'o 		break;
37454d3adbcSTheodore Ts'o 	case EROFS:
37554d3adbcSTheodore Ts'o 		err = EXT4_ERR_EROFS;
37654d3adbcSTheodore Ts'o 		break;
37754d3adbcSTheodore Ts'o 	case EFBIG:
37854d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFBIG;
37954d3adbcSTheodore Ts'o 		break;
38054d3adbcSTheodore Ts'o 	case EEXIST:
38154d3adbcSTheodore Ts'o 		err = EXT4_ERR_EEXIST;
38254d3adbcSTheodore Ts'o 		break;
38354d3adbcSTheodore Ts'o 	case ERANGE:
38454d3adbcSTheodore Ts'o 		err = EXT4_ERR_ERANGE;
38554d3adbcSTheodore Ts'o 		break;
38654d3adbcSTheodore Ts'o 	case EOVERFLOW:
38754d3adbcSTheodore Ts'o 		err = EXT4_ERR_EOVERFLOW;
38854d3adbcSTheodore Ts'o 		break;
38954d3adbcSTheodore Ts'o 	case EBUSY:
39054d3adbcSTheodore Ts'o 		err = EXT4_ERR_EBUSY;
39154d3adbcSTheodore Ts'o 		break;
39254d3adbcSTheodore Ts'o 	case ENOTDIR:
39354d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOTDIR;
39454d3adbcSTheodore Ts'o 		break;
39554d3adbcSTheodore Ts'o 	case ENOTEMPTY:
39654d3adbcSTheodore Ts'o 		err = EXT4_ERR_ENOTEMPTY;
39754d3adbcSTheodore Ts'o 		break;
39854d3adbcSTheodore Ts'o 	case ESHUTDOWN:
39954d3adbcSTheodore Ts'o 		err = EXT4_ERR_ESHUTDOWN;
40054d3adbcSTheodore Ts'o 		break;
40154d3adbcSTheodore Ts'o 	case EFAULT:
40254d3adbcSTheodore Ts'o 		err = EXT4_ERR_EFAULT;
40354d3adbcSTheodore Ts'o 		break;
40454d3adbcSTheodore Ts'o 	default:
40554d3adbcSTheodore Ts'o 		err = EXT4_ERR_UNKNOWN;
40654d3adbcSTheodore Ts'o 	}
40754d3adbcSTheodore Ts'o 	es->s_last_error_errcode = err;
4081c13d5c0STheodore Ts'o 	if (!es->s_first_error_time) {
4091c13d5c0STheodore Ts'o 		es->s_first_error_time = es->s_last_error_time;
4106a0678a7SArnd Bergmann 		es->s_first_error_time_hi = es->s_last_error_time_hi;
4111c13d5c0STheodore Ts'o 		strncpy(es->s_first_error_func, func,
4121c13d5c0STheodore Ts'o 			sizeof(es->s_first_error_func));
4131c13d5c0STheodore Ts'o 		es->s_first_error_line = cpu_to_le32(line);
4141c13d5c0STheodore Ts'o 		es->s_first_error_ino = es->s_last_error_ino;
4151c13d5c0STheodore Ts'o 		es->s_first_error_block = es->s_last_error_block;
416878520acSTheodore Ts'o 		es->s_first_error_errcode = es->s_last_error_errcode;
4171c13d5c0STheodore Ts'o 	}
41866e61a9eSTheodore Ts'o 	/*
41966e61a9eSTheodore Ts'o 	 * Start the daily error reporting function if it hasn't been
42066e61a9eSTheodore Ts'o 	 * started already
42166e61a9eSTheodore Ts'o 	 */
42266e61a9eSTheodore Ts'o 	if (!es->s_error_count)
42366e61a9eSTheodore Ts'o 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
424ba39ebb6SWei Yongjun 	le32_add_cpu(&es->s_error_count, 1);
4251c13d5c0STheodore Ts'o }
4261c13d5c0STheodore Ts'o 
42754d3adbcSTheodore Ts'o static void save_error_info(struct super_block *sb, int error,
42854d3adbcSTheodore Ts'o 			    __u32 ino, __u64 block,
42954d3adbcSTheodore Ts'o 			    const char *func, unsigned int line)
4301c13d5c0STheodore Ts'o {
43154d3adbcSTheodore Ts'o 	__save_error_info(sb, error, ino, block, func, line);
432c96e2b85SEric Sandeen 	if (!bdev_read_only(sb->s_bdev))
4331c13d5c0STheodore Ts'o 		ext4_commit_super(sb, 1);
4341c13d5c0STheodore Ts'o }
4351c13d5c0STheodore Ts'o 
436bdfe0cbdSTheodore Ts'o /*
437bdfe0cbdSTheodore Ts'o  * The del_gendisk() function uninitializes the disk-specific data
438bdfe0cbdSTheodore Ts'o  * structures, including the bdi structure, without telling anyone
439bdfe0cbdSTheodore Ts'o  * else.  Once this happens, any attempt to call mark_buffer_dirty()
440bdfe0cbdSTheodore Ts'o  * (for example, by ext4_commit_super), will cause a kernel OOPS.
441bdfe0cbdSTheodore Ts'o  * This is a kludge to prevent these oops until we can put in a proper
442bdfe0cbdSTheodore Ts'o  * hook in del_gendisk() to inform the VFS and file system layers.
443bdfe0cbdSTheodore Ts'o  */
444bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb)
445bdfe0cbdSTheodore Ts'o {
446bdfe0cbdSTheodore Ts'o 	struct inode *bd_inode = sb->s_bdev->bd_inode;
447bdfe0cbdSTheodore Ts'o 	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
448bdfe0cbdSTheodore Ts'o 
449bdfe0cbdSTheodore Ts'o 	return bdi->dev == NULL;
450bdfe0cbdSTheodore Ts'o }
451bdfe0cbdSTheodore Ts'o 
45218aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
45318aadd47SBobi Jam {
45418aadd47SBobi Jam 	struct super_block		*sb = journal->j_private;
45518aadd47SBobi Jam 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
45618aadd47SBobi Jam 	int				error = is_journal_aborted(journal);
4575d3ee208SDmitry Monakhov 	struct ext4_journal_cb_entry	*jce;
45818aadd47SBobi Jam 
4595d3ee208SDmitry Monakhov 	BUG_ON(txn->t_state == T_FINISHED);
460a0154344SDaeho Jeong 
461a0154344SDaeho Jeong 	ext4_process_freed_data(sb, txn->t_tid);
462a0154344SDaeho Jeong 
46318aadd47SBobi Jam 	spin_lock(&sbi->s_md_lock);
4645d3ee208SDmitry Monakhov 	while (!list_empty(&txn->t_private_list)) {
4655d3ee208SDmitry Monakhov 		jce = list_entry(txn->t_private_list.next,
4665d3ee208SDmitry Monakhov 				 struct ext4_journal_cb_entry, jce_list);
46718aadd47SBobi Jam 		list_del_init(&jce->jce_list);
46818aadd47SBobi Jam 		spin_unlock(&sbi->s_md_lock);
46918aadd47SBobi Jam 		jce->jce_func(sb, jce, error);
47018aadd47SBobi Jam 		spin_lock(&sbi->s_md_lock);
47118aadd47SBobi Jam 	}
47218aadd47SBobi Jam 	spin_unlock(&sbi->s_md_lock);
47318aadd47SBobi Jam }
4741c13d5c0STheodore Ts'o 
4751dc1097fSJan Kara static bool system_going_down(void)
4761dc1097fSJan Kara {
4771dc1097fSJan Kara 	return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
4781dc1097fSJan Kara 		|| system_state == SYSTEM_RESTART;
4791dc1097fSJan Kara }
4801dc1097fSJan Kara 
481ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as
482ac27a0ecSDave Kleikamp  * inconsistencies detected or read IO failures.
483ac27a0ecSDave Kleikamp  *
484ac27a0ecSDave Kleikamp  * On ext2, we can store the error state of the filesystem in the
485617ba13bSMingming Cao  * superblock.  That is not possible on ext4, because we may have other
486ac27a0ecSDave Kleikamp  * write ordering constraints on the superblock which prevent us from
487ac27a0ecSDave Kleikamp  * writing it out straight away; and given that the journal is about to
488ac27a0ecSDave Kleikamp  * be aborted, we can't rely on the current, or future, transactions to
489ac27a0ecSDave Kleikamp  * write out the superblock safely.
490ac27a0ecSDave Kleikamp  *
491dab291afSMingming Cao  * We'll just use the jbd2_journal_abort() error code to record an error in
492d6b198bcSThadeu Lima de Souza Cascardo  * the journal instead.  On recovery, the journal will complain about
493ac27a0ecSDave Kleikamp  * that error until we've noted it down and cleared it.
494ac27a0ecSDave Kleikamp  */
495ac27a0ecSDave Kleikamp 
496617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb)
497ac27a0ecSDave Kleikamp {
498327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
499327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
500327eaf73STheodore Ts'o 
501bc98a42cSDavid Howells 	if (sb_rdonly(sb))
502ac27a0ecSDave Kleikamp 		return;
503ac27a0ecSDave Kleikamp 
504ac27a0ecSDave Kleikamp 	if (!test_opt(sb, ERRORS_CONT)) {
505617ba13bSMingming Cao 		journal_t *journal = EXT4_SB(sb)->s_journal;
506ac27a0ecSDave Kleikamp 
5074ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
508ac27a0ecSDave Kleikamp 		if (journal)
509dab291afSMingming Cao 			jbd2_journal_abort(journal, -EIO);
510ac27a0ecSDave Kleikamp 	}
5111dc1097fSJan Kara 	/*
5121dc1097fSJan Kara 	 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
5131dc1097fSJan Kara 	 * could panic during 'reboot -f' as the underlying device got already
5141dc1097fSJan Kara 	 * disabled.
5151dc1097fSJan Kara 	 */
5161dc1097fSJan Kara 	if (test_opt(sb, ERRORS_RO) || system_going_down()) {
517b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
5184418e141SDmitry Monakhov 		/*
5194418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
5204418e141SDmitry Monakhov 		 * before ->s_flags update
5214418e141SDmitry Monakhov 		 */
5224418e141SDmitry Monakhov 		smp_wmb();
5231751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
5241dc1097fSJan Kara 	} else if (test_opt(sb, ERRORS_PANIC)) {
525617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
526ac27a0ecSDave Kleikamp 			sb->s_id);
527ac27a0ecSDave Kleikamp 	}
5284327ba52SDaeho Jeong }
529ac27a0ecSDave Kleikamp 
530efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
531efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
532efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
533efbed4dcSTheodore Ts'o 
53412062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
53554d3adbcSTheodore Ts'o 		  unsigned int line, int error, __u64 block,
53654d3adbcSTheodore Ts'o 		  const char *fmt, ...)
537ac27a0ecSDave Kleikamp {
5380ff2ea7dSJoe Perches 	struct va_format vaf;
539ac27a0ecSDave Kleikamp 	va_list args;
540ac27a0ecSDave Kleikamp 
5410db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
5420db1ff22STheodore Ts'o 		return;
5430db1ff22STheodore Ts'o 
544ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
545efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
546ac27a0ecSDave Kleikamp 		va_start(args, fmt);
5470ff2ea7dSJoe Perches 		vaf.fmt = fmt;
5480ff2ea7dSJoe Perches 		vaf.va = &args;
549efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
550efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
5510ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
552ac27a0ecSDave Kleikamp 		va_end(args);
553efbed4dcSTheodore Ts'o 	}
55454d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, block, function, line);
555617ba13bSMingming Cao 	ext4_handle_error(sb);
556ac27a0ecSDave Kleikamp }
557ac27a0ecSDave Kleikamp 
558e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
55954d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
560273df556SFrank Mayhar 			const char *fmt, ...)
561273df556SFrank Mayhar {
562273df556SFrank Mayhar 	va_list args;
563f7c21177STheodore Ts'o 	struct va_format vaf;
564273df556SFrank Mayhar 
5650db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5660db1ff22STheodore Ts'o 		return;
5670db1ff22STheodore Ts'o 
568ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
569efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
570273df556SFrank Mayhar 		va_start(args, fmt);
571f7c21177STheodore Ts'o 		vaf.fmt = fmt;
572f7c21177STheodore Ts'o 		vaf.va = &args;
573c398eda0STheodore Ts'o 		if (block)
574d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
575d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
576d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
577d9ee81daSJoe Perches 			       block, current->comm, &vaf);
578d9ee81daSJoe Perches 		else
579d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
580d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
581d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
582d9ee81daSJoe Perches 			       current->comm, &vaf);
583273df556SFrank Mayhar 		va_end(args);
584efbed4dcSTheodore Ts'o 	}
58554d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, error, inode->i_ino, block,
58654d3adbcSTheodore Ts'o 			function, line);
587273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
588273df556SFrank Mayhar }
589273df556SFrank Mayhar 
590e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
591f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
592f7c21177STheodore Ts'o 		       const char *fmt, ...)
593273df556SFrank Mayhar {
594273df556SFrank Mayhar 	va_list args;
595f7c21177STheodore Ts'o 	struct va_format vaf;
596496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
597273df556SFrank Mayhar 	char pathname[80], *path;
598273df556SFrank Mayhar 
5990db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
6000db1ff22STheodore Ts'o 		return;
6010db1ff22STheodore Ts'o 
602ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
603efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
6049bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
605f9a62d09SDan Carpenter 		if (IS_ERR(path))
606273df556SFrank Mayhar 			path = "(unknown)";
607f7c21177STheodore Ts'o 		va_start(args, fmt);
608f7c21177STheodore Ts'o 		vaf.fmt = fmt;
609f7c21177STheodore Ts'o 		vaf.va = &args;
610d9ee81daSJoe Perches 		if (block)
611d9ee81daSJoe Perches 			printk(KERN_CRIT
612d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
613d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
614d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
615d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
616d9ee81daSJoe Perches 		else
617d9ee81daSJoe Perches 			printk(KERN_CRIT
618d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
619d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
620d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
621d9ee81daSJoe Perches 			       current->comm, path, &vaf);
622273df556SFrank Mayhar 		va_end(args);
623efbed4dcSTheodore Ts'o 	}
62454d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block,
62554d3adbcSTheodore Ts'o 			function, line);
626273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
627273df556SFrank Mayhar }
628273df556SFrank Mayhar 
629722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
630ac27a0ecSDave Kleikamp 			      char nbuf[16])
631ac27a0ecSDave Kleikamp {
632ac27a0ecSDave Kleikamp 	char *errstr = NULL;
633ac27a0ecSDave Kleikamp 
634ac27a0ecSDave Kleikamp 	switch (errno) {
6356a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
6366a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
6376a797d27SDarrick J. Wong 		break;
6386a797d27SDarrick J. Wong 	case -EFSBADCRC:
6396a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
6406a797d27SDarrick J. Wong 		break;
641ac27a0ecSDave Kleikamp 	case -EIO:
642ac27a0ecSDave Kleikamp 		errstr = "IO failure";
643ac27a0ecSDave Kleikamp 		break;
644ac27a0ecSDave Kleikamp 	case -ENOMEM:
645ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
646ac27a0ecSDave Kleikamp 		break;
647ac27a0ecSDave Kleikamp 	case -EROFS:
64878f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
64978f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
650ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
651ac27a0ecSDave Kleikamp 		else
652ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
653ac27a0ecSDave Kleikamp 		break;
654ac27a0ecSDave Kleikamp 	default:
655ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
656ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
657ac27a0ecSDave Kleikamp 		 * NULL. */
658ac27a0ecSDave Kleikamp 		if (nbuf) {
659ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
660ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
661ac27a0ecSDave Kleikamp 				errstr = nbuf;
662ac27a0ecSDave Kleikamp 		}
663ac27a0ecSDave Kleikamp 		break;
664ac27a0ecSDave Kleikamp 	}
665ac27a0ecSDave Kleikamp 
666ac27a0ecSDave Kleikamp 	return errstr;
667ac27a0ecSDave Kleikamp }
668ac27a0ecSDave Kleikamp 
669617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
670ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
671ac27a0ecSDave Kleikamp 
672c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
673c398eda0STheodore Ts'o 		      unsigned int line, int errno)
674ac27a0ecSDave Kleikamp {
675ac27a0ecSDave Kleikamp 	char nbuf[16];
676ac27a0ecSDave Kleikamp 	const char *errstr;
677ac27a0ecSDave Kleikamp 
6780db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6790db1ff22STheodore Ts'o 		return;
6800db1ff22STheodore Ts'o 
681ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
682ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
683ac27a0ecSDave Kleikamp 	 * an error. */
684bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
685ac27a0ecSDave Kleikamp 		return;
686ac27a0ecSDave Kleikamp 
687efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
688617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
689c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
690c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
691efbed4dcSTheodore Ts'o 	}
692ac27a0ecSDave Kleikamp 
69354d3adbcSTheodore Ts'o 	save_error_info(sb, -errno, 0, 0, function, line);
694617ba13bSMingming Cao 	ext4_handle_error(sb);
695ac27a0ecSDave Kleikamp }
696ac27a0ecSDave Kleikamp 
697ac27a0ecSDave Kleikamp /*
698617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
699ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
700ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
701ac27a0ecSDave Kleikamp  *
702ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
703ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
704ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
705ac27a0ecSDave Kleikamp  */
706ac27a0ecSDave Kleikamp 
707c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
70854d3adbcSTheodore Ts'o 		  unsigned int line, int error, const char *fmt, ...)
709ac27a0ecSDave Kleikamp {
710651e1c3bSJoe Perches 	struct va_format vaf;
711ac27a0ecSDave Kleikamp 	va_list args;
712ac27a0ecSDave Kleikamp 
7130db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7140db1ff22STheodore Ts'o 		return;
7150db1ff22STheodore Ts'o 
71654d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, 0, function, line);
717ac27a0ecSDave Kleikamp 	va_start(args, fmt);
718651e1c3bSJoe Perches 	vaf.fmt = fmt;
719651e1c3bSJoe Perches 	vaf.va = &args;
720651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
721651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
722ac27a0ecSDave Kleikamp 	va_end(args);
723ac27a0ecSDave Kleikamp 
724bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
7254ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
7267b97d868Szhangyi (F) 		if (EXT4_SB(sb)->s_journal)
7277b97d868Szhangyi (F) 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
7287b97d868Szhangyi (F) 
7297b97d868Szhangyi (F) 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
7304418e141SDmitry Monakhov 		/*
7314418e141SDmitry Monakhov 		 * Make sure updated value of ->s_mount_flags will be visible
7324418e141SDmitry Monakhov 		 * before ->s_flags update
7334418e141SDmitry Monakhov 		 */
7344418e141SDmitry Monakhov 		smp_wmb();
7351751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
7361c13d5c0STheodore Ts'o 	}
7377b97d868Szhangyi (F) 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down())
7381c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
739ac27a0ecSDave Kleikamp }
740ac27a0ecSDave Kleikamp 
741e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
742e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
743b31e1552SEric Sandeen {
7440ff2ea7dSJoe Perches 	struct va_format vaf;
745b31e1552SEric Sandeen 	va_list args;
746b31e1552SEric Sandeen 
747efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
748efbed4dcSTheodore Ts'o 		return;
749efbed4dcSTheodore Ts'o 
750b31e1552SEric Sandeen 	va_start(args, fmt);
7510ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7520ff2ea7dSJoe Perches 	vaf.va = &args;
7530ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
754b31e1552SEric Sandeen 	va_end(args);
755b31e1552SEric Sandeen }
756b31e1552SEric Sandeen 
757b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
758b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
759b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
760b03a2f7eSAndreas Dilger 
76112062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
762c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
763ac27a0ecSDave Kleikamp {
7640ff2ea7dSJoe Perches 	struct va_format vaf;
765ac27a0ecSDave Kleikamp 	va_list args;
766ac27a0ecSDave Kleikamp 
767b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
768efbed4dcSTheodore Ts'o 		return;
769efbed4dcSTheodore Ts'o 
770ac27a0ecSDave Kleikamp 	va_start(args, fmt);
7710ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7720ff2ea7dSJoe Perches 	vaf.va = &args;
7730ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
7740ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
775ac27a0ecSDave Kleikamp 	va_end(args);
776ac27a0ecSDave Kleikamp }
777ac27a0ecSDave Kleikamp 
778b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
779b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
780b03a2f7eSAndreas Dilger {
781b03a2f7eSAndreas Dilger 	struct va_format vaf;
782b03a2f7eSAndreas Dilger 	va_list args;
783b03a2f7eSAndreas Dilger 
784b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
785b03a2f7eSAndreas Dilger 		return;
786b03a2f7eSAndreas Dilger 
787b03a2f7eSAndreas Dilger 	va_start(args, fmt);
788b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
789b03a2f7eSAndreas Dilger 	vaf.va = &args;
790b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
791b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
792b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
793b03a2f7eSAndreas Dilger 	va_end(args);
794b03a2f7eSAndreas Dilger }
795b03a2f7eSAndreas Dilger 
796e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
797e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
798e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
799e29136f8STheodore Ts'o 			     const char *fmt, ...)
8005d1b1b3fSAneesh Kumar K.V __releases(bitlock)
8015d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
8025d1b1b3fSAneesh Kumar K.V {
8030ff2ea7dSJoe Perches 	struct va_format vaf;
8045d1b1b3fSAneesh Kumar K.V 	va_list args;
8055d1b1b3fSAneesh Kumar K.V 
8060db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8070db1ff22STheodore Ts'o 		return;
8080db1ff22STheodore Ts'o 
809ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
81054d3adbcSTheodore Ts'o 	__save_error_info(sb, EFSCORRUPTED, ino, block, function, line);
8110ff2ea7dSJoe Perches 
812efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
8135d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
8140ff2ea7dSJoe Perches 		vaf.fmt = fmt;
8150ff2ea7dSJoe Perches 		vaf.va = &args;
81621149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
817e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
818e29136f8STheodore Ts'o 		if (ino)
8190ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
820e29136f8STheodore Ts'o 		if (block)
821efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
822efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
8230ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
8245d1b1b3fSAneesh Kumar K.V 		va_end(args);
825efbed4dcSTheodore Ts'o 	}
8265d1b1b3fSAneesh Kumar K.V 
827327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
828327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
829327eaf73STheodore Ts'o 
8305d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
831e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
8325d1b1b3fSAneesh Kumar K.V 		return;
8335d1b1b3fSAneesh Kumar K.V 	}
8341c13d5c0STheodore Ts'o 
8355d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
83606f29cc8SZhouyi Zhou 	ext4_commit_super(sb, 1);
8375d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
8385d1b1b3fSAneesh Kumar K.V 	/*
8395d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
8405d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
8415d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
8425d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
8435d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
84425985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
8455d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
8465d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
8475d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
8485d1b1b3fSAneesh Kumar K.V 	 */
8495d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
8505d1b1b3fSAneesh Kumar K.V 	return;
8515d1b1b3fSAneesh Kumar K.V }
8525d1b1b3fSAneesh Kumar K.V 
853db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
854db79e6d1SWang Shilong 				     ext4_group_t group,
855db79e6d1SWang Shilong 				     unsigned int flags)
856db79e6d1SWang Shilong {
857db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
858db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
859db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
8609af0b3d1SWang Shilong 	int ret;
861db79e6d1SWang Shilong 
8629af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
8639af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
8649af0b3d1SWang Shilong 					    &grp->bb_state);
8659af0b3d1SWang Shilong 		if (!ret)
866db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
867db79e6d1SWang Shilong 					   grp->bb_free);
868db79e6d1SWang Shilong 	}
869db79e6d1SWang Shilong 
8709af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
8719af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
8729af0b3d1SWang Shilong 					    &grp->bb_state);
8739af0b3d1SWang Shilong 		if (!ret && gdp) {
874db79e6d1SWang Shilong 			int count;
875db79e6d1SWang Shilong 
876db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
877db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
878db79e6d1SWang Shilong 					   count);
879db79e6d1SWang Shilong 		}
880db79e6d1SWang Shilong 	}
881db79e6d1SWang Shilong }
882db79e6d1SWang Shilong 
883617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
884ac27a0ecSDave Kleikamp {
885617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
886ac27a0ecSDave Kleikamp 
887617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
888ac27a0ecSDave Kleikamp 		return;
889ac27a0ecSDave Kleikamp 
89012062dddSEric Sandeen 	ext4_warning(sb,
891ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
892ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
893617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
894ac27a0ecSDave Kleikamp 
895617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
896617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
897617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
898ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
899ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
900ac27a0ecSDave Kleikamp 
901ac27a0ecSDave Kleikamp 	/*
902ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
903ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
904ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
905ac27a0ecSDave Kleikamp 	 */
906ac27a0ecSDave Kleikamp }
907ac27a0ecSDave Kleikamp 
908ac27a0ecSDave Kleikamp /*
909ac27a0ecSDave Kleikamp  * Open the external journal device
910ac27a0ecSDave Kleikamp  */
911b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
912ac27a0ecSDave Kleikamp {
913ac27a0ecSDave Kleikamp 	struct block_device *bdev;
914ac27a0ecSDave Kleikamp 
915d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
916ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
917ac27a0ecSDave Kleikamp 		goto fail;
918ac27a0ecSDave Kleikamp 	return bdev;
919ac27a0ecSDave Kleikamp 
920ac27a0ecSDave Kleikamp fail:
921ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
922ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
923ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
924ac27a0ecSDave Kleikamp 	return NULL;
925ac27a0ecSDave Kleikamp }
926ac27a0ecSDave Kleikamp 
927ac27a0ecSDave Kleikamp /*
928ac27a0ecSDave Kleikamp  * Release the journal device
929ac27a0ecSDave Kleikamp  */
9304385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
931ac27a0ecSDave Kleikamp {
9324385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
933ac27a0ecSDave Kleikamp }
934ac27a0ecSDave Kleikamp 
9354385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
936ac27a0ecSDave Kleikamp {
937ac27a0ecSDave Kleikamp 	struct block_device *bdev;
938ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
939ac27a0ecSDave Kleikamp 	if (bdev) {
9404385bab1SAl Viro 		ext4_blkdev_put(bdev);
941ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
942ac27a0ecSDave Kleikamp 	}
943ac27a0ecSDave Kleikamp }
944ac27a0ecSDave Kleikamp 
945ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
946ac27a0ecSDave Kleikamp {
947617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
948ac27a0ecSDave Kleikamp }
949ac27a0ecSDave Kleikamp 
950617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
951ac27a0ecSDave Kleikamp {
952ac27a0ecSDave Kleikamp 	struct list_head *l;
953ac27a0ecSDave Kleikamp 
954b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
955ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
956ac27a0ecSDave Kleikamp 
957ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
958ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
959ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
960ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
961ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
962ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
963ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
964ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
965ac27a0ecSDave Kleikamp 	}
966ac27a0ecSDave Kleikamp }
967ac27a0ecSDave Kleikamp 
968957153fcSJan Kara #ifdef CONFIG_QUOTA
969957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
970957153fcSJan Kara 
971957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
972957153fcSJan Kara {
973957153fcSJan Kara 	int type;
974957153fcSJan Kara 
975957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
976957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
977957153fcSJan Kara 		ext4_quota_off(sb, type);
978957153fcSJan Kara }
97933458eabSTheodore Ts'o 
98033458eabSTheodore Ts'o /*
98133458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
98233458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
98333458eabSTheodore Ts'o  */
98433458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
98533458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
98633458eabSTheodore Ts'o 				int type)
98733458eabSTheodore Ts'o {
98833458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
98933458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
99033458eabSTheodore Ts'o }
991957153fcSJan Kara #else
992957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
993957153fcSJan Kara {
994957153fcSJan Kara }
995957153fcSJan Kara #endif
996957153fcSJan Kara 
997617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
998ac27a0ecSDave Kleikamp {
999617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1000617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
10011d0c3924STheodore Ts'o 	struct buffer_head **group_desc;
10027c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
100397abd7d4STheodore Ts'o 	int aborted = 0;
1004ef2cabf7SHidehiro Kawai 	int i, err;
1005ac27a0ecSDave Kleikamp 
1006857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1007957153fcSJan Kara 	ext4_quota_off_umount(sb);
1008e0ccfd95SChristoph Hellwig 
10092e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
10104c0425ffSMingming Cao 
10115e47868fSRitesh Harjani 	/*
10125e47868fSRitesh Harjani 	 * Unregister sysfs before destroying jbd2 journal.
10135e47868fSRitesh Harjani 	 * Since we could still access attr_journal_task attribute via sysfs
10145e47868fSRitesh Harjani 	 * path which could have sbi->s_journal->j_task as NULL
10155e47868fSRitesh Harjani 	 */
10165e47868fSRitesh Harjani 	ext4_unregister_sysfs(sb);
10175e47868fSRitesh Harjani 
10180390131bSFrank Mayhar 	if (sbi->s_journal) {
101997abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1020ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
102147b4a50bSJan Kara 		sbi->s_journal = NULL;
1022878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
102354d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
10240390131bSFrank Mayhar 		}
1025878520acSTheodore Ts'o 	}
1026d4edac31SJosef Bacik 
1027d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
10289105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1029d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1030d4edac31SJosef Bacik 	ext4_mb_release(sb);
1031d4edac31SJosef Bacik 	ext4_ext_release(sb);
1032d4edac31SJosef Bacik 
1033bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1034e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
1035ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1036ac27a0ecSDave Kleikamp 	}
1037bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
1038a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
1039a8e25a83SArtem Bityutskiy 
10401d0c3924STheodore Ts'o 	rcu_read_lock();
10411d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
1042ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
10431d0c3924STheodore Ts'o 		brelse(group_desc[i]);
10441d0c3924STheodore Ts'o 	kvfree(group_desc);
10457c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
10467c990728SSuraj Jitindar Singh 	if (flex_groups) {
10477c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
10487c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
10497c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
10507c990728SSuraj Jitindar Singh 	}
10511d0c3924STheodore Ts'o 	rcu_read_unlock();
105257042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1053ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1054ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
105557042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1056bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1057ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1058a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
105933458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1060ac27a0ecSDave Kleikamp #endif
1061ac27a0ecSDave Kleikamp 
1062ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1063ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1064ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1065ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1066ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1067ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1068ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
1069ac27a0ecSDave Kleikamp 
107089d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1071f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1072ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
1073ac27a0ecSDave Kleikamp 		/*
1074ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1075ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1076ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1077ac27a0ecSDave Kleikamp 		 */
1078ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
1079f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
1080617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1081ac27a0ecSDave Kleikamp 	}
108250c15df6SChengguang Xu 
1083dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1084dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
108550c15df6SChengguang Xu 
108647387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
108747387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
108850c15df6SChengguang Xu 
1089c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1090c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
10919060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1092ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
10933197ebdbSTheodore Ts'o 	/*
10943197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
10953197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
10963197ebdbSTheodore Ts'o 	 */
10973197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
10983197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
10990441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
11000441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1101705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
11025e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1103ed318a6cSEric Biggers 	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
1104c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1105c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
1106c83ad55eSGabriel Krisman Bertazi #endif
1107ac27a0ecSDave Kleikamp 	kfree(sbi);
1108ac27a0ecSDave Kleikamp }
1109ac27a0ecSDave Kleikamp 
1110e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1111ac27a0ecSDave Kleikamp 
1112ac27a0ecSDave Kleikamp /*
1113ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1114ac27a0ecSDave Kleikamp  */
1115617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1116ac27a0ecSDave Kleikamp {
1117617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1118ac27a0ecSDave Kleikamp 
1119e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1120ac27a0ecSDave Kleikamp 	if (!ei)
1121ac27a0ecSDave Kleikamp 		return NULL;
11220b8e58a1SAndreas Dilger 
1123ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1124202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1125c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
1126c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
11279a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
11289a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1129edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1130eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1131edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1132dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1133d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1134d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
11351dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1136a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1137a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
113896c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1139a9e7f447SDmitry Monakhov #endif
11408aefcd55STheodore Ts'o 	ei->jinode = NULL;
11412e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1142744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1143b436b9beSJan Kara 	ei->i_sync_tid = 0;
1144b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1145e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
11462e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1147ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1148ac27a0ecSDave Kleikamp }
1149ac27a0ecSDave Kleikamp 
11507ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
11517ff9c073STheodore Ts'o {
11527ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
11537ff9c073STheodore Ts'o 
115429b3692eSEric Biggers 	if (!drop)
115529b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
115629b3692eSEric Biggers 
11577ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
11587ff9c073STheodore Ts'o 	return drop;
11597ff9c073STheodore Ts'o }
11607ff9c073STheodore Ts'o 
116194053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1162fa0d7e3dSNick Piggin {
11632c58d548SEric Biggers 	fscrypt_free_inode(inode);
1164fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1165fa0d7e3dSNick Piggin }
1166fa0d7e3dSNick Piggin 
1167617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1168ac27a0ecSDave Kleikamp {
11699f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1170b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1171b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1172b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
11739f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
11749f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
11759f7dd93dSVasily Averin 				true);
11769f7dd93dSVasily Averin 		dump_stack();
11779f7dd93dSVasily Averin 	}
1178ac27a0ecSDave Kleikamp }
1179ac27a0ecSDave Kleikamp 
118051cc5068SAlexey Dobriyan static void init_once(void *foo)
1181ac27a0ecSDave Kleikamp {
1182617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1183ac27a0ecSDave Kleikamp 
1184ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1185ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
11860e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1187ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1188ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1189ac27a0ecSDave Kleikamp }
1190ac27a0ecSDave Kleikamp 
1191e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1192ac27a0ecSDave Kleikamp {
1193f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1194f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1195f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1196f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1197f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1198f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
119920c2df83SPaul Mundt 				init_once);
1200617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1201ac27a0ecSDave Kleikamp 		return -ENOMEM;
1202ac27a0ecSDave Kleikamp 	return 0;
1203ac27a0ecSDave Kleikamp }
1204ac27a0ecSDave Kleikamp 
1205ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1206ac27a0ecSDave Kleikamp {
12078c0a8537SKirill A. Shutemov 	/*
12088c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
12098c0a8537SKirill A. Shutemov 	 * destroy cache.
12108c0a8537SKirill A. Shutemov 	 */
12118c0a8537SKirill A. Shutemov 	rcu_barrier();
1212617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1213ac27a0ecSDave Kleikamp }
1214ac27a0ecSDave Kleikamp 
12150930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1216ac27a0ecSDave Kleikamp {
12170930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1218dbd5768fSJan Kara 	clear_inode(inode);
1219c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
122051865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1221f4c2d372SJan Kara 	dquot_drop(inode);
12228aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
12238aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
12248aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
12258aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
12268aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
12278aefcd55STheodore Ts'o 	}
12283d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1229c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1230ac27a0ecSDave Kleikamp }
1231ac27a0ecSDave Kleikamp 
12321b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
12331b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1234ac27a0ecSDave Kleikamp {
1235ac27a0ecSDave Kleikamp 	struct inode *inode;
1236ac27a0ecSDave Kleikamp 
12378a363970STheodore Ts'o 	/*
1238ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1239ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1240ac27a0ecSDave Kleikamp 	 */
12418a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
12421d1fe1eeSDavid Howells 	if (IS_ERR(inode))
12431d1fe1eeSDavid Howells 		return ERR_CAST(inode);
12441d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1245ac27a0ecSDave Kleikamp 		iput(inode);
1246ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1247ac27a0ecSDave Kleikamp 	}
12481b961ac0SChristoph Hellwig 
12491b961ac0SChristoph Hellwig 	return inode;
1250ac27a0ecSDave Kleikamp }
12511b961ac0SChristoph Hellwig 
12521b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
12531b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12541b961ac0SChristoph Hellwig {
12551b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
12561b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
12571b961ac0SChristoph Hellwig }
12581b961ac0SChristoph Hellwig 
12591b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
12601b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12611b961ac0SChristoph Hellwig {
12621b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
12631b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1264ac27a0ecSDave Kleikamp }
1265ac27a0ecSDave Kleikamp 
1266fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1267fde87268STheodore Ts'o {
1268fde87268STheodore Ts'o 	struct writeback_control wbc = {
1269fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1270fde87268STheodore Ts'o 	};
1271fde87268STheodore Ts'o 
1272fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1273fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1274fde87268STheodore Ts'o }
1275fde87268STheodore Ts'o 
1276c39a7f84SToshiyuki Okajima /*
1277c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1278c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1279c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1280c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1281c39a7f84SToshiyuki Okajima  */
12820b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
12830b8e58a1SAndreas Dilger 				 gfp_t wait)
1284c39a7f84SToshiyuki Okajima {
1285c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1286c39a7f84SToshiyuki Okajima 
1287c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1288c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1289c39a7f84SToshiyuki Okajima 		return 0;
1290c39a7f84SToshiyuki Okajima 	if (journal)
1291c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1292d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1293c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1294c39a7f84SToshiyuki Okajima }
1295c39a7f84SToshiyuki Okajima 
1296643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1297a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1298a7550b30SJaegeuk Kim {
1299a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1300a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1301a7550b30SJaegeuk Kim }
1302a7550b30SJaegeuk Kim 
1303a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1304a7550b30SJaegeuk Kim 							void *fs_data)
1305a7550b30SJaegeuk Kim {
13062f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1307c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1308a7550b30SJaegeuk Kim 
13099ce0151aSEric Biggers 	/*
13109ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
13119ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
13129ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
13139ce0151aSEric Biggers 	 * the filename "lost+found" itself.
13149ce0151aSEric Biggers 	 */
13159ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
13169ce0151aSEric Biggers 		return -EPERM;
1317a7550b30SJaegeuk Kim 
13187d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
13197d3e06a8SRoss Zwisler 		return -EINVAL;
13207d3e06a8SRoss Zwisler 
1321b383a73fSIra Weiny 	if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1322b383a73fSIra Weiny 		return -EOPNOTSUPP;
1323b383a73fSIra Weiny 
132494840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
132594840e3cSEric Biggers 	if (res)
132694840e3cSEric Biggers 		return res;
132794840e3cSEric Biggers 
13282f8f5e76SEric Biggers 	/*
13292f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
13302f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
13312f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
13322f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
13332f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
13342f8f5e76SEric Biggers 	 */
13352f8f5e76SEric Biggers 
13362f8f5e76SEric Biggers 	if (handle) {
13372f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
13382f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
13392f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13402f8f5e76SEric Biggers 					    ctx, len, 0);
1341a7550b30SJaegeuk Kim 		if (!res) {
1342a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1343a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1344a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1345a3caa24bSJan Kara 			/*
13462ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13472ee6a576SEric Biggers 			 * S_DAX may be disabled
1348a3caa24bSJan Kara 			 */
1349043546e4SIra Weiny 			ext4_set_inode_flags(inode, false);
1350a7550b30SJaegeuk Kim 		}
1351a7550b30SJaegeuk Kim 		return res;
1352a7550b30SJaegeuk Kim 	}
1353a7550b30SJaegeuk Kim 
1354b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1355b8cb5a54STahsin Erdogan 	if (res)
1356b8cb5a54STahsin Erdogan 		return res;
13572f8f5e76SEric Biggers retry:
1358af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1359af65207cSTahsin Erdogan 				     &credits);
1360dec214d0STahsin Erdogan 	if (res)
1361dec214d0STahsin Erdogan 		return res;
1362dec214d0STahsin Erdogan 
1363c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1364a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1365a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1366a7550b30SJaegeuk Kim 
13672f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
13682f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13692f8f5e76SEric Biggers 				    ctx, len, 0);
1370a7550b30SJaegeuk Kim 	if (!res) {
1371a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
13722ee6a576SEric Biggers 		/*
13732ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13742ee6a576SEric Biggers 		 * S_DAX may be disabled
13752ee6a576SEric Biggers 		 */
1376043546e4SIra Weiny 		ext4_set_inode_flags(inode, false);
1377a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1378a7550b30SJaegeuk Kim 		if (res)
1379a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1380a7550b30SJaegeuk Kim 	}
1381a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
13822f8f5e76SEric Biggers 
13832f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
13842f8f5e76SEric Biggers 		goto retry;
1385a7550b30SJaegeuk Kim 	if (!res)
1386a7550b30SJaegeuk Kim 		res = res2;
1387a7550b30SJaegeuk Kim 	return res;
1388a7550b30SJaegeuk Kim }
1389a7550b30SJaegeuk Kim 
1390ed318a6cSEric Biggers static const union fscrypt_context *
1391ed318a6cSEric Biggers ext4_get_dummy_context(struct super_block *sb)
1392a7550b30SJaegeuk Kim {
1393ed318a6cSEric Biggers 	return EXT4_SB(sb)->s_dummy_enc_ctx.ctx;
1394a7550b30SJaegeuk Kim }
1395a7550b30SJaegeuk Kim 
1396b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1397b925acb8SEric Biggers {
1398b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1399b925acb8SEric Biggers }
1400b925acb8SEric Biggers 
1401b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1402b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1403b925acb8SEric Biggers {
1404b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1405b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1406b925acb8SEric Biggers }
1407b925acb8SEric Biggers 
14086f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1409a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1410a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1411a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1412ed318a6cSEric Biggers 	.get_dummy_context	= ext4_get_dummy_context,
1413a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1414e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1415b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1416b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1417a7550b30SJaegeuk Kim };
1418a7550b30SJaegeuk Kim #endif
1419a7550b30SJaegeuk Kim 
1420ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1421d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1422689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1423ac27a0ecSDave Kleikamp 
1424617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1425617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1426617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1427617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1428617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
14296f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
14308c54ca9cSAl Viro 			 const struct path *path);
1431617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1432617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1433ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1434617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1435ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
14367c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
14377c319d32SAditya Kali 			     unsigned int flags);
14387c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1439ac27a0ecSDave Kleikamp 
144096c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
144196c7e0d9SJan Kara {
144296c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
144396c7e0d9SJan Kara }
144496c7e0d9SJan Kara 
144561e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
144660e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1447617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1448617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1449617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1450617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1451a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1452a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1453a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1454040cb378SLi Xi 	.get_projid		= ext4_get_projid,
14557a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1456ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1457ac27a0ecSDave Kleikamp };
1458ac27a0ecSDave Kleikamp 
14590d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1460617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1461ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1462287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
14630a240339SJan Kara 	.get_state	= dquot_get_state,
1464287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1465287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
14666332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
14676332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1468ac27a0ecSDave Kleikamp };
1469ac27a0ecSDave Kleikamp #endif
1470ac27a0ecSDave Kleikamp 
1471ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1472617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
147394053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1474617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1475617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1476617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
14777ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
14780930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1479617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1480617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1481c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1482c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1483617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1484617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1485617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1486ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1487617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1488617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
148996c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1490ac27a0ecSDave Kleikamp #endif
1491c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1492ac27a0ecSDave Kleikamp };
1493ac27a0ecSDave Kleikamp 
149439655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
14951b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
14961b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1497617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1498fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1499ac27a0ecSDave Kleikamp };
1500ac27a0ecSDave Kleikamp 
1501ac27a0ecSDave Kleikamp enum {
1502ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1503ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
150472578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1505ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
150672578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1507ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1508ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1509ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
15106ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1511*4f74d15fSEric Biggers 	Opt_inlinecrypt,
1512ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
15135a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1514ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
15159cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
15169cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1517327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1518327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1519670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
15201449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
15215328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1522744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1523fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1524cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1525ac27a0ecSDave Kleikamp };
1526ac27a0ecSDave Kleikamp 
1527a447c093SSteven Whitehouse static const match_table_t tokens = {
1528ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1529ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1530ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1531ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1532ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1533ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1534ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1535ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1536ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1537ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1538ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1539ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1540ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1541ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
154272578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
154372578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1544ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1545ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1546ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1547ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1548e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
15495a916be1STheodore Ts'o 	{Opt_noload, "noload"},
155072578c33STheodore Ts'o 	{Opt_removed, "nobh"},
155172578c33STheodore Ts'o 	{Opt_removed, "bh"},
1552ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
155330773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
155430773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1555ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1556ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1557818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1558c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1559818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1560ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1561ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1562ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1563ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
15645bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
15655bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1566ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1567ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1568ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1569ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1570ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1571ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15725a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1573ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1574ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1575ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1576ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
157749da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1578ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
157906705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
158006705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
158125ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1582923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
15839cb20f94SIra Weiny 	{Opt_dax_always, "dax=always"},
15849cb20f94SIra Weiny 	{Opt_dax_inode, "dax=inode"},
15859cb20f94SIra Weiny 	{Opt_dax_never, "dax=never"},
1586c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
158764769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1588327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1589327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1590a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1591a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1592670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1593dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
159436ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
159536ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15966fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15976fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1598240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1599b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1600afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
160106705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
160206705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1603744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1604244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1605744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
16065328e635SEric Sandeen 	{Opt_discard, "discard"},
16075328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1608fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1609fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1610fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1611df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1612ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
16136ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1614*4f74d15fSEric Biggers 	{Opt_inlinecrypt, "inlinecrypt"},
1615cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1616cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1617c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1618c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1619c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1620c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1621c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1622f3f12faaSJosef Bacik 	{Opt_err, NULL},
1623ac27a0ecSDave Kleikamp };
1624ac27a0ecSDave Kleikamp 
1625617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1626ac27a0ecSDave Kleikamp {
1627617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1628ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1629ac27a0ecSDave Kleikamp 
1630ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1631ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
16320b8e58a1SAndreas Dilger 
1633ac27a0ecSDave Kleikamp 	options += 3;
16340b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1635ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1636ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
16374776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1638ac27a0ecSDave Kleikamp 		       (char *) *data);
1639ac27a0ecSDave Kleikamp 		return 1;
1640ac27a0ecSDave Kleikamp 	}
1641ac27a0ecSDave Kleikamp 	if (*options == ',')
1642ac27a0ecSDave Kleikamp 		options++;
1643ac27a0ecSDave Kleikamp 	*data = (void *) options;
16440b8e58a1SAndreas Dilger 
1645ac27a0ecSDave Kleikamp 	return sb_block;
1646ac27a0ecSDave Kleikamp }
1647ac27a0ecSDave Kleikamp 
1648b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1649d6006186SEric Biggers static const char deprecated_msg[] =
1650d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1651437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1652b3881f74STheodore Ts'o 
165356c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
165456c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
165556c50f11SDmitry Monakhov {
165656c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
165733458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
165803dafb5fSChen Gang 	int ret = -1;
165956c50f11SDmitry Monakhov 
166033458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
166156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
166256c50f11SDmitry Monakhov 			"Cannot change journaled "
166356c50f11SDmitry Monakhov 			"quota options when quota turned on");
166457f73c2cSTheodore Ts'o 		return -1;
166556c50f11SDmitry Monakhov 	}
1666e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1667c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1668c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1669c325a67cSTheodore Ts'o 		return 1;
1670262b4662SJan Kara 	}
167156c50f11SDmitry Monakhov 	qname = match_strdup(args);
167256c50f11SDmitry Monakhov 	if (!qname) {
167356c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
167456c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
167557f73c2cSTheodore Ts'o 		return -1;
167656c50f11SDmitry Monakhov 	}
167733458eabSTheodore Ts'o 	if (old_qname) {
167833458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
167903dafb5fSChen Gang 			ret = 1;
168003dafb5fSChen Gang 		else
168156c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
168203dafb5fSChen Gang 				 "%s quota file already specified",
168303dafb5fSChen Gang 				 QTYPE2NAME(qtype));
168403dafb5fSChen Gang 		goto errout;
168556c50f11SDmitry Monakhov 	}
168603dafb5fSChen Gang 	if (strchr(qname, '/')) {
168756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
168856c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
168903dafb5fSChen Gang 		goto errout;
169056c50f11SDmitry Monakhov 	}
169133458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1692fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
169356c50f11SDmitry Monakhov 	return 1;
169403dafb5fSChen Gang errout:
169503dafb5fSChen Gang 	kfree(qname);
169603dafb5fSChen Gang 	return ret;
169756c50f11SDmitry Monakhov }
169856c50f11SDmitry Monakhov 
169956c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
170056c50f11SDmitry Monakhov {
170156c50f11SDmitry Monakhov 
170256c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
170333458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
170456c50f11SDmitry Monakhov 
170533458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
170656c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
170756c50f11SDmitry Monakhov 			" when quota turned on");
170857f73c2cSTheodore Ts'o 		return -1;
170956c50f11SDmitry Monakhov 	}
171033458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
171133458eabSTheodore Ts'o 	synchronize_rcu();
171233458eabSTheodore Ts'o 	kfree(old_qname);
171356c50f11SDmitry Monakhov 	return 1;
171456c50f11SDmitry Monakhov }
171556c50f11SDmitry Monakhov #endif
171656c50f11SDmitry Monakhov 
171726092bf5STheodore Ts'o #define MOPT_SET	0x0001
171826092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
171926092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
172026092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
172126092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
172226092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
172326092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
172426092bf5STheodore Ts'o #define MOPT_Q		0
172526092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
172626092bf5STheodore Ts'o #else
172726092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
172826092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
172926092bf5STheodore Ts'o #endif
173026092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
17318dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
17328dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
17338dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1734ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
17359cb20f94SIra Weiny #define MOPT_SKIP	0x0800
173626092bf5STheodore Ts'o 
173726092bf5STheodore Ts'o static const struct mount_opts {
173826092bf5STheodore Ts'o 	int	token;
173926092bf5STheodore Ts'o 	int	mount_opt;
174026092bf5STheodore Ts'o 	int	flags;
174126092bf5STheodore Ts'o } ext4_mount_opts[] = {
174226092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
174326092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
174426092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
174526092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
174626092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
174726092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
17488dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
17498dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
17508dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
17518dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
175226092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
175326092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
17548dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
17558dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17568dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
175759d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1758327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1759327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1760c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1761c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
17628dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
17631e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
176426092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
17658dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
17661e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17678dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
176826092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
176926092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
177026092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17718dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17727915a861SAles Novak 	 MOPT_NO_EXT2},
17738dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17747915a861SAles Novak 	 MOPT_NO_EXT2},
177526092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
177626092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
177726092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
177826092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
177926092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
178026092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
178126092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
178226092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
178326092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
178426092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
17859cb20f94SIra Weiny 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
17869cb20f94SIra Weiny 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
17879cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
17889cb20f94SIra Weiny 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
17899cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
17909cb20f94SIra Weiny 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
17919cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
179226092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17930efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17940efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17955ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17965ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17975ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17988dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17998dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
18008dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
18018dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
180226092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
180326092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
180426092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
180526092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
180626092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
180726092bf5STheodore Ts'o #else
180826092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
180926092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
181026092bf5STheodore Ts'o #endif
181126092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
181226092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1813670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
181426092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
181526092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
181626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
181726092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
181826092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
181949da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
182049da9392SJan Kara 							MOPT_SET | MOPT_Q},
182126092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
182249da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
182349da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
182426092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
182526092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
182626092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
182726092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
182826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
182926092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
183026092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1831df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
1832ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, 0, MOPT_STRING},
1833cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
183426092bf5STheodore Ts'o 	{Opt_err, 0, 0}
183526092bf5STheodore Ts'o };
183626092bf5STheodore Ts'o 
1837c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1838c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1839c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1840c83ad55eSGabriel Krisman Bertazi 	char *name;
1841c83ad55eSGabriel Krisman Bertazi 	char *version;
1842c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1843c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1844c83ad55eSGabriel Krisman Bertazi };
1845c83ad55eSGabriel Krisman Bertazi 
1846c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1847c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1848c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1849c83ad55eSGabriel Krisman Bertazi {
1850c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1851c83ad55eSGabriel Krisman Bertazi 	int i;
1852c83ad55eSGabriel Krisman Bertazi 
1853c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1854c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1855c83ad55eSGabriel Krisman Bertazi 			break;
1856c83ad55eSGabriel Krisman Bertazi 
1857c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1858c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1859c83ad55eSGabriel Krisman Bertazi 
1860c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1861c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1862c83ad55eSGabriel Krisman Bertazi 
1863c83ad55eSGabriel Krisman Bertazi 	return 0;
1864c83ad55eSGabriel Krisman Bertazi }
1865c83ad55eSGabriel Krisman Bertazi #endif
1866c83ad55eSGabriel Krisman Bertazi 
1867ed318a6cSEric Biggers static int ext4_set_test_dummy_encryption(struct super_block *sb,
1868ed318a6cSEric Biggers 					  const char *opt,
1869ed318a6cSEric Biggers 					  const substring_t *arg,
1870ed318a6cSEric Biggers 					  bool is_remount)
1871ed318a6cSEric Biggers {
1872ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
1873ed318a6cSEric Biggers 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1874ed318a6cSEric Biggers 	int err;
1875ed318a6cSEric Biggers 
1876ed318a6cSEric Biggers 	/*
1877ed318a6cSEric Biggers 	 * This mount option is just for testing, and it's not worthwhile to
1878ed318a6cSEric Biggers 	 * implement the extra complexity (e.g. RCU protection) that would be
1879ed318a6cSEric Biggers 	 * needed to allow it to be set or changed during remount.  We do allow
1880ed318a6cSEric Biggers 	 * it to be specified during remount, but only if there is no change.
1881ed318a6cSEric Biggers 	 */
1882ed318a6cSEric Biggers 	if (is_remount && !sbi->s_dummy_enc_ctx.ctx) {
1883ed318a6cSEric Biggers 		ext4_msg(sb, KERN_WARNING,
1884ed318a6cSEric Biggers 			 "Can't set test_dummy_encryption on remount");
1885ed318a6cSEric Biggers 		return -1;
1886ed318a6cSEric Biggers 	}
1887ed318a6cSEric Biggers 	err = fscrypt_set_test_dummy_encryption(sb, arg, &sbi->s_dummy_enc_ctx);
1888ed318a6cSEric Biggers 	if (err) {
1889ed318a6cSEric Biggers 		if (err == -EEXIST)
1890ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1891ed318a6cSEric Biggers 				 "Can't change test_dummy_encryption on remount");
1892ed318a6cSEric Biggers 		else if (err == -EINVAL)
1893ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1894ed318a6cSEric Biggers 				 "Value of option \"%s\" is unrecognized", opt);
1895ed318a6cSEric Biggers 		else
1896ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1897ed318a6cSEric Biggers 				 "Error processing option \"%s\" [%d]",
1898ed318a6cSEric Biggers 				 opt, err);
1899ed318a6cSEric Biggers 		return -1;
1900ed318a6cSEric Biggers 	}
1901ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
1902ed318a6cSEric Biggers #else
1903ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING,
1904ed318a6cSEric Biggers 		 "Test dummy encryption mount option ignored");
1905ed318a6cSEric Biggers #endif
1906ed318a6cSEric Biggers 	return 1;
1907ed318a6cSEric Biggers }
1908ed318a6cSEric Biggers 
190926092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
191026092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
191126092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
191226092bf5STheodore Ts'o {
191326092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
191426092bf5STheodore Ts'o 	const struct mount_opts *m;
191508cefc7aSEric W. Biederman 	kuid_t uid;
191608cefc7aSEric W. Biederman 	kgid_t gid;
191726092bf5STheodore Ts'o 	int arg = 0;
191826092bf5STheodore Ts'o 
191957f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
192057f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
192157f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
192257f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
192357f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
192457f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
192557f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
192657f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
192757f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
192857f73c2cSTheodore Ts'o #endif
192926092bf5STheodore Ts'o 	switch (token) {
1930f7048605STheodore Ts'o 	case Opt_noacl:
1931f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1932f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1933f7048605STheodore Ts'o 		break;
193426092bf5STheodore Ts'o 	case Opt_sb:
193526092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
193626092bf5STheodore Ts'o 	case Opt_removed:
19375f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
193826092bf5STheodore Ts'o 		return 1;
193926092bf5STheodore Ts'o 	case Opt_abort:
194026092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
194126092bf5STheodore Ts'o 		return 1;
194226092bf5STheodore Ts'o 	case Opt_i_version:
1943357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
194426092bf5STheodore Ts'o 		return 1;
1945a26f4992STheodore Ts'o 	case Opt_lazytime:
19461751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1947a26f4992STheodore Ts'o 		return 1;
1948a26f4992STheodore Ts'o 	case Opt_nolazytime:
19491751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1950a26f4992STheodore Ts'o 		return 1;
1951*4f74d15fSEric Biggers 	case Opt_inlinecrypt:
1952*4f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1953*4f74d15fSEric Biggers 		sb->s_flags |= SB_INLINECRYPT;
1954*4f74d15fSEric Biggers #else
1955*4f74d15fSEric Biggers 		ext4_msg(sb, KERN_ERR, "inline encryption not supported");
1956*4f74d15fSEric Biggers #endif
1957*4f74d15fSEric Biggers 		return 1;
195826092bf5STheodore Ts'o 	}
195926092bf5STheodore Ts'o 
19605f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
19615f3633e3SJan Kara 		if (token == m->token)
19625f3633e3SJan Kara 			break;
19635f3633e3SJan Kara 
19645f3633e3SJan Kara 	if (m->token == Opt_err) {
19655f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
19665f3633e3SJan Kara 			 "or missing value", opt);
19675f3633e3SJan Kara 		return -1;
19685f3633e3SJan Kara 	}
19695f3633e3SJan Kara 
19708dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
19718dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19728dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
19738dc0aa8cSTheodore Ts'o 		return -1;
19748dc0aa8cSTheodore Ts'o 	}
19758dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
19768dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19778dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
19788dc0aa8cSTheodore Ts'o 		return -1;
19798dc0aa8cSTheodore Ts'o 	}
19808dc0aa8cSTheodore Ts'o 
1981ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
19820efb3b23SJan Kara 		return -1;
198326092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
198426092bf5STheodore Ts'o 		return -1;
1985c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1986c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
198726092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
19881e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
19891e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1990c93cf2d7SDmitry Monakhov 		} else
1991c93cf2d7SDmitry Monakhov 			return -1;
1992c93cf2d7SDmitry Monakhov 	}
199326092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
199426092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
199526092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
199626092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
199726092bf5STheodore Ts'o 			 "options when quota turned on");
199826092bf5STheodore Ts'o 		return -1;
199926092bf5STheodore Ts'o 	}
200026092bf5STheodore Ts'o 
200126092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
200226092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
200326092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
200426092bf5STheodore Ts'o 		if (arg == 0)
200526092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
20069ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
20079ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
20089ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
20099ba55543Szhangyi (F) 				 "must be smaller than %d",
20109ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
20119ba55543Szhangyi (F) 			return -1;
20129ba55543Szhangyi (F) 		}
201326092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
2014670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
20159803387cSTheodore Ts'o 		if ((arg & 1) ||
20169803387cSTheodore Ts'o 		    (arg < 4) ||
20179803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
20189803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
20199803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
20209803387cSTheodore Ts'o 			return -1;
20219803387cSTheodore Ts'o 		}
2022670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
202326092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
202426092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
202526092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
202626092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
202726092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
2028e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2029e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
2030e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2031e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
203226092bf5STheodore Ts'o 			return -1;
203326092bf5STheodore Ts'o 		}
203426092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
203526092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
203626092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
203726092bf5STheodore Ts'o 		if (!args->from)
203826092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
203926092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
2040df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
2041df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
204226092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
204326092bf5STheodore Ts'o 		sbi->s_stripe = arg;
20440efb3b23SJan Kara 	} else if (token == Opt_resuid) {
20450efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
20460efb3b23SJan Kara 		if (!uid_valid(uid)) {
20475f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
20480efb3b23SJan Kara 			return -1;
20490efb3b23SJan Kara 		}
20500efb3b23SJan Kara 		sbi->s_resuid = uid;
20510efb3b23SJan Kara 	} else if (token == Opt_resgid) {
20520efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
20530efb3b23SJan Kara 		if (!gid_valid(gid)) {
20545f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
20550efb3b23SJan Kara 			return -1;
20560efb3b23SJan Kara 		}
20570efb3b23SJan Kara 		sbi->s_resgid = gid;
20580efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
20590efb3b23SJan Kara 		if (is_remount) {
20600efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
20610efb3b23SJan Kara 				 "Cannot specify journal on remount");
20620efb3b23SJan Kara 			return -1;
20630efb3b23SJan Kara 		}
20640efb3b23SJan Kara 		*journal_devnum = arg;
2065ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2066ad4eec61SEric Sandeen 		char *journal_path;
2067ad4eec61SEric Sandeen 		struct inode *journal_inode;
2068ad4eec61SEric Sandeen 		struct path path;
2069ad4eec61SEric Sandeen 		int error;
2070ad4eec61SEric Sandeen 
2071ad4eec61SEric Sandeen 		if (is_remount) {
2072ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2073ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2074ad4eec61SEric Sandeen 			return -1;
2075ad4eec61SEric Sandeen 		}
2076ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2077ad4eec61SEric Sandeen 		if (!journal_path) {
2078ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2079ad4eec61SEric Sandeen 				"journal device string");
2080ad4eec61SEric Sandeen 			return -1;
2081ad4eec61SEric Sandeen 		}
2082ad4eec61SEric Sandeen 
2083ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2084ad4eec61SEric Sandeen 		if (error) {
2085ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2086ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2087ad4eec61SEric Sandeen 			kfree(journal_path);
2088ad4eec61SEric Sandeen 			return -1;
2089ad4eec61SEric Sandeen 		}
2090ad4eec61SEric Sandeen 
20912b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2092ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2093ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2094ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2095ad4eec61SEric Sandeen 			path_put(&path);
2096ad4eec61SEric Sandeen 			kfree(journal_path);
2097ad4eec61SEric Sandeen 			return -1;
2098ad4eec61SEric Sandeen 		}
2099ad4eec61SEric Sandeen 
2100ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2101ad4eec61SEric Sandeen 		path_put(&path);
2102ad4eec61SEric Sandeen 		kfree(journal_path);
21030efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
21040efb3b23SJan Kara 		if (arg > 7) {
21055f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
21060efb3b23SJan Kara 				 " (must be 0-7)");
21070efb3b23SJan Kara 			return -1;
21080efb3b23SJan Kara 		}
21090efb3b23SJan Kara 		*journal_ioprio =
21100efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
21116ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2112ed318a6cSEric Biggers 		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2113ed318a6cSEric Biggers 						      is_remount);
211426092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
211526092bf5STheodore Ts'o 		if (is_remount) {
211626092bf5STheodore Ts'o 			if (!sbi->s_journal)
211726092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
21185f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
211926092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
212026092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
212126092bf5STheodore Ts'o 				return -1;
212226092bf5STheodore Ts'o 			}
212326092bf5STheodore Ts'o 		} else {
212426092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
212526092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
212626092bf5STheodore Ts'o 		}
212726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
212826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
212926092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
213026092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
21315f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
21325f3633e3SJan Kara 				 "quota options when quota turned on");
213326092bf5STheodore Ts'o 			return -1;
213426092bf5STheodore Ts'o 		}
2135e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2136c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2137c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2138262b4662SJan Kara 				 "when QUOTA feature is enabled");
2139c325a67cSTheodore Ts'o 			return 1;
2140262b4662SJan Kara 		}
214126092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
214226092bf5STheodore Ts'o #endif
21439cb20f94SIra Weiny 	} else if (token == Opt_dax || token == Opt_dax_always ||
21449cb20f94SIra Weiny 		   token == Opt_dax_inode || token == Opt_dax_never) {
2145ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
21469cb20f94SIra Weiny 		switch (token) {
21479cb20f94SIra Weiny 		case Opt_dax:
21489cb20f94SIra Weiny 		case Opt_dax_always:
2149829b37b8STheodore Ts'o 			if (is_remount &&
2150829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2151829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2152829b37b8STheodore Ts'o 			fail_dax_change_remount:
2153829b37b8STheodore Ts'o 				ext4_msg(sb, KERN_ERR, "can't change "
2154829b37b8STheodore Ts'o 					 "dax mount option while remounting");
2155829b37b8STheodore Ts'o 				return -1;
2156829b37b8STheodore Ts'o 			}
2157829b37b8STheodore Ts'o 			if (is_remount &&
2158829b37b8STheodore Ts'o 			    (test_opt(sb, DATA_FLAGS) ==
2159829b37b8STheodore Ts'o 			     EXT4_MOUNT_JOURNAL_DATA)) {
2160829b37b8STheodore Ts'o 				    ext4_msg(sb, KERN_ERR, "can't mount with "
2161829b37b8STheodore Ts'o 					     "both data=journal and dax");
2162829b37b8STheodore Ts'o 				    return -1;
2163829b37b8STheodore Ts'o 			}
2164ef83b6e8SDan Williams 			ext4_msg(sb, KERN_WARNING,
2165ef83b6e8SDan Williams 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
21669cb20f94SIra Weiny 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
21679cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
21689cb20f94SIra Weiny 			break;
21699cb20f94SIra Weiny 		case Opt_dax_never:
2170829b37b8STheodore Ts'o 			if (is_remount &&
2171829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2172829b37b8STheodore Ts'o 			     (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2173829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
21749cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
21759cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
21769cb20f94SIra Weiny 			break;
21779cb20f94SIra Weiny 		case Opt_dax_inode:
2178829b37b8STheodore Ts'o 			if (is_remount &&
2179829b37b8STheodore Ts'o 			    ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2180829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2181829b37b8STheodore Ts'o 			     !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2182829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
21839cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
21849cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
21859cb20f94SIra Weiny 			/* Strictly for printing options */
21869cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
21879cb20f94SIra Weiny 			break;
21889cb20f94SIra Weiny 		}
2189ef83b6e8SDan Williams #else
2190923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
21919cb20f94SIra Weiny 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
21929cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2193923ae0ffSRoss Zwisler 		return -1;
2194923ae0ffSRoss Zwisler #endif
21957915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
21967915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
21977915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
21987915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
219926092bf5STheodore Ts'o 	} else {
220026092bf5STheodore Ts'o 		if (!args->from)
220126092bf5STheodore Ts'o 			arg = 1;
220226092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
220326092bf5STheodore Ts'o 			arg = !arg;
220426092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
220526092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
220626092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
220726092bf5STheodore Ts'o 			WARN_ON(1);
220826092bf5STheodore Ts'o 			return -1;
220926092bf5STheodore Ts'o 		}
221026092bf5STheodore Ts'o 		if (arg != 0)
221126092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
221226092bf5STheodore Ts'o 		else
221326092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
221426092bf5STheodore Ts'o 	}
221526092bf5STheodore Ts'o 	return 1;
221626092bf5STheodore Ts'o }
221726092bf5STheodore Ts'o 
2218ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2219c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2220b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2221661aa520SEric Sandeen 			 int is_remount)
2222ac27a0ecSDave Kleikamp {
22231e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
222433458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2225ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
222626092bf5STheodore Ts'o 	int token;
2227ac27a0ecSDave Kleikamp 
2228ac27a0ecSDave Kleikamp 	if (!options)
2229ac27a0ecSDave Kleikamp 		return 1;
2230ac27a0ecSDave Kleikamp 
2231ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2232ac27a0ecSDave Kleikamp 		if (!*p)
2233ac27a0ecSDave Kleikamp 			continue;
223415121c18SEric Sandeen 		/*
223515121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
223615121c18SEric Sandeen 		 * found; some options take optional arguments.
223715121c18SEric Sandeen 		 */
2238caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2239ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
224026092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
224126092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2242ac27a0ecSDave Kleikamp 			return 0;
2243ac27a0ecSDave Kleikamp 	}
2244ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
224549da9392SJan Kara 	/*
224649da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
224749da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
224849da9392SJan Kara 	 * to support legacy quotas in quota files.
224949da9392SJan Kara 	 */
225049da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
225149da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
225249da9392SJan Kara 			 "Cannot enable project quota enforcement.");
225349da9392SJan Kara 		return 0;
225449da9392SJan Kara 	}
225533458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
225633458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
225733458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
225833458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2259fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2260ac27a0ecSDave Kleikamp 
226133458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2262fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2263ac27a0ecSDave Kleikamp 
226456c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2265b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2266b31e1552SEric Sandeen 					"format mixing");
2267ac27a0ecSDave Kleikamp 			return 0;
2268ac27a0ecSDave Kleikamp 		}
2269ac27a0ecSDave Kleikamp 
2270ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2271b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2272b31e1552SEric Sandeen 					"not specified");
2273ac27a0ecSDave Kleikamp 			return 0;
2274ac27a0ecSDave Kleikamp 		}
2275ac27a0ecSDave Kleikamp 	}
2276ac27a0ecSDave Kleikamp #endif
2277626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2278626b035bSRitesh Harjani 		int blocksize =
2279626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2280626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2281626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2282626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2283626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2284626b035bSRitesh Harjani 	}
2285ac27a0ecSDave Kleikamp 	return 1;
2286ac27a0ecSDave Kleikamp }
2287ac27a0ecSDave Kleikamp 
22882adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
22892adf6da8STheodore Ts'o 					   struct super_block *sb)
22902adf6da8STheodore Ts'o {
22912adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
22922adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
229333458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
22942adf6da8STheodore Ts'o 
22952adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
22962adf6da8STheodore Ts'o 		char *fmtname = "";
22972adf6da8STheodore Ts'o 
22982adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
22992adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
23002adf6da8STheodore Ts'o 			fmtname = "vfsold";
23012adf6da8STheodore Ts'o 			break;
23022adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
23032adf6da8STheodore Ts'o 			fmtname = "vfsv0";
23042adf6da8STheodore Ts'o 			break;
23052adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
23062adf6da8STheodore Ts'o 			fmtname = "vfsv1";
23072adf6da8STheodore Ts'o 			break;
23082adf6da8STheodore Ts'o 		}
23092adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
23102adf6da8STheodore Ts'o 	}
23112adf6da8STheodore Ts'o 
231233458eabSTheodore Ts'o 	rcu_read_lock();
231333458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
231433458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
231533458eabSTheodore Ts'o 	if (usr_qf_name)
231633458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
231733458eabSTheodore Ts'o 	if (grp_qf_name)
231833458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
231933458eabSTheodore Ts'o 	rcu_read_unlock();
23202adf6da8STheodore Ts'o #endif
23212adf6da8STheodore Ts'o }
23222adf6da8STheodore Ts'o 
23235a916be1STheodore Ts'o static const char *token2str(int token)
23245a916be1STheodore Ts'o {
232550df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
23265a916be1STheodore Ts'o 
23275a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
23285a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
23295a916be1STheodore Ts'o 			break;
23305a916be1STheodore Ts'o 	return t->pattern;
23315a916be1STheodore Ts'o }
23325a916be1STheodore Ts'o 
23332adf6da8STheodore Ts'o /*
23342adf6da8STheodore Ts'o  * Show an option if
23352adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
23362adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
23372adf6da8STheodore Ts'o  */
233866acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
233966acdcf4STheodore Ts'o 			      int nodefs)
23402adf6da8STheodore Ts'o {
23412adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
23422adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
234368afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
23445a916be1STheodore Ts'o 	const struct mount_opts *m;
234566acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
23462adf6da8STheodore Ts'o 
234766acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
234866acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
23492adf6da8STheodore Ts'o 
23502adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
23515a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
23525a916be1STheodore Ts'o 
23535a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
23545a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
23555a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
23569cb20f94SIra Weiny 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
23575a916be1STheodore Ts'o 			continue;
235868afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
23595a916be1STheodore Ts'o 			continue; /* skip if same as the default */
23605a916be1STheodore Ts'o 		if ((want_set &&
23615a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
23625a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
23635a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
23645a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
23655a916be1STheodore Ts'o 	}
23665a916be1STheodore Ts'o 
236708cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
23685a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
236908cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
237008cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
237108cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
23725a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
237308cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
237408cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
237566acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
23765a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
23775a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
23782adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
23795a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
23802adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
23815a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
238266acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
23835a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
238466acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
23855a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
238666acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
23875a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2388357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
23895a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
239066acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
23915a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
239268afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
239368afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
23942adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
23955a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
23962adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
23975a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
23982adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
23995a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
24005a916be1STheodore Ts'o 	}
240166acdcf4STheodore Ts'o 	if (nodefs ||
240266acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
24035a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
24042adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
24052adf6da8STheodore Ts'o 
2406ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
240766acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
24085a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2409df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2410df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
24117915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
24127915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2413ed318a6cSEric Biggers 
2414ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
24152adf6da8STheodore Ts'o 
2416*4f74d15fSEric Biggers 	if (sb->s_flags & SB_INLINECRYPT)
2417*4f74d15fSEric Biggers 		SEQ_OPTS_PUTS("inlinecrypt");
2418*4f74d15fSEric Biggers 
24199cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
24209cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
24219cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
24229cb20f94SIra Weiny 		else
24239cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
24249cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
24259cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
24269cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
24279cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
24289cb20f94SIra Weiny 	}
24299cb20f94SIra Weiny 
24302adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
24312adf6da8STheodore Ts'o 	return 0;
24322adf6da8STheodore Ts'o }
24332adf6da8STheodore Ts'o 
243466acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
243566acdcf4STheodore Ts'o {
243666acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
243766acdcf4STheodore Ts'o }
243866acdcf4STheodore Ts'o 
2439ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
244066acdcf4STheodore Ts'o {
244166acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
244266acdcf4STheodore Ts'o 	int rc;
244366acdcf4STheodore Ts'o 
2444bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
244566acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
244666acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
244766acdcf4STheodore Ts'o 	return rc;
244866acdcf4STheodore Ts'o }
244966acdcf4STheodore Ts'o 
2450617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2451ac27a0ecSDave Kleikamp 			    int read_only)
2452ac27a0ecSDave Kleikamp {
2453617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2454c89128a0SJaegeuk Kim 	int err = 0;
2455ac27a0ecSDave Kleikamp 
2456617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2457b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2458b31e1552SEric Sandeen 			 "forcing read-only mode");
2459c89128a0SJaegeuk Kim 		err = -EROFS;
24605adaccacSyangerkun 		goto done;
2461ac27a0ecSDave Kleikamp 	}
2462ac27a0ecSDave Kleikamp 	if (read_only)
2463281b5995STheodore Ts'o 		goto done;
2464617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2465b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2466b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2467c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2468b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2469b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2470b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2471ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2472ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2473ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2474b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2475b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2476b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2477ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
24786a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
24796a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2480b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2481b31e1552SEric Sandeen 			 "warning: checktime reached, "
2482b31e1552SEric Sandeen 			 "running e2fsck is recommended");
24830390131bSFrank Mayhar 	if (!sbi->s_journal)
2484216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2485ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2486617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2487e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
24886a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
24890390131bSFrank Mayhar 	if (sbi->s_journal)
2490e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2491ac27a0ecSDave Kleikamp 
2492c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2493281b5995STheodore Ts'o done:
2494ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2495a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2496a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2497ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2498ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2499617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2500617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2501a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2502ac27a0ecSDave Kleikamp 
25037abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2504c89128a0SJaegeuk Kim 	return err;
2505ac27a0ecSDave Kleikamp }
2506ac27a0ecSDave Kleikamp 
2507117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2508117fff10STheodore Ts'o {
2509117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
25107c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
251137b0b6b8SDan Carpenter 	int size, i, j;
2512117fff10STheodore Ts'o 
2513117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2514117fff10STheodore Ts'o 		return 0;
2515117fff10STheodore Ts'o 
2516117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2517117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2518117fff10STheodore Ts'o 		return 0;
2519117fff10STheodore Ts'o 
25207c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
25217c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2522117fff10STheodore Ts'o 	if (!new_groups) {
25237c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
25247c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2525117fff10STheodore Ts'o 		return -ENOMEM;
2526117fff10STheodore Ts'o 	}
25277c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
25287c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
25297c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
25307c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
25317c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
253237b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
253337b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
25347c990728SSuraj Jitindar Singh 			kvfree(new_groups);
25357c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
25367c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
25377c990728SSuraj Jitindar Singh 			return -ENOMEM;
2538117fff10STheodore Ts'o 		}
25397c990728SSuraj Jitindar Singh 	}
25407c990728SSuraj Jitindar Singh 	rcu_read_lock();
25417c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
25427c990728SSuraj Jitindar Singh 	if (old_groups)
25437c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
25447c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
25457c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
25467c990728SSuraj Jitindar Singh 	rcu_read_unlock();
25477c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
25487c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
25497c990728SSuraj Jitindar Singh 	if (old_groups)
25507c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2551117fff10STheodore Ts'o 	return 0;
2552117fff10STheodore Ts'o }
2553117fff10STheodore Ts'o 
2554772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2555772cb7c8SJose R. Santos {
2556772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2557772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
25587c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2559772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2560117fff10STheodore Ts'o 	int i, err;
2561772cb7c8SJose R. Santos 
2562503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2563d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2564772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2565772cb7c8SJose R. Santos 		return 1;
2566772cb7c8SJose R. Santos 	}
2567772cb7c8SJose R. Santos 
2568117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2569117fff10STheodore Ts'o 	if (err)
2570772cb7c8SJose R. Santos 		goto failed;
2571772cb7c8SJose R. Santos 
2572772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
257388b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2574772cb7c8SJose R. Santos 
2575772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
25767c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
25777c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
257890ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
25797c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
25807c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2581772cb7c8SJose R. Santos 	}
2582772cb7c8SJose R. Santos 
2583772cb7c8SJose R. Santos 	return 1;
2584772cb7c8SJose R. Santos failed:
2585772cb7c8SJose R. Santos 	return 0;
2586772cb7c8SJose R. Santos }
2587772cb7c8SJose R. Santos 
2588e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2589717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2590717d50e4SAndreas Dilger {
2591b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2592717d50e4SAndreas Dilger 	__u16 crc = 0;
2593717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2594e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2595717d50e4SAndreas Dilger 
25969aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2597feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2598feb0ab32SDarrick J. Wong 		__u32 csum32;
2599b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2600feb0ab32SDarrick J. Wong 
2601feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2602feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2603b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2604b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2605b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2606b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2607b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2608b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2609b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2610feb0ab32SDarrick J. Wong 
2611feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2612feb0ab32SDarrick J. Wong 		goto out;
2613feb0ab32SDarrick J. Wong 	}
2614feb0ab32SDarrick J. Wong 
2615feb0ab32SDarrick J. Wong 	/* old crc16 code */
2616e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2617813d32f9SDarrick J. Wong 		return 0;
2618813d32f9SDarrick J. Wong 
2619717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2620717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2621717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2622717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2623717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2624e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2625717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2626717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2627717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2628717d50e4SAndreas Dilger 				offset);
2629717d50e4SAndreas Dilger 
2630feb0ab32SDarrick J. Wong out:
2631717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2632717d50e4SAndreas Dilger }
2633717d50e4SAndreas Dilger 
2634feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2635717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2636717d50e4SAndreas Dilger {
2637feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2638e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2639717d50e4SAndreas Dilger 		return 0;
2640717d50e4SAndreas Dilger 
2641717d50e4SAndreas Dilger 	return 1;
2642717d50e4SAndreas Dilger }
2643717d50e4SAndreas Dilger 
2644feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2645feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2646feb0ab32SDarrick J. Wong {
2647feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2648feb0ab32SDarrick J. Wong 		return;
2649e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2650feb0ab32SDarrick J. Wong }
2651feb0ab32SDarrick J. Wong 
2652ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2653bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2654829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2655bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2656ac27a0ecSDave Kleikamp {
2657617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2658617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2659617ba13bSMingming Cao 	ext4_fsblk_t last_block;
266044de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2661bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2662bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2663bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2664ce421581SJose R. Santos 	int flexbg_flag = 0;
2665bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2666ac27a0ecSDave Kleikamp 
2667e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2668ce421581SJose R. Santos 		flexbg_flag = 1;
2669ce421581SJose R. Santos 
2670617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2671ac27a0ecSDave Kleikamp 
2672197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2673197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2674197cd65aSAkinobu Mita 
2675ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2676bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2677ac27a0ecSDave Kleikamp 		else
2678ac27a0ecSDave Kleikamp 			last_block = first_block +
2679617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2680ac27a0ecSDave Kleikamp 
2681bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2682bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2683bfff6873SLukas Czerner 			grp = i;
2684bfff6873SLukas Czerner 
26858fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2686829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2687829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2688829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2689829fa70dSTheodore Ts'o 				 "superblock", i);
269018db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
269118db4b4eSTheodore Ts'o 				return 0;
2692829fa70dSTheodore Ts'o 		}
269377260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
269477260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
269577260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
269677260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
269777260807STheodore Ts'o 				 "block group descriptors", i);
269877260807STheodore Ts'o 			if (!sb_rdonly(sb))
269977260807STheodore Ts'o 				return 0;
270077260807STheodore Ts'o 		}
27012b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2702b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2703a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2704b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2705ac27a0ecSDave Kleikamp 			return 0;
2706ac27a0ecSDave Kleikamp 		}
27078fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2708829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2709829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2710829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2711829fa70dSTheodore Ts'o 				 "superblock", i);
271218db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
271318db4b4eSTheodore Ts'o 				return 0;
2714829fa70dSTheodore Ts'o 		}
271577260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
271677260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
271777260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
271877260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
271977260807STheodore Ts'o 				 "block group descriptors", i);
272077260807STheodore Ts'o 			if (!sb_rdonly(sb))
272177260807STheodore Ts'o 				return 0;
272277260807STheodore Ts'o 		}
27232b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2724b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2725a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2726b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2727ac27a0ecSDave Kleikamp 			return 0;
2728ac27a0ecSDave Kleikamp 		}
27298fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2730829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2731829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2732829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2733829fa70dSTheodore Ts'o 				 "superblock", i);
273418db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
273518db4b4eSTheodore Ts'o 				return 0;
2736829fa70dSTheodore Ts'o 		}
273777260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
273877260807STheodore Ts'o 		    inode_table <= last_bg_block) {
273977260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
274077260807STheodore Ts'o 				 "Inode table for group %u overlaps "
274177260807STheodore Ts'o 				 "block group descriptors", i);
274277260807STheodore Ts'o 			if (!sb_rdonly(sb))
274377260807STheodore Ts'o 				return 0;
274477260807STheodore Ts'o 		}
2745bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
27462b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2747b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2748a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2749b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2750ac27a0ecSDave Kleikamp 			return 0;
2751ac27a0ecSDave Kleikamp 		}
2752955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2753feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2754b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2755b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2756e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2757fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2758bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2759955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2760717d50e4SAndreas Dilger 				return 0;
2761717d50e4SAndreas Dilger 			}
27627ee1ec4cSLi Zefan 		}
2763955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2764ce421581SJose R. Santos 		if (!flexbg_flag)
2765617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2766ac27a0ecSDave Kleikamp 	}
2767bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2768bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2769ac27a0ecSDave Kleikamp 	return 1;
2770ac27a0ecSDave Kleikamp }
2771ac27a0ecSDave Kleikamp 
2772617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2773ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2774ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2775ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2776ac27a0ecSDave Kleikamp  *
2777ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2778ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2779ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2780ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2781ac27a0ecSDave Kleikamp  *
2782ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2783ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2784ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2785617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2786ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2787ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2788ac27a0ecSDave Kleikamp  */
2789617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2790617ba13bSMingming Cao 				struct ext4_super_block *es)
2791ac27a0ecSDave Kleikamp {
2792ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
27932c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2794ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
279595f1fda4Szhangyi (F) 	int quota_update = 0;
2796ac27a0ecSDave Kleikamp 	int i;
2797ac27a0ecSDave Kleikamp #endif
2798ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2799ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2800ac27a0ecSDave Kleikamp 		return;
2801ac27a0ecSDave Kleikamp 	}
2802ac27a0ecSDave Kleikamp 
2803a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2804b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2805b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2806a8f48a95SEric Sandeen 		return;
2807a8f48a95SEric Sandeen 	}
2808a8f48a95SEric Sandeen 
2809d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2810d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2811d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2812d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2813d39195c3SAmir Goldstein 		return;
2814d39195c3SAmir Goldstein 	}
2815d39195c3SAmir Goldstein 
2816617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2817c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
28181751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
281984474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2820ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2821ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2822c25f9bc6SEric Sandeen 		}
2823ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2824ac27a0ecSDave Kleikamp 		return;
2825ac27a0ecSDave Kleikamp 	}
2826ac27a0ecSDave Kleikamp 
28271751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2828b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
28291751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2830ac27a0ecSDave Kleikamp 	}
2831ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2832ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
28331751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
283495f1fda4Szhangyi (F) 
283595f1fda4Szhangyi (F) 	/*
283695f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
283795f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
283895f1fda4Szhangyi (F) 	 */
28391751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
284095f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
284195f1fda4Szhangyi (F) 
284295f1fda4Szhangyi (F) 		if (!ret)
284395f1fda4Szhangyi (F) 			quota_update = 1;
284495f1fda4Szhangyi (F) 		else
284595f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
284695f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
284795f1fda4Szhangyi (F) 	}
284895f1fda4Szhangyi (F) 
284995f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2850a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2851617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2852617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
285395f1fda4Szhangyi (F) 
285495f1fda4Szhangyi (F) 			if (!ret)
285595f1fda4Szhangyi (F) 				quota_update = 1;
285695f1fda4Szhangyi (F) 			else
2857b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2858b31e1552SEric Sandeen 					"Cannot turn on journaled "
285995f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2860ac27a0ecSDave Kleikamp 		}
2861ac27a0ecSDave Kleikamp 	}
2862ac27a0ecSDave Kleikamp #endif
2863ac27a0ecSDave Kleikamp 
2864ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2865ac27a0ecSDave Kleikamp 		struct inode *inode;
2866ac27a0ecSDave Kleikamp 
2867c65d5c6cSVegard Nossum 		/*
2868c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2869c65d5c6cSVegard Nossum 		 * so, skip the rest.
2870c65d5c6cSVegard Nossum 		 */
2871c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2872c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2873c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2874c65d5c6cSVegard Nossum 			break;
2875c65d5c6cSVegard Nossum 		}
2876c65d5c6cSVegard Nossum 
287797bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
287897bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2879ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2880ac27a0ecSDave Kleikamp 			break;
2881ac27a0ecSDave Kleikamp 		}
2882ac27a0ecSDave Kleikamp 
2883617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2884871a2931SChristoph Hellwig 		dquot_initialize(inode);
2885ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2886566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2887b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2888b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
288946e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2890e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2891ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
28925955102cSAl Viro 			inode_lock(inode);
289355f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
28942c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
28952c98eb5eSTheodore Ts'o 			if (ret)
28962c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
28975955102cSAl Viro 			inode_unlock(inode);
2898ac27a0ecSDave Kleikamp 			nr_truncates++;
2899ac27a0ecSDave Kleikamp 		} else {
2900566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2901b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2902b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
290346e665e9SHarvey Harrison 					__func__, inode->i_ino);
2904ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2905ac27a0ecSDave Kleikamp 				  inode->i_ino);
2906ac27a0ecSDave Kleikamp 			nr_orphans++;
2907ac27a0ecSDave Kleikamp 		}
2908ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2909ac27a0ecSDave Kleikamp 	}
2910ac27a0ecSDave Kleikamp 
2911ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2912ac27a0ecSDave Kleikamp 
2913ac27a0ecSDave Kleikamp 	if (nr_orphans)
2914b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2915b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2916ac27a0ecSDave Kleikamp 	if (nr_truncates)
2917b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2918b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2919ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
292095f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
292195f1fda4Szhangyi (F) 	if (quota_update) {
2922a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
292395f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2924287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2925ac27a0ecSDave Kleikamp 		}
292695f1fda4Szhangyi (F) 	}
2927ac27a0ecSDave Kleikamp #endif
29281751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2929ac27a0ecSDave Kleikamp }
29300b8e58a1SAndreas Dilger 
2931cd2291a4SEric Sandeen /*
2932cd2291a4SEric Sandeen  * Maximal extent format file size.
2933cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2934cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2935cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2936cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2937cd2291a4SEric Sandeen  *
2938f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2939f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2940f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2941f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2942f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2943f17722f9SLukas Czerner  *
2944cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2945cd2291a4SEric Sandeen  */
2946f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2947cd2291a4SEric Sandeen {
2948cd2291a4SEric Sandeen 	loff_t res;
2949cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2950cd2291a4SEric Sandeen 
295172deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
295272deb455SChristoph Hellwig 
295372deb455SChristoph Hellwig 	if (!has_huge_files) {
2954cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2955cd2291a4SEric Sandeen 
2956cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2957cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2958cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2959cd2291a4SEric Sandeen 	}
2960cd2291a4SEric Sandeen 
2961f17722f9SLukas Czerner 	/*
2962f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2963f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2964f17722f9SLukas Czerner 	 * size
2965f17722f9SLukas Czerner 	 */
2966f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2967cd2291a4SEric Sandeen 	res <<= blkbits;
2968cd2291a4SEric Sandeen 
2969cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2970cd2291a4SEric Sandeen 	if (res > upper_limit)
2971cd2291a4SEric Sandeen 		res = upper_limit;
2972cd2291a4SEric Sandeen 
2973cd2291a4SEric Sandeen 	return res;
2974cd2291a4SEric Sandeen }
2975ac27a0ecSDave Kleikamp 
2976ac27a0ecSDave Kleikamp /*
2977cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
29780fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
29790fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2980ac27a0ecSDave Kleikamp  */
2981f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2982ac27a0ecSDave Kleikamp {
2983617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
29840fc1b451SAneesh Kumar K.V 	int meta_blocks;
29850fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
29860b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
29870b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
29880b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
29890b8e58a1SAndreas Dilger 	 *
29900b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
29910b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
29920fc1b451SAneesh Kumar K.V 	 */
29930fc1b451SAneesh Kumar K.V 
299472deb455SChristoph Hellwig 	if (!has_huge_files) {
29950fc1b451SAneesh Kumar K.V 		/*
299672deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
299772deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
299872deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
29990fc1b451SAneesh Kumar K.V 		 */
30000fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
30010fc1b451SAneesh Kumar K.V 
30020fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
30030fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
30040fc1b451SAneesh Kumar K.V 
30050fc1b451SAneesh Kumar K.V 	} else {
30068180a562SAneesh Kumar K.V 		/*
30078180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
30088180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
30098180a562SAneesh Kumar K.V 		 * represent total number of blocks in
30108180a562SAneesh Kumar K.V 		 * file system block size
30118180a562SAneesh Kumar K.V 		 */
30120fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
30130fc1b451SAneesh Kumar K.V 
30140fc1b451SAneesh Kumar K.V 	}
30150fc1b451SAneesh Kumar K.V 
30160fc1b451SAneesh Kumar K.V 	/* indirect blocks */
30170fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
30180fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
30190fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
30200fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
30210fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
30220fc1b451SAneesh Kumar K.V 
30230fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
30240fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
3025ac27a0ecSDave Kleikamp 
3026ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
3027ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
3028ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
3029ac27a0ecSDave Kleikamp 	res <<= bits;
3030ac27a0ecSDave Kleikamp 	if (res > upper_limit)
3031ac27a0ecSDave Kleikamp 		res = upper_limit;
30320fc1b451SAneesh Kumar K.V 
30330fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
30340fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
30350fc1b451SAneesh Kumar K.V 
3036ac27a0ecSDave Kleikamp 	return res;
3037ac27a0ecSDave Kleikamp }
3038ac27a0ecSDave Kleikamp 
3039617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
304070bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3041ac27a0ecSDave Kleikamp {
3042617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3043fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3044ac27a0ecSDave Kleikamp 	int has_super = 0;
3045ac27a0ecSDave Kleikamp 
3046ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3047ac27a0ecSDave Kleikamp 
3048e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
304970bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3050ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3051617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3052ac27a0ecSDave Kleikamp 		has_super = 1;
30530b8e58a1SAndreas Dilger 
3054bd63f6b0SDarrick J. Wong 	/*
3055bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3056bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3057bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3058bd63f6b0SDarrick J. Wong 	 * compensate.
3059bd63f6b0SDarrick J. Wong 	 */
3060bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
306149598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3062bd63f6b0SDarrick J. Wong 		has_super++;
3063bd63f6b0SDarrick J. Wong 
3064617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3065ac27a0ecSDave Kleikamp }
3066ac27a0ecSDave Kleikamp 
3067c9de560dSAlex Tomas /**
3068c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3069c9de560dSAlex Tomas  * @sbi: In memory super block info
3070c9de560dSAlex Tomas  *
3071c9de560dSAlex Tomas  * If we have specified it via mount option, then
3072c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3073c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3074c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3075c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3076c9de560dSAlex Tomas  *
3077c9de560dSAlex Tomas  */
3078c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3079c9de560dSAlex Tomas {
3080c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3081c9de560dSAlex Tomas 	unsigned long stripe_width =
3082c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
30833eb08658SDan Ehrenberg 	int ret;
3084c9de560dSAlex Tomas 
3085c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
30863eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
30875469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
30883eb08658SDan Ehrenberg 		ret = stripe_width;
30895469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
30903eb08658SDan Ehrenberg 		ret = stride;
30913eb08658SDan Ehrenberg 	else
30923eb08658SDan Ehrenberg 		ret = 0;
3093c9de560dSAlex Tomas 
30943eb08658SDan Ehrenberg 	/*
30953eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
30963eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
30973eb08658SDan Ehrenberg 	 */
30983eb08658SDan Ehrenberg 	if (ret <= 1)
30993eb08658SDan Ehrenberg 		ret = 0;
3100c9de560dSAlex Tomas 
31013eb08658SDan Ehrenberg 	return ret;
3102c9de560dSAlex Tomas }
3103ac27a0ecSDave Kleikamp 
3104a13fb1a4SEric Sandeen /*
3105a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3106a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3107a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3108a13fb1a4SEric Sandeen  * 0 if it cannot be.
3109a13fb1a4SEric Sandeen  */
3110a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3111a13fb1a4SEric Sandeen {
3112e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3113a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3114a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3115a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3116a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3117a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3118a13fb1a4SEric Sandeen 		return 0;
3119a13fb1a4SEric Sandeen 	}
3120a13fb1a4SEric Sandeen 
3121c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3122c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3123c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3124c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3125c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3126c83ad55eSGabriel Krisman Bertazi 		return 0;
3127c83ad55eSGabriel Krisman Bertazi 	}
3128c83ad55eSGabriel Krisman Bertazi #endif
3129c83ad55eSGabriel Krisman Bertazi 
3130a13fb1a4SEric Sandeen 	if (readonly)
3131a13fb1a4SEric Sandeen 		return 1;
3132a13fb1a4SEric Sandeen 
3133e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
31342cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
31351751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
31362cb5cc8bSDarrick J. Wong 		return 1;
31372cb5cc8bSDarrick J. Wong 	}
31382cb5cc8bSDarrick J. Wong 
3139a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3140e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3141a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3142a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3143a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3144a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3145a13fb1a4SEric Sandeen 		return 0;
3146a13fb1a4SEric Sandeen 	}
3147e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3148bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3149bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3150bab08ab9STheodore Ts'o 			 "extents feature\n");
3151bab08ab9STheodore Ts'o 		return 0;
3152bab08ab9STheodore Ts'o 	}
31537c319d32SAditya Kali 
31549db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3155d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3156d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
31577c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3158d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3159689c958cSLi Xi 		return 0;
3160689c958cSLi Xi 	}
31617c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3162a13fb1a4SEric Sandeen 	return 1;
3163a13fb1a4SEric Sandeen }
3164a13fb1a4SEric Sandeen 
316566e61a9eSTheodore Ts'o /*
316666e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
316766e61a9eSTheodore Ts'o  * on the file system
316866e61a9eSTheodore Ts'o  */
3169235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
317066e61a9eSTheodore Ts'o {
3171235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3172235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3173235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
317466e61a9eSTheodore Ts'o 
317566e61a9eSTheodore Ts'o 	if (es->s_error_count)
3176ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3177ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
317866e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
317966e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
31806a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
31816a0678a7SArnd Bergmann 		       sb->s_id,
31826a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
318366e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
318466e61a9eSTheodore Ts'o 		       es->s_first_error_func,
318566e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
318666e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3187651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
318866e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
318966e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3190651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
319166e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3192651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
319366e61a9eSTheodore Ts'o 	}
319466e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
31956a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
31966a0678a7SArnd Bergmann 		       sb->s_id,
31976a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
319866e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
319966e61a9eSTheodore Ts'o 		       es->s_last_error_func,
320066e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
320166e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3202651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
320366e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
320466e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3205651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
320666e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3207651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
320866e61a9eSTheodore Ts'o 	}
320966e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
321066e61a9eSTheodore Ts'o }
321166e61a9eSTheodore Ts'o 
3212bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3213bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3214bfff6873SLukas Czerner {
3215bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3216bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3217bfff6873SLukas Czerner 	struct super_block *sb;
3218bfff6873SLukas Czerner 	unsigned long timeout = 0;
3219bfff6873SLukas Czerner 	int ret = 0;
3220bfff6873SLukas Czerner 
3221bfff6873SLukas Czerner 	sb = elr->lr_super;
3222bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3223bfff6873SLukas Czerner 
3224bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3225bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3226bfff6873SLukas Czerner 		if (!gdp) {
3227bfff6873SLukas Czerner 			ret = 1;
3228bfff6873SLukas Czerner 			break;
3229bfff6873SLukas Czerner 		}
3230bfff6873SLukas Czerner 
3231bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3232bfff6873SLukas Czerner 			break;
3233bfff6873SLukas Czerner 	}
3234bfff6873SLukas Czerner 
32357f511862STheodore Ts'o 	if (group >= ngroups)
3236bfff6873SLukas Czerner 		ret = 1;
3237bfff6873SLukas Czerner 
3238bfff6873SLukas Czerner 	if (!ret) {
3239bfff6873SLukas Czerner 		timeout = jiffies;
3240bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3241bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3242bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
324351ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
324451ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3245bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3246bfff6873SLukas Czerner 		}
3247bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3248bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3249bfff6873SLukas Czerner 	}
3250bfff6873SLukas Czerner 	return ret;
3251bfff6873SLukas Czerner }
3252bfff6873SLukas Czerner 
3253bfff6873SLukas Czerner /*
3254bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
32554ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3256bfff6873SLukas Czerner  */
3257bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3258bfff6873SLukas Czerner {
3259bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3260bfff6873SLukas Czerner 
3261bfff6873SLukas Czerner 	if (!elr)
3262bfff6873SLukas Czerner 		return;
3263bfff6873SLukas Czerner 
3264bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3265bfff6873SLukas Czerner 
3266bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3267bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3268bfff6873SLukas Czerner 	kfree(elr);
3269bfff6873SLukas Czerner }
3270bfff6873SLukas Czerner 
3271bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3272bfff6873SLukas Czerner {
32731bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
32741bb933fbSLukas Czerner 	if (!ext4_li_info) {
32751bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3276bfff6873SLukas Czerner 		return;
32771bb933fbSLukas Czerner 	}
3278bfff6873SLukas Czerner 
3279bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
32801bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3281bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
32821bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3283bfff6873SLukas Czerner }
3284bfff6873SLukas Czerner 
32858f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
32868f1f7453SEric Sandeen 
3287bfff6873SLukas Czerner /*
3288bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3289bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3290bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3291bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3292bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3293bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3294bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3295bfff6873SLukas Czerner  */
3296bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3297bfff6873SLukas Czerner {
3298bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3299bfff6873SLukas Czerner 	struct list_head *pos, *n;
3300bfff6873SLukas Czerner 	struct ext4_li_request *elr;
33014ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3302bfff6873SLukas Czerner 
3303bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3304bfff6873SLukas Czerner 
3305bfff6873SLukas Czerner cont_thread:
3306bfff6873SLukas Czerner 	while (true) {
3307bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3308bfff6873SLukas Czerner 
3309bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3310bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3311bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3312bfff6873SLukas Czerner 			goto exit_thread;
3313bfff6873SLukas Czerner 		}
3314bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3315e22834f0SDmitry Monakhov 			int err = 0;
3316e22834f0SDmitry Monakhov 			int progress = 0;
3317bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3318bfff6873SLukas Czerner 					 lr_request);
3319bfff6873SLukas Czerner 
3320e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3321e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3322e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3323e22834f0SDmitry Monakhov 				continue;
3324e22834f0SDmitry Monakhov 			}
3325e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3326e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3327e22834f0SDmitry Monakhov 					progress = 1;
3328e22834f0SDmitry Monakhov 					/*
3329e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3330e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3331e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3332e22834f0SDmitry Monakhov 					 */
3333e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3334e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3335e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3336e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3337e22834f0SDmitry Monakhov 					n = pos->next;
3338e22834f0SDmitry Monakhov 				}
3339e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3340e22834f0SDmitry Monakhov 			}
3341b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3342e22834f0SDmitry Monakhov 			if (err) {
3343bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3344bfff6873SLukas Czerner 				continue;
3345bfff6873SLukas Czerner 			}
3346e22834f0SDmitry Monakhov 			if (!progress) {
3347e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3348e22834f0SDmitry Monakhov 					(prandom_u32()
3349e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3350b2c78cd0STheodore Ts'o 			}
3351bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3352bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3353bfff6873SLukas Czerner 		}
3354bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3355bfff6873SLukas Czerner 
3356a0acae0eSTejun Heo 		try_to_freeze();
3357bfff6873SLukas Czerner 
33584ed5c033SLukas Czerner 		cur = jiffies;
33594ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3360f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3361bfff6873SLukas Czerner 			cond_resched();
3362bfff6873SLukas Czerner 			continue;
3363bfff6873SLukas Czerner 		}
3364bfff6873SLukas Czerner 
33654ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
33664ed5c033SLukas Czerner 
33678f1f7453SEric Sandeen 		if (kthread_should_stop()) {
33688f1f7453SEric Sandeen 			ext4_clear_request_list();
33698f1f7453SEric Sandeen 			goto exit_thread;
33708f1f7453SEric Sandeen 		}
3371bfff6873SLukas Czerner 	}
3372bfff6873SLukas Czerner 
3373bfff6873SLukas Czerner exit_thread:
3374bfff6873SLukas Czerner 	/*
3375bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3376bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3377bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3378bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3379bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3380bfff6873SLukas Czerner 	 * new one.
3381bfff6873SLukas Czerner 	 */
3382bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3383bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3384bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3385bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3386bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3387bfff6873SLukas Czerner 		goto cont_thread;
3388bfff6873SLukas Czerner 	}
3389bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3390bfff6873SLukas Czerner 	kfree(ext4_li_info);
3391bfff6873SLukas Czerner 	ext4_li_info = NULL;
3392bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3393bfff6873SLukas Czerner 
3394bfff6873SLukas Czerner 	return 0;
3395bfff6873SLukas Czerner }
3396bfff6873SLukas Czerner 
3397bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3398bfff6873SLukas Czerner {
3399bfff6873SLukas Czerner 	struct list_head *pos, *n;
3400bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3401bfff6873SLukas Czerner 
3402bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3403bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3404bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3405bfff6873SLukas Czerner 				 lr_request);
3406bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3407bfff6873SLukas Czerner 	}
3408bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3409bfff6873SLukas Czerner }
3410bfff6873SLukas Czerner 
3411bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3412bfff6873SLukas Czerner {
34138f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
34148f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
34158f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
34168f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3417bfff6873SLukas Czerner 		ext4_clear_request_list();
3418bfff6873SLukas Czerner 		kfree(ext4_li_info);
3419bfff6873SLukas Czerner 		ext4_li_info = NULL;
342092b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3421bfff6873SLukas Czerner 				 "initialization thread\n",
3422bfff6873SLukas Czerner 				 err);
3423bfff6873SLukas Czerner 		return err;
3424bfff6873SLukas Czerner 	}
3425bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3426bfff6873SLukas Czerner 	return 0;
3427bfff6873SLukas Czerner }
3428bfff6873SLukas Czerner 
3429bfff6873SLukas Czerner /*
3430bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3431bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3432bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3433bfff6873SLukas Czerner  * groups and return total number of groups.
3434bfff6873SLukas Czerner  */
3435bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3436bfff6873SLukas Czerner {
3437bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3438bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3439bfff6873SLukas Czerner 
34408844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
34418844618dSTheodore Ts'o 		return ngroups;
34428844618dSTheodore Ts'o 
3443bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3444bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3445bfff6873SLukas Czerner 		if (!gdp)
3446bfff6873SLukas Czerner 			continue;
3447bfff6873SLukas Czerner 
344850122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3449bfff6873SLukas Czerner 			break;
3450bfff6873SLukas Czerner 	}
3451bfff6873SLukas Czerner 
3452bfff6873SLukas Czerner 	return group;
3453bfff6873SLukas Czerner }
3454bfff6873SLukas Czerner 
3455bfff6873SLukas Czerner static int ext4_li_info_new(void)
3456bfff6873SLukas Czerner {
3457bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3458bfff6873SLukas Czerner 
3459bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3460bfff6873SLukas Czerner 	if (!eli)
3461bfff6873SLukas Czerner 		return -ENOMEM;
3462bfff6873SLukas Czerner 
3463bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3464bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3465bfff6873SLukas Czerner 
3466bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3467bfff6873SLukas Czerner 
3468bfff6873SLukas Czerner 	ext4_li_info = eli;
3469bfff6873SLukas Czerner 
3470bfff6873SLukas Czerner 	return 0;
3471bfff6873SLukas Czerner }
3472bfff6873SLukas Czerner 
3473bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3474bfff6873SLukas Czerner 					    ext4_group_t start)
3475bfff6873SLukas Czerner {
3476bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3477bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3478bfff6873SLukas Czerner 
3479bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3480bfff6873SLukas Czerner 	if (!elr)
3481bfff6873SLukas Czerner 		return NULL;
3482bfff6873SLukas Czerner 
3483bfff6873SLukas Czerner 	elr->lr_super = sb;
3484bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3485bfff6873SLukas Czerner 	elr->lr_next_group = start;
3486bfff6873SLukas Czerner 
3487bfff6873SLukas Czerner 	/*
3488bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3489bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3490bfff6873SLukas Czerner 	 * better.
3491bfff6873SLukas Czerner 	 */
3492dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3493dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3494bfff6873SLukas Czerner 	return elr;
3495bfff6873SLukas Czerner }
3496bfff6873SLukas Czerner 
34977f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3498bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3499bfff6873SLukas Czerner {
3500bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
35017f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
350249598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
35036c5a6cb9SAndrew Morton 	int ret = 0;
3504bfff6873SLukas Czerner 
35057f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
350651ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
350751ce6511SLukas Czerner 		/*
350851ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
350951ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
351051ce6511SLukas Czerner 		 */
351151ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
35127f511862STheodore Ts'o 		goto out;
351351ce6511SLukas Czerner 	}
3514bfff6873SLukas Czerner 
3515bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
351655ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
35177f511862STheodore Ts'o 		goto out;
3518bfff6873SLukas Czerner 
3519bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
35207f511862STheodore Ts'o 	if (!elr) {
35217f511862STheodore Ts'o 		ret = -ENOMEM;
35227f511862STheodore Ts'o 		goto out;
35237f511862STheodore Ts'o 	}
3524bfff6873SLukas Czerner 
3525bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3526bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3527bfff6873SLukas Czerner 		if (ret)
3528bfff6873SLukas Czerner 			goto out;
3529bfff6873SLukas Czerner 	}
3530bfff6873SLukas Czerner 
3531bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3532bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3533bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3534bfff6873SLukas Czerner 
3535bfff6873SLukas Czerner 	sbi->s_li_request = elr;
353646e4690bSTao Ma 	/*
353746e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
353846e4690bSTao Ma 	 * the request_list and the removal and free of it is
353946e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
354046e4690bSTao Ma 	 */
354146e4690bSTao Ma 	elr = NULL;
3542bfff6873SLukas Czerner 
3543bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3544bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3545bfff6873SLukas Czerner 		if (ret)
3546bfff6873SLukas Czerner 			goto out;
3547bfff6873SLukas Czerner 	}
3548bfff6873SLukas Czerner out:
3549bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3550beed5ecbSNicolas Kaiser 	if (ret)
3551bfff6873SLukas Czerner 		kfree(elr);
3552bfff6873SLukas Czerner 	return ret;
3553bfff6873SLukas Czerner }
3554bfff6873SLukas Czerner 
3555bfff6873SLukas Czerner /*
3556bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3557bfff6873SLukas Czerner  * module unload.
3558bfff6873SLukas Czerner  */
3559bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3560bfff6873SLukas Czerner {
3561bfff6873SLukas Czerner 	/*
3562bfff6873SLukas Czerner 	 * If thread exited earlier
3563bfff6873SLukas Czerner 	 * there's nothing to be done.
3564bfff6873SLukas Czerner 	 */
35658f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3566bfff6873SLukas Czerner 		return;
3567bfff6873SLukas Czerner 
35688f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3569bfff6873SLukas Czerner }
3570bfff6873SLukas Czerner 
357125ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
357225ed6e8aSDarrick J. Wong {
357325ed6e8aSDarrick J. Wong 	int ret = 1;
357425ed6e8aSDarrick J. Wong 	int compat, incompat;
357525ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
357625ed6e8aSDarrick J. Wong 
35779aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3578db9ee220SDarrick J. Wong 		/* journal checksum v3 */
357925ed6e8aSDarrick J. Wong 		compat = 0;
3580db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
358125ed6e8aSDarrick J. Wong 	} else {
358225ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
358325ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
358425ed6e8aSDarrick J. Wong 		incompat = 0;
358525ed6e8aSDarrick J. Wong 	}
358625ed6e8aSDarrick J. Wong 
3587feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3588feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3589feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3590feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
359125ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
359225ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
359325ed6e8aSDarrick J. Wong 				compat, 0,
359425ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
359525ed6e8aSDarrick J. Wong 				incompat);
359625ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
359725ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
359825ed6e8aSDarrick J. Wong 				compat, 0,
359925ed6e8aSDarrick J. Wong 				incompat);
360025ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
360125ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
360225ed6e8aSDarrick J. Wong 	} else {
3603feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3604feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
360525ed6e8aSDarrick J. Wong 	}
360625ed6e8aSDarrick J. Wong 
360725ed6e8aSDarrick J. Wong 	return ret;
360825ed6e8aSDarrick J. Wong }
360925ed6e8aSDarrick J. Wong 
3610952fc18eSTheodore Ts'o /*
3611952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3612952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3613952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3614952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3615952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3616952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3617952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3618952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3619952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3620952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3621952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3622952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3623952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3624952fc18eSTheodore Ts'o  */
3625952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3626952fc18eSTheodore Ts'o 			  char *buf)
3627952fc18eSTheodore Ts'o {
3628952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3629952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3630952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3631952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3632952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3633952fc18eSTheodore Ts'o 
3634e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
36350548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
36360548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
36370548bbb8STheodore Ts'o 
3638952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3639952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3640952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3641952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3642952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3643952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3644952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3645952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3646952fc18eSTheodore Ts'o 			count++;
3647952fc18eSTheodore Ts'o 		}
3648952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3649952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3650952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3651952fc18eSTheodore Ts'o 			count++;
3652952fc18eSTheodore Ts'o 		}
3653952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3654952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3655952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3656952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3657952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3658952fc18eSTheodore Ts'o 				count++;
3659952fc18eSTheodore Ts'o 			}
3660952fc18eSTheodore Ts'o 		if (i != grp)
3661952fc18eSTheodore Ts'o 			continue;
3662952fc18eSTheodore Ts'o 		s = 0;
3663952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3664952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3665952fc18eSTheodore Ts'o 			count++;
3666952fc18eSTheodore Ts'o 		}
3667c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3668c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3669c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3670c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3671c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3672952fc18eSTheodore Ts'o 		}
3673c48ae41bSTheodore Ts'o 		count += j;
3674c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3675c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3676952fc18eSTheodore Ts'o 	}
3677952fc18eSTheodore Ts'o 	if (!count)
3678952fc18eSTheodore Ts'o 		return 0;
3679952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3680952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3681952fc18eSTheodore Ts'o }
3682952fc18eSTheodore Ts'o 
3683952fc18eSTheodore Ts'o /*
3684952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3685952fc18eSTheodore Ts'o  */
3686952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3687952fc18eSTheodore Ts'o {
3688952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3689952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
36903c816dedSEric Whitney 	struct inode *j_inode;
36913c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3692952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3693952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
36944fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3695952fc18eSTheodore Ts'o 
3696952fc18eSTheodore Ts'o 	if (!buf)
3697952fc18eSTheodore Ts'o 		return -ENOMEM;
3698952fc18eSTheodore Ts'o 
3699952fc18eSTheodore Ts'o 	/*
3700952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3701952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3702952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3703952fc18eSTheodore Ts'o 	 */
3704952fc18eSTheodore Ts'o 
3705952fc18eSTheodore Ts'o 	/*
3706952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3707952fc18eSTheodore Ts'o 	 */
3708952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3709952fc18eSTheodore Ts'o 
3710952fc18eSTheodore Ts'o 	/*
3711952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3712952fc18eSTheodore Ts'o 	 */
3713952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3714952fc18eSTheodore Ts'o 		int blks;
3715952fc18eSTheodore Ts'o 
3716952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3717952fc18eSTheodore Ts'o 		overhead += blks;
3718952fc18eSTheodore Ts'o 		if (blks)
3719952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3720952fc18eSTheodore Ts'o 		cond_resched();
3721952fc18eSTheodore Ts'o 	}
37223c816dedSEric Whitney 
37233c816dedSEric Whitney 	/*
37243c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
37253c816dedSEric Whitney 	 * loaded or not
37263c816dedSEric Whitney 	 */
3727b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3728810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
3729f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3730f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
37313c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
37323c816dedSEric Whitney 		if (j_inode) {
37333c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
37343c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
37353c816dedSEric Whitney 			iput(j_inode);
37363c816dedSEric Whitney 		} else {
37373c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
37383c816dedSEric Whitney 		}
37393c816dedSEric Whitney 	}
3740952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3741952fc18eSTheodore Ts'o 	smp_wmb();
3742952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3743952fc18eSTheodore Ts'o 	return 0;
3744952fc18eSTheodore Ts'o }
3745952fc18eSTheodore Ts'o 
3746b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
374727dd4385SLukas Czerner {
374827dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3749b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
375027dd4385SLukas Czerner 
375127dd4385SLukas Czerner 	/*
375230fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
375330fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
375430fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
375530fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
375630fac0f7SJan Kara 	 */
3757e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3758b5799018STheodore Ts'o 		return;
375930fac0f7SJan Kara 	/*
376027dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
376127dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
376227dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3763556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
376427dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
376527dd4385SLukas Czerner 	 * very rare.
376627dd4385SLukas Czerner 	 */
3767b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3768b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
376927dd4385SLukas Czerner 
377027dd4385SLukas Czerner 	do_div(resv_clusters, 50);
377127dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
377227dd4385SLukas Czerner 
3773b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
377427dd4385SLukas Czerner }
377527dd4385SLukas Czerner 
3776617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3777ac27a0ecSDave Kleikamp {
37785e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3779d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
37801d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
3781617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
37825aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
37837c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
3784617ba13bSMingming Cao 	ext4_fsblk_t block;
3785617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
378670bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3787ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3788ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3789ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3790ac27a0ecSDave Kleikamp 	struct inode *root;
37910390131bSFrank Mayhar 	const char *descr;
3792dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3793281b5995STheodore Ts'o 	int blocksize, clustersize;
37944ec11028STheodore Ts'o 	unsigned int db_count;
37954ec11028STheodore Ts'o 	unsigned int i;
3796ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
3797bd81d8eeSLaurent Vivier 	__u64 blocks_count;
379807aa2ea1SLukas Czerner 	int err = 0;
3799b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3800bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3801ac27a0ecSDave Kleikamp 
38025aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
38035aee0f8aSTheodore Ts'o 		goto out_free_base;
3804705895b6SPekka Enberg 
3805aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3806705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3807705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
38085aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
38095aee0f8aSTheodore Ts'o 		goto out_free_base;
38105aee0f8aSTheodore Ts'o 
3811ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
38122c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3813240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3814d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3815f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3816f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3817dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3818ac27a0ecSDave Kleikamp 
38199f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3820ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
38219f6200bbSTheodore Ts'o 
382207aa2ea1SLukas Czerner 	/* -EINVAL is default */
3823dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3824617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3825ac27a0ecSDave Kleikamp 	if (!blocksize) {
3826b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3827ac27a0ecSDave Kleikamp 		goto out_fail;
3828ac27a0ecSDave Kleikamp 	}
3829ac27a0ecSDave Kleikamp 
3830ac27a0ecSDave Kleikamp 	/*
3831617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3832ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3833ac27a0ecSDave Kleikamp 	 */
3834617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
383570bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
383670bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3837ac27a0ecSDave Kleikamp 	} else {
383870bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3839ac27a0ecSDave Kleikamp 	}
3840ac27a0ecSDave Kleikamp 
3841a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3842b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3843ac27a0ecSDave Kleikamp 		goto out_fail;
3844ac27a0ecSDave Kleikamp 	}
3845ac27a0ecSDave Kleikamp 	/*
3846ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3847617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3848ac27a0ecSDave Kleikamp 	 */
38492716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3850ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3851ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3852617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3853617ba13bSMingming Cao 		goto cantfind_ext4;
3854afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3855ac27a0ecSDave Kleikamp 
3856feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3857e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3858e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3859363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3860feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3861feb0ab32SDarrick J. Wong 
3862d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3863d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3864d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3865d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3866d25425f8SDarrick J. Wong 		silent = 1;
3867d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3868d25425f8SDarrick J. Wong 	}
3869d25425f8SDarrick J. Wong 
38700441984aSDarrick J. Wong 	/* Load the checksum driver */
38710441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
38720441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
38730441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
38740441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
38750441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
38760441984aSDarrick J. Wong 		goto failed_mount;
38770441984aSDarrick J. Wong 	}
38780441984aSDarrick J. Wong 
3879a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3880a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3881a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3882a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3883a9c47317SDarrick J. Wong 		silent = 1;
38846a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3885a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3886a9c47317SDarrick J. Wong 	}
3887a9c47317SDarrick J. Wong 
3888a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3889e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
38908c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3891dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3892a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3893a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3894a9c47317SDarrick J. Wong 
3895ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3896ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3897fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3898617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3899fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
390087f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3901fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3902617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3903fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3904ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3905fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
390603010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3907fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
39082e7842b8SHugh Dickins #endif
390998c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
391098c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
391198c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
391298c1a759SDarrick J. Wong 
3913617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3914fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3915617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3916fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3917617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3918fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3919ac27a0ecSDave Kleikamp 
3920617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3921fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3922bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3923fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3924bb4f397aSAneesh Kumar K.V 	else
3925fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
392645f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3927fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
39288b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3929fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3930ac27a0ecSDave Kleikamp 
393108cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
393208cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
393330773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
393430773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
393530773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3936ac27a0ecSDave Kleikamp 
39378b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3938fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3939ac27a0ecSDave Kleikamp 
39401e2462f9SMingming Cao 	/*
3941dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3942dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3943dd919b98SAneesh Kumar K.V 	 */
3944bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
39458b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3946fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3947dd919b98SAneesh Kumar K.V 
394851ce6511SLukas Czerner 	/*
394951ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
395051ce6511SLukas Czerner 	 * no mount option specified.
395151ce6511SLukas Czerner 	 */
395251ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
395351ce6511SLukas Czerner 
39544f97a681STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3955626b035bSRitesh Harjani 
3956626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
3957626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
3958626b035bSRitesh Harjani 
39594f97a681STheodore Ts'o 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
39604f97a681STheodore Ts'o 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
39614f97a681STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39624f97a681STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
39634f97a681STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
39644f97a681STheodore Ts'o 		goto failed_mount;
39654f97a681STheodore Ts'o 	}
39664f97a681STheodore Ts'o 
39679803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
39689803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
39699803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
39709803387cSTheodore Ts'o 	} else {
39719803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
39729803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
39739803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
39749803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
39759803387cSTheodore Ts'o 				 sbi->s_first_ino);
39769803387cSTheodore Ts'o 			goto failed_mount;
39779803387cSTheodore Ts'o 		}
39789803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
39799803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
39809803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
39819803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
39829803387cSTheodore Ts'o 			       "unsupported inode size: %d",
39839803387cSTheodore Ts'o 			       sbi->s_inode_size);
39844f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
39859803387cSTheodore Ts'o 			goto failed_mount;
39869803387cSTheodore Ts'o 		}
39879803387cSTheodore Ts'o 		/*
39889803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
39899803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
39909803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
39919803387cSTheodore Ts'o 		 * for all three.
39929803387cSTheodore Ts'o 		 */
39939803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
39949803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
39959803387cSTheodore Ts'o 			sb->s_time_gran = 1;
39969803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
39979803387cSTheodore Ts'o 		} else {
39989803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
39999803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
40009803387cSTheodore Ts'o 		}
40019803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
40029803387cSTheodore Ts'o 	}
40039803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
40049803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
40059803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
40069803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
40079803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
40089803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
40099803387cSTheodore Ts'o 
40109803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
40119803387cSTheodore Ts'o 			if (v > max) {
40129803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
40139803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
40149803387cSTheodore Ts'o 				goto failed_mount;
40159803387cSTheodore Ts'o 			}
40169803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
40179803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
40189803387cSTheodore Ts'o 
40199803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
40209803387cSTheodore Ts'o 			if (v > max) {
40219803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
40229803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
40239803387cSTheodore Ts'o 				goto failed_mount;
40249803387cSTheodore Ts'o 			}
40259803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
40269803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
40279803387cSTheodore Ts'o 		}
40289803387cSTheodore Ts'o 	}
40299803387cSTheodore Ts'o 
40305aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
40315aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
40325aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
40335aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
40345aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
40355aee0f8aSTheodore Ts'o 			goto failed_mount;
40365aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
40375aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
40388b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
40398b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
40405aee0f8aSTheodore Ts'o 				 s_mount_opts);
40415aee0f8aSTheodore Ts'o 		}
40425aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
40438b67f04aSTheodore Ts'o 	}
40445a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
4045b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
4046661aa520SEric Sandeen 			   &journal_ioprio, 0))
4047ac27a0ecSDave Kleikamp 		goto failed_mount;
4048ac27a0ecSDave Kleikamp 
4049c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4050c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
4051c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
4052c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
4053c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
4054c83ad55eSGabriel Krisman Bertazi 
4055c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
4056c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4057c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
4058c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4059c83ad55eSGabriel Krisman Bertazi 		}
4060c83ad55eSGabriel Krisman Bertazi 
4061c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
4062c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
4063c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4064c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
4065c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4066c83ad55eSGabriel Krisman Bertazi 		}
4067c83ad55eSGabriel Krisman Bertazi 
4068c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
4069c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
4070c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4071c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
4072c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4073c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4074c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4075c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4076c83ad55eSGabriel Krisman Bertazi 		}
4077c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4078c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4079c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4080c83ad55eSGabriel Krisman Bertazi 
4081c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
4082c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
4083c83ad55eSGabriel Krisman Bertazi 	}
4084c83ad55eSGabriel Krisman Bertazi #endif
4085c83ad55eSGabriel Krisman Bertazi 
408656889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4087244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
4088781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4089244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
409056889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
409156889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
409256889787STheodore Ts'o 				 "both data=journal and delalloc");
409356889787STheodore Ts'o 			goto failed_mount;
409456889787STheodore Ts'o 		}
4095fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4096923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4097923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4098923ae0ffSRoss Zwisler 			goto failed_mount;
4099923ae0ffSRoss Zwisler 		}
410073b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
410173b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
410273b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
410373b92a2aSSergey Karamov 				 "instead of data journaling mode");
410473b92a2aSSergey Karamov 		}
410556889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
410656889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4107001e4a87STejun Heo 	} else {
4108001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
410956889787STheodore Ts'o 	}
411056889787STheodore Ts'o 
41111751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
41121751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4113ac27a0ecSDave Kleikamp 
4114617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4115e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4116e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4117e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4118b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4119b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4120b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4121469108ffSTheodore Tso 
4122ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4123ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4124e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4125ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4126ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4127ed3654ebSTheodore Ts'o 			goto failed_mount;
4128ed3654ebSTheodore Ts'o 		}
4129dec214d0STahsin Erdogan 
4130dec214d0STahsin Erdogan 		/*
4131dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4132dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4133dec214d0STahsin Erdogan 		 */
4134dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4135dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4136dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4137dec214d0STahsin Erdogan 			goto failed_mount;
4138dec214d0STahsin Erdogan 		}
4139ed3654ebSTheodore Ts'o 	}
4140ed3654ebSTheodore Ts'o 
41412035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
41422035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
41432035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
41442035e776STheodore Ts'o 				 "using the ext4 subsystem");
41452035e776STheodore Ts'o 		else {
41460d9366d6SEric Sandeen 			/*
41470d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
41480d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
41490d9366d6SEric Sandeen 			 */
41500d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
41510d9366d6SEric Sandeen 				goto failed_mount;
41522035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
41532035e776STheodore Ts'o 				 "to feature incompatibilities");
41542035e776STheodore Ts'o 			goto failed_mount;
41552035e776STheodore Ts'o 		}
41562035e776STheodore Ts'o 	}
41572035e776STheodore Ts'o 
41582035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
41592035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
41602035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
41612035e776STheodore Ts'o 				 "using the ext4 subsystem");
41622035e776STheodore Ts'o 		else {
41630d9366d6SEric Sandeen 			/*
41640d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
41650d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
41660d9366d6SEric Sandeen 			 */
41670d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
41680d9366d6SEric Sandeen 				goto failed_mount;
41692035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
41702035e776STheodore Ts'o 				 "to feature incompatibilities");
41712035e776STheodore Ts'o 			goto failed_mount;
41722035e776STheodore Ts'o 		}
41732035e776STheodore Ts'o 	}
41742035e776STheodore Ts'o 
4175469108ffSTheodore Tso 	/*
4176ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4177ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4178ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4179ac27a0ecSDave Kleikamp 	 */
4180bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4181ac27a0ecSDave Kleikamp 		goto failed_mount;
4182a13fb1a4SEric Sandeen 
41838cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
41848cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
41858cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
41868cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
41878cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4188ac27a0ecSDave Kleikamp 		goto failed_mount;
4189ac27a0ecSDave Kleikamp 	}
4190bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4191bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4192bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4193bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4194bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4195bfe0a5f4STheodore Ts'o 		goto failed_mount;
4196bfe0a5f4STheodore Ts'o 	}
4197ac27a0ecSDave Kleikamp 
41985b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
41995b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
42005b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
42015b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
42025b9554dcSTheodore Ts'o 		goto failed_mount;
42035b9554dcSTheodore Ts'o 	}
42045b9554dcSTheodore Ts'o 
4205a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4206a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4207a8ab6d38SIra Weiny 
4208fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4209559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4210559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4211559db4c6SRoss Zwisler 					" that may contain inline data");
4212361d24d4SEric Sandeen 			goto failed_mount;
4213559db4c6SRoss Zwisler 		}
4214a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
421524f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4216361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4217361d24d4SEric Sandeen 			goto failed_mount;
421824f3478dSDan Williams 		}
4219923ae0ffSRoss Zwisler 	}
4220923ae0ffSRoss Zwisler 
4221e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
42226ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
42236ddb2447STheodore Ts'o 			 es->s_encryption_level);
42246ddb2447STheodore Ts'o 		goto failed_mount;
42256ddb2447STheodore Ts'o 	}
42266ddb2447STheodore Ts'o 
4227ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4228ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4229ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4230b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4231ce40733cSAneesh Kumar K.V 					blocksize);
4232ac27a0ecSDave Kleikamp 			goto failed_mount;
4233ac27a0ecSDave Kleikamp 		}
4234ac27a0ecSDave Kleikamp 
4235ac27a0ecSDave Kleikamp 		brelse(bh);
423670bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
423770bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4238a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4239ac27a0ecSDave Kleikamp 		if (!bh) {
4240b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4241b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4242ac27a0ecSDave Kleikamp 			goto failed_mount;
4243ac27a0ecSDave Kleikamp 		}
42442716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4245ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4246617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4247b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4248b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4249ac27a0ecSDave Kleikamp 			goto failed_mount;
4250ac27a0ecSDave Kleikamp 		}
4251ac27a0ecSDave Kleikamp 	}
4252ac27a0ecSDave Kleikamp 
4253e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4254f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4255f287a1a5STheodore Ts'o 						      has_huge_files);
4256f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4257ac27a0ecSDave Kleikamp 
42580d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4259e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
42608fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
42610d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4262d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4263b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4264b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
42650d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
42660d1ee42fSAlexandre Ratchov 			goto failed_mount;
42670d1ee42fSAlexandre Ratchov 		}
42680d1ee42fSAlexandre Ratchov 	} else
42690d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
42700b8e58a1SAndreas Dilger 
4271ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4272ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
42730b8e58a1SAndreas Dilger 
4274617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4275ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4276617ba13bSMingming Cao 		goto cantfind_ext4;
4277cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4278cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4279cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4280b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4281cd6bb35bSTheodore Ts'o 		goto failed_mount;
4282cd6bb35bSTheodore Ts'o 	}
4283ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4284ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
42850d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4286ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4287ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4288e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4289e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
42900b8e58a1SAndreas Dilger 
4291ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4292ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4293ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4294e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4295f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4296f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4297f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4298f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4299f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4300bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
430123301410STheodore Ts'o 				es->s_flags |=
430223301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4303f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4304f99b2589STheodore Ts'o #else
4305bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
430623301410STheodore Ts'o 				es->s_flags |=
430723301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4308f99b2589STheodore Ts'o #endif
4309f99b2589STheodore Ts'o 		}
431023301410STheodore Ts'o 	}
4311ac27a0ecSDave Kleikamp 
4312281b5995STheodore Ts'o 	/* Handle clustersize */
4313281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4314ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4315281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4316281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4317281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4318281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4319281b5995STheodore Ts'o 			goto failed_mount;
4320281b5995STheodore Ts'o 		}
4321281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4322281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4323281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4324281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4325281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4326281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4327281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4328281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4329281b5995STheodore Ts'o 			goto failed_mount;
4330281b5995STheodore Ts'o 		}
4331281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4332281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4333281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4334281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4335281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4336281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4337281b5995STheodore Ts'o 			goto failed_mount;
4338281b5995STheodore Ts'o 		}
4339281b5995STheodore Ts'o 	} else {
4340281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4341bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4342bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4343bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4344bfe0a5f4STheodore Ts'o 			goto failed_mount;
4345281b5995STheodore Ts'o 		}
4346ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4347b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4348b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4349ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4350ac27a0ecSDave Kleikamp 			goto failed_mount;
4351ac27a0ecSDave Kleikamp 		}
4352281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4353281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4354281b5995STheodore Ts'o 	}
4355281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4356281b5995STheodore Ts'o 
4357960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4358960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4359960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4360960fd856STheodore Ts'o 
4361bf43d84bSEric Sandeen 	/*
4362bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4363bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4364bf43d84bSEric Sandeen 	 */
43655a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
436630ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
43675a9ae68aSDarrick J. Wong 	if (err) {
4368b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4369bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4370ac27a0ecSDave Kleikamp 		goto failed_mount;
4371ac27a0ecSDave Kleikamp 	}
4372ac27a0ecSDave Kleikamp 
4373617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4374617ba13bSMingming Cao 		goto cantfind_ext4;
4375e7c95593SEric Sandeen 
43760f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
43770f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
43780f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4379b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4380b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
43810f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
43820f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
43830f2ddca6SFrom: Thiemo Nagel 	}
43840f2ddca6SFrom: Thiemo Nagel 
43854ec11028STheodore Ts'o 	/*
43864ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
43874ec11028STheodore Ts'o 	 * of the filesystem.
43884ec11028STheodore Ts'o 	 */
43894ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4390b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4391b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4392e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
43934ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4394e7c95593SEric Sandeen 		goto failed_mount;
4395e7c95593SEric Sandeen 	}
4396bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4397bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4398bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4399bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4400bfe0a5f4STheodore Ts'o 		goto failed_mount;
4401bfe0a5f4STheodore Ts'o 	}
4402bfe0a5f4STheodore Ts'o 
4403bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4404bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4405bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4406bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
44074ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4408df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
44094ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4410df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
44114ec11028STheodore Ts'o 		       ext4_blocks_count(es),
44124ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
44134ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
44144ec11028STheodore Ts'o 		goto failed_mount;
44154ec11028STheodore Ts'o 	}
4416bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4417fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4418fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
44199e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
44209e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
44219e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
44229e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
44239e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
44249e463084STheodore Ts'o 		ret = -EINVAL;
44259e463084STheodore Ts'o 		goto failed_mount;
44269e463084STheodore Ts'o 	}
4427617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4428617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
44293a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
44302ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
44313a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
44323a4b77cdSEryu Guan 				 "first meta block group too large: %u "
44333a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
44343a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
44353a4b77cdSEryu Guan 			goto failed_mount;
44363a4b77cdSEryu Guan 		}
44373a4b77cdSEryu Guan 	}
44381d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
44391d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4440f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
44411d0c3924STheodore Ts'o 					  GFP_KERNEL));
4442ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4443b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
44442cde417dSTheodore Ts'o 		ret = -ENOMEM;
4445ac27a0ecSDave Kleikamp 		goto failed_mount;
4446ac27a0ecSDave Kleikamp 	}
4447ac27a0ecSDave Kleikamp 
4448705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4449ac27a0ecSDave Kleikamp 
445085c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
445185c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
445285c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
4453d87f6392SRoman Gushchin 		sb_breadahead_unmovable(sb, block);
445485c8f176SAndrew Perepechko 	}
445585c8f176SAndrew Perepechko 
4456ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
44571d0c3924STheodore Ts'o 		struct buffer_head *bh;
44581d0c3924STheodore Ts'o 
445970bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
44601d0c3924STheodore Ts'o 		bh = sb_bread_unmovable(sb, block);
44611d0c3924STheodore Ts'o 		if (!bh) {
4462b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4463b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4464ac27a0ecSDave Kleikamp 			db_count = i;
4465ac27a0ecSDave Kleikamp 			goto failed_mount2;
4466ac27a0ecSDave Kleikamp 		}
44671d0c3924STheodore Ts'o 		rcu_read_lock();
44681d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
44691d0c3924STheodore Ts'o 		rcu_read_unlock();
4470ac27a0ecSDave Kleikamp 	}
447144de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4472829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4473b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
44746a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4475f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4476ac27a0ecSDave Kleikamp 	}
4477772cb7c8SJose R. Santos 
4478235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
447904496411STao Ma 
4480a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4481eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4482ce7e010aSTheodore Ts'o 		goto failed_mount3;
4483ce7e010aSTheodore Ts'o 
4484c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
448567a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4486c9de560dSAlex Tomas 
4487f9ae9cf5STheodore Ts'o 	/*
4488f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4489f9ae9cf5STheodore Ts'o 	 */
4490f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4491617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4492617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4493643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4494a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4495ffcc4182SEric Biggers #endif
4496c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4497c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4498c93d8f88SEric Biggers #endif
4499ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4500617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4501e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
45021fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4503262b4662SJan Kara 	else
4504262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4505689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4506ac27a0ecSDave Kleikamp #endif
450785787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4508f2fa2ffcSAneesh Kumar K.V 
4509ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
45103b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4511ac27a0ecSDave Kleikamp 
4512ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4513ac27a0ecSDave Kleikamp 
4514ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4515e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4516ac27a0ecSDave Kleikamp 
4517bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4518c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
451950460fe8SDarrick J. Wong 			goto failed_mount3a;
4520c5e06d10SJohann Lombardi 
4521ac27a0ecSDave Kleikamp 	/*
4522ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4523ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4524ac27a0ecSDave Kleikamp 	 */
4525e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
45264753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
45274753d8a2STheodore Ts'o 		if (err)
452850460fe8SDarrick J. Wong 			goto failed_mount3a;
4529bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4530e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4531b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4532b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4533744692dcSJiaying Zhang 		goto failed_mount_wq;
4534ac27a0ecSDave Kleikamp 	} else {
45351e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
45361e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
45371e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45381e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
45391e381f60SDmitry Monakhov 			goto failed_mount_wq;
45401e381f60SDmitry Monakhov 		}
45411e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
45421e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45431e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
45441e381f60SDmitry Monakhov 			goto failed_mount_wq;
45451e381f60SDmitry Monakhov 		}
45461e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
45471e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45481e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
45491e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
45501e381f60SDmitry Monakhov 			goto failed_mount_wq;
45511e381f60SDmitry Monakhov 		}
45521e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
45531e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
45541e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45551e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
45561e381f60SDmitry Monakhov 			goto failed_mount_wq;
45571e381f60SDmitry Monakhov 		}
455850b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
45591e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4560fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
45610390131bSFrank Mayhar 		sbi->s_journal = NULL;
45620390131bSFrank Mayhar 		needs_recovery = 0;
45630390131bSFrank Mayhar 		goto no_journal;
4564ac27a0ecSDave Kleikamp 	}
4565ac27a0ecSDave Kleikamp 
4566e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4567eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4568eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4569b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4570744692dcSJiaying Zhang 		goto failed_mount_wq;
4571eb40a09cSJose R. Santos 	}
4572eb40a09cSJose R. Santos 
457325ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
457425ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
457525ed6e8aSDarrick J. Wong 			 "feature set");
457625ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4577d4da6c9cSLinus Torvalds 	}
4578818d276cSGirish Shilamkar 
4579ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4580ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4581ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4582ac27a0ecSDave Kleikamp 	case 0:
4583ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
458463f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
458563f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
458663f57933SAndrew Morton 		 */
4587dab291afSMingming Cao 		if (jbd2_journal_check_available_features
458827f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4589fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
459027f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
459127f394a7STyson Nottingham 		} else {
4592fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
459327f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
459427f394a7STyson Nottingham 		}
4595ac27a0ecSDave Kleikamp 		break;
4596ac27a0ecSDave Kleikamp 
4597617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4598617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4599dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4600dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4601b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4602b31e1552SEric Sandeen 			       "requested data journaling mode");
4603744692dcSJiaying Zhang 			goto failed_mount_wq;
4604ac27a0ecSDave Kleikamp 		}
4605ac27a0ecSDave Kleikamp 	default:
4606ac27a0ecSDave Kleikamp 		break;
4607ac27a0ecSDave Kleikamp 	}
4608ab04df78SJan Kara 
4609ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4610ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4611ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4612ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4613ab04df78SJan Kara 		goto failed_mount_wq;
4614ab04df78SJan Kara 	}
4615ab04df78SJan Kara 
4616b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4617ac27a0ecSDave Kleikamp 
461818aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
461918aadd47SBobi Jam 
4620ce7e010aSTheodore Ts'o no_journal:
4621cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
462247387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
462347387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4624cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4625cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
46269c191f70ST Makphaibulchoke 			goto failed_mount_wq;
46279c191f70ST Makphaibulchoke 		}
46289c191f70ST Makphaibulchoke 
4629dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4630dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4631dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4632dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4633dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4634dec214d0STahsin Erdogan 				goto failed_mount_wq;
4635dec214d0STahsin Erdogan 			}
4636dec214d0STahsin Erdogan 		}
4637cdb7ee4cSTahsin Erdogan 	}
4638dec214d0STahsin Erdogan 
4639c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4640c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4641c93d8f88SEric Biggers 		goto failed_mount_wq;
4642c93d8f88SEric Biggers 	}
4643c93d8f88SEric Biggers 
4644bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4645e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4646e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
46476ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
46486ddb2447STheodore Ts'o 	}
46496ddb2447STheodore Ts'o 
4650fd89d5f2STejun Heo 	/*
4651952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4652952fc18eSTheodore Ts'o 	 * superblock if present.
4653952fc18eSTheodore Ts'o 	 */
4654952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4655952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4656952fc18eSTheodore Ts'o 	else {
465707aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
465807aa2ea1SLukas Czerner 		if (err)
4659952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4660952fc18eSTheodore Ts'o 	}
4661952fc18eSTheodore Ts'o 
4662952fc18eSTheodore Ts'o 	/*
4663fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4664fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4665fd89d5f2STejun Heo 	 */
46662e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
46672e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
46682e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
46692e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
467007aa2ea1SLukas Czerner 		ret = -ENOMEM;
46712e8fa54eSJan Kara 		goto failed_mount4;
46722e8fa54eSJan Kara 	}
46732e8fa54eSJan Kara 
4674ac27a0ecSDave Kleikamp 	/*
4675dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4676ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4677ac27a0ecSDave Kleikamp 	 */
4678ac27a0ecSDave Kleikamp 
46798a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
46801d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4681b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
46821d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
468332a9bb57SManish Katiyar 		root = NULL;
4684ac27a0ecSDave Kleikamp 		goto failed_mount4;
4685ac27a0ecSDave Kleikamp 	}
4686ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4687b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
468894bf608aSAl Viro 		iput(root);
4689ac27a0ecSDave Kleikamp 		goto failed_mount4;
4690ac27a0ecSDave Kleikamp 	}
4691b886ee3eSGabriel Krisman Bertazi 
4692b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4693b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4694b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4695b886ee3eSGabriel Krisman Bertazi #endif
4696b886ee3eSGabriel Krisman Bertazi 
469748fde701SAl Viro 	sb->s_root = d_make_root(root);
46981d1fe1eeSDavid Howells 	if (!sb->s_root) {
4699b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
47001d1fe1eeSDavid Howells 		ret = -ENOMEM;
47011d1fe1eeSDavid Howells 		goto failed_mount4;
47021d1fe1eeSDavid Howells 	}
4703ac27a0ecSDave Kleikamp 
4704c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4705c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
47061751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4707c89128a0SJaegeuk Kim 		ret = 0;
4708c89128a0SJaegeuk Kim 	} else if (ret)
4709c89128a0SJaegeuk Kim 		goto failed_mount4a;
4710ef7f3835SKalpak Shah 
4711b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
471227dd4385SLukas Czerner 
47136fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
47146fd058f7STheodore Ts'o 	if (err) {
4715b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4716fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4717f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4718f9ae9cf5STheodore Ts'o 	}
4719f9ae9cf5STheodore Ts'o 
4720f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4721f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4722f9ae9cf5STheodore Ts'o 	if (err) {
4723f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4724f9ae9cf5STheodore Ts'o 			 err);
4725dcf2d804STao Ma 		goto failed_mount5;
4726c2774d84SAneesh Kumar K.V 	}
4727c2774d84SAneesh Kumar K.V 
4728d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4729d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4730d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
47314274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4732908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4733908c7f19STejun Heo 				  GFP_KERNEL);
4734d5e03cbbSTheodore Ts'o 	if (!err) {
4735d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4736d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
47374274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4738908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4739908c7f19STejun Heo 					  GFP_KERNEL);
4740d5e03cbbSTheodore Ts'o 	}
4741d5e03cbbSTheodore Ts'o 	if (!err)
4742d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4743908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4744d5e03cbbSTheodore Ts'o 	if (!err)
4745908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4746908c7f19STejun Heo 					  GFP_KERNEL);
4747c8585c6fSDaeho Jeong 	if (!err)
4748bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4749c8585c6fSDaeho Jeong 
4750d5e03cbbSTheodore Ts'o 	if (err) {
4751d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4752d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4753d5e03cbbSTheodore Ts'o 	}
4754d5e03cbbSTheodore Ts'o 
4755e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4756d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4757d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4758d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4759d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4760d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4761d5e03cbbSTheodore Ts'o 		}
4762d5e03cbbSTheodore Ts'o 
4763bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4764bfff6873SLukas Czerner 	if (err)
4765dcf2d804STao Ma 		goto failed_mount6;
4766bfff6873SLukas Czerner 
4767b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4768dcf2d804STao Ma 	if (err)
4769dcf2d804STao Ma 		goto failed_mount7;
47703197ebdbSTheodore Ts'o 
47719b2ff357SJan Kara #ifdef CONFIG_QUOTA
47729b2ff357SJan Kara 	/* Enable quota usage during mount. */
4773bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
47749b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
47759b2ff357SJan Kara 		if (err)
47769b2ff357SJan Kara 			goto failed_mount8;
47779b2ff357SJan Kara 	}
47789b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
47799b2ff357SJan Kara 
4780617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4781617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4782617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
47830390131bSFrank Mayhar 	if (needs_recovery) {
4784b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4785617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
47860390131bSFrank Mayhar 	}
47870390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
47880390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
47890390131bSFrank Mayhar 			descr = " journalled data mode";
47900390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
47910390131bSFrank Mayhar 			descr = " ordered data mode";
47920390131bSFrank Mayhar 		else
47930390131bSFrank Mayhar 			descr = " writeback data mode";
47940390131bSFrank Mayhar 	} else
47950390131bSFrank Mayhar 		descr = "out journal";
47960390131bSFrank Mayhar 
479779add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
479879add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
479979add3a3SLukas Czerner 		if (!blk_queue_discard(q))
480079add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
480179add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
480279add3a3SLukas Czerner 				 "the device does not support discard");
480379add3a3SLukas Czerner 	}
480479add3a3SLukas Czerner 
4805e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4806d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
48075aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
48085aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
48095aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
48108b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4811ac27a0ecSDave Kleikamp 
481266e61a9eSTheodore Ts'o 	if (es->s_error_count)
481366e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4814ac27a0ecSDave Kleikamp 
4815efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4816efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4817efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4818efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4819efbed4dcSTheodore Ts'o 
4820d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4821ac27a0ecSDave Kleikamp 	return 0;
4822ac27a0ecSDave Kleikamp 
4823617ba13bSMingming Cao cantfind_ext4:
4824ac27a0ecSDave Kleikamp 	if (!silent)
4825b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4826ac27a0ecSDave Kleikamp 	goto failed_mount;
4827ac27a0ecSDave Kleikamp 
482872ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
482972ba7450STheodore Ts'o failed_mount8:
4830ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
483172ba7450STheodore Ts'o #endif
4832dcf2d804STao Ma failed_mount7:
4833dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4834dcf2d804STao Ma failed_mount6:
4835f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
48367c990728SSuraj Jitindar Singh 	rcu_read_lock();
48377c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
48387c990728SSuraj Jitindar Singh 	if (flex_groups) {
48397c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
48407c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
48417c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
48427c990728SSuraj Jitindar Singh 	}
48437c990728SSuraj Jitindar Singh 	rcu_read_unlock();
4844d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4845d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4846d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4847d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4848bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
484900764937SAzat Khuzhin failed_mount5:
4850f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4851f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4852f9ae9cf5STheodore Ts'o failed_mount4a:
485394bf608aSAl Viro 	dput(sb->s_root);
485432a9bb57SManish Katiyar 	sb->s_root = NULL;
485594bf608aSAl Viro failed_mount4:
4856b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
48572e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
48582e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
48594c0425ffSMingming Cao failed_mount_wq:
4860dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4861dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
486250c15df6SChengguang Xu 
486347387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
486447387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
486550c15df6SChengguang Xu 
48660390131bSFrank Mayhar 	if (sbi->s_journal) {
4867dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
486847b4a50bSJan Kara 		sbi->s_journal = NULL;
48690390131bSFrank Mayhar 	}
487050460fe8SDarrick J. Wong failed_mount3a:
4871d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4872eb68d0e2SZheng Liu failed_mount3:
48739105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4874c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4875c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4876ac27a0ecSDave Kleikamp failed_mount2:
48771d0c3924STheodore Ts'o 	rcu_read_lock();
48781d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
4879ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
48801d0c3924STheodore Ts'o 		brelse(group_desc[i]);
48811d0c3924STheodore Ts'o 	kvfree(group_desc);
48821d0c3924STheodore Ts'o 	rcu_read_unlock();
4883ac27a0ecSDave Kleikamp failed_mount:
48840441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
48850441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4886c83ad55eSGabriel Krisman Bertazi 
4887c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4888c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4889c83ad55eSGabriel Krisman Bertazi #endif
4890c83ad55eSGabriel Krisman Bertazi 
4891ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4892a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
48930ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4894ac27a0ecSDave Kleikamp #endif
4895ed318a6cSEric Biggers 	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
4896617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4897ac27a0ecSDave Kleikamp 	brelse(bh);
4898ac27a0ecSDave Kleikamp out_fail:
4899ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4900f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
49015aee0f8aSTheodore Ts'o out_free_base:
4902ac27a0ecSDave Kleikamp 	kfree(sbi);
4903d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
49045e405595SDan Williams 	fs_put_dax(dax_dev);
490507aa2ea1SLukas Czerner 	return err ? err : ret;
4906ac27a0ecSDave Kleikamp }
4907ac27a0ecSDave Kleikamp 
4908ac27a0ecSDave Kleikamp /*
4909ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4910ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4911ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4912ac27a0ecSDave Kleikamp  */
4913617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4914ac27a0ecSDave Kleikamp {
4915617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4916ac27a0ecSDave Kleikamp 
4917ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
491830773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
491930773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4920ac27a0ecSDave Kleikamp 
4921a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4922ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4923dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4924ac27a0ecSDave Kleikamp 	else
4925dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
49265bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
49275bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
49285bf5683aSHidehiro Kawai 	else
49295bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4930a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4931ac27a0ecSDave Kleikamp }
4932ac27a0ecSDave Kleikamp 
4933c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4934ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4935ac27a0ecSDave Kleikamp {
4936ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4937ac27a0ecSDave Kleikamp 
4938c6cb7e77SEric Whitney 	/*
4939c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4940c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4941c6cb7e77SEric Whitney 	 * will try to delete it.
4942c6cb7e77SEric Whitney 	 */
49438a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
49441d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4945b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4946ac27a0ecSDave Kleikamp 		return NULL;
4947ac27a0ecSDave Kleikamp 	}
4948ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4949ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4950ac27a0ecSDave Kleikamp 		iput(journal_inode);
4951b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4952ac27a0ecSDave Kleikamp 		return NULL;
4953ac27a0ecSDave Kleikamp 	}
4954ac27a0ecSDave Kleikamp 
4955e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4956ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
49571d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4958b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4959ac27a0ecSDave Kleikamp 		iput(journal_inode);
4960ac27a0ecSDave Kleikamp 		return NULL;
4961ac27a0ecSDave Kleikamp 	}
4962c6cb7e77SEric Whitney 	return journal_inode;
4963c6cb7e77SEric Whitney }
4964c6cb7e77SEric Whitney 
4965c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4966c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4967c6cb7e77SEric Whitney {
4968c6cb7e77SEric Whitney 	struct inode *journal_inode;
4969c6cb7e77SEric Whitney 	journal_t *journal;
4970c6cb7e77SEric Whitney 
4971c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4972c6cb7e77SEric Whitney 
4973c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4974c6cb7e77SEric Whitney 	if (!journal_inode)
4975c6cb7e77SEric Whitney 		return NULL;
4976ac27a0ecSDave Kleikamp 
4977dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4978ac27a0ecSDave Kleikamp 	if (!journal) {
4979b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4980ac27a0ecSDave Kleikamp 		iput(journal_inode);
4981ac27a0ecSDave Kleikamp 		return NULL;
4982ac27a0ecSDave Kleikamp 	}
4983ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4984617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4985ac27a0ecSDave Kleikamp 	return journal;
4986ac27a0ecSDave Kleikamp }
4987ac27a0ecSDave Kleikamp 
4988617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4989ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4990ac27a0ecSDave Kleikamp {
4991ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4992ac27a0ecSDave Kleikamp 	journal_t *journal;
4993617ba13bSMingming Cao 	ext4_fsblk_t start;
4994617ba13bSMingming Cao 	ext4_fsblk_t len;
4995ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4996617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4997ac27a0ecSDave Kleikamp 	unsigned long offset;
4998617ba13bSMingming Cao 	struct ext4_super_block *es;
4999ac27a0ecSDave Kleikamp 	struct block_device *bdev;
5000ac27a0ecSDave Kleikamp 
5001e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
50020390131bSFrank Mayhar 
5003b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5004ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5005ac27a0ecSDave Kleikamp 		return NULL;
5006ac27a0ecSDave Kleikamp 
5007ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5008e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5009ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5010b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5011b31e1552SEric Sandeen 			"blocksize too small for journal device");
5012ac27a0ecSDave Kleikamp 		goto out_bdev;
5013ac27a0ecSDave Kleikamp 	}
5014ac27a0ecSDave Kleikamp 
5015617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5016617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5017ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5018ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5019b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5020b31e1552SEric Sandeen 		       "external journal");
5021ac27a0ecSDave Kleikamp 		goto out_bdev;
5022ac27a0ecSDave Kleikamp 	}
5023ac27a0ecSDave Kleikamp 
50242716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5025617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5026ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5027617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5028b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5029b31e1552SEric Sandeen 					"bad superblock");
5030ac27a0ecSDave Kleikamp 		brelse(bh);
5031ac27a0ecSDave Kleikamp 		goto out_bdev;
5032ac27a0ecSDave Kleikamp 	}
5033ac27a0ecSDave Kleikamp 
5034df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5035df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5036df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5037df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5038df4763beSDarrick J. Wong 				       "corrupt superblock");
5039df4763beSDarrick J. Wong 		brelse(bh);
5040df4763beSDarrick J. Wong 		goto out_bdev;
5041df4763beSDarrick J. Wong 	}
5042df4763beSDarrick J. Wong 
5043617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5044b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5045ac27a0ecSDave Kleikamp 		brelse(bh);
5046ac27a0ecSDave Kleikamp 		goto out_bdev;
5047ac27a0ecSDave Kleikamp 	}
5048ac27a0ecSDave Kleikamp 
5049bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5050ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5051ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5052ac27a0ecSDave Kleikamp 
5053dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5054ac27a0ecSDave Kleikamp 					start, len, blocksize);
5055ac27a0ecSDave Kleikamp 	if (!journal) {
5056b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5057ac27a0ecSDave Kleikamp 		goto out_bdev;
5058ac27a0ecSDave Kleikamp 	}
5059ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5060dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
5061ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
5062ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
5063b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5064ac27a0ecSDave Kleikamp 		goto out_journal;
5065ac27a0ecSDave Kleikamp 	}
5066ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5067b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5068b31e1552SEric Sandeen 					"user (unsupported) - %d",
5069ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5070ac27a0ecSDave Kleikamp 		goto out_journal;
5071ac27a0ecSDave Kleikamp 	}
5072617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
5073617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5074ac27a0ecSDave Kleikamp 	return journal;
50750b8e58a1SAndreas Dilger 
5076ac27a0ecSDave Kleikamp out_journal:
5077dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5078ac27a0ecSDave Kleikamp out_bdev:
5079617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5080ac27a0ecSDave Kleikamp 	return NULL;
5081ac27a0ecSDave Kleikamp }
5082ac27a0ecSDave Kleikamp 
5083617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5084617ba13bSMingming Cao 			     struct ext4_super_block *es,
5085ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5086ac27a0ecSDave Kleikamp {
5087ac27a0ecSDave Kleikamp 	journal_t *journal;
5088ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5089ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5090ac27a0ecSDave Kleikamp 	int err = 0;
5091ac27a0ecSDave Kleikamp 	int really_read_only;
5092ac27a0ecSDave Kleikamp 
5093e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
50940390131bSFrank Mayhar 
5095ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5096ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5097b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5098b31e1552SEric Sandeen 			"numbers have changed");
5099ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5100ac27a0ecSDave Kleikamp 	} else
5101ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5102ac27a0ecSDave Kleikamp 
5103ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
5104ac27a0ecSDave Kleikamp 
5105ac27a0ecSDave Kleikamp 	/*
5106ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5107ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5108ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5109ac27a0ecSDave Kleikamp 	 */
5110e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5111bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5112b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5113b31e1552SEric Sandeen 					"required on readonly filesystem");
5114ac27a0ecSDave Kleikamp 			if (really_read_only) {
5115b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5116d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5117d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5118ac27a0ecSDave Kleikamp 				return -EROFS;
5119ac27a0ecSDave Kleikamp 			}
5120b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5121b31e1552SEric Sandeen 			       "be enabled during recovery");
5122ac27a0ecSDave Kleikamp 		}
5123ac27a0ecSDave Kleikamp 	}
5124ac27a0ecSDave Kleikamp 
5125ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
5126b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
5127b31e1552SEric Sandeen 		       "and inode journals!");
5128ac27a0ecSDave Kleikamp 		return -EINVAL;
5129ac27a0ecSDave Kleikamp 	}
5130ac27a0ecSDave Kleikamp 
5131ac27a0ecSDave Kleikamp 	if (journal_inum) {
5132617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
5133ac27a0ecSDave Kleikamp 			return -EINVAL;
5134ac27a0ecSDave Kleikamp 	} else {
5135617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
5136ac27a0ecSDave Kleikamp 			return -EINVAL;
5137ac27a0ecSDave Kleikamp 	}
5138ac27a0ecSDave Kleikamp 
513990576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5140b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
51414776004fSTheodore Ts'o 
5142e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5143dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
51441c13d5c0STheodore Ts'o 	if (!err) {
51451c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
51461c13d5c0STheodore Ts'o 		if (save)
51471c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
51481c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5149dab291afSMingming Cao 		err = jbd2_journal_load(journal);
51501c13d5c0STheodore Ts'o 		if (save)
51511c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
51521c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
51531c13d5c0STheodore Ts'o 		kfree(save);
51541c13d5c0STheodore Ts'o 	}
5155ac27a0ecSDave Kleikamp 
5156ac27a0ecSDave Kleikamp 	if (err) {
5157b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5158dab291afSMingming Cao 		jbd2_journal_destroy(journal);
5159ac27a0ecSDave Kleikamp 		return err;
5160ac27a0ecSDave Kleikamp 	}
5161ac27a0ecSDave Kleikamp 
5162617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
5163617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
5164ac27a0ecSDave Kleikamp 
5165c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5166ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5167ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5168ac27a0ecSDave Kleikamp 
5169ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5170e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5171ac27a0ecSDave Kleikamp 	}
5172ac27a0ecSDave Kleikamp 
5173ac27a0ecSDave Kleikamp 	return 0;
5174ac27a0ecSDave Kleikamp }
5175ac27a0ecSDave Kleikamp 
5176e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5177ac27a0ecSDave Kleikamp {
5178e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5179617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5180c4be0c1dSTakashi Sato 	int error = 0;
5181ac27a0ecSDave Kleikamp 
5182bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5183c4be0c1dSTakashi Sato 		return error;
5184a17712c8SJon Derrick 
5185a17712c8SJon Derrick 	/*
5186a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
5187a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
5188a17712c8SJon Derrick 	 */
5189a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
5190a17712c8SJon Derrick 		return error;
5191a17712c8SJon Derrick 
519271290b36STheodore Ts'o 	/*
519371290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
519471290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
519571290b36STheodore Ts'o 	 * write time when we are mounting the root file system
519671290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
519771290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
519871290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
519971290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
520071290b36STheodore Ts'o 	 * to complain and force a full file system check.
520171290b36STheodore Ts'o 	 */
52021751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
52036a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5204f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5205afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5206afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5207dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5208dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5209afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5210f613dfcbSTheodore Ts'o 	else
5211f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5212f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5213d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
521457042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
521557042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
521657042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5217d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
52187f93cff9STheodore Ts'o 		es->s_free_inodes_count =
52197f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
52205d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5221ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
522206db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
52231566a48aSTheodore Ts'o 	if (sync)
52244743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5225e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
52264743f839SPranay Kr. Srivastava 		/*
52274743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
52284743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
52294743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
52304743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
52314743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
52324743f839SPranay Kr. Srivastava 		 * write and hope for the best.
52334743f839SPranay Kr. Srivastava 		 */
52344743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
52354743f839SPranay Kr. Srivastava 		       "superblock detected");
52364743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
52374743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
52384743f839SPranay Kr. Srivastava 	}
5239ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5240914258bfSTheodore Ts'o 	if (sync) {
52411566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5242564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
524300473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5244c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5245b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5246b31e1552SEric Sandeen 			       "superblock");
5247914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5248914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5249914258bfSTheodore Ts'o 		}
5250914258bfSTheodore Ts'o 	}
5251c4be0c1dSTakashi Sato 	return error;
5252ac27a0ecSDave Kleikamp }
5253ac27a0ecSDave Kleikamp 
5254ac27a0ecSDave Kleikamp /*
5255ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5256ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5257ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5258ac27a0ecSDave Kleikamp  */
5259617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5260617ba13bSMingming Cao 					struct ext4_super_block *es)
5261ac27a0ecSDave Kleikamp {
5262617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5263ac27a0ecSDave Kleikamp 
5264e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
52650390131bSFrank Mayhar 		BUG_ON(journal != NULL);
52660390131bSFrank Mayhar 		return;
52670390131bSFrank Mayhar 	}
5268dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
52697ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
52707ffe1ea8SHidehiro Kawai 		goto out;
52717ffe1ea8SHidehiro Kawai 
5272bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5273e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5274e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5275ac27a0ecSDave Kleikamp 	}
52767ffe1ea8SHidehiro Kawai 
52777ffe1ea8SHidehiro Kawai out:
5278dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5279ac27a0ecSDave Kleikamp }
5280ac27a0ecSDave Kleikamp 
5281ac27a0ecSDave Kleikamp /*
5282ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5283ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5284ac27a0ecSDave Kleikamp  * main filesystem now.
5285ac27a0ecSDave Kleikamp  */
5286617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5287617ba13bSMingming Cao 				   struct ext4_super_block *es)
5288ac27a0ecSDave Kleikamp {
5289ac27a0ecSDave Kleikamp 	journal_t *journal;
5290ac27a0ecSDave Kleikamp 	int j_errno;
5291ac27a0ecSDave Kleikamp 	const char *errstr;
5292ac27a0ecSDave Kleikamp 
5293e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
52940390131bSFrank Mayhar 
5295617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5296ac27a0ecSDave Kleikamp 
5297ac27a0ecSDave Kleikamp 	/*
5298ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5299617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5300ac27a0ecSDave Kleikamp 	 */
5301ac27a0ecSDave Kleikamp 
5302dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5303ac27a0ecSDave Kleikamp 	if (j_errno) {
5304ac27a0ecSDave Kleikamp 		char nbuf[16];
5305ac27a0ecSDave Kleikamp 
5306617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
530712062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5308ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
530912062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5310ac27a0ecSDave Kleikamp 
5311617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5312617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5313e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5314ac27a0ecSDave Kleikamp 
5315dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5316d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5317ac27a0ecSDave Kleikamp 	}
5318ac27a0ecSDave Kleikamp }
5319ac27a0ecSDave Kleikamp 
5320ac27a0ecSDave Kleikamp /*
5321ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5322ac27a0ecSDave Kleikamp  * and wait on the commit.
5323ac27a0ecSDave Kleikamp  */
5324617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5325ac27a0ecSDave Kleikamp {
5326ac27a0ecSDave Kleikamp 	journal_t *journal;
5327ac27a0ecSDave Kleikamp 
5328bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5329ac27a0ecSDave Kleikamp 		return 0;
5330ac27a0ecSDave Kleikamp 
5331617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5332b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5333ac27a0ecSDave Kleikamp }
5334ac27a0ecSDave Kleikamp 
5335617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5336ac27a0ecSDave Kleikamp {
533714ce0cb4STheodore Ts'o 	int ret = 0;
53389eddacf9SJan Kara 	tid_t target;
533906a407f1SDmitry Monakhov 	bool needs_barrier = false;
53408d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5341ac27a0ecSDave Kleikamp 
534249598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
53430db1ff22STheodore Ts'o 		return 0;
53440db1ff22STheodore Ts'o 
53459bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
53462e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5347a1177825SJan Kara 	/*
5348a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5349a1177825SJan Kara 	 * no dirty dquots
5350a1177825SJan Kara 	 */
5351a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
535206a407f1SDmitry Monakhov 	/*
535306a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
535406a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
535506a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
535606a407f1SDmitry Monakhov 	 */
5357bda32530STheodore Ts'o 	if (sbi->s_journal) {
535806a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
535906a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
536006a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
536106a407f1SDmitry Monakhov 			needs_barrier = true;
536206a407f1SDmitry Monakhov 
53638d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5364ac27a0ecSDave Kleikamp 			if (wait)
5365bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5366bda32530STheodore Ts'o 							   target);
53670390131bSFrank Mayhar 		}
5368bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5369bda32530STheodore Ts'o 		needs_barrier = true;
537006a407f1SDmitry Monakhov 	if (needs_barrier) {
537106a407f1SDmitry Monakhov 		int err;
53729398554fSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
537306a407f1SDmitry Monakhov 		if (!ret)
537406a407f1SDmitry Monakhov 			ret = err;
537506a407f1SDmitry Monakhov 	}
537606a407f1SDmitry Monakhov 
537706a407f1SDmitry Monakhov 	return ret;
537806a407f1SDmitry Monakhov }
537906a407f1SDmitry Monakhov 
5380ac27a0ecSDave Kleikamp /*
5381ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5382ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5383be4f27d3SYongqiang Yang  *
5384be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
53858e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
53868e8ad8a5SJan Kara  * modifications.
5387ac27a0ecSDave Kleikamp  */
5388c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5389ac27a0ecSDave Kleikamp {
5390c4be0c1dSTakashi Sato 	int error = 0;
5391c4be0c1dSTakashi Sato 	journal_t *journal;
5392ac27a0ecSDave Kleikamp 
5393bc98a42cSDavid Howells 	if (sb_rdonly(sb))
53949ca92389STheodore Ts'o 		return 0;
53959ca92389STheodore Ts'o 
5396c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5397ac27a0ecSDave Kleikamp 
5398bb044576STheodore Ts'o 	if (journal) {
5399ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5400dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
54017ffe1ea8SHidehiro Kawai 
54027ffe1ea8SHidehiro Kawai 		/*
5403bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5404bb044576STheodore Ts'o 		 * flush the journal.
54057ffe1ea8SHidehiro Kawai 		 */
5406c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
54076b0310fbSEric Sandeen 		if (error < 0)
54086b0310fbSEric Sandeen 			goto out;
5409ac27a0ecSDave Kleikamp 
5410ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5411e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5412c642dc9eSEric Sandeen 	}
5413c642dc9eSEric Sandeen 
5414e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
54156b0310fbSEric Sandeen out:
5416bb044576STheodore Ts'o 	if (journal)
54178e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5418bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
54196b0310fbSEric Sandeen 	return error;
5420ac27a0ecSDave Kleikamp }
5421ac27a0ecSDave Kleikamp 
5422ac27a0ecSDave Kleikamp /*
5423ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5424ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5425ac27a0ecSDave Kleikamp  */
5426c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5427ac27a0ecSDave Kleikamp {
5428bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
54299ca92389STheodore Ts'o 		return 0;
54309ca92389STheodore Ts'o 
5431c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
54329ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5433e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5434c642dc9eSEric Sandeen 	}
5435c642dc9eSEric Sandeen 
5436e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5437c4be0c1dSTakashi Sato 	return 0;
5438ac27a0ecSDave Kleikamp }
5439ac27a0ecSDave Kleikamp 
5440673c6100STheodore Ts'o /*
5441673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5442673c6100STheodore Ts'o  */
5443673c6100STheodore Ts'o struct ext4_mount_options {
5444673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5445a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
544608cefc7aSEric W. Biederman 	kuid_t s_resuid;
544708cefc7aSEric W. Biederman 	kgid_t s_resgid;
5448673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5449673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5450673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5451673c6100STheodore Ts'o 	int s_jquota_fmt;
5452a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5453673c6100STheodore Ts'o #endif
5454673c6100STheodore Ts'o };
5455673c6100STheodore Ts'o 
5456617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5457ac27a0ecSDave Kleikamp {
5458617ba13bSMingming Cao 	struct ext4_super_block *es;
5459617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5460ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5461617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5462c79d967dSChristoph Hellwig 	int enable_quota = 0;
54638a266467STheodore Ts'o 	ext4_group_t g;
5464b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5465c5e06d10SJohann Lombardi 	int err = 0;
5466ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
546703dafb5fSChen Gang 	int i, j;
546833458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5469ac27a0ecSDave Kleikamp #endif
5470d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5471ac27a0ecSDave Kleikamp 
547221ac738eSChengguang Xu 	if (data && !orig_data)
547321ac738eSChengguang Xu 		return -ENOMEM;
547421ac738eSChengguang Xu 
5475ac27a0ecSDave Kleikamp 	/* Store the original options */
5476ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5477ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5478a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5479ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5480ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5481ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
548230773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
548330773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5484ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5485ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5486a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
548703dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
548833458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
548933458eabSTheodore Ts'o 
549033458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
549103dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
549203dafb5fSChen Gang 				for (j = 0; j < i; j++)
549303dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
54943e36a163SWei Yongjun 				kfree(orig_data);
549503dafb5fSChen Gang 				return -ENOMEM;
549603dafb5fSChen Gang 			}
549703dafb5fSChen Gang 		} else
549803dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5499ac27a0ecSDave Kleikamp #endif
5500b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5501b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5502ac27a0ecSDave Kleikamp 
5503661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5504ac27a0ecSDave Kleikamp 		err = -EINVAL;
5505ac27a0ecSDave Kleikamp 		goto restore_opts;
5506ac27a0ecSDave Kleikamp 	}
5507ac27a0ecSDave Kleikamp 
55086b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
55096b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
55106b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
55112d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
55122d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5513c6d3d56dSDarrick J. Wong 	}
5514c6d3d56dSDarrick J. Wong 
55156ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
55166ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
55176ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
55186ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
55196ae6514bSPiotr Sarna 			err = -EINVAL;
55206ae6514bSPiotr Sarna 			goto restore_opts;
55216ae6514bSPiotr Sarna 		}
55226ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
55236ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
55246ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
55256ae6514bSPiotr Sarna 			err = -EINVAL;
55266ae6514bSPiotr Sarna 			goto restore_opts;
55276ae6514bSPiotr Sarna 		}
5528ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5529ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5530ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5531ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5532ab04df78SJan Kara 			err = -EINVAL;
5533ab04df78SJan Kara 			goto restore_opts;
5534ab04df78SJan Kara 		}
5535923ae0ffSRoss Zwisler 	}
5536923ae0ffSRoss Zwisler 
5537cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5538cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5539cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5540cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5541cdb7ee4cSTahsin Erdogan 	}
5542cdb7ee4cSTahsin Erdogan 
55434ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
554454d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5545ac27a0ecSDave Kleikamp 
55461751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
55471751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5548ac27a0ecSDave Kleikamp 
5549ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5550ac27a0ecSDave Kleikamp 
5551b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5552617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5553b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5554b3881f74STheodore Ts'o 	}
5555ac27a0ecSDave Kleikamp 
55561751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
55571751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5558a2fd66d0STheodore Ts'o 
55591751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
55604ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5561ac27a0ecSDave Kleikamp 			err = -EROFS;
5562ac27a0ecSDave Kleikamp 			goto restore_opts;
5563ac27a0ecSDave Kleikamp 		}
5564ac27a0ecSDave Kleikamp 
55651751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
556638c03b34STheodore Ts'o 			err = sync_filesystem(sb);
556738c03b34STheodore Ts'o 			if (err < 0)
556838c03b34STheodore Ts'o 				goto restore_opts;
55690f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
55700f0dd62fSChristoph Hellwig 			if (err < 0)
5571c79d967dSChristoph Hellwig 				goto restore_opts;
5572c79d967dSChristoph Hellwig 
5573ac27a0ecSDave Kleikamp 			/*
5574ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5575ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5576ac27a0ecSDave Kleikamp 			 */
55771751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5578ac27a0ecSDave Kleikamp 
5579ac27a0ecSDave Kleikamp 			/*
5580ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5581ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5582ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5583ac27a0ecSDave Kleikamp 			 */
5584617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5585617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5586ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5587ac27a0ecSDave Kleikamp 
5588a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5589617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
55902dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
55912dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5592ac27a0ecSDave Kleikamp 		} else {
5593a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5594e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
55952cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5596ac27a0ecSDave Kleikamp 				err = -EROFS;
5597ac27a0ecSDave Kleikamp 				goto restore_opts;
5598ac27a0ecSDave Kleikamp 			}
5599ead6596bSEric Sandeen 			/*
56008a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
56010b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
56028a266467STheodore Ts'o 			 */
56038a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
56048a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
56058a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
56068a266467STheodore Ts'o 
5607feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5608b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5609b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5610e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
56118a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
56126a797d27SDarrick J. Wong 					err = -EFSBADCRC;
56138a266467STheodore Ts'o 					goto restore_opts;
56148a266467STheodore Ts'o 				}
56158a266467STheodore Ts'o 			}
56168a266467STheodore Ts'o 
56178a266467STheodore Ts'o 			/*
5618ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5619ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5620ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5621ead6596bSEric Sandeen 			 */
5622ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5623b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5624ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5625ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5626b31e1552SEric Sandeen 				       "umount/remount instead");
5627ead6596bSEric Sandeen 				err = -EINVAL;
5628ead6596bSEric Sandeen 				goto restore_opts;
5629ead6596bSEric Sandeen 			}
5630ead6596bSEric Sandeen 
5631ac27a0ecSDave Kleikamp 			/*
5632ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5633ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5634ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5635ac27a0ecSDave Kleikamp 			 * the partition.)
5636ac27a0ecSDave Kleikamp 			 */
56370390131bSFrank Mayhar 			if (sbi->s_journal)
5638617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5639ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5640c89128a0SJaegeuk Kim 
5641c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5642c89128a0SJaegeuk Kim 			if (err)
5643c89128a0SJaegeuk Kim 				goto restore_opts;
5644c89128a0SJaegeuk Kim 
56451751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5646e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5647c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5648c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5649c5e06d10SJohann Lombardi 					err = -EROFS;
5650c5e06d10SJohann Lombardi 					goto restore_opts;
5651c5e06d10SJohann Lombardi 				}
5652c79d967dSChristoph Hellwig 			enable_quota = 1;
5653ac27a0ecSDave Kleikamp 		}
5654ac27a0ecSDave Kleikamp 	}
5655bfff6873SLukas Czerner 
5656bfff6873SLukas Czerner 	/*
5657bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5658bfff6873SLukas Czerner 	 * current settings
5659bfff6873SLukas Czerner 	 */
5660bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5661bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5662bfff6873SLukas Czerner 	else {
5663bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5664bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5665bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5666bfff6873SLukas Czerner 	}
5667bfff6873SLukas Czerner 
56686fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5669c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5670c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5671c89128a0SJaegeuk Kim 		if (err)
5672c89128a0SJaegeuk Kim 			goto restore_opts;
5673c89128a0SJaegeuk Kim 	}
56740390131bSFrank Mayhar 
5675ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5676ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5677a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5678ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
56797c319d32SAditya Kali 	if (enable_quota) {
56807c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
56810f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5682e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
56837c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
568407724f98STheodore Ts'o 			if (err)
56857c319d32SAditya Kali 				goto restore_opts;
56867c319d32SAditya Kali 		}
56877c319d32SAditya Kali 	}
56887c319d32SAditya Kali #endif
5689d4c402d9SCurt Wohlgemuth 
56901751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5691d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5692d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5693ac27a0ecSDave Kleikamp 	return 0;
56940b8e58a1SAndreas Dilger 
5695ac27a0ecSDave Kleikamp restore_opts:
5696ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5697ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5698a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5699ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5700ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5701ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
570230773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
570330773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5704ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5705ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5706a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
570733458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
570833458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5709ac27a0ecSDave Kleikamp 	}
571033458eabSTheodore Ts'o 	synchronize_rcu();
571133458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
571233458eabSTheodore Ts'o 		kfree(to_free[i]);
5713ac27a0ecSDave Kleikamp #endif
5714d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5715ac27a0ecSDave Kleikamp 	return err;
5716ac27a0ecSDave Kleikamp }
5717ac27a0ecSDave Kleikamp 
5718689c958cSLi Xi #ifdef CONFIG_QUOTA
5719689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5720689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5721689c958cSLi Xi {
5722689c958cSLi Xi 	struct kqid qid;
5723689c958cSLi Xi 	struct dquot *dquot;
5724689c958cSLi Xi 	u64 limit;
5725689c958cSLi Xi 	u64 curblock;
5726689c958cSLi Xi 
5727689c958cSLi Xi 	qid = make_kqid_projid(projid);
5728689c958cSLi Xi 	dquot = dqget(sb, qid);
5729689c958cSLi Xi 	if (IS_ERR(dquot))
5730689c958cSLi Xi 		return PTR_ERR(dquot);
57317b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5732689c958cSLi Xi 
5733a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
5734a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
573557c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
573657c32ea4SChengguang Xu 
5737689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5738f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5739f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5740689c958cSLi Xi 		buf->f_blocks = limit;
5741689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5742689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5743689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5744689c958cSLi Xi 	}
5745689c958cSLi Xi 
5746a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
5747a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
5748689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5749689c958cSLi Xi 		buf->f_files = limit;
5750689c958cSLi Xi 		buf->f_ffree =
5751689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5752689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5753689c958cSLi Xi 	}
5754689c958cSLi Xi 
57557b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5756689c958cSLi Xi 	dqput(dquot);
5757689c958cSLi Xi 	return 0;
5758689c958cSLi Xi }
5759689c958cSLi Xi #endif
5760689c958cSLi Xi 
5761617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5762ac27a0ecSDave Kleikamp {
5763ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5764617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5765617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
576627dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5767960cc398SPekka Enberg 	u64 fsid;
5768d02a9391SKazuya Mio 	s64 bfree;
576927dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5770ac27a0ecSDave Kleikamp 
5771952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5772952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5773ac27a0ecSDave Kleikamp 
5774617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5775ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5776b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
577757042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
577857042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5779d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
578057042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
578127dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
578227dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
578327dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5784ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5785ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
578652d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5787617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5788960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5789960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5790960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5791960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
57920b8e58a1SAndreas Dilger 
5793689c958cSLi Xi #ifdef CONFIG_QUOTA
5794689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5795689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5796689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5797689c958cSLi Xi #endif
5798ac27a0ecSDave Kleikamp 	return 0;
5799ac27a0ecSDave Kleikamp }
5800ac27a0ecSDave Kleikamp 
5801ac27a0ecSDave Kleikamp 
5802ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5803ac27a0ecSDave Kleikamp 
5804bc8230eeSJan Kara /*
5805bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5806bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5807bc8230eeSJan Kara  */
5808ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5809ac27a0ecSDave Kleikamp {
58104c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5811ac27a0ecSDave Kleikamp }
5812ac27a0ecSDave Kleikamp 
5813617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5814ac27a0ecSDave Kleikamp {
5815ac27a0ecSDave Kleikamp 	int ret, err;
5816ac27a0ecSDave Kleikamp 	handle_t *handle;
5817ac27a0ecSDave Kleikamp 	struct inode *inode;
5818ac27a0ecSDave Kleikamp 
5819ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
58209924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5821617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5822ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5823ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5824ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5825617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5826ac27a0ecSDave Kleikamp 	if (!ret)
5827ac27a0ecSDave Kleikamp 		ret = err;
5828ac27a0ecSDave Kleikamp 	return ret;
5829ac27a0ecSDave Kleikamp }
5830ac27a0ecSDave Kleikamp 
5831617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5832ac27a0ecSDave Kleikamp {
5833ac27a0ecSDave Kleikamp 	int ret, err;
5834ac27a0ecSDave Kleikamp 	handle_t *handle;
5835ac27a0ecSDave Kleikamp 
58369924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5837617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5838ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5839ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5840ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5841617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5842ac27a0ecSDave Kleikamp 	if (!ret)
5843ac27a0ecSDave Kleikamp 		ret = err;
5844ac27a0ecSDave Kleikamp 	return ret;
5845ac27a0ecSDave Kleikamp }
5846ac27a0ecSDave Kleikamp 
5847617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5848ac27a0ecSDave Kleikamp {
5849ac27a0ecSDave Kleikamp 	int ret, err;
5850ac27a0ecSDave Kleikamp 	handle_t *handle;
5851ac27a0ecSDave Kleikamp 
58529924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5853617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
58549c3013e9SJan Kara 	if (IS_ERR(handle)) {
58559c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
58569c3013e9SJan Kara 		dquot_release(dquot);
5857ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
58589c3013e9SJan Kara 	}
5859ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5860617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5861ac27a0ecSDave Kleikamp 	if (!ret)
5862ac27a0ecSDave Kleikamp 		ret = err;
5863ac27a0ecSDave Kleikamp 	return ret;
5864ac27a0ecSDave Kleikamp }
5865ac27a0ecSDave Kleikamp 
5866617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5867ac27a0ecSDave Kleikamp {
5868262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5869262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5870262b4662SJan Kara 
58712c8be6b2SJan Kara 	/* Are we journaling quotas? */
5872e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5873262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5874ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5875617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5876ac27a0ecSDave Kleikamp 	} else {
5877ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5878ac27a0ecSDave Kleikamp 	}
5879ac27a0ecSDave Kleikamp }
5880ac27a0ecSDave Kleikamp 
5881617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5882ac27a0ecSDave Kleikamp {
5883ac27a0ecSDave Kleikamp 	int ret, err;
5884ac27a0ecSDave Kleikamp 	handle_t *handle;
5885ac27a0ecSDave Kleikamp 
5886ac27a0ecSDave Kleikamp 	/* Data block + inode block */
58872b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5888ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5889ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5890ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5891617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5892ac27a0ecSDave Kleikamp 	if (!ret)
5893ac27a0ecSDave Kleikamp 		ret = err;
5894ac27a0ecSDave Kleikamp 	return ret;
5895ac27a0ecSDave Kleikamp }
5896ac27a0ecSDave Kleikamp 
5897ac27a0ecSDave Kleikamp /*
5898ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5899ac27a0ecSDave Kleikamp  * the quota file and such...
5900ac27a0ecSDave Kleikamp  */
5901617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5902ac27a0ecSDave Kleikamp {
590333458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5904617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5905ac27a0ecSDave Kleikamp }
5906ac27a0ecSDave Kleikamp 
5907daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5908daf647d2STheodore Ts'o {
5909daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5910daf647d2STheodore Ts'o 
5911daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5912daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5913daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5914daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5915daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5916daf647d2STheodore Ts'o 	 */
5917daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5918daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5919daf647d2STheodore Ts'o }
5920daf647d2STheodore Ts'o 
5921ac27a0ecSDave Kleikamp /*
5922ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5923ac27a0ecSDave Kleikamp  */
5924617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
59258c54ca9cSAl Viro 			 const struct path *path)
5926ac27a0ecSDave Kleikamp {
5927ac27a0ecSDave Kleikamp 	int err;
5928ac27a0ecSDave Kleikamp 
5929ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5930ac27a0ecSDave Kleikamp 		return -EINVAL;
59310623543bSJan Kara 
5932ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5933d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5934ac27a0ecSDave Kleikamp 		return -EXDEV;
59350623543bSJan Kara 	/* Journaling quota? */
59360623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
59372b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5938f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5939b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5940b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5941b31e1552SEric Sandeen 				"Journaled quota will not work");
594291389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
594391389240SJan Kara 	} else {
594491389240SJan Kara 		/*
594591389240SJan Kara 		 * Clear the flag just in case mount options changed since
594691389240SJan Kara 		 * last time.
594791389240SJan Kara 		 */
594891389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
59490623543bSJan Kara 	}
59500623543bSJan Kara 
59510623543bSJan Kara 	/*
59520623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
59530623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
59540623543bSJan Kara 	 */
59550390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
59562b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
59570623543bSJan Kara 		/*
59580623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
59590623543bSJan Kara 		 * otherwise be livelocked...
59600623543bSJan Kara 		 */
59610623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
59627ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
59630623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5964f00c9e44SJan Kara 		if (err)
59657ffe1ea8SHidehiro Kawai 			return err;
59667ffe1ea8SHidehiro Kawai 	}
5967957153fcSJan Kara 
5968daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5969daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5970957153fcSJan Kara 	if (err) {
5971daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5972daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5973957153fcSJan Kara 	} else {
5974957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5975957153fcSJan Kara 		handle_t *handle;
5976957153fcSJan Kara 
597761a92987SJan Kara 		/*
597861a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
597961a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
598061a92987SJan Kara 		 * are already enabled and this is not a hard failure.
598161a92987SJan Kara 		 */
5982957153fcSJan Kara 		inode_lock(inode);
5983957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5984957153fcSJan Kara 		if (IS_ERR(handle))
5985957153fcSJan Kara 			goto unlock_inode;
5986957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5987957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5988957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
59894209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
5990957153fcSJan Kara 		ext4_journal_stop(handle);
5991957153fcSJan Kara 	unlock_inode:
5992957153fcSJan Kara 		inode_unlock(inode);
5993957153fcSJan Kara 	}
5994daf647d2STheodore Ts'o 	return err;
5995ac27a0ecSDave Kleikamp }
5996ac27a0ecSDave Kleikamp 
59977c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
59987c319d32SAditya Kali 			     unsigned int flags)
59997c319d32SAditya Kali {
60007c319d32SAditya Kali 	int err;
60017c319d32SAditya Kali 	struct inode *qf_inode;
6002a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
60037c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6004689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6005689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
60067c319d32SAditya Kali 	};
60077c319d32SAditya Kali 
6008e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
60097c319d32SAditya Kali 
60107c319d32SAditya Kali 	if (!qf_inums[type])
60117c319d32SAditya Kali 		return -EPERM;
60127c319d32SAditya Kali 
60138a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
60147c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
60157c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
60167c319d32SAditya Kali 		return PTR_ERR(qf_inode);
60177c319d32SAditya Kali 	}
60187c319d32SAditya Kali 
6019bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6020bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6021daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
60227212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6023daf647d2STheodore Ts'o 	if (err)
6024daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
602561157b24SPan Bian 	iput(qf_inode);
60267c319d32SAditya Kali 
60277c319d32SAditya Kali 	return err;
60287c319d32SAditya Kali }
60297c319d32SAditya Kali 
60307c319d32SAditya Kali /* Enable usage tracking for all quota types. */
60317c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
60327c319d32SAditya Kali {
60337c319d32SAditya Kali 	int type, err = 0;
6034a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
60357c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6036689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6037689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
60387c319d32SAditya Kali 	};
603949da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
604049da9392SJan Kara 		test_opt(sb, USRQUOTA),
604149da9392SJan Kara 		test_opt(sb, GRPQUOTA),
604249da9392SJan Kara 		test_opt(sb, PRJQUOTA),
604349da9392SJan Kara 	};
60447c319d32SAditya Kali 
604591389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6046a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
60477c319d32SAditya Kali 		if (qf_inums[type]) {
60487c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
604949da9392SJan Kara 				DQUOT_USAGE_ENABLED |
605049da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
60517c319d32SAditya Kali 			if (err) {
60527c319d32SAditya Kali 				ext4_warning(sb,
605372ba7450STheodore Ts'o 					"Failed to enable quota tracking "
605472ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
605572ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
60567f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
60577f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
60587f144fd0SJunichi Uekawa 
60597c319d32SAditya Kali 				return err;
60607c319d32SAditya Kali 			}
60617c319d32SAditya Kali 		}
60627c319d32SAditya Kali 	}
60637c319d32SAditya Kali 	return 0;
60647c319d32SAditya Kali }
60657c319d32SAditya Kali 
6066ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6067ca0e05e4SDmitry Monakhov {
606821f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
606921f97697SJan Kara 	handle_t *handle;
6070957153fcSJan Kara 	int err;
607121f97697SJan Kara 
607287009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
607387009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
607487009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6075ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6076ca0e05e4SDmitry Monakhov 
6077957153fcSJan Kara 	if (!inode || !igrab(inode))
60780b268590SAmir Goldstein 		goto out;
60790b268590SAmir Goldstein 
6080957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6081964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6082957153fcSJan Kara 		goto out_put;
6083957153fcSJan Kara 
6084957153fcSJan Kara 	inode_lock(inode);
608561a92987SJan Kara 	/*
608661a92987SJan Kara 	 * Update modification times of quota files when userspace can
608761a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
608861a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
608961a92987SJan Kara 	 */
60909924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
60914209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
60924209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6093957153fcSJan Kara 		goto out_unlock;
60944209ae12SHarshad Shirwadkar 	}
6095957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6096957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6097eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
60984209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
609921f97697SJan Kara 	ext4_journal_stop(handle);
6100957153fcSJan Kara out_unlock:
6101957153fcSJan Kara 	inode_unlock(inode);
6102957153fcSJan Kara out_put:
6103964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6104957153fcSJan Kara 	iput(inode);
6105957153fcSJan Kara 	return err;
610621f97697SJan Kara out:
6107ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6108ca0e05e4SDmitry Monakhov }
6109ca0e05e4SDmitry Monakhov 
6110ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6111ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6112ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6113ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6114617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6115ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6116ac27a0ecSDave Kleikamp {
6117ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6118725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6119ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6120ac27a0ecSDave Kleikamp 	int tocopy;
6121ac27a0ecSDave Kleikamp 	size_t toread;
6122ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6123ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6124ac27a0ecSDave Kleikamp 
6125ac27a0ecSDave Kleikamp 	if (off > i_size)
6126ac27a0ecSDave Kleikamp 		return 0;
6127ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6128ac27a0ecSDave Kleikamp 		len = i_size-off;
6129ac27a0ecSDave Kleikamp 	toread = len;
6130ac27a0ecSDave Kleikamp 	while (toread > 0) {
6131ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6132ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
61331c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
61341c215028STheodore Ts'o 		if (IS_ERR(bh))
61351c215028STheodore Ts'o 			return PTR_ERR(bh);
6136ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6137ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6138ac27a0ecSDave Kleikamp 		else
6139ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6140ac27a0ecSDave Kleikamp 		brelse(bh);
6141ac27a0ecSDave Kleikamp 		offset = 0;
6142ac27a0ecSDave Kleikamp 		toread -= tocopy;
6143ac27a0ecSDave Kleikamp 		data += tocopy;
6144ac27a0ecSDave Kleikamp 		blk++;
6145ac27a0ecSDave Kleikamp 	}
6146ac27a0ecSDave Kleikamp 	return len;
6147ac27a0ecSDave Kleikamp }
6148ac27a0ecSDave Kleikamp 
6149ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6150ac27a0ecSDave Kleikamp  * enough credits) */
6151617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6152ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6153ac27a0ecSDave Kleikamp {
6154ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6155725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
61564209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6157c5e298aeSTheodore Ts'o 	int retries = 0;
6158ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6159ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6160ac27a0ecSDave Kleikamp 
61610390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6162b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6163b31e1552SEric Sandeen 			" cancelled because transaction is not started",
61649c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
61659c3013e9SJan Kara 		return -EIO;
61669c3013e9SJan Kara 	}
616767eeb568SDmitry Monakhov 	/*
616867eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
616967eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
617067eeb568SDmitry Monakhov 	 */
617167eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
617267eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
617367eeb568SDmitry Monakhov 			" cancelled because not block aligned",
617467eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
617567eeb568SDmitry Monakhov 		return -EIO;
617667eeb568SDmitry Monakhov 	}
617767eeb568SDmitry Monakhov 
6178c5e298aeSTheodore Ts'o 	do {
6179c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6180c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6181c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
618245586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6183c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
61841c215028STheodore Ts'o 	if (IS_ERR(bh))
61851c215028STheodore Ts'o 		return PTR_ERR(bh);
6186ac27a0ecSDave Kleikamp 	if (!bh)
6187ac27a0ecSDave Kleikamp 		goto out;
61885d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6189617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6190ac27a0ecSDave Kleikamp 	if (err) {
6191ac27a0ecSDave Kleikamp 		brelse(bh);
61921c215028STheodore Ts'o 		return err;
6193ac27a0ecSDave Kleikamp 	}
6194ac27a0ecSDave Kleikamp 	lock_buffer(bh);
619567eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6196ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6197ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
61980390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6199ac27a0ecSDave Kleikamp 	brelse(bh);
6200ac27a0ecSDave Kleikamp out:
620167eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
620267eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6203617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
62044209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
62054209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
62064209ae12SHarshad Shirwadkar 			err = err2;
620721f97697SJan Kara 	}
62084209ae12SHarshad Shirwadkar 	return err ? err : len;
6209ac27a0ecSDave Kleikamp }
6210ac27a0ecSDave Kleikamp #endif
6211ac27a0ecSDave Kleikamp 
6212152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6213152a0836SAl Viro 		       const char *dev_name, void *data)
6214ac27a0ecSDave Kleikamp {
6215152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6216ac27a0ecSDave Kleikamp }
6217ac27a0ecSDave Kleikamp 
6218c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
621924b58424STheodore Ts'o static inline void register_as_ext2(void)
622024b58424STheodore Ts'o {
622124b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
622224b58424STheodore Ts'o 	if (err)
622324b58424STheodore Ts'o 		printk(KERN_WARNING
622424b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
622524b58424STheodore Ts'o }
622624b58424STheodore Ts'o 
622724b58424STheodore Ts'o static inline void unregister_as_ext2(void)
622824b58424STheodore Ts'o {
622924b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
623024b58424STheodore Ts'o }
62312035e776STheodore Ts'o 
62322035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
62332035e776STheodore Ts'o {
6234e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
62352035e776STheodore Ts'o 		return 0;
6236bc98a42cSDavid Howells 	if (sb_rdonly(sb))
62372035e776STheodore Ts'o 		return 1;
6238e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
62392035e776STheodore Ts'o 		return 0;
62402035e776STheodore Ts'o 	return 1;
62412035e776STheodore Ts'o }
624224b58424STheodore Ts'o #else
624324b58424STheodore Ts'o static inline void register_as_ext2(void) { }
624424b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
62452035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
624624b58424STheodore Ts'o #endif
624724b58424STheodore Ts'o 
624824b58424STheodore Ts'o static inline void register_as_ext3(void)
624924b58424STheodore Ts'o {
625024b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
625124b58424STheodore Ts'o 	if (err)
625224b58424STheodore Ts'o 		printk(KERN_WARNING
625324b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
625424b58424STheodore Ts'o }
625524b58424STheodore Ts'o 
625624b58424STheodore Ts'o static inline void unregister_as_ext3(void)
625724b58424STheodore Ts'o {
625824b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
625924b58424STheodore Ts'o }
62602035e776STheodore Ts'o 
62612035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
62622035e776STheodore Ts'o {
6263e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
62642035e776STheodore Ts'o 		return 0;
6265e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
62662035e776STheodore Ts'o 		return 0;
6267bc98a42cSDavid Howells 	if (sb_rdonly(sb))
62682035e776STheodore Ts'o 		return 1;
6269e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
62702035e776STheodore Ts'o 		return 0;
62712035e776STheodore Ts'o 	return 1;
62722035e776STheodore Ts'o }
627324b58424STheodore Ts'o 
627403010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6275ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
627603010a33STheodore Ts'o 	.name		= "ext4",
6277152a0836SAl Viro 	.mount		= ext4_mount,
6278ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6279ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6280ac27a0ecSDave Kleikamp };
62817f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6282ac27a0ecSDave Kleikamp 
6283e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6284e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6285e9e3bcecSEric Sandeen 
62865dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6287ac27a0ecSDave Kleikamp {
6288e9e3bcecSEric Sandeen 	int i, err;
6289c9de560dSAlex Tomas 
6290e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
629107c0c5d8SAl Viro 	ext4_li_info = NULL;
629207c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
629307c0c5d8SAl Viro 
62949a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
629512e9b892SDmitry Monakhov 	ext4_check_flag_values();
6296e9e3bcecSEric Sandeen 
6297e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6298e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6299e9e3bcecSEric Sandeen 
630051865fdaSZheng Liu 	err = ext4_init_es();
63016fd058f7STheodore Ts'o 	if (err)
63026fd058f7STheodore Ts'o 		return err;
630351865fdaSZheng Liu 
63041dc0aa46SEric Whitney 	err = ext4_init_pending();
63051dc0aa46SEric Whitney 	if (err)
630622cfe4b4SEric Biggers 		goto out7;
630722cfe4b4SEric Biggers 
630822cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
630922cfe4b4SEric Biggers 	if (err)
63101dc0aa46SEric Whitney 		goto out6;
63111dc0aa46SEric Whitney 
631251865fdaSZheng Liu 	err = ext4_init_pageio();
631351865fdaSZheng Liu 	if (err)
6314b5799018STheodore Ts'o 		goto out5;
631551865fdaSZheng Liu 
63165dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6317bd2d0210STheodore Ts'o 	if (err)
6318b5799018STheodore Ts'o 		goto out4;
6319857ac889SLukas Czerner 
6320b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6321dd68314cSTheodore Ts'o 	if (err)
6322b5799018STheodore Ts'o 		goto out3;
6323857ac889SLukas Czerner 
63245dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6325ac27a0ecSDave Kleikamp 	if (err)
6326c9de560dSAlex Tomas 		goto out2;
6327ac27a0ecSDave Kleikamp 	err = init_inodecache();
6328ac27a0ecSDave Kleikamp 	if (err)
6329ac27a0ecSDave Kleikamp 		goto out1;
633024b58424STheodore Ts'o 	register_as_ext3();
63312035e776STheodore Ts'o 	register_as_ext2();
633203010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6333ac27a0ecSDave Kleikamp 	if (err)
6334ac27a0ecSDave Kleikamp 		goto out;
6335bfff6873SLukas Czerner 
6336ac27a0ecSDave Kleikamp 	return 0;
6337ac27a0ecSDave Kleikamp out:
633824b58424STheodore Ts'o 	unregister_as_ext2();
633924b58424STheodore Ts'o 	unregister_as_ext3();
6340ac27a0ecSDave Kleikamp 	destroy_inodecache();
6341ac27a0ecSDave Kleikamp out1:
63425dabfc78STheodore Ts'o 	ext4_exit_mballoc();
63439c191f70ST Makphaibulchoke out2:
6344b5799018STheodore Ts'o 	ext4_exit_sysfs();
6345b5799018STheodore Ts'o out3:
6346dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6347b5799018STheodore Ts'o out4:
63485dabfc78STheodore Ts'o 	ext4_exit_pageio();
6349b5799018STheodore Ts'o out5:
635022cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
63511dc0aa46SEric Whitney out6:
635222cfe4b4SEric Biggers 	ext4_exit_pending();
635322cfe4b4SEric Biggers out7:
635451865fdaSZheng Liu 	ext4_exit_es();
635551865fdaSZheng Liu 
6356ac27a0ecSDave Kleikamp 	return err;
6357ac27a0ecSDave Kleikamp }
6358ac27a0ecSDave Kleikamp 
63595dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6360ac27a0ecSDave Kleikamp {
6361bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
636224b58424STheodore Ts'o 	unregister_as_ext2();
636324b58424STheodore Ts'o 	unregister_as_ext3();
636403010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6365ac27a0ecSDave Kleikamp 	destroy_inodecache();
63665dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6367b5799018STheodore Ts'o 	ext4_exit_sysfs();
63685dabfc78STheodore Ts'o 	ext4_exit_system_zone();
63695dabfc78STheodore Ts'o 	ext4_exit_pageio();
637022cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6371dd12ed14SEric Sandeen 	ext4_exit_es();
63721dc0aa46SEric Whitney 	ext4_exit_pending();
6373ac27a0ecSDave Kleikamp }
6374ac27a0ecSDave Kleikamp 
6375ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
637683982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6377ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
63787ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
63795dabfc78STheodore Ts'o module_init(ext4_init_fs)
63805dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6381