xref: /linux/fs/ext4/super.c (revision 4209ae12b12265d475bba28634184423149bd14f)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
2ac27a0ecSDave Kleikamp /*
3617ba13bSMingming Cao  *  linux/fs/ext4/super.c
4ac27a0ecSDave Kleikamp  *
5ac27a0ecSDave Kleikamp  * Copyright (C) 1992, 1993, 1994, 1995
6ac27a0ecSDave Kleikamp  * Remy Card (card@masi.ibp.fr)
7ac27a0ecSDave Kleikamp  * Laboratoire MASI - Institut Blaise Pascal
8ac27a0ecSDave Kleikamp  * Universite Pierre et Marie Curie (Paris VI)
9ac27a0ecSDave Kleikamp  *
10ac27a0ecSDave Kleikamp  *  from
11ac27a0ecSDave Kleikamp  *
12ac27a0ecSDave Kleikamp  *  linux/fs/minix/inode.c
13ac27a0ecSDave Kleikamp  *
14ac27a0ecSDave Kleikamp  *  Copyright (C) 1991, 1992  Linus Torvalds
15ac27a0ecSDave Kleikamp  *
16ac27a0ecSDave Kleikamp  *  Big-endian to little-endian byte-swapping/bitmaps by
17ac27a0ecSDave Kleikamp  *        David S. Miller (davem@caip.rutgers.edu), 1995
18ac27a0ecSDave Kleikamp  */
19ac27a0ecSDave Kleikamp 
20ac27a0ecSDave Kleikamp #include <linux/module.h>
21ac27a0ecSDave Kleikamp #include <linux/string.h>
22ac27a0ecSDave Kleikamp #include <linux/fs.h>
23ac27a0ecSDave Kleikamp #include <linux/time.h>
24c5ca7c76STheodore Ts'o #include <linux/vmalloc.h>
25ac27a0ecSDave Kleikamp #include <linux/slab.h>
26ac27a0ecSDave Kleikamp #include <linux/init.h>
27ac27a0ecSDave Kleikamp #include <linux/blkdev.h>
2866114cadSTejun Heo #include <linux/backing-dev.h>
29ac27a0ecSDave Kleikamp #include <linux/parser.h>
30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h>
31a5694255SChristoph Hellwig #include <linux/exportfs.h>
32ac27a0ecSDave Kleikamp #include <linux/vfs.h>
33ac27a0ecSDave Kleikamp #include <linux/random.h>
34ac27a0ecSDave Kleikamp #include <linux/mount.h>
35ac27a0ecSDave Kleikamp #include <linux/namei.h>
36ac27a0ecSDave Kleikamp #include <linux/quotaops.h>
37ac27a0ecSDave Kleikamp #include <linux/seq_file.h>
383197ebdbSTheodore Ts'o #include <linux/ctype.h>
391330593eSVignesh Babu #include <linux/log2.h>
40717d50e4SAndreas Dilger #include <linux/crc16.h>
41ef510424SDan Williams #include <linux/dax.h>
427abc52c2SDan Magenheimer #include <linux/cleancache.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
44ee73f9a5SJeff Layton #include <linux/iversion.h>
45c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h>
46c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
47bfff6873SLukas Czerner #include <linux/kthread.h>
48bfff6873SLukas Czerner #include <linux/freezer.h>
49bfff6873SLukas Czerner 
503dcf5451SChristoph Hellwig #include "ext4.h"
514a092d73STheodore Ts'o #include "ext4_extents.h"	/* Needed for trace points definition */
523dcf5451SChristoph Hellwig #include "ext4_jbd2.h"
53ac27a0ecSDave Kleikamp #include "xattr.h"
54ac27a0ecSDave Kleikamp #include "acl.h"
553661d286STheodore Ts'o #include "mballoc.h"
560c9ec4beSDarrick J. Wong #include "fsmap.h"
57ac27a0ecSDave Kleikamp 
589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS
599bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
609bffad1eSTheodore Ts'o 
610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info;
620b75a840SLukas Czerner static struct mutex ext4_li_mtx;
63e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit;
649f6200bbSTheodore Ts'o 
65617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
66ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum);
672adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root);
68e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync);
69617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
70617ba13bSMingming Cao 					struct ext4_super_block *es);
71617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
72617ba13bSMingming Cao 				   struct ext4_super_block *es);
73617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait);
74617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data);
75617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
76c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb);
77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb);
78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79152a0836SAl Viro 		       const char *dev_name, void *data);
802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb);
812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb);
82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly);
83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void);
84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb);
858f1f7453SEric Sandeen static void ext4_clear_request_list(void);
86c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
87c6cb7e77SEric Whitney 					    unsigned int journal_inum);
88ac27a0ecSDave Kleikamp 
89e74031fdSJan Kara /*
90e74031fdSJan Kara  * Lock ordering
91e74031fdSJan Kara  *
92e74031fdSJan Kara  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
93e74031fdSJan Kara  * i_mmap_rwsem (inode->i_mmap_rwsem)!
94e74031fdSJan Kara  *
95e74031fdSJan Kara  * page fault path:
96e74031fdSJan Kara  * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
97e74031fdSJan Kara  *   page lock -> i_data_sem (rw)
98e74031fdSJan Kara  *
99e74031fdSJan Kara  * buffered write path:
100e74031fdSJan Kara  * sb_start_write -> i_mutex -> mmap_sem
101e74031fdSJan Kara  * sb_start_write -> i_mutex -> transaction start -> page lock ->
102e74031fdSJan Kara  *   i_data_sem (rw)
103e74031fdSJan Kara  *
104e74031fdSJan Kara  * truncate:
1051d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock
1061d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start ->
1071d39834fSNikolay Borisov  *   i_data_sem (rw)
108e74031fdSJan Kara  *
109e74031fdSJan Kara  * direct IO:
1101d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> mmap_sem
1111d39834fSNikolay Borisov  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
112e74031fdSJan Kara  *
113e74031fdSJan Kara  * writepages:
114e74031fdSJan Kara  * transaction start -> page lock(s) -> i_data_sem (rw)
115e74031fdSJan Kara  */
116e74031fdSJan Kara 
117c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
1182035e776STheodore Ts'o static struct file_system_type ext2_fs_type = {
1192035e776STheodore Ts'o 	.owner		= THIS_MODULE,
1202035e776STheodore Ts'o 	.name		= "ext2",
1212035e776STheodore Ts'o 	.mount		= ext4_mount,
1222035e776STheodore Ts'o 	.kill_sb	= kill_block_super,
1232035e776STheodore Ts'o 	.fs_flags	= FS_REQUIRES_DEV,
1242035e776STheodore Ts'o };
1257f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2");
126fa7614ddSEric W. Biederman MODULE_ALIAS("ext2");
1272035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
1282035e776STheodore Ts'o #else
1292035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0)
1302035e776STheodore Ts'o #endif
1312035e776STheodore Ts'o 
1322035e776STheodore Ts'o 
133ba69f9abSJan Kara static struct file_system_type ext3_fs_type = {
134ba69f9abSJan Kara 	.owner		= THIS_MODULE,
135ba69f9abSJan Kara 	.name		= "ext3",
136152a0836SAl Viro 	.mount		= ext4_mount,
137ba69f9abSJan Kara 	.kill_sb	= kill_block_super,
138ba69f9abSJan Kara 	.fs_flags	= FS_REQUIRES_DEV,
139ba69f9abSJan Kara };
1407f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3");
141fa7614ddSEric W. Biederman MODULE_ALIAS("ext3");
142ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
143bd81d8eeSLaurent Vivier 
144fb265c9cSTheodore Ts'o /*
145fb265c9cSTheodore Ts'o  * This works like sb_bread() except it uses ERR_PTR for error
146fb265c9cSTheodore Ts'o  * returns.  Currently with sb_bread it's impossible to distinguish
147fb265c9cSTheodore Ts'o  * between ENOMEM and EIO situations (since both result in a NULL
148fb265c9cSTheodore Ts'o  * return.
149fb265c9cSTheodore Ts'o  */
150fb265c9cSTheodore Ts'o struct buffer_head *
151fb265c9cSTheodore Ts'o ext4_sb_bread(struct super_block *sb, sector_t block, int op_flags)
152fb265c9cSTheodore Ts'o {
153fb265c9cSTheodore Ts'o 	struct buffer_head *bh = sb_getblk(sb, block);
154fb265c9cSTheodore Ts'o 
155fb265c9cSTheodore Ts'o 	if (bh == NULL)
156fb265c9cSTheodore Ts'o 		return ERR_PTR(-ENOMEM);
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)) {
5254327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
5264327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
5274327ba52SDaeho Jeong 			return;
528617ba13bSMingming Cao 		panic("EXT4-fs (device %s): panic forced after error\n",
529ac27a0ecSDave Kleikamp 			sb->s_id);
530ac27a0ecSDave Kleikamp 	}
5314327ba52SDaeho Jeong }
532ac27a0ecSDave Kleikamp 
533efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb)					\
534efbed4dcSTheodore Ts'o 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
535efbed4dcSTheodore Ts'o 			     "EXT4-fs error")
536efbed4dcSTheodore Ts'o 
53712062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function,
53854d3adbcSTheodore Ts'o 		  unsigned int line, int error, __u64 block,
53954d3adbcSTheodore Ts'o 		  const char *fmt, ...)
540ac27a0ecSDave Kleikamp {
5410ff2ea7dSJoe Perches 	struct va_format vaf;
542ac27a0ecSDave Kleikamp 	va_list args;
543ac27a0ecSDave Kleikamp 
5440db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
5450db1ff22STheodore Ts'o 		return;
5460db1ff22STheodore Ts'o 
547ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
548efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
549ac27a0ecSDave Kleikamp 		va_start(args, fmt);
5500ff2ea7dSJoe Perches 		vaf.fmt = fmt;
5510ff2ea7dSJoe Perches 		vaf.va = &args;
552efbed4dcSTheodore Ts'o 		printk(KERN_CRIT
553efbed4dcSTheodore Ts'o 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
5540ff2ea7dSJoe Perches 		       sb->s_id, function, line, current->comm, &vaf);
555ac27a0ecSDave Kleikamp 		va_end(args);
556efbed4dcSTheodore Ts'o 	}
55754d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, block, function, line);
558617ba13bSMingming Cao 	ext4_handle_error(sb);
559ac27a0ecSDave Kleikamp }
560ac27a0ecSDave Kleikamp 
561e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function,
56254d3adbcSTheodore Ts'o 			unsigned int line, ext4_fsblk_t block, int error,
563273df556SFrank Mayhar 			const char *fmt, ...)
564273df556SFrank Mayhar {
565273df556SFrank Mayhar 	va_list args;
566f7c21177STheodore Ts'o 	struct va_format vaf;
567273df556SFrank Mayhar 
5680db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5690db1ff22STheodore Ts'o 		return;
5700db1ff22STheodore Ts'o 
571ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
572efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
573273df556SFrank Mayhar 		va_start(args, fmt);
574f7c21177STheodore Ts'o 		vaf.fmt = fmt;
575f7c21177STheodore Ts'o 		vaf.va = &args;
576c398eda0STheodore Ts'o 		if (block)
577d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
578d9ee81daSJoe Perches 			       "inode #%lu: block %llu: comm %s: %pV\n",
579d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
580d9ee81daSJoe Perches 			       block, current->comm, &vaf);
581d9ee81daSJoe Perches 		else
582d9ee81daSJoe Perches 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
583d9ee81daSJoe Perches 			       "inode #%lu: comm %s: %pV\n",
584d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
585d9ee81daSJoe Perches 			       current->comm, &vaf);
586273df556SFrank Mayhar 		va_end(args);
587efbed4dcSTheodore Ts'o 	}
58854d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, error, inode->i_ino, block,
58954d3adbcSTheodore Ts'o 			function, line);
590273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
591273df556SFrank Mayhar }
592273df556SFrank Mayhar 
593e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function,
594f7c21177STheodore Ts'o 		       unsigned int line, ext4_fsblk_t block,
595f7c21177STheodore Ts'o 		       const char *fmt, ...)
596273df556SFrank Mayhar {
597273df556SFrank Mayhar 	va_list args;
598f7c21177STheodore Ts'o 	struct va_format vaf;
599496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
600273df556SFrank Mayhar 	char pathname[80], *path;
601273df556SFrank Mayhar 
6020db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
6030db1ff22STheodore Ts'o 		return;
6040db1ff22STheodore Ts'o 
605ccf0f32aSTheodore Ts'o 	trace_ext4_error(inode->i_sb, function, line);
606efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(inode->i_sb)) {
6079bf39ab2SMiklos Szeredi 		path = file_path(file, pathname, sizeof(pathname));
608f9a62d09SDan Carpenter 		if (IS_ERR(path))
609273df556SFrank Mayhar 			path = "(unknown)";
610f7c21177STheodore Ts'o 		va_start(args, fmt);
611f7c21177STheodore Ts'o 		vaf.fmt = fmt;
612f7c21177STheodore Ts'o 		vaf.va = &args;
613d9ee81daSJoe Perches 		if (block)
614d9ee81daSJoe Perches 			printk(KERN_CRIT
615d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
616d9ee81daSJoe Perches 			       "block %llu: comm %s: path %s: %pV\n",
617d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
618d9ee81daSJoe Perches 			       block, current->comm, path, &vaf);
619d9ee81daSJoe Perches 		else
620d9ee81daSJoe Perches 			printk(KERN_CRIT
621d9ee81daSJoe Perches 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
622d9ee81daSJoe Perches 			       "comm %s: path %s: %pV\n",
623d9ee81daSJoe Perches 			       inode->i_sb->s_id, function, line, inode->i_ino,
624d9ee81daSJoe Perches 			       current->comm, path, &vaf);
625273df556SFrank Mayhar 		va_end(args);
626efbed4dcSTheodore Ts'o 	}
62754d3adbcSTheodore Ts'o 	save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block,
62854d3adbcSTheodore Ts'o 			function, line);
629273df556SFrank Mayhar 	ext4_handle_error(inode->i_sb);
630273df556SFrank Mayhar }
631273df556SFrank Mayhar 
632722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno,
633ac27a0ecSDave Kleikamp 			      char nbuf[16])
634ac27a0ecSDave Kleikamp {
635ac27a0ecSDave Kleikamp 	char *errstr = NULL;
636ac27a0ecSDave Kleikamp 
637ac27a0ecSDave Kleikamp 	switch (errno) {
6386a797d27SDarrick J. Wong 	case -EFSCORRUPTED:
6396a797d27SDarrick J. Wong 		errstr = "Corrupt filesystem";
6406a797d27SDarrick J. Wong 		break;
6416a797d27SDarrick J. Wong 	case -EFSBADCRC:
6426a797d27SDarrick J. Wong 		errstr = "Filesystem failed CRC";
6436a797d27SDarrick J. Wong 		break;
644ac27a0ecSDave Kleikamp 	case -EIO:
645ac27a0ecSDave Kleikamp 		errstr = "IO failure";
646ac27a0ecSDave Kleikamp 		break;
647ac27a0ecSDave Kleikamp 	case -ENOMEM:
648ac27a0ecSDave Kleikamp 		errstr = "Out of memory";
649ac27a0ecSDave Kleikamp 		break;
650ac27a0ecSDave Kleikamp 	case -EROFS:
65178f1ddbbSTheodore Ts'o 		if (!sb || (EXT4_SB(sb)->s_journal &&
65278f1ddbbSTheodore Ts'o 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
653ac27a0ecSDave Kleikamp 			errstr = "Journal has aborted";
654ac27a0ecSDave Kleikamp 		else
655ac27a0ecSDave Kleikamp 			errstr = "Readonly filesystem";
656ac27a0ecSDave Kleikamp 		break;
657ac27a0ecSDave Kleikamp 	default:
658ac27a0ecSDave Kleikamp 		/* If the caller passed in an extra buffer for unknown
659ac27a0ecSDave Kleikamp 		 * errors, textualise them now.  Else we just return
660ac27a0ecSDave Kleikamp 		 * NULL. */
661ac27a0ecSDave Kleikamp 		if (nbuf) {
662ac27a0ecSDave Kleikamp 			/* Check for truncated error codes... */
663ac27a0ecSDave Kleikamp 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
664ac27a0ecSDave Kleikamp 				errstr = nbuf;
665ac27a0ecSDave Kleikamp 		}
666ac27a0ecSDave Kleikamp 		break;
667ac27a0ecSDave Kleikamp 	}
668ac27a0ecSDave Kleikamp 
669ac27a0ecSDave Kleikamp 	return errstr;
670ac27a0ecSDave Kleikamp }
671ac27a0ecSDave Kleikamp 
672617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions
673ac27a0ecSDave Kleikamp  * automatically and invokes the appropriate error response.  */
674ac27a0ecSDave Kleikamp 
675c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function,
676c398eda0STheodore Ts'o 		      unsigned int line, int errno)
677ac27a0ecSDave Kleikamp {
678ac27a0ecSDave Kleikamp 	char nbuf[16];
679ac27a0ecSDave Kleikamp 	const char *errstr;
680ac27a0ecSDave Kleikamp 
6810db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
6820db1ff22STheodore Ts'o 		return;
6830db1ff22STheodore Ts'o 
684ac27a0ecSDave Kleikamp 	/* Special case: if the error is EROFS, and we're not already
685ac27a0ecSDave Kleikamp 	 * inside a transaction, then there's really no point in logging
686ac27a0ecSDave Kleikamp 	 * an error. */
687bc98a42cSDavid Howells 	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
688ac27a0ecSDave Kleikamp 		return;
689ac27a0ecSDave Kleikamp 
690efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
691617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, errno, nbuf);
692c398eda0STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
693c398eda0STheodore Ts'o 		       sb->s_id, function, line, errstr);
694efbed4dcSTheodore Ts'o 	}
695ac27a0ecSDave Kleikamp 
69654d3adbcSTheodore Ts'o 	save_error_info(sb, -errno, 0, 0, function, line);
697617ba13bSMingming Cao 	ext4_handle_error(sb);
698ac27a0ecSDave Kleikamp }
699ac27a0ecSDave Kleikamp 
700ac27a0ecSDave Kleikamp /*
701617ba13bSMingming Cao  * ext4_abort is a much stronger failure handler than ext4_error.  The
702ac27a0ecSDave Kleikamp  * abort function may be used to deal with unrecoverable failures such
703ac27a0ecSDave Kleikamp  * as journal IO errors or ENOMEM at a critical moment in log management.
704ac27a0ecSDave Kleikamp  *
705ac27a0ecSDave Kleikamp  * We unconditionally force the filesystem into an ABORT|READONLY state,
706ac27a0ecSDave Kleikamp  * unless the error response on the fs has been set to panic in which
707ac27a0ecSDave Kleikamp  * case we take the easy way out and panic immediately.
708ac27a0ecSDave Kleikamp  */
709ac27a0ecSDave Kleikamp 
710c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function,
71154d3adbcSTheodore Ts'o 		  unsigned int line, int error, const char *fmt, ...)
712ac27a0ecSDave Kleikamp {
713651e1c3bSJoe Perches 	struct va_format vaf;
714ac27a0ecSDave Kleikamp 	va_list args;
715ac27a0ecSDave Kleikamp 
7160db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
7170db1ff22STheodore Ts'o 		return;
7180db1ff22STheodore Ts'o 
71954d3adbcSTheodore Ts'o 	save_error_info(sb, error, 0, 0, function, line);
720ac27a0ecSDave Kleikamp 	va_start(args, fmt);
721651e1c3bSJoe Perches 	vaf.fmt = fmt;
722651e1c3bSJoe Perches 	vaf.va = &args;
723651e1c3bSJoe Perches 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
724651e1c3bSJoe Perches 	       sb->s_id, function, line, &vaf);
725ac27a0ecSDave Kleikamp 	va_end(args);
726ac27a0ecSDave Kleikamp 
727bc98a42cSDavid Howells 	if (sb_rdonly(sb) == 0) {
728b31e1552SEric Sandeen 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
7294ab2f15bSTheodore Ts'o 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
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;
736ef2cabf7SHidehiro Kawai 		if (EXT4_SB(sb)->s_journal)
737dab291afSMingming Cao 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
7381c13d5c0STheodore Ts'o 	}
7392c1d0e36SJan Kara 	if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
7404327ba52SDaeho Jeong 		if (EXT4_SB(sb)->s_journal &&
7414327ba52SDaeho Jeong 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
7424327ba52SDaeho Jeong 			return;
7431c13d5c0STheodore Ts'o 		panic("EXT4-fs panic from previous error\n");
744ac27a0ecSDave Kleikamp 	}
7454327ba52SDaeho Jeong }
746ac27a0ecSDave Kleikamp 
747e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb,
748e7c96e8eSJoe Perches 		const char *prefix, const char *fmt, ...)
749b31e1552SEric Sandeen {
7500ff2ea7dSJoe Perches 	struct va_format vaf;
751b31e1552SEric Sandeen 	va_list args;
752b31e1552SEric Sandeen 
753efbed4dcSTheodore Ts'o 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
754efbed4dcSTheodore Ts'o 		return;
755efbed4dcSTheodore Ts'o 
756b31e1552SEric Sandeen 	va_start(args, fmt);
7570ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7580ff2ea7dSJoe Perches 	vaf.va = &args;
7590ff2ea7dSJoe Perches 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
760b31e1552SEric Sandeen 	va_end(args);
761b31e1552SEric Sandeen }
762b31e1552SEric Sandeen 
763b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb)					\
764b03a2f7eSAndreas Dilger 		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
765b03a2f7eSAndreas Dilger 			     "EXT4-fs warning")
766b03a2f7eSAndreas Dilger 
76712062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function,
768c398eda0STheodore Ts'o 		    unsigned int line, const char *fmt, ...)
769ac27a0ecSDave Kleikamp {
7700ff2ea7dSJoe Perches 	struct va_format vaf;
771ac27a0ecSDave Kleikamp 	va_list args;
772ac27a0ecSDave Kleikamp 
773b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(sb))
774efbed4dcSTheodore Ts'o 		return;
775efbed4dcSTheodore Ts'o 
776ac27a0ecSDave Kleikamp 	va_start(args, fmt);
7770ff2ea7dSJoe Perches 	vaf.fmt = fmt;
7780ff2ea7dSJoe Perches 	vaf.va = &args;
7790ff2ea7dSJoe Perches 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
7800ff2ea7dSJoe Perches 	       sb->s_id, function, line, &vaf);
781ac27a0ecSDave Kleikamp 	va_end(args);
782ac27a0ecSDave Kleikamp }
783ac27a0ecSDave Kleikamp 
784b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
785b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...)
786b03a2f7eSAndreas Dilger {
787b03a2f7eSAndreas Dilger 	struct va_format vaf;
788b03a2f7eSAndreas Dilger 	va_list args;
789b03a2f7eSAndreas Dilger 
790b03a2f7eSAndreas Dilger 	if (!ext4_warning_ratelimit(inode->i_sb))
791b03a2f7eSAndreas Dilger 		return;
792b03a2f7eSAndreas Dilger 
793b03a2f7eSAndreas Dilger 	va_start(args, fmt);
794b03a2f7eSAndreas Dilger 	vaf.fmt = fmt;
795b03a2f7eSAndreas Dilger 	vaf.va = &args;
796b03a2f7eSAndreas Dilger 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
797b03a2f7eSAndreas Dilger 	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
798b03a2f7eSAndreas Dilger 	       function, line, inode->i_ino, current->comm, &vaf);
799b03a2f7eSAndreas Dilger 	va_end(args);
800b03a2f7eSAndreas Dilger }
801b03a2f7eSAndreas Dilger 
802e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line,
803e29136f8STheodore Ts'o 			     struct super_block *sb, ext4_group_t grp,
804e29136f8STheodore Ts'o 			     unsigned long ino, ext4_fsblk_t block,
805e29136f8STheodore Ts'o 			     const char *fmt, ...)
8065d1b1b3fSAneesh Kumar K.V __releases(bitlock)
8075d1b1b3fSAneesh Kumar K.V __acquires(bitlock)
8085d1b1b3fSAneesh Kumar K.V {
8090ff2ea7dSJoe Perches 	struct va_format vaf;
8105d1b1b3fSAneesh Kumar K.V 	va_list args;
8115d1b1b3fSAneesh Kumar K.V 
8120db1ff22STheodore Ts'o 	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
8130db1ff22STheodore Ts'o 		return;
8140db1ff22STheodore Ts'o 
815ccf0f32aSTheodore Ts'o 	trace_ext4_error(sb, function, line);
81654d3adbcSTheodore Ts'o 	__save_error_info(sb, EFSCORRUPTED, ino, block, function, line);
8170ff2ea7dSJoe Perches 
818efbed4dcSTheodore Ts'o 	if (ext4_error_ratelimit(sb)) {
8195d1b1b3fSAneesh Kumar K.V 		va_start(args, fmt);
8200ff2ea7dSJoe Perches 		vaf.fmt = fmt;
8210ff2ea7dSJoe Perches 		vaf.va = &args;
82221149d61SRobin Dong 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
823e29136f8STheodore Ts'o 		       sb->s_id, function, line, grp);
824e29136f8STheodore Ts'o 		if (ino)
8250ff2ea7dSJoe Perches 			printk(KERN_CONT "inode %lu: ", ino);
826e29136f8STheodore Ts'o 		if (block)
827efbed4dcSTheodore Ts'o 			printk(KERN_CONT "block %llu:",
828efbed4dcSTheodore Ts'o 			       (unsigned long long) block);
8290ff2ea7dSJoe Perches 		printk(KERN_CONT "%pV\n", &vaf);
8305d1b1b3fSAneesh Kumar K.V 		va_end(args);
831efbed4dcSTheodore Ts'o 	}
8325d1b1b3fSAneesh Kumar K.V 
833327eaf73STheodore Ts'o 	if (test_opt(sb, WARN_ON_ERROR))
834327eaf73STheodore Ts'o 		WARN_ON_ONCE(1);
835327eaf73STheodore Ts'o 
8365d1b1b3fSAneesh Kumar K.V 	if (test_opt(sb, ERRORS_CONT)) {
837e2d67052STheodore Ts'o 		ext4_commit_super(sb, 0);
8385d1b1b3fSAneesh Kumar K.V 		return;
8395d1b1b3fSAneesh Kumar K.V 	}
8401c13d5c0STheodore Ts'o 
8415d1b1b3fSAneesh Kumar K.V 	ext4_unlock_group(sb, grp);
84206f29cc8SZhouyi Zhou 	ext4_commit_super(sb, 1);
8435d1b1b3fSAneesh Kumar K.V 	ext4_handle_error(sb);
8445d1b1b3fSAneesh Kumar K.V 	/*
8455d1b1b3fSAneesh Kumar K.V 	 * We only get here in the ERRORS_RO case; relocking the group
8465d1b1b3fSAneesh Kumar K.V 	 * may be dangerous, but nothing bad will happen since the
8475d1b1b3fSAneesh Kumar K.V 	 * filesystem will have already been marked read/only and the
8485d1b1b3fSAneesh Kumar K.V 	 * journal has been aborted.  We return 1 as a hint to callers
8495d1b1b3fSAneesh Kumar K.V 	 * who might what to use the return value from
85025985edcSLucas De Marchi 	 * ext4_grp_locked_error() to distinguish between the
8515d1b1b3fSAneesh Kumar K.V 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
8525d1b1b3fSAneesh Kumar K.V 	 * aggressively from the ext4 function in question, with a
8535d1b1b3fSAneesh Kumar K.V 	 * more appropriate error code.
8545d1b1b3fSAneesh Kumar K.V 	 */
8555d1b1b3fSAneesh Kumar K.V 	ext4_lock_group(sb, grp);
8565d1b1b3fSAneesh Kumar K.V 	return;
8575d1b1b3fSAneesh Kumar K.V }
8585d1b1b3fSAneesh Kumar K.V 
859db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
860db79e6d1SWang Shilong 				     ext4_group_t group,
861db79e6d1SWang Shilong 				     unsigned int flags)
862db79e6d1SWang Shilong {
863db79e6d1SWang Shilong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
864db79e6d1SWang Shilong 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
865db79e6d1SWang Shilong 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
8669af0b3d1SWang Shilong 	int ret;
867db79e6d1SWang Shilong 
8689af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
8699af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
8709af0b3d1SWang Shilong 					    &grp->bb_state);
8719af0b3d1SWang Shilong 		if (!ret)
872db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeclusters_counter,
873db79e6d1SWang Shilong 					   grp->bb_free);
874db79e6d1SWang Shilong 	}
875db79e6d1SWang Shilong 
8769af0b3d1SWang Shilong 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
8779af0b3d1SWang Shilong 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
8789af0b3d1SWang Shilong 					    &grp->bb_state);
8799af0b3d1SWang Shilong 		if (!ret && gdp) {
880db79e6d1SWang Shilong 			int count;
881db79e6d1SWang Shilong 
882db79e6d1SWang Shilong 			count = ext4_free_inodes_count(sb, gdp);
883db79e6d1SWang Shilong 			percpu_counter_sub(&sbi->s_freeinodes_counter,
884db79e6d1SWang Shilong 					   count);
885db79e6d1SWang Shilong 		}
886db79e6d1SWang Shilong 	}
887db79e6d1SWang Shilong }
888db79e6d1SWang Shilong 
889617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb)
890ac27a0ecSDave Kleikamp {
891617ba13bSMingming Cao 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
892ac27a0ecSDave Kleikamp 
893617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
894ac27a0ecSDave Kleikamp 		return;
895ac27a0ecSDave Kleikamp 
89612062dddSEric Sandeen 	ext4_warning(sb,
897ac27a0ecSDave Kleikamp 		     "updating to rev %d because of new feature flag, "
898ac27a0ecSDave Kleikamp 		     "running e2fsck is recommended",
899617ba13bSMingming Cao 		     EXT4_DYNAMIC_REV);
900ac27a0ecSDave Kleikamp 
901617ba13bSMingming Cao 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
902617ba13bSMingming Cao 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
903617ba13bSMingming Cao 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
904ac27a0ecSDave Kleikamp 	/* leave es->s_feature_*compat flags alone */
905ac27a0ecSDave Kleikamp 	/* es->s_uuid will be set by e2fsck if empty */
906ac27a0ecSDave Kleikamp 
907ac27a0ecSDave Kleikamp 	/*
908ac27a0ecSDave Kleikamp 	 * The rest of the superblock fields should be zero, and if not it
909ac27a0ecSDave Kleikamp 	 * means they are likely already in use, so leave them alone.  We
910ac27a0ecSDave Kleikamp 	 * can leave it up to e2fsck to clean up any inconsistencies there.
911ac27a0ecSDave Kleikamp 	 */
912ac27a0ecSDave Kleikamp }
913ac27a0ecSDave Kleikamp 
914ac27a0ecSDave Kleikamp /*
915ac27a0ecSDave Kleikamp  * Open the external journal device
916ac27a0ecSDave Kleikamp  */
917b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
918ac27a0ecSDave Kleikamp {
919ac27a0ecSDave Kleikamp 	struct block_device *bdev;
920ac27a0ecSDave Kleikamp 
921d4d77629STejun Heo 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
922ac27a0ecSDave Kleikamp 	if (IS_ERR(bdev))
923ac27a0ecSDave Kleikamp 		goto fail;
924ac27a0ecSDave Kleikamp 	return bdev;
925ac27a0ecSDave Kleikamp 
926ac27a0ecSDave Kleikamp fail:
927ea3edd4dSChristoph Hellwig 	ext4_msg(sb, KERN_ERR,
928ea3edd4dSChristoph Hellwig 		 "failed to open journal device unknown-block(%u,%u) %ld",
929ea3edd4dSChristoph Hellwig 		 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
930ac27a0ecSDave Kleikamp 	return NULL;
931ac27a0ecSDave Kleikamp }
932ac27a0ecSDave Kleikamp 
933ac27a0ecSDave Kleikamp /*
934ac27a0ecSDave Kleikamp  * Release the journal device
935ac27a0ecSDave Kleikamp  */
9364385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev)
937ac27a0ecSDave Kleikamp {
9384385bab1SAl Viro 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
939ac27a0ecSDave Kleikamp }
940ac27a0ecSDave Kleikamp 
9414385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
942ac27a0ecSDave Kleikamp {
943ac27a0ecSDave Kleikamp 	struct block_device *bdev;
944ac27a0ecSDave Kleikamp 	bdev = sbi->journal_bdev;
945ac27a0ecSDave Kleikamp 	if (bdev) {
9464385bab1SAl Viro 		ext4_blkdev_put(bdev);
947ac27a0ecSDave Kleikamp 		sbi->journal_bdev = NULL;
948ac27a0ecSDave Kleikamp 	}
949ac27a0ecSDave Kleikamp }
950ac27a0ecSDave Kleikamp 
951ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l)
952ac27a0ecSDave Kleikamp {
953617ba13bSMingming Cao 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
954ac27a0ecSDave Kleikamp }
955ac27a0ecSDave Kleikamp 
956617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
957ac27a0ecSDave Kleikamp {
958ac27a0ecSDave Kleikamp 	struct list_head *l;
959ac27a0ecSDave Kleikamp 
960b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
961ac27a0ecSDave Kleikamp 		 le32_to_cpu(sbi->s_es->s_last_orphan));
962ac27a0ecSDave Kleikamp 
963ac27a0ecSDave Kleikamp 	printk(KERN_ERR "sb_info orphan list:\n");
964ac27a0ecSDave Kleikamp 	list_for_each(l, &sbi->s_orphan) {
965ac27a0ecSDave Kleikamp 		struct inode *inode = orphan_list_entry(l);
966ac27a0ecSDave Kleikamp 		printk(KERN_ERR "  "
967ac27a0ecSDave Kleikamp 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
968ac27a0ecSDave Kleikamp 		       inode->i_sb->s_id, inode->i_ino, inode,
969ac27a0ecSDave Kleikamp 		       inode->i_mode, inode->i_nlink,
970ac27a0ecSDave Kleikamp 		       NEXT_ORPHAN(inode));
971ac27a0ecSDave Kleikamp 	}
972ac27a0ecSDave Kleikamp }
973ac27a0ecSDave Kleikamp 
974957153fcSJan Kara #ifdef CONFIG_QUOTA
975957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type);
976957153fcSJan Kara 
977957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
978957153fcSJan Kara {
979957153fcSJan Kara 	int type;
980957153fcSJan Kara 
981957153fcSJan Kara 	/* Use our quota_off function to clear inode flags etc. */
982957153fcSJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++)
983957153fcSJan Kara 		ext4_quota_off(sb, type);
984957153fcSJan Kara }
98533458eabSTheodore Ts'o 
98633458eabSTheodore Ts'o /*
98733458eabSTheodore Ts'o  * This is a helper function which is used in the mount/remount
98833458eabSTheodore Ts'o  * codepaths (which holds s_umount) to fetch the quota file name.
98933458eabSTheodore Ts'o  */
99033458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb,
99133458eabSTheodore Ts'o 				struct ext4_sb_info *sbi,
99233458eabSTheodore Ts'o 				int type)
99333458eabSTheodore Ts'o {
99433458eabSTheodore Ts'o 	return rcu_dereference_protected(sbi->s_qf_names[type],
99533458eabSTheodore Ts'o 					 lockdep_is_held(&sb->s_umount));
99633458eabSTheodore Ts'o }
997957153fcSJan Kara #else
998957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb)
999957153fcSJan Kara {
1000957153fcSJan Kara }
1001957153fcSJan Kara #endif
1002957153fcSJan Kara 
1003617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb)
1004ac27a0ecSDave Kleikamp {
1005617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1006617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
10071d0c3924STheodore Ts'o 	struct buffer_head **group_desc;
10087c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
100997abd7d4STheodore Ts'o 	int aborted = 0;
1010ef2cabf7SHidehiro Kawai 	int i, err;
1011ac27a0ecSDave Kleikamp 
1012857ac889SLukas Czerner 	ext4_unregister_li_request(sb);
1013957153fcSJan Kara 	ext4_quota_off_umount(sb);
1014e0ccfd95SChristoph Hellwig 
10152e8fa54eSJan Kara 	destroy_workqueue(sbi->rsv_conversion_wq);
10164c0425ffSMingming Cao 
10175e47868fSRitesh Harjani 	/*
10185e47868fSRitesh Harjani 	 * Unregister sysfs before destroying jbd2 journal.
10195e47868fSRitesh Harjani 	 * Since we could still access attr_journal_task attribute via sysfs
10205e47868fSRitesh Harjani 	 * path which could have sbi->s_journal->j_task as NULL
10215e47868fSRitesh Harjani 	 */
10225e47868fSRitesh Harjani 	ext4_unregister_sysfs(sb);
10235e47868fSRitesh Harjani 
10240390131bSFrank Mayhar 	if (sbi->s_journal) {
102597abd7d4STheodore Ts'o 		aborted = is_journal_aborted(sbi->s_journal);
1026ef2cabf7SHidehiro Kawai 		err = jbd2_journal_destroy(sbi->s_journal);
102747b4a50bSJan Kara 		sbi->s_journal = NULL;
1028878520acSTheodore Ts'o 		if ((err < 0) && !aborted) {
102954d3adbcSTheodore Ts'o 			ext4_abort(sb, -err, "Couldn't clean up the journal");
10300390131bSFrank Mayhar 		}
1031878520acSTheodore Ts'o 	}
1032d4edac31SJosef Bacik 
1033d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
10349105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
1035d4edac31SJosef Bacik 	ext4_release_system_zone(sb);
1036d4edac31SJosef Bacik 	ext4_mb_release(sb);
1037d4edac31SJosef Bacik 	ext4_ext_release(sb);
1038d4edac31SJosef Bacik 
1039bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && !aborted) {
1040e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
1041ac27a0ecSDave Kleikamp 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1042ac27a0ecSDave Kleikamp 	}
1043bc98a42cSDavid Howells 	if (!sb_rdonly(sb))
1044a8e25a83SArtem Bityutskiy 		ext4_commit_super(sb, 1);
1045a8e25a83SArtem Bityutskiy 
10461d0c3924STheodore Ts'o 	rcu_read_lock();
10471d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
1048ac27a0ecSDave Kleikamp 	for (i = 0; i < sbi->s_gdb_count; i++)
10491d0c3924STheodore Ts'o 		brelse(group_desc[i]);
10501d0c3924STheodore Ts'o 	kvfree(group_desc);
10517c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
10527c990728SSuraj Jitindar Singh 	if (flex_groups) {
10537c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
10547c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
10557c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
10567c990728SSuraj Jitindar Singh 	}
10571d0c3924STheodore Ts'o 	rcu_read_unlock();
105857042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
1059ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1060ac27a0ecSDave Kleikamp 	percpu_counter_destroy(&sbi->s_dirs_counter);
106157042651STheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1062bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
1063ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1064a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
106533458eabSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
1066ac27a0ecSDave Kleikamp #endif
1067ac27a0ecSDave Kleikamp 
1068ac27a0ecSDave Kleikamp 	/* Debugging code just in case the in-memory inode orphan list
1069ac27a0ecSDave Kleikamp 	 * isn't empty.  The on-disk one can be non-empty if we've
1070ac27a0ecSDave Kleikamp 	 * detected an error and taken the fs readonly, but the
1071ac27a0ecSDave Kleikamp 	 * in-memory list had better be clean by this point. */
1072ac27a0ecSDave Kleikamp 	if (!list_empty(&sbi->s_orphan))
1073ac27a0ecSDave Kleikamp 		dump_orphan_list(sb, sbi);
1074ac27a0ecSDave Kleikamp 	J_ASSERT(list_empty(&sbi->s_orphan));
1075ac27a0ecSDave Kleikamp 
107689d96a6fSTheodore Ts'o 	sync_blockdev(sb->s_bdev);
1077f98393a6SPeter Zijlstra 	invalidate_bdev(sb->s_bdev);
1078ac27a0ecSDave Kleikamp 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
1079ac27a0ecSDave Kleikamp 		/*
1080ac27a0ecSDave Kleikamp 		 * Invalidate the journal device's buffers.  We don't want them
1081ac27a0ecSDave Kleikamp 		 * floating about in memory - the physical journal device may
1082ac27a0ecSDave Kleikamp 		 * hotswapped, and it breaks the `ro-after' testing code.
1083ac27a0ecSDave Kleikamp 		 */
1084ac27a0ecSDave Kleikamp 		sync_blockdev(sbi->journal_bdev);
1085f98393a6SPeter Zijlstra 		invalidate_bdev(sbi->journal_bdev);
1086617ba13bSMingming Cao 		ext4_blkdev_remove(sbi);
1087ac27a0ecSDave Kleikamp 	}
108850c15df6SChengguang Xu 
1089dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1090dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
109150c15df6SChengguang Xu 
109247387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
109347387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
109450c15df6SChengguang Xu 
1095c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
1096c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
10979060dd2cSEric Sandeen 	brelse(sbi->s_sbh);
1098ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
10993197ebdbSTheodore Ts'o 	/*
11003197ebdbSTheodore Ts'o 	 * Now that we are completely done shutting down the
11013197ebdbSTheodore Ts'o 	 * superblock, we need to actually destroy the kobject.
11023197ebdbSTheodore Ts'o 	 */
11033197ebdbSTheodore Ts'o 	kobject_put(&sbi->s_kobj);
11043197ebdbSTheodore Ts'o 	wait_for_completion(&sbi->s_kobj_unregister);
11050441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
11060441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
1107705895b6SPekka Enberg 	kfree(sbi->s_blockgroup_lock);
11085e405595SDan Williams 	fs_put_dax(sbi->s_daxdev);
1109c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1110c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
1111c83ad55eSGabriel Krisman Bertazi #endif
1112ac27a0ecSDave Kleikamp 	kfree(sbi);
1113ac27a0ecSDave Kleikamp }
1114ac27a0ecSDave Kleikamp 
1115e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep;
1116ac27a0ecSDave Kleikamp 
1117ac27a0ecSDave Kleikamp /*
1118ac27a0ecSDave Kleikamp  * Called inside transaction, so use GFP_NOFS
1119ac27a0ecSDave Kleikamp  */
1120617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb)
1121ac27a0ecSDave Kleikamp {
1122617ba13bSMingming Cao 	struct ext4_inode_info *ei;
1123ac27a0ecSDave Kleikamp 
1124e6b4f8daSChristoph Lameter 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
1125ac27a0ecSDave Kleikamp 	if (!ei)
1126ac27a0ecSDave Kleikamp 		return NULL;
11270b8e58a1SAndreas Dilger 
1128ee73f9a5SJeff Layton 	inode_set_iversion(&ei->vfs_inode, 1);
1129202ee5dfSTheodore Ts'o 	spin_lock_init(&ei->i_raw_lock);
1130c9de560dSAlex Tomas 	INIT_LIST_HEAD(&ei->i_prealloc_list);
1131c9de560dSAlex Tomas 	spin_lock_init(&ei->i_prealloc_lock);
11329a26b661SZheng Liu 	ext4_es_init_tree(&ei->i_es_tree);
11339a26b661SZheng Liu 	rwlock_init(&ei->i_es_lock);
1134edaa53caSZheng Liu 	INIT_LIST_HEAD(&ei->i_es_list);
1135eb68d0e2SZheng Liu 	ei->i_es_all_nr = 0;
1136edaa53caSZheng Liu 	ei->i_es_shk_nr = 0;
1137dd475925SJan Kara 	ei->i_es_shrink_lblk = 0;
1138d2a17637SMingming Cao 	ei->i_reserved_data_blocks = 0;
1139d2a17637SMingming Cao 	spin_lock_init(&(ei->i_block_reservation_lock));
11401dc0aa46SEric Whitney 	ext4_init_pending_tree(&ei->i_pending_tree);
1141a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1142a9e7f447SDmitry Monakhov 	ei->i_reserved_quota = 0;
114396c7e0d9SJan Kara 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1144a9e7f447SDmitry Monakhov #endif
11458aefcd55STheodore Ts'o 	ei->jinode = NULL;
11462e8fa54eSJan Kara 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1147744692dcSJiaying Zhang 	spin_lock_init(&ei->i_completed_io_lock);
1148b436b9beSJan Kara 	ei->i_sync_tid = 0;
1149b436b9beSJan Kara 	ei->i_datasync_tid = 0;
1150e27f41e1SDmitry Monakhov 	atomic_set(&ei->i_unwritten, 0);
11512e8fa54eSJan Kara 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1152ac27a0ecSDave Kleikamp 	return &ei->vfs_inode;
1153ac27a0ecSDave Kleikamp }
1154ac27a0ecSDave Kleikamp 
11557ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode)
11567ff9c073STheodore Ts'o {
11577ff9c073STheodore Ts'o 	int drop = generic_drop_inode(inode);
11587ff9c073STheodore Ts'o 
115929b3692eSEric Biggers 	if (!drop)
116029b3692eSEric Biggers 		drop = fscrypt_drop_inode(inode);
116129b3692eSEric Biggers 
11627ff9c073STheodore Ts'o 	trace_ext4_drop_inode(inode, drop);
11637ff9c073STheodore Ts'o 	return drop;
11647ff9c073STheodore Ts'o }
11657ff9c073STheodore Ts'o 
116694053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode)
1167fa0d7e3dSNick Piggin {
11682c58d548SEric Biggers 	fscrypt_free_inode(inode);
1169fa0d7e3dSNick Piggin 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1170fa0d7e3dSNick Piggin }
1171fa0d7e3dSNick Piggin 
1172617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode)
1173ac27a0ecSDave Kleikamp {
11749f7dd93dSVasily Averin 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1175b31e1552SEric Sandeen 		ext4_msg(inode->i_sb, KERN_ERR,
1176b31e1552SEric Sandeen 			 "Inode %lu (%p): orphan list check failed!",
1177b31e1552SEric Sandeen 			 inode->i_ino, EXT4_I(inode));
11789f7dd93dSVasily Averin 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
11799f7dd93dSVasily Averin 				EXT4_I(inode), sizeof(struct ext4_inode_info),
11809f7dd93dSVasily Averin 				true);
11819f7dd93dSVasily Averin 		dump_stack();
11829f7dd93dSVasily Averin 	}
1183ac27a0ecSDave Kleikamp }
1184ac27a0ecSDave Kleikamp 
118551cc5068SAlexey Dobriyan static void init_once(void *foo)
1186ac27a0ecSDave Kleikamp {
1187617ba13bSMingming Cao 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1188ac27a0ecSDave Kleikamp 
1189ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&ei->i_orphan);
1190ac27a0ecSDave Kleikamp 	init_rwsem(&ei->xattr_sem);
11910e855ac8SAneesh Kumar K.V 	init_rwsem(&ei->i_data_sem);
1192ea3d7209SJan Kara 	init_rwsem(&ei->i_mmap_sem);
1193ac27a0ecSDave Kleikamp 	inode_init_once(&ei->vfs_inode);
1194ac27a0ecSDave Kleikamp }
1195ac27a0ecSDave Kleikamp 
1196e67bc2b3SFabian Frederick static int __init init_inodecache(void)
1197ac27a0ecSDave Kleikamp {
1198f8dd7c70SDavid Windsor 	ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1199f8dd7c70SDavid Windsor 				sizeof(struct ext4_inode_info), 0,
1200f8dd7c70SDavid Windsor 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1201f8dd7c70SDavid Windsor 					SLAB_ACCOUNT),
1202f8dd7c70SDavid Windsor 				offsetof(struct ext4_inode_info, i_data),
1203f8dd7c70SDavid Windsor 				sizeof_field(struct ext4_inode_info, i_data),
120420c2df83SPaul Mundt 				init_once);
1205617ba13bSMingming Cao 	if (ext4_inode_cachep == NULL)
1206ac27a0ecSDave Kleikamp 		return -ENOMEM;
1207ac27a0ecSDave Kleikamp 	return 0;
1208ac27a0ecSDave Kleikamp }
1209ac27a0ecSDave Kleikamp 
1210ac27a0ecSDave Kleikamp static void destroy_inodecache(void)
1211ac27a0ecSDave Kleikamp {
12128c0a8537SKirill A. Shutemov 	/*
12138c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
12148c0a8537SKirill A. Shutemov 	 * destroy cache.
12158c0a8537SKirill A. Shutemov 	 */
12168c0a8537SKirill A. Shutemov 	rcu_barrier();
1217617ba13bSMingming Cao 	kmem_cache_destroy(ext4_inode_cachep);
1218ac27a0ecSDave Kleikamp }
1219ac27a0ecSDave Kleikamp 
12200930fcc1SAl Viro void ext4_clear_inode(struct inode *inode)
1221ac27a0ecSDave Kleikamp {
12220930fcc1SAl Viro 	invalidate_inode_buffers(inode);
1223dbd5768fSJan Kara 	clear_inode(inode);
1224c2ea3fdeSTheodore Ts'o 	ext4_discard_preallocations(inode);
122551865fdaSZheng Liu 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1226f4c2d372SJan Kara 	dquot_drop(inode);
12278aefcd55STheodore Ts'o 	if (EXT4_I(inode)->jinode) {
12288aefcd55STheodore Ts'o 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
12298aefcd55STheodore Ts'o 					       EXT4_I(inode)->jinode);
12308aefcd55STheodore Ts'o 		jbd2_free_inode(EXT4_I(inode)->jinode);
12318aefcd55STheodore Ts'o 		EXT4_I(inode)->jinode = NULL;
12328aefcd55STheodore Ts'o 	}
12333d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
1234c93d8f88SEric Biggers 	fsverity_cleanup_inode(inode);
1235ac27a0ecSDave Kleikamp }
1236ac27a0ecSDave Kleikamp 
12371b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb,
12381b961ac0SChristoph Hellwig 					u64 ino, u32 generation)
1239ac27a0ecSDave Kleikamp {
1240ac27a0ecSDave Kleikamp 	struct inode *inode;
1241ac27a0ecSDave Kleikamp 
12428a363970STheodore Ts'o 	/*
1243ac27a0ecSDave Kleikamp 	 * Currently we don't know the generation for parent directory, so
1244ac27a0ecSDave Kleikamp 	 * a generation of 0 means "accept any"
1245ac27a0ecSDave Kleikamp 	 */
12468a363970STheodore Ts'o 	inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
12471d1fe1eeSDavid Howells 	if (IS_ERR(inode))
12481d1fe1eeSDavid Howells 		return ERR_CAST(inode);
12491d1fe1eeSDavid Howells 	if (generation && inode->i_generation != generation) {
1250ac27a0ecSDave Kleikamp 		iput(inode);
1251ac27a0ecSDave Kleikamp 		return ERR_PTR(-ESTALE);
1252ac27a0ecSDave Kleikamp 	}
12531b961ac0SChristoph Hellwig 
12541b961ac0SChristoph Hellwig 	return inode;
1255ac27a0ecSDave Kleikamp }
12561b961ac0SChristoph Hellwig 
12571b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
12581b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12591b961ac0SChristoph Hellwig {
12601b961ac0SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
12611b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
12621b961ac0SChristoph Hellwig }
12631b961ac0SChristoph Hellwig 
12641b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
12651b961ac0SChristoph Hellwig 					int fh_len, int fh_type)
12661b961ac0SChristoph Hellwig {
12671b961ac0SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
12681b961ac0SChristoph Hellwig 				    ext4_nfs_get_inode);
1269ac27a0ecSDave Kleikamp }
1270ac27a0ecSDave Kleikamp 
1271fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode)
1272fde87268STheodore Ts'o {
1273fde87268STheodore Ts'o 	struct writeback_control wbc = {
1274fde87268STheodore Ts'o 		.sync_mode = WB_SYNC_ALL
1275fde87268STheodore Ts'o 	};
1276fde87268STheodore Ts'o 
1277fde87268STheodore Ts'o 	trace_ext4_nfs_commit_metadata(inode);
1278fde87268STheodore Ts'o 	return ext4_write_inode(inode, &wbc);
1279fde87268STheodore Ts'o }
1280fde87268STheodore Ts'o 
1281c39a7f84SToshiyuki Okajima /*
1282c39a7f84SToshiyuki Okajima  * Try to release metadata pages (indirect blocks, directories) which are
1283c39a7f84SToshiyuki Okajima  * mapped via the block device.  Since these pages could have journal heads
1284c39a7f84SToshiyuki Okajima  * which would prevent try_to_free_buffers() from freeing them, we must use
1285c39a7f84SToshiyuki Okajima  * jbd2 layer's try_to_free_buffers() function to release them.
1286c39a7f84SToshiyuki Okajima  */
12870b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
12880b8e58a1SAndreas Dilger 				 gfp_t wait)
1289c39a7f84SToshiyuki Okajima {
1290c39a7f84SToshiyuki Okajima 	journal_t *journal = EXT4_SB(sb)->s_journal;
1291c39a7f84SToshiyuki Okajima 
1292c39a7f84SToshiyuki Okajima 	WARN_ON(PageChecked(page));
1293c39a7f84SToshiyuki Okajima 	if (!page_has_buffers(page))
1294c39a7f84SToshiyuki Okajima 		return 0;
1295c39a7f84SToshiyuki Okajima 	if (journal)
1296c39a7f84SToshiyuki Okajima 		return jbd2_journal_try_to_free_buffers(journal, page,
1297d0164adcSMel Gorman 						wait & ~__GFP_DIRECT_RECLAIM);
1298c39a7f84SToshiyuki Okajima 	return try_to_free_buffers(page);
1299c39a7f84SToshiyuki Okajima }
1300c39a7f84SToshiyuki Okajima 
1301643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1302a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1303a7550b30SJaegeuk Kim {
1304a7550b30SJaegeuk Kim 	return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1305a7550b30SJaegeuk Kim 				 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1306a7550b30SJaegeuk Kim }
1307a7550b30SJaegeuk Kim 
1308a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1309a7550b30SJaegeuk Kim 							void *fs_data)
1310a7550b30SJaegeuk Kim {
13112f8f5e76SEric Biggers 	handle_t *handle = fs_data;
1312c1a5d5f6STahsin Erdogan 	int res, res2, credits, retries = 0;
1313a7550b30SJaegeuk Kim 
13149ce0151aSEric Biggers 	/*
13159ce0151aSEric Biggers 	 * Encrypting the root directory is not allowed because e2fsck expects
13169ce0151aSEric Biggers 	 * lost+found to exist and be unencrypted, and encrypting the root
13179ce0151aSEric Biggers 	 * directory would imply encrypting the lost+found directory as well as
13189ce0151aSEric Biggers 	 * the filename "lost+found" itself.
13199ce0151aSEric Biggers 	 */
13209ce0151aSEric Biggers 	if (inode->i_ino == EXT4_ROOT_INO)
13219ce0151aSEric Biggers 		return -EPERM;
1322a7550b30SJaegeuk Kim 
13237d3e06a8SRoss Zwisler 	if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
13247d3e06a8SRoss Zwisler 		return -EINVAL;
13257d3e06a8SRoss Zwisler 
132694840e3cSEric Biggers 	res = ext4_convert_inline_data(inode);
132794840e3cSEric Biggers 	if (res)
132894840e3cSEric Biggers 		return res;
132994840e3cSEric Biggers 
13302f8f5e76SEric Biggers 	/*
13312f8f5e76SEric Biggers 	 * If a journal handle was specified, then the encryption context is
13322f8f5e76SEric Biggers 	 * being set on a new inode via inheritance and is part of a larger
13332f8f5e76SEric Biggers 	 * transaction to create the inode.  Otherwise the encryption context is
13342f8f5e76SEric Biggers 	 * being set on an existing inode in its own transaction.  Only in the
13352f8f5e76SEric Biggers 	 * latter case should the "retry on ENOSPC" logic be used.
13362f8f5e76SEric Biggers 	 */
13372f8f5e76SEric Biggers 
13382f8f5e76SEric Biggers 	if (handle) {
13392f8f5e76SEric Biggers 		res = ext4_xattr_set_handle(handle, inode,
13402f8f5e76SEric Biggers 					    EXT4_XATTR_INDEX_ENCRYPTION,
13412f8f5e76SEric Biggers 					    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13422f8f5e76SEric Biggers 					    ctx, len, 0);
1343a7550b30SJaegeuk Kim 		if (!res) {
1344a7550b30SJaegeuk Kim 			ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1345a7550b30SJaegeuk Kim 			ext4_clear_inode_state(inode,
1346a7550b30SJaegeuk Kim 					EXT4_STATE_MAY_INLINE_DATA);
1347a3caa24bSJan Kara 			/*
13482ee6a576SEric Biggers 			 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13492ee6a576SEric Biggers 			 * S_DAX may be disabled
1350a3caa24bSJan Kara 			 */
1351a3caa24bSJan Kara 			ext4_set_inode_flags(inode);
1352a7550b30SJaegeuk Kim 		}
1353a7550b30SJaegeuk Kim 		return res;
1354a7550b30SJaegeuk Kim 	}
1355a7550b30SJaegeuk Kim 
1356b8cb5a54STahsin Erdogan 	res = dquot_initialize(inode);
1357b8cb5a54STahsin Erdogan 	if (res)
1358b8cb5a54STahsin Erdogan 		return res;
13592f8f5e76SEric Biggers retry:
1360af65207cSTahsin Erdogan 	res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1361af65207cSTahsin Erdogan 				     &credits);
1362dec214d0STahsin Erdogan 	if (res)
1363dec214d0STahsin Erdogan 		return res;
1364dec214d0STahsin Erdogan 
1365c1a5d5f6STahsin Erdogan 	handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1366a7550b30SJaegeuk Kim 	if (IS_ERR(handle))
1367a7550b30SJaegeuk Kim 		return PTR_ERR(handle);
1368a7550b30SJaegeuk Kim 
13692f8f5e76SEric Biggers 	res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
13702f8f5e76SEric Biggers 				    EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
13712f8f5e76SEric Biggers 				    ctx, len, 0);
1372a7550b30SJaegeuk Kim 	if (!res) {
1373a7550b30SJaegeuk Kim 		ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
13742ee6a576SEric Biggers 		/*
13752ee6a576SEric Biggers 		 * Update inode->i_flags - S_ENCRYPTED will be enabled,
13762ee6a576SEric Biggers 		 * S_DAX may be disabled
13772ee6a576SEric Biggers 		 */
1378a3caa24bSJan Kara 		ext4_set_inode_flags(inode);
1379a7550b30SJaegeuk Kim 		res = ext4_mark_inode_dirty(handle, inode);
1380a7550b30SJaegeuk Kim 		if (res)
1381a7550b30SJaegeuk Kim 			EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1382a7550b30SJaegeuk Kim 	}
1383a7550b30SJaegeuk Kim 	res2 = ext4_journal_stop(handle);
13842f8f5e76SEric Biggers 
13852f8f5e76SEric Biggers 	if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
13862f8f5e76SEric Biggers 		goto retry;
1387a7550b30SJaegeuk Kim 	if (!res)
1388a7550b30SJaegeuk Kim 		res = res2;
1389a7550b30SJaegeuk Kim 	return res;
1390a7550b30SJaegeuk Kim }
1391a7550b30SJaegeuk Kim 
1392c250b7ddSEric Biggers static bool ext4_dummy_context(struct inode *inode)
1393a7550b30SJaegeuk Kim {
1394a7550b30SJaegeuk Kim 	return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
1395a7550b30SJaegeuk Kim }
1396a7550b30SJaegeuk Kim 
1397b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb)
1398b925acb8SEric Biggers {
1399b925acb8SEric Biggers 	return ext4_has_feature_stable_inodes(sb);
1400b925acb8SEric Biggers }
1401b925acb8SEric Biggers 
1402b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1403b925acb8SEric Biggers 				       int *ino_bits_ret, int *lblk_bits_ret)
1404b925acb8SEric Biggers {
1405b925acb8SEric Biggers 	*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1406b925acb8SEric Biggers 	*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1407b925acb8SEric Biggers }
1408b925acb8SEric Biggers 
14096f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = {
1410a5d431efSEric Biggers 	.key_prefix		= "ext4:",
1411a7550b30SJaegeuk Kim 	.get_context		= ext4_get_context,
1412a7550b30SJaegeuk Kim 	.set_context		= ext4_set_context,
1413a7550b30SJaegeuk Kim 	.dummy_context		= ext4_dummy_context,
1414a7550b30SJaegeuk Kim 	.empty_dir		= ext4_empty_dir,
1415e12ee683SEric Biggers 	.max_namelen		= EXT4_NAME_LEN,
1416b925acb8SEric Biggers 	.has_stable_inodes	= ext4_has_stable_inodes,
1417b925acb8SEric Biggers 	.get_ino_and_lblk_bits	= ext4_get_ino_and_lblk_bits,
1418a7550b30SJaegeuk Kim };
1419a7550b30SJaegeuk Kim #endif
1420a7550b30SJaegeuk Kim 
1421ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1422d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES;
1423689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t])
1424ac27a0ecSDave Kleikamp 
1425617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot);
1426617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot);
1427617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot);
1428617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot);
1429617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type);
14306f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id,
14318c54ca9cSAl Viro 			 const struct path *path);
1432617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type);
1433617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1434ac27a0ecSDave Kleikamp 			       size_t len, loff_t off);
1435617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
1436ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off);
14377c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
14387c319d32SAditya Kali 			     unsigned int flags);
14397c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb);
1440ac27a0ecSDave Kleikamp 
144196c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode)
144296c7e0d9SJan Kara {
144396c7e0d9SJan Kara 	return EXT4_I(inode)->i_dquot;
144496c7e0d9SJan Kara }
144596c7e0d9SJan Kara 
144661e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = {
144760e58e0fSMingming Cao 	.get_reserved_space	= ext4_get_reserved_space,
1448617ba13bSMingming Cao 	.write_dquot		= ext4_write_dquot,
1449617ba13bSMingming Cao 	.acquire_dquot		= ext4_acquire_dquot,
1450617ba13bSMingming Cao 	.release_dquot		= ext4_release_dquot,
1451617ba13bSMingming Cao 	.mark_dirty		= ext4_mark_dquot_dirty,
1452a5b5ee32SJan Kara 	.write_info		= ext4_write_info,
1453a5b5ee32SJan Kara 	.alloc_dquot		= dquot_alloc,
1454a5b5ee32SJan Kara 	.destroy_dquot		= dquot_destroy,
1455040cb378SLi Xi 	.get_projid		= ext4_get_projid,
14567a9ca53aSTahsin Erdogan 	.get_inode_usage	= ext4_get_inode_usage,
1457ebc11f7bSChengguang Xu 	.get_next_id		= dquot_get_next_id,
1458ac27a0ecSDave Kleikamp };
1459ac27a0ecSDave Kleikamp 
14600d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = {
1461617ba13bSMingming Cao 	.quota_on	= ext4_quota_on,
1462ca0e05e4SDmitry Monakhov 	.quota_off	= ext4_quota_off,
1463287a8095SChristoph Hellwig 	.quota_sync	= dquot_quota_sync,
14640a240339SJan Kara 	.get_state	= dquot_get_state,
1465287a8095SChristoph Hellwig 	.set_info	= dquot_set_dqinfo,
1466287a8095SChristoph Hellwig 	.get_dqblk	= dquot_get_dqblk,
14676332b9b5SEric Sandeen 	.set_dqblk	= dquot_set_dqblk,
14686332b9b5SEric Sandeen 	.get_nextdqblk	= dquot_get_next_dqblk,
1469ac27a0ecSDave Kleikamp };
1470ac27a0ecSDave Kleikamp #endif
1471ac27a0ecSDave Kleikamp 
1472ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = {
1473617ba13bSMingming Cao 	.alloc_inode	= ext4_alloc_inode,
147494053139SAl Viro 	.free_inode	= ext4_free_in_core_inode,
1475617ba13bSMingming Cao 	.destroy_inode	= ext4_destroy_inode,
1476617ba13bSMingming Cao 	.write_inode	= ext4_write_inode,
1477617ba13bSMingming Cao 	.dirty_inode	= ext4_dirty_inode,
14787ff9c073STheodore Ts'o 	.drop_inode	= ext4_drop_inode,
14790930fcc1SAl Viro 	.evict_inode	= ext4_evict_inode,
1480617ba13bSMingming Cao 	.put_super	= ext4_put_super,
1481617ba13bSMingming Cao 	.sync_fs	= ext4_sync_fs,
1482c4be0c1dSTakashi Sato 	.freeze_fs	= ext4_freeze,
1483c4be0c1dSTakashi Sato 	.unfreeze_fs	= ext4_unfreeze,
1484617ba13bSMingming Cao 	.statfs		= ext4_statfs,
1485617ba13bSMingming Cao 	.remount_fs	= ext4_remount,
1486617ba13bSMingming Cao 	.show_options	= ext4_show_options,
1487ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
1488617ba13bSMingming Cao 	.quota_read	= ext4_quota_read,
1489617ba13bSMingming Cao 	.quota_write	= ext4_quota_write,
149096c7e0d9SJan Kara 	.get_dquots	= ext4_get_dquots,
1491ac27a0ecSDave Kleikamp #endif
1492c39a7f84SToshiyuki Okajima 	.bdev_try_to_free_page = bdev_try_to_free_page,
1493ac27a0ecSDave Kleikamp };
1494ac27a0ecSDave Kleikamp 
149539655164SChristoph Hellwig static const struct export_operations ext4_export_ops = {
14961b961ac0SChristoph Hellwig 	.fh_to_dentry = ext4_fh_to_dentry,
14971b961ac0SChristoph Hellwig 	.fh_to_parent = ext4_fh_to_parent,
1498617ba13bSMingming Cao 	.get_parent = ext4_get_parent,
1499fde87268STheodore Ts'o 	.commit_metadata = ext4_nfs_commit_metadata,
1500ac27a0ecSDave Kleikamp };
1501ac27a0ecSDave Kleikamp 
1502ac27a0ecSDave Kleikamp enum {
1503ac27a0ecSDave Kleikamp 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1504ac27a0ecSDave Kleikamp 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
150572578c33STheodore Ts'o 	Opt_nouid32, Opt_debug, Opt_removed,
1506ac27a0ecSDave Kleikamp 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
150772578c33STheodore Ts'o 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1508ad4eec61SEric Sandeen 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1509ad4eec61SEric Sandeen 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1510ac27a0ecSDave Kleikamp 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
15116ddb2447STheodore Ts'o 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1512ac27a0ecSDave Kleikamp 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
15135a20bdfcSJan Kara 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1514ee4a3fcdSTheodore Ts'o 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
151549da9392SJan Kara 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
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"},
1582c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
158364769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1584327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1585327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1586a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1587a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1588670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1589dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
159036ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
159136ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15926fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15936fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1594240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1595b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1596afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
159706705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
159806705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1599744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1600244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1601744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
16025328e635SEric Sandeen 	{Opt_discard, "discard"},
16035328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1604fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1605fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1606fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1607df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
16086ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1609cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1610cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1611c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1612c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1613c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1614c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1615c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1616f3f12faaSJosef Bacik 	{Opt_err, NULL},
1617ac27a0ecSDave Kleikamp };
1618ac27a0ecSDave Kleikamp 
1619617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1620ac27a0ecSDave Kleikamp {
1621617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1622ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1623ac27a0ecSDave Kleikamp 
1624ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1625ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
16260b8e58a1SAndreas Dilger 
1627ac27a0ecSDave Kleikamp 	options += 3;
16280b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1629ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1630ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
16314776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1632ac27a0ecSDave Kleikamp 		       (char *) *data);
1633ac27a0ecSDave Kleikamp 		return 1;
1634ac27a0ecSDave Kleikamp 	}
1635ac27a0ecSDave Kleikamp 	if (*options == ',')
1636ac27a0ecSDave Kleikamp 		options++;
1637ac27a0ecSDave Kleikamp 	*data = (void *) options;
16380b8e58a1SAndreas Dilger 
1639ac27a0ecSDave Kleikamp 	return sb_block;
1640ac27a0ecSDave Kleikamp }
1641ac27a0ecSDave Kleikamp 
1642b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1643d6006186SEric Biggers static const char deprecated_msg[] =
1644d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1645437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1646b3881f74STheodore Ts'o 
164756c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
164856c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
164956c50f11SDmitry Monakhov {
165056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
165133458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
165203dafb5fSChen Gang 	int ret = -1;
165356c50f11SDmitry Monakhov 
165433458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
165556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
165656c50f11SDmitry Monakhov 			"Cannot change journaled "
165756c50f11SDmitry Monakhov 			"quota options when quota turned on");
165857f73c2cSTheodore Ts'o 		return -1;
165956c50f11SDmitry Monakhov 	}
1660e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1661c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1662c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1663c325a67cSTheodore Ts'o 		return 1;
1664262b4662SJan Kara 	}
166556c50f11SDmitry Monakhov 	qname = match_strdup(args);
166656c50f11SDmitry Monakhov 	if (!qname) {
166756c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
166856c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
166957f73c2cSTheodore Ts'o 		return -1;
167056c50f11SDmitry Monakhov 	}
167133458eabSTheodore Ts'o 	if (old_qname) {
167233458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
167303dafb5fSChen Gang 			ret = 1;
167403dafb5fSChen Gang 		else
167556c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
167603dafb5fSChen Gang 				 "%s quota file already specified",
167703dafb5fSChen Gang 				 QTYPE2NAME(qtype));
167803dafb5fSChen Gang 		goto errout;
167956c50f11SDmitry Monakhov 	}
168003dafb5fSChen Gang 	if (strchr(qname, '/')) {
168156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
168256c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
168303dafb5fSChen Gang 		goto errout;
168456c50f11SDmitry Monakhov 	}
168533458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1686fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
168756c50f11SDmitry Monakhov 	return 1;
168803dafb5fSChen Gang errout:
168903dafb5fSChen Gang 	kfree(qname);
169003dafb5fSChen Gang 	return ret;
169156c50f11SDmitry Monakhov }
169256c50f11SDmitry Monakhov 
169356c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
169456c50f11SDmitry Monakhov {
169556c50f11SDmitry Monakhov 
169656c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
169733458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
169856c50f11SDmitry Monakhov 
169933458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
170056c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
170156c50f11SDmitry Monakhov 			" when quota turned on");
170257f73c2cSTheodore Ts'o 		return -1;
170356c50f11SDmitry Monakhov 	}
170433458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
170533458eabSTheodore Ts'o 	synchronize_rcu();
170633458eabSTheodore Ts'o 	kfree(old_qname);
170756c50f11SDmitry Monakhov 	return 1;
170856c50f11SDmitry Monakhov }
170956c50f11SDmitry Monakhov #endif
171056c50f11SDmitry Monakhov 
171126092bf5STheodore Ts'o #define MOPT_SET	0x0001
171226092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
171326092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
171426092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
171526092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
171626092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
171726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
171826092bf5STheodore Ts'o #define MOPT_Q		0
171926092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
172026092bf5STheodore Ts'o #else
172126092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
172226092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
172326092bf5STheodore Ts'o #endif
172426092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
17258dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
17268dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
17278dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1728ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
172926092bf5STheodore Ts'o 
173026092bf5STheodore Ts'o static const struct mount_opts {
173126092bf5STheodore Ts'o 	int	token;
173226092bf5STheodore Ts'o 	int	mount_opt;
173326092bf5STheodore Ts'o 	int	flags;
173426092bf5STheodore Ts'o } ext4_mount_opts[] = {
173526092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
173626092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
173726092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
173826092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
173926092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
174026092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
17418dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
17428dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
17438dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
17448dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
174526092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
174626092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
17478dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
17488dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17498dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
175059d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1751327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1752327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1753c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1754c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
17558dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
17561e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
175726092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
17588dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
17591e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17608dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
176126092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
176226092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
176326092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17648dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17657915a861SAles Novak 	 MOPT_NO_EXT2},
17668dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17677915a861SAles Novak 	 MOPT_NO_EXT2},
176826092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
176926092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
177026092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
177126092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
177226092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
177326092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
177426092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
177526092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
177626092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
177726092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1778923ae0ffSRoss Zwisler 	{Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
177926092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17800efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17810efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17825ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17835ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17845ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17858dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17868dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17878dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17888dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
178926092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
179026092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
179126092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
179226092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
179326092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
179426092bf5STheodore Ts'o #else
179526092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
179626092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
179726092bf5STheodore Ts'o #endif
179826092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
179926092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1800670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
180126092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
180226092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
180326092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
180426092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
180526092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
180649da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
180749da9392SJan Kara 							MOPT_SET | MOPT_Q},
180826092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
180949da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
181049da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
181126092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
181226092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
181326092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
181426092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
181526092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
181626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
181726092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1818df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
18196ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1820cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
182126092bf5STheodore Ts'o 	{Opt_err, 0, 0}
182226092bf5STheodore Ts'o };
182326092bf5STheodore Ts'o 
1824c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1825c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1826c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1827c83ad55eSGabriel Krisman Bertazi 	char *name;
1828c83ad55eSGabriel Krisman Bertazi 	char *version;
1829c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1830c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1831c83ad55eSGabriel Krisman Bertazi };
1832c83ad55eSGabriel Krisman Bertazi 
1833c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1834c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1835c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1836c83ad55eSGabriel Krisman Bertazi {
1837c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1838c83ad55eSGabriel Krisman Bertazi 	int i;
1839c83ad55eSGabriel Krisman Bertazi 
1840c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1841c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1842c83ad55eSGabriel Krisman Bertazi 			break;
1843c83ad55eSGabriel Krisman Bertazi 
1844c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1845c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1846c83ad55eSGabriel Krisman Bertazi 
1847c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1848c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1849c83ad55eSGabriel Krisman Bertazi 
1850c83ad55eSGabriel Krisman Bertazi 	return 0;
1851c83ad55eSGabriel Krisman Bertazi }
1852c83ad55eSGabriel Krisman Bertazi #endif
1853c83ad55eSGabriel Krisman Bertazi 
185426092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
185526092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
185626092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
185726092bf5STheodore Ts'o {
185826092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
185926092bf5STheodore Ts'o 	const struct mount_opts *m;
186008cefc7aSEric W. Biederman 	kuid_t uid;
186108cefc7aSEric W. Biederman 	kgid_t gid;
186226092bf5STheodore Ts'o 	int arg = 0;
186326092bf5STheodore Ts'o 
186457f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
186557f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
186657f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
186757f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
186857f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
186957f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
187057f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
187157f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
187257f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
187357f73c2cSTheodore Ts'o #endif
187426092bf5STheodore Ts'o 	switch (token) {
1875f7048605STheodore Ts'o 	case Opt_noacl:
1876f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1877f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1878f7048605STheodore Ts'o 		break;
187926092bf5STheodore Ts'o 	case Opt_sb:
188026092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
188126092bf5STheodore Ts'o 	case Opt_removed:
18825f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
188326092bf5STheodore Ts'o 		return 1;
188426092bf5STheodore Ts'o 	case Opt_abort:
188526092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
188626092bf5STheodore Ts'o 		return 1;
188726092bf5STheodore Ts'o 	case Opt_i_version:
1888357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
188926092bf5STheodore Ts'o 		return 1;
1890a26f4992STheodore Ts'o 	case Opt_lazytime:
18911751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1892a26f4992STheodore Ts'o 		return 1;
1893a26f4992STheodore Ts'o 	case Opt_nolazytime:
18941751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1895a26f4992STheodore Ts'o 		return 1;
189626092bf5STheodore Ts'o 	}
189726092bf5STheodore Ts'o 
18985f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
18995f3633e3SJan Kara 		if (token == m->token)
19005f3633e3SJan Kara 			break;
19015f3633e3SJan Kara 
19025f3633e3SJan Kara 	if (m->token == Opt_err) {
19035f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
19045f3633e3SJan Kara 			 "or missing value", opt);
19055f3633e3SJan Kara 		return -1;
19065f3633e3SJan Kara 	}
19075f3633e3SJan Kara 
19088dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
19098dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19108dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
19118dc0aa8cSTheodore Ts'o 		return -1;
19128dc0aa8cSTheodore Ts'o 	}
19138dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
19148dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19158dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
19168dc0aa8cSTheodore Ts'o 		return -1;
19178dc0aa8cSTheodore Ts'o 	}
19188dc0aa8cSTheodore Ts'o 
1919ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
19200efb3b23SJan Kara 		return -1;
192126092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
192226092bf5STheodore Ts'o 		return -1;
1923c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1924c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
192526092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
19261e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
19271e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1928c93cf2d7SDmitry Monakhov 		} else
1929c93cf2d7SDmitry Monakhov 			return -1;
1930c93cf2d7SDmitry Monakhov 	}
193126092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
193226092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
193326092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
193426092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
193526092bf5STheodore Ts'o 			 "options when quota turned on");
193626092bf5STheodore Ts'o 		return -1;
193726092bf5STheodore Ts'o 	}
193826092bf5STheodore Ts'o 
193926092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
194026092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
194126092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
194226092bf5STheodore Ts'o 		if (arg == 0)
194326092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
19449ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
19459ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
19469ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
19479ba55543Szhangyi (F) 				 "must be smaller than %d",
19489ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
19499ba55543Szhangyi (F) 			return -1;
19509ba55543Szhangyi (F) 		}
195126092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1952670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
19539803387cSTheodore Ts'o 		if ((arg & 1) ||
19549803387cSTheodore Ts'o 		    (arg < 4) ||
19559803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
19569803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
19579803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
19589803387cSTheodore Ts'o 			return -1;
19599803387cSTheodore Ts'o 		}
1960670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
196126092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
196226092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
196326092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
196426092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
196526092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1966e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1967e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1968e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1969e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
197026092bf5STheodore Ts'o 			return -1;
197126092bf5STheodore Ts'o 		}
197226092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
197326092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
197426092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
197526092bf5STheodore Ts'o 		if (!args->from)
197626092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
197726092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1978df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1979df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
198026092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
198126092bf5STheodore Ts'o 		sbi->s_stripe = arg;
19820efb3b23SJan Kara 	} else if (token == Opt_resuid) {
19830efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
19840efb3b23SJan Kara 		if (!uid_valid(uid)) {
19855f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
19860efb3b23SJan Kara 			return -1;
19870efb3b23SJan Kara 		}
19880efb3b23SJan Kara 		sbi->s_resuid = uid;
19890efb3b23SJan Kara 	} else if (token == Opt_resgid) {
19900efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
19910efb3b23SJan Kara 		if (!gid_valid(gid)) {
19925f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
19930efb3b23SJan Kara 			return -1;
19940efb3b23SJan Kara 		}
19950efb3b23SJan Kara 		sbi->s_resgid = gid;
19960efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
19970efb3b23SJan Kara 		if (is_remount) {
19980efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
19990efb3b23SJan Kara 				 "Cannot specify journal on remount");
20000efb3b23SJan Kara 			return -1;
20010efb3b23SJan Kara 		}
20020efb3b23SJan Kara 		*journal_devnum = arg;
2003ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2004ad4eec61SEric Sandeen 		char *journal_path;
2005ad4eec61SEric Sandeen 		struct inode *journal_inode;
2006ad4eec61SEric Sandeen 		struct path path;
2007ad4eec61SEric Sandeen 		int error;
2008ad4eec61SEric Sandeen 
2009ad4eec61SEric Sandeen 		if (is_remount) {
2010ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2011ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2012ad4eec61SEric Sandeen 			return -1;
2013ad4eec61SEric Sandeen 		}
2014ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2015ad4eec61SEric Sandeen 		if (!journal_path) {
2016ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2017ad4eec61SEric Sandeen 				"journal device string");
2018ad4eec61SEric Sandeen 			return -1;
2019ad4eec61SEric Sandeen 		}
2020ad4eec61SEric Sandeen 
2021ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2022ad4eec61SEric Sandeen 		if (error) {
2023ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2024ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2025ad4eec61SEric Sandeen 			kfree(journal_path);
2026ad4eec61SEric Sandeen 			return -1;
2027ad4eec61SEric Sandeen 		}
2028ad4eec61SEric Sandeen 
20292b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2030ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2031ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2032ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2033ad4eec61SEric Sandeen 			path_put(&path);
2034ad4eec61SEric Sandeen 			kfree(journal_path);
2035ad4eec61SEric Sandeen 			return -1;
2036ad4eec61SEric Sandeen 		}
2037ad4eec61SEric Sandeen 
2038ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2039ad4eec61SEric Sandeen 		path_put(&path);
2040ad4eec61SEric Sandeen 		kfree(journal_path);
20410efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
20420efb3b23SJan Kara 		if (arg > 7) {
20435f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
20440efb3b23SJan Kara 				 " (must be 0-7)");
20450efb3b23SJan Kara 			return -1;
20460efb3b23SJan Kara 		}
20470efb3b23SJan Kara 		*journal_ioprio =
20480efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
20496ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2050643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
20516ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
20526ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20536ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
20546ddb2447STheodore Ts'o #else
20556ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20566ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
20576ddb2447STheodore Ts'o #endif
205826092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
205926092bf5STheodore Ts'o 		if (is_remount) {
206026092bf5STheodore Ts'o 			if (!sbi->s_journal)
206126092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
20625f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
206326092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
206426092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
206526092bf5STheodore Ts'o 				return -1;
206626092bf5STheodore Ts'o 			}
206726092bf5STheodore Ts'o 		} else {
206826092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
206926092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
207026092bf5STheodore Ts'o 		}
207126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
207226092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
207326092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
207426092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
20755f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
20765f3633e3SJan Kara 				 "quota options when quota turned on");
207726092bf5STheodore Ts'o 			return -1;
207826092bf5STheodore Ts'o 		}
2079e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2080c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2081c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2082262b4662SJan Kara 				 "when QUOTA feature is enabled");
2083c325a67cSTheodore Ts'o 			return 1;
2084262b4662SJan Kara 		}
208526092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
208626092bf5STheodore Ts'o #endif
2087923ae0ffSRoss Zwisler 	} else if (token == Opt_dax) {
2088ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2089ef83b6e8SDan Williams 		ext4_msg(sb, KERN_WARNING,
2090ef83b6e8SDan Williams 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2091ef83b6e8SDan Williams 		sbi->s_mount_opt |= m->mount_opt;
2092ef83b6e8SDan Williams #else
2093923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2094923ae0ffSRoss Zwisler 		return -1;
2095923ae0ffSRoss Zwisler #endif
20967915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
20977915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
20987915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
20997915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
210026092bf5STheodore Ts'o 	} else {
210126092bf5STheodore Ts'o 		if (!args->from)
210226092bf5STheodore Ts'o 			arg = 1;
210326092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
210426092bf5STheodore Ts'o 			arg = !arg;
210526092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
210626092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
210726092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
210826092bf5STheodore Ts'o 			WARN_ON(1);
210926092bf5STheodore Ts'o 			return -1;
211026092bf5STheodore Ts'o 		}
211126092bf5STheodore Ts'o 		if (arg != 0)
211226092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
211326092bf5STheodore Ts'o 		else
211426092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
211526092bf5STheodore Ts'o 	}
211626092bf5STheodore Ts'o 	return 1;
211726092bf5STheodore Ts'o }
211826092bf5STheodore Ts'o 
2119ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2120c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2121b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2122661aa520SEric Sandeen 			 int is_remount)
2123ac27a0ecSDave Kleikamp {
21241e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
212533458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2126ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
212726092bf5STheodore Ts'o 	int token;
2128ac27a0ecSDave Kleikamp 
2129ac27a0ecSDave Kleikamp 	if (!options)
2130ac27a0ecSDave Kleikamp 		return 1;
2131ac27a0ecSDave Kleikamp 
2132ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2133ac27a0ecSDave Kleikamp 		if (!*p)
2134ac27a0ecSDave Kleikamp 			continue;
213515121c18SEric Sandeen 		/*
213615121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
213715121c18SEric Sandeen 		 * found; some options take optional arguments.
213815121c18SEric Sandeen 		 */
2139caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2140ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
214126092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
214226092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2143ac27a0ecSDave Kleikamp 			return 0;
2144ac27a0ecSDave Kleikamp 	}
2145ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
214649da9392SJan Kara 	/*
214749da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
214849da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
214949da9392SJan Kara 	 * to support legacy quotas in quota files.
215049da9392SJan Kara 	 */
215149da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
215249da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
215349da9392SJan Kara 			 "Cannot enable project quota enforcement.");
215449da9392SJan Kara 		return 0;
215549da9392SJan Kara 	}
215633458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
215733458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
215833458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
215933458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2160fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2161ac27a0ecSDave Kleikamp 
216233458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2163fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2164ac27a0ecSDave Kleikamp 
216556c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2166b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2167b31e1552SEric Sandeen 					"format mixing");
2168ac27a0ecSDave Kleikamp 			return 0;
2169ac27a0ecSDave Kleikamp 		}
2170ac27a0ecSDave Kleikamp 
2171ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2172b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2173b31e1552SEric Sandeen 					"not specified");
2174ac27a0ecSDave Kleikamp 			return 0;
2175ac27a0ecSDave Kleikamp 		}
2176ac27a0ecSDave Kleikamp 	}
2177ac27a0ecSDave Kleikamp #endif
2178626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2179626b035bSRitesh Harjani 		int blocksize =
2180626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2181626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2182626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2183626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2184626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2185626b035bSRitesh Harjani 	}
2186ac27a0ecSDave Kleikamp 	return 1;
2187ac27a0ecSDave Kleikamp }
2188ac27a0ecSDave Kleikamp 
21892adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
21902adf6da8STheodore Ts'o 					   struct super_block *sb)
21912adf6da8STheodore Ts'o {
21922adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
21932adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
219433458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
21952adf6da8STheodore Ts'o 
21962adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
21972adf6da8STheodore Ts'o 		char *fmtname = "";
21982adf6da8STheodore Ts'o 
21992adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
22002adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
22012adf6da8STheodore Ts'o 			fmtname = "vfsold";
22022adf6da8STheodore Ts'o 			break;
22032adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
22042adf6da8STheodore Ts'o 			fmtname = "vfsv0";
22052adf6da8STheodore Ts'o 			break;
22062adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
22072adf6da8STheodore Ts'o 			fmtname = "vfsv1";
22082adf6da8STheodore Ts'o 			break;
22092adf6da8STheodore Ts'o 		}
22102adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
22112adf6da8STheodore Ts'o 	}
22122adf6da8STheodore Ts'o 
221333458eabSTheodore Ts'o 	rcu_read_lock();
221433458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
221533458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
221633458eabSTheodore Ts'o 	if (usr_qf_name)
221733458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
221833458eabSTheodore Ts'o 	if (grp_qf_name)
221933458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
222033458eabSTheodore Ts'o 	rcu_read_unlock();
22212adf6da8STheodore Ts'o #endif
22222adf6da8STheodore Ts'o }
22232adf6da8STheodore Ts'o 
22245a916be1STheodore Ts'o static const char *token2str(int token)
22255a916be1STheodore Ts'o {
222650df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
22275a916be1STheodore Ts'o 
22285a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
22295a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
22305a916be1STheodore Ts'o 			break;
22315a916be1STheodore Ts'o 	return t->pattern;
22325a916be1STheodore Ts'o }
22335a916be1STheodore Ts'o 
22342adf6da8STheodore Ts'o /*
22352adf6da8STheodore Ts'o  * Show an option if
22362adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
22372adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
22382adf6da8STheodore Ts'o  */
223966acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
224066acdcf4STheodore Ts'o 			      int nodefs)
22412adf6da8STheodore Ts'o {
22422adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
22432adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
224468afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
22455a916be1STheodore Ts'o 	const struct mount_opts *m;
224666acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
22472adf6da8STheodore Ts'o 
224866acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
224966acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
22502adf6da8STheodore Ts'o 
22512adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
22525a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
22535a916be1STheodore Ts'o 
22545a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
22555a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
22565a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
22575a916be1STheodore Ts'o 		    (m->flags & MOPT_CLEAR_ERR))
22585a916be1STheodore Ts'o 			continue;
225968afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
22605a916be1STheodore Ts'o 			continue; /* skip if same as the default */
22615a916be1STheodore Ts'o 		if ((want_set &&
22625a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
22635a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
22645a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
22655a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
22665a916be1STheodore Ts'o 	}
22675a916be1STheodore Ts'o 
226808cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
22695a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
227008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
227108cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
227208cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
22735a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
227408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
227508cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
227666acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
22775a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
22785a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
22792adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
22805a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
22812adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
22825a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
228366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
22845a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
228566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
22865a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
228766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
22885a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2289357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
22905a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
229166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
22925a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
229368afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
229468afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
22952adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
22965a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
22972adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
22985a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
22992adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
23005a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
23015a916be1STheodore Ts'o 	}
230266acdcf4STheodore Ts'o 	if (nodefs ||
230366acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
23045a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
23052adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
23062adf6da8STheodore Ts'o 
2307ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
230866acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
23095a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2310df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2311df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
23127915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
23137915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2314338affb5SEric Biggers 	if (DUMMY_ENCRYPTION_ENABLED(sbi))
2315338affb5SEric Biggers 		SEQ_OPTS_PUTS("test_dummy_encryption");
23162adf6da8STheodore Ts'o 
23172adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
23182adf6da8STheodore Ts'o 	return 0;
23192adf6da8STheodore Ts'o }
23202adf6da8STheodore Ts'o 
232166acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
232266acdcf4STheodore Ts'o {
232366acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
232466acdcf4STheodore Ts'o }
232566acdcf4STheodore Ts'o 
2326ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
232766acdcf4STheodore Ts'o {
232866acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
232966acdcf4STheodore Ts'o 	int rc;
233066acdcf4STheodore Ts'o 
2331bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
233266acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
233366acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
233466acdcf4STheodore Ts'o 	return rc;
233566acdcf4STheodore Ts'o }
233666acdcf4STheodore Ts'o 
2337617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2338ac27a0ecSDave Kleikamp 			    int read_only)
2339ac27a0ecSDave Kleikamp {
2340617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2341c89128a0SJaegeuk Kim 	int err = 0;
2342ac27a0ecSDave Kleikamp 
2343617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2344b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2345b31e1552SEric Sandeen 			 "forcing read-only mode");
2346c89128a0SJaegeuk Kim 		err = -EROFS;
2347ac27a0ecSDave Kleikamp 	}
2348ac27a0ecSDave Kleikamp 	if (read_only)
2349281b5995STheodore Ts'o 		goto done;
2350617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2351b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2352b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2353c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2354b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2355b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2356b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2357ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2358ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2359ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2360b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2361b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2362b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2363ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
23646a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
23656a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2366b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2367b31e1552SEric Sandeen 			 "warning: checktime reached, "
2368b31e1552SEric Sandeen 			 "running e2fsck is recommended");
23690390131bSFrank Mayhar 	if (!sbi->s_journal)
2370216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2371ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2372617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2373e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
23746a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
23750390131bSFrank Mayhar 	if (sbi->s_journal)
2376e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2377ac27a0ecSDave Kleikamp 
2378c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2379281b5995STheodore Ts'o done:
2380ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2381a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2382a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2383ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2384ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2385617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2386617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2387a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2388ac27a0ecSDave Kleikamp 
23897abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2390c89128a0SJaegeuk Kim 	return err;
2391ac27a0ecSDave Kleikamp }
2392ac27a0ecSDave Kleikamp 
2393117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2394117fff10STheodore Ts'o {
2395117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
23967c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
239737b0b6b8SDan Carpenter 	int size, i, j;
2398117fff10STheodore Ts'o 
2399117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2400117fff10STheodore Ts'o 		return 0;
2401117fff10STheodore Ts'o 
2402117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2403117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2404117fff10STheodore Ts'o 		return 0;
2405117fff10STheodore Ts'o 
24067c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
24077c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2408117fff10STheodore Ts'o 	if (!new_groups) {
24097c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
24107c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2411117fff10STheodore Ts'o 		return -ENOMEM;
2412117fff10STheodore Ts'o 	}
24137c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
24147c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
24157c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
24167c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
24177c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
241837b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
241937b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
24207c990728SSuraj Jitindar Singh 			kvfree(new_groups);
24217c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
24227c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
24237c990728SSuraj Jitindar Singh 			return -ENOMEM;
2424117fff10STheodore Ts'o 		}
24257c990728SSuraj Jitindar Singh 	}
24267c990728SSuraj Jitindar Singh 	rcu_read_lock();
24277c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
24287c990728SSuraj Jitindar Singh 	if (old_groups)
24297c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
24307c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
24317c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
24327c990728SSuraj Jitindar Singh 	rcu_read_unlock();
24337c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
24347c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
24357c990728SSuraj Jitindar Singh 	if (old_groups)
24367c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2437117fff10STheodore Ts'o 	return 0;
2438117fff10STheodore Ts'o }
2439117fff10STheodore Ts'o 
2440772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2441772cb7c8SJose R. Santos {
2442772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2443772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
24447c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2445772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2446117fff10STheodore Ts'o 	int i, err;
2447772cb7c8SJose R. Santos 
2448503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2449d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2450772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2451772cb7c8SJose R. Santos 		return 1;
2452772cb7c8SJose R. Santos 	}
2453772cb7c8SJose R. Santos 
2454117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2455117fff10STheodore Ts'o 	if (err)
2456772cb7c8SJose R. Santos 		goto failed;
2457772cb7c8SJose R. Santos 
2458772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
245988b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2460772cb7c8SJose R. Santos 
2461772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
24627c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
24637c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
246490ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
24657c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
24667c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2467772cb7c8SJose R. Santos 	}
2468772cb7c8SJose R. Santos 
2469772cb7c8SJose R. Santos 	return 1;
2470772cb7c8SJose R. Santos failed:
2471772cb7c8SJose R. Santos 	return 0;
2472772cb7c8SJose R. Santos }
2473772cb7c8SJose R. Santos 
2474e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2475717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2476717d50e4SAndreas Dilger {
2477b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2478717d50e4SAndreas Dilger 	__u16 crc = 0;
2479717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2480e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2481717d50e4SAndreas Dilger 
24829aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2483feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2484feb0ab32SDarrick J. Wong 		__u32 csum32;
2485b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2486feb0ab32SDarrick J. Wong 
2487feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2488feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2489b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2490b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2491b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2492b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2493b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2494b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2495b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2496feb0ab32SDarrick J. Wong 
2497feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2498feb0ab32SDarrick J. Wong 		goto out;
2499feb0ab32SDarrick J. Wong 	}
2500feb0ab32SDarrick J. Wong 
2501feb0ab32SDarrick J. Wong 	/* old crc16 code */
2502e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2503813d32f9SDarrick J. Wong 		return 0;
2504813d32f9SDarrick J. Wong 
2505717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2506717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2507717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2508717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2509717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2510e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2511717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2512717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2513717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2514717d50e4SAndreas Dilger 				offset);
2515717d50e4SAndreas Dilger 
2516feb0ab32SDarrick J. Wong out:
2517717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2518717d50e4SAndreas Dilger }
2519717d50e4SAndreas Dilger 
2520feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2521717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2522717d50e4SAndreas Dilger {
2523feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2524e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2525717d50e4SAndreas Dilger 		return 0;
2526717d50e4SAndreas Dilger 
2527717d50e4SAndreas Dilger 	return 1;
2528717d50e4SAndreas Dilger }
2529717d50e4SAndreas Dilger 
2530feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2531feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2532feb0ab32SDarrick J. Wong {
2533feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2534feb0ab32SDarrick J. Wong 		return;
2535e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2536feb0ab32SDarrick J. Wong }
2537feb0ab32SDarrick J. Wong 
2538ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2539bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2540829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2541bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2542ac27a0ecSDave Kleikamp {
2543617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2544617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2545617ba13bSMingming Cao 	ext4_fsblk_t last_block;
254644de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2547bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2548bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2549bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2550ce421581SJose R. Santos 	int flexbg_flag = 0;
2551bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2552ac27a0ecSDave Kleikamp 
2553e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2554ce421581SJose R. Santos 		flexbg_flag = 1;
2555ce421581SJose R. Santos 
2556617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2557ac27a0ecSDave Kleikamp 
2558197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2559197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2560197cd65aSAkinobu Mita 
2561ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2562bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2563ac27a0ecSDave Kleikamp 		else
2564ac27a0ecSDave Kleikamp 			last_block = first_block +
2565617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2566ac27a0ecSDave Kleikamp 
2567bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2568bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2569bfff6873SLukas Czerner 			grp = i;
2570bfff6873SLukas Czerner 
25718fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2572829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2573829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2574829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2575829fa70dSTheodore Ts'o 				 "superblock", i);
257618db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
257718db4b4eSTheodore Ts'o 				return 0;
2578829fa70dSTheodore Ts'o 		}
257977260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
258077260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
258177260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
258277260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
258377260807STheodore Ts'o 				 "block group descriptors", i);
258477260807STheodore Ts'o 			if (!sb_rdonly(sb))
258577260807STheodore Ts'o 				return 0;
258677260807STheodore Ts'o 		}
25872b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2588b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2589a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2590b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2591ac27a0ecSDave Kleikamp 			return 0;
2592ac27a0ecSDave Kleikamp 		}
25938fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2594829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2595829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2596829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2597829fa70dSTheodore Ts'o 				 "superblock", i);
259818db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
259918db4b4eSTheodore Ts'o 				return 0;
2600829fa70dSTheodore Ts'o 		}
260177260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
260277260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
260377260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
260477260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
260577260807STheodore Ts'o 				 "block group descriptors", i);
260677260807STheodore Ts'o 			if (!sb_rdonly(sb))
260777260807STheodore Ts'o 				return 0;
260877260807STheodore Ts'o 		}
26092b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2610b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2611a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2612b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2613ac27a0ecSDave Kleikamp 			return 0;
2614ac27a0ecSDave Kleikamp 		}
26158fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2616829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2617829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2618829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2619829fa70dSTheodore Ts'o 				 "superblock", i);
262018db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
262118db4b4eSTheodore Ts'o 				return 0;
2622829fa70dSTheodore Ts'o 		}
262377260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
262477260807STheodore Ts'o 		    inode_table <= last_bg_block) {
262577260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
262677260807STheodore Ts'o 				 "Inode table for group %u overlaps "
262777260807STheodore Ts'o 				 "block group descriptors", i);
262877260807STheodore Ts'o 			if (!sb_rdonly(sb))
262977260807STheodore Ts'o 				return 0;
263077260807STheodore Ts'o 		}
2631bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
26322b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2633b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2634a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2635b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2636ac27a0ecSDave Kleikamp 			return 0;
2637ac27a0ecSDave Kleikamp 		}
2638955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2639feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2640b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2641b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2642e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2643fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2644bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2645955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2646717d50e4SAndreas Dilger 				return 0;
2647717d50e4SAndreas Dilger 			}
26487ee1ec4cSLi Zefan 		}
2649955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2650ce421581SJose R. Santos 		if (!flexbg_flag)
2651617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2652ac27a0ecSDave Kleikamp 	}
2653bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2654bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2655ac27a0ecSDave Kleikamp 	return 1;
2656ac27a0ecSDave Kleikamp }
2657ac27a0ecSDave Kleikamp 
2658617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2659ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2660ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2661ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2662ac27a0ecSDave Kleikamp  *
2663ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2664ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2665ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2666ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2667ac27a0ecSDave Kleikamp  *
2668ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2669ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2670ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2671617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2672ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2673ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2674ac27a0ecSDave Kleikamp  */
2675617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2676617ba13bSMingming Cao 				struct ext4_super_block *es)
2677ac27a0ecSDave Kleikamp {
2678ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
26792c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2680ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
268195f1fda4Szhangyi (F) 	int quota_update = 0;
2682ac27a0ecSDave Kleikamp 	int i;
2683ac27a0ecSDave Kleikamp #endif
2684ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2685ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2686ac27a0ecSDave Kleikamp 		return;
2687ac27a0ecSDave Kleikamp 	}
2688ac27a0ecSDave Kleikamp 
2689a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2690b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2691b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2692a8f48a95SEric Sandeen 		return;
2693a8f48a95SEric Sandeen 	}
2694a8f48a95SEric Sandeen 
2695d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2696d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2697d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2698d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2699d39195c3SAmir Goldstein 		return;
2700d39195c3SAmir Goldstein 	}
2701d39195c3SAmir Goldstein 
2702617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2703c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
27041751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
270584474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2706ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2707ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2708c25f9bc6SEric Sandeen 		}
2709ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2710ac27a0ecSDave Kleikamp 		return;
2711ac27a0ecSDave Kleikamp 	}
2712ac27a0ecSDave Kleikamp 
27131751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2714b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
27151751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2716ac27a0ecSDave Kleikamp 	}
2717ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2718ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
27191751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
272095f1fda4Szhangyi (F) 
272195f1fda4Szhangyi (F) 	/*
272295f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
272395f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
272495f1fda4Szhangyi (F) 	 */
27251751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
272695f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
272795f1fda4Szhangyi (F) 
272895f1fda4Szhangyi (F) 		if (!ret)
272995f1fda4Szhangyi (F) 			quota_update = 1;
273095f1fda4Szhangyi (F) 		else
273195f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
273295f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
273395f1fda4Szhangyi (F) 	}
273495f1fda4Szhangyi (F) 
273595f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2736a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2737617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2738617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
273995f1fda4Szhangyi (F) 
274095f1fda4Szhangyi (F) 			if (!ret)
274195f1fda4Szhangyi (F) 				quota_update = 1;
274295f1fda4Szhangyi (F) 			else
2743b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2744b31e1552SEric Sandeen 					"Cannot turn on journaled "
274595f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2746ac27a0ecSDave Kleikamp 		}
2747ac27a0ecSDave Kleikamp 	}
2748ac27a0ecSDave Kleikamp #endif
2749ac27a0ecSDave Kleikamp 
2750ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2751ac27a0ecSDave Kleikamp 		struct inode *inode;
2752ac27a0ecSDave Kleikamp 
2753c65d5c6cSVegard Nossum 		/*
2754c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2755c65d5c6cSVegard Nossum 		 * so, skip the rest.
2756c65d5c6cSVegard Nossum 		 */
2757c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2758c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2759c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2760c65d5c6cSVegard Nossum 			break;
2761c65d5c6cSVegard Nossum 		}
2762c65d5c6cSVegard Nossum 
276397bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
276497bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2765ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2766ac27a0ecSDave Kleikamp 			break;
2767ac27a0ecSDave Kleikamp 		}
2768ac27a0ecSDave Kleikamp 
2769617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2770871a2931SChristoph Hellwig 		dquot_initialize(inode);
2771ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2772566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2773b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2774b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
277546e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2776e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2777ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
27785955102cSAl Viro 			inode_lock(inode);
277955f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
27802c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
27812c98eb5eSTheodore Ts'o 			if (ret)
27822c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
27835955102cSAl Viro 			inode_unlock(inode);
2784ac27a0ecSDave Kleikamp 			nr_truncates++;
2785ac27a0ecSDave Kleikamp 		} else {
2786566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2787b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2788b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
278946e665e9SHarvey Harrison 					__func__, inode->i_ino);
2790ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2791ac27a0ecSDave Kleikamp 				  inode->i_ino);
2792ac27a0ecSDave Kleikamp 			nr_orphans++;
2793ac27a0ecSDave Kleikamp 		}
2794ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2795ac27a0ecSDave Kleikamp 	}
2796ac27a0ecSDave Kleikamp 
2797ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2798ac27a0ecSDave Kleikamp 
2799ac27a0ecSDave Kleikamp 	if (nr_orphans)
2800b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2801b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2802ac27a0ecSDave Kleikamp 	if (nr_truncates)
2803b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2804b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2805ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
280695f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
280795f1fda4Szhangyi (F) 	if (quota_update) {
2808a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
280995f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2810287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2811ac27a0ecSDave Kleikamp 		}
281295f1fda4Szhangyi (F) 	}
2813ac27a0ecSDave Kleikamp #endif
28141751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2815ac27a0ecSDave Kleikamp }
28160b8e58a1SAndreas Dilger 
2817cd2291a4SEric Sandeen /*
2818cd2291a4SEric Sandeen  * Maximal extent format file size.
2819cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2820cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2821cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2822cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2823cd2291a4SEric Sandeen  *
2824f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2825f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2826f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2827f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2828f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2829f17722f9SLukas Czerner  *
2830cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2831cd2291a4SEric Sandeen  */
2832f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2833cd2291a4SEric Sandeen {
2834cd2291a4SEric Sandeen 	loff_t res;
2835cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2836cd2291a4SEric Sandeen 
283772deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
283872deb455SChristoph Hellwig 
283972deb455SChristoph Hellwig 	if (!has_huge_files) {
2840cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2841cd2291a4SEric Sandeen 
2842cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2843cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2844cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2845cd2291a4SEric Sandeen 	}
2846cd2291a4SEric Sandeen 
2847f17722f9SLukas Czerner 	/*
2848f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2849f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2850f17722f9SLukas Czerner 	 * size
2851f17722f9SLukas Czerner 	 */
2852f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2853cd2291a4SEric Sandeen 	res <<= blkbits;
2854cd2291a4SEric Sandeen 
2855cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2856cd2291a4SEric Sandeen 	if (res > upper_limit)
2857cd2291a4SEric Sandeen 		res = upper_limit;
2858cd2291a4SEric Sandeen 
2859cd2291a4SEric Sandeen 	return res;
2860cd2291a4SEric Sandeen }
2861ac27a0ecSDave Kleikamp 
2862ac27a0ecSDave Kleikamp /*
2863cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
28640fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
28650fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2866ac27a0ecSDave Kleikamp  */
2867f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2868ac27a0ecSDave Kleikamp {
2869617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
28700fc1b451SAneesh Kumar K.V 	int meta_blocks;
28710fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
28720b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
28730b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
28740b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
28750b8e58a1SAndreas Dilger 	 *
28760b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
28770b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
28780fc1b451SAneesh Kumar K.V 	 */
28790fc1b451SAneesh Kumar K.V 
288072deb455SChristoph Hellwig 	if (!has_huge_files) {
28810fc1b451SAneesh Kumar K.V 		/*
288272deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
288372deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
288472deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
28850fc1b451SAneesh Kumar K.V 		 */
28860fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
28870fc1b451SAneesh Kumar K.V 
28880fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
28890fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
28900fc1b451SAneesh Kumar K.V 
28910fc1b451SAneesh Kumar K.V 	} else {
28928180a562SAneesh Kumar K.V 		/*
28938180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
28948180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
28958180a562SAneesh Kumar K.V 		 * represent total number of blocks in
28968180a562SAneesh Kumar K.V 		 * file system block size
28978180a562SAneesh Kumar K.V 		 */
28980fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
28990fc1b451SAneesh Kumar K.V 
29000fc1b451SAneesh Kumar K.V 	}
29010fc1b451SAneesh Kumar K.V 
29020fc1b451SAneesh Kumar K.V 	/* indirect blocks */
29030fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
29040fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
29050fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
29060fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
29070fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
29080fc1b451SAneesh Kumar K.V 
29090fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
29100fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2911ac27a0ecSDave Kleikamp 
2912ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2913ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2914ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2915ac27a0ecSDave Kleikamp 	res <<= bits;
2916ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2917ac27a0ecSDave Kleikamp 		res = upper_limit;
29180fc1b451SAneesh Kumar K.V 
29190fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
29200fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
29210fc1b451SAneesh Kumar K.V 
2922ac27a0ecSDave Kleikamp 	return res;
2923ac27a0ecSDave Kleikamp }
2924ac27a0ecSDave Kleikamp 
2925617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
292670bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2927ac27a0ecSDave Kleikamp {
2928617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2929fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2930ac27a0ecSDave Kleikamp 	int has_super = 0;
2931ac27a0ecSDave Kleikamp 
2932ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2933ac27a0ecSDave Kleikamp 
2934e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
293570bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2936ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2937617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2938ac27a0ecSDave Kleikamp 		has_super = 1;
29390b8e58a1SAndreas Dilger 
2940bd63f6b0SDarrick J. Wong 	/*
2941bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2942bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2943bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2944bd63f6b0SDarrick J. Wong 	 * compensate.
2945bd63f6b0SDarrick J. Wong 	 */
2946bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
294749598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
2948bd63f6b0SDarrick J. Wong 		has_super++;
2949bd63f6b0SDarrick J. Wong 
2950617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2951ac27a0ecSDave Kleikamp }
2952ac27a0ecSDave Kleikamp 
2953c9de560dSAlex Tomas /**
2954c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2955c9de560dSAlex Tomas  * @sbi: In memory super block info
2956c9de560dSAlex Tomas  *
2957c9de560dSAlex Tomas  * If we have specified it via mount option, then
2958c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
2959c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
2960c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
2961c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
2962c9de560dSAlex Tomas  *
2963c9de560dSAlex Tomas  */
2964c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2965c9de560dSAlex Tomas {
2966c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2967c9de560dSAlex Tomas 	unsigned long stripe_width =
2968c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
29693eb08658SDan Ehrenberg 	int ret;
2970c9de560dSAlex Tomas 
2971c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
29723eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
29735469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
29743eb08658SDan Ehrenberg 		ret = stripe_width;
29755469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
29763eb08658SDan Ehrenberg 		ret = stride;
29773eb08658SDan Ehrenberg 	else
29783eb08658SDan Ehrenberg 		ret = 0;
2979c9de560dSAlex Tomas 
29803eb08658SDan Ehrenberg 	/*
29813eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
29823eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
29833eb08658SDan Ehrenberg 	 */
29843eb08658SDan Ehrenberg 	if (ret <= 1)
29853eb08658SDan Ehrenberg 		ret = 0;
2986c9de560dSAlex Tomas 
29873eb08658SDan Ehrenberg 	return ret;
2988c9de560dSAlex Tomas }
2989ac27a0ecSDave Kleikamp 
2990a13fb1a4SEric Sandeen /*
2991a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
2992a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
2993a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
2994a13fb1a4SEric Sandeen  * 0 if it cannot be.
2995a13fb1a4SEric Sandeen  */
2996a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2997a13fb1a4SEric Sandeen {
2998e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
2999a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3000a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3001a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3002a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3003a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3004a13fb1a4SEric Sandeen 		return 0;
3005a13fb1a4SEric Sandeen 	}
3006a13fb1a4SEric Sandeen 
3007c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3008c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3009c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3010c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3011c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3012c83ad55eSGabriel Krisman Bertazi 		return 0;
3013c83ad55eSGabriel Krisman Bertazi 	}
3014c83ad55eSGabriel Krisman Bertazi #endif
3015c83ad55eSGabriel Krisman Bertazi 
3016a13fb1a4SEric Sandeen 	if (readonly)
3017a13fb1a4SEric Sandeen 		return 1;
3018a13fb1a4SEric Sandeen 
3019e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
30202cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
30211751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
30222cb5cc8bSDarrick J. Wong 		return 1;
30232cb5cc8bSDarrick J. Wong 	}
30242cb5cc8bSDarrick J. Wong 
3025a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3026e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3027a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3028a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3029a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3030a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3031a13fb1a4SEric Sandeen 		return 0;
3032a13fb1a4SEric Sandeen 	}
3033e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3034bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3035bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3036bab08ab9STheodore Ts'o 			 "extents feature\n");
3037bab08ab9STheodore Ts'o 		return 0;
3038bab08ab9STheodore Ts'o 	}
30397c319d32SAditya Kali 
30409db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3041d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3042d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
30437c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3044d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3045689c958cSLi Xi 		return 0;
3046689c958cSLi Xi 	}
30477c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3048a13fb1a4SEric Sandeen 	return 1;
3049a13fb1a4SEric Sandeen }
3050a13fb1a4SEric Sandeen 
305166e61a9eSTheodore Ts'o /*
305266e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
305366e61a9eSTheodore Ts'o  * on the file system
305466e61a9eSTheodore Ts'o  */
3055235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
305666e61a9eSTheodore Ts'o {
3057235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3058235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3059235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
306066e61a9eSTheodore Ts'o 
306166e61a9eSTheodore Ts'o 	if (es->s_error_count)
3062ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3063ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
306466e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
306566e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
30666a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
30676a0678a7SArnd Bergmann 		       sb->s_id,
30686a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
306966e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
307066e61a9eSTheodore Ts'o 		       es->s_first_error_func,
307166e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
307266e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3073651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
307466e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
307566e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3076651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
307766e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3078651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
307966e61a9eSTheodore Ts'o 	}
308066e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
30816a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
30826a0678a7SArnd Bergmann 		       sb->s_id,
30836a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
308466e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
308566e61a9eSTheodore Ts'o 		       es->s_last_error_func,
308666e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
308766e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3088651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
308966e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
309066e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3091651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
309266e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3093651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
309466e61a9eSTheodore Ts'o 	}
309566e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
309666e61a9eSTheodore Ts'o }
309766e61a9eSTheodore Ts'o 
3098bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3099bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3100bfff6873SLukas Czerner {
3101bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3102bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3103bfff6873SLukas Czerner 	struct super_block *sb;
3104bfff6873SLukas Czerner 	unsigned long timeout = 0;
3105bfff6873SLukas Czerner 	int ret = 0;
3106bfff6873SLukas Czerner 
3107bfff6873SLukas Czerner 	sb = elr->lr_super;
3108bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3109bfff6873SLukas Czerner 
3110bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3111bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3112bfff6873SLukas Czerner 		if (!gdp) {
3113bfff6873SLukas Czerner 			ret = 1;
3114bfff6873SLukas Czerner 			break;
3115bfff6873SLukas Czerner 		}
3116bfff6873SLukas Czerner 
3117bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3118bfff6873SLukas Czerner 			break;
3119bfff6873SLukas Czerner 	}
3120bfff6873SLukas Czerner 
31217f511862STheodore Ts'o 	if (group >= ngroups)
3122bfff6873SLukas Czerner 		ret = 1;
3123bfff6873SLukas Czerner 
3124bfff6873SLukas Czerner 	if (!ret) {
3125bfff6873SLukas Czerner 		timeout = jiffies;
3126bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3127bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3128bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
312951ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
313051ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3131bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3132bfff6873SLukas Czerner 		}
3133bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3134bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3135bfff6873SLukas Czerner 	}
3136bfff6873SLukas Czerner 	return ret;
3137bfff6873SLukas Czerner }
3138bfff6873SLukas Czerner 
3139bfff6873SLukas Czerner /*
3140bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
31414ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3142bfff6873SLukas Czerner  */
3143bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3144bfff6873SLukas Czerner {
3145bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3146bfff6873SLukas Czerner 
3147bfff6873SLukas Czerner 	if (!elr)
3148bfff6873SLukas Czerner 		return;
3149bfff6873SLukas Czerner 
3150bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3151bfff6873SLukas Czerner 
3152bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3153bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3154bfff6873SLukas Czerner 	kfree(elr);
3155bfff6873SLukas Czerner }
3156bfff6873SLukas Czerner 
3157bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3158bfff6873SLukas Czerner {
31591bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
31601bb933fbSLukas Czerner 	if (!ext4_li_info) {
31611bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3162bfff6873SLukas Czerner 		return;
31631bb933fbSLukas Czerner 	}
3164bfff6873SLukas Czerner 
3165bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
31661bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3167bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
31681bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3169bfff6873SLukas Czerner }
3170bfff6873SLukas Czerner 
31718f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
31728f1f7453SEric Sandeen 
3173bfff6873SLukas Czerner /*
3174bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3175bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3176bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3177bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3178bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3179bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3180bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3181bfff6873SLukas Czerner  */
3182bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3183bfff6873SLukas Czerner {
3184bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3185bfff6873SLukas Czerner 	struct list_head *pos, *n;
3186bfff6873SLukas Czerner 	struct ext4_li_request *elr;
31874ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3188bfff6873SLukas Czerner 
3189bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3190bfff6873SLukas Czerner 
3191bfff6873SLukas Czerner cont_thread:
3192bfff6873SLukas Czerner 	while (true) {
3193bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3194bfff6873SLukas Czerner 
3195bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3196bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3197bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3198bfff6873SLukas Czerner 			goto exit_thread;
3199bfff6873SLukas Czerner 		}
3200bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3201e22834f0SDmitry Monakhov 			int err = 0;
3202e22834f0SDmitry Monakhov 			int progress = 0;
3203bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3204bfff6873SLukas Czerner 					 lr_request);
3205bfff6873SLukas Czerner 
3206e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3207e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3208e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3209e22834f0SDmitry Monakhov 				continue;
3210e22834f0SDmitry Monakhov 			}
3211e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3212e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3213e22834f0SDmitry Monakhov 					progress = 1;
3214e22834f0SDmitry Monakhov 					/*
3215e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3216e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3217e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3218e22834f0SDmitry Monakhov 					 */
3219e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3220e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3221e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3222e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3223e22834f0SDmitry Monakhov 					n = pos->next;
3224e22834f0SDmitry Monakhov 				}
3225e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3226e22834f0SDmitry Monakhov 			}
3227b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3228e22834f0SDmitry Monakhov 			if (err) {
3229bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3230bfff6873SLukas Czerner 				continue;
3231bfff6873SLukas Czerner 			}
3232e22834f0SDmitry Monakhov 			if (!progress) {
3233e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3234e22834f0SDmitry Monakhov 					(prandom_u32()
3235e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3236b2c78cd0STheodore Ts'o 			}
3237bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3238bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3239bfff6873SLukas Czerner 		}
3240bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3241bfff6873SLukas Czerner 
3242a0acae0eSTejun Heo 		try_to_freeze();
3243bfff6873SLukas Czerner 
32444ed5c033SLukas Czerner 		cur = jiffies;
32454ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3246f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3247bfff6873SLukas Czerner 			cond_resched();
3248bfff6873SLukas Czerner 			continue;
3249bfff6873SLukas Czerner 		}
3250bfff6873SLukas Czerner 
32514ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
32524ed5c033SLukas Czerner 
32538f1f7453SEric Sandeen 		if (kthread_should_stop()) {
32548f1f7453SEric Sandeen 			ext4_clear_request_list();
32558f1f7453SEric Sandeen 			goto exit_thread;
32568f1f7453SEric Sandeen 		}
3257bfff6873SLukas Czerner 	}
3258bfff6873SLukas Czerner 
3259bfff6873SLukas Czerner exit_thread:
3260bfff6873SLukas Czerner 	/*
3261bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3262bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3263bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3264bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3265bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3266bfff6873SLukas Czerner 	 * new one.
3267bfff6873SLukas Czerner 	 */
3268bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3269bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3270bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3271bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3272bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3273bfff6873SLukas Czerner 		goto cont_thread;
3274bfff6873SLukas Czerner 	}
3275bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3276bfff6873SLukas Czerner 	kfree(ext4_li_info);
3277bfff6873SLukas Czerner 	ext4_li_info = NULL;
3278bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3279bfff6873SLukas Czerner 
3280bfff6873SLukas Czerner 	return 0;
3281bfff6873SLukas Czerner }
3282bfff6873SLukas Czerner 
3283bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3284bfff6873SLukas Czerner {
3285bfff6873SLukas Czerner 	struct list_head *pos, *n;
3286bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3287bfff6873SLukas Czerner 
3288bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3289bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3290bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3291bfff6873SLukas Czerner 				 lr_request);
3292bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3293bfff6873SLukas Czerner 	}
3294bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3295bfff6873SLukas Czerner }
3296bfff6873SLukas Czerner 
3297bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3298bfff6873SLukas Czerner {
32998f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
33008f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
33018f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
33028f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3303bfff6873SLukas Czerner 		ext4_clear_request_list();
3304bfff6873SLukas Czerner 		kfree(ext4_li_info);
3305bfff6873SLukas Czerner 		ext4_li_info = NULL;
330692b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3307bfff6873SLukas Czerner 				 "initialization thread\n",
3308bfff6873SLukas Czerner 				 err);
3309bfff6873SLukas Czerner 		return err;
3310bfff6873SLukas Czerner 	}
3311bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3312bfff6873SLukas Czerner 	return 0;
3313bfff6873SLukas Czerner }
3314bfff6873SLukas Czerner 
3315bfff6873SLukas Czerner /*
3316bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3317bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3318bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3319bfff6873SLukas Czerner  * groups and return total number of groups.
3320bfff6873SLukas Czerner  */
3321bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3322bfff6873SLukas Czerner {
3323bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3324bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3325bfff6873SLukas Czerner 
33268844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
33278844618dSTheodore Ts'o 		return ngroups;
33288844618dSTheodore Ts'o 
3329bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3330bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3331bfff6873SLukas Czerner 		if (!gdp)
3332bfff6873SLukas Czerner 			continue;
3333bfff6873SLukas Czerner 
333450122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3335bfff6873SLukas Czerner 			break;
3336bfff6873SLukas Czerner 	}
3337bfff6873SLukas Czerner 
3338bfff6873SLukas Czerner 	return group;
3339bfff6873SLukas Czerner }
3340bfff6873SLukas Czerner 
3341bfff6873SLukas Czerner static int ext4_li_info_new(void)
3342bfff6873SLukas Czerner {
3343bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3344bfff6873SLukas Czerner 
3345bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3346bfff6873SLukas Czerner 	if (!eli)
3347bfff6873SLukas Czerner 		return -ENOMEM;
3348bfff6873SLukas Czerner 
3349bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3350bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3351bfff6873SLukas Czerner 
3352bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3353bfff6873SLukas Czerner 
3354bfff6873SLukas Czerner 	ext4_li_info = eli;
3355bfff6873SLukas Czerner 
3356bfff6873SLukas Czerner 	return 0;
3357bfff6873SLukas Czerner }
3358bfff6873SLukas Czerner 
3359bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3360bfff6873SLukas Czerner 					    ext4_group_t start)
3361bfff6873SLukas Czerner {
3362bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3363bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3364bfff6873SLukas Czerner 
3365bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3366bfff6873SLukas Czerner 	if (!elr)
3367bfff6873SLukas Czerner 		return NULL;
3368bfff6873SLukas Czerner 
3369bfff6873SLukas Czerner 	elr->lr_super = sb;
3370bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3371bfff6873SLukas Czerner 	elr->lr_next_group = start;
3372bfff6873SLukas Czerner 
3373bfff6873SLukas Czerner 	/*
3374bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3375bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3376bfff6873SLukas Czerner 	 * better.
3377bfff6873SLukas Czerner 	 */
3378dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3379dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3380bfff6873SLukas Czerner 	return elr;
3381bfff6873SLukas Czerner }
3382bfff6873SLukas Czerner 
33837f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3384bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3385bfff6873SLukas Czerner {
3386bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
33877f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
338849598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
33896c5a6cb9SAndrew Morton 	int ret = 0;
3390bfff6873SLukas Czerner 
33917f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
339251ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
339351ce6511SLukas Czerner 		/*
339451ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
339551ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
339651ce6511SLukas Czerner 		 */
339751ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
33987f511862STheodore Ts'o 		goto out;
339951ce6511SLukas Czerner 	}
3400bfff6873SLukas Czerner 
3401bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
340255ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
34037f511862STheodore Ts'o 		goto out;
3404bfff6873SLukas Czerner 
3405bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
34067f511862STheodore Ts'o 	if (!elr) {
34077f511862STheodore Ts'o 		ret = -ENOMEM;
34087f511862STheodore Ts'o 		goto out;
34097f511862STheodore Ts'o 	}
3410bfff6873SLukas Czerner 
3411bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3412bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3413bfff6873SLukas Czerner 		if (ret)
3414bfff6873SLukas Czerner 			goto out;
3415bfff6873SLukas Czerner 	}
3416bfff6873SLukas Czerner 
3417bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3418bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3419bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3420bfff6873SLukas Czerner 
3421bfff6873SLukas Czerner 	sbi->s_li_request = elr;
342246e4690bSTao Ma 	/*
342346e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
342446e4690bSTao Ma 	 * the request_list and the removal and free of it is
342546e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
342646e4690bSTao Ma 	 */
342746e4690bSTao Ma 	elr = NULL;
3428bfff6873SLukas Czerner 
3429bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3430bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3431bfff6873SLukas Czerner 		if (ret)
3432bfff6873SLukas Czerner 			goto out;
3433bfff6873SLukas Czerner 	}
3434bfff6873SLukas Czerner out:
3435bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3436beed5ecbSNicolas Kaiser 	if (ret)
3437bfff6873SLukas Czerner 		kfree(elr);
3438bfff6873SLukas Czerner 	return ret;
3439bfff6873SLukas Czerner }
3440bfff6873SLukas Czerner 
3441bfff6873SLukas Czerner /*
3442bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3443bfff6873SLukas Czerner  * module unload.
3444bfff6873SLukas Czerner  */
3445bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3446bfff6873SLukas Czerner {
3447bfff6873SLukas Czerner 	/*
3448bfff6873SLukas Czerner 	 * If thread exited earlier
3449bfff6873SLukas Czerner 	 * there's nothing to be done.
3450bfff6873SLukas Czerner 	 */
34518f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3452bfff6873SLukas Czerner 		return;
3453bfff6873SLukas Czerner 
34548f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3455bfff6873SLukas Czerner }
3456bfff6873SLukas Czerner 
345725ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
345825ed6e8aSDarrick J. Wong {
345925ed6e8aSDarrick J. Wong 	int ret = 1;
346025ed6e8aSDarrick J. Wong 	int compat, incompat;
346125ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
346225ed6e8aSDarrick J. Wong 
34639aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3464db9ee220SDarrick J. Wong 		/* journal checksum v3 */
346525ed6e8aSDarrick J. Wong 		compat = 0;
3466db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
346725ed6e8aSDarrick J. Wong 	} else {
346825ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
346925ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
347025ed6e8aSDarrick J. Wong 		incompat = 0;
347125ed6e8aSDarrick J. Wong 	}
347225ed6e8aSDarrick J. Wong 
3473feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3474feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3475feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3476feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
347725ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
347825ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
347925ed6e8aSDarrick J. Wong 				compat, 0,
348025ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
348125ed6e8aSDarrick J. Wong 				incompat);
348225ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
348325ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
348425ed6e8aSDarrick J. Wong 				compat, 0,
348525ed6e8aSDarrick J. Wong 				incompat);
348625ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
348725ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
348825ed6e8aSDarrick J. Wong 	} else {
3489feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3490feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
349125ed6e8aSDarrick J. Wong 	}
349225ed6e8aSDarrick J. Wong 
349325ed6e8aSDarrick J. Wong 	return ret;
349425ed6e8aSDarrick J. Wong }
349525ed6e8aSDarrick J. Wong 
3496952fc18eSTheodore Ts'o /*
3497952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3498952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3499952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3500952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3501952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3502952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3503952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3504952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3505952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3506952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3507952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3508952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3509952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3510952fc18eSTheodore Ts'o  */
3511952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3512952fc18eSTheodore Ts'o 			  char *buf)
3513952fc18eSTheodore Ts'o {
3514952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3515952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3516952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3517952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3518952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3519952fc18eSTheodore Ts'o 
3520e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
35210548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
35220548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
35230548bbb8STheodore Ts'o 
3524952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3525952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3526952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3527952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3528952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3529952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3530952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3531952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3532952fc18eSTheodore Ts'o 			count++;
3533952fc18eSTheodore Ts'o 		}
3534952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3535952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3536952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3537952fc18eSTheodore Ts'o 			count++;
3538952fc18eSTheodore Ts'o 		}
3539952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3540952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3541952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3542952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3543952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3544952fc18eSTheodore Ts'o 				count++;
3545952fc18eSTheodore Ts'o 			}
3546952fc18eSTheodore Ts'o 		if (i != grp)
3547952fc18eSTheodore Ts'o 			continue;
3548952fc18eSTheodore Ts'o 		s = 0;
3549952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3550952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3551952fc18eSTheodore Ts'o 			count++;
3552952fc18eSTheodore Ts'o 		}
3553c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3554c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3555c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3556c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3557c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3558952fc18eSTheodore Ts'o 		}
3559c48ae41bSTheodore Ts'o 		count += j;
3560c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3561c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3562952fc18eSTheodore Ts'o 	}
3563952fc18eSTheodore Ts'o 	if (!count)
3564952fc18eSTheodore Ts'o 		return 0;
3565952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3566952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3567952fc18eSTheodore Ts'o }
3568952fc18eSTheodore Ts'o 
3569952fc18eSTheodore Ts'o /*
3570952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3571952fc18eSTheodore Ts'o  */
3572952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3573952fc18eSTheodore Ts'o {
3574952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3575952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
35763c816dedSEric Whitney 	struct inode *j_inode;
35773c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3578952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3579952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
35804fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3581952fc18eSTheodore Ts'o 
3582952fc18eSTheodore Ts'o 	if (!buf)
3583952fc18eSTheodore Ts'o 		return -ENOMEM;
3584952fc18eSTheodore Ts'o 
3585952fc18eSTheodore Ts'o 	/*
3586952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3587952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3588952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3589952fc18eSTheodore Ts'o 	 */
3590952fc18eSTheodore Ts'o 
3591952fc18eSTheodore Ts'o 	/*
3592952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3593952fc18eSTheodore Ts'o 	 */
3594952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3595952fc18eSTheodore Ts'o 
3596952fc18eSTheodore Ts'o 	/*
3597952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3598952fc18eSTheodore Ts'o 	 */
3599952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3600952fc18eSTheodore Ts'o 		int blks;
3601952fc18eSTheodore Ts'o 
3602952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3603952fc18eSTheodore Ts'o 		overhead += blks;
3604952fc18eSTheodore Ts'o 		if (blks)
3605952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3606952fc18eSTheodore Ts'o 		cond_resched();
3607952fc18eSTheodore Ts'o 	}
36083c816dedSEric Whitney 
36093c816dedSEric Whitney 	/*
36103c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
36113c816dedSEric Whitney 	 * loaded or not
36123c816dedSEric Whitney 	 */
3613b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3614810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
3615f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3616f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
36173c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
36183c816dedSEric Whitney 		if (j_inode) {
36193c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
36203c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
36213c816dedSEric Whitney 			iput(j_inode);
36223c816dedSEric Whitney 		} else {
36233c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
36243c816dedSEric Whitney 		}
36253c816dedSEric Whitney 	}
3626952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3627952fc18eSTheodore Ts'o 	smp_wmb();
3628952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3629952fc18eSTheodore Ts'o 	return 0;
3630952fc18eSTheodore Ts'o }
3631952fc18eSTheodore Ts'o 
3632b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
363327dd4385SLukas Czerner {
363427dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3635b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
363627dd4385SLukas Czerner 
363727dd4385SLukas Czerner 	/*
363830fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
363930fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
364030fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
364130fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
364230fac0f7SJan Kara 	 */
3643e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3644b5799018STheodore Ts'o 		return;
364530fac0f7SJan Kara 	/*
364627dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
364727dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
364827dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3649556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
365027dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
365127dd4385SLukas Czerner 	 * very rare.
365227dd4385SLukas Czerner 	 */
3653b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3654b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
365527dd4385SLukas Czerner 
365627dd4385SLukas Czerner 	do_div(resv_clusters, 50);
365727dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
365827dd4385SLukas Czerner 
3659b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
366027dd4385SLukas Czerner }
366127dd4385SLukas Czerner 
3662617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3663ac27a0ecSDave Kleikamp {
36645e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3665d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
36661d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
3667617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
36685aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
36697c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
3670617ba13bSMingming Cao 	ext4_fsblk_t block;
3671617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
367270bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3673ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3674ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3675ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3676ac27a0ecSDave Kleikamp 	struct inode *root;
36770390131bSFrank Mayhar 	const char *descr;
3678dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3679281b5995STheodore Ts'o 	int blocksize, clustersize;
36804ec11028STheodore Ts'o 	unsigned int db_count;
36814ec11028STheodore Ts'o 	unsigned int i;
3682ef5fd681SKaixu Xia 	int needs_recovery, has_huge_files;
3683bd81d8eeSLaurent Vivier 	__u64 blocks_count;
368407aa2ea1SLukas Czerner 	int err = 0;
3685b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3686bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3687ac27a0ecSDave Kleikamp 
36885aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
36895aee0f8aSTheodore Ts'o 		goto out_free_base;
3690705895b6SPekka Enberg 
3691aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3692705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3693705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
36945aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
36955aee0f8aSTheodore Ts'o 		goto out_free_base;
36965aee0f8aSTheodore Ts'o 
3697ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
36982c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3699240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3700d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3701f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3702f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3703dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3704ac27a0ecSDave Kleikamp 
37059f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3706ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
37079f6200bbSTheodore Ts'o 
370807aa2ea1SLukas Czerner 	/* -EINVAL is default */
3709dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3710617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3711ac27a0ecSDave Kleikamp 	if (!blocksize) {
3712b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3713ac27a0ecSDave Kleikamp 		goto out_fail;
3714ac27a0ecSDave Kleikamp 	}
3715ac27a0ecSDave Kleikamp 
3716ac27a0ecSDave Kleikamp 	/*
3717617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3718ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3719ac27a0ecSDave Kleikamp 	 */
3720617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
372170bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
372270bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3723ac27a0ecSDave Kleikamp 	} else {
372470bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3725ac27a0ecSDave Kleikamp 	}
3726ac27a0ecSDave Kleikamp 
3727a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3728b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3729ac27a0ecSDave Kleikamp 		goto out_fail;
3730ac27a0ecSDave Kleikamp 	}
3731ac27a0ecSDave Kleikamp 	/*
3732ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3733617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3734ac27a0ecSDave Kleikamp 	 */
37352716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3736ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3737ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3738617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3739617ba13bSMingming Cao 		goto cantfind_ext4;
3740afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3741ac27a0ecSDave Kleikamp 
3742feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3743e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3744e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3745363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3746feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3747feb0ab32SDarrick J. Wong 
3748d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3749d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3750d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3751d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3752d25425f8SDarrick J. Wong 		silent = 1;
3753d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3754d25425f8SDarrick J. Wong 	}
3755d25425f8SDarrick J. Wong 
37560441984aSDarrick J. Wong 	/* Load the checksum driver */
37570441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
37580441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
37590441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
37600441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
37610441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
37620441984aSDarrick J. Wong 		goto failed_mount;
37630441984aSDarrick J. Wong 	}
37640441984aSDarrick J. Wong 
3765a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3766a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3767a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3768a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3769a9c47317SDarrick J. Wong 		silent = 1;
37706a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3771a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3772a9c47317SDarrick J. Wong 	}
3773a9c47317SDarrick J. Wong 
3774a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3775e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
37768c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3777dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3778a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3779a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3780a9c47317SDarrick J. Wong 
3781ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3782ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3783fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3784617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3785fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
378687f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3787fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3788617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3789fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3790ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3791fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
379203010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3793fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
37942e7842b8SHugh Dickins #endif
379598c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
379698c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
379798c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
379898c1a759SDarrick J. Wong 
3799617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3800fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3801617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3802fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3803617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3804fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3805ac27a0ecSDave Kleikamp 
3806617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3807fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3808bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3809fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3810bb4f397aSAneesh Kumar K.V 	else
3811fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
381245f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3813fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
38148b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3815fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3816ac27a0ecSDave Kleikamp 
381708cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
381808cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
381930773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
382030773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
382130773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3822ac27a0ecSDave Kleikamp 
38238b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3824fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3825ac27a0ecSDave Kleikamp 
38261e2462f9SMingming Cao 	/*
3827dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3828dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3829dd919b98SAneesh Kumar K.V 	 */
3830bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
38318b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3832fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3833dd919b98SAneesh Kumar K.V 
383451ce6511SLukas Czerner 	/*
383551ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
383651ce6511SLukas Czerner 	 * no mount option specified.
383751ce6511SLukas Czerner 	 */
383851ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
383951ce6511SLukas Czerner 
38404f97a681STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3841626b035bSRitesh Harjani 
3842626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
3843626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
3844626b035bSRitesh Harjani 
38454f97a681STheodore Ts'o 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
38464f97a681STheodore Ts'o 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
38474f97a681STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
38484f97a681STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
38494f97a681STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
38504f97a681STheodore Ts'o 		goto failed_mount;
38514f97a681STheodore Ts'o 	}
38524f97a681STheodore Ts'o 
38539803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
38549803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
38559803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
38569803387cSTheodore Ts'o 	} else {
38579803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
38589803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
38599803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
38609803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
38619803387cSTheodore Ts'o 				 sbi->s_first_ino);
38629803387cSTheodore Ts'o 			goto failed_mount;
38639803387cSTheodore Ts'o 		}
38649803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
38659803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
38669803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
38679803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
38689803387cSTheodore Ts'o 			       "unsupported inode size: %d",
38699803387cSTheodore Ts'o 			       sbi->s_inode_size);
38704f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
38719803387cSTheodore Ts'o 			goto failed_mount;
38729803387cSTheodore Ts'o 		}
38739803387cSTheodore Ts'o 		/*
38749803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
38759803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
38769803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
38779803387cSTheodore Ts'o 		 * for all three.
38789803387cSTheodore Ts'o 		 */
38799803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
38809803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
38819803387cSTheodore Ts'o 			sb->s_time_gran = 1;
38829803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
38839803387cSTheodore Ts'o 		} else {
38849803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
38859803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
38869803387cSTheodore Ts'o 		}
38879803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
38889803387cSTheodore Ts'o 	}
38899803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
38909803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
38919803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
38929803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
38939803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
38949803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
38959803387cSTheodore Ts'o 
38969803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
38979803387cSTheodore Ts'o 			if (v > max) {
38989803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
38999803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
39009803387cSTheodore Ts'o 				goto failed_mount;
39019803387cSTheodore Ts'o 			}
39029803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
39039803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
39049803387cSTheodore Ts'o 
39059803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
39069803387cSTheodore Ts'o 			if (v > max) {
39079803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
39089803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
39099803387cSTheodore Ts'o 				goto failed_mount;
39109803387cSTheodore Ts'o 			}
39119803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
39129803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
39139803387cSTheodore Ts'o 		}
39149803387cSTheodore Ts'o 	}
39159803387cSTheodore Ts'o 
39165aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
39175aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
39185aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
39195aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
39205aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
39215aee0f8aSTheodore Ts'o 			goto failed_mount;
39225aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
39235aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
39248b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
39258b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
39265aee0f8aSTheodore Ts'o 				 s_mount_opts);
39275aee0f8aSTheodore Ts'o 		}
39285aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
39298b67f04aSTheodore Ts'o 	}
39305a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3931b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3932661aa520SEric Sandeen 			   &journal_ioprio, 0))
3933ac27a0ecSDave Kleikamp 		goto failed_mount;
3934ac27a0ecSDave Kleikamp 
3935c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3936c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
3937c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
3938c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
3939c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
3940c83ad55eSGabriel Krisman Bertazi 
3941c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
3942c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3943c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
3944c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3945c83ad55eSGabriel Krisman Bertazi 		}
3946c83ad55eSGabriel Krisman Bertazi 
3947c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
3948c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
3949c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3950c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
3951c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3952c83ad55eSGabriel Krisman Bertazi 		}
3953c83ad55eSGabriel Krisman Bertazi 
3954c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
3955c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
3956c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3957c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
3958c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
3959c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
3960c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
3961c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3962c83ad55eSGabriel Krisman Bertazi 		}
3963c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
3964c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
3965c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
3966c83ad55eSGabriel Krisman Bertazi 
3967c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
3968c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
3969c83ad55eSGabriel Krisman Bertazi 	}
3970c83ad55eSGabriel Krisman Bertazi #endif
3971c83ad55eSGabriel Krisman Bertazi 
397256889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3973244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
3974781c036bSKaixu Xia 		/* can't mount with both data=journal and dioread_nolock. */
3975244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
397656889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
397756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
397856889787STheodore Ts'o 				 "both data=journal and delalloc");
397956889787STheodore Ts'o 			goto failed_mount;
398056889787STheodore Ts'o 		}
3981923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
3982923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
3983923ae0ffSRoss Zwisler 				 "both data=journal and dax");
3984923ae0ffSRoss Zwisler 			goto failed_mount;
3985923ae0ffSRoss Zwisler 		}
398673b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
398773b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
398873b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
398973b92a2aSSergey Karamov 				 "instead of data journaling mode");
399073b92a2aSSergey Karamov 		}
399156889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
399256889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
3993001e4a87STejun Heo 	} else {
3994001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
399556889787STheodore Ts'o 	}
399656889787STheodore Ts'o 
39971751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
39981751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
3999ac27a0ecSDave Kleikamp 
4000617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4001e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4002e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4003e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4004b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4005b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4006b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4007469108ffSTheodore Tso 
4008ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4009ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4010e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4011ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4012ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4013ed3654ebSTheodore Ts'o 			goto failed_mount;
4014ed3654ebSTheodore Ts'o 		}
4015dec214d0STahsin Erdogan 
4016dec214d0STahsin Erdogan 		/*
4017dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4018dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4019dec214d0STahsin Erdogan 		 */
4020dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4021dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4022dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4023dec214d0STahsin Erdogan 			goto failed_mount;
4024dec214d0STahsin Erdogan 		}
4025ed3654ebSTheodore Ts'o 	}
4026ed3654ebSTheodore Ts'o 
40272035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
40282035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
40292035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
40302035e776STheodore Ts'o 				 "using the ext4 subsystem");
40312035e776STheodore Ts'o 		else {
40320d9366d6SEric Sandeen 			/*
40330d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
40340d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
40350d9366d6SEric Sandeen 			 */
40360d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
40370d9366d6SEric Sandeen 				goto failed_mount;
40382035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
40392035e776STheodore Ts'o 				 "to feature incompatibilities");
40402035e776STheodore Ts'o 			goto failed_mount;
40412035e776STheodore Ts'o 		}
40422035e776STheodore Ts'o 	}
40432035e776STheodore Ts'o 
40442035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
40452035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
40462035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
40472035e776STheodore Ts'o 				 "using the ext4 subsystem");
40482035e776STheodore Ts'o 		else {
40490d9366d6SEric Sandeen 			/*
40500d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
40510d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
40520d9366d6SEric Sandeen 			 */
40530d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
40540d9366d6SEric Sandeen 				goto failed_mount;
40552035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
40562035e776STheodore Ts'o 				 "to feature incompatibilities");
40572035e776STheodore Ts'o 			goto failed_mount;
40582035e776STheodore Ts'o 		}
40592035e776STheodore Ts'o 	}
40602035e776STheodore Ts'o 
4061469108ffSTheodore Tso 	/*
4062ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4063ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4064ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4065ac27a0ecSDave Kleikamp 	 */
4066bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4067ac27a0ecSDave Kleikamp 		goto failed_mount;
4068a13fb1a4SEric Sandeen 
40698cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
40708cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
40718cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
40728cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
40738cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4074ac27a0ecSDave Kleikamp 		goto failed_mount;
4075ac27a0ecSDave Kleikamp 	}
4076bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4077bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4078bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4079bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4080bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4081bfe0a5f4STheodore Ts'o 		goto failed_mount;
4082bfe0a5f4STheodore Ts'o 	}
4083ac27a0ecSDave Kleikamp 
40845b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
40855b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
40865b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
40875b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
40885b9554dcSTheodore Ts'o 		goto failed_mount;
40895b9554dcSTheodore Ts'o 	}
40905b9554dcSTheodore Ts'o 
4091923ae0ffSRoss Zwisler 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
4092559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4093559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4094559db4c6SRoss Zwisler 					" that may contain inline data");
4095361d24d4SEric Sandeen 			goto failed_mount;
4096559db4c6SRoss Zwisler 		}
409780660f20SDave Jiang 		if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
409824f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4099361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4100361d24d4SEric Sandeen 			goto failed_mount;
410124f3478dSDan Williams 		}
4102923ae0ffSRoss Zwisler 	}
4103923ae0ffSRoss Zwisler 
4104e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
41056ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
41066ddb2447STheodore Ts'o 			 es->s_encryption_level);
41076ddb2447STheodore Ts'o 		goto failed_mount;
41086ddb2447STheodore Ts'o 	}
41096ddb2447STheodore Ts'o 
4110ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4111ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4112ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4113b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4114ce40733cSAneesh Kumar K.V 					blocksize);
4115ac27a0ecSDave Kleikamp 			goto failed_mount;
4116ac27a0ecSDave Kleikamp 		}
4117ac27a0ecSDave Kleikamp 
4118ac27a0ecSDave Kleikamp 		brelse(bh);
411970bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
412070bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4121a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4122ac27a0ecSDave Kleikamp 		if (!bh) {
4123b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4124b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4125ac27a0ecSDave Kleikamp 			goto failed_mount;
4126ac27a0ecSDave Kleikamp 		}
41272716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4128ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4129617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4130b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4131b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4132ac27a0ecSDave Kleikamp 			goto failed_mount;
4133ac27a0ecSDave Kleikamp 		}
4134ac27a0ecSDave Kleikamp 	}
4135ac27a0ecSDave Kleikamp 
4136e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4137f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4138f287a1a5STheodore Ts'o 						      has_huge_files);
4139f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4140ac27a0ecSDave Kleikamp 
41410d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4142e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
41438fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
41440d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4145d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4146b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4147b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
41480d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
41490d1ee42fSAlexandre Ratchov 			goto failed_mount;
41500d1ee42fSAlexandre Ratchov 		}
41510d1ee42fSAlexandre Ratchov 	} else
41520d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
41530b8e58a1SAndreas Dilger 
4154ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4155ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
41560b8e58a1SAndreas Dilger 
4157617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4158ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4159617ba13bSMingming Cao 		goto cantfind_ext4;
4160cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4161cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4162cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4163b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4164cd6bb35bSTheodore Ts'o 		goto failed_mount;
4165cd6bb35bSTheodore Ts'o 	}
4166ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4167ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
41680d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4169ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4170ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4171e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4172e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
41730b8e58a1SAndreas Dilger 
4174ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4175ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4176ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4177e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4178f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4179f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4180f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4181f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4182f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4183bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
418423301410STheodore Ts'o 				es->s_flags |=
418523301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4186f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4187f99b2589STheodore Ts'o #else
4188bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
418923301410STheodore Ts'o 				es->s_flags |=
419023301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4191f99b2589STheodore Ts'o #endif
4192f99b2589STheodore Ts'o 		}
419323301410STheodore Ts'o 	}
4194ac27a0ecSDave Kleikamp 
4195281b5995STheodore Ts'o 	/* Handle clustersize */
4196281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4197ef5fd681SKaixu Xia 	if (ext4_has_feature_bigalloc(sb)) {
4198281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4199281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4200281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4201281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4202281b5995STheodore Ts'o 			goto failed_mount;
4203281b5995STheodore Ts'o 		}
4204281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4205281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4206281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4207281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4208281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4209281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4210281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4211281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4212281b5995STheodore Ts'o 			goto failed_mount;
4213281b5995STheodore Ts'o 		}
4214281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4215281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4216281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4217281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4218281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4219281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4220281b5995STheodore Ts'o 			goto failed_mount;
4221281b5995STheodore Ts'o 		}
4222281b5995STheodore Ts'o 	} else {
4223281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4224bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4225bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4226bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4227bfe0a5f4STheodore Ts'o 			goto failed_mount;
4228281b5995STheodore Ts'o 		}
4229ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4230b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4231b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4232ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4233ac27a0ecSDave Kleikamp 			goto failed_mount;
4234ac27a0ecSDave Kleikamp 		}
4235281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4236281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4237281b5995STheodore Ts'o 	}
4238281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4239281b5995STheodore Ts'o 
4240960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4241960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4242960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4243960fd856STheodore Ts'o 
4244bf43d84bSEric Sandeen 	/*
4245bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4246bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4247bf43d84bSEric Sandeen 	 */
42485a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
424930ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
42505a9ae68aSDarrick J. Wong 	if (err) {
4251b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4252bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4253ac27a0ecSDave Kleikamp 		goto failed_mount;
4254ac27a0ecSDave Kleikamp 	}
4255ac27a0ecSDave Kleikamp 
4256617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4257617ba13bSMingming Cao 		goto cantfind_ext4;
4258e7c95593SEric Sandeen 
42590f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
42600f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
42610f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4262b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4263b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
42640f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
42650f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
42660f2ddca6SFrom: Thiemo Nagel 	}
42670f2ddca6SFrom: Thiemo Nagel 
42684ec11028STheodore Ts'o 	/*
42694ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
42704ec11028STheodore Ts'o 	 * of the filesystem.
42714ec11028STheodore Ts'o 	 */
42724ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4273b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4274b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4275e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
42764ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4277e7c95593SEric Sandeen 		goto failed_mount;
4278e7c95593SEric Sandeen 	}
4279bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4280bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4281bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4282bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4283bfe0a5f4STheodore Ts'o 		goto failed_mount;
4284bfe0a5f4STheodore Ts'o 	}
4285bfe0a5f4STheodore Ts'o 
4286bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4287bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4288bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4289bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
42904ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4291df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
42924ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4293df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
42944ec11028STheodore Ts'o 		       ext4_blocks_count(es),
42954ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
42964ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
42974ec11028STheodore Ts'o 		goto failed_mount;
42984ec11028STheodore Ts'o 	}
4299bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4300fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4301fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
43029e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
43039e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
43049e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
43059e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
43069e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
43079e463084STheodore Ts'o 		ret = -EINVAL;
43089e463084STheodore Ts'o 		goto failed_mount;
43099e463084STheodore Ts'o 	}
4310617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4311617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
43123a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
43132ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
43143a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
43153a4b77cdSEryu Guan 				 "first meta block group too large: %u "
43163a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
43173a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
43183a4b77cdSEryu Guan 			goto failed_mount;
43193a4b77cdSEryu Guan 		}
43203a4b77cdSEryu Guan 	}
43211d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
43221d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4323f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
43241d0c3924STheodore Ts'o 					  GFP_KERNEL));
4325ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4326b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
43272cde417dSTheodore Ts'o 		ret = -ENOMEM;
4328ac27a0ecSDave Kleikamp 		goto failed_mount;
4329ac27a0ecSDave Kleikamp 	}
4330ac27a0ecSDave Kleikamp 
4331705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4332ac27a0ecSDave Kleikamp 
433385c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
433485c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
433585c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
4336d87f6392SRoman Gushchin 		sb_breadahead_unmovable(sb, block);
433785c8f176SAndrew Perepechko 	}
433885c8f176SAndrew Perepechko 
4339ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
43401d0c3924STheodore Ts'o 		struct buffer_head *bh;
43411d0c3924STheodore Ts'o 
434270bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
43431d0c3924STheodore Ts'o 		bh = sb_bread_unmovable(sb, block);
43441d0c3924STheodore Ts'o 		if (!bh) {
4345b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4346b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4347ac27a0ecSDave Kleikamp 			db_count = i;
4348ac27a0ecSDave Kleikamp 			goto failed_mount2;
4349ac27a0ecSDave Kleikamp 		}
43501d0c3924STheodore Ts'o 		rcu_read_lock();
43511d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
43521d0c3924STheodore Ts'o 		rcu_read_unlock();
4353ac27a0ecSDave Kleikamp 	}
435444de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4355829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4356b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
43576a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4358f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4359ac27a0ecSDave Kleikamp 	}
4360772cb7c8SJose R. Santos 
4361235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
436204496411STao Ma 
4363a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4364eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4365ce7e010aSTheodore Ts'o 		goto failed_mount3;
4366ce7e010aSTheodore Ts'o 
4367c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
436867a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4369c9de560dSAlex Tomas 
4370f9ae9cf5STheodore Ts'o 	/*
4371f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4372f9ae9cf5STheodore Ts'o 	 */
4373f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4374617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4375617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4376643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4377a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4378ffcc4182SEric Biggers #endif
4379c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4380c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4381c93d8f88SEric Biggers #endif
4382ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4383617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4384e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
43851fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4386262b4662SJan Kara 	else
4387262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4388689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4389ac27a0ecSDave Kleikamp #endif
439085787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4391f2fa2ffcSAneesh Kumar K.V 
4392ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
43933b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4394ac27a0ecSDave Kleikamp 
4395ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4396ac27a0ecSDave Kleikamp 
4397ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4398e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4399ac27a0ecSDave Kleikamp 
4400bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4401c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
440250460fe8SDarrick J. Wong 			goto failed_mount3a;
4403c5e06d10SJohann Lombardi 
4404ac27a0ecSDave Kleikamp 	/*
4405ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4406ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4407ac27a0ecSDave Kleikamp 	 */
4408e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
44094753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
44104753d8a2STheodore Ts'o 		if (err)
441150460fe8SDarrick J. Wong 			goto failed_mount3a;
4412bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4413e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4414b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4415b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4416744692dcSJiaying Zhang 		goto failed_mount_wq;
4417ac27a0ecSDave Kleikamp 	} else {
44181e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
44191e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
44201e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44211e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
44221e381f60SDmitry Monakhov 			goto failed_mount_wq;
44231e381f60SDmitry Monakhov 		}
44241e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
44251e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44261e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
44271e381f60SDmitry Monakhov 			goto failed_mount_wq;
44281e381f60SDmitry Monakhov 		}
44291e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
44301e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44311e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
44321e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
44331e381f60SDmitry Monakhov 			goto failed_mount_wq;
44341e381f60SDmitry Monakhov 		}
44351e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
44361e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
44371e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44381e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
44391e381f60SDmitry Monakhov 			goto failed_mount_wq;
44401e381f60SDmitry Monakhov 		}
444150b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
44421e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4443fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
44440390131bSFrank Mayhar 		sbi->s_journal = NULL;
44450390131bSFrank Mayhar 		needs_recovery = 0;
44460390131bSFrank Mayhar 		goto no_journal;
4447ac27a0ecSDave Kleikamp 	}
4448ac27a0ecSDave Kleikamp 
4449e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4450eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4451eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4452b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4453744692dcSJiaying Zhang 		goto failed_mount_wq;
4454eb40a09cSJose R. Santos 	}
4455eb40a09cSJose R. Santos 
445625ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
445725ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
445825ed6e8aSDarrick J. Wong 			 "feature set");
445925ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4460d4da6c9cSLinus Torvalds 	}
4461818d276cSGirish Shilamkar 
4462ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4463ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4464ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4465ac27a0ecSDave Kleikamp 	case 0:
4466ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
446763f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
446863f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
446963f57933SAndrew Morton 		 */
4470dab291afSMingming Cao 		if (jbd2_journal_check_available_features
447127f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4472fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
447327f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
447427f394a7STyson Nottingham 		} else {
4475fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
447627f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
447727f394a7STyson Nottingham 		}
4478ac27a0ecSDave Kleikamp 		break;
4479ac27a0ecSDave Kleikamp 
4480617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4481617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4482dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4483dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4484b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4485b31e1552SEric Sandeen 			       "requested data journaling mode");
4486744692dcSJiaying Zhang 			goto failed_mount_wq;
4487ac27a0ecSDave Kleikamp 		}
4488ac27a0ecSDave Kleikamp 	default:
4489ac27a0ecSDave Kleikamp 		break;
4490ac27a0ecSDave Kleikamp 	}
4491ab04df78SJan Kara 
4492ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4493ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4494ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4495ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4496ab04df78SJan Kara 		goto failed_mount_wq;
4497ab04df78SJan Kara 	}
4498ab04df78SJan Kara 
4499b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4500ac27a0ecSDave Kleikamp 
450118aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
450218aadd47SBobi Jam 
4503ce7e010aSTheodore Ts'o no_journal:
4504cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
450547387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
450647387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4507cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4508cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
45099c191f70ST Makphaibulchoke 			goto failed_mount_wq;
45109c191f70ST Makphaibulchoke 		}
45119c191f70ST Makphaibulchoke 
4512dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4513dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4514dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4515dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4516dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4517dec214d0STahsin Erdogan 				goto failed_mount_wq;
4518dec214d0STahsin Erdogan 			}
4519dec214d0STahsin Erdogan 		}
4520cdb7ee4cSTahsin Erdogan 	}
4521dec214d0STahsin Erdogan 
4522c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4523c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4524c93d8f88SEric Biggers 		goto failed_mount_wq;
4525c93d8f88SEric Biggers 	}
4526c93d8f88SEric Biggers 
4527bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4528e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4529e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
45306ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
45316ddb2447STheodore Ts'o 	}
45326ddb2447STheodore Ts'o 
4533fd89d5f2STejun Heo 	/*
4534952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4535952fc18eSTheodore Ts'o 	 * superblock if present.
4536952fc18eSTheodore Ts'o 	 */
4537952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4538952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4539952fc18eSTheodore Ts'o 	else {
454007aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
454107aa2ea1SLukas Czerner 		if (err)
4542952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4543952fc18eSTheodore Ts'o 	}
4544952fc18eSTheodore Ts'o 
4545952fc18eSTheodore Ts'o 	/*
4546fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4547fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4548fd89d5f2STejun Heo 	 */
45492e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
45502e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
45512e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
45522e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
455307aa2ea1SLukas Czerner 		ret = -ENOMEM;
45542e8fa54eSJan Kara 		goto failed_mount4;
45552e8fa54eSJan Kara 	}
45562e8fa54eSJan Kara 
4557ac27a0ecSDave Kleikamp 	/*
4558dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4559ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4560ac27a0ecSDave Kleikamp 	 */
4561ac27a0ecSDave Kleikamp 
45628a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
45631d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4564b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
45651d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
456632a9bb57SManish Katiyar 		root = NULL;
4567ac27a0ecSDave Kleikamp 		goto failed_mount4;
4568ac27a0ecSDave Kleikamp 	}
4569ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4570b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
457194bf608aSAl Viro 		iput(root);
4572ac27a0ecSDave Kleikamp 		goto failed_mount4;
4573ac27a0ecSDave Kleikamp 	}
4574b886ee3eSGabriel Krisman Bertazi 
4575b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4576b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4577b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4578b886ee3eSGabriel Krisman Bertazi #endif
4579b886ee3eSGabriel Krisman Bertazi 
458048fde701SAl Viro 	sb->s_root = d_make_root(root);
45811d1fe1eeSDavid Howells 	if (!sb->s_root) {
4582b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
45831d1fe1eeSDavid Howells 		ret = -ENOMEM;
45841d1fe1eeSDavid Howells 		goto failed_mount4;
45851d1fe1eeSDavid Howells 	}
4586ac27a0ecSDave Kleikamp 
4587c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4588c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
45891751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4590c89128a0SJaegeuk Kim 		ret = 0;
4591c89128a0SJaegeuk Kim 	} else if (ret)
4592c89128a0SJaegeuk Kim 		goto failed_mount4a;
4593ef7f3835SKalpak Shah 
4594b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
459527dd4385SLukas Czerner 
45966fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
45976fd058f7STheodore Ts'o 	if (err) {
4598b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4599fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4600f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4601f9ae9cf5STheodore Ts'o 	}
4602f9ae9cf5STheodore Ts'o 
4603f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4604f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4605f9ae9cf5STheodore Ts'o 	if (err) {
4606f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4607f9ae9cf5STheodore Ts'o 			 err);
4608dcf2d804STao Ma 		goto failed_mount5;
4609c2774d84SAneesh Kumar K.V 	}
4610c2774d84SAneesh Kumar K.V 
4611d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4612d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4613d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
46144274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4615908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4616908c7f19STejun Heo 				  GFP_KERNEL);
4617d5e03cbbSTheodore Ts'o 	if (!err) {
4618d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4619d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
46204274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4621908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4622908c7f19STejun Heo 					  GFP_KERNEL);
4623d5e03cbbSTheodore Ts'o 	}
4624d5e03cbbSTheodore Ts'o 	if (!err)
4625d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4626908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4627d5e03cbbSTheodore Ts'o 	if (!err)
4628908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4629908c7f19STejun Heo 					  GFP_KERNEL);
4630c8585c6fSDaeho Jeong 	if (!err)
4631bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4632c8585c6fSDaeho Jeong 
4633d5e03cbbSTheodore Ts'o 	if (err) {
4634d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4635d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4636d5e03cbbSTheodore Ts'o 	}
4637d5e03cbbSTheodore Ts'o 
4638e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4639d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4640d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4641d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4642d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4643d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4644d5e03cbbSTheodore Ts'o 		}
4645d5e03cbbSTheodore Ts'o 
4646bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4647bfff6873SLukas Czerner 	if (err)
4648dcf2d804STao Ma 		goto failed_mount6;
4649bfff6873SLukas Czerner 
4650b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4651dcf2d804STao Ma 	if (err)
4652dcf2d804STao Ma 		goto failed_mount7;
46533197ebdbSTheodore Ts'o 
46549b2ff357SJan Kara #ifdef CONFIG_QUOTA
46559b2ff357SJan Kara 	/* Enable quota usage during mount. */
4656bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
46579b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
46589b2ff357SJan Kara 		if (err)
46599b2ff357SJan Kara 			goto failed_mount8;
46609b2ff357SJan Kara 	}
46619b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
46629b2ff357SJan Kara 
4663617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4664617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4665617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
46660390131bSFrank Mayhar 	if (needs_recovery) {
4667b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4668617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
46690390131bSFrank Mayhar 	}
46700390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
46710390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
46720390131bSFrank Mayhar 			descr = " journalled data mode";
46730390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
46740390131bSFrank Mayhar 			descr = " ordered data mode";
46750390131bSFrank Mayhar 		else
46760390131bSFrank Mayhar 			descr = " writeback data mode";
46770390131bSFrank Mayhar 	} else
46780390131bSFrank Mayhar 		descr = "out journal";
46790390131bSFrank Mayhar 
468079add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
468179add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
468279add3a3SLukas Czerner 		if (!blk_queue_discard(q))
468379add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
468479add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
468579add3a3SLukas Czerner 				 "the device does not support discard");
468679add3a3SLukas Czerner 	}
468779add3a3SLukas Czerner 
4688e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4689d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
46905aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
46915aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
46925aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
46938b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4694ac27a0ecSDave Kleikamp 
469566e61a9eSTheodore Ts'o 	if (es->s_error_count)
469666e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4697ac27a0ecSDave Kleikamp 
4698efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4699efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4700efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4701efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4702efbed4dcSTheodore Ts'o 
4703d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4704ac27a0ecSDave Kleikamp 	return 0;
4705ac27a0ecSDave Kleikamp 
4706617ba13bSMingming Cao cantfind_ext4:
4707ac27a0ecSDave Kleikamp 	if (!silent)
4708b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4709ac27a0ecSDave Kleikamp 	goto failed_mount;
4710ac27a0ecSDave Kleikamp 
471172ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
471272ba7450STheodore Ts'o failed_mount8:
4713ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
471472ba7450STheodore Ts'o #endif
4715dcf2d804STao Ma failed_mount7:
4716dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4717dcf2d804STao Ma failed_mount6:
4718f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
47197c990728SSuraj Jitindar Singh 	rcu_read_lock();
47207c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
47217c990728SSuraj Jitindar Singh 	if (flex_groups) {
47227c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
47237c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
47247c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
47257c990728SSuraj Jitindar Singh 	}
47267c990728SSuraj Jitindar Singh 	rcu_read_unlock();
4727d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4728d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4729d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4730d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4731bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
473200764937SAzat Khuzhin failed_mount5:
4733f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4734f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4735f9ae9cf5STheodore Ts'o failed_mount4a:
473694bf608aSAl Viro 	dput(sb->s_root);
473732a9bb57SManish Katiyar 	sb->s_root = NULL;
473894bf608aSAl Viro failed_mount4:
4739b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
47402e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
47412e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
47424c0425ffSMingming Cao failed_mount_wq:
4743dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4744dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
474550c15df6SChengguang Xu 
474647387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
474747387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
474850c15df6SChengguang Xu 
47490390131bSFrank Mayhar 	if (sbi->s_journal) {
4750dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
475147b4a50bSJan Kara 		sbi->s_journal = NULL;
47520390131bSFrank Mayhar 	}
475350460fe8SDarrick J. Wong failed_mount3a:
4754d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4755eb68d0e2SZheng Liu failed_mount3:
47569105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4757c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4758c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4759ac27a0ecSDave Kleikamp failed_mount2:
47601d0c3924STheodore Ts'o 	rcu_read_lock();
47611d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
4762ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
47631d0c3924STheodore Ts'o 		brelse(group_desc[i]);
47641d0c3924STheodore Ts'o 	kvfree(group_desc);
47651d0c3924STheodore Ts'o 	rcu_read_unlock();
4766ac27a0ecSDave Kleikamp failed_mount:
47670441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
47680441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4769c83ad55eSGabriel Krisman Bertazi 
4770c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4771c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4772c83ad55eSGabriel Krisman Bertazi #endif
4773c83ad55eSGabriel Krisman Bertazi 
4774ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4775a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
47760ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4777ac27a0ecSDave Kleikamp #endif
4778617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4779ac27a0ecSDave Kleikamp 	brelse(bh);
4780ac27a0ecSDave Kleikamp out_fail:
4781ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4782f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
47835aee0f8aSTheodore Ts'o out_free_base:
4784ac27a0ecSDave Kleikamp 	kfree(sbi);
4785d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
47865e405595SDan Williams 	fs_put_dax(dax_dev);
478707aa2ea1SLukas Czerner 	return err ? err : ret;
4788ac27a0ecSDave Kleikamp }
4789ac27a0ecSDave Kleikamp 
4790ac27a0ecSDave Kleikamp /*
4791ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4792ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4793ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4794ac27a0ecSDave Kleikamp  */
4795617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4796ac27a0ecSDave Kleikamp {
4797617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4798ac27a0ecSDave Kleikamp 
4799ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
480030773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
480130773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4802ac27a0ecSDave Kleikamp 
4803a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4804ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4805dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4806ac27a0ecSDave Kleikamp 	else
4807dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
48085bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
48095bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
48105bf5683aSHidehiro Kawai 	else
48115bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4812a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4813ac27a0ecSDave Kleikamp }
4814ac27a0ecSDave Kleikamp 
4815c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4816ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4817ac27a0ecSDave Kleikamp {
4818ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4819ac27a0ecSDave Kleikamp 
4820c6cb7e77SEric Whitney 	/*
4821c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4822c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4823c6cb7e77SEric Whitney 	 * will try to delete it.
4824c6cb7e77SEric Whitney 	 */
48258a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
48261d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4827b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4828ac27a0ecSDave Kleikamp 		return NULL;
4829ac27a0ecSDave Kleikamp 	}
4830ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4831ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4832ac27a0ecSDave Kleikamp 		iput(journal_inode);
4833b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4834ac27a0ecSDave Kleikamp 		return NULL;
4835ac27a0ecSDave Kleikamp 	}
4836ac27a0ecSDave Kleikamp 
4837e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4838ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
48391d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4840b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4841ac27a0ecSDave Kleikamp 		iput(journal_inode);
4842ac27a0ecSDave Kleikamp 		return NULL;
4843ac27a0ecSDave Kleikamp 	}
4844c6cb7e77SEric Whitney 	return journal_inode;
4845c6cb7e77SEric Whitney }
4846c6cb7e77SEric Whitney 
4847c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4848c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4849c6cb7e77SEric Whitney {
4850c6cb7e77SEric Whitney 	struct inode *journal_inode;
4851c6cb7e77SEric Whitney 	journal_t *journal;
4852c6cb7e77SEric Whitney 
4853c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4854c6cb7e77SEric Whitney 
4855c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4856c6cb7e77SEric Whitney 	if (!journal_inode)
4857c6cb7e77SEric Whitney 		return NULL;
4858ac27a0ecSDave Kleikamp 
4859dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4860ac27a0ecSDave Kleikamp 	if (!journal) {
4861b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4862ac27a0ecSDave Kleikamp 		iput(journal_inode);
4863ac27a0ecSDave Kleikamp 		return NULL;
4864ac27a0ecSDave Kleikamp 	}
4865ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4866617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4867ac27a0ecSDave Kleikamp 	return journal;
4868ac27a0ecSDave Kleikamp }
4869ac27a0ecSDave Kleikamp 
4870617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4871ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4872ac27a0ecSDave Kleikamp {
4873ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4874ac27a0ecSDave Kleikamp 	journal_t *journal;
4875617ba13bSMingming Cao 	ext4_fsblk_t start;
4876617ba13bSMingming Cao 	ext4_fsblk_t len;
4877ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4878617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4879ac27a0ecSDave Kleikamp 	unsigned long offset;
4880617ba13bSMingming Cao 	struct ext4_super_block *es;
4881ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4882ac27a0ecSDave Kleikamp 
4883e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
48840390131bSFrank Mayhar 
4885b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4886ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4887ac27a0ecSDave Kleikamp 		return NULL;
4888ac27a0ecSDave Kleikamp 
4889ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4890e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4891ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4892b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4893b31e1552SEric Sandeen 			"blocksize too small for journal device");
4894ac27a0ecSDave Kleikamp 		goto out_bdev;
4895ac27a0ecSDave Kleikamp 	}
4896ac27a0ecSDave Kleikamp 
4897617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4898617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4899ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4900ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4901b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4902b31e1552SEric Sandeen 		       "external journal");
4903ac27a0ecSDave Kleikamp 		goto out_bdev;
4904ac27a0ecSDave Kleikamp 	}
4905ac27a0ecSDave Kleikamp 
49062716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4907617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4908ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4909617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4910b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4911b31e1552SEric Sandeen 					"bad superblock");
4912ac27a0ecSDave Kleikamp 		brelse(bh);
4913ac27a0ecSDave Kleikamp 		goto out_bdev;
4914ac27a0ecSDave Kleikamp 	}
4915ac27a0ecSDave Kleikamp 
4916df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4917df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4918df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4919df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4920df4763beSDarrick J. Wong 				       "corrupt superblock");
4921df4763beSDarrick J. Wong 		brelse(bh);
4922df4763beSDarrick J. Wong 		goto out_bdev;
4923df4763beSDarrick J. Wong 	}
4924df4763beSDarrick J. Wong 
4925617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4926b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4927ac27a0ecSDave Kleikamp 		brelse(bh);
4928ac27a0ecSDave Kleikamp 		goto out_bdev;
4929ac27a0ecSDave Kleikamp 	}
4930ac27a0ecSDave Kleikamp 
4931bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4932ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4933ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4934ac27a0ecSDave Kleikamp 
4935dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4936ac27a0ecSDave Kleikamp 					start, len, blocksize);
4937ac27a0ecSDave Kleikamp 	if (!journal) {
4938b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4939ac27a0ecSDave Kleikamp 		goto out_bdev;
4940ac27a0ecSDave Kleikamp 	}
4941ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4942dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4943ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4944ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4945b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4946ac27a0ecSDave Kleikamp 		goto out_journal;
4947ac27a0ecSDave Kleikamp 	}
4948ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4949b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4950b31e1552SEric Sandeen 					"user (unsupported) - %d",
4951ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
4952ac27a0ecSDave Kleikamp 		goto out_journal;
4953ac27a0ecSDave Kleikamp 	}
4954617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
4955617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4956ac27a0ecSDave Kleikamp 	return journal;
49570b8e58a1SAndreas Dilger 
4958ac27a0ecSDave Kleikamp out_journal:
4959dab291afSMingming Cao 	jbd2_journal_destroy(journal);
4960ac27a0ecSDave Kleikamp out_bdev:
4961617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
4962ac27a0ecSDave Kleikamp 	return NULL;
4963ac27a0ecSDave Kleikamp }
4964ac27a0ecSDave Kleikamp 
4965617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
4966617ba13bSMingming Cao 			     struct ext4_super_block *es,
4967ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
4968ac27a0ecSDave Kleikamp {
4969ac27a0ecSDave Kleikamp 	journal_t *journal;
4970ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4971ac27a0ecSDave Kleikamp 	dev_t journal_dev;
4972ac27a0ecSDave Kleikamp 	int err = 0;
4973ac27a0ecSDave Kleikamp 	int really_read_only;
4974ac27a0ecSDave Kleikamp 
4975e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
49760390131bSFrank Mayhar 
4977ac27a0ecSDave Kleikamp 	if (journal_devnum &&
4978ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4979b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4980b31e1552SEric Sandeen 			"numbers have changed");
4981ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
4982ac27a0ecSDave Kleikamp 	} else
4983ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4984ac27a0ecSDave Kleikamp 
4985ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
4986ac27a0ecSDave Kleikamp 
4987ac27a0ecSDave Kleikamp 	/*
4988ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
4989ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
4990ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
4991ac27a0ecSDave Kleikamp 	 */
4992e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
4993bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
4994b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4995b31e1552SEric Sandeen 					"required on readonly filesystem");
4996ac27a0ecSDave Kleikamp 			if (really_read_only) {
4997b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
4998d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
4999d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5000ac27a0ecSDave Kleikamp 				return -EROFS;
5001ac27a0ecSDave Kleikamp 			}
5002b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5003b31e1552SEric Sandeen 			       "be enabled during recovery");
5004ac27a0ecSDave Kleikamp 		}
5005ac27a0ecSDave Kleikamp 	}
5006ac27a0ecSDave Kleikamp 
5007ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
5008b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
5009b31e1552SEric Sandeen 		       "and inode journals!");
5010ac27a0ecSDave Kleikamp 		return -EINVAL;
5011ac27a0ecSDave Kleikamp 	}
5012ac27a0ecSDave Kleikamp 
5013ac27a0ecSDave Kleikamp 	if (journal_inum) {
5014617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
5015ac27a0ecSDave Kleikamp 			return -EINVAL;
5016ac27a0ecSDave Kleikamp 	} else {
5017617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
5018ac27a0ecSDave Kleikamp 			return -EINVAL;
5019ac27a0ecSDave Kleikamp 	}
5020ac27a0ecSDave Kleikamp 
502190576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5022b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
50234776004fSTheodore Ts'o 
5024e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5025dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
50261c13d5c0STheodore Ts'o 	if (!err) {
50271c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
50281c13d5c0STheodore Ts'o 		if (save)
50291c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
50301c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5031dab291afSMingming Cao 		err = jbd2_journal_load(journal);
50321c13d5c0STheodore Ts'o 		if (save)
50331c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
50341c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
50351c13d5c0STheodore Ts'o 		kfree(save);
50361c13d5c0STheodore Ts'o 	}
5037ac27a0ecSDave Kleikamp 
5038ac27a0ecSDave Kleikamp 	if (err) {
5039b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5040dab291afSMingming Cao 		jbd2_journal_destroy(journal);
5041ac27a0ecSDave Kleikamp 		return err;
5042ac27a0ecSDave Kleikamp 	}
5043ac27a0ecSDave Kleikamp 
5044617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
5045617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
5046ac27a0ecSDave Kleikamp 
5047c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5048ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5049ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5050ac27a0ecSDave Kleikamp 
5051ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5052e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5053ac27a0ecSDave Kleikamp 	}
5054ac27a0ecSDave Kleikamp 
5055ac27a0ecSDave Kleikamp 	return 0;
5056ac27a0ecSDave Kleikamp }
5057ac27a0ecSDave Kleikamp 
5058e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5059ac27a0ecSDave Kleikamp {
5060e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5061617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5062c4be0c1dSTakashi Sato 	int error = 0;
5063ac27a0ecSDave Kleikamp 
5064bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5065c4be0c1dSTakashi Sato 		return error;
5066a17712c8SJon Derrick 
5067a17712c8SJon Derrick 	/*
5068a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
5069a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
5070a17712c8SJon Derrick 	 */
5071a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
5072a17712c8SJon Derrick 		return error;
5073a17712c8SJon Derrick 
507471290b36STheodore Ts'o 	/*
507571290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
507671290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
507771290b36STheodore Ts'o 	 * write time when we are mounting the root file system
507871290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
507971290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
508071290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
508171290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
508271290b36STheodore Ts'o 	 * to complain and force a full file system check.
508371290b36STheodore Ts'o 	 */
50841751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
50856a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5086f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5087afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5088afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5089dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5090dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5091afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5092f613dfcbSTheodore Ts'o 	else
5093f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5094f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5095d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
509657042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
509757042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
509857042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5099d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
51007f93cff9STheodore Ts'o 		es->s_free_inodes_count =
51017f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
51025d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5103ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
510406db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
51051566a48aSTheodore Ts'o 	if (sync)
51064743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5107e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
51084743f839SPranay Kr. Srivastava 		/*
51094743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
51104743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
51114743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
51124743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
51134743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
51144743f839SPranay Kr. Srivastava 		 * write and hope for the best.
51154743f839SPranay Kr. Srivastava 		 */
51164743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
51174743f839SPranay Kr. Srivastava 		       "superblock detected");
51184743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
51194743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
51204743f839SPranay Kr. Srivastava 	}
5121ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5122914258bfSTheodore Ts'o 	if (sync) {
51231566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5124564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
512500473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5126c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5127b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5128b31e1552SEric Sandeen 			       "superblock");
5129914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5130914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5131914258bfSTheodore Ts'o 		}
5132914258bfSTheodore Ts'o 	}
5133c4be0c1dSTakashi Sato 	return error;
5134ac27a0ecSDave Kleikamp }
5135ac27a0ecSDave Kleikamp 
5136ac27a0ecSDave Kleikamp /*
5137ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5138ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5139ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5140ac27a0ecSDave Kleikamp  */
5141617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5142617ba13bSMingming Cao 					struct ext4_super_block *es)
5143ac27a0ecSDave Kleikamp {
5144617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5145ac27a0ecSDave Kleikamp 
5146e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
51470390131bSFrank Mayhar 		BUG_ON(journal != NULL);
51480390131bSFrank Mayhar 		return;
51490390131bSFrank Mayhar 	}
5150dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
51517ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
51527ffe1ea8SHidehiro Kawai 		goto out;
51537ffe1ea8SHidehiro Kawai 
5154bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5155e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5156e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5157ac27a0ecSDave Kleikamp 	}
51587ffe1ea8SHidehiro Kawai 
51597ffe1ea8SHidehiro Kawai out:
5160dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5161ac27a0ecSDave Kleikamp }
5162ac27a0ecSDave Kleikamp 
5163ac27a0ecSDave Kleikamp /*
5164ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5165ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5166ac27a0ecSDave Kleikamp  * main filesystem now.
5167ac27a0ecSDave Kleikamp  */
5168617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5169617ba13bSMingming Cao 				   struct ext4_super_block *es)
5170ac27a0ecSDave Kleikamp {
5171ac27a0ecSDave Kleikamp 	journal_t *journal;
5172ac27a0ecSDave Kleikamp 	int j_errno;
5173ac27a0ecSDave Kleikamp 	const char *errstr;
5174ac27a0ecSDave Kleikamp 
5175e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
51760390131bSFrank Mayhar 
5177617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5178ac27a0ecSDave Kleikamp 
5179ac27a0ecSDave Kleikamp 	/*
5180ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5181617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5182ac27a0ecSDave Kleikamp 	 */
5183ac27a0ecSDave Kleikamp 
5184dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5185ac27a0ecSDave Kleikamp 	if (j_errno) {
5186ac27a0ecSDave Kleikamp 		char nbuf[16];
5187ac27a0ecSDave Kleikamp 
5188617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
518912062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5190ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
519112062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5192ac27a0ecSDave Kleikamp 
5193617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5194617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5195e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5196ac27a0ecSDave Kleikamp 
5197dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5198d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5199ac27a0ecSDave Kleikamp 	}
5200ac27a0ecSDave Kleikamp }
5201ac27a0ecSDave Kleikamp 
5202ac27a0ecSDave Kleikamp /*
5203ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5204ac27a0ecSDave Kleikamp  * and wait on the commit.
5205ac27a0ecSDave Kleikamp  */
5206617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5207ac27a0ecSDave Kleikamp {
5208ac27a0ecSDave Kleikamp 	journal_t *journal;
5209ac27a0ecSDave Kleikamp 
5210bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5211ac27a0ecSDave Kleikamp 		return 0;
5212ac27a0ecSDave Kleikamp 
5213617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5214b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5215ac27a0ecSDave Kleikamp }
5216ac27a0ecSDave Kleikamp 
5217617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5218ac27a0ecSDave Kleikamp {
521914ce0cb4STheodore Ts'o 	int ret = 0;
52209eddacf9SJan Kara 	tid_t target;
522106a407f1SDmitry Monakhov 	bool needs_barrier = false;
52228d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5223ac27a0ecSDave Kleikamp 
522449598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
52250db1ff22STheodore Ts'o 		return 0;
52260db1ff22STheodore Ts'o 
52279bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
52282e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5229a1177825SJan Kara 	/*
5230a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5231a1177825SJan Kara 	 * no dirty dquots
5232a1177825SJan Kara 	 */
5233a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
523406a407f1SDmitry Monakhov 	/*
523506a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
523606a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
523706a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
523806a407f1SDmitry Monakhov 	 */
5239bda32530STheodore Ts'o 	if (sbi->s_journal) {
524006a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
524106a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
524206a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
524306a407f1SDmitry Monakhov 			needs_barrier = true;
524406a407f1SDmitry Monakhov 
52458d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5246ac27a0ecSDave Kleikamp 			if (wait)
5247bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5248bda32530STheodore Ts'o 							   target);
52490390131bSFrank Mayhar 		}
5250bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5251bda32530STheodore Ts'o 		needs_barrier = true;
525206a407f1SDmitry Monakhov 	if (needs_barrier) {
525306a407f1SDmitry Monakhov 		int err;
525406a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
525506a407f1SDmitry Monakhov 		if (!ret)
525606a407f1SDmitry Monakhov 			ret = err;
525706a407f1SDmitry Monakhov 	}
525806a407f1SDmitry Monakhov 
525906a407f1SDmitry Monakhov 	return ret;
526006a407f1SDmitry Monakhov }
526106a407f1SDmitry Monakhov 
5262ac27a0ecSDave Kleikamp /*
5263ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5264ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5265be4f27d3SYongqiang Yang  *
5266be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
52678e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
52688e8ad8a5SJan Kara  * modifications.
5269ac27a0ecSDave Kleikamp  */
5270c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5271ac27a0ecSDave Kleikamp {
5272c4be0c1dSTakashi Sato 	int error = 0;
5273c4be0c1dSTakashi Sato 	journal_t *journal;
5274ac27a0ecSDave Kleikamp 
5275bc98a42cSDavid Howells 	if (sb_rdonly(sb))
52769ca92389STheodore Ts'o 		return 0;
52779ca92389STheodore Ts'o 
5278c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5279ac27a0ecSDave Kleikamp 
5280bb044576STheodore Ts'o 	if (journal) {
5281ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5282dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
52837ffe1ea8SHidehiro Kawai 
52847ffe1ea8SHidehiro Kawai 		/*
5285bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5286bb044576STheodore Ts'o 		 * flush the journal.
52877ffe1ea8SHidehiro Kawai 		 */
5288c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
52896b0310fbSEric Sandeen 		if (error < 0)
52906b0310fbSEric Sandeen 			goto out;
5291ac27a0ecSDave Kleikamp 
5292ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5293e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5294c642dc9eSEric Sandeen 	}
5295c642dc9eSEric Sandeen 
5296e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
52976b0310fbSEric Sandeen out:
5298bb044576STheodore Ts'o 	if (journal)
52998e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5300bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
53016b0310fbSEric Sandeen 	return error;
5302ac27a0ecSDave Kleikamp }
5303ac27a0ecSDave Kleikamp 
5304ac27a0ecSDave Kleikamp /*
5305ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5306ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5307ac27a0ecSDave Kleikamp  */
5308c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5309ac27a0ecSDave Kleikamp {
5310bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
53119ca92389STheodore Ts'o 		return 0;
53129ca92389STheodore Ts'o 
5313c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
53149ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5315e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5316c642dc9eSEric Sandeen 	}
5317c642dc9eSEric Sandeen 
5318e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5319c4be0c1dSTakashi Sato 	return 0;
5320ac27a0ecSDave Kleikamp }
5321ac27a0ecSDave Kleikamp 
5322673c6100STheodore Ts'o /*
5323673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5324673c6100STheodore Ts'o  */
5325673c6100STheodore Ts'o struct ext4_mount_options {
5326673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5327a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
532808cefc7aSEric W. Biederman 	kuid_t s_resuid;
532908cefc7aSEric W. Biederman 	kgid_t s_resgid;
5330673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5331673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5332673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5333673c6100STheodore Ts'o 	int s_jquota_fmt;
5334a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5335673c6100STheodore Ts'o #endif
5336673c6100STheodore Ts'o };
5337673c6100STheodore Ts'o 
5338617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5339ac27a0ecSDave Kleikamp {
5340617ba13bSMingming Cao 	struct ext4_super_block *es;
5341617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5342ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5343617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5344c79d967dSChristoph Hellwig 	int enable_quota = 0;
53458a266467STheodore Ts'o 	ext4_group_t g;
5346b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5347c5e06d10SJohann Lombardi 	int err = 0;
5348ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
534903dafb5fSChen Gang 	int i, j;
535033458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5351ac27a0ecSDave Kleikamp #endif
5352d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5353ac27a0ecSDave Kleikamp 
535421ac738eSChengguang Xu 	if (data && !orig_data)
535521ac738eSChengguang Xu 		return -ENOMEM;
535621ac738eSChengguang Xu 
5357ac27a0ecSDave Kleikamp 	/* Store the original options */
5358ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5359ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5360a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5361ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5362ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5363ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
536430773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
536530773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5366ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5367ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5368a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
536903dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
537033458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
537133458eabSTheodore Ts'o 
537233458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
537303dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
537403dafb5fSChen Gang 				for (j = 0; j < i; j++)
537503dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
53763e36a163SWei Yongjun 				kfree(orig_data);
537703dafb5fSChen Gang 				return -ENOMEM;
537803dafb5fSChen Gang 			}
537903dafb5fSChen Gang 		} else
538003dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5381ac27a0ecSDave Kleikamp #endif
5382b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5383b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5384ac27a0ecSDave Kleikamp 
5385661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5386ac27a0ecSDave Kleikamp 		err = -EINVAL;
5387ac27a0ecSDave Kleikamp 		goto restore_opts;
5388ac27a0ecSDave Kleikamp 	}
5389ac27a0ecSDave Kleikamp 
53906b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
53916b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
53926b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
53932d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
53942d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5395c6d3d56dSDarrick J. Wong 	}
5396c6d3d56dSDarrick J. Wong 
53976ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
53986ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
53996ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
54006ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
54016ae6514bSPiotr Sarna 			err = -EINVAL;
54026ae6514bSPiotr Sarna 			goto restore_opts;
54036ae6514bSPiotr Sarna 		}
54046ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
54056ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
54066ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
54076ae6514bSPiotr Sarna 			err = -EINVAL;
54086ae6514bSPiotr Sarna 			goto restore_opts;
54096ae6514bSPiotr Sarna 		}
5410923ae0ffSRoss Zwisler 		if (test_opt(sb, DAX)) {
5411923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5412923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5413923ae0ffSRoss Zwisler 			err = -EINVAL;
5414923ae0ffSRoss Zwisler 			goto restore_opts;
5415923ae0ffSRoss Zwisler 		}
5416ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5417ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5418ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5419ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5420ab04df78SJan Kara 			err = -EINVAL;
5421ab04df78SJan Kara 			goto restore_opts;
5422ab04df78SJan Kara 		}
5423923ae0ffSRoss Zwisler 	}
5424923ae0ffSRoss Zwisler 
5425cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5426cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5427cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5428cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5429cdb7ee4cSTahsin Erdogan 	}
5430cdb7ee4cSTahsin Erdogan 
5431923ae0ffSRoss Zwisler 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
5432923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5433923ae0ffSRoss Zwisler 			"dax flag with busy inodes while remounting");
5434923ae0ffSRoss Zwisler 		sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
54356ae6514bSPiotr Sarna 	}
54366ae6514bSPiotr Sarna 
54374ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
543854d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5439ac27a0ecSDave Kleikamp 
54401751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
54411751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5442ac27a0ecSDave Kleikamp 
5443ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5444ac27a0ecSDave Kleikamp 
5445b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5446617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5447b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5448b3881f74STheodore Ts'o 	}
5449ac27a0ecSDave Kleikamp 
54501751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
54511751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5452a2fd66d0STheodore Ts'o 
54531751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
54544ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5455ac27a0ecSDave Kleikamp 			err = -EROFS;
5456ac27a0ecSDave Kleikamp 			goto restore_opts;
5457ac27a0ecSDave Kleikamp 		}
5458ac27a0ecSDave Kleikamp 
54591751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
546038c03b34STheodore Ts'o 			err = sync_filesystem(sb);
546138c03b34STheodore Ts'o 			if (err < 0)
546238c03b34STheodore Ts'o 				goto restore_opts;
54630f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
54640f0dd62fSChristoph Hellwig 			if (err < 0)
5465c79d967dSChristoph Hellwig 				goto restore_opts;
5466c79d967dSChristoph Hellwig 
5467ac27a0ecSDave Kleikamp 			/*
5468ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5469ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5470ac27a0ecSDave Kleikamp 			 */
54711751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5472ac27a0ecSDave Kleikamp 
5473ac27a0ecSDave Kleikamp 			/*
5474ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5475ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5476ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5477ac27a0ecSDave Kleikamp 			 */
5478617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5479617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5480ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5481ac27a0ecSDave Kleikamp 
5482a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5483617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
54842dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
54852dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5486ac27a0ecSDave Kleikamp 		} else {
5487a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5488e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
54892cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5490ac27a0ecSDave Kleikamp 				err = -EROFS;
5491ac27a0ecSDave Kleikamp 				goto restore_opts;
5492ac27a0ecSDave Kleikamp 			}
5493ead6596bSEric Sandeen 			/*
54948a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
54950b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
54968a266467STheodore Ts'o 			 */
54978a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
54988a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
54998a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
55008a266467STheodore Ts'o 
5501feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5502b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5503b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5504e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
55058a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
55066a797d27SDarrick J. Wong 					err = -EFSBADCRC;
55078a266467STheodore Ts'o 					goto restore_opts;
55088a266467STheodore Ts'o 				}
55098a266467STheodore Ts'o 			}
55108a266467STheodore Ts'o 
55118a266467STheodore Ts'o 			/*
5512ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5513ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5514ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5515ead6596bSEric Sandeen 			 */
5516ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5517b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5518ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5519ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5520b31e1552SEric Sandeen 				       "umount/remount instead");
5521ead6596bSEric Sandeen 				err = -EINVAL;
5522ead6596bSEric Sandeen 				goto restore_opts;
5523ead6596bSEric Sandeen 			}
5524ead6596bSEric Sandeen 
5525ac27a0ecSDave Kleikamp 			/*
5526ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5527ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5528ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5529ac27a0ecSDave Kleikamp 			 * the partition.)
5530ac27a0ecSDave Kleikamp 			 */
55310390131bSFrank Mayhar 			if (sbi->s_journal)
5532617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5533ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5534c89128a0SJaegeuk Kim 
5535c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5536c89128a0SJaegeuk Kim 			if (err)
5537c89128a0SJaegeuk Kim 				goto restore_opts;
5538c89128a0SJaegeuk Kim 
55391751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5540e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5541c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5542c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5543c5e06d10SJohann Lombardi 					err = -EROFS;
5544c5e06d10SJohann Lombardi 					goto restore_opts;
5545c5e06d10SJohann Lombardi 				}
5546c79d967dSChristoph Hellwig 			enable_quota = 1;
5547ac27a0ecSDave Kleikamp 		}
5548ac27a0ecSDave Kleikamp 	}
5549bfff6873SLukas Czerner 
5550bfff6873SLukas Czerner 	/*
5551bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5552bfff6873SLukas Czerner 	 * current settings
5553bfff6873SLukas Czerner 	 */
5554bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5555bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5556bfff6873SLukas Czerner 	else {
5557bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5558bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5559bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5560bfff6873SLukas Czerner 	}
5561bfff6873SLukas Czerner 
55626fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5563c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5564c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5565c89128a0SJaegeuk Kim 		if (err)
5566c89128a0SJaegeuk Kim 			goto restore_opts;
5567c89128a0SJaegeuk Kim 	}
55680390131bSFrank Mayhar 
5569ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5570ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5571a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5572ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
55737c319d32SAditya Kali 	if (enable_quota) {
55747c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
55750f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5576e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
55777c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
557807724f98STheodore Ts'o 			if (err)
55797c319d32SAditya Kali 				goto restore_opts;
55807c319d32SAditya Kali 		}
55817c319d32SAditya Kali 	}
55827c319d32SAditya Kali #endif
5583d4c402d9SCurt Wohlgemuth 
55841751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5585d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5586d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5587ac27a0ecSDave Kleikamp 	return 0;
55880b8e58a1SAndreas Dilger 
5589ac27a0ecSDave Kleikamp restore_opts:
5590ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5591ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5592a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5593ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5594ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5595ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
559630773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
559730773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5598ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5599ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5600a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
560133458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
560233458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5603ac27a0ecSDave Kleikamp 	}
560433458eabSTheodore Ts'o 	synchronize_rcu();
560533458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
560633458eabSTheodore Ts'o 		kfree(to_free[i]);
5607ac27a0ecSDave Kleikamp #endif
5608d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5609ac27a0ecSDave Kleikamp 	return err;
5610ac27a0ecSDave Kleikamp }
5611ac27a0ecSDave Kleikamp 
5612689c958cSLi Xi #ifdef CONFIG_QUOTA
5613689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5614689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5615689c958cSLi Xi {
5616689c958cSLi Xi 	struct kqid qid;
5617689c958cSLi Xi 	struct dquot *dquot;
5618689c958cSLi Xi 	u64 limit;
5619689c958cSLi Xi 	u64 curblock;
5620689c958cSLi Xi 
5621689c958cSLi Xi 	qid = make_kqid_projid(projid);
5622689c958cSLi Xi 	dquot = dqget(sb, qid);
5623689c958cSLi Xi 	if (IS_ERR(dquot))
5624689c958cSLi Xi 		return PTR_ERR(dquot);
56257b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5626689c958cSLi Xi 
5627a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
5628a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
562957c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
563057c32ea4SChengguang Xu 
5631689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5632f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5633f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5634689c958cSLi Xi 		buf->f_blocks = limit;
5635689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5636689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5637689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5638689c958cSLi Xi 	}
5639689c958cSLi Xi 
5640a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
5641a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
5642689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5643689c958cSLi Xi 		buf->f_files = limit;
5644689c958cSLi Xi 		buf->f_ffree =
5645689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5646689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5647689c958cSLi Xi 	}
5648689c958cSLi Xi 
56497b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5650689c958cSLi Xi 	dqput(dquot);
5651689c958cSLi Xi 	return 0;
5652689c958cSLi Xi }
5653689c958cSLi Xi #endif
5654689c958cSLi Xi 
5655617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5656ac27a0ecSDave Kleikamp {
5657ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5658617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5659617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
566027dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5661960cc398SPekka Enberg 	u64 fsid;
5662d02a9391SKazuya Mio 	s64 bfree;
566327dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5664ac27a0ecSDave Kleikamp 
5665952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5666952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5667ac27a0ecSDave Kleikamp 
5668617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5669ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5670b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
567157042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
567257042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5673d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
567457042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
567527dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
567627dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
567727dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5678ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5679ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
568052d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5681617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5682960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5683960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5684960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5685960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
56860b8e58a1SAndreas Dilger 
5687689c958cSLi Xi #ifdef CONFIG_QUOTA
5688689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5689689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5690689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5691689c958cSLi Xi #endif
5692ac27a0ecSDave Kleikamp 	return 0;
5693ac27a0ecSDave Kleikamp }
5694ac27a0ecSDave Kleikamp 
5695ac27a0ecSDave Kleikamp 
5696ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5697ac27a0ecSDave Kleikamp 
5698bc8230eeSJan Kara /*
5699bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5700bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5701bc8230eeSJan Kara  */
5702ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5703ac27a0ecSDave Kleikamp {
57044c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5705ac27a0ecSDave Kleikamp }
5706ac27a0ecSDave Kleikamp 
5707617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5708ac27a0ecSDave Kleikamp {
5709ac27a0ecSDave Kleikamp 	int ret, err;
5710ac27a0ecSDave Kleikamp 	handle_t *handle;
5711ac27a0ecSDave Kleikamp 	struct inode *inode;
5712ac27a0ecSDave Kleikamp 
5713ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
57149924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5715617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5716ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5717ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5718ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5719617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5720ac27a0ecSDave Kleikamp 	if (!ret)
5721ac27a0ecSDave Kleikamp 		ret = err;
5722ac27a0ecSDave Kleikamp 	return ret;
5723ac27a0ecSDave Kleikamp }
5724ac27a0ecSDave Kleikamp 
5725617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5726ac27a0ecSDave Kleikamp {
5727ac27a0ecSDave Kleikamp 	int ret, err;
5728ac27a0ecSDave Kleikamp 	handle_t *handle;
5729ac27a0ecSDave Kleikamp 
57309924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5731617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5732ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5733ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5734ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5735617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5736ac27a0ecSDave Kleikamp 	if (!ret)
5737ac27a0ecSDave Kleikamp 		ret = err;
5738ac27a0ecSDave Kleikamp 	return ret;
5739ac27a0ecSDave Kleikamp }
5740ac27a0ecSDave Kleikamp 
5741617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5742ac27a0ecSDave Kleikamp {
5743ac27a0ecSDave Kleikamp 	int ret, err;
5744ac27a0ecSDave Kleikamp 	handle_t *handle;
5745ac27a0ecSDave Kleikamp 
57469924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5747617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
57489c3013e9SJan Kara 	if (IS_ERR(handle)) {
57499c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
57509c3013e9SJan Kara 		dquot_release(dquot);
5751ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
57529c3013e9SJan Kara 	}
5753ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5754617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5755ac27a0ecSDave Kleikamp 	if (!ret)
5756ac27a0ecSDave Kleikamp 		ret = err;
5757ac27a0ecSDave Kleikamp 	return ret;
5758ac27a0ecSDave Kleikamp }
5759ac27a0ecSDave Kleikamp 
5760617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5761ac27a0ecSDave Kleikamp {
5762262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5763262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5764262b4662SJan Kara 
57652c8be6b2SJan Kara 	/* Are we journaling quotas? */
5766e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5767262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5768ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5769617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5770ac27a0ecSDave Kleikamp 	} else {
5771ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5772ac27a0ecSDave Kleikamp 	}
5773ac27a0ecSDave Kleikamp }
5774ac27a0ecSDave Kleikamp 
5775617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5776ac27a0ecSDave Kleikamp {
5777ac27a0ecSDave Kleikamp 	int ret, err;
5778ac27a0ecSDave Kleikamp 	handle_t *handle;
5779ac27a0ecSDave Kleikamp 
5780ac27a0ecSDave Kleikamp 	/* Data block + inode block */
57812b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5782ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5783ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5784ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5785617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5786ac27a0ecSDave Kleikamp 	if (!ret)
5787ac27a0ecSDave Kleikamp 		ret = err;
5788ac27a0ecSDave Kleikamp 	return ret;
5789ac27a0ecSDave Kleikamp }
5790ac27a0ecSDave Kleikamp 
5791ac27a0ecSDave Kleikamp /*
5792ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5793ac27a0ecSDave Kleikamp  * the quota file and such...
5794ac27a0ecSDave Kleikamp  */
5795617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5796ac27a0ecSDave Kleikamp {
579733458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5798617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5799ac27a0ecSDave Kleikamp }
5800ac27a0ecSDave Kleikamp 
5801daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5802daf647d2STheodore Ts'o {
5803daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5804daf647d2STheodore Ts'o 
5805daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5806daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5807daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5808daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5809daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5810daf647d2STheodore Ts'o 	 */
5811daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5812daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5813daf647d2STheodore Ts'o }
5814daf647d2STheodore Ts'o 
5815ac27a0ecSDave Kleikamp /*
5816ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5817ac27a0ecSDave Kleikamp  */
5818617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
58198c54ca9cSAl Viro 			 const struct path *path)
5820ac27a0ecSDave Kleikamp {
5821ac27a0ecSDave Kleikamp 	int err;
5822ac27a0ecSDave Kleikamp 
5823ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5824ac27a0ecSDave Kleikamp 		return -EINVAL;
58250623543bSJan Kara 
5826ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5827d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5828ac27a0ecSDave Kleikamp 		return -EXDEV;
58290623543bSJan Kara 	/* Journaling quota? */
58300623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
58312b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5832f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5833b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5834b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5835b31e1552SEric Sandeen 				"Journaled quota will not work");
583691389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
583791389240SJan Kara 	} else {
583891389240SJan Kara 		/*
583991389240SJan Kara 		 * Clear the flag just in case mount options changed since
584091389240SJan Kara 		 * last time.
584191389240SJan Kara 		 */
584291389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
58430623543bSJan Kara 	}
58440623543bSJan Kara 
58450623543bSJan Kara 	/*
58460623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
58470623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
58480623543bSJan Kara 	 */
58490390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
58502b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
58510623543bSJan Kara 		/*
58520623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
58530623543bSJan Kara 		 * otherwise be livelocked...
58540623543bSJan Kara 		 */
58550623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
58567ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
58570623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5858f00c9e44SJan Kara 		if (err)
58597ffe1ea8SHidehiro Kawai 			return err;
58607ffe1ea8SHidehiro Kawai 	}
5861957153fcSJan Kara 
5862daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5863daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5864957153fcSJan Kara 	if (err) {
5865daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5866daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5867957153fcSJan Kara 	} else {
5868957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5869957153fcSJan Kara 		handle_t *handle;
5870957153fcSJan Kara 
587161a92987SJan Kara 		/*
587261a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
587361a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
587461a92987SJan Kara 		 * are already enabled and this is not a hard failure.
587561a92987SJan Kara 		 */
5876957153fcSJan Kara 		inode_lock(inode);
5877957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5878957153fcSJan Kara 		if (IS_ERR(handle))
5879957153fcSJan Kara 			goto unlock_inode;
5880957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5881957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5882957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5883*4209ae12SHarshad Shirwadkar 		err = ext4_mark_inode_dirty(handle, inode);
5884957153fcSJan Kara 		ext4_journal_stop(handle);
5885957153fcSJan Kara 	unlock_inode:
5886957153fcSJan Kara 		inode_unlock(inode);
5887957153fcSJan Kara 	}
5888daf647d2STheodore Ts'o 	return err;
5889ac27a0ecSDave Kleikamp }
5890ac27a0ecSDave Kleikamp 
58917c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
58927c319d32SAditya Kali 			     unsigned int flags)
58937c319d32SAditya Kali {
58947c319d32SAditya Kali 	int err;
58957c319d32SAditya Kali 	struct inode *qf_inode;
5896a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
58977c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5898689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5899689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
59007c319d32SAditya Kali 	};
59017c319d32SAditya Kali 
5902e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
59037c319d32SAditya Kali 
59047c319d32SAditya Kali 	if (!qf_inums[type])
59057c319d32SAditya Kali 		return -EPERM;
59067c319d32SAditya Kali 
59078a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
59087c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
59097c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
59107c319d32SAditya Kali 		return PTR_ERR(qf_inode);
59117c319d32SAditya Kali 	}
59127c319d32SAditya Kali 
5913bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5914bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5915daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
59167212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
5917daf647d2STheodore Ts'o 	if (err)
5918daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
591961157b24SPan Bian 	iput(qf_inode);
59207c319d32SAditya Kali 
59217c319d32SAditya Kali 	return err;
59227c319d32SAditya Kali }
59237c319d32SAditya Kali 
59247c319d32SAditya Kali /* Enable usage tracking for all quota types. */
59257c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
59267c319d32SAditya Kali {
59277c319d32SAditya Kali 	int type, err = 0;
5928a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
59297c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5930689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5931689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
59327c319d32SAditya Kali 	};
593349da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
593449da9392SJan Kara 		test_opt(sb, USRQUOTA),
593549da9392SJan Kara 		test_opt(sb, GRPQUOTA),
593649da9392SJan Kara 		test_opt(sb, PRJQUOTA),
593749da9392SJan Kara 	};
59387c319d32SAditya Kali 
593991389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5940a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
59417c319d32SAditya Kali 		if (qf_inums[type]) {
59427c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
594349da9392SJan Kara 				DQUOT_USAGE_ENABLED |
594449da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
59457c319d32SAditya Kali 			if (err) {
59467c319d32SAditya Kali 				ext4_warning(sb,
594772ba7450STheodore Ts'o 					"Failed to enable quota tracking "
594872ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
594972ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
59507f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
59517f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
59527f144fd0SJunichi Uekawa 
59537c319d32SAditya Kali 				return err;
59547c319d32SAditya Kali 			}
59557c319d32SAditya Kali 		}
59567c319d32SAditya Kali 	}
59577c319d32SAditya Kali 	return 0;
59587c319d32SAditya Kali }
59597c319d32SAditya Kali 
5960ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
5961ca0e05e4SDmitry Monakhov {
596221f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
596321f97697SJan Kara 	handle_t *handle;
5964957153fcSJan Kara 	int err;
596521f97697SJan Kara 
596687009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
596787009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
596887009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
5969ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
5970ca0e05e4SDmitry Monakhov 
5971957153fcSJan Kara 	if (!inode || !igrab(inode))
59720b268590SAmir Goldstein 		goto out;
59730b268590SAmir Goldstein 
5974957153fcSJan Kara 	err = dquot_quota_off(sb, type);
5975964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
5976957153fcSJan Kara 		goto out_put;
5977957153fcSJan Kara 
5978957153fcSJan Kara 	inode_lock(inode);
597961a92987SJan Kara 	/*
598061a92987SJan Kara 	 * Update modification times of quota files when userspace can
598161a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
598261a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
598361a92987SJan Kara 	 */
59849924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5985*4209ae12SHarshad Shirwadkar 	if (IS_ERR(handle)) {
5986*4209ae12SHarshad Shirwadkar 		err = PTR_ERR(handle);
5987957153fcSJan Kara 		goto out_unlock;
5988*4209ae12SHarshad Shirwadkar 	}
5989957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
5990957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
5991eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
5992*4209ae12SHarshad Shirwadkar 	err = ext4_mark_inode_dirty(handle, inode);
599321f97697SJan Kara 	ext4_journal_stop(handle);
5994957153fcSJan Kara out_unlock:
5995957153fcSJan Kara 	inode_unlock(inode);
5996957153fcSJan Kara out_put:
5997964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
5998957153fcSJan Kara 	iput(inode);
5999957153fcSJan Kara 	return err;
600021f97697SJan Kara out:
6001ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6002ca0e05e4SDmitry Monakhov }
6003ca0e05e4SDmitry Monakhov 
6004ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6005ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6006ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6007ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6008617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6009ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6010ac27a0ecSDave Kleikamp {
6011ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6012725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6013ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6014ac27a0ecSDave Kleikamp 	int tocopy;
6015ac27a0ecSDave Kleikamp 	size_t toread;
6016ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6017ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6018ac27a0ecSDave Kleikamp 
6019ac27a0ecSDave Kleikamp 	if (off > i_size)
6020ac27a0ecSDave Kleikamp 		return 0;
6021ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6022ac27a0ecSDave Kleikamp 		len = i_size-off;
6023ac27a0ecSDave Kleikamp 	toread = len;
6024ac27a0ecSDave Kleikamp 	while (toread > 0) {
6025ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6026ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
60271c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
60281c215028STheodore Ts'o 		if (IS_ERR(bh))
60291c215028STheodore Ts'o 			return PTR_ERR(bh);
6030ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6031ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6032ac27a0ecSDave Kleikamp 		else
6033ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6034ac27a0ecSDave Kleikamp 		brelse(bh);
6035ac27a0ecSDave Kleikamp 		offset = 0;
6036ac27a0ecSDave Kleikamp 		toread -= tocopy;
6037ac27a0ecSDave Kleikamp 		data += tocopy;
6038ac27a0ecSDave Kleikamp 		blk++;
6039ac27a0ecSDave Kleikamp 	}
6040ac27a0ecSDave Kleikamp 	return len;
6041ac27a0ecSDave Kleikamp }
6042ac27a0ecSDave Kleikamp 
6043ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6044ac27a0ecSDave Kleikamp  * enough credits) */
6045617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6046ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6047ac27a0ecSDave Kleikamp {
6048ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6049725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6050*4209ae12SHarshad Shirwadkar 	int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
6051c5e298aeSTheodore Ts'o 	int retries = 0;
6052ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6053ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6054ac27a0ecSDave Kleikamp 
60550390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6056b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6057b31e1552SEric Sandeen 			" cancelled because transaction is not started",
60589c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
60599c3013e9SJan Kara 		return -EIO;
60609c3013e9SJan Kara 	}
606167eeb568SDmitry Monakhov 	/*
606267eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
606367eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
606467eeb568SDmitry Monakhov 	 */
606567eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
606667eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
606767eeb568SDmitry Monakhov 			" cancelled because not block aligned",
606867eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
606967eeb568SDmitry Monakhov 		return -EIO;
607067eeb568SDmitry Monakhov 	}
607167eeb568SDmitry Monakhov 
6072c5e298aeSTheodore Ts'o 	do {
6073c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6074c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6075c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
607645586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6077c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
60781c215028STheodore Ts'o 	if (IS_ERR(bh))
60791c215028STheodore Ts'o 		return PTR_ERR(bh);
6080ac27a0ecSDave Kleikamp 	if (!bh)
6081ac27a0ecSDave Kleikamp 		goto out;
60825d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6083617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6084ac27a0ecSDave Kleikamp 	if (err) {
6085ac27a0ecSDave Kleikamp 		brelse(bh);
60861c215028STheodore Ts'o 		return err;
6087ac27a0ecSDave Kleikamp 	}
6088ac27a0ecSDave Kleikamp 	lock_buffer(bh);
608967eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6090ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6091ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
60920390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6093ac27a0ecSDave Kleikamp 	brelse(bh);
6094ac27a0ecSDave Kleikamp out:
609567eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
609667eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6097617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
6098*4209ae12SHarshad Shirwadkar 		err2 = ext4_mark_inode_dirty(handle, inode);
6099*4209ae12SHarshad Shirwadkar 		if (unlikely(err2 && !err))
6100*4209ae12SHarshad Shirwadkar 			err = err2;
610121f97697SJan Kara 	}
6102*4209ae12SHarshad Shirwadkar 	return err ? err : len;
6103ac27a0ecSDave Kleikamp }
6104ac27a0ecSDave Kleikamp #endif
6105ac27a0ecSDave Kleikamp 
6106152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6107152a0836SAl Viro 		       const char *dev_name, void *data)
6108ac27a0ecSDave Kleikamp {
6109152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6110ac27a0ecSDave Kleikamp }
6111ac27a0ecSDave Kleikamp 
6112c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
611324b58424STheodore Ts'o static inline void register_as_ext2(void)
611424b58424STheodore Ts'o {
611524b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
611624b58424STheodore Ts'o 	if (err)
611724b58424STheodore Ts'o 		printk(KERN_WARNING
611824b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
611924b58424STheodore Ts'o }
612024b58424STheodore Ts'o 
612124b58424STheodore Ts'o static inline void unregister_as_ext2(void)
612224b58424STheodore Ts'o {
612324b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
612424b58424STheodore Ts'o }
61252035e776STheodore Ts'o 
61262035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
61272035e776STheodore Ts'o {
6128e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
61292035e776STheodore Ts'o 		return 0;
6130bc98a42cSDavid Howells 	if (sb_rdonly(sb))
61312035e776STheodore Ts'o 		return 1;
6132e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
61332035e776STheodore Ts'o 		return 0;
61342035e776STheodore Ts'o 	return 1;
61352035e776STheodore Ts'o }
613624b58424STheodore Ts'o #else
613724b58424STheodore Ts'o static inline void register_as_ext2(void) { }
613824b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
61392035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
614024b58424STheodore Ts'o #endif
614124b58424STheodore Ts'o 
614224b58424STheodore Ts'o static inline void register_as_ext3(void)
614324b58424STheodore Ts'o {
614424b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
614524b58424STheodore Ts'o 	if (err)
614624b58424STheodore Ts'o 		printk(KERN_WARNING
614724b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
614824b58424STheodore Ts'o }
614924b58424STheodore Ts'o 
615024b58424STheodore Ts'o static inline void unregister_as_ext3(void)
615124b58424STheodore Ts'o {
615224b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
615324b58424STheodore Ts'o }
61542035e776STheodore Ts'o 
61552035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
61562035e776STheodore Ts'o {
6157e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
61582035e776STheodore Ts'o 		return 0;
6159e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
61602035e776STheodore Ts'o 		return 0;
6161bc98a42cSDavid Howells 	if (sb_rdonly(sb))
61622035e776STheodore Ts'o 		return 1;
6163e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
61642035e776STheodore Ts'o 		return 0;
61652035e776STheodore Ts'o 	return 1;
61662035e776STheodore Ts'o }
616724b58424STheodore Ts'o 
616803010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6169ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
617003010a33STheodore Ts'o 	.name		= "ext4",
6171152a0836SAl Viro 	.mount		= ext4_mount,
6172ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6173ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6174ac27a0ecSDave Kleikamp };
61757f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6176ac27a0ecSDave Kleikamp 
6177e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6178e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6179e9e3bcecSEric Sandeen 
61805dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6181ac27a0ecSDave Kleikamp {
6182e9e3bcecSEric Sandeen 	int i, err;
6183c9de560dSAlex Tomas 
6184e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
618507c0c5d8SAl Viro 	ext4_li_info = NULL;
618607c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
618707c0c5d8SAl Viro 
61889a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
618912e9b892SDmitry Monakhov 	ext4_check_flag_values();
6190e9e3bcecSEric Sandeen 
6191e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6192e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6193e9e3bcecSEric Sandeen 
619451865fdaSZheng Liu 	err = ext4_init_es();
61956fd058f7STheodore Ts'o 	if (err)
61966fd058f7STheodore Ts'o 		return err;
619751865fdaSZheng Liu 
61981dc0aa46SEric Whitney 	err = ext4_init_pending();
61991dc0aa46SEric Whitney 	if (err)
620022cfe4b4SEric Biggers 		goto out7;
620122cfe4b4SEric Biggers 
620222cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
620322cfe4b4SEric Biggers 	if (err)
62041dc0aa46SEric Whitney 		goto out6;
62051dc0aa46SEric Whitney 
620651865fdaSZheng Liu 	err = ext4_init_pageio();
620751865fdaSZheng Liu 	if (err)
6208b5799018STheodore Ts'o 		goto out5;
620951865fdaSZheng Liu 
62105dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6211bd2d0210STheodore Ts'o 	if (err)
6212b5799018STheodore Ts'o 		goto out4;
6213857ac889SLukas Czerner 
6214b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6215dd68314cSTheodore Ts'o 	if (err)
6216b5799018STheodore Ts'o 		goto out3;
6217857ac889SLukas Czerner 
62185dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6219ac27a0ecSDave Kleikamp 	if (err)
6220c9de560dSAlex Tomas 		goto out2;
6221ac27a0ecSDave Kleikamp 	err = init_inodecache();
6222ac27a0ecSDave Kleikamp 	if (err)
6223ac27a0ecSDave Kleikamp 		goto out1;
622424b58424STheodore Ts'o 	register_as_ext3();
62252035e776STheodore Ts'o 	register_as_ext2();
622603010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6227ac27a0ecSDave Kleikamp 	if (err)
6228ac27a0ecSDave Kleikamp 		goto out;
6229bfff6873SLukas Czerner 
6230ac27a0ecSDave Kleikamp 	return 0;
6231ac27a0ecSDave Kleikamp out:
623224b58424STheodore Ts'o 	unregister_as_ext2();
623324b58424STheodore Ts'o 	unregister_as_ext3();
6234ac27a0ecSDave Kleikamp 	destroy_inodecache();
6235ac27a0ecSDave Kleikamp out1:
62365dabfc78STheodore Ts'o 	ext4_exit_mballoc();
62379c191f70ST Makphaibulchoke out2:
6238b5799018STheodore Ts'o 	ext4_exit_sysfs();
6239b5799018STheodore Ts'o out3:
6240dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6241b5799018STheodore Ts'o out4:
62425dabfc78STheodore Ts'o 	ext4_exit_pageio();
6243b5799018STheodore Ts'o out5:
624422cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
62451dc0aa46SEric Whitney out6:
624622cfe4b4SEric Biggers 	ext4_exit_pending();
624722cfe4b4SEric Biggers out7:
624851865fdaSZheng Liu 	ext4_exit_es();
624951865fdaSZheng Liu 
6250ac27a0ecSDave Kleikamp 	return err;
6251ac27a0ecSDave Kleikamp }
6252ac27a0ecSDave Kleikamp 
62535dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6254ac27a0ecSDave Kleikamp {
6255bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
625624b58424STheodore Ts'o 	unregister_as_ext2();
625724b58424STheodore Ts'o 	unregister_as_ext3();
625803010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6259ac27a0ecSDave Kleikamp 	destroy_inodecache();
62605dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6261b5799018STheodore Ts'o 	ext4_exit_sysfs();
62625dabfc78STheodore Ts'o 	ext4_exit_system_zone();
62635dabfc78STheodore Ts'o 	ext4_exit_pageio();
626422cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6265dd12ed14SEric Sandeen 	ext4_exit_es();
62661dc0aa46SEric Whitney 	ext4_exit_pending();
6267ac27a0ecSDave Kleikamp }
6268ac27a0ecSDave Kleikamp 
6269ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
627083982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6271ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
62727ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
62735dabfc78STheodore Ts'o module_init(ext4_init_fs)
62745dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6275