xref: /linux/fs/ext4/super.c (revision bc71726c725767205757821df364acff87f92ac5)
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);
6911215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
70617ba13bSMingming Cao 					struct ext4_super_block *es);
7111215630SJan Kara static int 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,
1511ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
15125a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1513ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
15149cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
15159cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1516327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1517327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1518670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
15191449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
15205328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1521744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1522fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1523cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1524ac27a0ecSDave Kleikamp };
1525ac27a0ecSDave Kleikamp 
1526a447c093SSteven Whitehouse static const match_table_t tokens = {
1527ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1528ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1529ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1530ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1531ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1532ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1533ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1534ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1535ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1536ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1537ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1538ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1539ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1540ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
154172578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
154272578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1543ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1544ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1545ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1546ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1547e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
15485a916be1STheodore Ts'o 	{Opt_noload, "noload"},
154972578c33STheodore Ts'o 	{Opt_removed, "nobh"},
155072578c33STheodore Ts'o 	{Opt_removed, "bh"},
1551ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
155230773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
155330773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1554ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1555ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1556818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1557c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1558818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1559ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1560ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1561ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1562ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
15635bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
15645bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1565ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1566ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1567ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1568ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1569ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1570ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15715a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1572ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1573ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1574ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1575ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
157649da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1577ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
157806705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
157906705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
158025ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1581923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
15829cb20f94SIra Weiny 	{Opt_dax_always, "dax=always"},
15839cb20f94SIra Weiny 	{Opt_dax_inode, "dax=inode"},
15849cb20f94SIra Weiny 	{Opt_dax_never, "dax=never"},
1585c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
158664769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1587327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1588327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1589a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1590a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1591670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1592dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
159336ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
159436ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15956fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15966fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1597240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1598b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1599afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
160006705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
160106705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1602744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1603244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1604744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
16055328e635SEric Sandeen 	{Opt_discard, "discard"},
16065328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1607fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1608fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1609fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1610df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1611ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
16126ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1613cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1614cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1615c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1616c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1617c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1618c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1619c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1620f3f12faaSJosef Bacik 	{Opt_err, NULL},
1621ac27a0ecSDave Kleikamp };
1622ac27a0ecSDave Kleikamp 
1623617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1624ac27a0ecSDave Kleikamp {
1625617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1626ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1627ac27a0ecSDave Kleikamp 
1628ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1629ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
16300b8e58a1SAndreas Dilger 
1631ac27a0ecSDave Kleikamp 	options += 3;
16320b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1633ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1634ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
16354776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1636ac27a0ecSDave Kleikamp 		       (char *) *data);
1637ac27a0ecSDave Kleikamp 		return 1;
1638ac27a0ecSDave Kleikamp 	}
1639ac27a0ecSDave Kleikamp 	if (*options == ',')
1640ac27a0ecSDave Kleikamp 		options++;
1641ac27a0ecSDave Kleikamp 	*data = (void *) options;
16420b8e58a1SAndreas Dilger 
1643ac27a0ecSDave Kleikamp 	return sb_block;
1644ac27a0ecSDave Kleikamp }
1645ac27a0ecSDave Kleikamp 
1646b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1647d6006186SEric Biggers static const char deprecated_msg[] =
1648d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1649437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1650b3881f74STheodore Ts'o 
165156c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
165256c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
165356c50f11SDmitry Monakhov {
165456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
165533458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
165603dafb5fSChen Gang 	int ret = -1;
165756c50f11SDmitry Monakhov 
165833458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
165956c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
166056c50f11SDmitry Monakhov 			"Cannot change journaled "
166156c50f11SDmitry Monakhov 			"quota options when quota turned on");
166257f73c2cSTheodore Ts'o 		return -1;
166356c50f11SDmitry Monakhov 	}
1664e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1665c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1666c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1667c325a67cSTheodore Ts'o 		return 1;
1668262b4662SJan Kara 	}
166956c50f11SDmitry Monakhov 	qname = match_strdup(args);
167056c50f11SDmitry Monakhov 	if (!qname) {
167156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
167256c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
167357f73c2cSTheodore Ts'o 		return -1;
167456c50f11SDmitry Monakhov 	}
167533458eabSTheodore Ts'o 	if (old_qname) {
167633458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
167703dafb5fSChen Gang 			ret = 1;
167803dafb5fSChen Gang 		else
167956c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
168003dafb5fSChen Gang 				 "%s quota file already specified",
168103dafb5fSChen Gang 				 QTYPE2NAME(qtype));
168203dafb5fSChen Gang 		goto errout;
168356c50f11SDmitry Monakhov 	}
168403dafb5fSChen Gang 	if (strchr(qname, '/')) {
168556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
168656c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
168703dafb5fSChen Gang 		goto errout;
168856c50f11SDmitry Monakhov 	}
168933458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1690fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
169156c50f11SDmitry Monakhov 	return 1;
169203dafb5fSChen Gang errout:
169303dafb5fSChen Gang 	kfree(qname);
169403dafb5fSChen Gang 	return ret;
169556c50f11SDmitry Monakhov }
169656c50f11SDmitry Monakhov 
169756c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
169856c50f11SDmitry Monakhov {
169956c50f11SDmitry Monakhov 
170056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
170133458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
170256c50f11SDmitry Monakhov 
170333458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
170456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
170556c50f11SDmitry Monakhov 			" when quota turned on");
170657f73c2cSTheodore Ts'o 		return -1;
170756c50f11SDmitry Monakhov 	}
170833458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
170933458eabSTheodore Ts'o 	synchronize_rcu();
171033458eabSTheodore Ts'o 	kfree(old_qname);
171156c50f11SDmitry Monakhov 	return 1;
171256c50f11SDmitry Monakhov }
171356c50f11SDmitry Monakhov #endif
171456c50f11SDmitry Monakhov 
171526092bf5STheodore Ts'o #define MOPT_SET	0x0001
171626092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
171726092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
171826092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
171926092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
172026092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
172126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
172226092bf5STheodore Ts'o #define MOPT_Q		0
172326092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
172426092bf5STheodore Ts'o #else
172526092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
172626092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
172726092bf5STheodore Ts'o #endif
172826092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
17298dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
17308dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
17318dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1732ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
17339cb20f94SIra Weiny #define MOPT_SKIP	0x0800
173426092bf5STheodore Ts'o 
173526092bf5STheodore Ts'o static const struct mount_opts {
173626092bf5STheodore Ts'o 	int	token;
173726092bf5STheodore Ts'o 	int	mount_opt;
173826092bf5STheodore Ts'o 	int	flags;
173926092bf5STheodore Ts'o } ext4_mount_opts[] = {
174026092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
174126092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
174226092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
174326092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
174426092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
174526092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
17468dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
17478dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
17488dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
17498dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
175026092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
175126092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
17528dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
17538dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17548dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
175559d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1756327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1757327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1758c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1759c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
17608dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
17611e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
176226092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
17638dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
17641e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17658dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
176626092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
176726092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
176826092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17698dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17707915a861SAles Novak 	 MOPT_NO_EXT2},
17718dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17727915a861SAles Novak 	 MOPT_NO_EXT2},
177326092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
177426092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
177526092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
177626092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
177726092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
177826092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
177926092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
178026092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
178126092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
178226092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
17839cb20f94SIra Weiny 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
17849cb20f94SIra Weiny 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
17859cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
17869cb20f94SIra Weiny 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
17879cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
17889cb20f94SIra Weiny 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
17899cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
179026092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17910efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17920efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17935ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17945ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17955ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17968dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17978dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17988dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17998dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
180026092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
180126092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
180226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
180326092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
180426092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
180526092bf5STheodore Ts'o #else
180626092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
180726092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
180826092bf5STheodore Ts'o #endif
180926092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
181026092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1811670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
181226092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
181326092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
181426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
181526092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
181626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
181749da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
181849da9392SJan Kara 							MOPT_SET | MOPT_Q},
181926092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
182049da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
182149da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
182226092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
182326092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
182426092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
182526092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
182626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
182726092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
182826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1829df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
1830ed318a6cSEric Biggers 	{Opt_test_dummy_encryption, 0, MOPT_STRING},
1831cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
183226092bf5STheodore Ts'o 	{Opt_err, 0, 0}
183326092bf5STheodore Ts'o };
183426092bf5STheodore Ts'o 
1835c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1836c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1837c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1838c83ad55eSGabriel Krisman Bertazi 	char *name;
1839c83ad55eSGabriel Krisman Bertazi 	char *version;
1840c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1841c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1842c83ad55eSGabriel Krisman Bertazi };
1843c83ad55eSGabriel Krisman Bertazi 
1844c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1845c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1846c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1847c83ad55eSGabriel Krisman Bertazi {
1848c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1849c83ad55eSGabriel Krisman Bertazi 	int i;
1850c83ad55eSGabriel Krisman Bertazi 
1851c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1852c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1853c83ad55eSGabriel Krisman Bertazi 			break;
1854c83ad55eSGabriel Krisman Bertazi 
1855c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1856c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1857c83ad55eSGabriel Krisman Bertazi 
1858c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1859c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1860c83ad55eSGabriel Krisman Bertazi 
1861c83ad55eSGabriel Krisman Bertazi 	return 0;
1862c83ad55eSGabriel Krisman Bertazi }
1863c83ad55eSGabriel Krisman Bertazi #endif
1864c83ad55eSGabriel Krisman Bertazi 
1865ed318a6cSEric Biggers static int ext4_set_test_dummy_encryption(struct super_block *sb,
1866ed318a6cSEric Biggers 					  const char *opt,
1867ed318a6cSEric Biggers 					  const substring_t *arg,
1868ed318a6cSEric Biggers 					  bool is_remount)
1869ed318a6cSEric Biggers {
1870ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
1871ed318a6cSEric Biggers 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1872ed318a6cSEric Biggers 	int err;
1873ed318a6cSEric Biggers 
1874ed318a6cSEric Biggers 	/*
1875ed318a6cSEric Biggers 	 * This mount option is just for testing, and it's not worthwhile to
1876ed318a6cSEric Biggers 	 * implement the extra complexity (e.g. RCU protection) that would be
1877ed318a6cSEric Biggers 	 * needed to allow it to be set or changed during remount.  We do allow
1878ed318a6cSEric Biggers 	 * it to be specified during remount, but only if there is no change.
1879ed318a6cSEric Biggers 	 */
1880ed318a6cSEric Biggers 	if (is_remount && !sbi->s_dummy_enc_ctx.ctx) {
1881ed318a6cSEric Biggers 		ext4_msg(sb, KERN_WARNING,
1882ed318a6cSEric Biggers 			 "Can't set test_dummy_encryption on remount");
1883ed318a6cSEric Biggers 		return -1;
1884ed318a6cSEric Biggers 	}
1885ed318a6cSEric Biggers 	err = fscrypt_set_test_dummy_encryption(sb, arg, &sbi->s_dummy_enc_ctx);
1886ed318a6cSEric Biggers 	if (err) {
1887ed318a6cSEric Biggers 		if (err == -EEXIST)
1888ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1889ed318a6cSEric Biggers 				 "Can't change test_dummy_encryption on remount");
1890ed318a6cSEric Biggers 		else if (err == -EINVAL)
1891ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1892ed318a6cSEric Biggers 				 "Value of option \"%s\" is unrecognized", opt);
1893ed318a6cSEric Biggers 		else
1894ed318a6cSEric Biggers 			ext4_msg(sb, KERN_WARNING,
1895ed318a6cSEric Biggers 				 "Error processing option \"%s\" [%d]",
1896ed318a6cSEric Biggers 				 opt, err);
1897ed318a6cSEric Biggers 		return -1;
1898ed318a6cSEric Biggers 	}
1899ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
1900ed318a6cSEric Biggers #else
1901ed318a6cSEric Biggers 	ext4_msg(sb, KERN_WARNING,
1902ed318a6cSEric Biggers 		 "Test dummy encryption mount option ignored");
1903ed318a6cSEric Biggers #endif
1904ed318a6cSEric Biggers 	return 1;
1905ed318a6cSEric Biggers }
1906ed318a6cSEric Biggers 
190726092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
190826092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
190926092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
191026092bf5STheodore Ts'o {
191126092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
191226092bf5STheodore Ts'o 	const struct mount_opts *m;
191308cefc7aSEric W. Biederman 	kuid_t uid;
191408cefc7aSEric W. Biederman 	kgid_t gid;
191526092bf5STheodore Ts'o 	int arg = 0;
191626092bf5STheodore Ts'o 
191757f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
191857f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
191957f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
192057f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
192157f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
192257f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
192357f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
192457f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
192557f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
192657f73c2cSTheodore Ts'o #endif
192726092bf5STheodore Ts'o 	switch (token) {
1928f7048605STheodore Ts'o 	case Opt_noacl:
1929f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1930f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1931f7048605STheodore Ts'o 		break;
193226092bf5STheodore Ts'o 	case Opt_sb:
193326092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
193426092bf5STheodore Ts'o 	case Opt_removed:
19355f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
193626092bf5STheodore Ts'o 		return 1;
193726092bf5STheodore Ts'o 	case Opt_abort:
193826092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
193926092bf5STheodore Ts'o 		return 1;
194026092bf5STheodore Ts'o 	case Opt_i_version:
1941357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
194226092bf5STheodore Ts'o 		return 1;
1943a26f4992STheodore Ts'o 	case Opt_lazytime:
19441751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1945a26f4992STheodore Ts'o 		return 1;
1946a26f4992STheodore Ts'o 	case Opt_nolazytime:
19471751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1948a26f4992STheodore Ts'o 		return 1;
194926092bf5STheodore Ts'o 	}
195026092bf5STheodore Ts'o 
19515f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
19525f3633e3SJan Kara 		if (token == m->token)
19535f3633e3SJan Kara 			break;
19545f3633e3SJan Kara 
19555f3633e3SJan Kara 	if (m->token == Opt_err) {
19565f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
19575f3633e3SJan Kara 			 "or missing value", opt);
19585f3633e3SJan Kara 		return -1;
19595f3633e3SJan Kara 	}
19605f3633e3SJan Kara 
19618dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
19628dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19638dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
19648dc0aa8cSTheodore Ts'o 		return -1;
19658dc0aa8cSTheodore Ts'o 	}
19668dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
19678dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19688dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
19698dc0aa8cSTheodore Ts'o 		return -1;
19708dc0aa8cSTheodore Ts'o 	}
19718dc0aa8cSTheodore Ts'o 
1972ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
19730efb3b23SJan Kara 		return -1;
197426092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
197526092bf5STheodore Ts'o 		return -1;
1976c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1977c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
197826092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
19791e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
19801e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1981c93cf2d7SDmitry Monakhov 		} else
1982c93cf2d7SDmitry Monakhov 			return -1;
1983c93cf2d7SDmitry Monakhov 	}
198426092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
198526092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
198626092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
198726092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
198826092bf5STheodore Ts'o 			 "options when quota turned on");
198926092bf5STheodore Ts'o 		return -1;
199026092bf5STheodore Ts'o 	}
199126092bf5STheodore Ts'o 
199226092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
199326092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
199426092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
199526092bf5STheodore Ts'o 		if (arg == 0)
199626092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
19979ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
19989ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
19999ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
20009ba55543Szhangyi (F) 				 "must be smaller than %d",
20019ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
20029ba55543Szhangyi (F) 			return -1;
20039ba55543Szhangyi (F) 		}
200426092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
2005670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
20069803387cSTheodore Ts'o 		if ((arg & 1) ||
20079803387cSTheodore Ts'o 		    (arg < 4) ||
20089803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
20099803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
20109803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
20119803387cSTheodore Ts'o 			return -1;
20129803387cSTheodore Ts'o 		}
2013670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
201426092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
201526092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
201626092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
201726092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
201826092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
2019e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2020e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
2021e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
2022e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
202326092bf5STheodore Ts'o 			return -1;
202426092bf5STheodore Ts'o 		}
202526092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
202626092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
202726092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
202826092bf5STheodore Ts'o 		if (!args->from)
202926092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
203026092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
2031df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
2032df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
203326092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
203426092bf5STheodore Ts'o 		sbi->s_stripe = arg;
20350efb3b23SJan Kara 	} else if (token == Opt_resuid) {
20360efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
20370efb3b23SJan Kara 		if (!uid_valid(uid)) {
20385f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
20390efb3b23SJan Kara 			return -1;
20400efb3b23SJan Kara 		}
20410efb3b23SJan Kara 		sbi->s_resuid = uid;
20420efb3b23SJan Kara 	} else if (token == Opt_resgid) {
20430efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
20440efb3b23SJan Kara 		if (!gid_valid(gid)) {
20455f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
20460efb3b23SJan Kara 			return -1;
20470efb3b23SJan Kara 		}
20480efb3b23SJan Kara 		sbi->s_resgid = gid;
20490efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
20500efb3b23SJan Kara 		if (is_remount) {
20510efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
20520efb3b23SJan Kara 				 "Cannot specify journal on remount");
20530efb3b23SJan Kara 			return -1;
20540efb3b23SJan Kara 		}
20550efb3b23SJan Kara 		*journal_devnum = arg;
2056ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2057ad4eec61SEric Sandeen 		char *journal_path;
2058ad4eec61SEric Sandeen 		struct inode *journal_inode;
2059ad4eec61SEric Sandeen 		struct path path;
2060ad4eec61SEric Sandeen 		int error;
2061ad4eec61SEric Sandeen 
2062ad4eec61SEric Sandeen 		if (is_remount) {
2063ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2064ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2065ad4eec61SEric Sandeen 			return -1;
2066ad4eec61SEric Sandeen 		}
2067ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2068ad4eec61SEric Sandeen 		if (!journal_path) {
2069ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2070ad4eec61SEric Sandeen 				"journal device string");
2071ad4eec61SEric Sandeen 			return -1;
2072ad4eec61SEric Sandeen 		}
2073ad4eec61SEric Sandeen 
2074ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2075ad4eec61SEric Sandeen 		if (error) {
2076ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2077ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2078ad4eec61SEric Sandeen 			kfree(journal_path);
2079ad4eec61SEric Sandeen 			return -1;
2080ad4eec61SEric Sandeen 		}
2081ad4eec61SEric Sandeen 
20822b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2083ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2084ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2085ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2086ad4eec61SEric Sandeen 			path_put(&path);
2087ad4eec61SEric Sandeen 			kfree(journal_path);
2088ad4eec61SEric Sandeen 			return -1;
2089ad4eec61SEric Sandeen 		}
2090ad4eec61SEric Sandeen 
2091ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2092ad4eec61SEric Sandeen 		path_put(&path);
2093ad4eec61SEric Sandeen 		kfree(journal_path);
20940efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
20950efb3b23SJan Kara 		if (arg > 7) {
20965f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
20970efb3b23SJan Kara 				 " (must be 0-7)");
20980efb3b23SJan Kara 			return -1;
20990efb3b23SJan Kara 		}
21000efb3b23SJan Kara 		*journal_ioprio =
21010efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
21026ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2103ed318a6cSEric Biggers 		return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2104ed318a6cSEric Biggers 						      is_remount);
210526092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
210626092bf5STheodore Ts'o 		if (is_remount) {
210726092bf5STheodore Ts'o 			if (!sbi->s_journal)
210826092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
21095f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
211026092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
211126092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
211226092bf5STheodore Ts'o 				return -1;
211326092bf5STheodore Ts'o 			}
211426092bf5STheodore Ts'o 		} else {
211526092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
211626092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
211726092bf5STheodore Ts'o 		}
211826092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
211926092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
212026092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
212126092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
21225f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
21235f3633e3SJan Kara 				 "quota options when quota turned on");
212426092bf5STheodore Ts'o 			return -1;
212526092bf5STheodore Ts'o 		}
2126e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2127c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2128c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2129262b4662SJan Kara 				 "when QUOTA feature is enabled");
2130c325a67cSTheodore Ts'o 			return 1;
2131262b4662SJan Kara 		}
213226092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
213326092bf5STheodore Ts'o #endif
21349cb20f94SIra Weiny 	} else if (token == Opt_dax || token == Opt_dax_always ||
21359cb20f94SIra Weiny 		   token == Opt_dax_inode || token == Opt_dax_never) {
2136ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
21379cb20f94SIra Weiny 		switch (token) {
21389cb20f94SIra Weiny 		case Opt_dax:
21399cb20f94SIra Weiny 		case Opt_dax_always:
2140829b37b8STheodore Ts'o 			if (is_remount &&
2141829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2142829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2143829b37b8STheodore Ts'o 			fail_dax_change_remount:
2144829b37b8STheodore Ts'o 				ext4_msg(sb, KERN_ERR, "can't change "
2145829b37b8STheodore Ts'o 					 "dax mount option while remounting");
2146829b37b8STheodore Ts'o 				return -1;
2147829b37b8STheodore Ts'o 			}
2148829b37b8STheodore Ts'o 			if (is_remount &&
2149829b37b8STheodore Ts'o 			    (test_opt(sb, DATA_FLAGS) ==
2150829b37b8STheodore Ts'o 			     EXT4_MOUNT_JOURNAL_DATA)) {
2151829b37b8STheodore Ts'o 				    ext4_msg(sb, KERN_ERR, "can't mount with "
2152829b37b8STheodore Ts'o 					     "both data=journal and dax");
2153829b37b8STheodore Ts'o 				    return -1;
2154829b37b8STheodore Ts'o 			}
2155ef83b6e8SDan Williams 			ext4_msg(sb, KERN_WARNING,
2156ef83b6e8SDan Williams 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
21579cb20f94SIra Weiny 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
21589cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
21599cb20f94SIra Weiny 			break;
21609cb20f94SIra Weiny 		case Opt_dax_never:
2161829b37b8STheodore Ts'o 			if (is_remount &&
2162829b37b8STheodore Ts'o 			    (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2163829b37b8STheodore Ts'o 			     (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2164829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
21659cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
21669cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
21679cb20f94SIra Weiny 			break;
21689cb20f94SIra Weiny 		case Opt_dax_inode:
2169829b37b8STheodore Ts'o 			if (is_remount &&
2170829b37b8STheodore Ts'o 			    ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2171829b37b8STheodore Ts'o 			     (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2172829b37b8STheodore Ts'o 			     !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2173829b37b8STheodore Ts'o 				goto fail_dax_change_remount;
21749cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
21759cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
21769cb20f94SIra Weiny 			/* Strictly for printing options */
21779cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
21789cb20f94SIra Weiny 			break;
21799cb20f94SIra Weiny 		}
2180ef83b6e8SDan Williams #else
2181923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
21829cb20f94SIra Weiny 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
21839cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2184923ae0ffSRoss Zwisler 		return -1;
2185923ae0ffSRoss Zwisler #endif
21867915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
21877915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
21887915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
21897915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
219026092bf5STheodore Ts'o 	} else {
219126092bf5STheodore Ts'o 		if (!args->from)
219226092bf5STheodore Ts'o 			arg = 1;
219326092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
219426092bf5STheodore Ts'o 			arg = !arg;
219526092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
219626092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
219726092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
219826092bf5STheodore Ts'o 			WARN_ON(1);
219926092bf5STheodore Ts'o 			return -1;
220026092bf5STheodore Ts'o 		}
220126092bf5STheodore Ts'o 		if (arg != 0)
220226092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
220326092bf5STheodore Ts'o 		else
220426092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
220526092bf5STheodore Ts'o 	}
220626092bf5STheodore Ts'o 	return 1;
220726092bf5STheodore Ts'o }
220826092bf5STheodore Ts'o 
2209ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2210c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2211b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2212661aa520SEric Sandeen 			 int is_remount)
2213ac27a0ecSDave Kleikamp {
22141e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
221533458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2216ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
221726092bf5STheodore Ts'o 	int token;
2218ac27a0ecSDave Kleikamp 
2219ac27a0ecSDave Kleikamp 	if (!options)
2220ac27a0ecSDave Kleikamp 		return 1;
2221ac27a0ecSDave Kleikamp 
2222ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2223ac27a0ecSDave Kleikamp 		if (!*p)
2224ac27a0ecSDave Kleikamp 			continue;
222515121c18SEric Sandeen 		/*
222615121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
222715121c18SEric Sandeen 		 * found; some options take optional arguments.
222815121c18SEric Sandeen 		 */
2229caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2230ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
223126092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
223226092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2233ac27a0ecSDave Kleikamp 			return 0;
2234ac27a0ecSDave Kleikamp 	}
2235ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
223649da9392SJan Kara 	/*
223749da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
223849da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
223949da9392SJan Kara 	 * to support legacy quotas in quota files.
224049da9392SJan Kara 	 */
224149da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
224249da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
224349da9392SJan Kara 			 "Cannot enable project quota enforcement.");
224449da9392SJan Kara 		return 0;
224549da9392SJan Kara 	}
224633458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
224733458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
224833458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
224933458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2250fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2251ac27a0ecSDave Kleikamp 
225233458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2253fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2254ac27a0ecSDave Kleikamp 
225556c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2256b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2257b31e1552SEric Sandeen 					"format mixing");
2258ac27a0ecSDave Kleikamp 			return 0;
2259ac27a0ecSDave Kleikamp 		}
2260ac27a0ecSDave Kleikamp 
2261ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2262b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2263b31e1552SEric Sandeen 					"not specified");
2264ac27a0ecSDave Kleikamp 			return 0;
2265ac27a0ecSDave Kleikamp 		}
2266ac27a0ecSDave Kleikamp 	}
2267ac27a0ecSDave Kleikamp #endif
2268626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2269626b035bSRitesh Harjani 		int blocksize =
2270626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2271626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2272626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2273626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2274626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2275626b035bSRitesh Harjani 	}
2276ac27a0ecSDave Kleikamp 	return 1;
2277ac27a0ecSDave Kleikamp }
2278ac27a0ecSDave Kleikamp 
22792adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
22802adf6da8STheodore Ts'o 					   struct super_block *sb)
22812adf6da8STheodore Ts'o {
22822adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
22832adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
228433458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
22852adf6da8STheodore Ts'o 
22862adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
22872adf6da8STheodore Ts'o 		char *fmtname = "";
22882adf6da8STheodore Ts'o 
22892adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
22902adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
22912adf6da8STheodore Ts'o 			fmtname = "vfsold";
22922adf6da8STheodore Ts'o 			break;
22932adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
22942adf6da8STheodore Ts'o 			fmtname = "vfsv0";
22952adf6da8STheodore Ts'o 			break;
22962adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
22972adf6da8STheodore Ts'o 			fmtname = "vfsv1";
22982adf6da8STheodore Ts'o 			break;
22992adf6da8STheodore Ts'o 		}
23002adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
23012adf6da8STheodore Ts'o 	}
23022adf6da8STheodore Ts'o 
230333458eabSTheodore Ts'o 	rcu_read_lock();
230433458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
230533458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
230633458eabSTheodore Ts'o 	if (usr_qf_name)
230733458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
230833458eabSTheodore Ts'o 	if (grp_qf_name)
230933458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
231033458eabSTheodore Ts'o 	rcu_read_unlock();
23112adf6da8STheodore Ts'o #endif
23122adf6da8STheodore Ts'o }
23132adf6da8STheodore Ts'o 
23145a916be1STheodore Ts'o static const char *token2str(int token)
23155a916be1STheodore Ts'o {
231650df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
23175a916be1STheodore Ts'o 
23185a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
23195a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
23205a916be1STheodore Ts'o 			break;
23215a916be1STheodore Ts'o 	return t->pattern;
23225a916be1STheodore Ts'o }
23235a916be1STheodore Ts'o 
23242adf6da8STheodore Ts'o /*
23252adf6da8STheodore Ts'o  * Show an option if
23262adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
23272adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
23282adf6da8STheodore Ts'o  */
232966acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
233066acdcf4STheodore Ts'o 			      int nodefs)
23312adf6da8STheodore Ts'o {
23322adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
23332adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
233468afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
23355a916be1STheodore Ts'o 	const struct mount_opts *m;
233666acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
23372adf6da8STheodore Ts'o 
233866acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
233966acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
23402adf6da8STheodore Ts'o 
23412adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
23425a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
23435a916be1STheodore Ts'o 
23445a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
23455a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
23465a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
23479cb20f94SIra Weiny 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
23485a916be1STheodore Ts'o 			continue;
234968afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
23505a916be1STheodore Ts'o 			continue; /* skip if same as the default */
23515a916be1STheodore Ts'o 		if ((want_set &&
23525a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
23535a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
23545a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
23555a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
23565a916be1STheodore Ts'o 	}
23575a916be1STheodore Ts'o 
235808cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
23595a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
236008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
236108cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
236208cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
23635a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
236408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
236508cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
236666acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
23675a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
23685a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
23692adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
23705a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
23712adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
23725a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
237366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
23745a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
237566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
23765a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
237766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
23785a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2379357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
23805a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
238166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
23825a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
238368afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
238468afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
23852adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
23865a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
23872adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
23885a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
23892adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
23905a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
23915a916be1STheodore Ts'o 	}
239266acdcf4STheodore Ts'o 	if (nodefs ||
239366acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
23945a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
23952adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
23962adf6da8STheodore Ts'o 
2397ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
239866acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
23995a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2400df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2401df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
24027915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
24037915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2404ed318a6cSEric Biggers 
2405ed318a6cSEric Biggers 	fscrypt_show_test_dummy_encryption(seq, sep, sb);
24062adf6da8STheodore Ts'o 
24079cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
24089cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
24099cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
24109cb20f94SIra Weiny 		else
24119cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
24129cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
24139cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
24149cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
24159cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
24169cb20f94SIra Weiny 	}
24179cb20f94SIra Weiny 
24182adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
24192adf6da8STheodore Ts'o 	return 0;
24202adf6da8STheodore Ts'o }
24212adf6da8STheodore Ts'o 
242266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
242366acdcf4STheodore Ts'o {
242466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
242566acdcf4STheodore Ts'o }
242666acdcf4STheodore Ts'o 
2427ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
242866acdcf4STheodore Ts'o {
242966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
243066acdcf4STheodore Ts'o 	int rc;
243166acdcf4STheodore Ts'o 
2432bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
243366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
243466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
243566acdcf4STheodore Ts'o 	return rc;
243666acdcf4STheodore Ts'o }
243766acdcf4STheodore Ts'o 
2438617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2439ac27a0ecSDave Kleikamp 			    int read_only)
2440ac27a0ecSDave Kleikamp {
2441617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2442c89128a0SJaegeuk Kim 	int err = 0;
2443ac27a0ecSDave Kleikamp 
2444617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2445b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2446b31e1552SEric Sandeen 			 "forcing read-only mode");
2447c89128a0SJaegeuk Kim 		err = -EROFS;
24485adaccacSyangerkun 		goto done;
2449ac27a0ecSDave Kleikamp 	}
2450ac27a0ecSDave Kleikamp 	if (read_only)
2451281b5995STheodore Ts'o 		goto done;
2452617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2453b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2454b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2455c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2456b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2457b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2458b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2459ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2460ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2461ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2462b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2463b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2464b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2465ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
24666a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
24676a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2468b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2469b31e1552SEric Sandeen 			 "warning: checktime reached, "
2470b31e1552SEric Sandeen 			 "running e2fsck is recommended");
24710390131bSFrank Mayhar 	if (!sbi->s_journal)
2472216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2473ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2474617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2475e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
24766a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
24770390131bSFrank Mayhar 	if (sbi->s_journal)
2478e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2479ac27a0ecSDave Kleikamp 
2480c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2481281b5995STheodore Ts'o done:
2482ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2483a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2484a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2485ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2486ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2487617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2488617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2489a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2490ac27a0ecSDave Kleikamp 
24917abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2492c89128a0SJaegeuk Kim 	return err;
2493ac27a0ecSDave Kleikamp }
2494ac27a0ecSDave Kleikamp 
2495117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2496117fff10STheodore Ts'o {
2497117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
24987c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
249937b0b6b8SDan Carpenter 	int size, i, j;
2500117fff10STheodore Ts'o 
2501117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2502117fff10STheodore Ts'o 		return 0;
2503117fff10STheodore Ts'o 
2504117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2505117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2506117fff10STheodore Ts'o 		return 0;
2507117fff10STheodore Ts'o 
25087c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
25097c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2510117fff10STheodore Ts'o 	if (!new_groups) {
25117c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
25127c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2513117fff10STheodore Ts'o 		return -ENOMEM;
2514117fff10STheodore Ts'o 	}
25157c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
25167c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
25177c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
25187c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
25197c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
252037b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
252137b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
25227c990728SSuraj Jitindar Singh 			kvfree(new_groups);
25237c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
25247c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
25257c990728SSuraj Jitindar Singh 			return -ENOMEM;
2526117fff10STheodore Ts'o 		}
25277c990728SSuraj Jitindar Singh 	}
25287c990728SSuraj Jitindar Singh 	rcu_read_lock();
25297c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
25307c990728SSuraj Jitindar Singh 	if (old_groups)
25317c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
25327c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
25337c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
25347c990728SSuraj Jitindar Singh 	rcu_read_unlock();
25357c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
25367c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
25377c990728SSuraj Jitindar Singh 	if (old_groups)
25387c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2539117fff10STheodore Ts'o 	return 0;
2540117fff10STheodore Ts'o }
2541117fff10STheodore Ts'o 
2542772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2543772cb7c8SJose R. Santos {
2544772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2545772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
25467c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2547772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2548117fff10STheodore Ts'o 	int i, err;
2549772cb7c8SJose R. Santos 
2550503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2551d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2552772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2553772cb7c8SJose R. Santos 		return 1;
2554772cb7c8SJose R. Santos 	}
2555772cb7c8SJose R. Santos 
2556117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2557117fff10STheodore Ts'o 	if (err)
2558772cb7c8SJose R. Santos 		goto failed;
2559772cb7c8SJose R. Santos 
2560772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
256188b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2562772cb7c8SJose R. Santos 
2563772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
25647c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
25657c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
256690ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
25677c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
25687c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2569772cb7c8SJose R. Santos 	}
2570772cb7c8SJose R. Santos 
2571772cb7c8SJose R. Santos 	return 1;
2572772cb7c8SJose R. Santos failed:
2573772cb7c8SJose R. Santos 	return 0;
2574772cb7c8SJose R. Santos }
2575772cb7c8SJose R. Santos 
2576e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2577717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2578717d50e4SAndreas Dilger {
2579b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2580717d50e4SAndreas Dilger 	__u16 crc = 0;
2581717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2582e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2583717d50e4SAndreas Dilger 
25849aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2585feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2586feb0ab32SDarrick J. Wong 		__u32 csum32;
2587b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2588feb0ab32SDarrick J. Wong 
2589feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2590feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2591b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2592b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2593b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2594b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2595b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2596b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2597b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2598feb0ab32SDarrick J. Wong 
2599feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2600feb0ab32SDarrick J. Wong 		goto out;
2601feb0ab32SDarrick J. Wong 	}
2602feb0ab32SDarrick J. Wong 
2603feb0ab32SDarrick J. Wong 	/* old crc16 code */
2604e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2605813d32f9SDarrick J. Wong 		return 0;
2606813d32f9SDarrick J. Wong 
2607717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2608717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2609717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2610717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2611717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2612e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2613717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2614717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2615717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2616717d50e4SAndreas Dilger 				offset);
2617717d50e4SAndreas Dilger 
2618feb0ab32SDarrick J. Wong out:
2619717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2620717d50e4SAndreas Dilger }
2621717d50e4SAndreas Dilger 
2622feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2623717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2624717d50e4SAndreas Dilger {
2625feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2626e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2627717d50e4SAndreas Dilger 		return 0;
2628717d50e4SAndreas Dilger 
2629717d50e4SAndreas Dilger 	return 1;
2630717d50e4SAndreas Dilger }
2631717d50e4SAndreas Dilger 
2632feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2633feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2634feb0ab32SDarrick J. Wong {
2635feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2636feb0ab32SDarrick J. Wong 		return;
2637e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2638feb0ab32SDarrick J. Wong }
2639feb0ab32SDarrick J. Wong 
2640ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2641bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2642829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2643bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2644ac27a0ecSDave Kleikamp {
2645617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2646617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2647617ba13bSMingming Cao 	ext4_fsblk_t last_block;
264844de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2649bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2650bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2651bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2652ce421581SJose R. Santos 	int flexbg_flag = 0;
2653bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2654ac27a0ecSDave Kleikamp 
2655e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2656ce421581SJose R. Santos 		flexbg_flag = 1;
2657ce421581SJose R. Santos 
2658617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2659ac27a0ecSDave Kleikamp 
2660197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2661197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2662197cd65aSAkinobu Mita 
2663ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2664bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2665ac27a0ecSDave Kleikamp 		else
2666ac27a0ecSDave Kleikamp 			last_block = first_block +
2667617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2668ac27a0ecSDave Kleikamp 
2669bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2670bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2671bfff6873SLukas Czerner 			grp = i;
2672bfff6873SLukas Czerner 
26738fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2674829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2675829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2676829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2677829fa70dSTheodore Ts'o 				 "superblock", i);
267818db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
267918db4b4eSTheodore Ts'o 				return 0;
2680829fa70dSTheodore Ts'o 		}
268177260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
268277260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
268377260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
268477260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
268577260807STheodore Ts'o 				 "block group descriptors", i);
268677260807STheodore Ts'o 			if (!sb_rdonly(sb))
268777260807STheodore Ts'o 				return 0;
268877260807STheodore Ts'o 		}
26892b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2690b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2691a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2692b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2693ac27a0ecSDave Kleikamp 			return 0;
2694ac27a0ecSDave Kleikamp 		}
26958fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2696829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2697829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2698829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2699829fa70dSTheodore Ts'o 				 "superblock", i);
270018db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
270118db4b4eSTheodore Ts'o 				return 0;
2702829fa70dSTheodore Ts'o 		}
270377260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
270477260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
270577260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
270677260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
270777260807STheodore Ts'o 				 "block group descriptors", i);
270877260807STheodore Ts'o 			if (!sb_rdonly(sb))
270977260807STheodore Ts'o 				return 0;
271077260807STheodore Ts'o 		}
27112b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2712b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2713a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2714b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2715ac27a0ecSDave Kleikamp 			return 0;
2716ac27a0ecSDave Kleikamp 		}
27178fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2718829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2719829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2720829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2721829fa70dSTheodore Ts'o 				 "superblock", i);
272218db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
272318db4b4eSTheodore Ts'o 				return 0;
2724829fa70dSTheodore Ts'o 		}
272577260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
272677260807STheodore Ts'o 		    inode_table <= last_bg_block) {
272777260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
272877260807STheodore Ts'o 				 "Inode table for group %u overlaps "
272977260807STheodore Ts'o 				 "block group descriptors", i);
273077260807STheodore Ts'o 			if (!sb_rdonly(sb))
273177260807STheodore Ts'o 				return 0;
273277260807STheodore Ts'o 		}
2733bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
27342b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2735b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2736a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2737b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2738ac27a0ecSDave Kleikamp 			return 0;
2739ac27a0ecSDave Kleikamp 		}
2740955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2741feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2742b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2743b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2744e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2745fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2746bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2747955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2748717d50e4SAndreas Dilger 				return 0;
2749717d50e4SAndreas Dilger 			}
27507ee1ec4cSLi Zefan 		}
2751955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2752ce421581SJose R. Santos 		if (!flexbg_flag)
2753617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2754ac27a0ecSDave Kleikamp 	}
2755bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2756bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2757ac27a0ecSDave Kleikamp 	return 1;
2758ac27a0ecSDave Kleikamp }
2759ac27a0ecSDave Kleikamp 
2760617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2761ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2762ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2763ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2764ac27a0ecSDave Kleikamp  *
2765ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2766ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2767ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2768ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2769ac27a0ecSDave Kleikamp  *
2770ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2771ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2772ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2773617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2774ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2775ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2776ac27a0ecSDave Kleikamp  */
2777617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2778617ba13bSMingming Cao 				struct ext4_super_block *es)
2779ac27a0ecSDave Kleikamp {
2780ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
27812c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2782ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
278395f1fda4Szhangyi (F) 	int quota_update = 0;
2784ac27a0ecSDave Kleikamp 	int i;
2785ac27a0ecSDave Kleikamp #endif
2786ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2787ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2788ac27a0ecSDave Kleikamp 		return;
2789ac27a0ecSDave Kleikamp 	}
2790ac27a0ecSDave Kleikamp 
2791a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2792b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2793b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2794a8f48a95SEric Sandeen 		return;
2795a8f48a95SEric Sandeen 	}
2796a8f48a95SEric Sandeen 
2797d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2798d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2799d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2800d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2801d39195c3SAmir Goldstein 		return;
2802d39195c3SAmir Goldstein 	}
2803d39195c3SAmir Goldstein 
2804617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2805c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
28061751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
280784474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2808ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2809ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2810c25f9bc6SEric Sandeen 		}
2811ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2812ac27a0ecSDave Kleikamp 		return;
2813ac27a0ecSDave Kleikamp 	}
2814ac27a0ecSDave Kleikamp 
28151751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2816b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
28171751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2818ac27a0ecSDave Kleikamp 	}
2819ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2820ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
28211751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
282295f1fda4Szhangyi (F) 
282395f1fda4Szhangyi (F) 	/*
282495f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
282595f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
282695f1fda4Szhangyi (F) 	 */
28271751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
282895f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
282995f1fda4Szhangyi (F) 
283095f1fda4Szhangyi (F) 		if (!ret)
283195f1fda4Szhangyi (F) 			quota_update = 1;
283295f1fda4Szhangyi (F) 		else
283395f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
283495f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
283595f1fda4Szhangyi (F) 	}
283695f1fda4Szhangyi (F) 
283795f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2838a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2839617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2840617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
284195f1fda4Szhangyi (F) 
284295f1fda4Szhangyi (F) 			if (!ret)
284395f1fda4Szhangyi (F) 				quota_update = 1;
284495f1fda4Szhangyi (F) 			else
2845b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2846b31e1552SEric Sandeen 					"Cannot turn on journaled "
284795f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2848ac27a0ecSDave Kleikamp 		}
2849ac27a0ecSDave Kleikamp 	}
2850ac27a0ecSDave Kleikamp #endif
2851ac27a0ecSDave Kleikamp 
2852ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2853ac27a0ecSDave Kleikamp 		struct inode *inode;
2854ac27a0ecSDave Kleikamp 
2855c65d5c6cSVegard Nossum 		/*
2856c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2857c65d5c6cSVegard Nossum 		 * so, skip the rest.
2858c65d5c6cSVegard Nossum 		 */
2859c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2860c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2861c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2862c65d5c6cSVegard Nossum 			break;
2863c65d5c6cSVegard Nossum 		}
2864c65d5c6cSVegard Nossum 
286597bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
286697bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2867ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2868ac27a0ecSDave Kleikamp 			break;
2869ac27a0ecSDave Kleikamp 		}
2870ac27a0ecSDave Kleikamp 
2871617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2872871a2931SChristoph Hellwig 		dquot_initialize(inode);
2873ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2874566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2875b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2876b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
287746e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2878e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2879ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
28805955102cSAl Viro 			inode_lock(inode);
288155f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
28822c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
28832c98eb5eSTheodore Ts'o 			if (ret)
28842c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
28855955102cSAl Viro 			inode_unlock(inode);
2886ac27a0ecSDave Kleikamp 			nr_truncates++;
2887ac27a0ecSDave Kleikamp 		} else {
2888566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2889b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2890b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
289146e665e9SHarvey Harrison 					__func__, inode->i_ino);
2892ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2893ac27a0ecSDave Kleikamp 				  inode->i_ino);
2894ac27a0ecSDave Kleikamp 			nr_orphans++;
2895ac27a0ecSDave Kleikamp 		}
2896ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2897ac27a0ecSDave Kleikamp 	}
2898ac27a0ecSDave Kleikamp 
2899ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2900ac27a0ecSDave Kleikamp 
2901ac27a0ecSDave Kleikamp 	if (nr_orphans)
2902b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2903b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2904ac27a0ecSDave Kleikamp 	if (nr_truncates)
2905b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2906b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2907ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
290895f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
290995f1fda4Szhangyi (F) 	if (quota_update) {
2910a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
291195f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2912287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2913ac27a0ecSDave Kleikamp 		}
291495f1fda4Szhangyi (F) 	}
2915ac27a0ecSDave Kleikamp #endif
29161751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2917ac27a0ecSDave Kleikamp }
29180b8e58a1SAndreas Dilger 
2919cd2291a4SEric Sandeen /*
2920cd2291a4SEric Sandeen  * Maximal extent format file size.
2921cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2922cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2923cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2924cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2925cd2291a4SEric Sandeen  *
2926f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2927f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2928f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2929f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2930f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2931f17722f9SLukas Czerner  *
2932cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2933cd2291a4SEric Sandeen  */
2934f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2935cd2291a4SEric Sandeen {
2936cd2291a4SEric Sandeen 	loff_t res;
2937cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2938cd2291a4SEric Sandeen 
293972deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
294072deb455SChristoph Hellwig 
294172deb455SChristoph Hellwig 	if (!has_huge_files) {
2942cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2943cd2291a4SEric Sandeen 
2944cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2945cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2946cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2947cd2291a4SEric Sandeen 	}
2948cd2291a4SEric Sandeen 
2949f17722f9SLukas Czerner 	/*
2950f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2951f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2952f17722f9SLukas Czerner 	 * size
2953f17722f9SLukas Czerner 	 */
2954f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2955cd2291a4SEric Sandeen 	res <<= blkbits;
2956cd2291a4SEric Sandeen 
2957cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2958cd2291a4SEric Sandeen 	if (res > upper_limit)
2959cd2291a4SEric Sandeen 		res = upper_limit;
2960cd2291a4SEric Sandeen 
2961cd2291a4SEric Sandeen 	return res;
2962cd2291a4SEric Sandeen }
2963ac27a0ecSDave Kleikamp 
2964ac27a0ecSDave Kleikamp /*
2965cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
29660fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
29670fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2968ac27a0ecSDave Kleikamp  */
2969f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2970ac27a0ecSDave Kleikamp {
2971617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
29720fc1b451SAneesh Kumar K.V 	int meta_blocks;
29730fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
29740b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
29750b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
29760b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
29770b8e58a1SAndreas Dilger 	 *
29780b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
29790b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
29800fc1b451SAneesh Kumar K.V 	 */
29810fc1b451SAneesh Kumar K.V 
298272deb455SChristoph Hellwig 	if (!has_huge_files) {
29830fc1b451SAneesh Kumar K.V 		/*
298472deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
298572deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
298672deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
29870fc1b451SAneesh Kumar K.V 		 */
29880fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
29890fc1b451SAneesh Kumar K.V 
29900fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
29910fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
29920fc1b451SAneesh Kumar K.V 
29930fc1b451SAneesh Kumar K.V 	} else {
29948180a562SAneesh Kumar K.V 		/*
29958180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
29968180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
29978180a562SAneesh Kumar K.V 		 * represent total number of blocks in
29988180a562SAneesh Kumar K.V 		 * file system block size
29998180a562SAneesh Kumar K.V 		 */
30000fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
30010fc1b451SAneesh Kumar K.V 
30020fc1b451SAneesh Kumar K.V 	}
30030fc1b451SAneesh Kumar K.V 
30040fc1b451SAneesh Kumar K.V 	/* indirect blocks */
30050fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
30060fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
30070fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
30080fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
30090fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
30100fc1b451SAneesh Kumar K.V 
30110fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
30120fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
3013ac27a0ecSDave Kleikamp 
3014ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
3015ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
3016ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
3017ac27a0ecSDave Kleikamp 	res <<= bits;
3018ac27a0ecSDave Kleikamp 	if (res > upper_limit)
3019ac27a0ecSDave Kleikamp 		res = upper_limit;
30200fc1b451SAneesh Kumar K.V 
30210fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
30220fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
30230fc1b451SAneesh Kumar K.V 
3024ac27a0ecSDave Kleikamp 	return res;
3025ac27a0ecSDave Kleikamp }
3026ac27a0ecSDave Kleikamp 
3027617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
302870bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
3029ac27a0ecSDave Kleikamp {
3030617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3031fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
3032ac27a0ecSDave Kleikamp 	int has_super = 0;
3033ac27a0ecSDave Kleikamp 
3034ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3035ac27a0ecSDave Kleikamp 
3036e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
303770bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
3038ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
3039617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
3040ac27a0ecSDave Kleikamp 		has_super = 1;
30410b8e58a1SAndreas Dilger 
3042bd63f6b0SDarrick J. Wong 	/*
3043bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3044bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
3045bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3046bd63f6b0SDarrick J. Wong 	 * compensate.
3047bd63f6b0SDarrick J. Wong 	 */
3048bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
304949598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
3050bd63f6b0SDarrick J. Wong 		has_super++;
3051bd63f6b0SDarrick J. Wong 
3052617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
3053ac27a0ecSDave Kleikamp }
3054ac27a0ecSDave Kleikamp 
3055c9de560dSAlex Tomas /**
3056c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
3057c9de560dSAlex Tomas  * @sbi: In memory super block info
3058c9de560dSAlex Tomas  *
3059c9de560dSAlex Tomas  * If we have specified it via mount option, then
3060c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3061c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3062c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3063c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3064c9de560dSAlex Tomas  *
3065c9de560dSAlex Tomas  */
3066c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3067c9de560dSAlex Tomas {
3068c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3069c9de560dSAlex Tomas 	unsigned long stripe_width =
3070c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
30713eb08658SDan Ehrenberg 	int ret;
3072c9de560dSAlex Tomas 
3073c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
30743eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
30755469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
30763eb08658SDan Ehrenberg 		ret = stripe_width;
30775469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
30783eb08658SDan Ehrenberg 		ret = stride;
30793eb08658SDan Ehrenberg 	else
30803eb08658SDan Ehrenberg 		ret = 0;
3081c9de560dSAlex Tomas 
30823eb08658SDan Ehrenberg 	/*
30833eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
30843eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
30853eb08658SDan Ehrenberg 	 */
30863eb08658SDan Ehrenberg 	if (ret <= 1)
30873eb08658SDan Ehrenberg 		ret = 0;
3088c9de560dSAlex Tomas 
30893eb08658SDan Ehrenberg 	return ret;
3090c9de560dSAlex Tomas }
3091ac27a0ecSDave Kleikamp 
3092a13fb1a4SEric Sandeen /*
3093a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3094a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3095a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3096a13fb1a4SEric Sandeen  * 0 if it cannot be.
3097a13fb1a4SEric Sandeen  */
3098a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3099a13fb1a4SEric Sandeen {
3100e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3101a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3102a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3103a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3104a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3105a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3106a13fb1a4SEric Sandeen 		return 0;
3107a13fb1a4SEric Sandeen 	}
3108a13fb1a4SEric Sandeen 
3109c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3110c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3111c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3112c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3113c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3114c83ad55eSGabriel Krisman Bertazi 		return 0;
3115c83ad55eSGabriel Krisman Bertazi 	}
3116c83ad55eSGabriel Krisman Bertazi #endif
3117c83ad55eSGabriel Krisman Bertazi 
3118a13fb1a4SEric Sandeen 	if (readonly)
3119a13fb1a4SEric Sandeen 		return 1;
3120a13fb1a4SEric Sandeen 
3121e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
31222cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
31231751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
31242cb5cc8bSDarrick J. Wong 		return 1;
31252cb5cc8bSDarrick J. Wong 	}
31262cb5cc8bSDarrick J. Wong 
3127a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3128e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3129a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3130a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3131a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3132a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3133a13fb1a4SEric Sandeen 		return 0;
3134a13fb1a4SEric Sandeen 	}
3135e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3136bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3137bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3138bab08ab9STheodore Ts'o 			 "extents feature\n");
3139bab08ab9STheodore Ts'o 		return 0;
3140bab08ab9STheodore Ts'o 	}
31417c319d32SAditya Kali 
31429db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3143d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3144d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
31457c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3146d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3147689c958cSLi Xi 		return 0;
3148689c958cSLi Xi 	}
31497c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3150a13fb1a4SEric Sandeen 	return 1;
3151a13fb1a4SEric Sandeen }
3152a13fb1a4SEric Sandeen 
315366e61a9eSTheodore Ts'o /*
315466e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
315566e61a9eSTheodore Ts'o  * on the file system
315666e61a9eSTheodore Ts'o  */
3157235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
315866e61a9eSTheodore Ts'o {
3159235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3160235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3161235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
316266e61a9eSTheodore Ts'o 
316366e61a9eSTheodore Ts'o 	if (es->s_error_count)
3164ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3165ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
316666e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
316766e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
31686a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
31696a0678a7SArnd Bergmann 		       sb->s_id,
31706a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
317166e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
317266e61a9eSTheodore Ts'o 		       es->s_first_error_func,
317366e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
317466e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3175651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
317666e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
317766e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3178651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
317966e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3180651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
318166e61a9eSTheodore Ts'o 	}
318266e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
31836a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
31846a0678a7SArnd Bergmann 		       sb->s_id,
31856a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
318666e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
318766e61a9eSTheodore Ts'o 		       es->s_last_error_func,
318866e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
318966e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3190651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
319166e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
319266e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3193651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
319466e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3195651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
319666e61a9eSTheodore Ts'o 	}
319766e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
319866e61a9eSTheodore Ts'o }
319966e61a9eSTheodore Ts'o 
3200bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3201bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3202bfff6873SLukas Czerner {
3203bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3204bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3205bfff6873SLukas Czerner 	struct super_block *sb;
3206bfff6873SLukas Czerner 	unsigned long timeout = 0;
3207bfff6873SLukas Czerner 	int ret = 0;
3208bfff6873SLukas Czerner 
3209bfff6873SLukas Czerner 	sb = elr->lr_super;
3210bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3211bfff6873SLukas Czerner 
3212bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3213bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3214bfff6873SLukas Czerner 		if (!gdp) {
3215bfff6873SLukas Czerner 			ret = 1;
3216bfff6873SLukas Czerner 			break;
3217bfff6873SLukas Czerner 		}
3218bfff6873SLukas Czerner 
3219bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3220bfff6873SLukas Czerner 			break;
3221bfff6873SLukas Czerner 	}
3222bfff6873SLukas Czerner 
32237f511862STheodore Ts'o 	if (group >= ngroups)
3224bfff6873SLukas Czerner 		ret = 1;
3225bfff6873SLukas Czerner 
3226bfff6873SLukas Czerner 	if (!ret) {
3227bfff6873SLukas Czerner 		timeout = jiffies;
3228bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3229bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3230bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
323151ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
323251ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3233bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3234bfff6873SLukas Czerner 		}
3235bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3236bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3237bfff6873SLukas Czerner 	}
3238bfff6873SLukas Czerner 	return ret;
3239bfff6873SLukas Czerner }
3240bfff6873SLukas Czerner 
3241bfff6873SLukas Czerner /*
3242bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
32434ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3244bfff6873SLukas Czerner  */
3245bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3246bfff6873SLukas Czerner {
3247bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3248bfff6873SLukas Czerner 
3249bfff6873SLukas Czerner 	if (!elr)
3250bfff6873SLukas Czerner 		return;
3251bfff6873SLukas Czerner 
3252bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3253bfff6873SLukas Czerner 
3254bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3255bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3256bfff6873SLukas Czerner 	kfree(elr);
3257bfff6873SLukas Czerner }
3258bfff6873SLukas Czerner 
3259bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3260bfff6873SLukas Czerner {
32611bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
32621bb933fbSLukas Czerner 	if (!ext4_li_info) {
32631bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3264bfff6873SLukas Czerner 		return;
32651bb933fbSLukas Czerner 	}
3266bfff6873SLukas Czerner 
3267bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
32681bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3269bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
32701bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3271bfff6873SLukas Czerner }
3272bfff6873SLukas Czerner 
32738f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
32748f1f7453SEric Sandeen 
3275bfff6873SLukas Czerner /*
3276bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3277bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3278bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3279bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3280bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3281bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3282bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3283bfff6873SLukas Czerner  */
3284bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3285bfff6873SLukas Czerner {
3286bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3287bfff6873SLukas Czerner 	struct list_head *pos, *n;
3288bfff6873SLukas Czerner 	struct ext4_li_request *elr;
32894ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3290bfff6873SLukas Czerner 
3291bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3292bfff6873SLukas Czerner 
3293bfff6873SLukas Czerner cont_thread:
3294bfff6873SLukas Czerner 	while (true) {
3295bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3296bfff6873SLukas Czerner 
3297bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3298bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3299bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3300bfff6873SLukas Czerner 			goto exit_thread;
3301bfff6873SLukas Czerner 		}
3302bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3303e22834f0SDmitry Monakhov 			int err = 0;
3304e22834f0SDmitry Monakhov 			int progress = 0;
3305bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3306bfff6873SLukas Czerner 					 lr_request);
3307bfff6873SLukas Czerner 
3308e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3309e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3310e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3311e22834f0SDmitry Monakhov 				continue;
3312e22834f0SDmitry Monakhov 			}
3313e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3314e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3315e22834f0SDmitry Monakhov 					progress = 1;
3316e22834f0SDmitry Monakhov 					/*
3317e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3318e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3319e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3320e22834f0SDmitry Monakhov 					 */
3321e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3322e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3323e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3324e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3325e22834f0SDmitry Monakhov 					n = pos->next;
3326e22834f0SDmitry Monakhov 				}
3327e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3328e22834f0SDmitry Monakhov 			}
3329b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3330e22834f0SDmitry Monakhov 			if (err) {
3331bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3332bfff6873SLukas Czerner 				continue;
3333bfff6873SLukas Czerner 			}
3334e22834f0SDmitry Monakhov 			if (!progress) {
3335e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3336e22834f0SDmitry Monakhov 					(prandom_u32()
3337e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3338b2c78cd0STheodore Ts'o 			}
3339bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3340bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3341bfff6873SLukas Czerner 		}
3342bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3343bfff6873SLukas Czerner 
3344a0acae0eSTejun Heo 		try_to_freeze();
3345bfff6873SLukas Czerner 
33464ed5c033SLukas Czerner 		cur = jiffies;
33474ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3348f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3349bfff6873SLukas Czerner 			cond_resched();
3350bfff6873SLukas Czerner 			continue;
3351bfff6873SLukas Czerner 		}
3352bfff6873SLukas Czerner 
33534ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
33544ed5c033SLukas Czerner 
33558f1f7453SEric Sandeen 		if (kthread_should_stop()) {
33568f1f7453SEric Sandeen 			ext4_clear_request_list();
33578f1f7453SEric Sandeen 			goto exit_thread;
33588f1f7453SEric Sandeen 		}
3359bfff6873SLukas Czerner 	}
3360bfff6873SLukas Czerner 
3361bfff6873SLukas Czerner exit_thread:
3362bfff6873SLukas Czerner 	/*
3363bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3364bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3365bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3366bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3367bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3368bfff6873SLukas Czerner 	 * new one.
3369bfff6873SLukas Czerner 	 */
3370bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3371bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3372bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3373bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3374bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3375bfff6873SLukas Czerner 		goto cont_thread;
3376bfff6873SLukas Czerner 	}
3377bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3378bfff6873SLukas Czerner 	kfree(ext4_li_info);
3379bfff6873SLukas Czerner 	ext4_li_info = NULL;
3380bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3381bfff6873SLukas Czerner 
3382bfff6873SLukas Czerner 	return 0;
3383bfff6873SLukas Czerner }
3384bfff6873SLukas Czerner 
3385bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3386bfff6873SLukas Czerner {
3387bfff6873SLukas Czerner 	struct list_head *pos, *n;
3388bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3389bfff6873SLukas Czerner 
3390bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3391bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3392bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3393bfff6873SLukas Czerner 				 lr_request);
3394bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3395bfff6873SLukas Czerner 	}
3396bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3397bfff6873SLukas Czerner }
3398bfff6873SLukas Czerner 
3399bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3400bfff6873SLukas Czerner {
34018f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
34028f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
34038f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
34048f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3405bfff6873SLukas Czerner 		ext4_clear_request_list();
3406bfff6873SLukas Czerner 		kfree(ext4_li_info);
3407bfff6873SLukas Czerner 		ext4_li_info = NULL;
340892b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3409bfff6873SLukas Czerner 				 "initialization thread\n",
3410bfff6873SLukas Czerner 				 err);
3411bfff6873SLukas Czerner 		return err;
3412bfff6873SLukas Czerner 	}
3413bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3414bfff6873SLukas Czerner 	return 0;
3415bfff6873SLukas Czerner }
3416bfff6873SLukas Czerner 
3417bfff6873SLukas Czerner /*
3418bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3419bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3420bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3421bfff6873SLukas Czerner  * groups and return total number of groups.
3422bfff6873SLukas Czerner  */
3423bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3424bfff6873SLukas Czerner {
3425bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3426bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3427bfff6873SLukas Czerner 
34288844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
34298844618dSTheodore Ts'o 		return ngroups;
34308844618dSTheodore Ts'o 
3431bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3432bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3433bfff6873SLukas Czerner 		if (!gdp)
3434bfff6873SLukas Czerner 			continue;
3435bfff6873SLukas Czerner 
343650122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3437bfff6873SLukas Czerner 			break;
3438bfff6873SLukas Czerner 	}
3439bfff6873SLukas Czerner 
3440bfff6873SLukas Czerner 	return group;
3441bfff6873SLukas Czerner }
3442bfff6873SLukas Czerner 
3443bfff6873SLukas Czerner static int ext4_li_info_new(void)
3444bfff6873SLukas Czerner {
3445bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3446bfff6873SLukas Czerner 
3447bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3448bfff6873SLukas Czerner 	if (!eli)
3449bfff6873SLukas Czerner 		return -ENOMEM;
3450bfff6873SLukas Czerner 
3451bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3452bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3453bfff6873SLukas Czerner 
3454bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3455bfff6873SLukas Czerner 
3456bfff6873SLukas Czerner 	ext4_li_info = eli;
3457bfff6873SLukas Czerner 
3458bfff6873SLukas Czerner 	return 0;
3459bfff6873SLukas Czerner }
3460bfff6873SLukas Czerner 
3461bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3462bfff6873SLukas Czerner 					    ext4_group_t start)
3463bfff6873SLukas Czerner {
3464bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3465bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3466bfff6873SLukas Czerner 
3467bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3468bfff6873SLukas Czerner 	if (!elr)
3469bfff6873SLukas Czerner 		return NULL;
3470bfff6873SLukas Czerner 
3471bfff6873SLukas Czerner 	elr->lr_super = sb;
3472bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3473bfff6873SLukas Czerner 	elr->lr_next_group = start;
3474bfff6873SLukas Czerner 
3475bfff6873SLukas Czerner 	/*
3476bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3477bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3478bfff6873SLukas Czerner 	 * better.
3479bfff6873SLukas Czerner 	 */
3480dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3481dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3482bfff6873SLukas Czerner 	return elr;
3483bfff6873SLukas Czerner }
3484bfff6873SLukas Czerner 
34857f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3486bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3487bfff6873SLukas Czerner {
3488bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
34897f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
349049598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
34916c5a6cb9SAndrew Morton 	int ret = 0;
3492bfff6873SLukas Czerner 
34937f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
349451ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
349551ce6511SLukas Czerner 		/*
349651ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
349751ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
349851ce6511SLukas Czerner 		 */
349951ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
35007f511862STheodore Ts'o 		goto out;
350151ce6511SLukas Czerner 	}
3502bfff6873SLukas Czerner 
3503bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
350455ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
35057f511862STheodore Ts'o 		goto out;
3506bfff6873SLukas Czerner 
3507bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
35087f511862STheodore Ts'o 	if (!elr) {
35097f511862STheodore Ts'o 		ret = -ENOMEM;
35107f511862STheodore Ts'o 		goto out;
35117f511862STheodore Ts'o 	}
3512bfff6873SLukas Czerner 
3513bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3514bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3515bfff6873SLukas Czerner 		if (ret)
3516bfff6873SLukas Czerner 			goto out;
3517bfff6873SLukas Czerner 	}
3518bfff6873SLukas Czerner 
3519bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3520bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3521bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3522bfff6873SLukas Czerner 
3523bfff6873SLukas Czerner 	sbi->s_li_request = elr;
352446e4690bSTao Ma 	/*
352546e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
352646e4690bSTao Ma 	 * the request_list and the removal and free of it is
352746e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
352846e4690bSTao Ma 	 */
352946e4690bSTao Ma 	elr = NULL;
3530bfff6873SLukas Czerner 
3531bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3532bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3533bfff6873SLukas Czerner 		if (ret)
3534bfff6873SLukas Czerner 			goto out;
3535bfff6873SLukas Czerner 	}
3536bfff6873SLukas Czerner out:
3537bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3538beed5ecbSNicolas Kaiser 	if (ret)
3539bfff6873SLukas Czerner 		kfree(elr);
3540bfff6873SLukas Czerner 	return ret;
3541bfff6873SLukas Czerner }
3542bfff6873SLukas Czerner 
3543bfff6873SLukas Czerner /*
3544bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3545bfff6873SLukas Czerner  * module unload.
3546bfff6873SLukas Czerner  */
3547bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3548bfff6873SLukas Czerner {
3549bfff6873SLukas Czerner 	/*
3550bfff6873SLukas Czerner 	 * If thread exited earlier
3551bfff6873SLukas Czerner 	 * there's nothing to be done.
3552bfff6873SLukas Czerner 	 */
35538f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3554bfff6873SLukas Czerner 		return;
3555bfff6873SLukas Czerner 
35568f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3557bfff6873SLukas Czerner }
3558bfff6873SLukas Czerner 
355925ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
356025ed6e8aSDarrick J. Wong {
356125ed6e8aSDarrick J. Wong 	int ret = 1;
356225ed6e8aSDarrick J. Wong 	int compat, incompat;
356325ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
356425ed6e8aSDarrick J. Wong 
35659aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3566db9ee220SDarrick J. Wong 		/* journal checksum v3 */
356725ed6e8aSDarrick J. Wong 		compat = 0;
3568db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
356925ed6e8aSDarrick J. Wong 	} else {
357025ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
357125ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
357225ed6e8aSDarrick J. Wong 		incompat = 0;
357325ed6e8aSDarrick J. Wong 	}
357425ed6e8aSDarrick J. Wong 
3575feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3576feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3577feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3578feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
357925ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
358025ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
358125ed6e8aSDarrick J. Wong 				compat, 0,
358225ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
358325ed6e8aSDarrick J. Wong 				incompat);
358425ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
358525ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
358625ed6e8aSDarrick J. Wong 				compat, 0,
358725ed6e8aSDarrick J. Wong 				incompat);
358825ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
358925ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
359025ed6e8aSDarrick J. Wong 	} else {
3591feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3592feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
359325ed6e8aSDarrick J. Wong 	}
359425ed6e8aSDarrick J. Wong 
359525ed6e8aSDarrick J. Wong 	return ret;
359625ed6e8aSDarrick J. Wong }
359725ed6e8aSDarrick J. Wong 
3598952fc18eSTheodore Ts'o /*
3599952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3600952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3601952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3602952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3603952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3604952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3605952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3606952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3607952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3608952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3609952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3610952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3611952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3612952fc18eSTheodore Ts'o  */
3613952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3614952fc18eSTheodore Ts'o 			  char *buf)
3615952fc18eSTheodore Ts'o {
3616952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3617952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3618952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3619952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3620952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3621952fc18eSTheodore Ts'o 
3622e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
36230548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
36240548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
36250548bbb8STheodore Ts'o 
3626952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3627952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3628952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3629952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3630952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3631952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3632952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3633952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3634952fc18eSTheodore Ts'o 			count++;
3635952fc18eSTheodore Ts'o 		}
3636952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3637952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3638952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3639952fc18eSTheodore Ts'o 			count++;
3640952fc18eSTheodore Ts'o 		}
3641952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3642952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3643952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3644952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3645952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3646952fc18eSTheodore Ts'o 				count++;
3647952fc18eSTheodore Ts'o 			}
3648952fc18eSTheodore Ts'o 		if (i != grp)
3649952fc18eSTheodore Ts'o 			continue;
3650952fc18eSTheodore Ts'o 		s = 0;
3651952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3652952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3653952fc18eSTheodore Ts'o 			count++;
3654952fc18eSTheodore Ts'o 		}
3655c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3656c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3657c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3658c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3659c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3660952fc18eSTheodore Ts'o 		}
3661c48ae41bSTheodore Ts'o 		count += j;
3662c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3663c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3664952fc18eSTheodore Ts'o 	}
3665952fc18eSTheodore Ts'o 	if (!count)
3666952fc18eSTheodore Ts'o 		return 0;
3667952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3668952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3669952fc18eSTheodore Ts'o }
3670952fc18eSTheodore Ts'o 
3671952fc18eSTheodore Ts'o /*
3672952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3673952fc18eSTheodore Ts'o  */
3674952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3675952fc18eSTheodore Ts'o {
3676952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3677952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
36783c816dedSEric Whitney 	struct inode *j_inode;
36793c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3680952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3681952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
36824fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3683952fc18eSTheodore Ts'o 
3684952fc18eSTheodore Ts'o 	if (!buf)
3685952fc18eSTheodore Ts'o 		return -ENOMEM;
3686952fc18eSTheodore Ts'o 
3687952fc18eSTheodore Ts'o 	/*
3688952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3689952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3690952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3691952fc18eSTheodore Ts'o 	 */
3692952fc18eSTheodore Ts'o 
3693952fc18eSTheodore Ts'o 	/*
3694952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3695952fc18eSTheodore Ts'o 	 */
3696952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3697952fc18eSTheodore Ts'o 
3698952fc18eSTheodore Ts'o 	/*
3699952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3700952fc18eSTheodore Ts'o 	 */
3701952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3702952fc18eSTheodore Ts'o 		int blks;
3703952fc18eSTheodore Ts'o 
3704952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3705952fc18eSTheodore Ts'o 		overhead += blks;
3706952fc18eSTheodore Ts'o 		if (blks)
3707952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3708952fc18eSTheodore Ts'o 		cond_resched();
3709952fc18eSTheodore Ts'o 	}
37103c816dedSEric Whitney 
37113c816dedSEric Whitney 	/*
37123c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
37133c816dedSEric Whitney 	 * loaded or not
37143c816dedSEric Whitney 	 */
3715b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3716810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
3717f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3718f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
37193c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
37203c816dedSEric Whitney 		if (j_inode) {
37213c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
37223c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
37233c816dedSEric Whitney 			iput(j_inode);
37243c816dedSEric Whitney 		} else {
37253c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
37263c816dedSEric Whitney 		}
37273c816dedSEric Whitney 	}
3728952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3729952fc18eSTheodore Ts'o 	smp_wmb();
3730952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3731952fc18eSTheodore Ts'o 	return 0;
3732952fc18eSTheodore Ts'o }
3733952fc18eSTheodore Ts'o 
3734b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
373527dd4385SLukas Czerner {
373627dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3737b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
373827dd4385SLukas Czerner 
373927dd4385SLukas Czerner 	/*
374030fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
374130fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
374230fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
374330fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
374430fac0f7SJan Kara 	 */
3745e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3746b5799018STheodore Ts'o 		return;
374730fac0f7SJan Kara 	/*
374827dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
374927dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
375027dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3751556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
375227dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
375327dd4385SLukas Czerner 	 * very rare.
375427dd4385SLukas Czerner 	 */
3755b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3756b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
375727dd4385SLukas Czerner 
375827dd4385SLukas Czerner 	do_div(resv_clusters, 50);
375927dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
376027dd4385SLukas Czerner 
3761b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
376227dd4385SLukas Czerner }
376327dd4385SLukas Czerner 
3764617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3765ac27a0ecSDave Kleikamp {
37665e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3767d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
37681d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
3769617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
37705aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
37717c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
3772617ba13bSMingming Cao 	ext4_fsblk_t block;
3773617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
377470bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3775ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3776ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3777ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3778ac27a0ecSDave Kleikamp 	struct inode *root;
37790390131bSFrank Mayhar 	const char *descr;
3780dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3781281b5995STheodore Ts'o 	int blocksize, clustersize;
37824ec11028STheodore Ts'o 	unsigned int db_count;
37834ec11028STheodore Ts'o 	unsigned int i;
3784ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
3785bd81d8eeSLaurent Vivier 	__u64 blocks_count;
378607aa2ea1SLukas Czerner 	int err = 0;
3787b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3788bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3789ac27a0ecSDave Kleikamp 
37905aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
37915aee0f8aSTheodore Ts'o 		goto out_free_base;
3792705895b6SPekka Enberg 
3793aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3794705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3795705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
37965aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
37975aee0f8aSTheodore Ts'o 		goto out_free_base;
37985aee0f8aSTheodore Ts'o 
3799ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
38002c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3801240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3802d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3803f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3804f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3805dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3806ac27a0ecSDave Kleikamp 
38079f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3808ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
38099f6200bbSTheodore Ts'o 
381007aa2ea1SLukas Czerner 	/* -EINVAL is default */
3811dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3812617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3813ac27a0ecSDave Kleikamp 	if (!blocksize) {
3814b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3815ac27a0ecSDave Kleikamp 		goto out_fail;
3816ac27a0ecSDave Kleikamp 	}
3817ac27a0ecSDave Kleikamp 
3818ac27a0ecSDave Kleikamp 	/*
3819617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3820ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3821ac27a0ecSDave Kleikamp 	 */
3822617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
382370bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
382470bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3825ac27a0ecSDave Kleikamp 	} else {
382670bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3827ac27a0ecSDave Kleikamp 	}
3828ac27a0ecSDave Kleikamp 
3829a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3830b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3831ac27a0ecSDave Kleikamp 		goto out_fail;
3832ac27a0ecSDave Kleikamp 	}
3833ac27a0ecSDave Kleikamp 	/*
3834ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3835617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3836ac27a0ecSDave Kleikamp 	 */
38372716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3838ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3839ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3840617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3841617ba13bSMingming Cao 		goto cantfind_ext4;
3842afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3843ac27a0ecSDave Kleikamp 
3844feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3845e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3846e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3847363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3848feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3849feb0ab32SDarrick J. Wong 
3850d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3851d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3852d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3853d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3854d25425f8SDarrick J. Wong 		silent = 1;
3855d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3856d25425f8SDarrick J. Wong 	}
3857d25425f8SDarrick J. Wong 
38580441984aSDarrick J. Wong 	/* Load the checksum driver */
38590441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
38600441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
38610441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
38620441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
38630441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
38640441984aSDarrick J. Wong 		goto failed_mount;
38650441984aSDarrick J. Wong 	}
38660441984aSDarrick J. Wong 
3867a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3868a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3869a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3870a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3871a9c47317SDarrick J. Wong 		silent = 1;
38726a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3873a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3874a9c47317SDarrick J. Wong 	}
3875a9c47317SDarrick J. Wong 
3876a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3877e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
38788c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3879dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3880a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3881a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3882a9c47317SDarrick J. Wong 
3883ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3884ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3885fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3886617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3887fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
388887f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3889fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3890617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3891fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3892ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3893fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
389403010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3895fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
38962e7842b8SHugh Dickins #endif
389798c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
389898c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
389998c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
390098c1a759SDarrick J. Wong 
3901617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3902fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3903617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3904fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3905617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3906fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3907ac27a0ecSDave Kleikamp 
3908617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3909fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3910bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3911fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3912bb4f397aSAneesh Kumar K.V 	else
3913fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
391445f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3915fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
39168b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3917fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3918ac27a0ecSDave Kleikamp 
391908cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
392008cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
392130773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
392230773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
392330773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3924ac27a0ecSDave Kleikamp 
39258b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3926fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3927ac27a0ecSDave Kleikamp 
39281e2462f9SMingming Cao 	/*
3929dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3930dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3931dd919b98SAneesh Kumar K.V 	 */
3932bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
39338b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3934fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3935dd919b98SAneesh Kumar K.V 
393651ce6511SLukas Czerner 	/*
393751ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
393851ce6511SLukas Czerner 	 * no mount option specified.
393951ce6511SLukas Czerner 	 */
394051ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
394151ce6511SLukas Czerner 
39424f97a681STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3943626b035bSRitesh Harjani 
3944626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
3945626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
3946626b035bSRitesh Harjani 
39474f97a681STheodore Ts'o 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
39484f97a681STheodore Ts'o 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
39494f97a681STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
39504f97a681STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
39514f97a681STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
39524f97a681STheodore Ts'o 		goto failed_mount;
39534f97a681STheodore Ts'o 	}
39544f97a681STheodore Ts'o 
39559803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
39569803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
39579803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
39589803387cSTheodore Ts'o 	} else {
39599803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
39609803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
39619803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
39629803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
39639803387cSTheodore Ts'o 				 sbi->s_first_ino);
39649803387cSTheodore Ts'o 			goto failed_mount;
39659803387cSTheodore Ts'o 		}
39669803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
39679803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
39689803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
39699803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
39709803387cSTheodore Ts'o 			       "unsupported inode size: %d",
39719803387cSTheodore Ts'o 			       sbi->s_inode_size);
39724f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
39739803387cSTheodore Ts'o 			goto failed_mount;
39749803387cSTheodore Ts'o 		}
39759803387cSTheodore Ts'o 		/*
39769803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
39779803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
39789803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
39799803387cSTheodore Ts'o 		 * for all three.
39809803387cSTheodore Ts'o 		 */
39819803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
39829803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
39839803387cSTheodore Ts'o 			sb->s_time_gran = 1;
39849803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
39859803387cSTheodore Ts'o 		} else {
39869803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
39879803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
39889803387cSTheodore Ts'o 		}
39899803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
39909803387cSTheodore Ts'o 	}
39919803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
39929803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
39939803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
39949803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
39959803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
39969803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
39979803387cSTheodore Ts'o 
39989803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
39999803387cSTheodore Ts'o 			if (v > max) {
40009803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
40019803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
40029803387cSTheodore Ts'o 				goto failed_mount;
40039803387cSTheodore Ts'o 			}
40049803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
40059803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
40069803387cSTheodore Ts'o 
40079803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
40089803387cSTheodore Ts'o 			if (v > max) {
40099803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
40109803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
40119803387cSTheodore Ts'o 				goto failed_mount;
40129803387cSTheodore Ts'o 			}
40139803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
40149803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
40159803387cSTheodore Ts'o 		}
40169803387cSTheodore Ts'o 	}
40179803387cSTheodore Ts'o 
40185aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
40195aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
40205aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
40215aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
40225aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
40235aee0f8aSTheodore Ts'o 			goto failed_mount;
40245aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
40255aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
40268b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
40278b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
40285aee0f8aSTheodore Ts'o 				 s_mount_opts);
40295aee0f8aSTheodore Ts'o 		}
40305aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
40318b67f04aSTheodore Ts'o 	}
40325a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
4033b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
4034661aa520SEric Sandeen 			   &journal_ioprio, 0))
4035ac27a0ecSDave Kleikamp 		goto failed_mount;
4036ac27a0ecSDave Kleikamp 
4037c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4038c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
4039c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
4040c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
4041c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
4042c83ad55eSGabriel Krisman Bertazi 
4043c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
4044c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4045c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
4046c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4047c83ad55eSGabriel Krisman Bertazi 		}
4048c83ad55eSGabriel Krisman Bertazi 
4049c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
4050c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
4051c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4052c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
4053c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4054c83ad55eSGabriel Krisman Bertazi 		}
4055c83ad55eSGabriel Krisman Bertazi 
4056c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
4057c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
4058c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
4059c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
4060c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4061c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4062c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4063c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4064c83ad55eSGabriel Krisman Bertazi 		}
4065c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4066c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4067c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4068c83ad55eSGabriel Krisman Bertazi 
4069c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
4070c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
4071c83ad55eSGabriel Krisman Bertazi 	}
4072c83ad55eSGabriel Krisman Bertazi #endif
4073c83ad55eSGabriel Krisman Bertazi 
407456889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4075244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
4076781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
4077244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
407856889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
407956889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
408056889787STheodore Ts'o 				 "both data=journal and delalloc");
408156889787STheodore Ts'o 			goto failed_mount;
408256889787STheodore Ts'o 		}
4083fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4084923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4085923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4086923ae0ffSRoss Zwisler 			goto failed_mount;
4087923ae0ffSRoss Zwisler 		}
408873b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
408973b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
409073b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
409173b92a2aSSergey Karamov 				 "instead of data journaling mode");
409273b92a2aSSergey Karamov 		}
409356889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
409456889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4095001e4a87STejun Heo 	} else {
4096001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
409756889787STheodore Ts'o 	}
409856889787STheodore Ts'o 
40991751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
41001751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4101ac27a0ecSDave Kleikamp 
4102617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4103e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4104e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4105e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4106b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4107b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4108b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4109469108ffSTheodore Tso 
4110ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4111ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4112e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4113ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4114ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4115ed3654ebSTheodore Ts'o 			goto failed_mount;
4116ed3654ebSTheodore Ts'o 		}
4117dec214d0STahsin Erdogan 
4118dec214d0STahsin Erdogan 		/*
4119dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4120dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4121dec214d0STahsin Erdogan 		 */
4122dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4123dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4124dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4125dec214d0STahsin Erdogan 			goto failed_mount;
4126dec214d0STahsin Erdogan 		}
4127ed3654ebSTheodore Ts'o 	}
4128ed3654ebSTheodore Ts'o 
41292035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
41302035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
41312035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
41322035e776STheodore Ts'o 				 "using the ext4 subsystem");
41332035e776STheodore Ts'o 		else {
41340d9366d6SEric Sandeen 			/*
41350d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
41360d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
41370d9366d6SEric Sandeen 			 */
41380d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
41390d9366d6SEric Sandeen 				goto failed_mount;
41402035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
41412035e776STheodore Ts'o 				 "to feature incompatibilities");
41422035e776STheodore Ts'o 			goto failed_mount;
41432035e776STheodore Ts'o 		}
41442035e776STheodore Ts'o 	}
41452035e776STheodore Ts'o 
41462035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
41472035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
41482035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
41492035e776STheodore Ts'o 				 "using the ext4 subsystem");
41502035e776STheodore Ts'o 		else {
41510d9366d6SEric Sandeen 			/*
41520d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
41530d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
41540d9366d6SEric Sandeen 			 */
41550d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
41560d9366d6SEric Sandeen 				goto failed_mount;
41572035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
41582035e776STheodore Ts'o 				 "to feature incompatibilities");
41592035e776STheodore Ts'o 			goto failed_mount;
41602035e776STheodore Ts'o 		}
41612035e776STheodore Ts'o 	}
41622035e776STheodore Ts'o 
4163469108ffSTheodore Tso 	/*
4164ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4165ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4166ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4167ac27a0ecSDave Kleikamp 	 */
4168bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4169ac27a0ecSDave Kleikamp 		goto failed_mount;
4170a13fb1a4SEric Sandeen 
41718cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
41728cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
41738cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
41748cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
41758cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4176ac27a0ecSDave Kleikamp 		goto failed_mount;
4177ac27a0ecSDave Kleikamp 	}
4178bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4179bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4180bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4181bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4182bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4183bfe0a5f4STheodore Ts'o 		goto failed_mount;
4184bfe0a5f4STheodore Ts'o 	}
4185ac27a0ecSDave Kleikamp 
41865b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
41875b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
41885b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
41895b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
41905b9554dcSTheodore Ts'o 		goto failed_mount;
41915b9554dcSTheodore Ts'o 	}
41925b9554dcSTheodore Ts'o 
4193a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4194a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4195a8ab6d38SIra Weiny 
4196fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4197559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4198559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4199559db4c6SRoss Zwisler 					" that may contain inline data");
4200361d24d4SEric Sandeen 			goto failed_mount;
4201559db4c6SRoss Zwisler 		}
4202a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
420324f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4204361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4205361d24d4SEric Sandeen 			goto failed_mount;
420624f3478dSDan Williams 		}
4207923ae0ffSRoss Zwisler 	}
4208923ae0ffSRoss Zwisler 
4209e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
42106ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
42116ddb2447STheodore Ts'o 			 es->s_encryption_level);
42126ddb2447STheodore Ts'o 		goto failed_mount;
42136ddb2447STheodore Ts'o 	}
42146ddb2447STheodore Ts'o 
4215ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4216ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4217ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4218b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4219ce40733cSAneesh Kumar K.V 					blocksize);
4220ac27a0ecSDave Kleikamp 			goto failed_mount;
4221ac27a0ecSDave Kleikamp 		}
4222ac27a0ecSDave Kleikamp 
4223ac27a0ecSDave Kleikamp 		brelse(bh);
422470bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
422570bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4226a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4227ac27a0ecSDave Kleikamp 		if (!bh) {
4228b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4229b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4230ac27a0ecSDave Kleikamp 			goto failed_mount;
4231ac27a0ecSDave Kleikamp 		}
42322716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4233ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4234617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4235b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4236b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4237ac27a0ecSDave Kleikamp 			goto failed_mount;
4238ac27a0ecSDave Kleikamp 		}
4239ac27a0ecSDave Kleikamp 	}
4240ac27a0ecSDave Kleikamp 
4241e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4242f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4243f287a1a5STheodore Ts'o 						      has_huge_files);
4244f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4245ac27a0ecSDave Kleikamp 
42460d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4247e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
42488fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
42490d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4250d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4251b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4252b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
42530d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
42540d1ee42fSAlexandre Ratchov 			goto failed_mount;
42550d1ee42fSAlexandre Ratchov 		}
42560d1ee42fSAlexandre Ratchov 	} else
42570d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
42580b8e58a1SAndreas Dilger 
4259ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4260ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
42610b8e58a1SAndreas Dilger 
4262617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4263ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4264617ba13bSMingming Cao 		goto cantfind_ext4;
4265cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4266cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4267cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4268b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4269cd6bb35bSTheodore Ts'o 		goto failed_mount;
4270cd6bb35bSTheodore Ts'o 	}
4271ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4272ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
42730d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4274ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4275ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4276e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4277e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
42780b8e58a1SAndreas Dilger 
4279ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4280ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4281ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4282e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4283f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4284f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4285f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4286f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4287f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4288bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
428923301410STheodore Ts'o 				es->s_flags |=
429023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4291f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4292f99b2589STheodore Ts'o #else
4293bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
429423301410STheodore Ts'o 				es->s_flags |=
429523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4296f99b2589STheodore Ts'o #endif
4297f99b2589STheodore Ts'o 		}
429823301410STheodore Ts'o 	}
4299ac27a0ecSDave Kleikamp 
4300281b5995STheodore Ts'o 	/* Handle clustersize */
4301281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4302ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4303281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4304281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4305281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4306281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4307281b5995STheodore Ts'o 			goto failed_mount;
4308281b5995STheodore Ts'o 		}
4309281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4310281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4311281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4312281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4313281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4314281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4315281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4316281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4317281b5995STheodore Ts'o 			goto failed_mount;
4318281b5995STheodore Ts'o 		}
4319281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4320281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4321281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4322281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4323281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4324281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4325281b5995STheodore Ts'o 			goto failed_mount;
4326281b5995STheodore Ts'o 		}
4327281b5995STheodore Ts'o 	} else {
4328281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4329bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4330bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4331bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4332bfe0a5f4STheodore Ts'o 			goto failed_mount;
4333281b5995STheodore Ts'o 		}
4334ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4335b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4336b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4337ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4338ac27a0ecSDave Kleikamp 			goto failed_mount;
4339ac27a0ecSDave Kleikamp 		}
4340281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4341281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4342281b5995STheodore Ts'o 	}
4343281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4344281b5995STheodore Ts'o 
4345960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4346960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4347960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4348960fd856STheodore Ts'o 
4349bf43d84bSEric Sandeen 	/*
4350bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4351bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4352bf43d84bSEric Sandeen 	 */
43535a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
435430ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
43555a9ae68aSDarrick J. Wong 	if (err) {
4356b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4357bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4358ac27a0ecSDave Kleikamp 		goto failed_mount;
4359ac27a0ecSDave Kleikamp 	}
4360ac27a0ecSDave Kleikamp 
4361617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4362617ba13bSMingming Cao 		goto cantfind_ext4;
4363e7c95593SEric Sandeen 
43640f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
43650f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
43660f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4367b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4368b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
43690f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
43700f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
43710f2ddca6SFrom: Thiemo Nagel 	}
43720f2ddca6SFrom: Thiemo Nagel 
43734ec11028STheodore Ts'o 	/*
43744ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
43754ec11028STheodore Ts'o 	 * of the filesystem.
43764ec11028STheodore Ts'o 	 */
43774ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4378b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4379b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4380e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
43814ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4382e7c95593SEric Sandeen 		goto failed_mount;
4383e7c95593SEric Sandeen 	}
4384bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4385bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4386bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4387bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4388bfe0a5f4STheodore Ts'o 		goto failed_mount;
4389bfe0a5f4STheodore Ts'o 	}
4390bfe0a5f4STheodore Ts'o 
4391bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4392bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4393bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4394bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
43954ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4396df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
43974ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4398df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
43994ec11028STheodore Ts'o 		       ext4_blocks_count(es),
44004ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
44014ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
44024ec11028STheodore Ts'o 		goto failed_mount;
44034ec11028STheodore Ts'o 	}
4404bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4405fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4406fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
44079e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
44089e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
44099e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
44109e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
44119e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
44129e463084STheodore Ts'o 		ret = -EINVAL;
44139e463084STheodore Ts'o 		goto failed_mount;
44149e463084STheodore Ts'o 	}
4415617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4416617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
44173a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
44182ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
44193a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
44203a4b77cdSEryu Guan 				 "first meta block group too large: %u "
44213a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
44223a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
44233a4b77cdSEryu Guan 			goto failed_mount;
44243a4b77cdSEryu Guan 		}
44253a4b77cdSEryu Guan 	}
44261d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
44271d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4428f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
44291d0c3924STheodore Ts'o 					  GFP_KERNEL));
4430ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4431b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
44322cde417dSTheodore Ts'o 		ret = -ENOMEM;
4433ac27a0ecSDave Kleikamp 		goto failed_mount;
4434ac27a0ecSDave Kleikamp 	}
4435ac27a0ecSDave Kleikamp 
4436705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4437ac27a0ecSDave Kleikamp 
443885c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
443985c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
444085c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
4441d87f6392SRoman Gushchin 		sb_breadahead_unmovable(sb, block);
444285c8f176SAndrew Perepechko 	}
444385c8f176SAndrew Perepechko 
4444ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
44451d0c3924STheodore Ts'o 		struct buffer_head *bh;
44461d0c3924STheodore Ts'o 
444770bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
44481d0c3924STheodore Ts'o 		bh = sb_bread_unmovable(sb, block);
44491d0c3924STheodore Ts'o 		if (!bh) {
4450b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4451b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4452ac27a0ecSDave Kleikamp 			db_count = i;
4453ac27a0ecSDave Kleikamp 			goto failed_mount2;
4454ac27a0ecSDave Kleikamp 		}
44551d0c3924STheodore Ts'o 		rcu_read_lock();
44561d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
44571d0c3924STheodore Ts'o 		rcu_read_unlock();
4458ac27a0ecSDave Kleikamp 	}
445944de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4460829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4461b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
44626a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4463f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4464ac27a0ecSDave Kleikamp 	}
4465772cb7c8SJose R. Santos 
4466235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
446704496411STao Ma 
4468a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4469eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4470ce7e010aSTheodore Ts'o 		goto failed_mount3;
4471ce7e010aSTheodore Ts'o 
4472c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
447367a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4474c9de560dSAlex Tomas 
4475f9ae9cf5STheodore Ts'o 	/*
4476f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4477f9ae9cf5STheodore Ts'o 	 */
4478f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4479617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4480617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4481643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4482a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4483ffcc4182SEric Biggers #endif
4484c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4485c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4486c93d8f88SEric Biggers #endif
4487ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4488617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4489e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
44901fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4491262b4662SJan Kara 	else
4492262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4493689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4494ac27a0ecSDave Kleikamp #endif
449585787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4496f2fa2ffcSAneesh Kumar K.V 
4497ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
44983b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4499ac27a0ecSDave Kleikamp 
4500ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4501ac27a0ecSDave Kleikamp 
4502ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4503e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4504ac27a0ecSDave Kleikamp 
4505bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4506c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
450750460fe8SDarrick J. Wong 			goto failed_mount3a;
4508c5e06d10SJohann Lombardi 
4509ac27a0ecSDave Kleikamp 	/*
4510ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4511ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4512ac27a0ecSDave Kleikamp 	 */
4513e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
45144753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
45154753d8a2STheodore Ts'o 		if (err)
451650460fe8SDarrick J. Wong 			goto failed_mount3a;
4517bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4518e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4519b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4520b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4521744692dcSJiaying Zhang 		goto failed_mount_wq;
4522ac27a0ecSDave Kleikamp 	} else {
45231e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
45241e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
45251e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45261e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
45271e381f60SDmitry Monakhov 			goto failed_mount_wq;
45281e381f60SDmitry Monakhov 		}
45291e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
45301e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45311e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
45321e381f60SDmitry Monakhov 			goto failed_mount_wq;
45331e381f60SDmitry Monakhov 		}
45341e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
45351e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45361e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
45371e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
45381e381f60SDmitry Monakhov 			goto failed_mount_wq;
45391e381f60SDmitry Monakhov 		}
45401e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
45411e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
45421e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
45431e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
45441e381f60SDmitry Monakhov 			goto failed_mount_wq;
45451e381f60SDmitry Monakhov 		}
454650b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
45471e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4548fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
45490390131bSFrank Mayhar 		sbi->s_journal = NULL;
45500390131bSFrank Mayhar 		needs_recovery = 0;
45510390131bSFrank Mayhar 		goto no_journal;
4552ac27a0ecSDave Kleikamp 	}
4553ac27a0ecSDave Kleikamp 
4554e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4555eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4556eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4557b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4558744692dcSJiaying Zhang 		goto failed_mount_wq;
4559eb40a09cSJose R. Santos 	}
4560eb40a09cSJose R. Santos 
456125ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
456225ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
456325ed6e8aSDarrick J. Wong 			 "feature set");
456425ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4565d4da6c9cSLinus Torvalds 	}
4566818d276cSGirish Shilamkar 
4567ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4568ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4569ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4570ac27a0ecSDave Kleikamp 	case 0:
4571ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
457263f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
457363f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
457463f57933SAndrew Morton 		 */
4575dab291afSMingming Cao 		if (jbd2_journal_check_available_features
457627f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4577fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
457827f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
457927f394a7STyson Nottingham 		} else {
4580fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
458127f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
458227f394a7STyson Nottingham 		}
4583ac27a0ecSDave Kleikamp 		break;
4584ac27a0ecSDave Kleikamp 
4585617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4586617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4587dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4588dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4589b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4590b31e1552SEric Sandeen 			       "requested data journaling mode");
4591744692dcSJiaying Zhang 			goto failed_mount_wq;
4592ac27a0ecSDave Kleikamp 		}
4593ac27a0ecSDave Kleikamp 	default:
4594ac27a0ecSDave Kleikamp 		break;
4595ac27a0ecSDave Kleikamp 	}
4596ab04df78SJan Kara 
4597ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4598ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4599ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4600ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4601ab04df78SJan Kara 		goto failed_mount_wq;
4602ab04df78SJan Kara 	}
4603ab04df78SJan Kara 
4604b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4605ac27a0ecSDave Kleikamp 
460618aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
460718aadd47SBobi Jam 
4608ce7e010aSTheodore Ts'o no_journal:
4609cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
461047387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
461147387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4612cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4613cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
46149c191f70ST Makphaibulchoke 			goto failed_mount_wq;
46159c191f70ST Makphaibulchoke 		}
46169c191f70ST Makphaibulchoke 
4617dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4618dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4619dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4620dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4621dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4622dec214d0STahsin Erdogan 				goto failed_mount_wq;
4623dec214d0STahsin Erdogan 			}
4624dec214d0STahsin Erdogan 		}
4625cdb7ee4cSTahsin Erdogan 	}
4626dec214d0STahsin Erdogan 
4627c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4628c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4629c93d8f88SEric Biggers 		goto failed_mount_wq;
4630c93d8f88SEric Biggers 	}
4631c93d8f88SEric Biggers 
4632bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4633e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4634e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
46356ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
46366ddb2447STheodore Ts'o 	}
46376ddb2447STheodore Ts'o 
4638fd89d5f2STejun Heo 	/*
4639952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4640952fc18eSTheodore Ts'o 	 * superblock if present.
4641952fc18eSTheodore Ts'o 	 */
4642952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4643952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4644952fc18eSTheodore Ts'o 	else {
464507aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
464607aa2ea1SLukas Czerner 		if (err)
4647952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4648952fc18eSTheodore Ts'o 	}
4649952fc18eSTheodore Ts'o 
4650952fc18eSTheodore Ts'o 	/*
4651fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4652fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4653fd89d5f2STejun Heo 	 */
46542e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
46552e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
46562e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
46572e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
465807aa2ea1SLukas Czerner 		ret = -ENOMEM;
46592e8fa54eSJan Kara 		goto failed_mount4;
46602e8fa54eSJan Kara 	}
46612e8fa54eSJan Kara 
4662ac27a0ecSDave Kleikamp 	/*
4663dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4664ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4665ac27a0ecSDave Kleikamp 	 */
4666ac27a0ecSDave Kleikamp 
46678a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
46681d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4669b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
46701d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
467132a9bb57SManish Katiyar 		root = NULL;
4672ac27a0ecSDave Kleikamp 		goto failed_mount4;
4673ac27a0ecSDave Kleikamp 	}
4674ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4675b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
467694bf608aSAl Viro 		iput(root);
4677ac27a0ecSDave Kleikamp 		goto failed_mount4;
4678ac27a0ecSDave Kleikamp 	}
4679b886ee3eSGabriel Krisman Bertazi 
4680b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4681b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4682b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4683b886ee3eSGabriel Krisman Bertazi #endif
4684b886ee3eSGabriel Krisman Bertazi 
468548fde701SAl Viro 	sb->s_root = d_make_root(root);
46861d1fe1eeSDavid Howells 	if (!sb->s_root) {
4687b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
46881d1fe1eeSDavid Howells 		ret = -ENOMEM;
46891d1fe1eeSDavid Howells 		goto failed_mount4;
46901d1fe1eeSDavid Howells 	}
4691ac27a0ecSDave Kleikamp 
4692c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4693c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
46941751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4695c89128a0SJaegeuk Kim 		ret = 0;
4696c89128a0SJaegeuk Kim 	} else if (ret)
4697c89128a0SJaegeuk Kim 		goto failed_mount4a;
4698ef7f3835SKalpak Shah 
4699b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
470027dd4385SLukas Czerner 
47016fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
47026fd058f7STheodore Ts'o 	if (err) {
4703b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4704fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4705f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4706f9ae9cf5STheodore Ts'o 	}
4707f9ae9cf5STheodore Ts'o 
4708f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4709f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4710f9ae9cf5STheodore Ts'o 	if (err) {
4711f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4712f9ae9cf5STheodore Ts'o 			 err);
4713dcf2d804STao Ma 		goto failed_mount5;
4714c2774d84SAneesh Kumar K.V 	}
4715c2774d84SAneesh Kumar K.V 
4716d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4717d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4718d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
47194274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4720908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4721908c7f19STejun Heo 				  GFP_KERNEL);
4722d5e03cbbSTheodore Ts'o 	if (!err) {
4723d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4724d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
47254274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4726908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4727908c7f19STejun Heo 					  GFP_KERNEL);
4728d5e03cbbSTheodore Ts'o 	}
4729d5e03cbbSTheodore Ts'o 	if (!err)
4730d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4731908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4732d5e03cbbSTheodore Ts'o 	if (!err)
4733908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4734908c7f19STejun Heo 					  GFP_KERNEL);
4735c8585c6fSDaeho Jeong 	if (!err)
4736bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4737c8585c6fSDaeho Jeong 
4738d5e03cbbSTheodore Ts'o 	if (err) {
4739d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4740d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4741d5e03cbbSTheodore Ts'o 	}
4742d5e03cbbSTheodore Ts'o 
4743e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4744d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4745d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4746d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4747d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4748d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4749d5e03cbbSTheodore Ts'o 		}
4750d5e03cbbSTheodore Ts'o 
4751bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4752bfff6873SLukas Czerner 	if (err)
4753dcf2d804STao Ma 		goto failed_mount6;
4754bfff6873SLukas Czerner 
4755b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4756dcf2d804STao Ma 	if (err)
4757dcf2d804STao Ma 		goto failed_mount7;
47583197ebdbSTheodore Ts'o 
47599b2ff357SJan Kara #ifdef CONFIG_QUOTA
47609b2ff357SJan Kara 	/* Enable quota usage during mount. */
4761bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
47629b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
47639b2ff357SJan Kara 		if (err)
47649b2ff357SJan Kara 			goto failed_mount8;
47659b2ff357SJan Kara 	}
47669b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
47679b2ff357SJan Kara 
4768*bc71726cSzhangyi (F) 	/*
4769*bc71726cSzhangyi (F) 	 * Save the original bdev mapping's wb_err value which could be
4770*bc71726cSzhangyi (F) 	 * used to detect the metadata async write error.
4771*bc71726cSzhangyi (F) 	 */
4772*bc71726cSzhangyi (F) 	spin_lock_init(&sbi->s_bdev_wb_lock);
4773*bc71726cSzhangyi (F) 	if (!sb_rdonly(sb))
4774*bc71726cSzhangyi (F) 		errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
4775*bc71726cSzhangyi (F) 					 &sbi->s_bdev_wb_err);
4776*bc71726cSzhangyi (F) 	sb->s_bdev->bd_super = sb;
4777617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4778617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4779617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
47800390131bSFrank Mayhar 	if (needs_recovery) {
4781b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
478211215630SJan Kara 		err = ext4_mark_recovery_complete(sb, es);
478311215630SJan Kara 		if (err)
478411215630SJan Kara 			goto failed_mount8;
47850390131bSFrank Mayhar 	}
47860390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
47870390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
47880390131bSFrank Mayhar 			descr = " journalled data mode";
47890390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
47900390131bSFrank Mayhar 			descr = " ordered data mode";
47910390131bSFrank Mayhar 		else
47920390131bSFrank Mayhar 			descr = " writeback data mode";
47930390131bSFrank Mayhar 	} else
47940390131bSFrank Mayhar 		descr = "out journal";
47950390131bSFrank Mayhar 
479679add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
479779add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
479879add3a3SLukas Czerner 		if (!blk_queue_discard(q))
479979add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
480079add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
480179add3a3SLukas Czerner 				 "the device does not support discard");
480279add3a3SLukas Czerner 	}
480379add3a3SLukas Czerner 
4804e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4805d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
48065aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
48075aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
48085aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
48098b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4810ac27a0ecSDave Kleikamp 
481166e61a9eSTheodore Ts'o 	if (es->s_error_count)
481266e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4813ac27a0ecSDave Kleikamp 
4814efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4815efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4816efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4817efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4818efbed4dcSTheodore Ts'o 
4819d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4820ac27a0ecSDave Kleikamp 	return 0;
4821ac27a0ecSDave Kleikamp 
4822617ba13bSMingming Cao cantfind_ext4:
4823ac27a0ecSDave Kleikamp 	if (!silent)
4824b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4825ac27a0ecSDave Kleikamp 	goto failed_mount;
4826ac27a0ecSDave Kleikamp 
482772ba7450STheodore Ts'o failed_mount8:
4828ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
4829dcf2d804STao Ma failed_mount7:
4830dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4831dcf2d804STao Ma failed_mount6:
4832f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
48337c990728SSuraj Jitindar Singh 	rcu_read_lock();
48347c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
48357c990728SSuraj Jitindar Singh 	if (flex_groups) {
48367c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
48377c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
48387c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
48397c990728SSuraj Jitindar Singh 	}
48407c990728SSuraj Jitindar Singh 	rcu_read_unlock();
4841d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4842d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4843d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4844d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4845bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
484600764937SAzat Khuzhin failed_mount5:
4847f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4848f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4849f9ae9cf5STheodore Ts'o failed_mount4a:
485094bf608aSAl Viro 	dput(sb->s_root);
485132a9bb57SManish Katiyar 	sb->s_root = NULL;
485294bf608aSAl Viro failed_mount4:
4853b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
48542e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
48552e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
48564c0425ffSMingming Cao failed_mount_wq:
4857dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4858dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
485950c15df6SChengguang Xu 
486047387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
486147387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
486250c15df6SChengguang Xu 
48630390131bSFrank Mayhar 	if (sbi->s_journal) {
4864dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
486547b4a50bSJan Kara 		sbi->s_journal = NULL;
48660390131bSFrank Mayhar 	}
486750460fe8SDarrick J. Wong failed_mount3a:
4868d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4869eb68d0e2SZheng Liu failed_mount3:
48709105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4871c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4872c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4873ac27a0ecSDave Kleikamp failed_mount2:
48741d0c3924STheodore Ts'o 	rcu_read_lock();
48751d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
4876ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
48771d0c3924STheodore Ts'o 		brelse(group_desc[i]);
48781d0c3924STheodore Ts'o 	kvfree(group_desc);
48791d0c3924STheodore Ts'o 	rcu_read_unlock();
4880ac27a0ecSDave Kleikamp failed_mount:
48810441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
48820441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4883c83ad55eSGabriel Krisman Bertazi 
4884c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4885c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4886c83ad55eSGabriel Krisman Bertazi #endif
4887c83ad55eSGabriel Krisman Bertazi 
4888ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4889a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
48900ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4891ac27a0ecSDave Kleikamp #endif
4892ed318a6cSEric Biggers 	fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
4893617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4894ac27a0ecSDave Kleikamp 	brelse(bh);
4895ac27a0ecSDave Kleikamp out_fail:
4896ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4897f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
48985aee0f8aSTheodore Ts'o out_free_base:
4899ac27a0ecSDave Kleikamp 	kfree(sbi);
4900d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
49015e405595SDan Williams 	fs_put_dax(dax_dev);
490207aa2ea1SLukas Czerner 	return err ? err : ret;
4903ac27a0ecSDave Kleikamp }
4904ac27a0ecSDave Kleikamp 
4905ac27a0ecSDave Kleikamp /*
4906ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4907ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4908ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4909ac27a0ecSDave Kleikamp  */
4910617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4911ac27a0ecSDave Kleikamp {
4912617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4913ac27a0ecSDave Kleikamp 
4914ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
491530773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
491630773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4917ac27a0ecSDave Kleikamp 
4918a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4919ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4920dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4921ac27a0ecSDave Kleikamp 	else
4922dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
49235bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
49245bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
49255bf5683aSHidehiro Kawai 	else
49265bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4927a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4928ac27a0ecSDave Kleikamp }
4929ac27a0ecSDave Kleikamp 
4930c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4931ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4932ac27a0ecSDave Kleikamp {
4933ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4934ac27a0ecSDave Kleikamp 
4935c6cb7e77SEric Whitney 	/*
4936c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4937c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4938c6cb7e77SEric Whitney 	 * will try to delete it.
4939c6cb7e77SEric Whitney 	 */
49408a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
49411d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4942b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4943ac27a0ecSDave Kleikamp 		return NULL;
4944ac27a0ecSDave Kleikamp 	}
4945ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4946ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4947ac27a0ecSDave Kleikamp 		iput(journal_inode);
4948b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4949ac27a0ecSDave Kleikamp 		return NULL;
4950ac27a0ecSDave Kleikamp 	}
4951ac27a0ecSDave Kleikamp 
4952e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4953ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
49541d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4955b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4956ac27a0ecSDave Kleikamp 		iput(journal_inode);
4957ac27a0ecSDave Kleikamp 		return NULL;
4958ac27a0ecSDave Kleikamp 	}
4959c6cb7e77SEric Whitney 	return journal_inode;
4960c6cb7e77SEric Whitney }
4961c6cb7e77SEric Whitney 
4962c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4963c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4964c6cb7e77SEric Whitney {
4965c6cb7e77SEric Whitney 	struct inode *journal_inode;
4966c6cb7e77SEric Whitney 	journal_t *journal;
4967c6cb7e77SEric Whitney 
496811215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
496911215630SJan Kara 		return NULL;
4970c6cb7e77SEric Whitney 
4971c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4972c6cb7e77SEric Whitney 	if (!journal_inode)
4973c6cb7e77SEric Whitney 		return NULL;
4974ac27a0ecSDave Kleikamp 
4975dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4976ac27a0ecSDave Kleikamp 	if (!journal) {
4977b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4978ac27a0ecSDave Kleikamp 		iput(journal_inode);
4979ac27a0ecSDave Kleikamp 		return NULL;
4980ac27a0ecSDave Kleikamp 	}
4981ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4982617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4983ac27a0ecSDave Kleikamp 	return journal;
4984ac27a0ecSDave Kleikamp }
4985ac27a0ecSDave Kleikamp 
4986617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4987ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4988ac27a0ecSDave Kleikamp {
4989ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4990ac27a0ecSDave Kleikamp 	journal_t *journal;
4991617ba13bSMingming Cao 	ext4_fsblk_t start;
4992617ba13bSMingming Cao 	ext4_fsblk_t len;
4993ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4994617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4995ac27a0ecSDave Kleikamp 	unsigned long offset;
4996617ba13bSMingming Cao 	struct ext4_super_block *es;
4997ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4998ac27a0ecSDave Kleikamp 
499911215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
500011215630SJan Kara 		return NULL;
50010390131bSFrank Mayhar 
5002b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
5003ac27a0ecSDave Kleikamp 	if (bdev == NULL)
5004ac27a0ecSDave Kleikamp 		return NULL;
5005ac27a0ecSDave Kleikamp 
5006ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
5007e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
5008ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
5009b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
5010b31e1552SEric Sandeen 			"blocksize too small for journal device");
5011ac27a0ecSDave Kleikamp 		goto out_bdev;
5012ac27a0ecSDave Kleikamp 	}
5013ac27a0ecSDave Kleikamp 
5014617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5015617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5016ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
5017ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
5018b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5019b31e1552SEric Sandeen 		       "external journal");
5020ac27a0ecSDave Kleikamp 		goto out_bdev;
5021ac27a0ecSDave Kleikamp 	}
5022ac27a0ecSDave Kleikamp 
50232716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
5024617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
5025ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
5026617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
5027b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
5028b31e1552SEric Sandeen 					"bad superblock");
5029ac27a0ecSDave Kleikamp 		brelse(bh);
5030ac27a0ecSDave Kleikamp 		goto out_bdev;
5031ac27a0ecSDave Kleikamp 	}
5032ac27a0ecSDave Kleikamp 
5033df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
5034df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5035df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
5036df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
5037df4763beSDarrick J. Wong 				       "corrupt superblock");
5038df4763beSDarrick J. Wong 		brelse(bh);
5039df4763beSDarrick J. Wong 		goto out_bdev;
5040df4763beSDarrick J. Wong 	}
5041df4763beSDarrick J. Wong 
5042617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
5043b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
5044ac27a0ecSDave Kleikamp 		brelse(bh);
5045ac27a0ecSDave Kleikamp 		goto out_bdev;
5046ac27a0ecSDave Kleikamp 	}
5047ac27a0ecSDave Kleikamp 
5048bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
5049ac27a0ecSDave Kleikamp 	start = sb_block + 1;
5050ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
5051ac27a0ecSDave Kleikamp 
5052dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
5053ac27a0ecSDave Kleikamp 					start, len, blocksize);
5054ac27a0ecSDave Kleikamp 	if (!journal) {
5055b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
5056ac27a0ecSDave Kleikamp 		goto out_bdev;
5057ac27a0ecSDave Kleikamp 	}
5058ac27a0ecSDave Kleikamp 	journal->j_private = sb;
5059dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
5060ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
5061ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
5062b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
5063ac27a0ecSDave Kleikamp 		goto out_journal;
5064ac27a0ecSDave Kleikamp 	}
5065ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
5066b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
5067b31e1552SEric Sandeen 					"user (unsupported) - %d",
5068ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5069ac27a0ecSDave Kleikamp 		goto out_journal;
5070ac27a0ecSDave Kleikamp 	}
5071617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
5072617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5073ac27a0ecSDave Kleikamp 	return journal;
50740b8e58a1SAndreas Dilger 
5075ac27a0ecSDave Kleikamp out_journal:
5076dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5077ac27a0ecSDave Kleikamp out_bdev:
5078617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5079ac27a0ecSDave Kleikamp 	return NULL;
5080ac27a0ecSDave Kleikamp }
5081ac27a0ecSDave Kleikamp 
5082617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5083617ba13bSMingming Cao 			     struct ext4_super_block *es,
5084ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5085ac27a0ecSDave Kleikamp {
5086ac27a0ecSDave Kleikamp 	journal_t *journal;
5087ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5088ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5089ac27a0ecSDave Kleikamp 	int err = 0;
5090ac27a0ecSDave Kleikamp 	int really_read_only;
5091273108faSLukas Czerner 	int journal_dev_ro;
5092ac27a0ecSDave Kleikamp 
509311215630SJan Kara 	if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
509411215630SJan Kara 		return -EFSCORRUPTED;
50950390131bSFrank Mayhar 
5096ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5097ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5098b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5099b31e1552SEric Sandeen 			"numbers have changed");
5100ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5101ac27a0ecSDave Kleikamp 	} else
5102ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5103ac27a0ecSDave Kleikamp 
5104273108faSLukas Czerner 	if (journal_inum && journal_dev) {
5105273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5106273108faSLukas Czerner 			 "filesystem has both journal inode and journal device!");
5107273108faSLukas Czerner 		return -EINVAL;
5108273108faSLukas Czerner 	}
5109273108faSLukas Czerner 
5110273108faSLukas Czerner 	if (journal_inum) {
5111273108faSLukas Czerner 		journal = ext4_get_journal(sb, journal_inum);
5112273108faSLukas Czerner 		if (!journal)
5113273108faSLukas Czerner 			return -EINVAL;
5114273108faSLukas Czerner 	} else {
5115273108faSLukas Czerner 		journal = ext4_get_dev_journal(sb, journal_dev);
5116273108faSLukas Czerner 		if (!journal)
5117273108faSLukas Czerner 			return -EINVAL;
5118273108faSLukas Czerner 	}
5119273108faSLukas Czerner 
5120273108faSLukas Czerner 	journal_dev_ro = bdev_read_only(journal->j_dev);
5121273108faSLukas Czerner 	really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5122273108faSLukas Czerner 
5123273108faSLukas Czerner 	if (journal_dev_ro && !sb_rdonly(sb)) {
5124273108faSLukas Czerner 		ext4_msg(sb, KERN_ERR,
5125273108faSLukas Czerner 			 "journal device read-only, try mounting with '-o ro'");
5126273108faSLukas Czerner 		err = -EROFS;
5127273108faSLukas Czerner 		goto err_out;
5128273108faSLukas Czerner 	}
5129ac27a0ecSDave Kleikamp 
5130ac27a0ecSDave Kleikamp 	/*
5131ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5132ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5133ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5134ac27a0ecSDave Kleikamp 	 */
5135e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5136bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5137b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5138b31e1552SEric Sandeen 					"required on readonly filesystem");
5139ac27a0ecSDave Kleikamp 			if (really_read_only) {
5140b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5141d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5142d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5143273108faSLukas Czerner 				err = -EROFS;
5144273108faSLukas Czerner 				goto err_out;
5145ac27a0ecSDave Kleikamp 			}
5146b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5147b31e1552SEric Sandeen 			       "be enabled during recovery");
5148ac27a0ecSDave Kleikamp 		}
5149ac27a0ecSDave Kleikamp 	}
5150ac27a0ecSDave Kleikamp 
515190576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5152b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
51534776004fSTheodore Ts'o 
5154e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5155dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
51561c13d5c0STheodore Ts'o 	if (!err) {
51571c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
51581c13d5c0STheodore Ts'o 		if (save)
51591c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
51601c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5161dab291afSMingming Cao 		err = jbd2_journal_load(journal);
51621c13d5c0STheodore Ts'o 		if (save)
51631c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
51641c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
51651c13d5c0STheodore Ts'o 		kfree(save);
51661c13d5c0STheodore Ts'o 	}
5167ac27a0ecSDave Kleikamp 
5168ac27a0ecSDave Kleikamp 	if (err) {
5169b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5170273108faSLukas Czerner 		goto err_out;
5171ac27a0ecSDave Kleikamp 	}
5172ac27a0ecSDave Kleikamp 
5173617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
517411215630SJan Kara 	err = ext4_clear_journal_err(sb, es);
517511215630SJan Kara 	if (err) {
517611215630SJan Kara 		EXT4_SB(sb)->s_journal = NULL;
517711215630SJan Kara 		jbd2_journal_destroy(journal);
517811215630SJan Kara 		return err;
517911215630SJan Kara 	}
5180ac27a0ecSDave Kleikamp 
5181c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5182ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5183ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5184ac27a0ecSDave Kleikamp 
5185ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5186e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5187ac27a0ecSDave Kleikamp 	}
5188ac27a0ecSDave Kleikamp 
5189ac27a0ecSDave Kleikamp 	return 0;
5190273108faSLukas Czerner 
5191273108faSLukas Czerner err_out:
5192273108faSLukas Czerner 	jbd2_journal_destroy(journal);
5193273108faSLukas Czerner 	return err;
5194ac27a0ecSDave Kleikamp }
5195ac27a0ecSDave Kleikamp 
5196e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5197ac27a0ecSDave Kleikamp {
5198e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5199617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5200c4be0c1dSTakashi Sato 	int error = 0;
5201ac27a0ecSDave Kleikamp 
5202bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5203c4be0c1dSTakashi Sato 		return error;
5204a17712c8SJon Derrick 
5205a17712c8SJon Derrick 	/*
5206a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
5207a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
5208a17712c8SJon Derrick 	 */
5209a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
5210a17712c8SJon Derrick 		return error;
5211a17712c8SJon Derrick 
521271290b36STheodore Ts'o 	/*
521371290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
521471290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
521571290b36STheodore Ts'o 	 * write time when we are mounting the root file system
521671290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
521771290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
521871290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
521971290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
522071290b36STheodore Ts'o 	 * to complain and force a full file system check.
522171290b36STheodore Ts'o 	 */
52221751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
52236a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5224f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5225afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5226afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5227dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5228dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5229afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5230f613dfcbSTheodore Ts'o 	else
5231f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5232f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5233d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
523457042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
523557042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
523657042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5237d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
52387f93cff9STheodore Ts'o 		es->s_free_inodes_count =
52397f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
52405d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5241ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
524206db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
52431566a48aSTheodore Ts'o 	if (sync)
52444743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5245e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
52464743f839SPranay Kr. Srivastava 		/*
52474743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
52484743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
52494743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
52504743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
52514743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
52524743f839SPranay Kr. Srivastava 		 * write and hope for the best.
52534743f839SPranay Kr. Srivastava 		 */
52544743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
52554743f839SPranay Kr. Srivastava 		       "superblock detected");
52564743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
52574743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
52584743f839SPranay Kr. Srivastava 	}
5259ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5260914258bfSTheodore Ts'o 	if (sync) {
52611566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5262564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
526300473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5264c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5265b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5266b31e1552SEric Sandeen 			       "superblock");
5267914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5268914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5269914258bfSTheodore Ts'o 		}
5270914258bfSTheodore Ts'o 	}
5271c4be0c1dSTakashi Sato 	return error;
5272ac27a0ecSDave Kleikamp }
5273ac27a0ecSDave Kleikamp 
5274ac27a0ecSDave Kleikamp /*
5275ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5276ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5277ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5278ac27a0ecSDave Kleikamp  */
527911215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb,
5280617ba13bSMingming Cao 				       struct ext4_super_block *es)
5281ac27a0ecSDave Kleikamp {
528211215630SJan Kara 	int err;
5283617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5284ac27a0ecSDave Kleikamp 
5285e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
528611215630SJan Kara 		if (journal != NULL) {
528711215630SJan Kara 			ext4_error(sb, "Journal got removed while the fs was "
528811215630SJan Kara 				   "mounted!");
528911215630SJan Kara 			return -EFSCORRUPTED;
529011215630SJan Kara 		}
529111215630SJan Kara 		return 0;
52920390131bSFrank Mayhar 	}
5293dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
529411215630SJan Kara 	err = jbd2_journal_flush(journal);
529511215630SJan Kara 	if (err < 0)
52967ffe1ea8SHidehiro Kawai 		goto out;
52977ffe1ea8SHidehiro Kawai 
5298bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5299e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5300e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5301ac27a0ecSDave Kleikamp 	}
53027ffe1ea8SHidehiro Kawai out:
5303dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
530411215630SJan Kara 	return err;
5305ac27a0ecSDave Kleikamp }
5306ac27a0ecSDave Kleikamp 
5307ac27a0ecSDave Kleikamp /*
5308ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5309ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5310ac27a0ecSDave Kleikamp  * main filesystem now.
5311ac27a0ecSDave Kleikamp  */
531211215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb,
5313617ba13bSMingming Cao 				   struct ext4_super_block *es)
5314ac27a0ecSDave Kleikamp {
5315ac27a0ecSDave Kleikamp 	journal_t *journal;
5316ac27a0ecSDave Kleikamp 	int j_errno;
5317ac27a0ecSDave Kleikamp 	const char *errstr;
5318ac27a0ecSDave Kleikamp 
531911215630SJan Kara 	if (!ext4_has_feature_journal(sb)) {
532011215630SJan Kara 		ext4_error(sb, "Journal got removed while the fs was mounted!");
532111215630SJan Kara 		return -EFSCORRUPTED;
532211215630SJan Kara 	}
53230390131bSFrank Mayhar 
5324617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5325ac27a0ecSDave Kleikamp 
5326ac27a0ecSDave Kleikamp 	/*
5327ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5328617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5329ac27a0ecSDave Kleikamp 	 */
5330ac27a0ecSDave Kleikamp 
5331dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5332ac27a0ecSDave Kleikamp 	if (j_errno) {
5333ac27a0ecSDave Kleikamp 		char nbuf[16];
5334ac27a0ecSDave Kleikamp 
5335617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
533612062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5337ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
533812062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5339ac27a0ecSDave Kleikamp 
5340617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5341617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5342e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5343ac27a0ecSDave Kleikamp 
5344dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5345d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5346ac27a0ecSDave Kleikamp 	}
534711215630SJan Kara 	return 0;
5348ac27a0ecSDave Kleikamp }
5349ac27a0ecSDave Kleikamp 
5350ac27a0ecSDave Kleikamp /*
5351ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5352ac27a0ecSDave Kleikamp  * and wait on the commit.
5353ac27a0ecSDave Kleikamp  */
5354617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5355ac27a0ecSDave Kleikamp {
5356ac27a0ecSDave Kleikamp 	journal_t *journal;
5357ac27a0ecSDave Kleikamp 
5358bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5359ac27a0ecSDave Kleikamp 		return 0;
5360ac27a0ecSDave Kleikamp 
5361617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5362b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5363ac27a0ecSDave Kleikamp }
5364ac27a0ecSDave Kleikamp 
5365617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5366ac27a0ecSDave Kleikamp {
536714ce0cb4STheodore Ts'o 	int ret = 0;
53689eddacf9SJan Kara 	tid_t target;
536906a407f1SDmitry Monakhov 	bool needs_barrier = false;
53708d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5371ac27a0ecSDave Kleikamp 
537249598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
53730db1ff22STheodore Ts'o 		return 0;
53740db1ff22STheodore Ts'o 
53759bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
53762e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5377a1177825SJan Kara 	/*
5378a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5379a1177825SJan Kara 	 * no dirty dquots
5380a1177825SJan Kara 	 */
5381a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
538206a407f1SDmitry Monakhov 	/*
538306a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
538406a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
538506a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
538606a407f1SDmitry Monakhov 	 */
5387bda32530STheodore Ts'o 	if (sbi->s_journal) {
538806a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
538906a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
539006a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
539106a407f1SDmitry Monakhov 			needs_barrier = true;
539206a407f1SDmitry Monakhov 
53938d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5394ac27a0ecSDave Kleikamp 			if (wait)
5395bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5396bda32530STheodore Ts'o 							   target);
53970390131bSFrank Mayhar 		}
5398bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5399bda32530STheodore Ts'o 		needs_barrier = true;
540006a407f1SDmitry Monakhov 	if (needs_barrier) {
540106a407f1SDmitry Monakhov 		int err;
54029398554fSChristoph Hellwig 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
540306a407f1SDmitry Monakhov 		if (!ret)
540406a407f1SDmitry Monakhov 			ret = err;
540506a407f1SDmitry Monakhov 	}
540606a407f1SDmitry Monakhov 
540706a407f1SDmitry Monakhov 	return ret;
540806a407f1SDmitry Monakhov }
540906a407f1SDmitry Monakhov 
5410ac27a0ecSDave Kleikamp /*
5411ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5412ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5413be4f27d3SYongqiang Yang  *
5414be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
54158e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
54168e8ad8a5SJan Kara  * modifications.
5417ac27a0ecSDave Kleikamp  */
5418c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5419ac27a0ecSDave Kleikamp {
5420c4be0c1dSTakashi Sato 	int error = 0;
5421c4be0c1dSTakashi Sato 	journal_t *journal;
5422ac27a0ecSDave Kleikamp 
5423bc98a42cSDavid Howells 	if (sb_rdonly(sb))
54249ca92389STheodore Ts'o 		return 0;
54259ca92389STheodore Ts'o 
5426c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5427ac27a0ecSDave Kleikamp 
5428bb044576STheodore Ts'o 	if (journal) {
5429ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5430dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
54317ffe1ea8SHidehiro Kawai 
54327ffe1ea8SHidehiro Kawai 		/*
5433bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5434bb044576STheodore Ts'o 		 * flush the journal.
54357ffe1ea8SHidehiro Kawai 		 */
5436c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
54376b0310fbSEric Sandeen 		if (error < 0)
54386b0310fbSEric Sandeen 			goto out;
5439ac27a0ecSDave Kleikamp 
5440ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5441e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5442c642dc9eSEric Sandeen 	}
5443c642dc9eSEric Sandeen 
5444e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
54456b0310fbSEric Sandeen out:
5446bb044576STheodore Ts'o 	if (journal)
54478e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5448bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
54496b0310fbSEric Sandeen 	return error;
5450ac27a0ecSDave Kleikamp }
5451ac27a0ecSDave Kleikamp 
5452ac27a0ecSDave Kleikamp /*
5453ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5454ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5455ac27a0ecSDave Kleikamp  */
5456c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5457ac27a0ecSDave Kleikamp {
5458bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
54599ca92389STheodore Ts'o 		return 0;
54609ca92389STheodore Ts'o 
5461c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
54629ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5463e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5464c642dc9eSEric Sandeen 	}
5465c642dc9eSEric Sandeen 
5466e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5467c4be0c1dSTakashi Sato 	return 0;
5468ac27a0ecSDave Kleikamp }
5469ac27a0ecSDave Kleikamp 
5470673c6100STheodore Ts'o /*
5471673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5472673c6100STheodore Ts'o  */
5473673c6100STheodore Ts'o struct ext4_mount_options {
5474673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5475a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
547608cefc7aSEric W. Biederman 	kuid_t s_resuid;
547708cefc7aSEric W. Biederman 	kgid_t s_resgid;
5478673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5479673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5480673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5481673c6100STheodore Ts'o 	int s_jquota_fmt;
5482a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5483673c6100STheodore Ts'o #endif
5484673c6100STheodore Ts'o };
5485673c6100STheodore Ts'o 
5486617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5487ac27a0ecSDave Kleikamp {
5488617ba13bSMingming Cao 	struct ext4_super_block *es;
5489617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5490ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5491617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5492c79d967dSChristoph Hellwig 	int enable_quota = 0;
54938a266467STheodore Ts'o 	ext4_group_t g;
5494b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5495c5e06d10SJohann Lombardi 	int err = 0;
5496ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
549703dafb5fSChen Gang 	int i, j;
549833458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5499ac27a0ecSDave Kleikamp #endif
5500d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5501ac27a0ecSDave Kleikamp 
550221ac738eSChengguang Xu 	if (data && !orig_data)
550321ac738eSChengguang Xu 		return -ENOMEM;
550421ac738eSChengguang Xu 
5505ac27a0ecSDave Kleikamp 	/* Store the original options */
5506ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5507ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5508a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5509ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5510ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5511ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
551230773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
551330773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5514ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5515ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5516a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
551703dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
551833458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
551933458eabSTheodore Ts'o 
552033458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
552103dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
552203dafb5fSChen Gang 				for (j = 0; j < i; j++)
552303dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
55243e36a163SWei Yongjun 				kfree(orig_data);
552503dafb5fSChen Gang 				return -ENOMEM;
552603dafb5fSChen Gang 			}
552703dafb5fSChen Gang 		} else
552803dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5529ac27a0ecSDave Kleikamp #endif
5530b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5531b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5532ac27a0ecSDave Kleikamp 
5533661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5534ac27a0ecSDave Kleikamp 		err = -EINVAL;
5535ac27a0ecSDave Kleikamp 		goto restore_opts;
5536ac27a0ecSDave Kleikamp 	}
5537ac27a0ecSDave Kleikamp 
55386b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
55396b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
55406b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
55412d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
55422d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5543c6d3d56dSDarrick J. Wong 	}
5544c6d3d56dSDarrick J. Wong 
55456ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
55466ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
55476ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
55486ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
55496ae6514bSPiotr Sarna 			err = -EINVAL;
55506ae6514bSPiotr Sarna 			goto restore_opts;
55516ae6514bSPiotr Sarna 		}
55526ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
55536ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
55546ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
55556ae6514bSPiotr Sarna 			err = -EINVAL;
55566ae6514bSPiotr Sarna 			goto restore_opts;
55576ae6514bSPiotr Sarna 		}
5558ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5559ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5560ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5561ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5562ab04df78SJan Kara 			err = -EINVAL;
5563ab04df78SJan Kara 			goto restore_opts;
5564ab04df78SJan Kara 		}
5565923ae0ffSRoss Zwisler 	}
5566923ae0ffSRoss Zwisler 
5567cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5568cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5569cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5570cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5571cdb7ee4cSTahsin Erdogan 	}
5572cdb7ee4cSTahsin Erdogan 
55734ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
557454d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5575ac27a0ecSDave Kleikamp 
55761751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
55771751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5578ac27a0ecSDave Kleikamp 
5579ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5580ac27a0ecSDave Kleikamp 
5581b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5582617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5583b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5584b3881f74STheodore Ts'o 	}
5585ac27a0ecSDave Kleikamp 
55861751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
55871751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5588a2fd66d0STheodore Ts'o 
55891751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
55904ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5591ac27a0ecSDave Kleikamp 			err = -EROFS;
5592ac27a0ecSDave Kleikamp 			goto restore_opts;
5593ac27a0ecSDave Kleikamp 		}
5594ac27a0ecSDave Kleikamp 
55951751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
559638c03b34STheodore Ts'o 			err = sync_filesystem(sb);
559738c03b34STheodore Ts'o 			if (err < 0)
559838c03b34STheodore Ts'o 				goto restore_opts;
55990f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
56000f0dd62fSChristoph Hellwig 			if (err < 0)
5601c79d967dSChristoph Hellwig 				goto restore_opts;
5602c79d967dSChristoph Hellwig 
5603ac27a0ecSDave Kleikamp 			/*
5604ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5605ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5606ac27a0ecSDave Kleikamp 			 */
56071751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5608ac27a0ecSDave Kleikamp 
5609ac27a0ecSDave Kleikamp 			/*
5610ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5611ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5612ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5613ac27a0ecSDave Kleikamp 			 */
5614617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5615617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5616ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5617ac27a0ecSDave Kleikamp 
561811215630SJan Kara 			if (sbi->s_journal) {
561911215630SJan Kara 				/*
562011215630SJan Kara 				 * We let remount-ro finish even if marking fs
562111215630SJan Kara 				 * as clean failed...
562211215630SJan Kara 				 */
5623617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
562411215630SJan Kara 			}
56252dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
56262dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5627ac27a0ecSDave Kleikamp 		} else {
5628a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5629e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
56302cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5631ac27a0ecSDave Kleikamp 				err = -EROFS;
5632ac27a0ecSDave Kleikamp 				goto restore_opts;
5633ac27a0ecSDave Kleikamp 			}
5634ead6596bSEric Sandeen 			/*
56358a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
56360b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
56378a266467STheodore Ts'o 			 */
56388a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
56398a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
56408a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
56418a266467STheodore Ts'o 
5642feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5643b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5644b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5645e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
56468a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
56476a797d27SDarrick J. Wong 					err = -EFSBADCRC;
56488a266467STheodore Ts'o 					goto restore_opts;
56498a266467STheodore Ts'o 				}
56508a266467STheodore Ts'o 			}
56518a266467STheodore Ts'o 
56528a266467STheodore Ts'o 			/*
5653ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5654ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5655ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5656ead6596bSEric Sandeen 			 */
5657ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5658b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5659ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5660ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5661b31e1552SEric Sandeen 				       "umount/remount instead");
5662ead6596bSEric Sandeen 				err = -EINVAL;
5663ead6596bSEric Sandeen 				goto restore_opts;
5664ead6596bSEric Sandeen 			}
5665ead6596bSEric Sandeen 
5666ac27a0ecSDave Kleikamp 			/*
5667*bc71726cSzhangyi (F) 			 * Update the original bdev mapping's wb_err value
5668*bc71726cSzhangyi (F) 			 * which could be used to detect the metadata async
5669*bc71726cSzhangyi (F) 			 * write error.
5670*bc71726cSzhangyi (F) 			 */
5671*bc71726cSzhangyi (F) 			errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5672*bc71726cSzhangyi (F) 						 &sbi->s_bdev_wb_err);
5673*bc71726cSzhangyi (F) 
5674*bc71726cSzhangyi (F) 			/*
5675ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5676ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5677ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5678ac27a0ecSDave Kleikamp 			 * the partition.)
5679ac27a0ecSDave Kleikamp 			 */
568011215630SJan Kara 			if (sbi->s_journal) {
568111215630SJan Kara 				err = ext4_clear_journal_err(sb, es);
568211215630SJan Kara 				if (err)
568311215630SJan Kara 					goto restore_opts;
568411215630SJan Kara 			}
5685ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5686c89128a0SJaegeuk Kim 
5687c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5688c89128a0SJaegeuk Kim 			if (err)
5689c89128a0SJaegeuk Kim 				goto restore_opts;
5690c89128a0SJaegeuk Kim 
56911751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5692e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5693c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5694c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5695c5e06d10SJohann Lombardi 					err = -EROFS;
5696c5e06d10SJohann Lombardi 					goto restore_opts;
5697c5e06d10SJohann Lombardi 				}
5698c79d967dSChristoph Hellwig 			enable_quota = 1;
5699ac27a0ecSDave Kleikamp 		}
5700ac27a0ecSDave Kleikamp 	}
5701bfff6873SLukas Czerner 
5702bfff6873SLukas Czerner 	/*
5703bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5704bfff6873SLukas Czerner 	 * current settings
5705bfff6873SLukas Czerner 	 */
5706bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5707bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5708bfff6873SLukas Czerner 	else {
5709bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5710bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5711bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5712bfff6873SLukas Czerner 	}
5713bfff6873SLukas Czerner 
57146fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5715c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5716c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5717c89128a0SJaegeuk Kim 		if (err)
5718c89128a0SJaegeuk Kim 			goto restore_opts;
5719c89128a0SJaegeuk Kim 	}
57200390131bSFrank Mayhar 
5721ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5722ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5723a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5724ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
57257c319d32SAditya Kali 	if (enable_quota) {
57267c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
57270f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5728e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
57297c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
573007724f98STheodore Ts'o 			if (err)
57317c319d32SAditya Kali 				goto restore_opts;
57327c319d32SAditya Kali 		}
57337c319d32SAditya Kali 	}
57347c319d32SAditya Kali #endif
5735d4c402d9SCurt Wohlgemuth 
57361751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5737d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5738d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5739ac27a0ecSDave Kleikamp 	return 0;
57400b8e58a1SAndreas Dilger 
5741ac27a0ecSDave Kleikamp restore_opts:
5742ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5743ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5744a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5745ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5746ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5747ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
574830773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
574930773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5750ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5751ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5752a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
575333458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
575433458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5755ac27a0ecSDave Kleikamp 	}
575633458eabSTheodore Ts'o 	synchronize_rcu();
575733458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
575833458eabSTheodore Ts'o 		kfree(to_free[i]);
5759ac27a0ecSDave Kleikamp #endif
5760d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5761ac27a0ecSDave Kleikamp 	return err;
5762ac27a0ecSDave Kleikamp }
5763ac27a0ecSDave Kleikamp 
5764689c958cSLi Xi #ifdef CONFIG_QUOTA
5765689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5766689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5767689c958cSLi Xi {
5768689c958cSLi Xi 	struct kqid qid;
5769689c958cSLi Xi 	struct dquot *dquot;
5770689c958cSLi Xi 	u64 limit;
5771689c958cSLi Xi 	u64 curblock;
5772689c958cSLi Xi 
5773689c958cSLi Xi 	qid = make_kqid_projid(projid);
5774689c958cSLi Xi 	dquot = dqget(sb, qid);
5775689c958cSLi Xi 	if (IS_ERR(dquot))
5776689c958cSLi Xi 		return PTR_ERR(dquot);
57777b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5778689c958cSLi Xi 
5779a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
5780a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
578157c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
578257c32ea4SChengguang Xu 
5783689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5784f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5785f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5786689c958cSLi Xi 		buf->f_blocks = limit;
5787689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5788689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5789689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5790689c958cSLi Xi 	}
5791689c958cSLi Xi 
5792a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
5793a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
5794689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5795689c958cSLi Xi 		buf->f_files = limit;
5796689c958cSLi Xi 		buf->f_ffree =
5797689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5798689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5799689c958cSLi Xi 	}
5800689c958cSLi Xi 
58017b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5802689c958cSLi Xi 	dqput(dquot);
5803689c958cSLi Xi 	return 0;
5804689c958cSLi Xi }
5805689c958cSLi Xi #endif
5806689c958cSLi Xi 
5807617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5808ac27a0ecSDave Kleikamp {
5809ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5810617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5811617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
581227dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5813960cc398SPekka Enberg 	u64 fsid;
5814d02a9391SKazuya Mio 	s64 bfree;
581527dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5816ac27a0ecSDave Kleikamp 
5817952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5818952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5819ac27a0ecSDave Kleikamp 
5820617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5821ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5822b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
582357042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
582457042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5825d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
582657042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
582727dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
582827dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
582927dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5830ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5831ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
583252d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5833617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5834960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5835960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5836960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5837960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
58380b8e58a1SAndreas Dilger 
5839689c958cSLi Xi #ifdef CONFIG_QUOTA
5840689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5841689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5842689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5843689c958cSLi Xi #endif
5844ac27a0ecSDave Kleikamp 	return 0;
5845ac27a0ecSDave Kleikamp }
5846ac27a0ecSDave Kleikamp 
5847ac27a0ecSDave Kleikamp 
5848ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5849ac27a0ecSDave Kleikamp 
5850bc8230eeSJan Kara /*
5851bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5852bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5853bc8230eeSJan Kara  */
5854ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5855ac27a0ecSDave Kleikamp {
58564c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5857ac27a0ecSDave Kleikamp }
5858ac27a0ecSDave Kleikamp 
5859617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5860ac27a0ecSDave Kleikamp {
5861ac27a0ecSDave Kleikamp 	int ret, err;
5862ac27a0ecSDave Kleikamp 	handle_t *handle;
5863ac27a0ecSDave Kleikamp 	struct inode *inode;
5864ac27a0ecSDave Kleikamp 
5865ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
58669924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5867617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5868ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5869ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5870ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5871617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5872ac27a0ecSDave Kleikamp 	if (!ret)
5873ac27a0ecSDave Kleikamp 		ret = err;
5874ac27a0ecSDave Kleikamp 	return ret;
5875ac27a0ecSDave Kleikamp }
5876ac27a0ecSDave Kleikamp 
5877617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5878ac27a0ecSDave Kleikamp {
5879ac27a0ecSDave Kleikamp 	int ret, err;
5880ac27a0ecSDave Kleikamp 	handle_t *handle;
5881ac27a0ecSDave Kleikamp 
58829924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5883617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5884ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5885ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5886ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5887617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5888ac27a0ecSDave Kleikamp 	if (!ret)
5889ac27a0ecSDave Kleikamp 		ret = err;
5890ac27a0ecSDave Kleikamp 	return ret;
5891ac27a0ecSDave Kleikamp }
5892ac27a0ecSDave Kleikamp 
5893617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5894ac27a0ecSDave Kleikamp {
5895ac27a0ecSDave Kleikamp 	int ret, err;
5896ac27a0ecSDave Kleikamp 	handle_t *handle;
5897ac27a0ecSDave Kleikamp 
58989924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5899617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
59009c3013e9SJan Kara 	if (IS_ERR(handle)) {
59019c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
59029c3013e9SJan Kara 		dquot_release(dquot);
5903ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
59049c3013e9SJan Kara 	}
5905ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5906617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5907ac27a0ecSDave Kleikamp 	if (!ret)
5908ac27a0ecSDave Kleikamp 		ret = err;
5909ac27a0ecSDave Kleikamp 	return ret;
5910ac27a0ecSDave Kleikamp }
5911ac27a0ecSDave Kleikamp 
5912617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5913ac27a0ecSDave Kleikamp {
5914262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5915262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5916262b4662SJan Kara 
59172c8be6b2SJan Kara 	/* Are we journaling quotas? */
5918e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5919262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5920ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5921617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5922ac27a0ecSDave Kleikamp 	} else {
5923ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5924ac27a0ecSDave Kleikamp 	}
5925ac27a0ecSDave Kleikamp }
5926ac27a0ecSDave Kleikamp 
5927617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5928ac27a0ecSDave Kleikamp {
5929ac27a0ecSDave Kleikamp 	int ret, err;
5930ac27a0ecSDave Kleikamp 	handle_t *handle;
5931ac27a0ecSDave Kleikamp 
5932ac27a0ecSDave Kleikamp 	/* Data block + inode block */
59332b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5934ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5935ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5936ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5937617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5938ac27a0ecSDave Kleikamp 	if (!ret)
5939ac27a0ecSDave Kleikamp 		ret = err;
5940ac27a0ecSDave Kleikamp 	return ret;
5941ac27a0ecSDave Kleikamp }
5942ac27a0ecSDave Kleikamp 
5943ac27a0ecSDave Kleikamp /*
5944ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5945ac27a0ecSDave Kleikamp  * the quota file and such...
5946ac27a0ecSDave Kleikamp  */
5947617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5948ac27a0ecSDave Kleikamp {
594933458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5950617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5951ac27a0ecSDave Kleikamp }
5952ac27a0ecSDave Kleikamp 
5953daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5954daf647d2STheodore Ts'o {
5955daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5956daf647d2STheodore Ts'o 
5957daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5958daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5959daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5960daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5961daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5962daf647d2STheodore Ts'o 	 */
5963daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5964daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5965daf647d2STheodore Ts'o }
5966daf647d2STheodore Ts'o 
5967ac27a0ecSDave Kleikamp /*
5968ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5969ac27a0ecSDave Kleikamp  */
5970617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
59718c54ca9cSAl Viro 			 const struct path *path)
5972ac27a0ecSDave Kleikamp {
5973ac27a0ecSDave Kleikamp 	int err;
5974ac27a0ecSDave Kleikamp 
5975ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5976ac27a0ecSDave Kleikamp 		return -EINVAL;
59770623543bSJan Kara 
5978ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5979d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5980ac27a0ecSDave Kleikamp 		return -EXDEV;
59810623543bSJan Kara 	/* Journaling quota? */
59820623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
59832b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5984f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5985b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5986b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5987b31e1552SEric Sandeen 				"Journaled quota will not work");
598891389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
598991389240SJan Kara 	} else {
599091389240SJan Kara 		/*
599191389240SJan Kara 		 * Clear the flag just in case mount options changed since
599291389240SJan Kara 		 * last time.
599391389240SJan Kara 		 */
599491389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
59950623543bSJan Kara 	}
59960623543bSJan Kara 
59970623543bSJan Kara 	/*
59980623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
59990623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
60000623543bSJan Kara 	 */
60010390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
60022b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
60030623543bSJan Kara 		/*
60040623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
60050623543bSJan Kara 		 * otherwise be livelocked...
60060623543bSJan Kara 		 */
60070623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
60087ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
60090623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
6010f00c9e44SJan Kara 		if (err)
60117ffe1ea8SHidehiro Kawai 			return err;
60127ffe1ea8SHidehiro Kawai 	}
6013957153fcSJan Kara 
6014daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6015daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
6016957153fcSJan Kara 	if (err) {
6017daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
6018daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
6019957153fcSJan Kara 	} else {
6020957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
6021957153fcSJan Kara 		handle_t *handle;
6022957153fcSJan Kara 
602361a92987SJan Kara 		/*
602461a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
602561a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
602661a92987SJan Kara 		 * are already enabled and this is not a hard failure.
602761a92987SJan Kara 		 */
6028957153fcSJan Kara 		inode_lock(inode);
6029957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6030957153fcSJan Kara 		if (IS_ERR(handle))
6031957153fcSJan Kara 			goto unlock_inode;
6032957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6033957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6034957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
60354209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
6036957153fcSJan Kara 		ext4_journal_stop(handle);
6037957153fcSJan Kara 	unlock_inode:
6038957153fcSJan Kara 		inode_unlock(inode);
6039957153fcSJan Kara 	}
6040daf647d2STheodore Ts'o 	return err;
6041ac27a0ecSDave Kleikamp }
6042ac27a0ecSDave Kleikamp 
60437c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
60447c319d32SAditya Kali 			     unsigned int flags)
60457c319d32SAditya Kali {
60467c319d32SAditya Kali 	int err;
60477c319d32SAditya Kali 	struct inode *qf_inode;
6048a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
60497c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6050689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6051689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
60527c319d32SAditya Kali 	};
60537c319d32SAditya Kali 
6054e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
60557c319d32SAditya Kali 
60567c319d32SAditya Kali 	if (!qf_inums[type])
60577c319d32SAditya Kali 		return -EPERM;
60587c319d32SAditya Kali 
60598a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
60607c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
60617c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
60627c319d32SAditya Kali 		return PTR_ERR(qf_inode);
60637c319d32SAditya Kali 	}
60647c319d32SAditya Kali 
6065bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
6066bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
6067daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
60687212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
6069daf647d2STheodore Ts'o 	if (err)
6070daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
607161157b24SPan Bian 	iput(qf_inode);
60727c319d32SAditya Kali 
60737c319d32SAditya Kali 	return err;
60747c319d32SAditya Kali }
60757c319d32SAditya Kali 
60767c319d32SAditya Kali /* Enable usage tracking for all quota types. */
60777c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
60787c319d32SAditya Kali {
60797c319d32SAditya Kali 	int type, err = 0;
6080a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
60817c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
6082689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6083689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
60847c319d32SAditya Kali 	};
608549da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
608649da9392SJan Kara 		test_opt(sb, USRQUOTA),
608749da9392SJan Kara 		test_opt(sb, GRPQUOTA),
608849da9392SJan Kara 		test_opt(sb, PRJQUOTA),
608949da9392SJan Kara 	};
60907c319d32SAditya Kali 
609191389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
6092a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
60937c319d32SAditya Kali 		if (qf_inums[type]) {
60947c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
609549da9392SJan Kara 				DQUOT_USAGE_ENABLED |
609649da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
60977c319d32SAditya Kali 			if (err) {
60987c319d32SAditya Kali 				ext4_warning(sb,
609972ba7450STheodore Ts'o 					"Failed to enable quota tracking "
610072ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
610172ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
61027f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
61037f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
61047f144fd0SJunichi Uekawa 
61057c319d32SAditya Kali 				return err;
61067c319d32SAditya Kali 			}
61077c319d32SAditya Kali 		}
61087c319d32SAditya Kali 	}
61097c319d32SAditya Kali 	return 0;
61107c319d32SAditya Kali }
61117c319d32SAditya Kali 
6112ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6113ca0e05e4SDmitry Monakhov {
611421f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
611521f97697SJan Kara 	handle_t *handle;
6116957153fcSJan Kara 	int err;
611721f97697SJan Kara 
611887009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
611987009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
612087009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6121ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6122ca0e05e4SDmitry Monakhov 
6123957153fcSJan Kara 	if (!inode || !igrab(inode))
61240b268590SAmir Goldstein 		goto out;
61250b268590SAmir Goldstein 
6126957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6127964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6128957153fcSJan Kara 		goto out_put;
6129957153fcSJan Kara 
6130957153fcSJan Kara 	inode_lock(inode);
613161a92987SJan Kara 	/*
613261a92987SJan Kara 	 * Update modification times of quota files when userspace can
613361a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
613461a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
613561a92987SJan Kara 	 */
61369924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
61374209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
61384209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
6139957153fcSJan Kara 		goto out_unlock;
61404209ae12SHarshad Shirwadkar 	}
6141957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6142957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6143eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
61444209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
614521f97697SJan Kara 	ext4_journal_stop(handle);
6146957153fcSJan Kara out_unlock:
6147957153fcSJan Kara 	inode_unlock(inode);
6148957153fcSJan Kara out_put:
6149964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6150957153fcSJan Kara 	iput(inode);
6151957153fcSJan Kara 	return err;
615221f97697SJan Kara out:
6153ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6154ca0e05e4SDmitry Monakhov }
6155ca0e05e4SDmitry Monakhov 
6156ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6157ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6158ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6159ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6160617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6161ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6162ac27a0ecSDave Kleikamp {
6163ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6164725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6165ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6166ac27a0ecSDave Kleikamp 	int tocopy;
6167ac27a0ecSDave Kleikamp 	size_t toread;
6168ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6169ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6170ac27a0ecSDave Kleikamp 
6171ac27a0ecSDave Kleikamp 	if (off > i_size)
6172ac27a0ecSDave Kleikamp 		return 0;
6173ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6174ac27a0ecSDave Kleikamp 		len = i_size-off;
6175ac27a0ecSDave Kleikamp 	toread = len;
6176ac27a0ecSDave Kleikamp 	while (toread > 0) {
6177ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6178ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
61791c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
61801c215028STheodore Ts'o 		if (IS_ERR(bh))
61811c215028STheodore Ts'o 			return PTR_ERR(bh);
6182ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6183ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6184ac27a0ecSDave Kleikamp 		else
6185ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6186ac27a0ecSDave Kleikamp 		brelse(bh);
6187ac27a0ecSDave Kleikamp 		offset = 0;
6188ac27a0ecSDave Kleikamp 		toread -= tocopy;
6189ac27a0ecSDave Kleikamp 		data += tocopy;
6190ac27a0ecSDave Kleikamp 		blk++;
6191ac27a0ecSDave Kleikamp 	}
6192ac27a0ecSDave Kleikamp 	return len;
6193ac27a0ecSDave Kleikamp }
6194ac27a0ecSDave Kleikamp 
6195ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6196ac27a0ecSDave Kleikamp  * enough credits) */
6197617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6198ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6199ac27a0ecSDave Kleikamp {
6200ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6201725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
62024209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6203c5e298aeSTheodore Ts'o 	int retries = 0;
6204ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6205ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6206ac27a0ecSDave Kleikamp 
62070390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6208b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6209b31e1552SEric Sandeen 			" cancelled because transaction is not started",
62109c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
62119c3013e9SJan Kara 		return -EIO;
62129c3013e9SJan Kara 	}
621367eeb568SDmitry Monakhov 	/*
621467eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
621567eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
621667eeb568SDmitry Monakhov 	 */
621767eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
621867eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
621967eeb568SDmitry Monakhov 			" cancelled because not block aligned",
622067eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
622167eeb568SDmitry Monakhov 		return -EIO;
622267eeb568SDmitry Monakhov 	}
622367eeb568SDmitry Monakhov 
6224c5e298aeSTheodore Ts'o 	do {
6225c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6226c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6227c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
622845586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6229c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
62301c215028STheodore Ts'o 	if (IS_ERR(bh))
62311c215028STheodore Ts'o 		return PTR_ERR(bh);
6232ac27a0ecSDave Kleikamp 	if (!bh)
6233ac27a0ecSDave Kleikamp 		goto out;
62345d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6235617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6236ac27a0ecSDave Kleikamp 	if (err) {
6237ac27a0ecSDave Kleikamp 		brelse(bh);
62381c215028STheodore Ts'o 		return err;
6239ac27a0ecSDave Kleikamp 	}
6240ac27a0ecSDave Kleikamp 	lock_buffer(bh);
624167eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6242ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6243ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
62440390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6245ac27a0ecSDave Kleikamp 	brelse(bh);
6246ac27a0ecSDave Kleikamp out:
624767eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
624867eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6249617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
62504209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
62514209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
62524209ae12SHarshad Shirwadkar 			err = err2;
625321f97697SJan Kara 	}
62544209ae12SHarshad Shirwadkar 	return err ? err : len;
6255ac27a0ecSDave Kleikamp }
6256ac27a0ecSDave Kleikamp #endif
6257ac27a0ecSDave Kleikamp 
6258152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6259152a0836SAl Viro 		       const char *dev_name, void *data)
6260ac27a0ecSDave Kleikamp {
6261152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6262ac27a0ecSDave Kleikamp }
6263ac27a0ecSDave Kleikamp 
6264c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
626524b58424STheodore Ts'o static inline void register_as_ext2(void)
626624b58424STheodore Ts'o {
626724b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
626824b58424STheodore Ts'o 	if (err)
626924b58424STheodore Ts'o 		printk(KERN_WARNING
627024b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
627124b58424STheodore Ts'o }
627224b58424STheodore Ts'o 
627324b58424STheodore Ts'o static inline void unregister_as_ext2(void)
627424b58424STheodore Ts'o {
627524b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
627624b58424STheodore Ts'o }
62772035e776STheodore Ts'o 
62782035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
62792035e776STheodore Ts'o {
6280e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
62812035e776STheodore Ts'o 		return 0;
6282bc98a42cSDavid Howells 	if (sb_rdonly(sb))
62832035e776STheodore Ts'o 		return 1;
6284e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
62852035e776STheodore Ts'o 		return 0;
62862035e776STheodore Ts'o 	return 1;
62872035e776STheodore Ts'o }
628824b58424STheodore Ts'o #else
628924b58424STheodore Ts'o static inline void register_as_ext2(void) { }
629024b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
62912035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
629224b58424STheodore Ts'o #endif
629324b58424STheodore Ts'o 
629424b58424STheodore Ts'o static inline void register_as_ext3(void)
629524b58424STheodore Ts'o {
629624b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
629724b58424STheodore Ts'o 	if (err)
629824b58424STheodore Ts'o 		printk(KERN_WARNING
629924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
630024b58424STheodore Ts'o }
630124b58424STheodore Ts'o 
630224b58424STheodore Ts'o static inline void unregister_as_ext3(void)
630324b58424STheodore Ts'o {
630424b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
630524b58424STheodore Ts'o }
63062035e776STheodore Ts'o 
63072035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
63082035e776STheodore Ts'o {
6309e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
63102035e776STheodore Ts'o 		return 0;
6311e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
63122035e776STheodore Ts'o 		return 0;
6313bc98a42cSDavid Howells 	if (sb_rdonly(sb))
63142035e776STheodore Ts'o 		return 1;
6315e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
63162035e776STheodore Ts'o 		return 0;
63172035e776STheodore Ts'o 	return 1;
63182035e776STheodore Ts'o }
631924b58424STheodore Ts'o 
632003010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6321ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
632203010a33STheodore Ts'o 	.name		= "ext4",
6323152a0836SAl Viro 	.mount		= ext4_mount,
6324ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6325ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6326ac27a0ecSDave Kleikamp };
63277f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6328ac27a0ecSDave Kleikamp 
6329e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6330e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6331e9e3bcecSEric Sandeen 
63325dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6333ac27a0ecSDave Kleikamp {
6334e9e3bcecSEric Sandeen 	int i, err;
6335c9de560dSAlex Tomas 
6336e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
633707c0c5d8SAl Viro 	ext4_li_info = NULL;
633807c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
633907c0c5d8SAl Viro 
63409a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
634112e9b892SDmitry Monakhov 	ext4_check_flag_values();
6342e9e3bcecSEric Sandeen 
6343e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6344e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6345e9e3bcecSEric Sandeen 
634651865fdaSZheng Liu 	err = ext4_init_es();
63476fd058f7STheodore Ts'o 	if (err)
63486fd058f7STheodore Ts'o 		return err;
634951865fdaSZheng Liu 
63501dc0aa46SEric Whitney 	err = ext4_init_pending();
63511dc0aa46SEric Whitney 	if (err)
635222cfe4b4SEric Biggers 		goto out7;
635322cfe4b4SEric Biggers 
635422cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
635522cfe4b4SEric Biggers 	if (err)
63561dc0aa46SEric Whitney 		goto out6;
63571dc0aa46SEric Whitney 
635851865fdaSZheng Liu 	err = ext4_init_pageio();
635951865fdaSZheng Liu 	if (err)
6360b5799018STheodore Ts'o 		goto out5;
636151865fdaSZheng Liu 
63625dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6363bd2d0210STheodore Ts'o 	if (err)
6364b5799018STheodore Ts'o 		goto out4;
6365857ac889SLukas Czerner 
6366b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6367dd68314cSTheodore Ts'o 	if (err)
6368b5799018STheodore Ts'o 		goto out3;
6369857ac889SLukas Czerner 
63705dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6371ac27a0ecSDave Kleikamp 	if (err)
6372c9de560dSAlex Tomas 		goto out2;
6373ac27a0ecSDave Kleikamp 	err = init_inodecache();
6374ac27a0ecSDave Kleikamp 	if (err)
6375ac27a0ecSDave Kleikamp 		goto out1;
637624b58424STheodore Ts'o 	register_as_ext3();
63772035e776STheodore Ts'o 	register_as_ext2();
637803010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6379ac27a0ecSDave Kleikamp 	if (err)
6380ac27a0ecSDave Kleikamp 		goto out;
6381bfff6873SLukas Czerner 
6382ac27a0ecSDave Kleikamp 	return 0;
6383ac27a0ecSDave Kleikamp out:
638424b58424STheodore Ts'o 	unregister_as_ext2();
638524b58424STheodore Ts'o 	unregister_as_ext3();
6386ac27a0ecSDave Kleikamp 	destroy_inodecache();
6387ac27a0ecSDave Kleikamp out1:
63885dabfc78STheodore Ts'o 	ext4_exit_mballoc();
63899c191f70ST Makphaibulchoke out2:
6390b5799018STheodore Ts'o 	ext4_exit_sysfs();
6391b5799018STheodore Ts'o out3:
6392dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6393b5799018STheodore Ts'o out4:
63945dabfc78STheodore Ts'o 	ext4_exit_pageio();
6395b5799018STheodore Ts'o out5:
639622cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
63971dc0aa46SEric Whitney out6:
639822cfe4b4SEric Biggers 	ext4_exit_pending();
639922cfe4b4SEric Biggers out7:
640051865fdaSZheng Liu 	ext4_exit_es();
640151865fdaSZheng Liu 
6402ac27a0ecSDave Kleikamp 	return err;
6403ac27a0ecSDave Kleikamp }
6404ac27a0ecSDave Kleikamp 
64055dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6406ac27a0ecSDave Kleikamp {
6407bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
640824b58424STheodore Ts'o 	unregister_as_ext2();
640924b58424STheodore Ts'o 	unregister_as_ext3();
641003010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6411ac27a0ecSDave Kleikamp 	destroy_inodecache();
64125dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6413b5799018STheodore Ts'o 	ext4_exit_sysfs();
64145dabfc78STheodore Ts'o 	ext4_exit_system_zone();
64155dabfc78STheodore Ts'o 	ext4_exit_pageio();
641622cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6417dd12ed14SEric Sandeen 	ext4_exit_es();
64181dc0aa46SEric Whitney 	ext4_exit_pending();
6419ac27a0ecSDave Kleikamp }
6420ac27a0ecSDave Kleikamp 
6421ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
642283982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6423ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
64247ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
64255dabfc78STheodore Ts'o module_init(ext4_init_fs)
64265dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6427