xref: /linux/fs/ext4/super.c (revision 9cb20f94afcd2964944f9468e38da736ee855b19)
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 			 */
1351043546e4SIra Weiny 			ext4_set_inode_flags(inode, false);
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 		 */
1378043546e4SIra Weiny 		ext4_set_inode_flags(inode, false);
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,
1515*9cb20f94SIra Weiny 	Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
1516*9cb20f94SIra Weiny 	Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
1517327eaf73STheodore Ts'o 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1518327eaf73STheodore Ts'o 	Opt_nowarn_on_error, Opt_mblk_io_submit,
1519670e9875STheodore Ts'o 	Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
15201449032bSTheodore Ts'o 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
15215328e635SEric Sandeen 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1522744692dcSJiaying Zhang 	Opt_dioread_nolock, Opt_dioread_lock,
1523fc6cb1cdSTheodore Ts'o 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1524cdb7ee4cSTahsin Erdogan 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
1525ac27a0ecSDave Kleikamp };
1526ac27a0ecSDave Kleikamp 
1527a447c093SSteven Whitehouse static const match_table_t tokens = {
1528ac27a0ecSDave Kleikamp 	{Opt_bsd_df, "bsddf"},
1529ac27a0ecSDave Kleikamp 	{Opt_minix_df, "minixdf"},
1530ac27a0ecSDave Kleikamp 	{Opt_grpid, "grpid"},
1531ac27a0ecSDave Kleikamp 	{Opt_grpid, "bsdgroups"},
1532ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "nogrpid"},
1533ac27a0ecSDave Kleikamp 	{Opt_nogrpid, "sysvgroups"},
1534ac27a0ecSDave Kleikamp 	{Opt_resgid, "resgid=%u"},
1535ac27a0ecSDave Kleikamp 	{Opt_resuid, "resuid=%u"},
1536ac27a0ecSDave Kleikamp 	{Opt_sb, "sb=%u"},
1537ac27a0ecSDave Kleikamp 	{Opt_err_cont, "errors=continue"},
1538ac27a0ecSDave Kleikamp 	{Opt_err_panic, "errors=panic"},
1539ac27a0ecSDave Kleikamp 	{Opt_err_ro, "errors=remount-ro"},
1540ac27a0ecSDave Kleikamp 	{Opt_nouid32, "nouid32"},
1541ac27a0ecSDave Kleikamp 	{Opt_debug, "debug"},
154272578c33STheodore Ts'o 	{Opt_removed, "oldalloc"},
154372578c33STheodore Ts'o 	{Opt_removed, "orlov"},
1544ac27a0ecSDave Kleikamp 	{Opt_user_xattr, "user_xattr"},
1545ac27a0ecSDave Kleikamp 	{Opt_nouser_xattr, "nouser_xattr"},
1546ac27a0ecSDave Kleikamp 	{Opt_acl, "acl"},
1547ac27a0ecSDave Kleikamp 	{Opt_noacl, "noacl"},
1548e3bb52aeSEric Sandeen 	{Opt_noload, "norecovery"},
15495a916be1STheodore Ts'o 	{Opt_noload, "noload"},
155072578c33STheodore Ts'o 	{Opt_removed, "nobh"},
155172578c33STheodore Ts'o 	{Opt_removed, "bh"},
1552ac27a0ecSDave Kleikamp 	{Opt_commit, "commit=%u"},
155330773840STheodore Ts'o 	{Opt_min_batch_time, "min_batch_time=%u"},
155430773840STheodore Ts'o 	{Opt_max_batch_time, "max_batch_time=%u"},
1555ac27a0ecSDave Kleikamp 	{Opt_journal_dev, "journal_dev=%u"},
1556ad4eec61SEric Sandeen 	{Opt_journal_path, "journal_path=%s"},
1557818d276cSGirish Shilamkar 	{Opt_journal_checksum, "journal_checksum"},
1558c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, "nojournal_checksum"},
1559818d276cSGirish Shilamkar 	{Opt_journal_async_commit, "journal_async_commit"},
1560ac27a0ecSDave Kleikamp 	{Opt_abort, "abort"},
1561ac27a0ecSDave Kleikamp 	{Opt_data_journal, "data=journal"},
1562ac27a0ecSDave Kleikamp 	{Opt_data_ordered, "data=ordered"},
1563ac27a0ecSDave Kleikamp 	{Opt_data_writeback, "data=writeback"},
15645bf5683aSHidehiro Kawai 	{Opt_data_err_abort, "data_err=abort"},
15655bf5683aSHidehiro Kawai 	{Opt_data_err_ignore, "data_err=ignore"},
1566ac27a0ecSDave Kleikamp 	{Opt_offusrjquota, "usrjquota="},
1567ac27a0ecSDave Kleikamp 	{Opt_usrjquota, "usrjquota=%s"},
1568ac27a0ecSDave Kleikamp 	{Opt_offgrpjquota, "grpjquota="},
1569ac27a0ecSDave Kleikamp 	{Opt_grpjquota, "grpjquota=%s"},
1570ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1571ac27a0ecSDave Kleikamp 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
15725a20bdfcSJan Kara 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1573ac27a0ecSDave Kleikamp 	{Opt_grpquota, "grpquota"},
1574ac27a0ecSDave Kleikamp 	{Opt_noquota, "noquota"},
1575ac27a0ecSDave Kleikamp 	{Opt_quota, "quota"},
1576ac27a0ecSDave Kleikamp 	{Opt_usrquota, "usrquota"},
157749da9392SJan Kara 	{Opt_prjquota, "prjquota"},
1578ac27a0ecSDave Kleikamp 	{Opt_barrier, "barrier=%u"},
157906705bffSTheodore Ts'o 	{Opt_barrier, "barrier"},
158006705bffSTheodore Ts'o 	{Opt_nobarrier, "nobarrier"},
158125ec56b5SJean Noel Cordenner 	{Opt_i_version, "i_version"},
1582923ae0ffSRoss Zwisler 	{Opt_dax, "dax"},
1583*9cb20f94SIra Weiny 	{Opt_dax_always, "dax=always"},
1584*9cb20f94SIra Weiny 	{Opt_dax_inode, "dax=inode"},
1585*9cb20f94SIra Weiny 	{Opt_dax_never, "dax=never"},
1586c9de560dSAlex Tomas 	{Opt_stripe, "stripe=%u"},
158764769240SAlex Tomas 	{Opt_delalloc, "delalloc"},
1588327eaf73STheodore Ts'o 	{Opt_warn_on_error, "warn_on_error"},
1589327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, "nowarn_on_error"},
1590a26f4992STheodore Ts'o 	{Opt_lazytime, "lazytime"},
1591a26f4992STheodore Ts'o 	{Opt_nolazytime, "nolazytime"},
1592670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
1593dd919b98SAneesh Kumar K.V 	{Opt_nodelalloc, "nodelalloc"},
159436ade451SJan Kara 	{Opt_removed, "mblk_io_submit"},
159536ade451SJan Kara 	{Opt_removed, "nomblk_io_submit"},
15966fd058f7STheodore Ts'o 	{Opt_block_validity, "block_validity"},
15976fd058f7STheodore Ts'o 	{Opt_noblock_validity, "noblock_validity"},
1598240799cdSTheodore Ts'o 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1599b3881f74STheodore Ts'o 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1600afd4672dSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
160106705bffSTheodore Ts'o 	{Opt_auto_da_alloc, "auto_da_alloc"},
160206705bffSTheodore Ts'o 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1603744692dcSJiaying Zhang 	{Opt_dioread_nolock, "dioread_nolock"},
1604244adf64STheodore Ts'o 	{Opt_dioread_lock, "nodioread_nolock"},
1605744692dcSJiaying Zhang 	{Opt_dioread_lock, "dioread_lock"},
16065328e635SEric Sandeen 	{Opt_discard, "discard"},
16075328e635SEric Sandeen 	{Opt_nodiscard, "nodiscard"},
1608fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable=%u"},
1609fc6cb1cdSTheodore Ts'o 	{Opt_init_itable, "init_itable"},
1610fc6cb1cdSTheodore Ts'o 	{Opt_noinit_itable, "noinit_itable"},
1611df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
16126ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1613cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "nombcache"},
1614cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, "no_mbcache"},	/* for backward compatibility */
1615c7198b9cSTheodore Ts'o 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1616c7198b9cSTheodore Ts'o 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1617c7198b9cSTheodore Ts'o 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1618c7198b9cSTheodore Ts'o 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1619c7198b9cSTheodore Ts'o 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1620f3f12faaSJosef Bacik 	{Opt_err, NULL},
1621ac27a0ecSDave Kleikamp };
1622ac27a0ecSDave Kleikamp 
1623617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data)
1624ac27a0ecSDave Kleikamp {
1625617ba13bSMingming Cao 	ext4_fsblk_t	sb_block;
1626ac27a0ecSDave Kleikamp 	char		*options = (char *) *data;
1627ac27a0ecSDave Kleikamp 
1628ac27a0ecSDave Kleikamp 	if (!options || strncmp(options, "sb=", 3) != 0)
1629ac27a0ecSDave Kleikamp 		return 1;	/* Default location */
16300b8e58a1SAndreas Dilger 
1631ac27a0ecSDave Kleikamp 	options += 3;
16320b8e58a1SAndreas Dilger 	/* TODO: use simple_strtoll with >32bit ext4 */
1633ac27a0ecSDave Kleikamp 	sb_block = simple_strtoul(options, &options, 0);
1634ac27a0ecSDave Kleikamp 	if (*options && *options != ',') {
16354776004fSTheodore Ts'o 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1636ac27a0ecSDave Kleikamp 		       (char *) *data);
1637ac27a0ecSDave Kleikamp 		return 1;
1638ac27a0ecSDave Kleikamp 	}
1639ac27a0ecSDave Kleikamp 	if (*options == ',')
1640ac27a0ecSDave Kleikamp 		options++;
1641ac27a0ecSDave Kleikamp 	*data = (void *) options;
16420b8e58a1SAndreas Dilger 
1643ac27a0ecSDave Kleikamp 	return sb_block;
1644ac27a0ecSDave Kleikamp }
1645ac27a0ecSDave Kleikamp 
1646b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1647d6006186SEric Biggers static const char deprecated_msg[] =
1648d6006186SEric Biggers 	"Mount option \"%s\" will be removed by %s\n"
1649437ca0fdSDmitry Monakhov 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1650b3881f74STheodore Ts'o 
165156c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA
165256c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
165356c50f11SDmitry Monakhov {
165456c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
165533458eabSTheodore Ts'o 	char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
165603dafb5fSChen Gang 	int ret = -1;
165756c50f11SDmitry Monakhov 
165833458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && !old_qname) {
165956c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
166056c50f11SDmitry Monakhov 			"Cannot change journaled "
166156c50f11SDmitry Monakhov 			"quota options when quota turned on");
166257f73c2cSTheodore Ts'o 		return -1;
166356c50f11SDmitry Monakhov 	}
1664e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb)) {
1665c325a67cSTheodore Ts'o 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1666c325a67cSTheodore Ts'o 			 "ignored when QUOTA feature is enabled");
1667c325a67cSTheodore Ts'o 		return 1;
1668262b4662SJan Kara 	}
166956c50f11SDmitry Monakhov 	qname = match_strdup(args);
167056c50f11SDmitry Monakhov 	if (!qname) {
167156c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
167256c50f11SDmitry Monakhov 			"Not enough memory for storing quotafile name");
167357f73c2cSTheodore Ts'o 		return -1;
167456c50f11SDmitry Monakhov 	}
167533458eabSTheodore Ts'o 	if (old_qname) {
167633458eabSTheodore Ts'o 		if (strcmp(old_qname, qname) == 0)
167703dafb5fSChen Gang 			ret = 1;
167803dafb5fSChen Gang 		else
167956c50f11SDmitry Monakhov 			ext4_msg(sb, KERN_ERR,
168003dafb5fSChen Gang 				 "%s quota file already specified",
168103dafb5fSChen Gang 				 QTYPE2NAME(qtype));
168203dafb5fSChen Gang 		goto errout;
168356c50f11SDmitry Monakhov 	}
168403dafb5fSChen Gang 	if (strchr(qname, '/')) {
168556c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR,
168656c50f11SDmitry Monakhov 			"quotafile must be on filesystem root");
168703dafb5fSChen Gang 		goto errout;
168856c50f11SDmitry Monakhov 	}
168933458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
1690fd8c37ecSTheodore Ts'o 	set_opt(sb, QUOTA);
169156c50f11SDmitry Monakhov 	return 1;
169203dafb5fSChen Gang errout:
169303dafb5fSChen Gang 	kfree(qname);
169403dafb5fSChen Gang 	return ret;
169556c50f11SDmitry Monakhov }
169656c50f11SDmitry Monakhov 
169756c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype)
169856c50f11SDmitry Monakhov {
169956c50f11SDmitry Monakhov 
170056c50f11SDmitry Monakhov 	struct ext4_sb_info *sbi = EXT4_SB(sb);
170133458eabSTheodore Ts'o 	char *old_qname = get_qf_name(sb, sbi, qtype);
170256c50f11SDmitry Monakhov 
170333458eabSTheodore Ts'o 	if (sb_any_quota_loaded(sb) && old_qname) {
170456c50f11SDmitry Monakhov 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
170556c50f11SDmitry Monakhov 			" when quota turned on");
170657f73c2cSTheodore Ts'o 		return -1;
170756c50f11SDmitry Monakhov 	}
170833458eabSTheodore Ts'o 	rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
170933458eabSTheodore Ts'o 	synchronize_rcu();
171033458eabSTheodore Ts'o 	kfree(old_qname);
171156c50f11SDmitry Monakhov 	return 1;
171256c50f11SDmitry Monakhov }
171356c50f11SDmitry Monakhov #endif
171456c50f11SDmitry Monakhov 
171526092bf5STheodore Ts'o #define MOPT_SET	0x0001
171626092bf5STheodore Ts'o #define MOPT_CLEAR	0x0002
171726092bf5STheodore Ts'o #define MOPT_NOSUPPORT	0x0004
171826092bf5STheodore Ts'o #define MOPT_EXPLICIT	0x0008
171926092bf5STheodore Ts'o #define MOPT_CLEAR_ERR	0x0010
172026092bf5STheodore Ts'o #define MOPT_GTE0	0x0020
172126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
172226092bf5STheodore Ts'o #define MOPT_Q		0
172326092bf5STheodore Ts'o #define MOPT_QFMT	0x0040
172426092bf5STheodore Ts'o #else
172526092bf5STheodore Ts'o #define MOPT_Q		MOPT_NOSUPPORT
172626092bf5STheodore Ts'o #define MOPT_QFMT	MOPT_NOSUPPORT
172726092bf5STheodore Ts'o #endif
172826092bf5STheodore Ts'o #define MOPT_DATAJ	0x0080
17298dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2	0x0100
17308dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3	0x0200
17318dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1732ad4eec61SEric Sandeen #define MOPT_STRING	0x0400
1733*9cb20f94SIra Weiny #define MOPT_SKIP	0x0800
173426092bf5STheodore Ts'o 
173526092bf5STheodore Ts'o static const struct mount_opts {
173626092bf5STheodore Ts'o 	int	token;
173726092bf5STheodore Ts'o 	int	mount_opt;
173826092bf5STheodore Ts'o 	int	flags;
173926092bf5STheodore Ts'o } ext4_mount_opts[] = {
174026092bf5STheodore Ts'o 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
174126092bf5STheodore Ts'o 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
174226092bf5STheodore Ts'o 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
174326092bf5STheodore Ts'o 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
174426092bf5STheodore Ts'o 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
174526092bf5STheodore Ts'o 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
17468dc0aa8cSTheodore Ts'o 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
17478dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET},
17488dc0aa8cSTheodore Ts'o 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
17498dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
175026092bf5STheodore Ts'o 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
175126092bf5STheodore Ts'o 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
17528dc0aa8cSTheodore Ts'o 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
17538dc0aa8cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17548dc0aa8cSTheodore Ts'o 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
175559d9fa5cSTheodore Ts'o 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1756327eaf73STheodore Ts'o 	{Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1757327eaf73STheodore Ts'o 	{Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
1758c6d3d56dSDarrick J. Wong 	{Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1759c6d3d56dSDarrick J. Wong 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
17608dc0aa8cSTheodore Ts'o 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
17611e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
176226092bf5STheodore Ts'o 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
17638dc0aa8cSTheodore Ts'o 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
17641e381f60SDmitry Monakhov 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
17658dc0aa8cSTheodore Ts'o 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
176626092bf5STheodore Ts'o 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
176726092bf5STheodore Ts'o 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
176826092bf5STheodore Ts'o 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
17698dc0aa8cSTheodore Ts'o 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
17707915a861SAles Novak 	 MOPT_NO_EXT2},
17718dc0aa8cSTheodore Ts'o 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
17727915a861SAles Novak 	 MOPT_NO_EXT2},
177326092bf5STheodore Ts'o 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
177426092bf5STheodore Ts'o 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
177526092bf5STheodore Ts'o 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
177626092bf5STheodore Ts'o 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
177726092bf5STheodore Ts'o 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
177826092bf5STheodore Ts'o 	{Opt_commit, 0, MOPT_GTE0},
177926092bf5STheodore Ts'o 	{Opt_max_batch_time, 0, MOPT_GTE0},
178026092bf5STheodore Ts'o 	{Opt_min_batch_time, 0, MOPT_GTE0},
178126092bf5STheodore Ts'o 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
178226092bf5STheodore Ts'o 	{Opt_init_itable, 0, MOPT_GTE0},
1783*9cb20f94SIra Weiny 	{Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
1784*9cb20f94SIra Weiny 	{Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
1785*9cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1786*9cb20f94SIra Weiny 	{Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
1787*9cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1788*9cb20f94SIra Weiny 	{Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
1789*9cb20f94SIra Weiny 		MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
179026092bf5STheodore Ts'o 	{Opt_stripe, 0, MOPT_GTE0},
17910efb3b23SJan Kara 	{Opt_resuid, 0, MOPT_GTE0},
17920efb3b23SJan Kara 	{Opt_resgid, 0, MOPT_GTE0},
17935ba92bcfSCarlos Maiolino 	{Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17945ba92bcfSCarlos Maiolino 	{Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
17955ba92bcfSCarlos Maiolino 	{Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
17968dc0aa8cSTheodore Ts'o 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17978dc0aa8cSTheodore Ts'o 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
17988dc0aa8cSTheodore Ts'o 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
17998dc0aa8cSTheodore Ts'o 	 MOPT_NO_EXT2 | MOPT_DATAJ},
180026092bf5STheodore Ts'o 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
180126092bf5STheodore Ts'o 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
180226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
180326092bf5STheodore Ts'o 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
180426092bf5STheodore Ts'o 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
180526092bf5STheodore Ts'o #else
180626092bf5STheodore Ts'o 	{Opt_acl, 0, MOPT_NOSUPPORT},
180726092bf5STheodore Ts'o 	{Opt_noacl, 0, MOPT_NOSUPPORT},
180826092bf5STheodore Ts'o #endif
180926092bf5STheodore Ts'o 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
181026092bf5STheodore Ts'o 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1811670e9875STheodore Ts'o 	{Opt_debug_want_extra_isize, 0, MOPT_GTE0},
181226092bf5STheodore Ts'o 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
181326092bf5STheodore Ts'o 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
181426092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
181526092bf5STheodore Ts'o 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
181626092bf5STheodore Ts'o 							MOPT_SET | MOPT_Q},
181749da9392SJan Kara 	{Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
181849da9392SJan Kara 							MOPT_SET | MOPT_Q},
181926092bf5STheodore Ts'o 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
182049da9392SJan Kara 		       EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
182149da9392SJan Kara 							MOPT_CLEAR | MOPT_Q},
182226092bf5STheodore Ts'o 	{Opt_usrjquota, 0, MOPT_Q},
182326092bf5STheodore Ts'o 	{Opt_grpjquota, 0, MOPT_Q},
182426092bf5STheodore Ts'o 	{Opt_offusrjquota, 0, MOPT_Q},
182526092bf5STheodore Ts'o 	{Opt_offgrpjquota, 0, MOPT_Q},
182626092bf5STheodore Ts'o 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
182726092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
182826092bf5STheodore Ts'o 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1829df981d03STheodore Ts'o 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
18306ddb2447STheodore Ts'o 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1831cdb7ee4cSTahsin Erdogan 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
183226092bf5STheodore Ts'o 	{Opt_err, 0, 0}
183326092bf5STheodore Ts'o };
183426092bf5STheodore Ts'o 
1835c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1836c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings {
1837c83ad55eSGabriel Krisman Bertazi 	__u16 magic;
1838c83ad55eSGabriel Krisman Bertazi 	char *name;
1839c83ad55eSGabriel Krisman Bertazi 	char *version;
1840c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = {
1841c83ad55eSGabriel Krisman Bertazi 	{EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1842c83ad55eSGabriel Krisman Bertazi };
1843c83ad55eSGabriel Krisman Bertazi 
1844c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1845c83ad55eSGabriel Krisman Bertazi 				 const struct ext4_sb_encodings **encoding,
1846c83ad55eSGabriel Krisman Bertazi 				 __u16 *flags)
1847c83ad55eSGabriel Krisman Bertazi {
1848c83ad55eSGabriel Krisman Bertazi 	__u16 magic = le16_to_cpu(es->s_encoding);
1849c83ad55eSGabriel Krisman Bertazi 	int i;
1850c83ad55eSGabriel Krisman Bertazi 
1851c83ad55eSGabriel Krisman Bertazi 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1852c83ad55eSGabriel Krisman Bertazi 		if (magic == ext4_sb_encoding_map[i].magic)
1853c83ad55eSGabriel Krisman Bertazi 			break;
1854c83ad55eSGabriel Krisman Bertazi 
1855c83ad55eSGabriel Krisman Bertazi 	if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1856c83ad55eSGabriel Krisman Bertazi 		return -EINVAL;
1857c83ad55eSGabriel Krisman Bertazi 
1858c83ad55eSGabriel Krisman Bertazi 	*encoding = &ext4_sb_encoding_map[i];
1859c83ad55eSGabriel Krisman Bertazi 	*flags = le16_to_cpu(es->s_encoding_flags);
1860c83ad55eSGabriel Krisman Bertazi 
1861c83ad55eSGabriel Krisman Bertazi 	return 0;
1862c83ad55eSGabriel Krisman Bertazi }
1863c83ad55eSGabriel Krisman Bertazi #endif
1864c83ad55eSGabriel Krisman Bertazi 
186526092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token,
186626092bf5STheodore Ts'o 			    substring_t *args, unsigned long *journal_devnum,
186726092bf5STheodore Ts'o 			    unsigned int *journal_ioprio, int is_remount)
186826092bf5STheodore Ts'o {
186926092bf5STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
187026092bf5STheodore Ts'o 	const struct mount_opts *m;
187108cefc7aSEric W. Biederman 	kuid_t uid;
187208cefc7aSEric W. Biederman 	kgid_t gid;
187326092bf5STheodore Ts'o 	int arg = 0;
187426092bf5STheodore Ts'o 
187557f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA
187657f73c2cSTheodore Ts'o 	if (token == Opt_usrjquota)
187757f73c2cSTheodore Ts'o 		return set_qf_name(sb, USRQUOTA, &args[0]);
187857f73c2cSTheodore Ts'o 	else if (token == Opt_grpjquota)
187957f73c2cSTheodore Ts'o 		return set_qf_name(sb, GRPQUOTA, &args[0]);
188057f73c2cSTheodore Ts'o 	else if (token == Opt_offusrjquota)
188157f73c2cSTheodore Ts'o 		return clear_qf_name(sb, USRQUOTA);
188257f73c2cSTheodore Ts'o 	else if (token == Opt_offgrpjquota)
188357f73c2cSTheodore Ts'o 		return clear_qf_name(sb, GRPQUOTA);
188457f73c2cSTheodore Ts'o #endif
188526092bf5STheodore Ts'o 	switch (token) {
1886f7048605STheodore Ts'o 	case Opt_noacl:
1887f7048605STheodore Ts'o 	case Opt_nouser_xattr:
1888f7048605STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1889f7048605STheodore Ts'o 		break;
189026092bf5STheodore Ts'o 	case Opt_sb:
189126092bf5STheodore Ts'o 		return 1;	/* handled by get_sb_block() */
189226092bf5STheodore Ts'o 	case Opt_removed:
18935f3633e3SJan Kara 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
189426092bf5STheodore Ts'o 		return 1;
189526092bf5STheodore Ts'o 	case Opt_abort:
189626092bf5STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
189726092bf5STheodore Ts'o 		return 1;
189826092bf5STheodore Ts'o 	case Opt_i_version:
1899357fdad0SMatthew Garrett 		sb->s_flags |= SB_I_VERSION;
190026092bf5STheodore Ts'o 		return 1;
1901a26f4992STheodore Ts'o 	case Opt_lazytime:
19021751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
1903a26f4992STheodore Ts'o 		return 1;
1904a26f4992STheodore Ts'o 	case Opt_nolazytime:
19051751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_LAZYTIME;
1906a26f4992STheodore Ts'o 		return 1;
190726092bf5STheodore Ts'o 	}
190826092bf5STheodore Ts'o 
19095f3633e3SJan Kara 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
19105f3633e3SJan Kara 		if (token == m->token)
19115f3633e3SJan Kara 			break;
19125f3633e3SJan Kara 
19135f3633e3SJan Kara 	if (m->token == Opt_err) {
19145f3633e3SJan Kara 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
19155f3633e3SJan Kara 			 "or missing value", opt);
19165f3633e3SJan Kara 		return -1;
19175f3633e3SJan Kara 	}
19185f3633e3SJan Kara 
19198dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
19208dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19218dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext2", opt);
19228dc0aa8cSTheodore Ts'o 		return -1;
19238dc0aa8cSTheodore Ts'o 	}
19248dc0aa8cSTheodore Ts'o 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
19258dc0aa8cSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
19268dc0aa8cSTheodore Ts'o 			 "Mount option \"%s\" incompatible with ext3", opt);
19278dc0aa8cSTheodore Ts'o 		return -1;
19288dc0aa8cSTheodore Ts'o 	}
19298dc0aa8cSTheodore Ts'o 
1930ad4eec61SEric Sandeen 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
19310efb3b23SJan Kara 		return -1;
193226092bf5STheodore Ts'o 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
193326092bf5STheodore Ts'o 		return -1;
1934c93cf2d7SDmitry Monakhov 	if (m->flags & MOPT_EXPLICIT) {
1935c93cf2d7SDmitry Monakhov 		if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
193626092bf5STheodore Ts'o 			set_opt2(sb, EXPLICIT_DELALLOC);
19371e381f60SDmitry Monakhov 		} else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
19381e381f60SDmitry Monakhov 			set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
1939c93cf2d7SDmitry Monakhov 		} else
1940c93cf2d7SDmitry Monakhov 			return -1;
1941c93cf2d7SDmitry Monakhov 	}
194226092bf5STheodore Ts'o 	if (m->flags & MOPT_CLEAR_ERR)
194326092bf5STheodore Ts'o 		clear_opt(sb, ERRORS_MASK);
194426092bf5STheodore Ts'o 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
194526092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
194626092bf5STheodore Ts'o 			 "options when quota turned on");
194726092bf5STheodore Ts'o 		return -1;
194826092bf5STheodore Ts'o 	}
194926092bf5STheodore Ts'o 
195026092bf5STheodore Ts'o 	if (m->flags & MOPT_NOSUPPORT) {
195126092bf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
195226092bf5STheodore Ts'o 	} else if (token == Opt_commit) {
195326092bf5STheodore Ts'o 		if (arg == 0)
195426092bf5STheodore Ts'o 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
19559ba55543Szhangyi (F) 		else if (arg > INT_MAX / HZ) {
19569ba55543Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
19579ba55543Szhangyi (F) 				 "Invalid commit interval %d, "
19589ba55543Szhangyi (F) 				 "must be smaller than %d",
19599ba55543Szhangyi (F) 				 arg, INT_MAX / HZ);
19609ba55543Szhangyi (F) 			return -1;
19619ba55543Szhangyi (F) 		}
196226092bf5STheodore Ts'o 		sbi->s_commit_interval = HZ * arg;
1963670e9875STheodore Ts'o 	} else if (token == Opt_debug_want_extra_isize) {
19649803387cSTheodore Ts'o 		if ((arg & 1) ||
19659803387cSTheodore Ts'o 		    (arg < 4) ||
19669803387cSTheodore Ts'o 		    (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
19679803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
19689803387cSTheodore Ts'o 				 "Invalid want_extra_isize %d", arg);
19699803387cSTheodore Ts'o 			return -1;
19709803387cSTheodore Ts'o 		}
1971670e9875STheodore Ts'o 		sbi->s_want_extra_isize = arg;
197226092bf5STheodore Ts'o 	} else if (token == Opt_max_batch_time) {
197326092bf5STheodore Ts'o 		sbi->s_max_batch_time = arg;
197426092bf5STheodore Ts'o 	} else if (token == Opt_min_batch_time) {
197526092bf5STheodore Ts'o 		sbi->s_min_batch_time = arg;
197626092bf5STheodore Ts'o 	} else if (token == Opt_inode_readahead_blks) {
1977e33e60eaSJan Kara 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1978e33e60eaSJan Kara 			ext4_msg(sb, KERN_ERR,
1979e33e60eaSJan Kara 				 "EXT4-fs: inode_readahead_blks must be "
1980e33e60eaSJan Kara 				 "0 or a power of 2 smaller than 2^31");
198126092bf5STheodore Ts'o 			return -1;
198226092bf5STheodore Ts'o 		}
198326092bf5STheodore Ts'o 		sbi->s_inode_readahead_blks = arg;
198426092bf5STheodore Ts'o 	} else if (token == Opt_init_itable) {
198526092bf5STheodore Ts'o 		set_opt(sb, INIT_INODE_TABLE);
198626092bf5STheodore Ts'o 		if (!args->from)
198726092bf5STheodore Ts'o 			arg = EXT4_DEF_LI_WAIT_MULT;
198826092bf5STheodore Ts'o 		sbi->s_li_wait_mult = arg;
1989df981d03STheodore Ts'o 	} else if (token == Opt_max_dir_size_kb) {
1990df981d03STheodore Ts'o 		sbi->s_max_dir_size_kb = arg;
199126092bf5STheodore Ts'o 	} else if (token == Opt_stripe) {
199226092bf5STheodore Ts'o 		sbi->s_stripe = arg;
19930efb3b23SJan Kara 	} else if (token == Opt_resuid) {
19940efb3b23SJan Kara 		uid = make_kuid(current_user_ns(), arg);
19950efb3b23SJan Kara 		if (!uid_valid(uid)) {
19965f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
19970efb3b23SJan Kara 			return -1;
19980efb3b23SJan Kara 		}
19990efb3b23SJan Kara 		sbi->s_resuid = uid;
20000efb3b23SJan Kara 	} else if (token == Opt_resgid) {
20010efb3b23SJan Kara 		gid = make_kgid(current_user_ns(), arg);
20020efb3b23SJan Kara 		if (!gid_valid(gid)) {
20035f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
20040efb3b23SJan Kara 			return -1;
20050efb3b23SJan Kara 		}
20060efb3b23SJan Kara 		sbi->s_resgid = gid;
20070efb3b23SJan Kara 	} else if (token == Opt_journal_dev) {
20080efb3b23SJan Kara 		if (is_remount) {
20090efb3b23SJan Kara 			ext4_msg(sb, KERN_ERR,
20100efb3b23SJan Kara 				 "Cannot specify journal on remount");
20110efb3b23SJan Kara 			return -1;
20120efb3b23SJan Kara 		}
20130efb3b23SJan Kara 		*journal_devnum = arg;
2014ad4eec61SEric Sandeen 	} else if (token == Opt_journal_path) {
2015ad4eec61SEric Sandeen 		char *journal_path;
2016ad4eec61SEric Sandeen 		struct inode *journal_inode;
2017ad4eec61SEric Sandeen 		struct path path;
2018ad4eec61SEric Sandeen 		int error;
2019ad4eec61SEric Sandeen 
2020ad4eec61SEric Sandeen 		if (is_remount) {
2021ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR,
2022ad4eec61SEric Sandeen 				 "Cannot specify journal on remount");
2023ad4eec61SEric Sandeen 			return -1;
2024ad4eec61SEric Sandeen 		}
2025ad4eec61SEric Sandeen 		journal_path = match_strdup(&args[0]);
2026ad4eec61SEric Sandeen 		if (!journal_path) {
2027ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not dup "
2028ad4eec61SEric Sandeen 				"journal device string");
2029ad4eec61SEric Sandeen 			return -1;
2030ad4eec61SEric Sandeen 		}
2031ad4eec61SEric Sandeen 
2032ad4eec61SEric Sandeen 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2033ad4eec61SEric Sandeen 		if (error) {
2034ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: could not find "
2035ad4eec61SEric Sandeen 				"journal device path: error %d", error);
2036ad4eec61SEric Sandeen 			kfree(journal_path);
2037ad4eec61SEric Sandeen 			return -1;
2038ad4eec61SEric Sandeen 		}
2039ad4eec61SEric Sandeen 
20402b0143b5SDavid Howells 		journal_inode = d_inode(path.dentry);
2041ad4eec61SEric Sandeen 		if (!S_ISBLK(journal_inode->i_mode)) {
2042ad4eec61SEric Sandeen 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
2043ad4eec61SEric Sandeen 				"is not a block device", journal_path);
2044ad4eec61SEric Sandeen 			path_put(&path);
2045ad4eec61SEric Sandeen 			kfree(journal_path);
2046ad4eec61SEric Sandeen 			return -1;
2047ad4eec61SEric Sandeen 		}
2048ad4eec61SEric Sandeen 
2049ad4eec61SEric Sandeen 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
2050ad4eec61SEric Sandeen 		path_put(&path);
2051ad4eec61SEric Sandeen 		kfree(journal_path);
20520efb3b23SJan Kara 	} else if (token == Opt_journal_ioprio) {
20530efb3b23SJan Kara 		if (arg > 7) {
20545f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
20550efb3b23SJan Kara 				 " (must be 0-7)");
20560efb3b23SJan Kara 			return -1;
20570efb3b23SJan Kara 		}
20580efb3b23SJan Kara 		*journal_ioprio =
20590efb3b23SJan Kara 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
20606ddb2447STheodore Ts'o 	} else if (token == Opt_test_dummy_encryption) {
2061643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
20626ddb2447STheodore Ts'o 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
20636ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20646ddb2447STheodore Ts'o 			 "Test dummy encryption mode enabled");
20656ddb2447STheodore Ts'o #else
20666ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_WARNING,
20676ddb2447STheodore Ts'o 			 "Test dummy encryption mount option ignored");
20686ddb2447STheodore Ts'o #endif
206926092bf5STheodore Ts'o 	} else if (m->flags & MOPT_DATAJ) {
207026092bf5STheodore Ts'o 		if (is_remount) {
207126092bf5STheodore Ts'o 			if (!sbi->s_journal)
207226092bf5STheodore Ts'o 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
20735f3633e3SJan Kara 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
207426092bf5STheodore Ts'o 				ext4_msg(sb, KERN_ERR,
207526092bf5STheodore Ts'o 					 "Cannot change data mode on remount");
207626092bf5STheodore Ts'o 				return -1;
207726092bf5STheodore Ts'o 			}
207826092bf5STheodore Ts'o 		} else {
207926092bf5STheodore Ts'o 			clear_opt(sb, DATA_FLAGS);
208026092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
208126092bf5STheodore Ts'o 		}
208226092bf5STheodore Ts'o #ifdef CONFIG_QUOTA
208326092bf5STheodore Ts'o 	} else if (m->flags & MOPT_QFMT) {
208426092bf5STheodore Ts'o 		if (sb_any_quota_loaded(sb) &&
208526092bf5STheodore Ts'o 		    sbi->s_jquota_fmt != m->mount_opt) {
20865f3633e3SJan Kara 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
20875f3633e3SJan Kara 				 "quota options when quota turned on");
208826092bf5STheodore Ts'o 			return -1;
208926092bf5STheodore Ts'o 		}
2090e2b911c5SDarrick J. Wong 		if (ext4_has_feature_quota(sb)) {
2091c325a67cSTheodore Ts'o 			ext4_msg(sb, KERN_INFO,
2092c325a67cSTheodore Ts'o 				 "Quota format mount options ignored "
2093262b4662SJan Kara 				 "when QUOTA feature is enabled");
2094c325a67cSTheodore Ts'o 			return 1;
2095262b4662SJan Kara 		}
209626092bf5STheodore Ts'o 		sbi->s_jquota_fmt = m->mount_opt;
209726092bf5STheodore Ts'o #endif
2098*9cb20f94SIra Weiny 	} else if (token == Opt_dax || token == Opt_dax_always ||
2099*9cb20f94SIra Weiny 		   token == Opt_dax_inode || token == Opt_dax_never) {
2100ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX
2101*9cb20f94SIra Weiny 		switch (token) {
2102*9cb20f94SIra Weiny 		case Opt_dax:
2103*9cb20f94SIra Weiny 		case Opt_dax_always:
2104ef83b6e8SDan Williams 			ext4_msg(sb, KERN_WARNING,
2105ef83b6e8SDan Williams 				"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2106*9cb20f94SIra Weiny 			sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
2107*9cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2108*9cb20f94SIra Weiny 			break;
2109*9cb20f94SIra Weiny 		case Opt_dax_never:
2110*9cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2111*9cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2112*9cb20f94SIra Weiny 			break;
2113*9cb20f94SIra Weiny 		case Opt_dax_inode:
2114*9cb20f94SIra Weiny 			sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2115*9cb20f94SIra Weiny 			sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2116*9cb20f94SIra Weiny 			/* Strictly for printing options */
2117*9cb20f94SIra Weiny 			sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
2118*9cb20f94SIra Weiny 			break;
2119*9cb20f94SIra Weiny 		}
2120ef83b6e8SDan Williams #else
2121923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_INFO, "dax option not supported");
2122*9cb20f94SIra Weiny 		sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2123*9cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2124923ae0ffSRoss Zwisler 		return -1;
2125923ae0ffSRoss Zwisler #endif
21267915a861SAles Novak 	} else if (token == Opt_data_err_abort) {
21277915a861SAles Novak 		sbi->s_mount_opt |= m->mount_opt;
21287915a861SAles Novak 	} else if (token == Opt_data_err_ignore) {
21297915a861SAles Novak 		sbi->s_mount_opt &= ~m->mount_opt;
213026092bf5STheodore Ts'o 	} else {
213126092bf5STheodore Ts'o 		if (!args->from)
213226092bf5STheodore Ts'o 			arg = 1;
213326092bf5STheodore Ts'o 		if (m->flags & MOPT_CLEAR)
213426092bf5STheodore Ts'o 			arg = !arg;
213526092bf5STheodore Ts'o 		else if (unlikely(!(m->flags & MOPT_SET))) {
213626092bf5STheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
213726092bf5STheodore Ts'o 				 "buggy handling of option %s", opt);
213826092bf5STheodore Ts'o 			WARN_ON(1);
213926092bf5STheodore Ts'o 			return -1;
214026092bf5STheodore Ts'o 		}
214126092bf5STheodore Ts'o 		if (arg != 0)
214226092bf5STheodore Ts'o 			sbi->s_mount_opt |= m->mount_opt;
214326092bf5STheodore Ts'o 		else
214426092bf5STheodore Ts'o 			sbi->s_mount_opt &= ~m->mount_opt;
214526092bf5STheodore Ts'o 	}
214626092bf5STheodore Ts'o 	return 1;
214726092bf5STheodore Ts'o }
214826092bf5STheodore Ts'o 
2149ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb,
2150c3191067STheodore Ts'o 			 unsigned long *journal_devnum,
2151b3881f74STheodore Ts'o 			 unsigned int *journal_ioprio,
2152661aa520SEric Sandeen 			 int is_remount)
2153ac27a0ecSDave Kleikamp {
21541e1a76edSOlof Johansson 	struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
215533458eabSTheodore Ts'o 	char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
2156ac27a0ecSDave Kleikamp 	substring_t args[MAX_OPT_ARGS];
215726092bf5STheodore Ts'o 	int token;
2158ac27a0ecSDave Kleikamp 
2159ac27a0ecSDave Kleikamp 	if (!options)
2160ac27a0ecSDave Kleikamp 		return 1;
2161ac27a0ecSDave Kleikamp 
2162ac27a0ecSDave Kleikamp 	while ((p = strsep(&options, ",")) != NULL) {
2163ac27a0ecSDave Kleikamp 		if (!*p)
2164ac27a0ecSDave Kleikamp 			continue;
216515121c18SEric Sandeen 		/*
216615121c18SEric Sandeen 		 * Initialize args struct so we know whether arg was
216715121c18SEric Sandeen 		 * found; some options take optional arguments.
216815121c18SEric Sandeen 		 */
2169caecd0afSSachin Kamat 		args[0].to = args[0].from = NULL;
2170ac27a0ecSDave Kleikamp 		token = match_token(p, tokens, args);
217126092bf5STheodore Ts'o 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
217226092bf5STheodore Ts'o 				     journal_ioprio, is_remount) < 0)
2173ac27a0ecSDave Kleikamp 			return 0;
2174ac27a0ecSDave Kleikamp 	}
2175ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
217649da9392SJan Kara 	/*
217749da9392SJan Kara 	 * We do the test below only for project quotas. 'usrquota' and
217849da9392SJan Kara 	 * 'grpquota' mount options are allowed even without quota feature
217949da9392SJan Kara 	 * to support legacy quotas in quota files.
218049da9392SJan Kara 	 */
218149da9392SJan Kara 	if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
218249da9392SJan Kara 		ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
218349da9392SJan Kara 			 "Cannot enable project quota enforcement.");
218449da9392SJan Kara 		return 0;
218549da9392SJan Kara 	}
218633458eabSTheodore Ts'o 	usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
218733458eabSTheodore Ts'o 	grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
218833458eabSTheodore Ts'o 	if (usr_qf_name || grp_qf_name) {
218933458eabSTheodore Ts'o 		if (test_opt(sb, USRQUOTA) && usr_qf_name)
2190fd8c37ecSTheodore Ts'o 			clear_opt(sb, USRQUOTA);
2191ac27a0ecSDave Kleikamp 
219233458eabSTheodore Ts'o 		if (test_opt(sb, GRPQUOTA) && grp_qf_name)
2193fd8c37ecSTheodore Ts'o 			clear_opt(sb, GRPQUOTA);
2194ac27a0ecSDave Kleikamp 
219556c50f11SDmitry Monakhov 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
2196b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "old and new quota "
2197b31e1552SEric Sandeen 					"format mixing");
2198ac27a0ecSDave Kleikamp 			return 0;
2199ac27a0ecSDave Kleikamp 		}
2200ac27a0ecSDave Kleikamp 
2201ac27a0ecSDave Kleikamp 		if (!sbi->s_jquota_fmt) {
2202b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "journaled quota format "
2203b31e1552SEric Sandeen 					"not specified");
2204ac27a0ecSDave Kleikamp 			return 0;
2205ac27a0ecSDave Kleikamp 		}
2206ac27a0ecSDave Kleikamp 	}
2207ac27a0ecSDave Kleikamp #endif
2208626b035bSRitesh Harjani 	if (test_opt(sb, DIOREAD_NOLOCK)) {
2209626b035bSRitesh Harjani 		int blocksize =
2210626b035bSRitesh Harjani 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2211626b035bSRitesh Harjani 		if (blocksize < PAGE_SIZE)
2212626b035bSRitesh Harjani 			ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2213626b035bSRitesh Harjani 				 "experimental mount option 'dioread_nolock' "
2214626b035bSRitesh Harjani 				 "for blocksize < PAGE_SIZE");
2215626b035bSRitesh Harjani 	}
2216ac27a0ecSDave Kleikamp 	return 1;
2217ac27a0ecSDave Kleikamp }
2218ac27a0ecSDave Kleikamp 
22192adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq,
22202adf6da8STheodore Ts'o 					   struct super_block *sb)
22212adf6da8STheodore Ts'o {
22222adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA)
22232adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
222433458eabSTheodore Ts'o 	char *usr_qf_name, *grp_qf_name;
22252adf6da8STheodore Ts'o 
22262adf6da8STheodore Ts'o 	if (sbi->s_jquota_fmt) {
22272adf6da8STheodore Ts'o 		char *fmtname = "";
22282adf6da8STheodore Ts'o 
22292adf6da8STheodore Ts'o 		switch (sbi->s_jquota_fmt) {
22302adf6da8STheodore Ts'o 		case QFMT_VFS_OLD:
22312adf6da8STheodore Ts'o 			fmtname = "vfsold";
22322adf6da8STheodore Ts'o 			break;
22332adf6da8STheodore Ts'o 		case QFMT_VFS_V0:
22342adf6da8STheodore Ts'o 			fmtname = "vfsv0";
22352adf6da8STheodore Ts'o 			break;
22362adf6da8STheodore Ts'o 		case QFMT_VFS_V1:
22372adf6da8STheodore Ts'o 			fmtname = "vfsv1";
22382adf6da8STheodore Ts'o 			break;
22392adf6da8STheodore Ts'o 		}
22402adf6da8STheodore Ts'o 		seq_printf(seq, ",jqfmt=%s", fmtname);
22412adf6da8STheodore Ts'o 	}
22422adf6da8STheodore Ts'o 
224333458eabSTheodore Ts'o 	rcu_read_lock();
224433458eabSTheodore Ts'o 	usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
224533458eabSTheodore Ts'o 	grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
224633458eabSTheodore Ts'o 	if (usr_qf_name)
224733458eabSTheodore Ts'o 		seq_show_option(seq, "usrjquota", usr_qf_name);
224833458eabSTheodore Ts'o 	if (grp_qf_name)
224933458eabSTheodore Ts'o 		seq_show_option(seq, "grpjquota", grp_qf_name);
225033458eabSTheodore Ts'o 	rcu_read_unlock();
22512adf6da8STheodore Ts'o #endif
22522adf6da8STheodore Ts'o }
22532adf6da8STheodore Ts'o 
22545a916be1STheodore Ts'o static const char *token2str(int token)
22555a916be1STheodore Ts'o {
225650df9fd5SHerton Ronaldo Krzesinski 	const struct match_token *t;
22575a916be1STheodore Ts'o 
22585a916be1STheodore Ts'o 	for (t = tokens; t->token != Opt_err; t++)
22595a916be1STheodore Ts'o 		if (t->token == token && !strchr(t->pattern, '='))
22605a916be1STheodore Ts'o 			break;
22615a916be1STheodore Ts'o 	return t->pattern;
22625a916be1STheodore Ts'o }
22635a916be1STheodore Ts'o 
22642adf6da8STheodore Ts'o /*
22652adf6da8STheodore Ts'o  * Show an option if
22662adf6da8STheodore Ts'o  *  - it's set to a non-default value OR
22672adf6da8STheodore Ts'o  *  - if the per-sb default is different from the global default
22682adf6da8STheodore Ts'o  */
226966acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
227066acdcf4STheodore Ts'o 			      int nodefs)
22712adf6da8STheodore Ts'o {
22722adf6da8STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
22732adf6da8STheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
227468afa7e0STyson Nottingham 	int def_errors, def_mount_opt = sbi->s_def_mount_opt;
22755a916be1STheodore Ts'o 	const struct mount_opts *m;
227666acdcf4STheodore Ts'o 	char sep = nodefs ? '\n' : ',';
22772adf6da8STheodore Ts'o 
227866acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
227966acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
22802adf6da8STheodore Ts'o 
22812adf6da8STheodore Ts'o 	if (sbi->s_sb_block != 1)
22825a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
22835a916be1STheodore Ts'o 
22845a916be1STheodore Ts'o 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
22855a916be1STheodore Ts'o 		int want_set = m->flags & MOPT_SET;
22865a916be1STheodore Ts'o 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
2287*9cb20f94SIra Weiny 		    (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
22885a916be1STheodore Ts'o 			continue;
228968afa7e0STyson Nottingham 		if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
22905a916be1STheodore Ts'o 			continue; /* skip if same as the default */
22915a916be1STheodore Ts'o 		if ((want_set &&
22925a916be1STheodore Ts'o 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
22935a916be1STheodore Ts'o 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
22945a916be1STheodore Ts'o 			continue; /* select Opt_noFoo vs Opt_Foo */
22955a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("%s", token2str(m->token));
22965a916be1STheodore Ts'o 	}
22975a916be1STheodore Ts'o 
229808cefc7aSEric W. Biederman 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
22995a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
230008cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resuid=%u",
230108cefc7aSEric W. Biederman 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
230208cefc7aSEric W. Biederman 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
23035a916be1STheodore Ts'o 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
230408cefc7aSEric W. Biederman 		SEQ_OPTS_PRINT("resgid=%u",
230508cefc7aSEric W. Biederman 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
230666acdcf4STheodore Ts'o 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
23075a916be1STheodore Ts'o 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
23085a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=remount-ro");
23092adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
23105a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=continue");
23112adf6da8STheodore Ts'o 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
23125a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("errors=panic");
231366acdcf4STheodore Ts'o 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
23145a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
231566acdcf4STheodore Ts'o 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
23165a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
231766acdcf4STheodore Ts'o 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
23185a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2319357fdad0SMatthew Garrett 	if (sb->s_flags & SB_I_VERSION)
23205a916be1STheodore Ts'o 		SEQ_OPTS_PUTS("i_version");
232166acdcf4STheodore Ts'o 	if (nodefs || sbi->s_stripe)
23225a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
232368afa7e0STyson Nottingham 	if (nodefs || EXT4_MOUNT_DATA_FLAGS &
232468afa7e0STyson Nottingham 			(sbi->s_mount_opt ^ def_mount_opt)) {
23252adf6da8STheodore Ts'o 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
23265a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=journal");
23272adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
23285a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=ordered");
23292adf6da8STheodore Ts'o 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
23305a916be1STheodore Ts'o 			SEQ_OPTS_PUTS("data=writeback");
23315a916be1STheodore Ts'o 	}
233266acdcf4STheodore Ts'o 	if (nodefs ||
233366acdcf4STheodore Ts'o 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
23345a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
23352adf6da8STheodore Ts'o 			       sbi->s_inode_readahead_blks);
23362adf6da8STheodore Ts'o 
2337ceec0376STyson Nottingham 	if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
233866acdcf4STheodore Ts'o 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
23395a916be1STheodore Ts'o 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
2340df981d03STheodore Ts'o 	if (nodefs || sbi->s_max_dir_size_kb)
2341df981d03STheodore Ts'o 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
23427915a861SAles Novak 	if (test_opt(sb, DATA_ERR_ABORT))
23437915a861SAles Novak 		SEQ_OPTS_PUTS("data_err=abort");
2344338affb5SEric Biggers 	if (DUMMY_ENCRYPTION_ENABLED(sbi))
2345338affb5SEric Biggers 		SEQ_OPTS_PUTS("test_dummy_encryption");
23462adf6da8STheodore Ts'o 
2347*9cb20f94SIra Weiny 	if (test_opt(sb, DAX_ALWAYS)) {
2348*9cb20f94SIra Weiny 		if (IS_EXT2_SB(sb))
2349*9cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax");
2350*9cb20f94SIra Weiny 		else
2351*9cb20f94SIra Weiny 			SEQ_OPTS_PUTS("dax=always");
2352*9cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_NEVER)) {
2353*9cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=never");
2354*9cb20f94SIra Weiny 	} else if (test_opt2(sb, DAX_INODE)) {
2355*9cb20f94SIra Weiny 		SEQ_OPTS_PUTS("dax=inode");
2356*9cb20f94SIra Weiny 	}
2357*9cb20f94SIra Weiny 
23582adf6da8STheodore Ts'o 	ext4_show_quota_options(seq, sb);
23592adf6da8STheodore Ts'o 	return 0;
23602adf6da8STheodore Ts'o }
23612adf6da8STheodore Ts'o 
236266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root)
236366acdcf4STheodore Ts'o {
236466acdcf4STheodore Ts'o 	return _ext4_show_options(seq, root->d_sb, 0);
236566acdcf4STheodore Ts'o }
236666acdcf4STheodore Ts'o 
2367ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset)
236866acdcf4STheodore Ts'o {
236966acdcf4STheodore Ts'o 	struct super_block *sb = seq->private;
237066acdcf4STheodore Ts'o 	int rc;
237166acdcf4STheodore Ts'o 
2372bc98a42cSDavid Howells 	seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
237366acdcf4STheodore Ts'o 	rc = _ext4_show_options(seq, sb, 1);
237466acdcf4STheodore Ts'o 	seq_puts(seq, "\n");
237566acdcf4STheodore Ts'o 	return rc;
237666acdcf4STheodore Ts'o }
237766acdcf4STheodore Ts'o 
2378617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
2379ac27a0ecSDave Kleikamp 			    int read_only)
2380ac27a0ecSDave Kleikamp {
2381617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2382c89128a0SJaegeuk Kim 	int err = 0;
2383ac27a0ecSDave Kleikamp 
2384617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
2385b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "revision level too high, "
2386b31e1552SEric Sandeen 			 "forcing read-only mode");
2387c89128a0SJaegeuk Kim 		err = -EROFS;
2388ac27a0ecSDave Kleikamp 	}
2389ac27a0ecSDave Kleikamp 	if (read_only)
2390281b5995STheodore Ts'o 		goto done;
2391617ba13bSMingming Cao 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
2392b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2393b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2394c8b459f4SLukas Czerner 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
2395b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2396b31e1552SEric Sandeen 			 "warning: mounting fs with errors, "
2397b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2398ed3ce80aSTao Ma 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
2399ac27a0ecSDave Kleikamp 		 le16_to_cpu(es->s_mnt_count) >=
2400ac27a0ecSDave Kleikamp 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
2401b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2402b31e1552SEric Sandeen 			 "warning: maximal mount count reached, "
2403b31e1552SEric Sandeen 			 "running e2fsck is recommended");
2404ac27a0ecSDave Kleikamp 	else if (le32_to_cpu(es->s_checkinterval) &&
24056a0678a7SArnd Bergmann 		 (ext4_get_tstamp(es, s_lastcheck) +
24066a0678a7SArnd Bergmann 		  le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
2407b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
2408b31e1552SEric Sandeen 			 "warning: checktime reached, "
2409b31e1552SEric Sandeen 			 "running e2fsck is recommended");
24100390131bSFrank Mayhar 	if (!sbi->s_journal)
2411216c34b2SMarcin Slusarz 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
2412ac27a0ecSDave Kleikamp 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
2413617ba13bSMingming Cao 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
2414e8546d06SMarcin Slusarz 	le16_add_cpu(&es->s_mnt_count, 1);
24156a0678a7SArnd Bergmann 	ext4_update_tstamp(es, s_mtime);
24160390131bSFrank Mayhar 	if (sbi->s_journal)
2417e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
2418ac27a0ecSDave Kleikamp 
2419c89128a0SJaegeuk Kim 	err = ext4_commit_super(sb, 1);
2420281b5995STheodore Ts'o done:
2421ac27a0ecSDave Kleikamp 	if (test_opt(sb, DEBUG))
2422a9df9a49STheodore Ts'o 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
2423a2595b8aSTheodore Ts'o 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
2424ac27a0ecSDave Kleikamp 			sb->s_blocksize,
2425ac27a0ecSDave Kleikamp 			sbi->s_groups_count,
2426617ba13bSMingming Cao 			EXT4_BLOCKS_PER_GROUP(sb),
2427617ba13bSMingming Cao 			EXT4_INODES_PER_GROUP(sb),
2428a2595b8aSTheodore Ts'o 			sbi->s_mount_opt, sbi->s_mount_opt2);
2429ac27a0ecSDave Kleikamp 
24307abc52c2SDan Magenheimer 	cleancache_init_fs(sb);
2431c89128a0SJaegeuk Kim 	return err;
2432ac27a0ecSDave Kleikamp }
2433ac27a0ecSDave Kleikamp 
2434117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2435117fff10STheodore Ts'o {
2436117fff10STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
24377c990728SSuraj Jitindar Singh 	struct flex_groups **old_groups, **new_groups;
243837b0b6b8SDan Carpenter 	int size, i, j;
2439117fff10STheodore Ts'o 
2440117fff10STheodore Ts'o 	if (!sbi->s_log_groups_per_flex)
2441117fff10STheodore Ts'o 		return 0;
2442117fff10STheodore Ts'o 
2443117fff10STheodore Ts'o 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
2444117fff10STheodore Ts'o 	if (size <= sbi->s_flex_groups_allocated)
2445117fff10STheodore Ts'o 		return 0;
2446117fff10STheodore Ts'o 
24477c990728SSuraj Jitindar Singh 	new_groups = kvzalloc(roundup_pow_of_two(size *
24487c990728SSuraj Jitindar Singh 			      sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
2449117fff10STheodore Ts'o 	if (!new_groups) {
24507c990728SSuraj Jitindar Singh 		ext4_msg(sb, KERN_ERR,
24517c990728SSuraj Jitindar Singh 			 "not enough memory for %d flex group pointers", size);
2452117fff10STheodore Ts'o 		return -ENOMEM;
2453117fff10STheodore Ts'o 	}
24547c990728SSuraj Jitindar Singh 	for (i = sbi->s_flex_groups_allocated; i < size; i++) {
24557c990728SSuraj Jitindar Singh 		new_groups[i] = kvzalloc(roundup_pow_of_two(
24567c990728SSuraj Jitindar Singh 					 sizeof(struct flex_groups)),
24577c990728SSuraj Jitindar Singh 					 GFP_KERNEL);
24587c990728SSuraj Jitindar Singh 		if (!new_groups[i]) {
245937b0b6b8SDan Carpenter 			for (j = sbi->s_flex_groups_allocated; j < i; j++)
246037b0b6b8SDan Carpenter 				kvfree(new_groups[j]);
24617c990728SSuraj Jitindar Singh 			kvfree(new_groups);
24627c990728SSuraj Jitindar Singh 			ext4_msg(sb, KERN_ERR,
24637c990728SSuraj Jitindar Singh 				 "not enough memory for %d flex groups", size);
24647c990728SSuraj Jitindar Singh 			return -ENOMEM;
2465117fff10STheodore Ts'o 		}
24667c990728SSuraj Jitindar Singh 	}
24677c990728SSuraj Jitindar Singh 	rcu_read_lock();
24687c990728SSuraj Jitindar Singh 	old_groups = rcu_dereference(sbi->s_flex_groups);
24697c990728SSuraj Jitindar Singh 	if (old_groups)
24707c990728SSuraj Jitindar Singh 		memcpy(new_groups, old_groups,
24717c990728SSuraj Jitindar Singh 		       (sbi->s_flex_groups_allocated *
24727c990728SSuraj Jitindar Singh 			sizeof(struct flex_groups *)));
24737c990728SSuraj Jitindar Singh 	rcu_read_unlock();
24747c990728SSuraj Jitindar Singh 	rcu_assign_pointer(sbi->s_flex_groups, new_groups);
24757c990728SSuraj Jitindar Singh 	sbi->s_flex_groups_allocated = size;
24767c990728SSuraj Jitindar Singh 	if (old_groups)
24777c990728SSuraj Jitindar Singh 		ext4_kvfree_array_rcu(old_groups);
2478117fff10STheodore Ts'o 	return 0;
2479117fff10STheodore Ts'o }
2480117fff10STheodore Ts'o 
2481772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb)
2482772cb7c8SJose R. Santos {
2483772cb7c8SJose R. Santos 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2484772cb7c8SJose R. Santos 	struct ext4_group_desc *gdp = NULL;
24857c990728SSuraj Jitindar Singh 	struct flex_groups *fg;
2486772cb7c8SJose R. Santos 	ext4_group_t flex_group;
2487117fff10STheodore Ts'o 	int i, err;
2488772cb7c8SJose R. Santos 
2489503358aeSTheodore Ts'o 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
2490d50f2ab6SXi Wang 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
2491772cb7c8SJose R. Santos 		sbi->s_log_groups_per_flex = 0;
2492772cb7c8SJose R. Santos 		return 1;
2493772cb7c8SJose R. Santos 	}
2494772cb7c8SJose R. Santos 
2495117fff10STheodore Ts'o 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2496117fff10STheodore Ts'o 	if (err)
2497772cb7c8SJose R. Santos 		goto failed;
2498772cb7c8SJose R. Santos 
2499772cb7c8SJose R. Santos 	for (i = 0; i < sbi->s_groups_count; i++) {
250088b6edd1STheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
2501772cb7c8SJose R. Santos 
2502772cb7c8SJose R. Santos 		flex_group = ext4_flex_group(sbi, i);
25037c990728SSuraj Jitindar Singh 		fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
25047c990728SSuraj Jitindar Singh 		atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
250590ba983fSTheodore Ts'o 		atomic64_add(ext4_free_group_clusters(sb, gdp),
25067c990728SSuraj Jitindar Singh 			     &fg->free_clusters);
25077c990728SSuraj Jitindar Singh 		atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
2508772cb7c8SJose R. Santos 	}
2509772cb7c8SJose R. Santos 
2510772cb7c8SJose R. Santos 	return 1;
2511772cb7c8SJose R. Santos failed:
2512772cb7c8SJose R. Santos 	return 0;
2513772cb7c8SJose R. Santos }
2514772cb7c8SJose R. Santos 
2515e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
2516717d50e4SAndreas Dilger 				   struct ext4_group_desc *gdp)
2517717d50e4SAndreas Dilger {
2518b47820edSDaeho Jeong 	int offset = offsetof(struct ext4_group_desc, bg_checksum);
2519717d50e4SAndreas Dilger 	__u16 crc = 0;
2520717d50e4SAndreas Dilger 	__le32 le_group = cpu_to_le32(block_group);
2521e2b911c5SDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2522717d50e4SAndreas Dilger 
25239aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2524feb0ab32SDarrick J. Wong 		/* Use new metadata_csum algorithm */
2525feb0ab32SDarrick J. Wong 		__u32 csum32;
2526b47820edSDaeho Jeong 		__u16 dummy_csum = 0;
2527feb0ab32SDarrick J. Wong 
2528feb0ab32SDarrick J. Wong 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2529feb0ab32SDarrick J. Wong 				     sizeof(le_group));
2530b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2531b47820edSDaeho Jeong 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2532b47820edSDaeho Jeong 				     sizeof(dummy_csum));
2533b47820edSDaeho Jeong 		offset += sizeof(dummy_csum);
2534b47820edSDaeho Jeong 		if (offset < sbi->s_desc_size)
2535b47820edSDaeho Jeong 			csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2536b47820edSDaeho Jeong 					     sbi->s_desc_size - offset);
2537feb0ab32SDarrick J. Wong 
2538feb0ab32SDarrick J. Wong 		crc = csum32 & 0xFFFF;
2539feb0ab32SDarrick J. Wong 		goto out;
2540feb0ab32SDarrick J. Wong 	}
2541feb0ab32SDarrick J. Wong 
2542feb0ab32SDarrick J. Wong 	/* old crc16 code */
2543e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_gdt_csum(sb))
2544813d32f9SDarrick J. Wong 		return 0;
2545813d32f9SDarrick J. Wong 
2546717d50e4SAndreas Dilger 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2547717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2548717d50e4SAndreas Dilger 	crc = crc16(crc, (__u8 *)gdp, offset);
2549717d50e4SAndreas Dilger 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2550717d50e4SAndreas Dilger 	/* for checksum of struct ext4_group_desc do the rest...*/
2551e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
2552717d50e4SAndreas Dilger 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2553717d50e4SAndreas Dilger 		crc = crc16(crc, (__u8 *)gdp + offset,
2554717d50e4SAndreas Dilger 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2555717d50e4SAndreas Dilger 				offset);
2556717d50e4SAndreas Dilger 
2557feb0ab32SDarrick J. Wong out:
2558717d50e4SAndreas Dilger 	return cpu_to_le16(crc);
2559717d50e4SAndreas Dilger }
2560717d50e4SAndreas Dilger 
2561feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2562717d50e4SAndreas Dilger 				struct ext4_group_desc *gdp)
2563717d50e4SAndreas Dilger {
2564feb0ab32SDarrick J. Wong 	if (ext4_has_group_desc_csum(sb) &&
2565e2b911c5SDarrick J. Wong 	    (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
2566717d50e4SAndreas Dilger 		return 0;
2567717d50e4SAndreas Dilger 
2568717d50e4SAndreas Dilger 	return 1;
2569717d50e4SAndreas Dilger }
2570717d50e4SAndreas Dilger 
2571feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2572feb0ab32SDarrick J. Wong 			      struct ext4_group_desc *gdp)
2573feb0ab32SDarrick J. Wong {
2574feb0ab32SDarrick J. Wong 	if (!ext4_has_group_desc_csum(sb))
2575feb0ab32SDarrick J. Wong 		return;
2576e2b911c5SDarrick J. Wong 	gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
2577feb0ab32SDarrick J. Wong }
2578feb0ab32SDarrick J. Wong 
2579ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */
2580bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb,
2581829fa70dSTheodore Ts'o 				  ext4_fsblk_t sb_block,
2582bfff6873SLukas Czerner 				  ext4_group_t *first_not_zeroed)
2583ac27a0ecSDave Kleikamp {
2584617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2585617ba13bSMingming Cao 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2586617ba13bSMingming Cao 	ext4_fsblk_t last_block;
258744de022cSTheodore Ts'o 	ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
2588bd81d8eeSLaurent Vivier 	ext4_fsblk_t block_bitmap;
2589bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_bitmap;
2590bd81d8eeSLaurent Vivier 	ext4_fsblk_t inode_table;
2591ce421581SJose R. Santos 	int flexbg_flag = 0;
2592bfff6873SLukas Czerner 	ext4_group_t i, grp = sbi->s_groups_count;
2593ac27a0ecSDave Kleikamp 
2594e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
2595ce421581SJose R. Santos 		flexbg_flag = 1;
2596ce421581SJose R. Santos 
2597617ba13bSMingming Cao 	ext4_debug("Checking group descriptors");
2598ac27a0ecSDave Kleikamp 
2599197cd65aSAkinobu Mita 	for (i = 0; i < sbi->s_groups_count; i++) {
2600197cd65aSAkinobu Mita 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2601197cd65aSAkinobu Mita 
2602ce421581SJose R. Santos 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2603bd81d8eeSLaurent Vivier 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2604ac27a0ecSDave Kleikamp 		else
2605ac27a0ecSDave Kleikamp 			last_block = first_block +
2606617ba13bSMingming Cao 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2607ac27a0ecSDave Kleikamp 
2608bfff6873SLukas Czerner 		if ((grp == sbi->s_groups_count) &&
2609bfff6873SLukas Czerner 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2610bfff6873SLukas Czerner 			grp = i;
2611bfff6873SLukas Czerner 
26128fadc143SAlexandre Ratchov 		block_bitmap = ext4_block_bitmap(sb, gdp);
2613829fa70dSTheodore Ts'o 		if (block_bitmap == sb_block) {
2614829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2615829fa70dSTheodore Ts'o 				 "Block bitmap for group %u overlaps "
2616829fa70dSTheodore Ts'o 				 "superblock", i);
261718db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
261818db4b4eSTheodore Ts'o 				return 0;
2619829fa70dSTheodore Ts'o 		}
262077260807STheodore Ts'o 		if (block_bitmap >= sb_block + 1 &&
262177260807STheodore Ts'o 		    block_bitmap <= last_bg_block) {
262277260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
262377260807STheodore Ts'o 				 "Block bitmap for group %u overlaps "
262477260807STheodore Ts'o 				 "block group descriptors", i);
262577260807STheodore Ts'o 			if (!sb_rdonly(sb))
262677260807STheodore Ts'o 				return 0;
262777260807STheodore Ts'o 		}
26282b2d6d01STheodore Ts'o 		if (block_bitmap < first_block || block_bitmap > last_block) {
2629b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2630a9df9a49STheodore Ts'o 			       "Block bitmap for group %u not in group "
2631b31e1552SEric Sandeen 			       "(block %llu)!", i, block_bitmap);
2632ac27a0ecSDave Kleikamp 			return 0;
2633ac27a0ecSDave Kleikamp 		}
26348fadc143SAlexandre Ratchov 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2635829fa70dSTheodore Ts'o 		if (inode_bitmap == sb_block) {
2636829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2637829fa70dSTheodore Ts'o 				 "Inode bitmap for group %u overlaps "
2638829fa70dSTheodore Ts'o 				 "superblock", i);
263918db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
264018db4b4eSTheodore Ts'o 				return 0;
2641829fa70dSTheodore Ts'o 		}
264277260807STheodore Ts'o 		if (inode_bitmap >= sb_block + 1 &&
264377260807STheodore Ts'o 		    inode_bitmap <= last_bg_block) {
264477260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
264577260807STheodore Ts'o 				 "Inode bitmap for group %u overlaps "
264677260807STheodore Ts'o 				 "block group descriptors", i);
264777260807STheodore Ts'o 			if (!sb_rdonly(sb))
264877260807STheodore Ts'o 				return 0;
264977260807STheodore Ts'o 		}
26502b2d6d01STheodore Ts'o 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2651b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2652a9df9a49STheodore Ts'o 			       "Inode bitmap for group %u not in group "
2653b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_bitmap);
2654ac27a0ecSDave Kleikamp 			return 0;
2655ac27a0ecSDave Kleikamp 		}
26568fadc143SAlexandre Ratchov 		inode_table = ext4_inode_table(sb, gdp);
2657829fa70dSTheodore Ts'o 		if (inode_table == sb_block) {
2658829fa70dSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2659829fa70dSTheodore Ts'o 				 "Inode table for group %u overlaps "
2660829fa70dSTheodore Ts'o 				 "superblock", i);
266118db4b4eSTheodore Ts'o 			if (!sb_rdonly(sb))
266218db4b4eSTheodore Ts'o 				return 0;
2663829fa70dSTheodore Ts'o 		}
266477260807STheodore Ts'o 		if (inode_table >= sb_block + 1 &&
266577260807STheodore Ts'o 		    inode_table <= last_bg_block) {
266677260807STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
266777260807STheodore Ts'o 				 "Inode table for group %u overlaps "
266877260807STheodore Ts'o 				 "block group descriptors", i);
266977260807STheodore Ts'o 			if (!sb_rdonly(sb))
267077260807STheodore Ts'o 				return 0;
267177260807STheodore Ts'o 		}
2672bd81d8eeSLaurent Vivier 		if (inode_table < first_block ||
26732b2d6d01STheodore Ts'o 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2674b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2675a9df9a49STheodore Ts'o 			       "Inode table for group %u not in group "
2676b31e1552SEric Sandeen 			       "(block %llu)!", i, inode_table);
2677ac27a0ecSDave Kleikamp 			return 0;
2678ac27a0ecSDave Kleikamp 		}
2679955ce5f5SAneesh Kumar K.V 		ext4_lock_group(sb, i);
2680feb0ab32SDarrick J. Wong 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2681b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2682b31e1552SEric Sandeen 				 "Checksum for group %u failed (%u!=%u)",
2683e2b911c5SDarrick J. Wong 				 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
2684fd2d4291SAvantika Mathur 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2685bc98a42cSDavid Howells 			if (!sb_rdonly(sb)) {
2686955ce5f5SAneesh Kumar K.V 				ext4_unlock_group(sb, i);
2687717d50e4SAndreas Dilger 				return 0;
2688717d50e4SAndreas Dilger 			}
26897ee1ec4cSLi Zefan 		}
2690955ce5f5SAneesh Kumar K.V 		ext4_unlock_group(sb, i);
2691ce421581SJose R. Santos 		if (!flexbg_flag)
2692617ba13bSMingming Cao 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2693ac27a0ecSDave Kleikamp 	}
2694bfff6873SLukas Czerner 	if (NULL != first_not_zeroed)
2695bfff6873SLukas Czerner 		*first_not_zeroed = grp;
2696ac27a0ecSDave Kleikamp 	return 1;
2697ac27a0ecSDave Kleikamp }
2698ac27a0ecSDave Kleikamp 
2699617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2700ac27a0ecSDave Kleikamp  * the superblock) which were deleted from all directories, but held open by
2701ac27a0ecSDave Kleikamp  * a process at the time of a crash.  We walk the list and try to delete these
2702ac27a0ecSDave Kleikamp  * inodes at recovery time (only with a read-write filesystem).
2703ac27a0ecSDave Kleikamp  *
2704ac27a0ecSDave Kleikamp  * In order to keep the orphan inode chain consistent during traversal (in
2705ac27a0ecSDave Kleikamp  * case of crash during recovery), we link each inode into the superblock
2706ac27a0ecSDave Kleikamp  * orphan list_head and handle it the same way as an inode deletion during
2707ac27a0ecSDave Kleikamp  * normal operation (which journals the operations for us).
2708ac27a0ecSDave Kleikamp  *
2709ac27a0ecSDave Kleikamp  * We only do an iget() and an iput() on each inode, which is very safe if we
2710ac27a0ecSDave Kleikamp  * accidentally point at an in-use or already deleted inode.  The worst that
2711ac27a0ecSDave Kleikamp  * can happen in this case is that we get a "bit already cleared" message from
2712617ba13bSMingming Cao  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2713ac27a0ecSDave Kleikamp  * e2fsck was run on this filesystem, and it must have already done the orphan
2714ac27a0ecSDave Kleikamp  * inode cleanup for us, so we can safely abort without any further action.
2715ac27a0ecSDave Kleikamp  */
2716617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb,
2717617ba13bSMingming Cao 				struct ext4_super_block *es)
2718ac27a0ecSDave Kleikamp {
2719ac27a0ecSDave Kleikamp 	unsigned int s_flags = sb->s_flags;
27202c98eb5eSTheodore Ts'o 	int ret, nr_orphans = 0, nr_truncates = 0;
2721ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
272295f1fda4Szhangyi (F) 	int quota_update = 0;
2723ac27a0ecSDave Kleikamp 	int i;
2724ac27a0ecSDave Kleikamp #endif
2725ac27a0ecSDave Kleikamp 	if (!es->s_last_orphan) {
2726ac27a0ecSDave Kleikamp 		jbd_debug(4, "no orphan inodes to clean up\n");
2727ac27a0ecSDave Kleikamp 		return;
2728ac27a0ecSDave Kleikamp 	}
2729ac27a0ecSDave Kleikamp 
2730a8f48a95SEric Sandeen 	if (bdev_read_only(sb->s_bdev)) {
2731b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "write access "
2732b31e1552SEric Sandeen 			"unavailable, skipping orphan cleanup");
2733a8f48a95SEric Sandeen 		return;
2734a8f48a95SEric Sandeen 	}
2735a8f48a95SEric Sandeen 
2736d39195c3SAmir Goldstein 	/* Check if feature set would not allow a r/w mount */
2737d39195c3SAmir Goldstein 	if (!ext4_feature_set_ok(sb, 0)) {
2738d39195c3SAmir Goldstein 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2739d39195c3SAmir Goldstein 			 "unknown ROCOMPAT features");
2740d39195c3SAmir Goldstein 		return;
2741d39195c3SAmir Goldstein 	}
2742d39195c3SAmir Goldstein 
2743617ba13bSMingming Cao 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2744c25f9bc6SEric Sandeen 		/* don't clear list on RO mount w/ errors */
27451751e8a6SLinus Torvalds 		if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
274684474976SDmitry Monakhov 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2747ac27a0ecSDave Kleikamp 				  "clearing orphan list.\n");
2748ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2749c25f9bc6SEric Sandeen 		}
2750ac27a0ecSDave Kleikamp 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2751ac27a0ecSDave Kleikamp 		return;
2752ac27a0ecSDave Kleikamp 	}
2753ac27a0ecSDave Kleikamp 
27541751e8a6SLinus Torvalds 	if (s_flags & SB_RDONLY) {
2755b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
27561751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
2757ac27a0ecSDave Kleikamp 	}
2758ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
2759ac27a0ecSDave Kleikamp 	/* Needed for iput() to work correctly and not trash data */
27601751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
276195f1fda4Szhangyi (F) 
276295f1fda4Szhangyi (F) 	/*
276395f1fda4Szhangyi (F) 	 * Turn on quotas which were not enabled for read-only mounts if
276495f1fda4Szhangyi (F) 	 * filesystem has quota feature, so that they are updated correctly.
276595f1fda4Szhangyi (F) 	 */
27661751e8a6SLinus Torvalds 	if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
276795f1fda4Szhangyi (F) 		int ret = ext4_enable_quotas(sb);
276895f1fda4Szhangyi (F) 
276995f1fda4Szhangyi (F) 		if (!ret)
277095f1fda4Szhangyi (F) 			quota_update = 1;
277195f1fda4Szhangyi (F) 		else
277295f1fda4Szhangyi (F) 			ext4_msg(sb, KERN_ERR,
277395f1fda4Szhangyi (F) 				"Cannot turn on quotas: error %d", ret);
277495f1fda4Szhangyi (F) 	}
277595f1fda4Szhangyi (F) 
277695f1fda4Szhangyi (F) 	/* Turn on journaled quotas used for old sytle */
2777a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2778617ba13bSMingming Cao 		if (EXT4_SB(sb)->s_qf_names[i]) {
2779617ba13bSMingming Cao 			int ret = ext4_quota_on_mount(sb, i);
278095f1fda4Szhangyi (F) 
278195f1fda4Szhangyi (F) 			if (!ret)
278295f1fda4Szhangyi (F) 				quota_update = 1;
278395f1fda4Szhangyi (F) 			else
2784b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR,
2785b31e1552SEric Sandeen 					"Cannot turn on journaled "
278695f1fda4Szhangyi (F) 					"quota: type %d: error %d", i, ret);
2787ac27a0ecSDave Kleikamp 		}
2788ac27a0ecSDave Kleikamp 	}
2789ac27a0ecSDave Kleikamp #endif
2790ac27a0ecSDave Kleikamp 
2791ac27a0ecSDave Kleikamp 	while (es->s_last_orphan) {
2792ac27a0ecSDave Kleikamp 		struct inode *inode;
2793ac27a0ecSDave Kleikamp 
2794c65d5c6cSVegard Nossum 		/*
2795c65d5c6cSVegard Nossum 		 * We may have encountered an error during cleanup; if
2796c65d5c6cSVegard Nossum 		 * so, skip the rest.
2797c65d5c6cSVegard Nossum 		 */
2798c65d5c6cSVegard Nossum 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2799c65d5c6cSVegard Nossum 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2800c65d5c6cSVegard Nossum 			es->s_last_orphan = 0;
2801c65d5c6cSVegard Nossum 			break;
2802c65d5c6cSVegard Nossum 		}
2803c65d5c6cSVegard Nossum 
280497bd42b9SJosef Bacik 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
280597bd42b9SJosef Bacik 		if (IS_ERR(inode)) {
2806ac27a0ecSDave Kleikamp 			es->s_last_orphan = 0;
2807ac27a0ecSDave Kleikamp 			break;
2808ac27a0ecSDave Kleikamp 		}
2809ac27a0ecSDave Kleikamp 
2810617ba13bSMingming Cao 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2811871a2931SChristoph Hellwig 		dquot_initialize(inode);
2812ac27a0ecSDave Kleikamp 		if (inode->i_nlink) {
2813566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2814b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2815b31e1552SEric Sandeen 					"%s: truncating inode %lu to %lld bytes",
281646e665e9SHarvey Harrison 					__func__, inode->i_ino, inode->i_size);
2817e5f8eab8STheodore Ts'o 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2818ac27a0ecSDave Kleikamp 				  inode->i_ino, inode->i_size);
28195955102cSAl Viro 			inode_lock(inode);
282055f252c9SLukas Czerner 			truncate_inode_pages(inode->i_mapping, inode->i_size);
28212c98eb5eSTheodore Ts'o 			ret = ext4_truncate(inode);
28222c98eb5eSTheodore Ts'o 			if (ret)
28232c98eb5eSTheodore Ts'o 				ext4_std_error(inode->i_sb, ret);
28245955102cSAl Viro 			inode_unlock(inode);
2825ac27a0ecSDave Kleikamp 			nr_truncates++;
2826ac27a0ecSDave Kleikamp 		} else {
2827566370a2SPaul Taysom 			if (test_opt(sb, DEBUG))
2828b31e1552SEric Sandeen 				ext4_msg(sb, KERN_DEBUG,
2829b31e1552SEric Sandeen 					"%s: deleting unreferenced inode %lu",
283046e665e9SHarvey Harrison 					__func__, inode->i_ino);
2831ac27a0ecSDave Kleikamp 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2832ac27a0ecSDave Kleikamp 				  inode->i_ino);
2833ac27a0ecSDave Kleikamp 			nr_orphans++;
2834ac27a0ecSDave Kleikamp 		}
2835ac27a0ecSDave Kleikamp 		iput(inode);  /* The delete magic happens here! */
2836ac27a0ecSDave Kleikamp 	}
2837ac27a0ecSDave Kleikamp 
2838ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2839ac27a0ecSDave Kleikamp 
2840ac27a0ecSDave Kleikamp 	if (nr_orphans)
2841b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2842b31e1552SEric Sandeen 		       PLURAL(nr_orphans));
2843ac27a0ecSDave Kleikamp 	if (nr_truncates)
2844b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2845b31e1552SEric Sandeen 		       PLURAL(nr_truncates));
2846ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
284795f1fda4Szhangyi (F) 	/* Turn off quotas if they were enabled for orphan cleanup */
284895f1fda4Szhangyi (F) 	if (quota_update) {
2849a2d4a646SJan Kara 		for (i = 0; i < EXT4_MAXQUOTAS; i++) {
285095f1fda4Szhangyi (F) 			if (sb_dqopt(sb)->files[i])
2851287a8095SChristoph Hellwig 				dquot_quota_off(sb, i);
2852ac27a0ecSDave Kleikamp 		}
285395f1fda4Szhangyi (F) 	}
2854ac27a0ecSDave Kleikamp #endif
28551751e8a6SLinus Torvalds 	sb->s_flags = s_flags; /* Restore SB_RDONLY status */
2856ac27a0ecSDave Kleikamp }
28570b8e58a1SAndreas Dilger 
2858cd2291a4SEric Sandeen /*
2859cd2291a4SEric Sandeen  * Maximal extent format file size.
2860cd2291a4SEric Sandeen  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2861cd2291a4SEric Sandeen  * extent format containers, within a sector_t, and within i_blocks
2862cd2291a4SEric Sandeen  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2863cd2291a4SEric Sandeen  * so that won't be a limiting factor.
2864cd2291a4SEric Sandeen  *
2865f17722f9SLukas Czerner  * However there is other limiting factor. We do store extents in the form
2866f17722f9SLukas Czerner  * of starting block and length, hence the resulting length of the extent
2867f17722f9SLukas Czerner  * covering maximum file size must fit into on-disk format containers as
2868f17722f9SLukas Czerner  * well. Given that length is always by 1 unit bigger than max unit (because
2869f17722f9SLukas Czerner  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2870f17722f9SLukas Czerner  *
2871cd2291a4SEric Sandeen  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2872cd2291a4SEric Sandeen  */
2873f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files)
2874cd2291a4SEric Sandeen {
2875cd2291a4SEric Sandeen 	loff_t res;
2876cd2291a4SEric Sandeen 	loff_t upper_limit = MAX_LFS_FILESIZE;
2877cd2291a4SEric Sandeen 
287872deb455SChristoph Hellwig 	BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
287972deb455SChristoph Hellwig 
288072deb455SChristoph Hellwig 	if (!has_huge_files) {
2881cd2291a4SEric Sandeen 		upper_limit = (1LL << 32) - 1;
2882cd2291a4SEric Sandeen 
2883cd2291a4SEric Sandeen 		/* total blocks in file system block size */
2884cd2291a4SEric Sandeen 		upper_limit >>= (blkbits - 9);
2885cd2291a4SEric Sandeen 		upper_limit <<= blkbits;
2886cd2291a4SEric Sandeen 	}
2887cd2291a4SEric Sandeen 
2888f17722f9SLukas Czerner 	/*
2889f17722f9SLukas Czerner 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2890f17722f9SLukas Czerner 	 * by one fs block, so ee_len can cover the extent of maximum file
2891f17722f9SLukas Czerner 	 * size
2892f17722f9SLukas Czerner 	 */
2893f17722f9SLukas Czerner 	res = (1LL << 32) - 1;
2894cd2291a4SEric Sandeen 	res <<= blkbits;
2895cd2291a4SEric Sandeen 
2896cd2291a4SEric Sandeen 	/* Sanity check against vm- & vfs- imposed limits */
2897cd2291a4SEric Sandeen 	if (res > upper_limit)
2898cd2291a4SEric Sandeen 		res = upper_limit;
2899cd2291a4SEric Sandeen 
2900cd2291a4SEric Sandeen 	return res;
2901cd2291a4SEric Sandeen }
2902ac27a0ecSDave Kleikamp 
2903ac27a0ecSDave Kleikamp /*
2904cd2291a4SEric Sandeen  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
29050fc1b451SAneesh Kumar K.V  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
29060fc1b451SAneesh Kumar K.V  * We need to be 1 filesystem block less than the 2^48 sector limit.
2907ac27a0ecSDave Kleikamp  */
2908f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2909ac27a0ecSDave Kleikamp {
2910617ba13bSMingming Cao 	loff_t res = EXT4_NDIR_BLOCKS;
29110fc1b451SAneesh Kumar K.V 	int meta_blocks;
29120fc1b451SAneesh Kumar K.V 	loff_t upper_limit;
29130b8e58a1SAndreas Dilger 	/* This is calculated to be the largest file size for a dense, block
29140b8e58a1SAndreas Dilger 	 * mapped file such that the file's total number of 512-byte sectors,
29150b8e58a1SAndreas Dilger 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
29160b8e58a1SAndreas Dilger 	 *
29170b8e58a1SAndreas Dilger 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
29180b8e58a1SAndreas Dilger 	 * number of 512-byte sectors of the file.
29190fc1b451SAneesh Kumar K.V 	 */
29200fc1b451SAneesh Kumar K.V 
292172deb455SChristoph Hellwig 	if (!has_huge_files) {
29220fc1b451SAneesh Kumar K.V 		/*
292372deb455SChristoph Hellwig 		 * !has_huge_files or implies that the inode i_block field
292472deb455SChristoph Hellwig 		 * represents total file blocks in 2^32 512-byte sectors ==
292572deb455SChristoph Hellwig 		 * size of vfs inode i_blocks * 8
29260fc1b451SAneesh Kumar K.V 		 */
29270fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 32) - 1;
29280fc1b451SAneesh Kumar K.V 
29290fc1b451SAneesh Kumar K.V 		/* total blocks in file system block size */
29300fc1b451SAneesh Kumar K.V 		upper_limit >>= (bits - 9);
29310fc1b451SAneesh Kumar K.V 
29320fc1b451SAneesh Kumar K.V 	} else {
29338180a562SAneesh Kumar K.V 		/*
29348180a562SAneesh Kumar K.V 		 * We use 48 bit ext4_inode i_blocks
29358180a562SAneesh Kumar K.V 		 * With EXT4_HUGE_FILE_FL set the i_blocks
29368180a562SAneesh Kumar K.V 		 * represent total number of blocks in
29378180a562SAneesh Kumar K.V 		 * file system block size
29388180a562SAneesh Kumar K.V 		 */
29390fc1b451SAneesh Kumar K.V 		upper_limit = (1LL << 48) - 1;
29400fc1b451SAneesh Kumar K.V 
29410fc1b451SAneesh Kumar K.V 	}
29420fc1b451SAneesh Kumar K.V 
29430fc1b451SAneesh Kumar K.V 	/* indirect blocks */
29440fc1b451SAneesh Kumar K.V 	meta_blocks = 1;
29450fc1b451SAneesh Kumar K.V 	/* double indirect blocks */
29460fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2));
29470fc1b451SAneesh Kumar K.V 	/* tripple indirect blocks */
29480fc1b451SAneesh Kumar K.V 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
29490fc1b451SAneesh Kumar K.V 
29500fc1b451SAneesh Kumar K.V 	upper_limit -= meta_blocks;
29510fc1b451SAneesh Kumar K.V 	upper_limit <<= bits;
2952ac27a0ecSDave Kleikamp 
2953ac27a0ecSDave Kleikamp 	res += 1LL << (bits-2);
2954ac27a0ecSDave Kleikamp 	res += 1LL << (2*(bits-2));
2955ac27a0ecSDave Kleikamp 	res += 1LL << (3*(bits-2));
2956ac27a0ecSDave Kleikamp 	res <<= bits;
2957ac27a0ecSDave Kleikamp 	if (res > upper_limit)
2958ac27a0ecSDave Kleikamp 		res = upper_limit;
29590fc1b451SAneesh Kumar K.V 
29600fc1b451SAneesh Kumar K.V 	if (res > MAX_LFS_FILESIZE)
29610fc1b451SAneesh Kumar K.V 		res = MAX_LFS_FILESIZE;
29620fc1b451SAneesh Kumar K.V 
2963ac27a0ecSDave Kleikamp 	return res;
2964ac27a0ecSDave Kleikamp }
2965ac27a0ecSDave Kleikamp 
2966617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb,
296770bbb3e0SAndrew Morton 				   ext4_fsblk_t logical_sb_block, int nr)
2968ac27a0ecSDave Kleikamp {
2969617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2970fd2d4291SAvantika Mathur 	ext4_group_t bg, first_meta_bg;
2971ac27a0ecSDave Kleikamp 	int has_super = 0;
2972ac27a0ecSDave Kleikamp 
2973ac27a0ecSDave Kleikamp 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2974ac27a0ecSDave Kleikamp 
2975e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
297670bbb3e0SAndrew Morton 		return logical_sb_block + nr + 1;
2977ac27a0ecSDave Kleikamp 	bg = sbi->s_desc_per_block * nr;
2978617ba13bSMingming Cao 	if (ext4_bg_has_super(sb, bg))
2979ac27a0ecSDave Kleikamp 		has_super = 1;
29800b8e58a1SAndreas Dilger 
2981bd63f6b0SDarrick J. Wong 	/*
2982bd63f6b0SDarrick J. Wong 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2983bd63f6b0SDarrick J. Wong 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2984bd63f6b0SDarrick J. Wong 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2985bd63f6b0SDarrick J. Wong 	 * compensate.
2986bd63f6b0SDarrick J. Wong 	 */
2987bd63f6b0SDarrick J. Wong 	if (sb->s_blocksize == 1024 && nr == 0 &&
298849598e04SJun Piao 	    le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
2989bd63f6b0SDarrick J. Wong 		has_super++;
2990bd63f6b0SDarrick J. Wong 
2991617ba13bSMingming Cao 	return (has_super + ext4_group_first_block_no(sb, bg));
2992ac27a0ecSDave Kleikamp }
2993ac27a0ecSDave Kleikamp 
2994c9de560dSAlex Tomas /**
2995c9de560dSAlex Tomas  * ext4_get_stripe_size: Get the stripe size.
2996c9de560dSAlex Tomas  * @sbi: In memory super block info
2997c9de560dSAlex Tomas  *
2998c9de560dSAlex Tomas  * If we have specified it via mount option, then
2999c9de560dSAlex Tomas  * use the mount option value. If the value specified at mount time is
3000c9de560dSAlex Tomas  * greater than the blocks per group use the super block value.
3001c9de560dSAlex Tomas  * If the super block value is greater than blocks per group return 0.
3002c9de560dSAlex Tomas  * Allocator needs it be less than blocks per group.
3003c9de560dSAlex Tomas  *
3004c9de560dSAlex Tomas  */
3005c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3006c9de560dSAlex Tomas {
3007c9de560dSAlex Tomas 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3008c9de560dSAlex Tomas 	unsigned long stripe_width =
3009c9de560dSAlex Tomas 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
30103eb08658SDan Ehrenberg 	int ret;
3011c9de560dSAlex Tomas 
3012c9de560dSAlex Tomas 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
30133eb08658SDan Ehrenberg 		ret = sbi->s_stripe;
30145469d7c3SJan Kara 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
30153eb08658SDan Ehrenberg 		ret = stripe_width;
30165469d7c3SJan Kara 	else if (stride && stride <= sbi->s_blocks_per_group)
30173eb08658SDan Ehrenberg 		ret = stride;
30183eb08658SDan Ehrenberg 	else
30193eb08658SDan Ehrenberg 		ret = 0;
3020c9de560dSAlex Tomas 
30213eb08658SDan Ehrenberg 	/*
30223eb08658SDan Ehrenberg 	 * If the stripe width is 1, this makes no sense and
30233eb08658SDan Ehrenberg 	 * we set it to 0 to turn off stripe handling code.
30243eb08658SDan Ehrenberg 	 */
30253eb08658SDan Ehrenberg 	if (ret <= 1)
30263eb08658SDan Ehrenberg 		ret = 0;
3027c9de560dSAlex Tomas 
30283eb08658SDan Ehrenberg 	return ret;
3029c9de560dSAlex Tomas }
3030ac27a0ecSDave Kleikamp 
3031a13fb1a4SEric Sandeen /*
3032a13fb1a4SEric Sandeen  * Check whether this filesystem can be mounted based on
3033a13fb1a4SEric Sandeen  * the features present and the RDONLY/RDWR mount requested.
3034a13fb1a4SEric Sandeen  * Returns 1 if this filesystem can be mounted as requested,
3035a13fb1a4SEric Sandeen  * 0 if it cannot be.
3036a13fb1a4SEric Sandeen  */
3037a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3038a13fb1a4SEric Sandeen {
3039e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_incompat_features(sb)) {
3040a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR,
3041a13fb1a4SEric Sandeen 			"Couldn't mount because of "
3042a13fb1a4SEric Sandeen 			"unsupported optional features (%x)",
3043a13fb1a4SEric Sandeen 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3044a13fb1a4SEric Sandeen 			~EXT4_FEATURE_INCOMPAT_SUPP));
3045a13fb1a4SEric Sandeen 		return 0;
3046a13fb1a4SEric Sandeen 	}
3047a13fb1a4SEric Sandeen 
3048c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE
3049c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb)) {
3050c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_ERR,
3051c83ad55eSGabriel Krisman Bertazi 			 "Filesystem with casefold feature cannot be "
3052c83ad55eSGabriel Krisman Bertazi 			 "mounted without CONFIG_UNICODE");
3053c83ad55eSGabriel Krisman Bertazi 		return 0;
3054c83ad55eSGabriel Krisman Bertazi 	}
3055c83ad55eSGabriel Krisman Bertazi #endif
3056c83ad55eSGabriel Krisman Bertazi 
3057a13fb1a4SEric Sandeen 	if (readonly)
3058a13fb1a4SEric Sandeen 		return 1;
3059a13fb1a4SEric Sandeen 
3060e2b911c5SDarrick J. Wong 	if (ext4_has_feature_readonly(sb)) {
30612cb5cc8bSDarrick J. Wong 		ext4_msg(sb, KERN_INFO, "filesystem is read-only");
30621751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
30632cb5cc8bSDarrick J. Wong 		return 1;
30642cb5cc8bSDarrick J. Wong 	}
30652cb5cc8bSDarrick J. Wong 
3066a13fb1a4SEric Sandeen 	/* Check that feature set is OK for a read-write mount */
3067e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
3068a13fb1a4SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3069a13fb1a4SEric Sandeen 			 "unsupported optional features (%x)",
3070a13fb1a4SEric Sandeen 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3071a13fb1a4SEric Sandeen 				~EXT4_FEATURE_RO_COMPAT_SUPP));
3072a13fb1a4SEric Sandeen 		return 0;
3073a13fb1a4SEric Sandeen 	}
3074e2b911c5SDarrick J. Wong 	if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
3075bab08ab9STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
3076bab08ab9STheodore Ts'o 			 "Can't support bigalloc feature without "
3077bab08ab9STheodore Ts'o 			 "extents feature\n");
3078bab08ab9STheodore Ts'o 		return 0;
3079bab08ab9STheodore Ts'o 	}
30807c319d32SAditya Kali 
30819db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
3082d65d87a0STheodore Ts'o 	if (!readonly && (ext4_has_feature_quota(sb) ||
3083d65d87a0STheodore Ts'o 			  ext4_has_feature_project(sb))) {
30847c319d32SAditya Kali 		ext4_msg(sb, KERN_ERR,
3085d65d87a0STheodore Ts'o 			 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
3086689c958cSLi Xi 		return 0;
3087689c958cSLi Xi 	}
30887c319d32SAditya Kali #endif  /* CONFIG_QUOTA */
3089a13fb1a4SEric Sandeen 	return 1;
3090a13fb1a4SEric Sandeen }
3091a13fb1a4SEric Sandeen 
309266e61a9eSTheodore Ts'o /*
309366e61a9eSTheodore Ts'o  * This function is called once a day if we have errors logged
309466e61a9eSTheodore Ts'o  * on the file system
309566e61a9eSTheodore Ts'o  */
3096235699a8SKees Cook static void print_daily_error_info(struct timer_list *t)
309766e61a9eSTheodore Ts'o {
3098235699a8SKees Cook 	struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3099235699a8SKees Cook 	struct super_block *sb = sbi->s_sb;
3100235699a8SKees Cook 	struct ext4_super_block *es = sbi->s_es;
310166e61a9eSTheodore Ts'o 
310266e61a9eSTheodore Ts'o 	if (es->s_error_count)
3103ae0f78deSTheodore Ts'o 		/* fsck newer than v1.41.13 is needed to clean this condition. */
3104ae0f78deSTheodore Ts'o 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
310566e61a9eSTheodore Ts'o 			 le32_to_cpu(es->s_error_count));
310666e61a9eSTheodore Ts'o 	if (es->s_first_error_time) {
31076a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
31086a0678a7SArnd Bergmann 		       sb->s_id,
31096a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_first_error_time),
311066e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_first_error_func),
311166e61a9eSTheodore Ts'o 		       es->s_first_error_func,
311266e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_first_error_line));
311366e61a9eSTheodore Ts'o 		if (es->s_first_error_ino)
3114651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
311566e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_first_error_ino));
311666e61a9eSTheodore Ts'o 		if (es->s_first_error_block)
3117651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
311866e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_first_error_block));
3119651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
312066e61a9eSTheodore Ts'o 	}
312166e61a9eSTheodore Ts'o 	if (es->s_last_error_time) {
31226a0678a7SArnd Bergmann 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
31236a0678a7SArnd Bergmann 		       sb->s_id,
31246a0678a7SArnd Bergmann 		       ext4_get_tstamp(es, s_last_error_time),
312566e61a9eSTheodore Ts'o 		       (int) sizeof(es->s_last_error_func),
312666e61a9eSTheodore Ts'o 		       es->s_last_error_func,
312766e61a9eSTheodore Ts'o 		       le32_to_cpu(es->s_last_error_line));
312866e61a9eSTheodore Ts'o 		if (es->s_last_error_ino)
3129651e1c3bSJoe Perches 			printk(KERN_CONT ": inode %u",
313066e61a9eSTheodore Ts'o 			       le32_to_cpu(es->s_last_error_ino));
313166e61a9eSTheodore Ts'o 		if (es->s_last_error_block)
3132651e1c3bSJoe Perches 			printk(KERN_CONT ": block %llu", (unsigned long long)
313366e61a9eSTheodore Ts'o 			       le64_to_cpu(es->s_last_error_block));
3134651e1c3bSJoe Perches 		printk(KERN_CONT "\n");
313566e61a9eSTheodore Ts'o 	}
313666e61a9eSTheodore Ts'o 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
313766e61a9eSTheodore Ts'o }
313866e61a9eSTheodore Ts'o 
3139bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */
3140bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr)
3141bfff6873SLukas Czerner {
3142bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3143bfff6873SLukas Czerner 	ext4_group_t group, ngroups;
3144bfff6873SLukas Czerner 	struct super_block *sb;
3145bfff6873SLukas Czerner 	unsigned long timeout = 0;
3146bfff6873SLukas Czerner 	int ret = 0;
3147bfff6873SLukas Czerner 
3148bfff6873SLukas Czerner 	sb = elr->lr_super;
3149bfff6873SLukas Czerner 	ngroups = EXT4_SB(sb)->s_groups_count;
3150bfff6873SLukas Czerner 
3151bfff6873SLukas Czerner 	for (group = elr->lr_next_group; group < ngroups; group++) {
3152bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3153bfff6873SLukas Czerner 		if (!gdp) {
3154bfff6873SLukas Czerner 			ret = 1;
3155bfff6873SLukas Czerner 			break;
3156bfff6873SLukas Czerner 		}
3157bfff6873SLukas Czerner 
3158bfff6873SLukas Czerner 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3159bfff6873SLukas Czerner 			break;
3160bfff6873SLukas Czerner 	}
3161bfff6873SLukas Czerner 
31627f511862STheodore Ts'o 	if (group >= ngroups)
3163bfff6873SLukas Czerner 		ret = 1;
3164bfff6873SLukas Czerner 
3165bfff6873SLukas Czerner 	if (!ret) {
3166bfff6873SLukas Czerner 		timeout = jiffies;
3167bfff6873SLukas Czerner 		ret = ext4_init_inode_table(sb, group,
3168bfff6873SLukas Czerner 					    elr->lr_timeout ? 0 : 1);
3169bfff6873SLukas Czerner 		if (elr->lr_timeout == 0) {
317051ce6511SLukas Czerner 			timeout = (jiffies - timeout) *
317151ce6511SLukas Czerner 				  elr->lr_sbi->s_li_wait_mult;
3172bfff6873SLukas Czerner 			elr->lr_timeout = timeout;
3173bfff6873SLukas Czerner 		}
3174bfff6873SLukas Czerner 		elr->lr_next_sched = jiffies + elr->lr_timeout;
3175bfff6873SLukas Czerner 		elr->lr_next_group = group + 1;
3176bfff6873SLukas Czerner 	}
3177bfff6873SLukas Czerner 	return ret;
3178bfff6873SLukas Czerner }
3179bfff6873SLukas Czerner 
3180bfff6873SLukas Czerner /*
3181bfff6873SLukas Czerner  * Remove lr_request from the list_request and free the
31824ed5c033SLukas Czerner  * request structure. Should be called with li_list_mtx held
3183bfff6873SLukas Czerner  */
3184bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr)
3185bfff6873SLukas Czerner {
3186bfff6873SLukas Czerner 	struct ext4_sb_info *sbi;
3187bfff6873SLukas Czerner 
3188bfff6873SLukas Czerner 	if (!elr)
3189bfff6873SLukas Czerner 		return;
3190bfff6873SLukas Czerner 
3191bfff6873SLukas Czerner 	sbi = elr->lr_sbi;
3192bfff6873SLukas Czerner 
3193bfff6873SLukas Czerner 	list_del(&elr->lr_request);
3194bfff6873SLukas Czerner 	sbi->s_li_request = NULL;
3195bfff6873SLukas Czerner 	kfree(elr);
3196bfff6873SLukas Czerner }
3197bfff6873SLukas Czerner 
3198bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb)
3199bfff6873SLukas Czerner {
32001bb933fbSLukas Czerner 	mutex_lock(&ext4_li_mtx);
32011bb933fbSLukas Czerner 	if (!ext4_li_info) {
32021bb933fbSLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3203bfff6873SLukas Czerner 		return;
32041bb933fbSLukas Czerner 	}
3205bfff6873SLukas Czerner 
3206bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
32071bb933fbSLukas Czerner 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
3208bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
32091bb933fbSLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3210bfff6873SLukas Czerner }
3211bfff6873SLukas Czerner 
32128f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task;
32138f1f7453SEric Sandeen 
3214bfff6873SLukas Czerner /*
3215bfff6873SLukas Czerner  * This is the function where ext4lazyinit thread lives. It walks
3216bfff6873SLukas Czerner  * through the request list searching for next scheduled filesystem.
3217bfff6873SLukas Czerner  * When such a fs is found, run the lazy initialization request
3218bfff6873SLukas Czerner  * (ext4_rn_li_request) and keep track of the time spend in this
3219bfff6873SLukas Czerner  * function. Based on that time we compute next schedule time of
3220bfff6873SLukas Czerner  * the request. When walking through the list is complete, compute
3221bfff6873SLukas Czerner  * next waking time and put itself into sleep.
3222bfff6873SLukas Czerner  */
3223bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg)
3224bfff6873SLukas Czerner {
3225bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3226bfff6873SLukas Czerner 	struct list_head *pos, *n;
3227bfff6873SLukas Czerner 	struct ext4_li_request *elr;
32284ed5c033SLukas Czerner 	unsigned long next_wakeup, cur;
3229bfff6873SLukas Czerner 
3230bfff6873SLukas Czerner 	BUG_ON(NULL == eli);
3231bfff6873SLukas Czerner 
3232bfff6873SLukas Czerner cont_thread:
3233bfff6873SLukas Czerner 	while (true) {
3234bfff6873SLukas Czerner 		next_wakeup = MAX_JIFFY_OFFSET;
3235bfff6873SLukas Czerner 
3236bfff6873SLukas Czerner 		mutex_lock(&eli->li_list_mtx);
3237bfff6873SLukas Czerner 		if (list_empty(&eli->li_request_list)) {
3238bfff6873SLukas Czerner 			mutex_unlock(&eli->li_list_mtx);
3239bfff6873SLukas Czerner 			goto exit_thread;
3240bfff6873SLukas Czerner 		}
3241bfff6873SLukas Czerner 		list_for_each_safe(pos, n, &eli->li_request_list) {
3242e22834f0SDmitry Monakhov 			int err = 0;
3243e22834f0SDmitry Monakhov 			int progress = 0;
3244bfff6873SLukas Czerner 			elr = list_entry(pos, struct ext4_li_request,
3245bfff6873SLukas Czerner 					 lr_request);
3246bfff6873SLukas Czerner 
3247e22834f0SDmitry Monakhov 			if (time_before(jiffies, elr->lr_next_sched)) {
3248e22834f0SDmitry Monakhov 				if (time_before(elr->lr_next_sched, next_wakeup))
3249e22834f0SDmitry Monakhov 					next_wakeup = elr->lr_next_sched;
3250e22834f0SDmitry Monakhov 				continue;
3251e22834f0SDmitry Monakhov 			}
3252e22834f0SDmitry Monakhov 			if (down_read_trylock(&elr->lr_super->s_umount)) {
3253e22834f0SDmitry Monakhov 				if (sb_start_write_trylock(elr->lr_super)) {
3254e22834f0SDmitry Monakhov 					progress = 1;
3255e22834f0SDmitry Monakhov 					/*
3256e22834f0SDmitry Monakhov 					 * We hold sb->s_umount, sb can not
3257e22834f0SDmitry Monakhov 					 * be removed from the list, it is
3258e22834f0SDmitry Monakhov 					 * now safe to drop li_list_mtx
3259e22834f0SDmitry Monakhov 					 */
3260e22834f0SDmitry Monakhov 					mutex_unlock(&eli->li_list_mtx);
3261e22834f0SDmitry Monakhov 					err = ext4_run_li_request(elr);
3262e22834f0SDmitry Monakhov 					sb_end_write(elr->lr_super);
3263e22834f0SDmitry Monakhov 					mutex_lock(&eli->li_list_mtx);
3264e22834f0SDmitry Monakhov 					n = pos->next;
3265e22834f0SDmitry Monakhov 				}
3266e22834f0SDmitry Monakhov 				up_read((&elr->lr_super->s_umount));
3267e22834f0SDmitry Monakhov 			}
3268b2c78cd0STheodore Ts'o 			/* error, remove the lazy_init job */
3269e22834f0SDmitry Monakhov 			if (err) {
3270bfff6873SLukas Czerner 				ext4_remove_li_request(elr);
3271bfff6873SLukas Czerner 				continue;
3272bfff6873SLukas Czerner 			}
3273e22834f0SDmitry Monakhov 			if (!progress) {
3274e22834f0SDmitry Monakhov 				elr->lr_next_sched = jiffies +
3275e22834f0SDmitry Monakhov 					(prandom_u32()
3276e22834f0SDmitry Monakhov 					 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3277b2c78cd0STheodore Ts'o 			}
3278bfff6873SLukas Czerner 			if (time_before(elr->lr_next_sched, next_wakeup))
3279bfff6873SLukas Czerner 				next_wakeup = elr->lr_next_sched;
3280bfff6873SLukas Czerner 		}
3281bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3282bfff6873SLukas Czerner 
3283a0acae0eSTejun Heo 		try_to_freeze();
3284bfff6873SLukas Czerner 
32854ed5c033SLukas Czerner 		cur = jiffies;
32864ed5c033SLukas Czerner 		if ((time_after_eq(cur, next_wakeup)) ||
3287f4245bd4SLukas Czerner 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3288bfff6873SLukas Czerner 			cond_resched();
3289bfff6873SLukas Czerner 			continue;
3290bfff6873SLukas Czerner 		}
3291bfff6873SLukas Czerner 
32924ed5c033SLukas Czerner 		schedule_timeout_interruptible(next_wakeup - cur);
32934ed5c033SLukas Czerner 
32948f1f7453SEric Sandeen 		if (kthread_should_stop()) {
32958f1f7453SEric Sandeen 			ext4_clear_request_list();
32968f1f7453SEric Sandeen 			goto exit_thread;
32978f1f7453SEric Sandeen 		}
3298bfff6873SLukas Czerner 	}
3299bfff6873SLukas Czerner 
3300bfff6873SLukas Czerner exit_thread:
3301bfff6873SLukas Czerner 	/*
3302bfff6873SLukas Czerner 	 * It looks like the request list is empty, but we need
3303bfff6873SLukas Czerner 	 * to check it under the li_list_mtx lock, to prevent any
3304bfff6873SLukas Czerner 	 * additions into it, and of course we should lock ext4_li_mtx
3305bfff6873SLukas Czerner 	 * to atomically free the list and ext4_li_info, because at
3306bfff6873SLukas Czerner 	 * this point another ext4 filesystem could be registering
3307bfff6873SLukas Czerner 	 * new one.
3308bfff6873SLukas Czerner 	 */
3309bfff6873SLukas Czerner 	mutex_lock(&ext4_li_mtx);
3310bfff6873SLukas Czerner 	mutex_lock(&eli->li_list_mtx);
3311bfff6873SLukas Czerner 	if (!list_empty(&eli->li_request_list)) {
3312bfff6873SLukas Czerner 		mutex_unlock(&eli->li_list_mtx);
3313bfff6873SLukas Czerner 		mutex_unlock(&ext4_li_mtx);
3314bfff6873SLukas Czerner 		goto cont_thread;
3315bfff6873SLukas Czerner 	}
3316bfff6873SLukas Czerner 	mutex_unlock(&eli->li_list_mtx);
3317bfff6873SLukas Czerner 	kfree(ext4_li_info);
3318bfff6873SLukas Czerner 	ext4_li_info = NULL;
3319bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3320bfff6873SLukas Czerner 
3321bfff6873SLukas Czerner 	return 0;
3322bfff6873SLukas Czerner }
3323bfff6873SLukas Czerner 
3324bfff6873SLukas Czerner static void ext4_clear_request_list(void)
3325bfff6873SLukas Czerner {
3326bfff6873SLukas Czerner 	struct list_head *pos, *n;
3327bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3328bfff6873SLukas Czerner 
3329bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3330bfff6873SLukas Czerner 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3331bfff6873SLukas Czerner 		elr = list_entry(pos, struct ext4_li_request,
3332bfff6873SLukas Czerner 				 lr_request);
3333bfff6873SLukas Czerner 		ext4_remove_li_request(elr);
3334bfff6873SLukas Czerner 	}
3335bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3336bfff6873SLukas Czerner }
3337bfff6873SLukas Czerner 
3338bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void)
3339bfff6873SLukas Czerner {
33408f1f7453SEric Sandeen 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
33418f1f7453SEric Sandeen 					 ext4_li_info, "ext4lazyinit");
33428f1f7453SEric Sandeen 	if (IS_ERR(ext4_lazyinit_task)) {
33438f1f7453SEric Sandeen 		int err = PTR_ERR(ext4_lazyinit_task);
3344bfff6873SLukas Czerner 		ext4_clear_request_list();
3345bfff6873SLukas Czerner 		kfree(ext4_li_info);
3346bfff6873SLukas Czerner 		ext4_li_info = NULL;
334792b97816STheodore Ts'o 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3348bfff6873SLukas Czerner 				 "initialization thread\n",
3349bfff6873SLukas Czerner 				 err);
3350bfff6873SLukas Czerner 		return err;
3351bfff6873SLukas Czerner 	}
3352bfff6873SLukas Czerner 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3353bfff6873SLukas Czerner 	return 0;
3354bfff6873SLukas Czerner }
3355bfff6873SLukas Czerner 
3356bfff6873SLukas Czerner /*
3357bfff6873SLukas Czerner  * Check whether it make sense to run itable init. thread or not.
3358bfff6873SLukas Czerner  * If there is at least one uninitialized inode table, return
3359bfff6873SLukas Czerner  * corresponding group number, else the loop goes through all
3360bfff6873SLukas Czerner  * groups and return total number of groups.
3361bfff6873SLukas Czerner  */
3362bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3363bfff6873SLukas Czerner {
3364bfff6873SLukas Czerner 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3365bfff6873SLukas Czerner 	struct ext4_group_desc *gdp = NULL;
3366bfff6873SLukas Czerner 
33678844618dSTheodore Ts'o 	if (!ext4_has_group_desc_csum(sb))
33688844618dSTheodore Ts'o 		return ngroups;
33698844618dSTheodore Ts'o 
3370bfff6873SLukas Czerner 	for (group = 0; group < ngroups; group++) {
3371bfff6873SLukas Czerner 		gdp = ext4_get_group_desc(sb, group, NULL);
3372bfff6873SLukas Czerner 		if (!gdp)
3373bfff6873SLukas Czerner 			continue;
3374bfff6873SLukas Czerner 
337550122847STheodore Ts'o 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3376bfff6873SLukas Czerner 			break;
3377bfff6873SLukas Czerner 	}
3378bfff6873SLukas Czerner 
3379bfff6873SLukas Czerner 	return group;
3380bfff6873SLukas Czerner }
3381bfff6873SLukas Czerner 
3382bfff6873SLukas Czerner static int ext4_li_info_new(void)
3383bfff6873SLukas Czerner {
3384bfff6873SLukas Czerner 	struct ext4_lazy_init *eli = NULL;
3385bfff6873SLukas Czerner 
3386bfff6873SLukas Czerner 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3387bfff6873SLukas Czerner 	if (!eli)
3388bfff6873SLukas Czerner 		return -ENOMEM;
3389bfff6873SLukas Czerner 
3390bfff6873SLukas Czerner 	INIT_LIST_HEAD(&eli->li_request_list);
3391bfff6873SLukas Czerner 	mutex_init(&eli->li_list_mtx);
3392bfff6873SLukas Czerner 
3393bfff6873SLukas Czerner 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3394bfff6873SLukas Czerner 
3395bfff6873SLukas Czerner 	ext4_li_info = eli;
3396bfff6873SLukas Czerner 
3397bfff6873SLukas Czerner 	return 0;
3398bfff6873SLukas Czerner }
3399bfff6873SLukas Czerner 
3400bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3401bfff6873SLukas Czerner 					    ext4_group_t start)
3402bfff6873SLukas Czerner {
3403bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3404bfff6873SLukas Czerner 	struct ext4_li_request *elr;
3405bfff6873SLukas Czerner 
3406bfff6873SLukas Czerner 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3407bfff6873SLukas Czerner 	if (!elr)
3408bfff6873SLukas Czerner 		return NULL;
3409bfff6873SLukas Czerner 
3410bfff6873SLukas Czerner 	elr->lr_super = sb;
3411bfff6873SLukas Czerner 	elr->lr_sbi = sbi;
3412bfff6873SLukas Czerner 	elr->lr_next_group = start;
3413bfff6873SLukas Czerner 
3414bfff6873SLukas Czerner 	/*
3415bfff6873SLukas Czerner 	 * Randomize first schedule time of the request to
3416bfff6873SLukas Czerner 	 * spread the inode table initialization requests
3417bfff6873SLukas Czerner 	 * better.
3418bfff6873SLukas Czerner 	 */
3419dd1f723bSTheodore Ts'o 	elr->lr_next_sched = jiffies + (prandom_u32() %
3420dd1f723bSTheodore Ts'o 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3421bfff6873SLukas Czerner 	return elr;
3422bfff6873SLukas Czerner }
3423bfff6873SLukas Czerner 
34247f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb,
3425bfff6873SLukas Czerner 			     ext4_group_t first_not_zeroed)
3426bfff6873SLukas Czerner {
3427bfff6873SLukas Czerner 	struct ext4_sb_info *sbi = EXT4_SB(sb);
34287f511862STheodore Ts'o 	struct ext4_li_request *elr = NULL;
342949598e04SJun Piao 	ext4_group_t ngroups = sbi->s_groups_count;
34306c5a6cb9SAndrew Morton 	int ret = 0;
3431bfff6873SLukas Czerner 
34327f511862STheodore Ts'o 	mutex_lock(&ext4_li_mtx);
343351ce6511SLukas Czerner 	if (sbi->s_li_request != NULL) {
343451ce6511SLukas Czerner 		/*
343551ce6511SLukas Czerner 		 * Reset timeout so it can be computed again, because
343651ce6511SLukas Czerner 		 * s_li_wait_mult might have changed.
343751ce6511SLukas Czerner 		 */
343851ce6511SLukas Czerner 		sbi->s_li_request->lr_timeout = 0;
34397f511862STheodore Ts'o 		goto out;
344051ce6511SLukas Czerner 	}
3441bfff6873SLukas Czerner 
3442bc98a42cSDavid Howells 	if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
344355ff3840STao Ma 	    !test_opt(sb, INIT_INODE_TABLE))
34447f511862STheodore Ts'o 		goto out;
3445bfff6873SLukas Czerner 
3446bfff6873SLukas Czerner 	elr = ext4_li_request_new(sb, first_not_zeroed);
34477f511862STheodore Ts'o 	if (!elr) {
34487f511862STheodore Ts'o 		ret = -ENOMEM;
34497f511862STheodore Ts'o 		goto out;
34507f511862STheodore Ts'o 	}
3451bfff6873SLukas Czerner 
3452bfff6873SLukas Czerner 	if (NULL == ext4_li_info) {
3453bfff6873SLukas Czerner 		ret = ext4_li_info_new();
3454bfff6873SLukas Czerner 		if (ret)
3455bfff6873SLukas Czerner 			goto out;
3456bfff6873SLukas Czerner 	}
3457bfff6873SLukas Czerner 
3458bfff6873SLukas Czerner 	mutex_lock(&ext4_li_info->li_list_mtx);
3459bfff6873SLukas Czerner 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3460bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_info->li_list_mtx);
3461bfff6873SLukas Czerner 
3462bfff6873SLukas Czerner 	sbi->s_li_request = elr;
346346e4690bSTao Ma 	/*
346446e4690bSTao Ma 	 * set elr to NULL here since it has been inserted to
346546e4690bSTao Ma 	 * the request_list and the removal and free of it is
346646e4690bSTao Ma 	 * handled by ext4_clear_request_list from now on.
346746e4690bSTao Ma 	 */
346846e4690bSTao Ma 	elr = NULL;
3469bfff6873SLukas Czerner 
3470bfff6873SLukas Czerner 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3471bfff6873SLukas Czerner 		ret = ext4_run_lazyinit_thread();
3472bfff6873SLukas Czerner 		if (ret)
3473bfff6873SLukas Czerner 			goto out;
3474bfff6873SLukas Czerner 	}
3475bfff6873SLukas Czerner out:
3476bfff6873SLukas Czerner 	mutex_unlock(&ext4_li_mtx);
3477beed5ecbSNicolas Kaiser 	if (ret)
3478bfff6873SLukas Czerner 		kfree(elr);
3479bfff6873SLukas Czerner 	return ret;
3480bfff6873SLukas Czerner }
3481bfff6873SLukas Czerner 
3482bfff6873SLukas Czerner /*
3483bfff6873SLukas Czerner  * We do not need to lock anything since this is called on
3484bfff6873SLukas Czerner  * module unload.
3485bfff6873SLukas Czerner  */
3486bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void)
3487bfff6873SLukas Czerner {
3488bfff6873SLukas Czerner 	/*
3489bfff6873SLukas Czerner 	 * If thread exited earlier
3490bfff6873SLukas Czerner 	 * there's nothing to be done.
3491bfff6873SLukas Czerner 	 */
34928f1f7453SEric Sandeen 	if (!ext4_li_info || !ext4_lazyinit_task)
3493bfff6873SLukas Czerner 		return;
3494bfff6873SLukas Czerner 
34958f1f7453SEric Sandeen 	kthread_stop(ext4_lazyinit_task);
3496bfff6873SLukas Czerner }
3497bfff6873SLukas Czerner 
349825ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb)
349925ed6e8aSDarrick J. Wong {
350025ed6e8aSDarrick J. Wong 	int ret = 1;
350125ed6e8aSDarrick J. Wong 	int compat, incompat;
350225ed6e8aSDarrick J. Wong 	struct ext4_sb_info *sbi = EXT4_SB(sb);
350325ed6e8aSDarrick J. Wong 
35049aa5d32bSDmitry Monakhov 	if (ext4_has_metadata_csum(sb)) {
3505db9ee220SDarrick J. Wong 		/* journal checksum v3 */
350625ed6e8aSDarrick J. Wong 		compat = 0;
3507db9ee220SDarrick J. Wong 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
350825ed6e8aSDarrick J. Wong 	} else {
350925ed6e8aSDarrick J. Wong 		/* journal checksum v1 */
351025ed6e8aSDarrick J. Wong 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
351125ed6e8aSDarrick J. Wong 		incompat = 0;
351225ed6e8aSDarrick J. Wong 	}
351325ed6e8aSDarrick J. Wong 
3514feb8c6d3SDarrick J. Wong 	jbd2_journal_clear_features(sbi->s_journal,
3515feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3516feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3517feb8c6d3SDarrick J. Wong 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
351825ed6e8aSDarrick J. Wong 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
351925ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
352025ed6e8aSDarrick J. Wong 				compat, 0,
352125ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
352225ed6e8aSDarrick J. Wong 				incompat);
352325ed6e8aSDarrick J. Wong 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
352425ed6e8aSDarrick J. Wong 		ret = jbd2_journal_set_features(sbi->s_journal,
352525ed6e8aSDarrick J. Wong 				compat, 0,
352625ed6e8aSDarrick J. Wong 				incompat);
352725ed6e8aSDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
352825ed6e8aSDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
352925ed6e8aSDarrick J. Wong 	} else {
3530feb8c6d3SDarrick J. Wong 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3531feb8c6d3SDarrick J. Wong 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
353225ed6e8aSDarrick J. Wong 	}
353325ed6e8aSDarrick J. Wong 
353425ed6e8aSDarrick J. Wong 	return ret;
353525ed6e8aSDarrick J. Wong }
353625ed6e8aSDarrick J. Wong 
3537952fc18eSTheodore Ts'o /*
3538952fc18eSTheodore Ts'o  * Note: calculating the overhead so we can be compatible with
3539952fc18eSTheodore Ts'o  * historical BSD practice is quite difficult in the face of
3540952fc18eSTheodore Ts'o  * clusters/bigalloc.  This is because multiple metadata blocks from
3541952fc18eSTheodore Ts'o  * different block group can end up in the same allocation cluster.
3542952fc18eSTheodore Ts'o  * Calculating the exact overhead in the face of clustered allocation
3543952fc18eSTheodore Ts'o  * requires either O(all block bitmaps) in memory or O(number of block
3544952fc18eSTheodore Ts'o  * groups**2) in time.  We will still calculate the superblock for
3545952fc18eSTheodore Ts'o  * older file systems --- and if we come across with a bigalloc file
3546952fc18eSTheodore Ts'o  * system with zero in s_overhead_clusters the estimate will be close to
3547952fc18eSTheodore Ts'o  * correct especially for very large cluster sizes --- but for newer
3548952fc18eSTheodore Ts'o  * file systems, it's better to calculate this figure once at mkfs
3549952fc18eSTheodore Ts'o  * time, and store it in the superblock.  If the superblock value is
3550952fc18eSTheodore Ts'o  * present (even for non-bigalloc file systems), we will use it.
3551952fc18eSTheodore Ts'o  */
3552952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp,
3553952fc18eSTheodore Ts'o 			  char *buf)
3554952fc18eSTheodore Ts'o {
3555952fc18eSTheodore Ts'o 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3556952fc18eSTheodore Ts'o 	struct ext4_group_desc	*gdp;
3557952fc18eSTheodore Ts'o 	ext4_fsblk_t		first_block, last_block, b;
3558952fc18eSTheodore Ts'o 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3559952fc18eSTheodore Ts'o 	int			s, j, count = 0;
3560952fc18eSTheodore Ts'o 
3561e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_bigalloc(sb))
35620548bbb8STheodore Ts'o 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
35630548bbb8STheodore Ts'o 			sbi->s_itb_per_group + 2);
35640548bbb8STheodore Ts'o 
3565952fc18eSTheodore Ts'o 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3566952fc18eSTheodore Ts'o 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3567952fc18eSTheodore Ts'o 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3568952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3569952fc18eSTheodore Ts'o 		gdp = ext4_get_group_desc(sb, i, NULL);
3570952fc18eSTheodore Ts'o 		b = ext4_block_bitmap(sb, gdp);
3571952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3572952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3573952fc18eSTheodore Ts'o 			count++;
3574952fc18eSTheodore Ts'o 		}
3575952fc18eSTheodore Ts'o 		b = ext4_inode_bitmap(sb, gdp);
3576952fc18eSTheodore Ts'o 		if (b >= first_block && b <= last_block) {
3577952fc18eSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3578952fc18eSTheodore Ts'o 			count++;
3579952fc18eSTheodore Ts'o 		}
3580952fc18eSTheodore Ts'o 		b = ext4_inode_table(sb, gdp);
3581952fc18eSTheodore Ts'o 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3582952fc18eSTheodore Ts'o 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3583952fc18eSTheodore Ts'o 				int c = EXT4_B2C(sbi, b - first_block);
3584952fc18eSTheodore Ts'o 				ext4_set_bit(c, buf);
3585952fc18eSTheodore Ts'o 				count++;
3586952fc18eSTheodore Ts'o 			}
3587952fc18eSTheodore Ts'o 		if (i != grp)
3588952fc18eSTheodore Ts'o 			continue;
3589952fc18eSTheodore Ts'o 		s = 0;
3590952fc18eSTheodore Ts'o 		if (ext4_bg_has_super(sb, grp)) {
3591952fc18eSTheodore Ts'o 			ext4_set_bit(s++, buf);
3592952fc18eSTheodore Ts'o 			count++;
3593952fc18eSTheodore Ts'o 		}
3594c48ae41bSTheodore Ts'o 		j = ext4_bg_num_gdb(sb, grp);
3595c48ae41bSTheodore Ts'o 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3596c48ae41bSTheodore Ts'o 			ext4_error(sb, "Invalid number of block group "
3597c48ae41bSTheodore Ts'o 				   "descriptor blocks: %d", j);
3598c48ae41bSTheodore Ts'o 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3599952fc18eSTheodore Ts'o 		}
3600c48ae41bSTheodore Ts'o 		count += j;
3601c48ae41bSTheodore Ts'o 		for (; j > 0; j--)
3602c48ae41bSTheodore Ts'o 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3603952fc18eSTheodore Ts'o 	}
3604952fc18eSTheodore Ts'o 	if (!count)
3605952fc18eSTheodore Ts'o 		return 0;
3606952fc18eSTheodore Ts'o 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3607952fc18eSTheodore Ts'o 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3608952fc18eSTheodore Ts'o }
3609952fc18eSTheodore Ts'o 
3610952fc18eSTheodore Ts'o /*
3611952fc18eSTheodore Ts'o  * Compute the overhead and stash it in sbi->s_overhead
3612952fc18eSTheodore Ts'o  */
3613952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb)
3614952fc18eSTheodore Ts'o {
3615952fc18eSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3616952fc18eSTheodore Ts'o 	struct ext4_super_block *es = sbi->s_es;
36173c816dedSEric Whitney 	struct inode *j_inode;
36183c816dedSEric Whitney 	unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
3619952fc18eSTheodore Ts'o 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3620952fc18eSTheodore Ts'o 	ext4_fsblk_t overhead = 0;
36214fdb5543SDmitry Monakhov 	char *buf = (char *) get_zeroed_page(GFP_NOFS);
3622952fc18eSTheodore Ts'o 
3623952fc18eSTheodore Ts'o 	if (!buf)
3624952fc18eSTheodore Ts'o 		return -ENOMEM;
3625952fc18eSTheodore Ts'o 
3626952fc18eSTheodore Ts'o 	/*
3627952fc18eSTheodore Ts'o 	 * Compute the overhead (FS structures).  This is constant
3628952fc18eSTheodore Ts'o 	 * for a given filesystem unless the number of block groups
3629952fc18eSTheodore Ts'o 	 * changes so we cache the previous value until it does.
3630952fc18eSTheodore Ts'o 	 */
3631952fc18eSTheodore Ts'o 
3632952fc18eSTheodore Ts'o 	/*
3633952fc18eSTheodore Ts'o 	 * All of the blocks before first_data_block are overhead
3634952fc18eSTheodore Ts'o 	 */
3635952fc18eSTheodore Ts'o 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3636952fc18eSTheodore Ts'o 
3637952fc18eSTheodore Ts'o 	/*
3638952fc18eSTheodore Ts'o 	 * Add the overhead found in each block group
3639952fc18eSTheodore Ts'o 	 */
3640952fc18eSTheodore Ts'o 	for (i = 0; i < ngroups; i++) {
3641952fc18eSTheodore Ts'o 		int blks;
3642952fc18eSTheodore Ts'o 
3643952fc18eSTheodore Ts'o 		blks = count_overhead(sb, i, buf);
3644952fc18eSTheodore Ts'o 		overhead += blks;
3645952fc18eSTheodore Ts'o 		if (blks)
3646952fc18eSTheodore Ts'o 			memset(buf, 0, PAGE_SIZE);
3647952fc18eSTheodore Ts'o 		cond_resched();
3648952fc18eSTheodore Ts'o 	}
36493c816dedSEric Whitney 
36503c816dedSEric Whitney 	/*
36513c816dedSEric Whitney 	 * Add the internal journal blocks whether the journal has been
36523c816dedSEric Whitney 	 * loaded or not
36533c816dedSEric Whitney 	 */
3654b003b524SEric Sandeen 	if (sbi->s_journal && !sbi->journal_bdev)
3655810da240SLukas Czerner 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
3656f1eec3b0SRitesh Harjani 	else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3657f1eec3b0SRitesh Harjani 		/* j_inum for internal journal is non-zero */
36583c816dedSEric Whitney 		j_inode = ext4_get_journal_inode(sb, j_inum);
36593c816dedSEric Whitney 		if (j_inode) {
36603c816dedSEric Whitney 			j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
36613c816dedSEric Whitney 			overhead += EXT4_NUM_B2C(sbi, j_blocks);
36623c816dedSEric Whitney 			iput(j_inode);
36633c816dedSEric Whitney 		} else {
36643c816dedSEric Whitney 			ext4_msg(sb, KERN_ERR, "can't get journal size");
36653c816dedSEric Whitney 		}
36663c816dedSEric Whitney 	}
3667952fc18eSTheodore Ts'o 	sbi->s_overhead = overhead;
3668952fc18eSTheodore Ts'o 	smp_wmb();
3669952fc18eSTheodore Ts'o 	free_page((unsigned long) buf);
3670952fc18eSTheodore Ts'o 	return 0;
3671952fc18eSTheodore Ts'o }
3672952fc18eSTheodore Ts'o 
3673b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb)
367427dd4385SLukas Czerner {
367527dd4385SLukas Czerner 	ext4_fsblk_t resv_clusters;
3676b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
367727dd4385SLukas Czerner 
367827dd4385SLukas Czerner 	/*
367930fac0f7SJan Kara 	 * There's no need to reserve anything when we aren't using extents.
368030fac0f7SJan Kara 	 * The space estimates are exact, there are no unwritten extents,
368130fac0f7SJan Kara 	 * hole punching doesn't need new metadata... This is needed especially
368230fac0f7SJan Kara 	 * to keep ext2/3 backward compatibility.
368330fac0f7SJan Kara 	 */
3684e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_extents(sb))
3685b5799018STheodore Ts'o 		return;
368630fac0f7SJan Kara 	/*
368727dd4385SLukas Czerner 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
368827dd4385SLukas Czerner 	 * This should cover the situations where we can not afford to run
368927dd4385SLukas Czerner 	 * out of space like for example punch hole, or converting
3690556615dcSLukas Czerner 	 * unwritten extents in delalloc path. In most cases such
369127dd4385SLukas Czerner 	 * allocation would require 1, or 2 blocks, higher numbers are
369227dd4385SLukas Czerner 	 * very rare.
369327dd4385SLukas Czerner 	 */
3694b5799018STheodore Ts'o 	resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3695b5799018STheodore Ts'o 			 sbi->s_cluster_bits);
369627dd4385SLukas Czerner 
369727dd4385SLukas Czerner 	do_div(resv_clusters, 50);
369827dd4385SLukas Czerner 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
369927dd4385SLukas Czerner 
3700b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, resv_clusters);
370127dd4385SLukas Czerner }
370227dd4385SLukas Czerner 
3703617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3704ac27a0ecSDave Kleikamp {
37055e405595SDan Williams 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
3706d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
37071d0c3924STheodore Ts'o 	struct buffer_head *bh, **group_desc;
3708617ba13bSMingming Cao 	struct ext4_super_block *es = NULL;
37095aee0f8aSTheodore Ts'o 	struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
37107c990728SSuraj Jitindar Singh 	struct flex_groups **flex_groups;
3711617ba13bSMingming Cao 	ext4_fsblk_t block;
3712617ba13bSMingming Cao 	ext4_fsblk_t sb_block = get_sb_block(&data);
371370bbb3e0SAndrew Morton 	ext4_fsblk_t logical_sb_block;
3714ac27a0ecSDave Kleikamp 	unsigned long offset = 0;
3715ac27a0ecSDave Kleikamp 	unsigned long journal_devnum = 0;
3716ac27a0ecSDave Kleikamp 	unsigned long def_mount_opts;
3717ac27a0ecSDave Kleikamp 	struct inode *root;
37180390131bSFrank Mayhar 	const char *descr;
3719dcc7dae3SCyrill Gorcunov 	int ret = -ENOMEM;
3720281b5995STheodore Ts'o 	int blocksize, clustersize;
37214ec11028STheodore Ts'o 	unsigned int db_count;
37224ec11028STheodore Ts'o 	unsigned int i;
3723281b5995STheodore Ts'o 	int needs_recovery, has_huge_files, has_bigalloc;
3724bd81d8eeSLaurent Vivier 	__u64 blocks_count;
372507aa2ea1SLukas Czerner 	int err = 0;
3726b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3727bfff6873SLukas Czerner 	ext4_group_t first_not_zeroed;
3728ac27a0ecSDave Kleikamp 
37295aee0f8aSTheodore Ts'o 	if ((data && !orig_data) || !sbi)
37305aee0f8aSTheodore Ts'o 		goto out_free_base;
3731705895b6SPekka Enberg 
3732aed9eb1bSColin Ian King 	sbi->s_daxdev = dax_dev;
3733705895b6SPekka Enberg 	sbi->s_blockgroup_lock =
3734705895b6SPekka Enberg 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
37355aee0f8aSTheodore Ts'o 	if (!sbi->s_blockgroup_lock)
37365aee0f8aSTheodore Ts'o 		goto out_free_base;
37375aee0f8aSTheodore Ts'o 
3738ac27a0ecSDave Kleikamp 	sb->s_fs_info = sbi;
37392c0544b2STheodore Ts'o 	sbi->s_sb = sb;
3740240799cdSTheodore Ts'o 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3741d9c9bef1SMiklos Szeredi 	sbi->s_sb_block = sb_block;
3742f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
3743f613dfcbSTheodore Ts'o 		sbi->s_sectors_written_start =
3744dbae2c55SMichael Callahan 			part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
3745ac27a0ecSDave Kleikamp 
37469f6200bbSTheodore Ts'o 	/* Cleanup superblock name */
3747ec3904dcSRasmus Villemoes 	strreplace(sb->s_id, '/', '!');
37489f6200bbSTheodore Ts'o 
374907aa2ea1SLukas Czerner 	/* -EINVAL is default */
3750dcc7dae3SCyrill Gorcunov 	ret = -EINVAL;
3751617ba13bSMingming Cao 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3752ac27a0ecSDave Kleikamp 	if (!blocksize) {
3753b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3754ac27a0ecSDave Kleikamp 		goto out_fail;
3755ac27a0ecSDave Kleikamp 	}
3756ac27a0ecSDave Kleikamp 
3757ac27a0ecSDave Kleikamp 	/*
3758617ba13bSMingming Cao 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3759ac27a0ecSDave Kleikamp 	 * block sizes.  We need to calculate the offset from buffer start.
3760ac27a0ecSDave Kleikamp 	 */
3761617ba13bSMingming Cao 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
376270bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
376370bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
3764ac27a0ecSDave Kleikamp 	} else {
376570bbb3e0SAndrew Morton 		logical_sb_block = sb_block;
3766ac27a0ecSDave Kleikamp 	}
3767ac27a0ecSDave Kleikamp 
3768a8ac900bSGioh Kim 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3769b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3770ac27a0ecSDave Kleikamp 		goto out_fail;
3771ac27a0ecSDave Kleikamp 	}
3772ac27a0ecSDave Kleikamp 	/*
3773ac27a0ecSDave Kleikamp 	 * Note: s_es must be initialized as soon as possible because
3774617ba13bSMingming Cao 	 *       some ext4 macro-instructions depend on its value
3775ac27a0ecSDave Kleikamp 	 */
37762716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
3777ac27a0ecSDave Kleikamp 	sbi->s_es = es;
3778ac27a0ecSDave Kleikamp 	sb->s_magic = le16_to_cpu(es->s_magic);
3779617ba13bSMingming Cao 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3780617ba13bSMingming Cao 		goto cantfind_ext4;
3781afc32f7eSTheodore Ts'o 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3782ac27a0ecSDave Kleikamp 
3783feb0ab32SDarrick J. Wong 	/* Warn if metadata_csum and gdt_csum are both set. */
3784e2b911c5SDarrick J. Wong 	if (ext4_has_feature_metadata_csum(sb) &&
3785e2b911c5SDarrick J. Wong 	    ext4_has_feature_gdt_csum(sb))
3786363307e6SJakub Wilk 		ext4_warning(sb, "metadata_csum and uninit_bg are "
3787feb0ab32SDarrick J. Wong 			     "redundant flags; please run fsck.");
3788feb0ab32SDarrick J. Wong 
3789d25425f8SDarrick J. Wong 	/* Check for a known checksum algorithm */
3790d25425f8SDarrick J. Wong 	if (!ext4_verify_csum_type(sb, es)) {
3791d25425f8SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3792d25425f8SDarrick J. Wong 			 "unknown checksum algorithm.");
3793d25425f8SDarrick J. Wong 		silent = 1;
3794d25425f8SDarrick J. Wong 		goto cantfind_ext4;
3795d25425f8SDarrick J. Wong 	}
3796d25425f8SDarrick J. Wong 
37970441984aSDarrick J. Wong 	/* Load the checksum driver */
37980441984aSDarrick J. Wong 	sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
37990441984aSDarrick J. Wong 	if (IS_ERR(sbi->s_chksum_driver)) {
38000441984aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
38010441984aSDarrick J. Wong 		ret = PTR_ERR(sbi->s_chksum_driver);
38020441984aSDarrick J. Wong 		sbi->s_chksum_driver = NULL;
38030441984aSDarrick J. Wong 		goto failed_mount;
38040441984aSDarrick J. Wong 	}
38050441984aSDarrick J. Wong 
3806a9c47317SDarrick J. Wong 	/* Check superblock checksum */
3807a9c47317SDarrick J. Wong 	if (!ext4_superblock_csum_verify(sb, es)) {
3808a9c47317SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3809a9c47317SDarrick J. Wong 			 "invalid superblock checksum.  Run e2fsck?");
3810a9c47317SDarrick J. Wong 		silent = 1;
38116a797d27SDarrick J. Wong 		ret = -EFSBADCRC;
3812a9c47317SDarrick J. Wong 		goto cantfind_ext4;
3813a9c47317SDarrick J. Wong 	}
3814a9c47317SDarrick J. Wong 
3815a9c47317SDarrick J. Wong 	/* Precompute checksum seed for all metadata */
3816e2b911c5SDarrick J. Wong 	if (ext4_has_feature_csum_seed(sb))
38178c81bd8fSDarrick J. Wong 		sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
3818dec214d0STahsin Erdogan 	else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
3819a9c47317SDarrick J. Wong 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3820a9c47317SDarrick J. Wong 					       sizeof(es->s_uuid));
3821a9c47317SDarrick J. Wong 
3822ac27a0ecSDave Kleikamp 	/* Set defaults before we parse the mount options */
3823ac27a0ecSDave Kleikamp 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3824fd8c37ecSTheodore Ts'o 	set_opt(sb, INIT_INODE_TABLE);
3825617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3826fd8c37ecSTheodore Ts'o 		set_opt(sb, DEBUG);
382787f26807STheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3828fd8c37ecSTheodore Ts'o 		set_opt(sb, GRPID);
3829617ba13bSMingming Cao 	if (def_mount_opts & EXT4_DEFM_UID16)
3830fd8c37ecSTheodore Ts'o 		set_opt(sb, NO_UID32);
3831ea663336SEric Sandeen 	/* xattr user namespace & acls are now defaulted on */
3832fd8c37ecSTheodore Ts'o 	set_opt(sb, XATTR_USER);
383303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL
3834fd8c37ecSTheodore Ts'o 	set_opt(sb, POSIX_ACL);
38352e7842b8SHugh Dickins #endif
383698c1a759SDarrick J. Wong 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
383798c1a759SDarrick J. Wong 	if (ext4_has_metadata_csum(sb))
383898c1a759SDarrick J. Wong 		set_opt(sb, JOURNAL_CHECKSUM);
383998c1a759SDarrick J. Wong 
3840617ba13bSMingming Cao 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3841fd8c37ecSTheodore Ts'o 		set_opt(sb, JOURNAL_DATA);
3842617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3843fd8c37ecSTheodore Ts'o 		set_opt(sb, ORDERED_DATA);
3844617ba13bSMingming Cao 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3845fd8c37ecSTheodore Ts'o 		set_opt(sb, WRITEBACK_DATA);
3846ac27a0ecSDave Kleikamp 
3847617ba13bSMingming Cao 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3848fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_PANIC);
3849bb4f397aSAneesh Kumar K.V 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3850fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_CONT);
3851bb4f397aSAneesh Kumar K.V 	else
3852fd8c37ecSTheodore Ts'o 		set_opt(sb, ERRORS_RO);
385345f1a9c3SDarrick J. Wong 	/* block_validity enabled by default; disable with noblock_validity */
3854fd8c37ecSTheodore Ts'o 	set_opt(sb, BLOCK_VALIDITY);
38558b67f04aSTheodore Ts'o 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3856fd8c37ecSTheodore Ts'o 		set_opt(sb, DISCARD);
3857ac27a0ecSDave Kleikamp 
385808cefc7aSEric W. Biederman 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
385908cefc7aSEric W. Biederman 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
386030773840STheodore Ts'o 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
386130773840STheodore Ts'o 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
386230773840STheodore Ts'o 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3863ac27a0ecSDave Kleikamp 
38648b67f04aSTheodore Ts'o 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3865fd8c37ecSTheodore Ts'o 		set_opt(sb, BARRIER);
3866ac27a0ecSDave Kleikamp 
38671e2462f9SMingming Cao 	/*
3868dd919b98SAneesh Kumar K.V 	 * enable delayed allocation by default
3869dd919b98SAneesh Kumar K.V 	 * Use -o nodelalloc to turn it off
3870dd919b98SAneesh Kumar K.V 	 */
3871bc0b75f7STheodore Ts'o 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
38728b67f04aSTheodore Ts'o 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3873fd8c37ecSTheodore Ts'o 		set_opt(sb, DELALLOC);
3874dd919b98SAneesh Kumar K.V 
387551ce6511SLukas Czerner 	/*
387651ce6511SLukas Czerner 	 * set default s_li_wait_mult for lazyinit, for the case there is
387751ce6511SLukas Czerner 	 * no mount option specified.
387851ce6511SLukas Czerner 	 */
387951ce6511SLukas Czerner 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
388051ce6511SLukas Czerner 
38814f97a681STheodore Ts'o 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3882626b035bSRitesh Harjani 
3883626b035bSRitesh Harjani 	if (blocksize == PAGE_SIZE)
3884626b035bSRitesh Harjani 		set_opt(sb, DIOREAD_NOLOCK);
3885626b035bSRitesh Harjani 
38864f97a681STheodore Ts'o 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
38874f97a681STheodore Ts'o 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
38884f97a681STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
38894f97a681STheodore Ts'o 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
38904f97a681STheodore Ts'o 			 blocksize, le32_to_cpu(es->s_log_block_size));
38914f97a681STheodore Ts'o 		goto failed_mount;
38924f97a681STheodore Ts'o 	}
38934f97a681STheodore Ts'o 
38949803387cSTheodore Ts'o 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
38959803387cSTheodore Ts'o 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
38969803387cSTheodore Ts'o 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
38979803387cSTheodore Ts'o 	} else {
38989803387cSTheodore Ts'o 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
38999803387cSTheodore Ts'o 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
39009803387cSTheodore Ts'o 		if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
39019803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
39029803387cSTheodore Ts'o 				 sbi->s_first_ino);
39039803387cSTheodore Ts'o 			goto failed_mount;
39049803387cSTheodore Ts'o 		}
39059803387cSTheodore Ts'o 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
39069803387cSTheodore Ts'o 		    (!is_power_of_2(sbi->s_inode_size)) ||
39079803387cSTheodore Ts'o 		    (sbi->s_inode_size > blocksize)) {
39089803387cSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
39099803387cSTheodore Ts'o 			       "unsupported inode size: %d",
39109803387cSTheodore Ts'o 			       sbi->s_inode_size);
39114f97a681STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
39129803387cSTheodore Ts'o 			goto failed_mount;
39139803387cSTheodore Ts'o 		}
39149803387cSTheodore Ts'o 		/*
39159803387cSTheodore Ts'o 		 * i_atime_extra is the last extra field available for
39169803387cSTheodore Ts'o 		 * [acm]times in struct ext4_inode. Checking for that
39179803387cSTheodore Ts'o 		 * field should suffice to ensure we have extra space
39189803387cSTheodore Ts'o 		 * for all three.
39199803387cSTheodore Ts'o 		 */
39209803387cSTheodore Ts'o 		if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
39219803387cSTheodore Ts'o 			sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
39229803387cSTheodore Ts'o 			sb->s_time_gran = 1;
39239803387cSTheodore Ts'o 			sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
39249803387cSTheodore Ts'o 		} else {
39259803387cSTheodore Ts'o 			sb->s_time_gran = NSEC_PER_SEC;
39269803387cSTheodore Ts'o 			sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
39279803387cSTheodore Ts'o 		}
39289803387cSTheodore Ts'o 		sb->s_time_min = EXT4_TIMESTAMP_MIN;
39299803387cSTheodore Ts'o 	}
39309803387cSTheodore Ts'o 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
39319803387cSTheodore Ts'o 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
39329803387cSTheodore Ts'o 			EXT4_GOOD_OLD_INODE_SIZE;
39339803387cSTheodore Ts'o 		if (ext4_has_feature_extra_isize(sb)) {
39349803387cSTheodore Ts'o 			unsigned v, max = (sbi->s_inode_size -
39359803387cSTheodore Ts'o 					   EXT4_GOOD_OLD_INODE_SIZE);
39369803387cSTheodore Ts'o 
39379803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_want_extra_isize);
39389803387cSTheodore Ts'o 			if (v > max) {
39399803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
39409803387cSTheodore Ts'o 					 "bad s_want_extra_isize: %d", v);
39419803387cSTheodore Ts'o 				goto failed_mount;
39429803387cSTheodore Ts'o 			}
39439803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
39449803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
39459803387cSTheodore Ts'o 
39469803387cSTheodore Ts'o 			v = le16_to_cpu(es->s_min_extra_isize);
39479803387cSTheodore Ts'o 			if (v > max) {
39489803387cSTheodore Ts'o 				ext4_msg(sb, KERN_ERR,
39499803387cSTheodore Ts'o 					 "bad s_min_extra_isize: %d", v);
39509803387cSTheodore Ts'o 				goto failed_mount;
39519803387cSTheodore Ts'o 			}
39529803387cSTheodore Ts'o 			if (sbi->s_want_extra_isize < v)
39539803387cSTheodore Ts'o 				sbi->s_want_extra_isize = v;
39549803387cSTheodore Ts'o 		}
39559803387cSTheodore Ts'o 	}
39569803387cSTheodore Ts'o 
39575aee0f8aSTheodore Ts'o 	if (sbi->s_es->s_mount_opts[0]) {
39585aee0f8aSTheodore Ts'o 		char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
39595aee0f8aSTheodore Ts'o 					      sizeof(sbi->s_es->s_mount_opts),
39605aee0f8aSTheodore Ts'o 					      GFP_KERNEL);
39615aee0f8aSTheodore Ts'o 		if (!s_mount_opts)
39625aee0f8aSTheodore Ts'o 			goto failed_mount;
39635aee0f8aSTheodore Ts'o 		if (!parse_options(s_mount_opts, sb, &journal_devnum,
39645aee0f8aSTheodore Ts'o 				   &journal_ioprio, 0)) {
39658b67f04aSTheodore Ts'o 			ext4_msg(sb, KERN_WARNING,
39668b67f04aSTheodore Ts'o 				 "failed to parse options in superblock: %s",
39675aee0f8aSTheodore Ts'o 				 s_mount_opts);
39685aee0f8aSTheodore Ts'o 		}
39695aee0f8aSTheodore Ts'o 		kfree(s_mount_opts);
39708b67f04aSTheodore Ts'o 	}
39715a916be1STheodore Ts'o 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3972b3881f74STheodore Ts'o 	if (!parse_options((char *) data, sb, &journal_devnum,
3973661aa520SEric Sandeen 			   &journal_ioprio, 0))
3974ac27a0ecSDave Kleikamp 		goto failed_mount;
3975ac27a0ecSDave Kleikamp 
3976c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3977c83ad55eSGabriel Krisman Bertazi 	if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
3978c83ad55eSGabriel Krisman Bertazi 		const struct ext4_sb_encodings *encoding_info;
3979c83ad55eSGabriel Krisman Bertazi 		struct unicode_map *encoding;
3980c83ad55eSGabriel Krisman Bertazi 		__u16 encoding_flags;
3981c83ad55eSGabriel Krisman Bertazi 
3982c83ad55eSGabriel Krisman Bertazi 		if (ext4_has_feature_encrypt(sb)) {
3983c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3984c83ad55eSGabriel Krisman Bertazi 				 "Can't mount with encoding and encryption");
3985c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3986c83ad55eSGabriel Krisman Bertazi 		}
3987c83ad55eSGabriel Krisman Bertazi 
3988c83ad55eSGabriel Krisman Bertazi 		if (ext4_sb_read_encoding(es, &encoding_info,
3989c83ad55eSGabriel Krisman Bertazi 					  &encoding_flags)) {
3990c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3991c83ad55eSGabriel Krisman Bertazi 				 "Encoding requested by superblock is unknown");
3992c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
3993c83ad55eSGabriel Krisman Bertazi 		}
3994c83ad55eSGabriel Krisman Bertazi 
3995c83ad55eSGabriel Krisman Bertazi 		encoding = utf8_load(encoding_info->version);
3996c83ad55eSGabriel Krisman Bertazi 		if (IS_ERR(encoding)) {
3997c83ad55eSGabriel Krisman Bertazi 			ext4_msg(sb, KERN_ERR,
3998c83ad55eSGabriel Krisman Bertazi 				 "can't mount with superblock charset: %s-%s "
3999c83ad55eSGabriel Krisman Bertazi 				 "not supported by the kernel. flags: 0x%x.",
4000c83ad55eSGabriel Krisman Bertazi 				 encoding_info->name, encoding_info->version,
4001c83ad55eSGabriel Krisman Bertazi 				 encoding_flags);
4002c83ad55eSGabriel Krisman Bertazi 			goto failed_mount;
4003c83ad55eSGabriel Krisman Bertazi 		}
4004c83ad55eSGabriel Krisman Bertazi 		ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4005c83ad55eSGabriel Krisman Bertazi 			 "%s-%s with flags 0x%hx", encoding_info->name,
4006c83ad55eSGabriel Krisman Bertazi 			 encoding_info->version?:"\b", encoding_flags);
4007c83ad55eSGabriel Krisman Bertazi 
4008c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding = encoding;
4009c83ad55eSGabriel Krisman Bertazi 		sbi->s_encoding_flags = encoding_flags;
4010c83ad55eSGabriel Krisman Bertazi 	}
4011c83ad55eSGabriel Krisman Bertazi #endif
4012c83ad55eSGabriel Krisman Bertazi 
401356889787STheodore Ts'o 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4014244adf64STheodore Ts'o 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
4015244adf64STheodore Ts'o 		clear_opt(sb, DIOREAD_NOLOCK);
401656889787STheodore Ts'o 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
401756889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
401856889787STheodore Ts'o 				 "both data=journal and delalloc");
401956889787STheodore Ts'o 			goto failed_mount;
402056889787STheodore Ts'o 		}
402156889787STheodore Ts'o 		if (test_opt(sb, DIOREAD_NOLOCK)) {
402256889787STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "can't mount with "
40236ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
402456889787STheodore Ts'o 			goto failed_mount;
402556889787STheodore Ts'o 		}
4026fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
4027923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
4028923ae0ffSRoss Zwisler 				 "both data=journal and dax");
4029923ae0ffSRoss Zwisler 			goto failed_mount;
4030923ae0ffSRoss Zwisler 		}
403173b92a2aSSergey Karamov 		if (ext4_has_feature_encrypt(sb)) {
403273b92a2aSSergey Karamov 			ext4_msg(sb, KERN_WARNING,
403373b92a2aSSergey Karamov 				 "encrypted files will use data=ordered "
403473b92a2aSSergey Karamov 				 "instead of data journaling mode");
403573b92a2aSSergey Karamov 		}
403656889787STheodore Ts'o 		if (test_opt(sb, DELALLOC))
403756889787STheodore Ts'o 			clear_opt(sb, DELALLOC);
4038001e4a87STejun Heo 	} else {
4039001e4a87STejun Heo 		sb->s_iflags |= SB_I_CGROUPWB;
404056889787STheodore Ts'o 	}
404156889787STheodore Ts'o 
40421751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
40431751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
4044ac27a0ecSDave Kleikamp 
4045617ba13bSMingming Cao 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4046e2b911c5SDarrick J. Wong 	    (ext4_has_compat_features(sb) ||
4047e2b911c5SDarrick J. Wong 	     ext4_has_ro_compat_features(sb) ||
4048e2b911c5SDarrick J. Wong 	     ext4_has_incompat_features(sb)))
4049b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING,
4050b31e1552SEric Sandeen 		       "feature flags set on rev 0 fs, "
4051b31e1552SEric Sandeen 		       "running e2fsck is recommended");
4052469108ffSTheodore Tso 
4053ed3654ebSTheodore Ts'o 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4054ed3654ebSTheodore Ts'o 		set_opt2(sb, HURD_COMPAT);
4055e2b911c5SDarrick J. Wong 		if (ext4_has_feature_64bit(sb)) {
4056ed3654ebSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4057ed3654ebSTheodore Ts'o 				 "The Hurd can't support 64-bit file systems");
4058ed3654ebSTheodore Ts'o 			goto failed_mount;
4059ed3654ebSTheodore Ts'o 		}
4060dec214d0STahsin Erdogan 
4061dec214d0STahsin Erdogan 		/*
4062dec214d0STahsin Erdogan 		 * ea_inode feature uses l_i_version field which is not
4063dec214d0STahsin Erdogan 		 * available in HURD_COMPAT mode.
4064dec214d0STahsin Erdogan 		 */
4065dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4066dec214d0STahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4067dec214d0STahsin Erdogan 				 "ea_inode feature is not supported for Hurd");
4068dec214d0STahsin Erdogan 			goto failed_mount;
4069dec214d0STahsin Erdogan 		}
4070ed3654ebSTheodore Ts'o 	}
4071ed3654ebSTheodore Ts'o 
40722035e776STheodore Ts'o 	if (IS_EXT2_SB(sb)) {
40732035e776STheodore Ts'o 		if (ext2_feature_set_ok(sb))
40742035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
40752035e776STheodore Ts'o 				 "using the ext4 subsystem");
40762035e776STheodore Ts'o 		else {
40770d9366d6SEric Sandeen 			/*
40780d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
40790d9366d6SEric Sandeen 			 * it's actually an ext[34] filesystem.
40800d9366d6SEric Sandeen 			 */
40810d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
40820d9366d6SEric Sandeen 				goto failed_mount;
40832035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
40842035e776STheodore Ts'o 				 "to feature incompatibilities");
40852035e776STheodore Ts'o 			goto failed_mount;
40862035e776STheodore Ts'o 		}
40872035e776STheodore Ts'o 	}
40882035e776STheodore Ts'o 
40892035e776STheodore Ts'o 	if (IS_EXT3_SB(sb)) {
40902035e776STheodore Ts'o 		if (ext3_feature_set_ok(sb))
40912035e776STheodore Ts'o 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
40922035e776STheodore Ts'o 				 "using the ext4 subsystem");
40932035e776STheodore Ts'o 		else {
40940d9366d6SEric Sandeen 			/*
40950d9366d6SEric Sandeen 			 * If we're probing be silent, if this looks like
40960d9366d6SEric Sandeen 			 * it's actually an ext4 filesystem.
40970d9366d6SEric Sandeen 			 */
40980d9366d6SEric Sandeen 			if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
40990d9366d6SEric Sandeen 				goto failed_mount;
41002035e776STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
41012035e776STheodore Ts'o 				 "to feature incompatibilities");
41022035e776STheodore Ts'o 			goto failed_mount;
41032035e776STheodore Ts'o 		}
41042035e776STheodore Ts'o 	}
41052035e776STheodore Ts'o 
4106469108ffSTheodore Tso 	/*
4107ac27a0ecSDave Kleikamp 	 * Check feature flags regardless of the revision level, since we
4108ac27a0ecSDave Kleikamp 	 * previously didn't change the revision level when setting the flags,
4109ac27a0ecSDave Kleikamp 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
4110ac27a0ecSDave Kleikamp 	 */
4111bc98a42cSDavid Howells 	if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4112ac27a0ecSDave Kleikamp 		goto failed_mount;
4113a13fb1a4SEric Sandeen 
41148cdf3372STheodore Ts'o 	if (le32_to_cpu(es->s_log_block_size) >
41158cdf3372STheodore Ts'o 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
41168cdf3372STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
41178cdf3372STheodore Ts'o 			 "Invalid log block size: %u",
41188cdf3372STheodore Ts'o 			 le32_to_cpu(es->s_log_block_size));
4119ac27a0ecSDave Kleikamp 		goto failed_mount;
4120ac27a0ecSDave Kleikamp 	}
4121bfe0a5f4STheodore Ts'o 	if (le32_to_cpu(es->s_log_cluster_size) >
4122bfe0a5f4STheodore Ts'o 	    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4123bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_ERR,
4124bfe0a5f4STheodore Ts'o 			 "Invalid log cluster size: %u",
4125bfe0a5f4STheodore Ts'o 			 le32_to_cpu(es->s_log_cluster_size));
4126bfe0a5f4STheodore Ts'o 		goto failed_mount;
4127bfe0a5f4STheodore Ts'o 	}
4128ac27a0ecSDave Kleikamp 
41295b9554dcSTheodore Ts'o 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
41305b9554dcSTheodore Ts'o 		ext4_msg(sb, KERN_ERR,
41315b9554dcSTheodore Ts'o 			 "Number of reserved GDT blocks insanely large: %d",
41325b9554dcSTheodore Ts'o 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
41335b9554dcSTheodore Ts'o 		goto failed_mount;
41345b9554dcSTheodore Ts'o 	}
41355b9554dcSTheodore Ts'o 
4136a8ab6d38SIra Weiny 	if (bdev_dax_supported(sb->s_bdev, blocksize))
4137a8ab6d38SIra Weiny 		set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4138a8ab6d38SIra Weiny 
4139fc626fe3SIra Weiny 	if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4140559db4c6SRoss Zwisler 		if (ext4_has_feature_inline_data(sb)) {
4141559db4c6SRoss Zwisler 			ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4142559db4c6SRoss Zwisler 					" that may contain inline data");
4143361d24d4SEric Sandeen 			goto failed_mount;
4144559db4c6SRoss Zwisler 		}
4145a8ab6d38SIra Weiny 		if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
414624f3478dSDan Williams 			ext4_msg(sb, KERN_ERR,
4147361d24d4SEric Sandeen 				"DAX unsupported by block device.");
4148361d24d4SEric Sandeen 			goto failed_mount;
414924f3478dSDan Williams 		}
4150923ae0ffSRoss Zwisler 	}
4151923ae0ffSRoss Zwisler 
4152e2b911c5SDarrick J. Wong 	if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
41536ddb2447STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
41546ddb2447STheodore Ts'o 			 es->s_encryption_level);
41556ddb2447STheodore Ts'o 		goto failed_mount;
41566ddb2447STheodore Ts'o 	}
41576ddb2447STheodore Ts'o 
4158ac27a0ecSDave Kleikamp 	if (sb->s_blocksize != blocksize) {
4159ce40733cSAneesh Kumar K.V 		/* Validate the filesystem blocksize */
4160ce40733cSAneesh Kumar K.V 		if (!sb_set_blocksize(sb, blocksize)) {
4161b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "bad block size %d",
4162ce40733cSAneesh Kumar K.V 					blocksize);
4163ac27a0ecSDave Kleikamp 			goto failed_mount;
4164ac27a0ecSDave Kleikamp 		}
4165ac27a0ecSDave Kleikamp 
4166ac27a0ecSDave Kleikamp 		brelse(bh);
416770bbb3e0SAndrew Morton 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
416870bbb3e0SAndrew Morton 		offset = do_div(logical_sb_block, blocksize);
4169a8ac900bSGioh Kim 		bh = sb_bread_unmovable(sb, logical_sb_block);
4170ac27a0ecSDave Kleikamp 		if (!bh) {
4171b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4172b31e1552SEric Sandeen 			       "Can't read superblock on 2nd try");
4173ac27a0ecSDave Kleikamp 			goto failed_mount;
4174ac27a0ecSDave Kleikamp 		}
41752716b802STheodore Ts'o 		es = (struct ext4_super_block *)(bh->b_data + offset);
4176ac27a0ecSDave Kleikamp 		sbi->s_es = es;
4177617ba13bSMingming Cao 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
4178b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4179b31e1552SEric Sandeen 			       "Magic mismatch, very weird!");
4180ac27a0ecSDave Kleikamp 			goto failed_mount;
4181ac27a0ecSDave Kleikamp 		}
4182ac27a0ecSDave Kleikamp 	}
4183ac27a0ecSDave Kleikamp 
4184e2b911c5SDarrick J. Wong 	has_huge_files = ext4_has_feature_huge_file(sb);
4185f287a1a5STheodore Ts'o 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4186f287a1a5STheodore Ts'o 						      has_huge_files);
4187f287a1a5STheodore Ts'o 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
4188ac27a0ecSDave Kleikamp 
41890d1ee42fSAlexandre Ratchov 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
4190e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb)) {
41918fadc143SAlexandre Ratchov 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
41920d1ee42fSAlexandre Ratchov 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
4193d8ea6cf8Svignesh babu 		    !is_power_of_2(sbi->s_desc_size)) {
4194b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4195b31e1552SEric Sandeen 			       "unsupported descriptor size %lu",
41960d1ee42fSAlexandre Ratchov 			       sbi->s_desc_size);
41970d1ee42fSAlexandre Ratchov 			goto failed_mount;
41980d1ee42fSAlexandre Ratchov 		}
41990d1ee42fSAlexandre Ratchov 	} else
42000d1ee42fSAlexandre Ratchov 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
42010b8e58a1SAndreas Dilger 
4202ac27a0ecSDave Kleikamp 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
4203ac27a0ecSDave Kleikamp 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
42040b8e58a1SAndreas Dilger 
4205617ba13bSMingming Cao 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
4206ac27a0ecSDave Kleikamp 	if (sbi->s_inodes_per_block == 0)
4207617ba13bSMingming Cao 		goto cantfind_ext4;
4208cd6bb35bSTheodore Ts'o 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4209cd6bb35bSTheodore Ts'o 	    sbi->s_inodes_per_group > blocksize * 8) {
4210cd6bb35bSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
4211b9c538daSJosh Triplett 			 sbi->s_inodes_per_group);
4212cd6bb35bSTheodore Ts'o 		goto failed_mount;
4213cd6bb35bSTheodore Ts'o 	}
4214ac27a0ecSDave Kleikamp 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
4215ac27a0ecSDave Kleikamp 					sbi->s_inodes_per_block;
42160d1ee42fSAlexandre Ratchov 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
4217ac27a0ecSDave Kleikamp 	sbi->s_sbh = bh;
4218ac27a0ecSDave Kleikamp 	sbi->s_mount_state = le16_to_cpu(es->s_state);
4219e57aa839SFengguang Wu 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4220e57aa839SFengguang Wu 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
42210b8e58a1SAndreas Dilger 
4222ac27a0ecSDave Kleikamp 	for (i = 0; i < 4; i++)
4223ac27a0ecSDave Kleikamp 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4224ac27a0ecSDave Kleikamp 	sbi->s_def_hash_version = es->s_def_hash_version;
4225e2b911c5SDarrick J. Wong 	if (ext4_has_feature_dir_index(sb)) {
4226f99b2589STheodore Ts'o 		i = le32_to_cpu(es->s_flags);
4227f99b2589STheodore Ts'o 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
4228f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4229f99b2589STheodore Ts'o 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
4230f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__
4231bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
423223301410STheodore Ts'o 				es->s_flags |=
423323301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4234f99b2589STheodore Ts'o 			sbi->s_hash_unsigned = 3;
4235f99b2589STheodore Ts'o #else
4236bc98a42cSDavid Howells 			if (!sb_rdonly(sb))
423723301410STheodore Ts'o 				es->s_flags |=
423823301410STheodore Ts'o 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
4239f99b2589STheodore Ts'o #endif
4240f99b2589STheodore Ts'o 		}
424123301410STheodore Ts'o 	}
4242ac27a0ecSDave Kleikamp 
4243281b5995STheodore Ts'o 	/* Handle clustersize */
4244281b5995STheodore Ts'o 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4245e2b911c5SDarrick J. Wong 	has_bigalloc = ext4_has_feature_bigalloc(sb);
4246281b5995STheodore Ts'o 	if (has_bigalloc) {
4247281b5995STheodore Ts'o 		if (clustersize < blocksize) {
4248281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4249281b5995STheodore Ts'o 				 "cluster size (%d) smaller than "
4250281b5995STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4251281b5995STheodore Ts'o 			goto failed_mount;
4252281b5995STheodore Ts'o 		}
4253281b5995STheodore Ts'o 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4254281b5995STheodore Ts'o 			le32_to_cpu(es->s_log_block_size);
4255281b5995STheodore Ts'o 		sbi->s_clusters_per_group =
4256281b5995STheodore Ts'o 			le32_to_cpu(es->s_clusters_per_group);
4257281b5995STheodore Ts'o 		if (sbi->s_clusters_per_group > blocksize * 8) {
4258281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4259281b5995STheodore Ts'o 				 "#clusters per group too big: %lu",
4260281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4261281b5995STheodore Ts'o 			goto failed_mount;
4262281b5995STheodore Ts'o 		}
4263281b5995STheodore Ts'o 		if (sbi->s_blocks_per_group !=
4264281b5995STheodore Ts'o 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4265281b5995STheodore Ts'o 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4266281b5995STheodore Ts'o 				 "clusters per group (%lu) inconsistent",
4267281b5995STheodore Ts'o 				 sbi->s_blocks_per_group,
4268281b5995STheodore Ts'o 				 sbi->s_clusters_per_group);
4269281b5995STheodore Ts'o 			goto failed_mount;
4270281b5995STheodore Ts'o 		}
4271281b5995STheodore Ts'o 	} else {
4272281b5995STheodore Ts'o 		if (clustersize != blocksize) {
4273bfe0a5f4STheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4274bfe0a5f4STheodore Ts'o 				 "fragment/cluster size (%d) != "
4275bfe0a5f4STheodore Ts'o 				 "block size (%d)", clustersize, blocksize);
4276bfe0a5f4STheodore Ts'o 			goto failed_mount;
4277281b5995STheodore Ts'o 		}
4278ac27a0ecSDave Kleikamp 		if (sbi->s_blocks_per_group > blocksize * 8) {
4279b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4280b31e1552SEric Sandeen 				 "#blocks per group too big: %lu",
4281ac27a0ecSDave Kleikamp 				 sbi->s_blocks_per_group);
4282ac27a0ecSDave Kleikamp 			goto failed_mount;
4283ac27a0ecSDave Kleikamp 		}
4284281b5995STheodore Ts'o 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4285281b5995STheodore Ts'o 		sbi->s_cluster_bits = 0;
4286281b5995STheodore Ts'o 	}
4287281b5995STheodore Ts'o 	sbi->s_cluster_ratio = clustersize / blocksize;
4288281b5995STheodore Ts'o 
4289960fd856STheodore Ts'o 	/* Do we have standard group size of clustersize * 8 blocks ? */
4290960fd856STheodore Ts'o 	if (sbi->s_blocks_per_group == clustersize << 3)
4291960fd856STheodore Ts'o 		set_opt2(sb, STD_GROUP_SIZE);
4292960fd856STheodore Ts'o 
4293bf43d84bSEric Sandeen 	/*
4294bf43d84bSEric Sandeen 	 * Test whether we have more sectors than will fit in sector_t,
4295bf43d84bSEric Sandeen 	 * and whether the max offset is addressable by the page cache.
4296bf43d84bSEric Sandeen 	 */
42975a9ae68aSDarrick J. Wong 	err = generic_check_addressable(sb->s_blocksize_bits,
429830ca22c7SPatrick J. LoPresti 					ext4_blocks_count(es));
42995a9ae68aSDarrick J. Wong 	if (err) {
4300b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem"
4301bf43d84bSEric Sandeen 			 " too large to mount safely on this system");
4302ac27a0ecSDave Kleikamp 		goto failed_mount;
4303ac27a0ecSDave Kleikamp 	}
4304ac27a0ecSDave Kleikamp 
4305617ba13bSMingming Cao 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4306617ba13bSMingming Cao 		goto cantfind_ext4;
4307e7c95593SEric Sandeen 
43080f2ddca6SFrom: Thiemo Nagel 	/* check blocks count against device size */
43090f2ddca6SFrom: Thiemo Nagel 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
43100f2ddca6SFrom: Thiemo Nagel 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4311b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4312b31e1552SEric Sandeen 		       "exceeds size of device (%llu blocks)",
43130f2ddca6SFrom: Thiemo Nagel 		       ext4_blocks_count(es), blocks_count);
43140f2ddca6SFrom: Thiemo Nagel 		goto failed_mount;
43150f2ddca6SFrom: Thiemo Nagel 	}
43160f2ddca6SFrom: Thiemo Nagel 
43174ec11028STheodore Ts'o 	/*
43184ec11028STheodore Ts'o 	 * It makes no sense for the first data block to be beyond the end
43194ec11028STheodore Ts'o 	 * of the filesystem.
43204ec11028STheodore Ts'o 	 */
43214ec11028STheodore Ts'o 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4322b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4323b31e1552SEric Sandeen 			 "block %u is beyond end of filesystem (%llu)",
4324e7c95593SEric Sandeen 			 le32_to_cpu(es->s_first_data_block),
43254ec11028STheodore Ts'o 			 ext4_blocks_count(es));
4326e7c95593SEric Sandeen 		goto failed_mount;
4327e7c95593SEric Sandeen 	}
4328bfe0a5f4STheodore Ts'o 	if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4329bfe0a5f4STheodore Ts'o 	    (sbi->s_cluster_ratio == 1)) {
4330bfe0a5f4STheodore Ts'o 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4331bfe0a5f4STheodore Ts'o 			 "block is 0 with a 1k block and cluster size");
4332bfe0a5f4STheodore Ts'o 		goto failed_mount;
4333bfe0a5f4STheodore Ts'o 	}
4334bfe0a5f4STheodore Ts'o 
4335bd81d8eeSLaurent Vivier 	blocks_count = (ext4_blocks_count(es) -
4336bd81d8eeSLaurent Vivier 			le32_to_cpu(es->s_first_data_block) +
4337bd81d8eeSLaurent Vivier 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
4338bd81d8eeSLaurent Vivier 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
43394ec11028STheodore Ts'o 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4340df41460aSJosh Triplett 		ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
43414ec11028STheodore Ts'o 		       "(block count %llu, first data block %u, "
4342df41460aSJosh Triplett 		       "blocks per group %lu)", blocks_count,
43434ec11028STheodore Ts'o 		       ext4_blocks_count(es),
43444ec11028STheodore Ts'o 		       le32_to_cpu(es->s_first_data_block),
43454ec11028STheodore Ts'o 		       EXT4_BLOCKS_PER_GROUP(sb));
43464ec11028STheodore Ts'o 		goto failed_mount;
43474ec11028STheodore Ts'o 	}
4348bd81d8eeSLaurent Vivier 	sbi->s_groups_count = blocks_count;
4349fb0a387dSEric Sandeen 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4350fb0a387dSEric Sandeen 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
43519e463084STheodore Ts'o 	if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
43529e463084STheodore Ts'o 	    le32_to_cpu(es->s_inodes_count)) {
43539e463084STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
43549e463084STheodore Ts'o 			 le32_to_cpu(es->s_inodes_count),
43559e463084STheodore Ts'o 			 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
43569e463084STheodore Ts'o 		ret = -EINVAL;
43579e463084STheodore Ts'o 		goto failed_mount;
43589e463084STheodore Ts'o 	}
4359617ba13bSMingming Cao 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4360617ba13bSMingming Cao 		   EXT4_DESC_PER_BLOCK(sb);
43613a4b77cdSEryu Guan 	if (ext4_has_feature_meta_bg(sb)) {
43622ba3e6e8STheodore Ts'o 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
43633a4b77cdSEryu Guan 			ext4_msg(sb, KERN_WARNING,
43643a4b77cdSEryu Guan 				 "first meta block group too large: %u "
43653a4b77cdSEryu Guan 				 "(group descriptor block count %u)",
43663a4b77cdSEryu Guan 				 le32_to_cpu(es->s_first_meta_bg), db_count);
43673a4b77cdSEryu Guan 			goto failed_mount;
43683a4b77cdSEryu Guan 		}
43693a4b77cdSEryu Guan 	}
43701d0c3924STheodore Ts'o 	rcu_assign_pointer(sbi->s_group_desc,
43711d0c3924STheodore Ts'o 			   kvmalloc_array(db_count,
4372f18a5f21STheodore Ts'o 					  sizeof(struct buffer_head *),
43731d0c3924STheodore Ts'o 					  GFP_KERNEL));
4374ac27a0ecSDave Kleikamp 	if (sbi->s_group_desc == NULL) {
4375b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "not enough memory");
43762cde417dSTheodore Ts'o 		ret = -ENOMEM;
4377ac27a0ecSDave Kleikamp 		goto failed_mount;
4378ac27a0ecSDave Kleikamp 	}
4379ac27a0ecSDave Kleikamp 
4380705895b6SPekka Enberg 	bgl_lock_init(sbi->s_blockgroup_lock);
4381ac27a0ecSDave Kleikamp 
438285c8f176SAndrew Perepechko 	/* Pre-read the descriptors into the buffer cache */
438385c8f176SAndrew Perepechko 	for (i = 0; i < db_count; i++) {
438485c8f176SAndrew Perepechko 		block = descriptor_loc(sb, logical_sb_block, i);
4385d87f6392SRoman Gushchin 		sb_breadahead_unmovable(sb, block);
438685c8f176SAndrew Perepechko 	}
438785c8f176SAndrew Perepechko 
4388ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++) {
43891d0c3924STheodore Ts'o 		struct buffer_head *bh;
43901d0c3924STheodore Ts'o 
439170bbb3e0SAndrew Morton 		block = descriptor_loc(sb, logical_sb_block, i);
43921d0c3924STheodore Ts'o 		bh = sb_bread_unmovable(sb, block);
43931d0c3924STheodore Ts'o 		if (!bh) {
4394b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR,
4395b31e1552SEric Sandeen 			       "can't read group descriptor %d", i);
4396ac27a0ecSDave Kleikamp 			db_count = i;
4397ac27a0ecSDave Kleikamp 			goto failed_mount2;
4398ac27a0ecSDave Kleikamp 		}
43991d0c3924STheodore Ts'o 		rcu_read_lock();
44001d0c3924STheodore Ts'o 		rcu_dereference(sbi->s_group_desc)[i] = bh;
44011d0c3924STheodore Ts'o 		rcu_read_unlock();
4402ac27a0ecSDave Kleikamp 	}
440344de022cSTheodore Ts'o 	sbi->s_gdb_count = db_count;
4404829fa70dSTheodore Ts'o 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4405b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
44066a797d27SDarrick J. Wong 		ret = -EFSCORRUPTED;
4407f9ae9cf5STheodore Ts'o 		goto failed_mount2;
4408ac27a0ecSDave Kleikamp 	}
4409772cb7c8SJose R. Santos 
4410235699a8SKees Cook 	timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
441104496411STao Ma 
4412a75ae78fSDmitry Monakhov 	/* Register extent status tree shrinker */
4413eb68d0e2SZheng Liu 	if (ext4_es_register_shrinker(sbi))
4414ce7e010aSTheodore Ts'o 		goto failed_mount3;
4415ce7e010aSTheodore Ts'o 
4416c9de560dSAlex Tomas 	sbi->s_stripe = ext4_get_stripe_size(sbi);
441767a5da56SZheng Liu 	sbi->s_extent_max_zeroout_kb = 32;
4418c9de560dSAlex Tomas 
4419f9ae9cf5STheodore Ts'o 	/*
4420f9ae9cf5STheodore Ts'o 	 * set up enough so that it can read an inode
4421f9ae9cf5STheodore Ts'o 	 */
4422f9ae9cf5STheodore Ts'o 	sb->s_op = &ext4_sops;
4423617ba13bSMingming Cao 	sb->s_export_op = &ext4_export_ops;
4424617ba13bSMingming Cao 	sb->s_xattr = ext4_xattr_handlers;
4425643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4426a7550b30SJaegeuk Kim 	sb->s_cop = &ext4_cryptops;
4427ffcc4182SEric Biggers #endif
4428c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
4429c93d8f88SEric Biggers 	sb->s_vop = &ext4_verityops;
4430c93d8f88SEric Biggers #endif
4431ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4432617ba13bSMingming Cao 	sb->dq_op = &ext4_quota_operations;
4433e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb))
44341fa5efe3SJan Kara 		sb->s_qcop = &dquot_quotactl_sysfile_ops;
4435262b4662SJan Kara 	else
4436262b4662SJan Kara 		sb->s_qcop = &ext4_qctl_operations;
4437689c958cSLi Xi 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
4438ac27a0ecSDave Kleikamp #endif
443985787090SChristoph Hellwig 	memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4440f2fa2ffcSAneesh Kumar K.V 
4441ac27a0ecSDave Kleikamp 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
44423b9d4ed2STheodore Ts'o 	mutex_init(&sbi->s_orphan_lock);
4443ac27a0ecSDave Kleikamp 
4444ac27a0ecSDave Kleikamp 	sb->s_root = NULL;
4445ac27a0ecSDave Kleikamp 
4446ac27a0ecSDave Kleikamp 	needs_recovery = (es->s_last_orphan != 0 ||
4447e2b911c5SDarrick J. Wong 			  ext4_has_feature_journal_needs_recovery(sb));
4448ac27a0ecSDave Kleikamp 
4449bc98a42cSDavid Howells 	if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
4450c5e06d10SJohann Lombardi 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
445150460fe8SDarrick J. Wong 			goto failed_mount3a;
4452c5e06d10SJohann Lombardi 
4453ac27a0ecSDave Kleikamp 	/*
4454ac27a0ecSDave Kleikamp 	 * The first inode we look at is the journal inode.  Don't try
4455ac27a0ecSDave Kleikamp 	 * root first: it may be modified in the journal!
4456ac27a0ecSDave Kleikamp 	 */
4457e2b911c5SDarrick J. Wong 	if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
44584753d8a2STheodore Ts'o 		err = ext4_load_journal(sb, es, journal_devnum);
44594753d8a2STheodore Ts'o 		if (err)
446050460fe8SDarrick J. Wong 			goto failed_mount3a;
4461bc98a42cSDavid Howells 	} else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
4462e2b911c5SDarrick J. Wong 		   ext4_has_feature_journal_needs_recovery(sb)) {
4463b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4464b31e1552SEric Sandeen 		       "suppressed and not mounted read-only");
4465744692dcSJiaying Zhang 		goto failed_mount_wq;
4466ac27a0ecSDave Kleikamp 	} else {
44671e381f60SDmitry Monakhov 		/* Nojournal mode, all journal mount options are illegal */
44681e381f60SDmitry Monakhov 		if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
44691e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44701e381f60SDmitry Monakhov 				 "journal_checksum, fs mounted w/o journal");
44711e381f60SDmitry Monakhov 			goto failed_mount_wq;
44721e381f60SDmitry Monakhov 		}
44731e381f60SDmitry Monakhov 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
44741e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44751e381f60SDmitry Monakhov 				 "journal_async_commit, fs mounted w/o journal");
44761e381f60SDmitry Monakhov 			goto failed_mount_wq;
44771e381f60SDmitry Monakhov 		}
44781e381f60SDmitry Monakhov 		if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
44791e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44801e381f60SDmitry Monakhov 				 "commit=%lu, fs mounted w/o journal",
44811e381f60SDmitry Monakhov 				 sbi->s_commit_interval / HZ);
44821e381f60SDmitry Monakhov 			goto failed_mount_wq;
44831e381f60SDmitry Monakhov 		}
44841e381f60SDmitry Monakhov 		if (EXT4_MOUNT_DATA_FLAGS &
44851e381f60SDmitry Monakhov 		    (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
44861e381f60SDmitry Monakhov 			ext4_msg(sb, KERN_ERR, "can't mount with "
44871e381f60SDmitry Monakhov 				 "data=, fs mounted w/o journal");
44881e381f60SDmitry Monakhov 			goto failed_mount_wq;
44891e381f60SDmitry Monakhov 		}
449050b29d8fSDebabrata Banerjee 		sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
44911e381f60SDmitry Monakhov 		clear_opt(sb, JOURNAL_CHECKSUM);
4492fd8c37ecSTheodore Ts'o 		clear_opt(sb, DATA_FLAGS);
44930390131bSFrank Mayhar 		sbi->s_journal = NULL;
44940390131bSFrank Mayhar 		needs_recovery = 0;
44950390131bSFrank Mayhar 		goto no_journal;
4496ac27a0ecSDave Kleikamp 	}
4497ac27a0ecSDave Kleikamp 
4498e2b911c5SDarrick J. Wong 	if (ext4_has_feature_64bit(sb) &&
4499eb40a09cSJose R. Santos 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4500eb40a09cSJose R. Santos 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4501b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4502744692dcSJiaying Zhang 		goto failed_mount_wq;
4503eb40a09cSJose R. Santos 	}
4504eb40a09cSJose R. Santos 
450525ed6e8aSDarrick J. Wong 	if (!set_journal_csum_feature_set(sb)) {
450625ed6e8aSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
450725ed6e8aSDarrick J. Wong 			 "feature set");
450825ed6e8aSDarrick J. Wong 		goto failed_mount_wq;
4509d4da6c9cSLinus Torvalds 	}
4510818d276cSGirish Shilamkar 
4511ac27a0ecSDave Kleikamp 	/* We have now updated the journal if required, so we can
4512ac27a0ecSDave Kleikamp 	 * validate the data journaling mode. */
4513ac27a0ecSDave Kleikamp 	switch (test_opt(sb, DATA_FLAGS)) {
4514ac27a0ecSDave Kleikamp 	case 0:
4515ac27a0ecSDave Kleikamp 		/* No mode set, assume a default based on the journal
451663f57933SAndrew Morton 		 * capabilities: ORDERED_DATA if the journal can
451763f57933SAndrew Morton 		 * cope, else JOURNAL_DATA
451863f57933SAndrew Morton 		 */
4519dab291afSMingming Cao 		if (jbd2_journal_check_available_features
452027f394a7STyson Nottingham 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4521fd8c37ecSTheodore Ts'o 			set_opt(sb, ORDERED_DATA);
452227f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
452327f394a7STyson Nottingham 		} else {
4524fd8c37ecSTheodore Ts'o 			set_opt(sb, JOURNAL_DATA);
452527f394a7STyson Nottingham 			sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
452627f394a7STyson Nottingham 		}
4527ac27a0ecSDave Kleikamp 		break;
4528ac27a0ecSDave Kleikamp 
4529617ba13bSMingming Cao 	case EXT4_MOUNT_ORDERED_DATA:
4530617ba13bSMingming Cao 	case EXT4_MOUNT_WRITEBACK_DATA:
4531dab291afSMingming Cao 		if (!jbd2_journal_check_available_features
4532dab291afSMingming Cao 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4533b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4534b31e1552SEric Sandeen 			       "requested data journaling mode");
4535744692dcSJiaying Zhang 			goto failed_mount_wq;
4536ac27a0ecSDave Kleikamp 		}
4537ac27a0ecSDave Kleikamp 	default:
4538ac27a0ecSDave Kleikamp 		break;
4539ac27a0ecSDave Kleikamp 	}
4540ab04df78SJan Kara 
4541ab04df78SJan Kara 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4542ab04df78SJan Kara 	    test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4543ab04df78SJan Kara 		ext4_msg(sb, KERN_ERR, "can't mount with "
4544ab04df78SJan Kara 			"journal_async_commit in data=ordered mode");
4545ab04df78SJan Kara 		goto failed_mount_wq;
4546ab04df78SJan Kara 	}
4547ab04df78SJan Kara 
4548b3881f74STheodore Ts'o 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4549ac27a0ecSDave Kleikamp 
455018aadd47SBobi Jam 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
455118aadd47SBobi Jam 
4552ce7e010aSTheodore Ts'o no_journal:
4553cdb7ee4cSTahsin Erdogan 	if (!test_opt(sb, NO_MBCACHE)) {
455447387409STahsin Erdogan 		sbi->s_ea_block_cache = ext4_xattr_create_cache();
455547387409STahsin Erdogan 		if (!sbi->s_ea_block_cache) {
4556cdb7ee4cSTahsin Erdogan 			ext4_msg(sb, KERN_ERR,
4557cdb7ee4cSTahsin Erdogan 				 "Failed to create ea_block_cache");
45589c191f70ST Makphaibulchoke 			goto failed_mount_wq;
45599c191f70ST Makphaibulchoke 		}
45609c191f70ST Makphaibulchoke 
4561dec214d0STahsin Erdogan 		if (ext4_has_feature_ea_inode(sb)) {
4562dec214d0STahsin Erdogan 			sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4563dec214d0STahsin Erdogan 			if (!sbi->s_ea_inode_cache) {
4564dec214d0STahsin Erdogan 				ext4_msg(sb, KERN_ERR,
4565dec214d0STahsin Erdogan 					 "Failed to create ea_inode_cache");
4566dec214d0STahsin Erdogan 				goto failed_mount_wq;
4567dec214d0STahsin Erdogan 			}
4568dec214d0STahsin Erdogan 		}
4569cdb7ee4cSTahsin Erdogan 	}
4570dec214d0STahsin Erdogan 
4571c93d8f88SEric Biggers 	if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4572c93d8f88SEric Biggers 		ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4573c93d8f88SEric Biggers 		goto failed_mount_wq;
4574c93d8f88SEric Biggers 	}
4575c93d8f88SEric Biggers 
4576bc98a42cSDavid Howells 	if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
4577e2b911c5SDarrick J. Wong 	    !ext4_has_feature_encrypt(sb)) {
4578e2b911c5SDarrick J. Wong 		ext4_set_feature_encrypt(sb);
45796ddb2447STheodore Ts'o 		ext4_commit_super(sb, 1);
45806ddb2447STheodore Ts'o 	}
45816ddb2447STheodore Ts'o 
4582fd89d5f2STejun Heo 	/*
4583952fc18eSTheodore Ts'o 	 * Get the # of file system overhead blocks from the
4584952fc18eSTheodore Ts'o 	 * superblock if present.
4585952fc18eSTheodore Ts'o 	 */
4586952fc18eSTheodore Ts'o 	if (es->s_overhead_clusters)
4587952fc18eSTheodore Ts'o 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4588952fc18eSTheodore Ts'o 	else {
458907aa2ea1SLukas Czerner 		err = ext4_calculate_overhead(sb);
459007aa2ea1SLukas Czerner 		if (err)
4591952fc18eSTheodore Ts'o 			goto failed_mount_wq;
4592952fc18eSTheodore Ts'o 	}
4593952fc18eSTheodore Ts'o 
4594952fc18eSTheodore Ts'o 	/*
4595fd89d5f2STejun Heo 	 * The maximum number of concurrent works can be high and
4596fd89d5f2STejun Heo 	 * concurrency isn't really necessary.  Limit it to 1.
4597fd89d5f2STejun Heo 	 */
45982e8fa54eSJan Kara 	EXT4_SB(sb)->rsv_conversion_wq =
45992e8fa54eSJan Kara 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
46002e8fa54eSJan Kara 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
46012e8fa54eSJan Kara 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
460207aa2ea1SLukas Czerner 		ret = -ENOMEM;
46032e8fa54eSJan Kara 		goto failed_mount4;
46042e8fa54eSJan Kara 	}
46052e8fa54eSJan Kara 
4606ac27a0ecSDave Kleikamp 	/*
4607dab291afSMingming Cao 	 * The jbd2_journal_load will have done any necessary log recovery,
4608ac27a0ecSDave Kleikamp 	 * so we can safely mount the rest of the filesystem now.
4609ac27a0ecSDave Kleikamp 	 */
4610ac27a0ecSDave Kleikamp 
46118a363970STheodore Ts'o 	root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
46121d1fe1eeSDavid Howells 	if (IS_ERR(root)) {
4613b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root inode failed");
46141d1fe1eeSDavid Howells 		ret = PTR_ERR(root);
461532a9bb57SManish Katiyar 		root = NULL;
4616ac27a0ecSDave Kleikamp 		goto failed_mount4;
4617ac27a0ecSDave Kleikamp 	}
4618ac27a0ecSDave Kleikamp 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4619b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
462094bf608aSAl Viro 		iput(root);
4621ac27a0ecSDave Kleikamp 		goto failed_mount4;
4622ac27a0ecSDave Kleikamp 	}
4623b886ee3eSGabriel Krisman Bertazi 
4624b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4625b886ee3eSGabriel Krisman Bertazi 	if (sbi->s_encoding)
4626b886ee3eSGabriel Krisman Bertazi 		sb->s_d_op = &ext4_dentry_ops;
4627b886ee3eSGabriel Krisman Bertazi #endif
4628b886ee3eSGabriel Krisman Bertazi 
462948fde701SAl Viro 	sb->s_root = d_make_root(root);
46301d1fe1eeSDavid Howells 	if (!sb->s_root) {
4631b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
46321d1fe1eeSDavid Howells 		ret = -ENOMEM;
46331d1fe1eeSDavid Howells 		goto failed_mount4;
46341d1fe1eeSDavid Howells 	}
4635ac27a0ecSDave Kleikamp 
4636c89128a0SJaegeuk Kim 	ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4637c89128a0SJaegeuk Kim 	if (ret == -EROFS) {
46381751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4639c89128a0SJaegeuk Kim 		ret = 0;
4640c89128a0SJaegeuk Kim 	} else if (ret)
4641c89128a0SJaegeuk Kim 		goto failed_mount4a;
4642ef7f3835SKalpak Shah 
4643b5799018STheodore Ts'o 	ext4_set_resv_clusters(sb);
464427dd4385SLukas Czerner 
46456fd058f7STheodore Ts'o 	err = ext4_setup_system_zone(sb);
46466fd058f7STheodore Ts'o 	if (err) {
4647b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4648fbe845ddSCurt Wohlgemuth 			 "zone (%d)", err);
4649f9ae9cf5STheodore Ts'o 		goto failed_mount4a;
4650f9ae9cf5STheodore Ts'o 	}
4651f9ae9cf5STheodore Ts'o 
4652f9ae9cf5STheodore Ts'o 	ext4_ext_init(sb);
4653f9ae9cf5STheodore Ts'o 	err = ext4_mb_init(sb);
4654f9ae9cf5STheodore Ts'o 	if (err) {
4655f9ae9cf5STheodore Ts'o 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4656f9ae9cf5STheodore Ts'o 			 err);
4657dcf2d804STao Ma 		goto failed_mount5;
4658c2774d84SAneesh Kumar K.V 	}
4659c2774d84SAneesh Kumar K.V 
4660d5e03cbbSTheodore Ts'o 	block = ext4_count_free_clusters(sb);
4661d5e03cbbSTheodore Ts'o 	ext4_free_blocks_count_set(sbi->s_es,
4662d5e03cbbSTheodore Ts'o 				   EXT4_C2B(sbi, block));
46634274f516STheodore Ts'o 	ext4_superblock_csum_set(sb);
4664908c7f19STejun Heo 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4665908c7f19STejun Heo 				  GFP_KERNEL);
4666d5e03cbbSTheodore Ts'o 	if (!err) {
4667d5e03cbbSTheodore Ts'o 		unsigned long freei = ext4_count_free_inodes(sb);
4668d5e03cbbSTheodore Ts'o 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
46694274f516STheodore Ts'o 		ext4_superblock_csum_set(sb);
4670908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4671908c7f19STejun Heo 					  GFP_KERNEL);
4672d5e03cbbSTheodore Ts'o 	}
4673d5e03cbbSTheodore Ts'o 	if (!err)
4674d5e03cbbSTheodore Ts'o 		err = percpu_counter_init(&sbi->s_dirs_counter,
4675908c7f19STejun Heo 					  ext4_count_dirs(sb), GFP_KERNEL);
4676d5e03cbbSTheodore Ts'o 	if (!err)
4677908c7f19STejun Heo 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4678908c7f19STejun Heo 					  GFP_KERNEL);
4679c8585c6fSDaeho Jeong 	if (!err)
4680bbd55937SEric Biggers 		err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
4681c8585c6fSDaeho Jeong 
4682d5e03cbbSTheodore Ts'o 	if (err) {
4683d5e03cbbSTheodore Ts'o 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4684d5e03cbbSTheodore Ts'o 		goto failed_mount6;
4685d5e03cbbSTheodore Ts'o 	}
4686d5e03cbbSTheodore Ts'o 
4687e2b911c5SDarrick J. Wong 	if (ext4_has_feature_flex_bg(sb))
4688d5e03cbbSTheodore Ts'o 		if (!ext4_fill_flex_info(sb)) {
4689d5e03cbbSTheodore Ts'o 			ext4_msg(sb, KERN_ERR,
4690d5e03cbbSTheodore Ts'o 			       "unable to initialize "
4691d5e03cbbSTheodore Ts'o 			       "flex_bg meta info!");
4692d5e03cbbSTheodore Ts'o 			goto failed_mount6;
4693d5e03cbbSTheodore Ts'o 		}
4694d5e03cbbSTheodore Ts'o 
4695bfff6873SLukas Czerner 	err = ext4_register_li_request(sb, first_not_zeroed);
4696bfff6873SLukas Czerner 	if (err)
4697dcf2d804STao Ma 		goto failed_mount6;
4698bfff6873SLukas Czerner 
4699b5799018STheodore Ts'o 	err = ext4_register_sysfs(sb);
4700dcf2d804STao Ma 	if (err)
4701dcf2d804STao Ma 		goto failed_mount7;
47023197ebdbSTheodore Ts'o 
47039b2ff357SJan Kara #ifdef CONFIG_QUOTA
47049b2ff357SJan Kara 	/* Enable quota usage during mount. */
4705bc98a42cSDavid Howells 	if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
47069b2ff357SJan Kara 		err = ext4_enable_quotas(sb);
47079b2ff357SJan Kara 		if (err)
47089b2ff357SJan Kara 			goto failed_mount8;
47099b2ff357SJan Kara 	}
47109b2ff357SJan Kara #endif  /* CONFIG_QUOTA */
47119b2ff357SJan Kara 
4712617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4713617ba13bSMingming Cao 	ext4_orphan_cleanup(sb, es);
4714617ba13bSMingming Cao 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
47150390131bSFrank Mayhar 	if (needs_recovery) {
4716b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "recovery complete");
4717617ba13bSMingming Cao 		ext4_mark_recovery_complete(sb, es);
47180390131bSFrank Mayhar 	}
47190390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal) {
47200390131bSFrank Mayhar 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
47210390131bSFrank Mayhar 			descr = " journalled data mode";
47220390131bSFrank Mayhar 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
47230390131bSFrank Mayhar 			descr = " ordered data mode";
47240390131bSFrank Mayhar 		else
47250390131bSFrank Mayhar 			descr = " writeback data mode";
47260390131bSFrank Mayhar 	} else
47270390131bSFrank Mayhar 		descr = "out journal";
47280390131bSFrank Mayhar 
472979add3a3SLukas Czerner 	if (test_opt(sb, DISCARD)) {
473079add3a3SLukas Czerner 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
473179add3a3SLukas Czerner 		if (!blk_queue_discard(q))
473279add3a3SLukas Czerner 			ext4_msg(sb, KERN_WARNING,
473379add3a3SLukas Czerner 				 "mounting with \"discard\" option, but "
473479add3a3SLukas Czerner 				 "the device does not support discard");
473579add3a3SLukas Czerner 	}
473679add3a3SLukas Czerner 
4737e294a537STheodore Ts'o 	if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4738d4c402d9SCurt Wohlgemuth 		ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
47395aee0f8aSTheodore Ts'o 			 "Opts: %.*s%s%s", descr,
47405aee0f8aSTheodore Ts'o 			 (int) sizeof(sbi->s_es->s_mount_opts),
47415aee0f8aSTheodore Ts'o 			 sbi->s_es->s_mount_opts,
47428b67f04aSTheodore Ts'o 			 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4743ac27a0ecSDave Kleikamp 
474466e61a9eSTheodore Ts'o 	if (es->s_error_count)
474566e61a9eSTheodore Ts'o 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4746ac27a0ecSDave Kleikamp 
4747efbed4dcSTheodore Ts'o 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4748efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4749efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4750efbed4dcSTheodore Ts'o 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4751efbed4dcSTheodore Ts'o 
4752d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
4753ac27a0ecSDave Kleikamp 	return 0;
4754ac27a0ecSDave Kleikamp 
4755617ba13bSMingming Cao cantfind_ext4:
4756ac27a0ecSDave Kleikamp 	if (!silent)
4757b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4758ac27a0ecSDave Kleikamp 	goto failed_mount;
4759ac27a0ecSDave Kleikamp 
476072ba7450STheodore Ts'o #ifdef CONFIG_QUOTA
476172ba7450STheodore Ts'o failed_mount8:
4762ebd173beSTheodore Ts'o 	ext4_unregister_sysfs(sb);
476372ba7450STheodore Ts'o #endif
4764dcf2d804STao Ma failed_mount7:
4765dcf2d804STao Ma 	ext4_unregister_li_request(sb);
4766dcf2d804STao Ma failed_mount6:
4767f9ae9cf5STheodore Ts'o 	ext4_mb_release(sb);
47687c990728SSuraj Jitindar Singh 	rcu_read_lock();
47697c990728SSuraj Jitindar Singh 	flex_groups = rcu_dereference(sbi->s_flex_groups);
47707c990728SSuraj Jitindar Singh 	if (flex_groups) {
47717c990728SSuraj Jitindar Singh 		for (i = 0; i < sbi->s_flex_groups_allocated; i++)
47727c990728SSuraj Jitindar Singh 			kvfree(flex_groups[i]);
47737c990728SSuraj Jitindar Singh 		kvfree(flex_groups);
47747c990728SSuraj Jitindar Singh 	}
47757c990728SSuraj Jitindar Singh 	rcu_read_unlock();
4776d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4777d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4778d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirs_counter);
4779d5e03cbbSTheodore Ts'o 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4780bbd55937SEric Biggers 	percpu_free_rwsem(&sbi->s_writepages_rwsem);
478100764937SAzat Khuzhin failed_mount5:
4782f9ae9cf5STheodore Ts'o 	ext4_ext_release(sb);
4783f9ae9cf5STheodore Ts'o 	ext4_release_system_zone(sb);
4784f9ae9cf5STheodore Ts'o failed_mount4a:
478594bf608aSAl Viro 	dput(sb->s_root);
478632a9bb57SManish Katiyar 	sb->s_root = NULL;
478794bf608aSAl Viro failed_mount4:
4788b31e1552SEric Sandeen 	ext4_msg(sb, KERN_ERR, "mount failed");
47892e8fa54eSJan Kara 	if (EXT4_SB(sb)->rsv_conversion_wq)
47902e8fa54eSJan Kara 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
47914c0425ffSMingming Cao failed_mount_wq:
4792dec214d0STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4793dec214d0STahsin Erdogan 	sbi->s_ea_inode_cache = NULL;
479450c15df6SChengguang Xu 
479547387409STahsin Erdogan 	ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
479647387409STahsin Erdogan 	sbi->s_ea_block_cache = NULL;
479750c15df6SChengguang Xu 
47980390131bSFrank Mayhar 	if (sbi->s_journal) {
4799dab291afSMingming Cao 		jbd2_journal_destroy(sbi->s_journal);
480047b4a50bSJan Kara 		sbi->s_journal = NULL;
48010390131bSFrank Mayhar 	}
480250460fe8SDarrick J. Wong failed_mount3a:
4803d3922a77SZheng Liu 	ext4_es_unregister_shrinker(sbi);
4804eb68d0e2SZheng Liu failed_mount3:
48059105bb14SAl Viro 	del_timer_sync(&sbi->s_err_report);
4806c5e06d10SJohann Lombardi 	if (sbi->s_mmp_tsk)
4807c5e06d10SJohann Lombardi 		kthread_stop(sbi->s_mmp_tsk);
4808ac27a0ecSDave Kleikamp failed_mount2:
48091d0c3924STheodore Ts'o 	rcu_read_lock();
48101d0c3924STheodore Ts'o 	group_desc = rcu_dereference(sbi->s_group_desc);
4811ac27a0ecSDave Kleikamp 	for (i = 0; i < db_count; i++)
48121d0c3924STheodore Ts'o 		brelse(group_desc[i]);
48131d0c3924STheodore Ts'o 	kvfree(group_desc);
48141d0c3924STheodore Ts'o 	rcu_read_unlock();
4815ac27a0ecSDave Kleikamp failed_mount:
48160441984aSDarrick J. Wong 	if (sbi->s_chksum_driver)
48170441984aSDarrick J. Wong 		crypto_free_shash(sbi->s_chksum_driver);
4818c83ad55eSGabriel Krisman Bertazi 
4819c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
4820c83ad55eSGabriel Krisman Bertazi 	utf8_unload(sbi->s_encoding);
4821c83ad55eSGabriel Krisman Bertazi #endif
4822c83ad55eSGabriel Krisman Bertazi 
4823ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
4824a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
48250ba33facSTheodore Ts'o 		kfree(get_qf_name(sb, sbi, i));
4826ac27a0ecSDave Kleikamp #endif
4827617ba13bSMingming Cao 	ext4_blkdev_remove(sbi);
4828ac27a0ecSDave Kleikamp 	brelse(bh);
4829ac27a0ecSDave Kleikamp out_fail:
4830ac27a0ecSDave Kleikamp 	sb->s_fs_info = NULL;
4831f6830165SManish Katiyar 	kfree(sbi->s_blockgroup_lock);
48325aee0f8aSTheodore Ts'o out_free_base:
4833ac27a0ecSDave Kleikamp 	kfree(sbi);
4834d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
48355e405595SDan Williams 	fs_put_dax(dax_dev);
483607aa2ea1SLukas Czerner 	return err ? err : ret;
4837ac27a0ecSDave Kleikamp }
4838ac27a0ecSDave Kleikamp 
4839ac27a0ecSDave Kleikamp /*
4840ac27a0ecSDave Kleikamp  * Setup any per-fs journal parameters now.  We'll do this both on
4841ac27a0ecSDave Kleikamp  * initial mount, once the journal has been initialised but before we've
4842ac27a0ecSDave Kleikamp  * done any recovery; and again on any subsequent remount.
4843ac27a0ecSDave Kleikamp  */
4844617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4845ac27a0ecSDave Kleikamp {
4846617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4847ac27a0ecSDave Kleikamp 
4848ac27a0ecSDave Kleikamp 	journal->j_commit_interval = sbi->s_commit_interval;
484930773840STheodore Ts'o 	journal->j_min_batch_time = sbi->s_min_batch_time;
485030773840STheodore Ts'o 	journal->j_max_batch_time = sbi->s_max_batch_time;
4851ac27a0ecSDave Kleikamp 
4852a931da6aSTheodore Ts'o 	write_lock(&journal->j_state_lock);
4853ac27a0ecSDave Kleikamp 	if (test_opt(sb, BARRIER))
4854dab291afSMingming Cao 		journal->j_flags |= JBD2_BARRIER;
4855ac27a0ecSDave Kleikamp 	else
4856dab291afSMingming Cao 		journal->j_flags &= ~JBD2_BARRIER;
48575bf5683aSHidehiro Kawai 	if (test_opt(sb, DATA_ERR_ABORT))
48585bf5683aSHidehiro Kawai 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
48595bf5683aSHidehiro Kawai 	else
48605bf5683aSHidehiro Kawai 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4861a931da6aSTheodore Ts'o 	write_unlock(&journal->j_state_lock);
4862ac27a0ecSDave Kleikamp }
4863ac27a0ecSDave Kleikamp 
4864c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb,
4865ac27a0ecSDave Kleikamp 					     unsigned int journal_inum)
4866ac27a0ecSDave Kleikamp {
4867ac27a0ecSDave Kleikamp 	struct inode *journal_inode;
4868ac27a0ecSDave Kleikamp 
4869c6cb7e77SEric Whitney 	/*
4870c6cb7e77SEric Whitney 	 * Test for the existence of a valid inode on disk.  Bad things
4871c6cb7e77SEric Whitney 	 * happen if we iget() an unused inode, as the subsequent iput()
4872c6cb7e77SEric Whitney 	 * will try to delete it.
4873c6cb7e77SEric Whitney 	 */
48748a363970STheodore Ts'o 	journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
48751d1fe1eeSDavid Howells 	if (IS_ERR(journal_inode)) {
4876b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "no journal found");
4877ac27a0ecSDave Kleikamp 		return NULL;
4878ac27a0ecSDave Kleikamp 	}
4879ac27a0ecSDave Kleikamp 	if (!journal_inode->i_nlink) {
4880ac27a0ecSDave Kleikamp 		make_bad_inode(journal_inode);
4881ac27a0ecSDave Kleikamp 		iput(journal_inode);
4882b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4883ac27a0ecSDave Kleikamp 		return NULL;
4884ac27a0ecSDave Kleikamp 	}
4885ac27a0ecSDave Kleikamp 
4886e5f8eab8STheodore Ts'o 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4887ac27a0ecSDave Kleikamp 		  journal_inode, journal_inode->i_size);
48881d1fe1eeSDavid Howells 	if (!S_ISREG(journal_inode->i_mode)) {
4889b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4890ac27a0ecSDave Kleikamp 		iput(journal_inode);
4891ac27a0ecSDave Kleikamp 		return NULL;
4892ac27a0ecSDave Kleikamp 	}
4893c6cb7e77SEric Whitney 	return journal_inode;
4894c6cb7e77SEric Whitney }
4895c6cb7e77SEric Whitney 
4896c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb,
4897c6cb7e77SEric Whitney 				   unsigned int journal_inum)
4898c6cb7e77SEric Whitney {
4899c6cb7e77SEric Whitney 	struct inode *journal_inode;
4900c6cb7e77SEric Whitney 	journal_t *journal;
4901c6cb7e77SEric Whitney 
4902c6cb7e77SEric Whitney 	BUG_ON(!ext4_has_feature_journal(sb));
4903c6cb7e77SEric Whitney 
4904c6cb7e77SEric Whitney 	journal_inode = ext4_get_journal_inode(sb, journal_inum);
4905c6cb7e77SEric Whitney 	if (!journal_inode)
4906c6cb7e77SEric Whitney 		return NULL;
4907ac27a0ecSDave Kleikamp 
4908dab291afSMingming Cao 	journal = jbd2_journal_init_inode(journal_inode);
4909ac27a0ecSDave Kleikamp 	if (!journal) {
4910b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4911ac27a0ecSDave Kleikamp 		iput(journal_inode);
4912ac27a0ecSDave Kleikamp 		return NULL;
4913ac27a0ecSDave Kleikamp 	}
4914ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4915617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
4916ac27a0ecSDave Kleikamp 	return journal;
4917ac27a0ecSDave Kleikamp }
4918ac27a0ecSDave Kleikamp 
4919617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb,
4920ac27a0ecSDave Kleikamp 				       dev_t j_dev)
4921ac27a0ecSDave Kleikamp {
4922ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
4923ac27a0ecSDave Kleikamp 	journal_t *journal;
4924617ba13bSMingming Cao 	ext4_fsblk_t start;
4925617ba13bSMingming Cao 	ext4_fsblk_t len;
4926ac27a0ecSDave Kleikamp 	int hblock, blocksize;
4927617ba13bSMingming Cao 	ext4_fsblk_t sb_block;
4928ac27a0ecSDave Kleikamp 	unsigned long offset;
4929617ba13bSMingming Cao 	struct ext4_super_block *es;
4930ac27a0ecSDave Kleikamp 	struct block_device *bdev;
4931ac27a0ecSDave Kleikamp 
4932e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
49330390131bSFrank Mayhar 
4934b31e1552SEric Sandeen 	bdev = ext4_blkdev_get(j_dev, sb);
4935ac27a0ecSDave Kleikamp 	if (bdev == NULL)
4936ac27a0ecSDave Kleikamp 		return NULL;
4937ac27a0ecSDave Kleikamp 
4938ac27a0ecSDave Kleikamp 	blocksize = sb->s_blocksize;
4939e1defc4fSMartin K. Petersen 	hblock = bdev_logical_block_size(bdev);
4940ac27a0ecSDave Kleikamp 	if (blocksize < hblock) {
4941b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR,
4942b31e1552SEric Sandeen 			"blocksize too small for journal device");
4943ac27a0ecSDave Kleikamp 		goto out_bdev;
4944ac27a0ecSDave Kleikamp 	}
4945ac27a0ecSDave Kleikamp 
4946617ba13bSMingming Cao 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4947617ba13bSMingming Cao 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4948ac27a0ecSDave Kleikamp 	set_blocksize(bdev, blocksize);
4949ac27a0ecSDave Kleikamp 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4950b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4951b31e1552SEric Sandeen 		       "external journal");
4952ac27a0ecSDave Kleikamp 		goto out_bdev;
4953ac27a0ecSDave Kleikamp 	}
4954ac27a0ecSDave Kleikamp 
49552716b802STheodore Ts'o 	es = (struct ext4_super_block *) (bh->b_data + offset);
4956617ba13bSMingming Cao 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4957ac27a0ecSDave Kleikamp 	    !(le32_to_cpu(es->s_feature_incompat) &
4958617ba13bSMingming Cao 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4959b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "external journal has "
4960b31e1552SEric Sandeen 					"bad superblock");
4961ac27a0ecSDave Kleikamp 		brelse(bh);
4962ac27a0ecSDave Kleikamp 		goto out_bdev;
4963ac27a0ecSDave Kleikamp 	}
4964ac27a0ecSDave Kleikamp 
4965df4763beSDarrick J. Wong 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4966df4763beSDarrick J. Wong 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4967df4763beSDarrick J. Wong 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4968df4763beSDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "external journal has "
4969df4763beSDarrick J. Wong 				       "corrupt superblock");
4970df4763beSDarrick J. Wong 		brelse(bh);
4971df4763beSDarrick J. Wong 		goto out_bdev;
4972df4763beSDarrick J. Wong 	}
4973df4763beSDarrick J. Wong 
4974617ba13bSMingming Cao 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4975b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4976ac27a0ecSDave Kleikamp 		brelse(bh);
4977ac27a0ecSDave Kleikamp 		goto out_bdev;
4978ac27a0ecSDave Kleikamp 	}
4979ac27a0ecSDave Kleikamp 
4980bd81d8eeSLaurent Vivier 	len = ext4_blocks_count(es);
4981ac27a0ecSDave Kleikamp 	start = sb_block + 1;
4982ac27a0ecSDave Kleikamp 	brelse(bh);	/* we're done with the superblock */
4983ac27a0ecSDave Kleikamp 
4984dab291afSMingming Cao 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4985ac27a0ecSDave Kleikamp 					start, len, blocksize);
4986ac27a0ecSDave Kleikamp 	if (!journal) {
4987b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4988ac27a0ecSDave Kleikamp 		goto out_bdev;
4989ac27a0ecSDave Kleikamp 	}
4990ac27a0ecSDave Kleikamp 	journal->j_private = sb;
4991dfec8a14SMike Christie 	ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4992ac27a0ecSDave Kleikamp 	wait_on_buffer(journal->j_sb_buffer);
4993ac27a0ecSDave Kleikamp 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4994b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4995ac27a0ecSDave Kleikamp 		goto out_journal;
4996ac27a0ecSDave Kleikamp 	}
4997ac27a0ecSDave Kleikamp 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4998b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4999b31e1552SEric Sandeen 					"user (unsupported) - %d",
5000ac27a0ecSDave Kleikamp 			be32_to_cpu(journal->j_superblock->s_nr_users));
5001ac27a0ecSDave Kleikamp 		goto out_journal;
5002ac27a0ecSDave Kleikamp 	}
5003617ba13bSMingming Cao 	EXT4_SB(sb)->journal_bdev = bdev;
5004617ba13bSMingming Cao 	ext4_init_journal_params(sb, journal);
5005ac27a0ecSDave Kleikamp 	return journal;
50060b8e58a1SAndreas Dilger 
5007ac27a0ecSDave Kleikamp out_journal:
5008dab291afSMingming Cao 	jbd2_journal_destroy(journal);
5009ac27a0ecSDave Kleikamp out_bdev:
5010617ba13bSMingming Cao 	ext4_blkdev_put(bdev);
5011ac27a0ecSDave Kleikamp 	return NULL;
5012ac27a0ecSDave Kleikamp }
5013ac27a0ecSDave Kleikamp 
5014617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb,
5015617ba13bSMingming Cao 			     struct ext4_super_block *es,
5016ac27a0ecSDave Kleikamp 			     unsigned long journal_devnum)
5017ac27a0ecSDave Kleikamp {
5018ac27a0ecSDave Kleikamp 	journal_t *journal;
5019ac27a0ecSDave Kleikamp 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5020ac27a0ecSDave Kleikamp 	dev_t journal_dev;
5021ac27a0ecSDave Kleikamp 	int err = 0;
5022ac27a0ecSDave Kleikamp 	int really_read_only;
5023ac27a0ecSDave Kleikamp 
5024e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
50250390131bSFrank Mayhar 
5026ac27a0ecSDave Kleikamp 	if (journal_devnum &&
5027ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5028b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5029b31e1552SEric Sandeen 			"numbers have changed");
5030ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(journal_devnum);
5031ac27a0ecSDave Kleikamp 	} else
5032ac27a0ecSDave Kleikamp 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5033ac27a0ecSDave Kleikamp 
5034ac27a0ecSDave Kleikamp 	really_read_only = bdev_read_only(sb->s_bdev);
5035ac27a0ecSDave Kleikamp 
5036ac27a0ecSDave Kleikamp 	/*
5037ac27a0ecSDave Kleikamp 	 * Are we loading a blank journal or performing recovery after a
5038ac27a0ecSDave Kleikamp 	 * crash?  For recovery, we need to check in advance whether we
5039ac27a0ecSDave Kleikamp 	 * can get read-write access to the device.
5040ac27a0ecSDave Kleikamp 	 */
5041e2b911c5SDarrick J. Wong 	if (ext4_has_feature_journal_needs_recovery(sb)) {
5042bc98a42cSDavid Howells 		if (sb_rdonly(sb)) {
5043b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
5044b31e1552SEric Sandeen 					"required on readonly filesystem");
5045ac27a0ecSDave Kleikamp 			if (really_read_only) {
5046b31e1552SEric Sandeen 				ext4_msg(sb, KERN_ERR, "write access "
5047d98bf8cdSSimon Ruderich 					"unavailable, cannot proceed "
5048d98bf8cdSSimon Ruderich 					"(try mounting with noload)");
5049ac27a0ecSDave Kleikamp 				return -EROFS;
5050ac27a0ecSDave Kleikamp 			}
5051b31e1552SEric Sandeen 			ext4_msg(sb, KERN_INFO, "write access will "
5052b31e1552SEric Sandeen 			       "be enabled during recovery");
5053ac27a0ecSDave Kleikamp 		}
5054ac27a0ecSDave Kleikamp 	}
5055ac27a0ecSDave Kleikamp 
5056ac27a0ecSDave Kleikamp 	if (journal_inum && journal_dev) {
5057b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
5058b31e1552SEric Sandeen 		       "and inode journals!");
5059ac27a0ecSDave Kleikamp 		return -EINVAL;
5060ac27a0ecSDave Kleikamp 	}
5061ac27a0ecSDave Kleikamp 
5062ac27a0ecSDave Kleikamp 	if (journal_inum) {
5063617ba13bSMingming Cao 		if (!(journal = ext4_get_journal(sb, journal_inum)))
5064ac27a0ecSDave Kleikamp 			return -EINVAL;
5065ac27a0ecSDave Kleikamp 	} else {
5066617ba13bSMingming Cao 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
5067ac27a0ecSDave Kleikamp 			return -EINVAL;
5068ac27a0ecSDave Kleikamp 	}
5069ac27a0ecSDave Kleikamp 
507090576c0bSTheodore Ts'o 	if (!(journal->j_flags & JBD2_BARRIER))
5071b31e1552SEric Sandeen 		ext4_msg(sb, KERN_INFO, "barriers disabled");
50724776004fSTheodore Ts'o 
5073e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal_needs_recovery(sb))
5074dab291afSMingming Cao 		err = jbd2_journal_wipe(journal, !really_read_only);
50751c13d5c0STheodore Ts'o 	if (!err) {
50761c13d5c0STheodore Ts'o 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
50771c13d5c0STheodore Ts'o 		if (save)
50781c13d5c0STheodore Ts'o 			memcpy(save, ((char *) es) +
50791c13d5c0STheodore Ts'o 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
5080dab291afSMingming Cao 		err = jbd2_journal_load(journal);
50811c13d5c0STheodore Ts'o 		if (save)
50821c13d5c0STheodore Ts'o 			memcpy(((char *) es) + EXT4_S_ERR_START,
50831c13d5c0STheodore Ts'o 			       save, EXT4_S_ERR_LEN);
50841c13d5c0STheodore Ts'o 		kfree(save);
50851c13d5c0STheodore Ts'o 	}
5086ac27a0ecSDave Kleikamp 
5087ac27a0ecSDave Kleikamp 	if (err) {
5088b31e1552SEric Sandeen 		ext4_msg(sb, KERN_ERR, "error loading journal");
5089dab291afSMingming Cao 		jbd2_journal_destroy(journal);
5090ac27a0ecSDave Kleikamp 		return err;
5091ac27a0ecSDave Kleikamp 	}
5092ac27a0ecSDave Kleikamp 
5093617ba13bSMingming Cao 	EXT4_SB(sb)->s_journal = journal;
5094617ba13bSMingming Cao 	ext4_clear_journal_err(sb, es);
5095ac27a0ecSDave Kleikamp 
5096c41303ceSMaciej Żenczykowski 	if (!really_read_only && journal_devnum &&
5097ac27a0ecSDave Kleikamp 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5098ac27a0ecSDave Kleikamp 		es->s_journal_dev = cpu_to_le32(journal_devnum);
5099ac27a0ecSDave Kleikamp 
5100ac27a0ecSDave Kleikamp 		/* Make sure we flush the recovery flag to disk. */
5101e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5102ac27a0ecSDave Kleikamp 	}
5103ac27a0ecSDave Kleikamp 
5104ac27a0ecSDave Kleikamp 	return 0;
5105ac27a0ecSDave Kleikamp }
5106ac27a0ecSDave Kleikamp 
5107e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync)
5108ac27a0ecSDave Kleikamp {
5109e2d67052STheodore Ts'o 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
5110617ba13bSMingming Cao 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
5111c4be0c1dSTakashi Sato 	int error = 0;
5112ac27a0ecSDave Kleikamp 
5113bdfe0cbdSTheodore Ts'o 	if (!sbh || block_device_ejected(sb))
5114c4be0c1dSTakashi Sato 		return error;
5115a17712c8SJon Derrick 
5116a17712c8SJon Derrick 	/*
5117a17712c8SJon Derrick 	 * The superblock bh should be mapped, but it might not be if the
5118a17712c8SJon Derrick 	 * device was hot-removed. Not much we can do but fail the I/O.
5119a17712c8SJon Derrick 	 */
5120a17712c8SJon Derrick 	if (!buffer_mapped(sbh))
5121a17712c8SJon Derrick 		return error;
5122a17712c8SJon Derrick 
512371290b36STheodore Ts'o 	/*
512471290b36STheodore Ts'o 	 * If the file system is mounted read-only, don't update the
512571290b36STheodore Ts'o 	 * superblock write time.  This avoids updating the superblock
512671290b36STheodore Ts'o 	 * write time when we are mounting the root file system
512771290b36STheodore Ts'o 	 * read/only but we need to replay the journal; at that point,
512871290b36STheodore Ts'o 	 * for people who are east of GMT and who make their clock
512971290b36STheodore Ts'o 	 * tick in localtime for Windows bug-for-bug compatibility,
513071290b36STheodore Ts'o 	 * the clock is set in the future, and this will cause e2fsck
513171290b36STheodore Ts'o 	 * to complain and force a full file system check.
513271290b36STheodore Ts'o 	 */
51331751e8a6SLinus Torvalds 	if (!(sb->s_flags & SB_RDONLY))
51346a0678a7SArnd Bergmann 		ext4_update_tstamp(es, s_wtime);
5135f613dfcbSTheodore Ts'o 	if (sb->s_bdev->bd_part)
5136afc32f7eSTheodore Ts'o 		es->s_kbytes_written =
5137afc32f7eSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
5138dbae2c55SMichael Callahan 			    ((part_stat_read(sb->s_bdev->bd_part,
5139dbae2c55SMichael Callahan 					     sectors[STAT_WRITE]) -
5140afc32f7eSTheodore Ts'o 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
5141f613dfcbSTheodore Ts'o 	else
5142f613dfcbSTheodore Ts'o 		es->s_kbytes_written =
5143f613dfcbSTheodore Ts'o 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
5144d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
514557042651STheodore Ts'o 		ext4_free_blocks_count_set(es,
514657042651STheodore Ts'o 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
514757042651STheodore Ts'o 				&EXT4_SB(sb)->s_freeclusters_counter)));
5148d5e03cbbSTheodore Ts'o 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
51497f93cff9STheodore Ts'o 		es->s_free_inodes_count =
51507f93cff9STheodore Ts'o 			cpu_to_le32(percpu_counter_sum_positive(
51515d1b1b3fSAneesh Kumar K.V 				&EXT4_SB(sb)->s_freeinodes_counter));
5152ac27a0ecSDave Kleikamp 	BUFFER_TRACE(sbh, "marking dirty");
515306db49e6STheodore Ts'o 	ext4_superblock_csum_set(sb);
51541566a48aSTheodore Ts'o 	if (sync)
51554743f839SPranay Kr. Srivastava 		lock_buffer(sbh);
5156e8680786STheodore Ts'o 	if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
51574743f839SPranay Kr. Srivastava 		/*
51584743f839SPranay Kr. Srivastava 		 * Oh, dear.  A previous attempt to write the
51594743f839SPranay Kr. Srivastava 		 * superblock failed.  This could happen because the
51604743f839SPranay Kr. Srivastava 		 * USB device was yanked out.  Or it could happen to
51614743f839SPranay Kr. Srivastava 		 * be a transient write error and maybe the block will
51624743f839SPranay Kr. Srivastava 		 * be remapped.  Nothing we can do but to retry the
51634743f839SPranay Kr. Srivastava 		 * write and hope for the best.
51644743f839SPranay Kr. Srivastava 		 */
51654743f839SPranay Kr. Srivastava 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
51664743f839SPranay Kr. Srivastava 		       "superblock detected");
51674743f839SPranay Kr. Srivastava 		clear_buffer_write_io_error(sbh);
51684743f839SPranay Kr. Srivastava 		set_buffer_uptodate(sbh);
51694743f839SPranay Kr. Srivastava 	}
5170ac27a0ecSDave Kleikamp 	mark_buffer_dirty(sbh);
5171914258bfSTheodore Ts'o 	if (sync) {
51721566a48aSTheodore Ts'o 		unlock_buffer(sbh);
5173564bc402SDaeho Jeong 		error = __sync_dirty_buffer(sbh,
517400473374SJan Kara 			REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
5175c89128a0SJaegeuk Kim 		if (buffer_write_io_error(sbh)) {
5176b31e1552SEric Sandeen 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
5177b31e1552SEric Sandeen 			       "superblock");
5178914258bfSTheodore Ts'o 			clear_buffer_write_io_error(sbh);
5179914258bfSTheodore Ts'o 			set_buffer_uptodate(sbh);
5180914258bfSTheodore Ts'o 		}
5181914258bfSTheodore Ts'o 	}
5182c4be0c1dSTakashi Sato 	return error;
5183ac27a0ecSDave Kleikamp }
5184ac27a0ecSDave Kleikamp 
5185ac27a0ecSDave Kleikamp /*
5186ac27a0ecSDave Kleikamp  * Have we just finished recovery?  If so, and if we are mounting (or
5187ac27a0ecSDave Kleikamp  * remounting) the filesystem readonly, then we will end up with a
5188ac27a0ecSDave Kleikamp  * consistent fs on disk.  Record that fact.
5189ac27a0ecSDave Kleikamp  */
5190617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb,
5191617ba13bSMingming Cao 					struct ext4_super_block *es)
5192ac27a0ecSDave Kleikamp {
5193617ba13bSMingming Cao 	journal_t *journal = EXT4_SB(sb)->s_journal;
5194ac27a0ecSDave Kleikamp 
5195e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb)) {
51960390131bSFrank Mayhar 		BUG_ON(journal != NULL);
51970390131bSFrank Mayhar 		return;
51980390131bSFrank Mayhar 	}
5199dab291afSMingming Cao 	jbd2_journal_lock_updates(journal);
52007ffe1ea8SHidehiro Kawai 	if (jbd2_journal_flush(journal) < 0)
52017ffe1ea8SHidehiro Kawai 		goto out;
52027ffe1ea8SHidehiro Kawai 
5203bc98a42cSDavid Howells 	if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
5204e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5205e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5206ac27a0ecSDave Kleikamp 	}
52077ffe1ea8SHidehiro Kawai 
52087ffe1ea8SHidehiro Kawai out:
5209dab291afSMingming Cao 	jbd2_journal_unlock_updates(journal);
5210ac27a0ecSDave Kleikamp }
5211ac27a0ecSDave Kleikamp 
5212ac27a0ecSDave Kleikamp /*
5213ac27a0ecSDave Kleikamp  * If we are mounting (or read-write remounting) a filesystem whose journal
5214ac27a0ecSDave Kleikamp  * has recorded an error from a previous lifetime, move that error to the
5215ac27a0ecSDave Kleikamp  * main filesystem now.
5216ac27a0ecSDave Kleikamp  */
5217617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb,
5218617ba13bSMingming Cao 				   struct ext4_super_block *es)
5219ac27a0ecSDave Kleikamp {
5220ac27a0ecSDave Kleikamp 	journal_t *journal;
5221ac27a0ecSDave Kleikamp 	int j_errno;
5222ac27a0ecSDave Kleikamp 	const char *errstr;
5223ac27a0ecSDave Kleikamp 
5224e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_journal(sb));
52250390131bSFrank Mayhar 
5226617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5227ac27a0ecSDave Kleikamp 
5228ac27a0ecSDave Kleikamp 	/*
5229ac27a0ecSDave Kleikamp 	 * Now check for any error status which may have been recorded in the
5230617ba13bSMingming Cao 	 * journal by a prior ext4_error() or ext4_abort()
5231ac27a0ecSDave Kleikamp 	 */
5232ac27a0ecSDave Kleikamp 
5233dab291afSMingming Cao 	j_errno = jbd2_journal_errno(journal);
5234ac27a0ecSDave Kleikamp 	if (j_errno) {
5235ac27a0ecSDave Kleikamp 		char nbuf[16];
5236ac27a0ecSDave Kleikamp 
5237617ba13bSMingming Cao 		errstr = ext4_decode_error(sb, j_errno, nbuf);
523812062dddSEric Sandeen 		ext4_warning(sb, "Filesystem error recorded "
5239ac27a0ecSDave Kleikamp 			     "from previous mount: %s", errstr);
524012062dddSEric Sandeen 		ext4_warning(sb, "Marking fs in need of filesystem check.");
5241ac27a0ecSDave Kleikamp 
5242617ba13bSMingming Cao 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5243617ba13bSMingming Cao 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
5244e2d67052STheodore Ts'o 		ext4_commit_super(sb, 1);
5245ac27a0ecSDave Kleikamp 
5246dab291afSMingming Cao 		jbd2_journal_clear_err(journal);
5247d796c52eSTheodore Ts'o 		jbd2_journal_update_sb_errno(journal);
5248ac27a0ecSDave Kleikamp 	}
5249ac27a0ecSDave Kleikamp }
5250ac27a0ecSDave Kleikamp 
5251ac27a0ecSDave Kleikamp /*
5252ac27a0ecSDave Kleikamp  * Force the running and committing transactions to commit,
5253ac27a0ecSDave Kleikamp  * and wait on the commit.
5254ac27a0ecSDave Kleikamp  */
5255617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb)
5256ac27a0ecSDave Kleikamp {
5257ac27a0ecSDave Kleikamp 	journal_t *journal;
5258ac27a0ecSDave Kleikamp 
5259bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5260ac27a0ecSDave Kleikamp 		return 0;
5261ac27a0ecSDave Kleikamp 
5262617ba13bSMingming Cao 	journal = EXT4_SB(sb)->s_journal;
5263b1deefc9SGuo Chao 	return ext4_journal_force_commit(journal);
5264ac27a0ecSDave Kleikamp }
5265ac27a0ecSDave Kleikamp 
5266617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait)
5267ac27a0ecSDave Kleikamp {
526814ce0cb4STheodore Ts'o 	int ret = 0;
52699eddacf9SJan Kara 	tid_t target;
527006a407f1SDmitry Monakhov 	bool needs_barrier = false;
52718d5d02e6SMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5272ac27a0ecSDave Kleikamp 
527349598e04SJun Piao 	if (unlikely(ext4_forced_shutdown(sbi)))
52740db1ff22STheodore Ts'o 		return 0;
52750db1ff22STheodore Ts'o 
52769bffad1eSTheodore Ts'o 	trace_ext4_sync_fs(sb, wait);
52772e8fa54eSJan Kara 	flush_workqueue(sbi->rsv_conversion_wq);
5278a1177825SJan Kara 	/*
5279a1177825SJan Kara 	 * Writeback quota in non-journalled quota case - journalled quota has
5280a1177825SJan Kara 	 * no dirty dquots
5281a1177825SJan Kara 	 */
5282a1177825SJan Kara 	dquot_writeback_dquots(sb, -1);
528306a407f1SDmitry Monakhov 	/*
528406a407f1SDmitry Monakhov 	 * Data writeback is possible w/o journal transaction, so barrier must
528506a407f1SDmitry Monakhov 	 * being sent at the end of the function. But we can skip it if
528606a407f1SDmitry Monakhov 	 * transaction_commit will do it for us.
528706a407f1SDmitry Monakhov 	 */
5288bda32530STheodore Ts'o 	if (sbi->s_journal) {
528906a407f1SDmitry Monakhov 		target = jbd2_get_latest_transaction(sbi->s_journal);
529006a407f1SDmitry Monakhov 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
529106a407f1SDmitry Monakhov 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
529206a407f1SDmitry Monakhov 			needs_barrier = true;
529306a407f1SDmitry Monakhov 
52948d5d02e6SMingming Cao 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5295ac27a0ecSDave Kleikamp 			if (wait)
5296bda32530STheodore Ts'o 				ret = jbd2_log_wait_commit(sbi->s_journal,
5297bda32530STheodore Ts'o 							   target);
52980390131bSFrank Mayhar 		}
5299bda32530STheodore Ts'o 	} else if (wait && test_opt(sb, BARRIER))
5300bda32530STheodore Ts'o 		needs_barrier = true;
530106a407f1SDmitry Monakhov 	if (needs_barrier) {
530206a407f1SDmitry Monakhov 		int err;
530306a407f1SDmitry Monakhov 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
530406a407f1SDmitry Monakhov 		if (!ret)
530506a407f1SDmitry Monakhov 			ret = err;
530606a407f1SDmitry Monakhov 	}
530706a407f1SDmitry Monakhov 
530806a407f1SDmitry Monakhov 	return ret;
530906a407f1SDmitry Monakhov }
531006a407f1SDmitry Monakhov 
5311ac27a0ecSDave Kleikamp /*
5312ac27a0ecSDave Kleikamp  * LVM calls this function before a (read-only) snapshot is created.  This
5313ac27a0ecSDave Kleikamp  * gives us a chance to flush the journal completely and mark the fs clean.
5314be4f27d3SYongqiang Yang  *
5315be4f27d3SYongqiang Yang  * Note that only this function cannot bring a filesystem to be in a clean
53168e8ad8a5SJan Kara  * state independently. It relies on upper layer to stop all data & metadata
53178e8ad8a5SJan Kara  * modifications.
5318ac27a0ecSDave Kleikamp  */
5319c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb)
5320ac27a0ecSDave Kleikamp {
5321c4be0c1dSTakashi Sato 	int error = 0;
5322c4be0c1dSTakashi Sato 	journal_t *journal;
5323ac27a0ecSDave Kleikamp 
5324bc98a42cSDavid Howells 	if (sb_rdonly(sb))
53259ca92389STheodore Ts'o 		return 0;
53269ca92389STheodore Ts'o 
5327c4be0c1dSTakashi Sato 	journal = EXT4_SB(sb)->s_journal;
5328ac27a0ecSDave Kleikamp 
5329bb044576STheodore Ts'o 	if (journal) {
5330ac27a0ecSDave Kleikamp 		/* Now we set up the journal barrier. */
5331dab291afSMingming Cao 		jbd2_journal_lock_updates(journal);
53327ffe1ea8SHidehiro Kawai 
53337ffe1ea8SHidehiro Kawai 		/*
5334bb044576STheodore Ts'o 		 * Don't clear the needs_recovery flag if we failed to
5335bb044576STheodore Ts'o 		 * flush the journal.
53367ffe1ea8SHidehiro Kawai 		 */
5337c4be0c1dSTakashi Sato 		error = jbd2_journal_flush(journal);
53386b0310fbSEric Sandeen 		if (error < 0)
53396b0310fbSEric Sandeen 			goto out;
5340ac27a0ecSDave Kleikamp 
5341ac27a0ecSDave Kleikamp 		/* Journal blocked and flushed, clear needs_recovery flag. */
5342e2b911c5SDarrick J. Wong 		ext4_clear_feature_journal_needs_recovery(sb);
5343c642dc9eSEric Sandeen 	}
5344c642dc9eSEric Sandeen 
5345e2d67052STheodore Ts'o 	error = ext4_commit_super(sb, 1);
53466b0310fbSEric Sandeen out:
5347bb044576STheodore Ts'o 	if (journal)
53488e8ad8a5SJan Kara 		/* we rely on upper layer to stop further updates */
5349bb044576STheodore Ts'o 		jbd2_journal_unlock_updates(journal);
53506b0310fbSEric Sandeen 	return error;
5351ac27a0ecSDave Kleikamp }
5352ac27a0ecSDave Kleikamp 
5353ac27a0ecSDave Kleikamp /*
5354ac27a0ecSDave Kleikamp  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
5355ac27a0ecSDave Kleikamp  * flag here, even though the filesystem is not technically dirty yet.
5356ac27a0ecSDave Kleikamp  */
5357c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb)
5358ac27a0ecSDave Kleikamp {
5359bc98a42cSDavid Howells 	if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
53609ca92389STheodore Ts'o 		return 0;
53619ca92389STheodore Ts'o 
5362c642dc9eSEric Sandeen 	if (EXT4_SB(sb)->s_journal) {
53639ca92389STheodore Ts'o 		/* Reset the needs_recovery flag before the fs is unlocked. */
5364e2b911c5SDarrick J. Wong 		ext4_set_feature_journal_needs_recovery(sb);
5365c642dc9eSEric Sandeen 	}
5366c642dc9eSEric Sandeen 
5367e2d67052STheodore Ts'o 	ext4_commit_super(sb, 1);
5368c4be0c1dSTakashi Sato 	return 0;
5369ac27a0ecSDave Kleikamp }
5370ac27a0ecSDave Kleikamp 
5371673c6100STheodore Ts'o /*
5372673c6100STheodore Ts'o  * Structure to save mount options for ext4_remount's benefit
5373673c6100STheodore Ts'o  */
5374673c6100STheodore Ts'o struct ext4_mount_options {
5375673c6100STheodore Ts'o 	unsigned long s_mount_opt;
5376a2595b8aSTheodore Ts'o 	unsigned long s_mount_opt2;
537708cefc7aSEric W. Biederman 	kuid_t s_resuid;
537808cefc7aSEric W. Biederman 	kgid_t s_resgid;
5379673c6100STheodore Ts'o 	unsigned long s_commit_interval;
5380673c6100STheodore Ts'o 	u32 s_min_batch_time, s_max_batch_time;
5381673c6100STheodore Ts'o #ifdef CONFIG_QUOTA
5382673c6100STheodore Ts'o 	int s_jquota_fmt;
5383a2d4a646SJan Kara 	char *s_qf_names[EXT4_MAXQUOTAS];
5384673c6100STheodore Ts'o #endif
5385673c6100STheodore Ts'o };
5386673c6100STheodore Ts'o 
5387617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data)
5388ac27a0ecSDave Kleikamp {
5389617ba13bSMingming Cao 	struct ext4_super_block *es;
5390617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5391ac27a0ecSDave Kleikamp 	unsigned long old_sb_flags;
5392617ba13bSMingming Cao 	struct ext4_mount_options old_opts;
5393c79d967dSChristoph Hellwig 	int enable_quota = 0;
53948a266467STheodore Ts'o 	ext4_group_t g;
5395b3881f74STheodore Ts'o 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5396c5e06d10SJohann Lombardi 	int err = 0;
5397ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
539803dafb5fSChen Gang 	int i, j;
539933458eabSTheodore Ts'o 	char *to_free[EXT4_MAXQUOTAS];
5400ac27a0ecSDave Kleikamp #endif
5401d4c402d9SCurt Wohlgemuth 	char *orig_data = kstrdup(data, GFP_KERNEL);
5402ac27a0ecSDave Kleikamp 
540321ac738eSChengguang Xu 	if (data && !orig_data)
540421ac738eSChengguang Xu 		return -ENOMEM;
540521ac738eSChengguang Xu 
5406ac27a0ecSDave Kleikamp 	/* Store the original options */
5407ac27a0ecSDave Kleikamp 	old_sb_flags = sb->s_flags;
5408ac27a0ecSDave Kleikamp 	old_opts.s_mount_opt = sbi->s_mount_opt;
5409a2595b8aSTheodore Ts'o 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
5410ac27a0ecSDave Kleikamp 	old_opts.s_resuid = sbi->s_resuid;
5411ac27a0ecSDave Kleikamp 	old_opts.s_resgid = sbi->s_resgid;
5412ac27a0ecSDave Kleikamp 	old_opts.s_commit_interval = sbi->s_commit_interval;
541330773840STheodore Ts'o 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
541430773840STheodore Ts'o 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
5415ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5416ac27a0ecSDave Kleikamp 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
5417a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
541803dafb5fSChen Gang 		if (sbi->s_qf_names[i]) {
541933458eabSTheodore Ts'o 			char *qf_name = get_qf_name(sb, sbi, i);
542033458eabSTheodore Ts'o 
542133458eabSTheodore Ts'o 			old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
542203dafb5fSChen Gang 			if (!old_opts.s_qf_names[i]) {
542303dafb5fSChen Gang 				for (j = 0; j < i; j++)
542403dafb5fSChen Gang 					kfree(old_opts.s_qf_names[j]);
54253e36a163SWei Yongjun 				kfree(orig_data);
542603dafb5fSChen Gang 				return -ENOMEM;
542703dafb5fSChen Gang 			}
542803dafb5fSChen Gang 		} else
542903dafb5fSChen Gang 			old_opts.s_qf_names[i] = NULL;
5430ac27a0ecSDave Kleikamp #endif
5431b3881f74STheodore Ts'o 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5432b3881f74STheodore Ts'o 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
5433ac27a0ecSDave Kleikamp 
5434661aa520SEric Sandeen 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
5435ac27a0ecSDave Kleikamp 		err = -EINVAL;
5436ac27a0ecSDave Kleikamp 		goto restore_opts;
5437ac27a0ecSDave Kleikamp 	}
5438ac27a0ecSDave Kleikamp 
54396b992ff2SDarrick J. Wong 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
54406b992ff2SDarrick J. Wong 	    test_opt(sb, JOURNAL_CHECKSUM)) {
54416b992ff2SDarrick J. Wong 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
54422d5b86e0SEric Sandeen 			 "during remount not supported; ignoring");
54432d5b86e0SEric Sandeen 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
5444c6d3d56dSDarrick J. Wong 	}
5445c6d3d56dSDarrick J. Wong 
54466ae6514bSPiotr Sarna 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
54476ae6514bSPiotr Sarna 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
54486ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
54496ae6514bSPiotr Sarna 				 "both data=journal and delalloc");
54506ae6514bSPiotr Sarna 			err = -EINVAL;
54516ae6514bSPiotr Sarna 			goto restore_opts;
54526ae6514bSPiotr Sarna 		}
54536ae6514bSPiotr Sarna 		if (test_opt(sb, DIOREAD_NOLOCK)) {
54546ae6514bSPiotr Sarna 			ext4_msg(sb, KERN_ERR, "can't mount with "
54556ae6514bSPiotr Sarna 				 "both data=journal and dioread_nolock");
54566ae6514bSPiotr Sarna 			err = -EINVAL;
54576ae6514bSPiotr Sarna 			goto restore_opts;
54586ae6514bSPiotr Sarna 		}
5459fc626fe3SIra Weiny 		if (test_opt(sb, DAX_ALWAYS)) {
5460923ae0ffSRoss Zwisler 			ext4_msg(sb, KERN_ERR, "can't mount with "
5461923ae0ffSRoss Zwisler 				 "both data=journal and dax");
5462923ae0ffSRoss Zwisler 			err = -EINVAL;
5463923ae0ffSRoss Zwisler 			goto restore_opts;
5464923ae0ffSRoss Zwisler 		}
5465ab04df78SJan Kara 	} else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5466ab04df78SJan Kara 		if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5467ab04df78SJan Kara 			ext4_msg(sb, KERN_ERR, "can't mount with "
5468ab04df78SJan Kara 				"journal_async_commit in data=ordered mode");
5469ab04df78SJan Kara 			err = -EINVAL;
5470ab04df78SJan Kara 			goto restore_opts;
5471ab04df78SJan Kara 		}
5472923ae0ffSRoss Zwisler 	}
5473923ae0ffSRoss Zwisler 
5474cdb7ee4cSTahsin Erdogan 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5475cdb7ee4cSTahsin Erdogan 		ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5476cdb7ee4cSTahsin Erdogan 		err = -EINVAL;
5477cdb7ee4cSTahsin Erdogan 		goto restore_opts;
5478cdb7ee4cSTahsin Erdogan 	}
5479cdb7ee4cSTahsin Erdogan 
5480*9cb20f94SIra Weiny 	if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX_ALWAYS ||
5481*9cb20f94SIra Weiny 	    (sbi->s_mount_opt2 ^ old_opts.s_mount_opt2) & EXT4_MOUNT2_DAX_NEVER ||
5482*9cb20f94SIra Weiny 	    (sbi->s_mount_opt2 ^ old_opts.s_mount_opt2) & EXT4_MOUNT2_DAX_INODE) {
5483923ae0ffSRoss Zwisler 		ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
5484*9cb20f94SIra Weiny 			"dax mount option with busy inodes while remounting");
5485*9cb20f94SIra Weiny 		sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
5486*9cb20f94SIra Weiny 		sbi->s_mount_opt |= old_opts.s_mount_opt & EXT4_MOUNT_DAX_ALWAYS;
5487*9cb20f94SIra Weiny 		sbi->s_mount_opt2 &= ~(EXT4_MOUNT2_DAX_NEVER | EXT4_MOUNT2_DAX_INODE);
5488*9cb20f94SIra Weiny 		sbi->s_mount_opt2 |= old_opts.s_mount_opt2 &
5489*9cb20f94SIra Weiny 				     (EXT4_MOUNT2_DAX_NEVER | EXT4_MOUNT2_DAX_INODE);
54906ae6514bSPiotr Sarna 	}
54916ae6514bSPiotr Sarna 
54924ab2f15bSTheodore Ts'o 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
549354d3adbcSTheodore Ts'o 		ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
5494ac27a0ecSDave Kleikamp 
54951751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
54961751e8a6SLinus Torvalds 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
5497ac27a0ecSDave Kleikamp 
5498ac27a0ecSDave Kleikamp 	es = sbi->s_es;
5499ac27a0ecSDave Kleikamp 
5500b3881f74STheodore Ts'o 	if (sbi->s_journal) {
5501617ba13bSMingming Cao 		ext4_init_journal_params(sb, sbi->s_journal);
5502b3881f74STheodore Ts'o 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5503b3881f74STheodore Ts'o 	}
5504ac27a0ecSDave Kleikamp 
55051751e8a6SLinus Torvalds 	if (*flags & SB_LAZYTIME)
55061751e8a6SLinus Torvalds 		sb->s_flags |= SB_LAZYTIME;
5507a2fd66d0STheodore Ts'o 
55081751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
55094ab2f15bSTheodore Ts'o 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5510ac27a0ecSDave Kleikamp 			err = -EROFS;
5511ac27a0ecSDave Kleikamp 			goto restore_opts;
5512ac27a0ecSDave Kleikamp 		}
5513ac27a0ecSDave Kleikamp 
55141751e8a6SLinus Torvalds 		if (*flags & SB_RDONLY) {
551538c03b34STheodore Ts'o 			err = sync_filesystem(sb);
551638c03b34STheodore Ts'o 			if (err < 0)
551738c03b34STheodore Ts'o 				goto restore_opts;
55180f0dd62fSChristoph Hellwig 			err = dquot_suspend(sb, -1);
55190f0dd62fSChristoph Hellwig 			if (err < 0)
5520c79d967dSChristoph Hellwig 				goto restore_opts;
5521c79d967dSChristoph Hellwig 
5522ac27a0ecSDave Kleikamp 			/*
5523ac27a0ecSDave Kleikamp 			 * First of all, the unconditional stuff we have to do
5524ac27a0ecSDave Kleikamp 			 * to disable replay of the journal when we next remount
5525ac27a0ecSDave Kleikamp 			 */
55261751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
5527ac27a0ecSDave Kleikamp 
5528ac27a0ecSDave Kleikamp 			/*
5529ac27a0ecSDave Kleikamp 			 * OK, test if we are remounting a valid rw partition
5530ac27a0ecSDave Kleikamp 			 * readonly, and if so set the rdonly flag and then
5531ac27a0ecSDave Kleikamp 			 * mark the partition as valid again.
5532ac27a0ecSDave Kleikamp 			 */
5533617ba13bSMingming Cao 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5534617ba13bSMingming Cao 			    (sbi->s_mount_state & EXT4_VALID_FS))
5535ac27a0ecSDave Kleikamp 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5536ac27a0ecSDave Kleikamp 
5537a63c9eb2STheodore Ts'o 			if (sbi->s_journal)
5538617ba13bSMingming Cao 				ext4_mark_recovery_complete(sb, es);
55392dca60d9STheodore Ts'o 			if (sbi->s_mmp_tsk)
55402dca60d9STheodore Ts'o 				kthread_stop(sbi->s_mmp_tsk);
5541ac27a0ecSDave Kleikamp 		} else {
5542a13fb1a4SEric Sandeen 			/* Make sure we can mount this feature set readwrite */
5543e2b911c5SDarrick J. Wong 			if (ext4_has_feature_readonly(sb) ||
55442cb5cc8bSDarrick J. Wong 			    !ext4_feature_set_ok(sb, 0)) {
5545ac27a0ecSDave Kleikamp 				err = -EROFS;
5546ac27a0ecSDave Kleikamp 				goto restore_opts;
5547ac27a0ecSDave Kleikamp 			}
5548ead6596bSEric Sandeen 			/*
55498a266467STheodore Ts'o 			 * Make sure the group descriptor checksums
55500b8e58a1SAndreas Dilger 			 * are sane.  If they aren't, refuse to remount r/w.
55518a266467STheodore Ts'o 			 */
55528a266467STheodore Ts'o 			for (g = 0; g < sbi->s_groups_count; g++) {
55538a266467STheodore Ts'o 				struct ext4_group_desc *gdp =
55548a266467STheodore Ts'o 					ext4_get_group_desc(sb, g, NULL);
55558a266467STheodore Ts'o 
5556feb0ab32SDarrick J. Wong 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5557b31e1552SEric Sandeen 					ext4_msg(sb, KERN_ERR,
5558b31e1552SEric Sandeen 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5559e2b911c5SDarrick J. Wong 		g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
55608a266467STheodore Ts'o 					       le16_to_cpu(gdp->bg_checksum));
55616a797d27SDarrick J. Wong 					err = -EFSBADCRC;
55628a266467STheodore Ts'o 					goto restore_opts;
55638a266467STheodore Ts'o 				}
55648a266467STheodore Ts'o 			}
55658a266467STheodore Ts'o 
55668a266467STheodore Ts'o 			/*
5567ead6596bSEric Sandeen 			 * If we have an unprocessed orphan list hanging
5568ead6596bSEric Sandeen 			 * around from a previously readonly bdev mount,
5569ead6596bSEric Sandeen 			 * require a full umount/remount for now.
5570ead6596bSEric Sandeen 			 */
5571ead6596bSEric Sandeen 			if (es->s_last_orphan) {
5572b31e1552SEric Sandeen 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5573ead6596bSEric Sandeen 				       "remount RDWR because of unprocessed "
5574ead6596bSEric Sandeen 				       "orphan inode list.  Please "
5575b31e1552SEric Sandeen 				       "umount/remount instead");
5576ead6596bSEric Sandeen 				err = -EINVAL;
5577ead6596bSEric Sandeen 				goto restore_opts;
5578ead6596bSEric Sandeen 			}
5579ead6596bSEric Sandeen 
5580ac27a0ecSDave Kleikamp 			/*
5581ac27a0ecSDave Kleikamp 			 * Mounting a RDONLY partition read-write, so reread
5582ac27a0ecSDave Kleikamp 			 * and store the current valid flag.  (It may have
5583ac27a0ecSDave Kleikamp 			 * been changed by e2fsck since we originally mounted
5584ac27a0ecSDave Kleikamp 			 * the partition.)
5585ac27a0ecSDave Kleikamp 			 */
55860390131bSFrank Mayhar 			if (sbi->s_journal)
5587617ba13bSMingming Cao 				ext4_clear_journal_err(sb, es);
5588ac27a0ecSDave Kleikamp 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5589c89128a0SJaegeuk Kim 
5590c89128a0SJaegeuk Kim 			err = ext4_setup_super(sb, es, 0);
5591c89128a0SJaegeuk Kim 			if (err)
5592c89128a0SJaegeuk Kim 				goto restore_opts;
5593c89128a0SJaegeuk Kim 
55941751e8a6SLinus Torvalds 			sb->s_flags &= ~SB_RDONLY;
5595e2b911c5SDarrick J. Wong 			if (ext4_has_feature_mmp(sb))
5596c5e06d10SJohann Lombardi 				if (ext4_multi_mount_protect(sb,
5597c5e06d10SJohann Lombardi 						le64_to_cpu(es->s_mmp_block))) {
5598c5e06d10SJohann Lombardi 					err = -EROFS;
5599c5e06d10SJohann Lombardi 					goto restore_opts;
5600c5e06d10SJohann Lombardi 				}
5601c79d967dSChristoph Hellwig 			enable_quota = 1;
5602ac27a0ecSDave Kleikamp 		}
5603ac27a0ecSDave Kleikamp 	}
5604bfff6873SLukas Czerner 
5605bfff6873SLukas Czerner 	/*
5606bfff6873SLukas Czerner 	 * Reinitialize lazy itable initialization thread based on
5607bfff6873SLukas Czerner 	 * current settings
5608bfff6873SLukas Czerner 	 */
5609bc98a42cSDavid Howells 	if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
5610bfff6873SLukas Czerner 		ext4_unregister_li_request(sb);
5611bfff6873SLukas Czerner 	else {
5612bfff6873SLukas Czerner 		ext4_group_t first_not_zeroed;
5613bfff6873SLukas Czerner 		first_not_zeroed = ext4_has_uninit_itable(sb);
5614bfff6873SLukas Czerner 		ext4_register_li_request(sb, first_not_zeroed);
5615bfff6873SLukas Czerner 	}
5616bfff6873SLukas Czerner 
56176fd058f7STheodore Ts'o 	ext4_setup_system_zone(sb);
5618c89128a0SJaegeuk Kim 	if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5619c89128a0SJaegeuk Kim 		err = ext4_commit_super(sb, 1);
5620c89128a0SJaegeuk Kim 		if (err)
5621c89128a0SJaegeuk Kim 			goto restore_opts;
5622c89128a0SJaegeuk Kim 	}
56230390131bSFrank Mayhar 
5624ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5625ac27a0ecSDave Kleikamp 	/* Release old quota file names */
5626a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5627ac27a0ecSDave Kleikamp 		kfree(old_opts.s_qf_names[i]);
56287c319d32SAditya Kali 	if (enable_quota) {
56297c319d32SAditya Kali 		if (sb_any_quota_suspended(sb))
56300f0dd62fSChristoph Hellwig 			dquot_resume(sb, -1);
5631e2b911c5SDarrick J. Wong 		else if (ext4_has_feature_quota(sb)) {
56327c319d32SAditya Kali 			err = ext4_enable_quotas(sb);
563307724f98STheodore Ts'o 			if (err)
56347c319d32SAditya Kali 				goto restore_opts;
56357c319d32SAditya Kali 		}
56367c319d32SAditya Kali 	}
56377c319d32SAditya Kali #endif
5638d4c402d9SCurt Wohlgemuth 
56391751e8a6SLinus Torvalds 	*flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
5640d4c402d9SCurt Wohlgemuth 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5641d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5642ac27a0ecSDave Kleikamp 	return 0;
56430b8e58a1SAndreas Dilger 
5644ac27a0ecSDave Kleikamp restore_opts:
5645ac27a0ecSDave Kleikamp 	sb->s_flags = old_sb_flags;
5646ac27a0ecSDave Kleikamp 	sbi->s_mount_opt = old_opts.s_mount_opt;
5647a2595b8aSTheodore Ts'o 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5648ac27a0ecSDave Kleikamp 	sbi->s_resuid = old_opts.s_resuid;
5649ac27a0ecSDave Kleikamp 	sbi->s_resgid = old_opts.s_resgid;
5650ac27a0ecSDave Kleikamp 	sbi->s_commit_interval = old_opts.s_commit_interval;
565130773840STheodore Ts'o 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
565230773840STheodore Ts'o 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5653ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5654ac27a0ecSDave Kleikamp 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5655a2d4a646SJan Kara 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
565633458eabSTheodore Ts'o 		to_free[i] = get_qf_name(sb, sbi, i);
565733458eabSTheodore Ts'o 		rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
5658ac27a0ecSDave Kleikamp 	}
565933458eabSTheodore Ts'o 	synchronize_rcu();
566033458eabSTheodore Ts'o 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
566133458eabSTheodore Ts'o 		kfree(to_free[i]);
5662ac27a0ecSDave Kleikamp #endif
5663d4c402d9SCurt Wohlgemuth 	kfree(orig_data);
5664ac27a0ecSDave Kleikamp 	return err;
5665ac27a0ecSDave Kleikamp }
5666ac27a0ecSDave Kleikamp 
5667689c958cSLi Xi #ifdef CONFIG_QUOTA
5668689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb,
5669689c958cSLi Xi 			       kprojid_t projid, struct kstatfs *buf)
5670689c958cSLi Xi {
5671689c958cSLi Xi 	struct kqid qid;
5672689c958cSLi Xi 	struct dquot *dquot;
5673689c958cSLi Xi 	u64 limit;
5674689c958cSLi Xi 	u64 curblock;
5675689c958cSLi Xi 
5676689c958cSLi Xi 	qid = make_kqid_projid(projid);
5677689c958cSLi Xi 	dquot = dqget(sb, qid);
5678689c958cSLi Xi 	if (IS_ERR(dquot))
5679689c958cSLi Xi 		return PTR_ERR(dquot);
56807b9ca4c6SJan Kara 	spin_lock(&dquot->dq_dqb_lock);
5681689c958cSLi Xi 
5682a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
5683a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_bhardlimit);
568457c32ea4SChengguang Xu 	limit >>= sb->s_blocksize_bits;
568557c32ea4SChengguang Xu 
5686689c958cSLi Xi 	if (limit && buf->f_blocks > limit) {
5687f06925c7SKonstantin Khlebnikov 		curblock = (dquot->dq_dqb.dqb_curspace +
5688f06925c7SKonstantin Khlebnikov 			    dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
5689689c958cSLi Xi 		buf->f_blocks = limit;
5690689c958cSLi Xi 		buf->f_bfree = buf->f_bavail =
5691689c958cSLi Xi 			(buf->f_blocks > curblock) ?
5692689c958cSLi Xi 			 (buf->f_blocks - curblock) : 0;
5693689c958cSLi Xi 	}
5694689c958cSLi Xi 
5695a08fe66eSChengguang Xu 	limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
5696a08fe66eSChengguang Xu 			     dquot->dq_dqb.dqb_ihardlimit);
5697689c958cSLi Xi 	if (limit && buf->f_files > limit) {
5698689c958cSLi Xi 		buf->f_files = limit;
5699689c958cSLi Xi 		buf->f_ffree =
5700689c958cSLi Xi 			(buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5701689c958cSLi Xi 			 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5702689c958cSLi Xi 	}
5703689c958cSLi Xi 
57047b9ca4c6SJan Kara 	spin_unlock(&dquot->dq_dqb_lock);
5705689c958cSLi Xi 	dqput(dquot);
5706689c958cSLi Xi 	return 0;
5707689c958cSLi Xi }
5708689c958cSLi Xi #endif
5709689c958cSLi Xi 
5710617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5711ac27a0ecSDave Kleikamp {
5712ac27a0ecSDave Kleikamp 	struct super_block *sb = dentry->d_sb;
5713617ba13bSMingming Cao 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5714617ba13bSMingming Cao 	struct ext4_super_block *es = sbi->s_es;
571527dd4385SLukas Czerner 	ext4_fsblk_t overhead = 0, resv_blocks;
5716960cc398SPekka Enberg 	u64 fsid;
5717d02a9391SKazuya Mio 	s64 bfree;
571827dd4385SLukas Czerner 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5719ac27a0ecSDave Kleikamp 
5720952fc18eSTheodore Ts'o 	if (!test_opt(sb, MINIX_DF))
5721952fc18eSTheodore Ts'o 		overhead = sbi->s_overhead;
5722ac27a0ecSDave Kleikamp 
5723617ba13bSMingming Cao 	buf->f_type = EXT4_SUPER_MAGIC;
5724ac27a0ecSDave Kleikamp 	buf->f_bsize = sb->s_blocksize;
5725b72f78cbSEric Sandeen 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
572657042651STheodore Ts'o 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
572757042651STheodore Ts'o 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5728d02a9391SKazuya Mio 	/* prevent underflow in case that few free space is available */
572957042651STheodore Ts'o 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
573027dd4385SLukas Czerner 	buf->f_bavail = buf->f_bfree -
573127dd4385SLukas Czerner 			(ext4_r_blocks_count(es) + resv_blocks);
573227dd4385SLukas Czerner 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5733ac27a0ecSDave Kleikamp 		buf->f_bavail = 0;
5734ac27a0ecSDave Kleikamp 	buf->f_files = le32_to_cpu(es->s_inodes_count);
573552d9f3b4SPeter Zijlstra 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5736617ba13bSMingming Cao 	buf->f_namelen = EXT4_NAME_LEN;
5737960cc398SPekka Enberg 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5738960cc398SPekka Enberg 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5739960cc398SPekka Enberg 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5740960cc398SPekka Enberg 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
57410b8e58a1SAndreas Dilger 
5742689c958cSLi Xi #ifdef CONFIG_QUOTA
5743689c958cSLi Xi 	if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5744689c958cSLi Xi 	    sb_has_quota_limits_enabled(sb, PRJQUOTA))
5745689c958cSLi Xi 		ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5746689c958cSLi Xi #endif
5747ac27a0ecSDave Kleikamp 	return 0;
5748ac27a0ecSDave Kleikamp }
5749ac27a0ecSDave Kleikamp 
5750ac27a0ecSDave Kleikamp 
5751ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA
5752ac27a0ecSDave Kleikamp 
5753bc8230eeSJan Kara /*
5754bc8230eeSJan Kara  * Helper functions so that transaction is started before we acquire dqio_sem
5755bc8230eeSJan Kara  * to keep correct lock ordering of transaction > dqio_sem
5756bc8230eeSJan Kara  */
5757ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot)
5758ac27a0ecSDave Kleikamp {
57594c376dcaSEric W. Biederman 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5760ac27a0ecSDave Kleikamp }
5761ac27a0ecSDave Kleikamp 
5762617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot)
5763ac27a0ecSDave Kleikamp {
5764ac27a0ecSDave Kleikamp 	int ret, err;
5765ac27a0ecSDave Kleikamp 	handle_t *handle;
5766ac27a0ecSDave Kleikamp 	struct inode *inode;
5767ac27a0ecSDave Kleikamp 
5768ac27a0ecSDave Kleikamp 	inode = dquot_to_inode(dquot);
57699924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5770617ba13bSMingming Cao 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5771ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5772ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5773ac27a0ecSDave Kleikamp 	ret = dquot_commit(dquot);
5774617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5775ac27a0ecSDave Kleikamp 	if (!ret)
5776ac27a0ecSDave Kleikamp 		ret = err;
5777ac27a0ecSDave Kleikamp 	return ret;
5778ac27a0ecSDave Kleikamp }
5779ac27a0ecSDave Kleikamp 
5780617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot)
5781ac27a0ecSDave Kleikamp {
5782ac27a0ecSDave Kleikamp 	int ret, err;
5783ac27a0ecSDave Kleikamp 	handle_t *handle;
5784ac27a0ecSDave Kleikamp 
57859924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5786617ba13bSMingming Cao 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5787ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5788ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5789ac27a0ecSDave Kleikamp 	ret = dquot_acquire(dquot);
5790617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5791ac27a0ecSDave Kleikamp 	if (!ret)
5792ac27a0ecSDave Kleikamp 		ret = err;
5793ac27a0ecSDave Kleikamp 	return ret;
5794ac27a0ecSDave Kleikamp }
5795ac27a0ecSDave Kleikamp 
5796617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot)
5797ac27a0ecSDave Kleikamp {
5798ac27a0ecSDave Kleikamp 	int ret, err;
5799ac27a0ecSDave Kleikamp 	handle_t *handle;
5800ac27a0ecSDave Kleikamp 
58019924a92aSTheodore Ts'o 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5802617ba13bSMingming Cao 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
58039c3013e9SJan Kara 	if (IS_ERR(handle)) {
58049c3013e9SJan Kara 		/* Release dquot anyway to avoid endless cycle in dqput() */
58059c3013e9SJan Kara 		dquot_release(dquot);
5806ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
58079c3013e9SJan Kara 	}
5808ac27a0ecSDave Kleikamp 	ret = dquot_release(dquot);
5809617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5810ac27a0ecSDave Kleikamp 	if (!ret)
5811ac27a0ecSDave Kleikamp 		ret = err;
5812ac27a0ecSDave Kleikamp 	return ret;
5813ac27a0ecSDave Kleikamp }
5814ac27a0ecSDave Kleikamp 
5815617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot)
5816ac27a0ecSDave Kleikamp {
5817262b4662SJan Kara 	struct super_block *sb = dquot->dq_sb;
5818262b4662SJan Kara 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5819262b4662SJan Kara 
58202c8be6b2SJan Kara 	/* Are we journaling quotas? */
5821e2b911c5SDarrick J. Wong 	if (ext4_has_feature_quota(sb) ||
5822262b4662SJan Kara 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5823ac27a0ecSDave Kleikamp 		dquot_mark_dquot_dirty(dquot);
5824617ba13bSMingming Cao 		return ext4_write_dquot(dquot);
5825ac27a0ecSDave Kleikamp 	} else {
5826ac27a0ecSDave Kleikamp 		return dquot_mark_dquot_dirty(dquot);
5827ac27a0ecSDave Kleikamp 	}
5828ac27a0ecSDave Kleikamp }
5829ac27a0ecSDave Kleikamp 
5830617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type)
5831ac27a0ecSDave Kleikamp {
5832ac27a0ecSDave Kleikamp 	int ret, err;
5833ac27a0ecSDave Kleikamp 	handle_t *handle;
5834ac27a0ecSDave Kleikamp 
5835ac27a0ecSDave Kleikamp 	/* Data block + inode block */
58362b0143b5SDavid Howells 	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
5837ac27a0ecSDave Kleikamp 	if (IS_ERR(handle))
5838ac27a0ecSDave Kleikamp 		return PTR_ERR(handle);
5839ac27a0ecSDave Kleikamp 	ret = dquot_commit_info(sb, type);
5840617ba13bSMingming Cao 	err = ext4_journal_stop(handle);
5841ac27a0ecSDave Kleikamp 	if (!ret)
5842ac27a0ecSDave Kleikamp 		ret = err;
5843ac27a0ecSDave Kleikamp 	return ret;
5844ac27a0ecSDave Kleikamp }
5845ac27a0ecSDave Kleikamp 
5846ac27a0ecSDave Kleikamp /*
5847ac27a0ecSDave Kleikamp  * Turn on quotas during mount time - we need to find
5848ac27a0ecSDave Kleikamp  * the quota file and such...
5849ac27a0ecSDave Kleikamp  */
5850617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type)
5851ac27a0ecSDave Kleikamp {
585233458eabSTheodore Ts'o 	return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
5853617ba13bSMingming Cao 					EXT4_SB(sb)->s_jquota_fmt, type);
5854ac27a0ecSDave Kleikamp }
5855ac27a0ecSDave Kleikamp 
5856daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5857daf647d2STheodore Ts'o {
5858daf647d2STheodore Ts'o 	struct ext4_inode_info *ei = EXT4_I(inode);
5859daf647d2STheodore Ts'o 
5860daf647d2STheodore Ts'o 	/* The first argument of lockdep_set_subclass has to be
5861daf647d2STheodore Ts'o 	 * *exactly* the same as the argument to init_rwsem() --- in
5862daf647d2STheodore Ts'o 	 * this case, in init_once() --- or lockdep gets unhappy
5863daf647d2STheodore Ts'o 	 * because the name of the lock is set using the
5864daf647d2STheodore Ts'o 	 * stringification of the argument to init_rwsem().
5865daf647d2STheodore Ts'o 	 */
5866daf647d2STheodore Ts'o 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5867daf647d2STheodore Ts'o 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5868daf647d2STheodore Ts'o }
5869daf647d2STheodore Ts'o 
5870ac27a0ecSDave Kleikamp /*
5871ac27a0ecSDave Kleikamp  * Standard function to be called on quota_on
5872ac27a0ecSDave Kleikamp  */
5873617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id,
58748c54ca9cSAl Viro 			 const struct path *path)
5875ac27a0ecSDave Kleikamp {
5876ac27a0ecSDave Kleikamp 	int err;
5877ac27a0ecSDave Kleikamp 
5878ac27a0ecSDave Kleikamp 	if (!test_opt(sb, QUOTA))
5879ac27a0ecSDave Kleikamp 		return -EINVAL;
58800623543bSJan Kara 
5881ac27a0ecSDave Kleikamp 	/* Quotafile not on the same filesystem? */
5882d8c9584eSAl Viro 	if (path->dentry->d_sb != sb)
5883ac27a0ecSDave Kleikamp 		return -EXDEV;
58840623543bSJan Kara 	/* Journaling quota? */
58850623543bSJan Kara 	if (EXT4_SB(sb)->s_qf_names[type]) {
58862b2d6d01STheodore Ts'o 		/* Quotafile not in fs root? */
5887f00c9e44SJan Kara 		if (path->dentry->d_parent != sb->s_root)
5888b31e1552SEric Sandeen 			ext4_msg(sb, KERN_WARNING,
5889b31e1552SEric Sandeen 				"Quota file not on filesystem root. "
5890b31e1552SEric Sandeen 				"Journaled quota will not work");
589191389240SJan Kara 		sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
589291389240SJan Kara 	} else {
589391389240SJan Kara 		/*
589491389240SJan Kara 		 * Clear the flag just in case mount options changed since
589591389240SJan Kara 		 * last time.
589691389240SJan Kara 		 */
589791389240SJan Kara 		sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
58980623543bSJan Kara 	}
58990623543bSJan Kara 
59000623543bSJan Kara 	/*
59010623543bSJan Kara 	 * When we journal data on quota file, we have to flush journal to see
59020623543bSJan Kara 	 * all updates to the file when we bypass pagecache...
59030623543bSJan Kara 	 */
59040390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal &&
59052b0143b5SDavid Howells 	    ext4_should_journal_data(d_inode(path->dentry))) {
59060623543bSJan Kara 		/*
59070623543bSJan Kara 		 * We don't need to lock updates but journal_flush() could
59080623543bSJan Kara 		 * otherwise be livelocked...
59090623543bSJan Kara 		 */
59100623543bSJan Kara 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
59117ffe1ea8SHidehiro Kawai 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
59120623543bSJan Kara 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5913f00c9e44SJan Kara 		if (err)
59147ffe1ea8SHidehiro Kawai 			return err;
59157ffe1ea8SHidehiro Kawai 	}
5916957153fcSJan Kara 
5917daf647d2STheodore Ts'o 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5918daf647d2STheodore Ts'o 	err = dquot_quota_on(sb, type, format_id, path);
5919957153fcSJan Kara 	if (err) {
5920daf647d2STheodore Ts'o 		lockdep_set_quota_inode(path->dentry->d_inode,
5921daf647d2STheodore Ts'o 					     I_DATA_SEM_NORMAL);
5922957153fcSJan Kara 	} else {
5923957153fcSJan Kara 		struct inode *inode = d_inode(path->dentry);
5924957153fcSJan Kara 		handle_t *handle;
5925957153fcSJan Kara 
592661a92987SJan Kara 		/*
592761a92987SJan Kara 		 * Set inode flags to prevent userspace from messing with quota
592861a92987SJan Kara 		 * files. If this fails, we return success anyway since quotas
592961a92987SJan Kara 		 * are already enabled and this is not a hard failure.
593061a92987SJan Kara 		 */
5931957153fcSJan Kara 		inode_lock(inode);
5932957153fcSJan Kara 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5933957153fcSJan Kara 		if (IS_ERR(handle))
5934957153fcSJan Kara 			goto unlock_inode;
5935957153fcSJan Kara 		EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
5936957153fcSJan Kara 		inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
5937957153fcSJan Kara 				S_NOATIME | S_IMMUTABLE);
5938957153fcSJan Kara 		ext4_mark_inode_dirty(handle, inode);
5939957153fcSJan Kara 		ext4_journal_stop(handle);
5940957153fcSJan Kara 	unlock_inode:
5941957153fcSJan Kara 		inode_unlock(inode);
5942957153fcSJan Kara 	}
5943daf647d2STheodore Ts'o 	return err;
5944ac27a0ecSDave Kleikamp }
5945ac27a0ecSDave Kleikamp 
59467c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
59477c319d32SAditya Kali 			     unsigned int flags)
59487c319d32SAditya Kali {
59497c319d32SAditya Kali 	int err;
59507c319d32SAditya Kali 	struct inode *qf_inode;
5951a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
59527c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5953689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5954689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
59557c319d32SAditya Kali 	};
59567c319d32SAditya Kali 
5957e2b911c5SDarrick J. Wong 	BUG_ON(!ext4_has_feature_quota(sb));
59587c319d32SAditya Kali 
59597c319d32SAditya Kali 	if (!qf_inums[type])
59607c319d32SAditya Kali 		return -EPERM;
59617c319d32SAditya Kali 
59628a363970STheodore Ts'o 	qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
59637c319d32SAditya Kali 	if (IS_ERR(qf_inode)) {
59647c319d32SAditya Kali 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
59657c319d32SAditya Kali 		return PTR_ERR(qf_inode);
59667c319d32SAditya Kali 	}
59677c319d32SAditya Kali 
5968bcb13850SJan Kara 	/* Don't account quota for quota files to avoid recursion */
5969bcb13850SJan Kara 	qf_inode->i_flags |= S_NOQUOTA;
5970daf647d2STheodore Ts'o 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
59717212b95eSJan Kara 	err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
5972daf647d2STheodore Ts'o 	if (err)
5973daf647d2STheodore Ts'o 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
597461157b24SPan Bian 	iput(qf_inode);
59757c319d32SAditya Kali 
59767c319d32SAditya Kali 	return err;
59777c319d32SAditya Kali }
59787c319d32SAditya Kali 
59797c319d32SAditya Kali /* Enable usage tracking for all quota types. */
59807c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb)
59817c319d32SAditya Kali {
59827c319d32SAditya Kali 	int type, err = 0;
5983a2d4a646SJan Kara 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
59847c319d32SAditya Kali 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5985689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
5986689c958cSLi Xi 		le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
59877c319d32SAditya Kali 	};
598849da9392SJan Kara 	bool quota_mopt[EXT4_MAXQUOTAS] = {
598949da9392SJan Kara 		test_opt(sb, USRQUOTA),
599049da9392SJan Kara 		test_opt(sb, GRPQUOTA),
599149da9392SJan Kara 		test_opt(sb, PRJQUOTA),
599249da9392SJan Kara 	};
59937c319d32SAditya Kali 
599491389240SJan Kara 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
5995a2d4a646SJan Kara 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
59967c319d32SAditya Kali 		if (qf_inums[type]) {
59977c319d32SAditya Kali 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
599849da9392SJan Kara 				DQUOT_USAGE_ENABLED |
599949da9392SJan Kara 				(quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
60007c319d32SAditya Kali 			if (err) {
60017c319d32SAditya Kali 				ext4_warning(sb,
600272ba7450STheodore Ts'o 					"Failed to enable quota tracking "
600372ba7450STheodore Ts'o 					"(type=%d, err=%d). Please run "
600472ba7450STheodore Ts'o 					"e2fsck to fix.", type, err);
60057f144fd0SJunichi Uekawa 				for (type--; type >= 0; type--)
60067f144fd0SJunichi Uekawa 					dquot_quota_off(sb, type);
60077f144fd0SJunichi Uekawa 
60087c319d32SAditya Kali 				return err;
60097c319d32SAditya Kali 			}
60107c319d32SAditya Kali 		}
60117c319d32SAditya Kali 	}
60127c319d32SAditya Kali 	return 0;
60137c319d32SAditya Kali }
60147c319d32SAditya Kali 
6015ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type)
6016ca0e05e4SDmitry Monakhov {
601721f97697SJan Kara 	struct inode *inode = sb_dqopt(sb)->files[type];
601821f97697SJan Kara 	handle_t *handle;
6019957153fcSJan Kara 	int err;
602021f97697SJan Kara 
602187009d86SDmitry Monakhov 	/* Force all delayed allocation blocks to be allocated.
602287009d86SDmitry Monakhov 	 * Caller already holds s_umount sem */
602387009d86SDmitry Monakhov 	if (test_opt(sb, DELALLOC))
6024ca0e05e4SDmitry Monakhov 		sync_filesystem(sb);
6025ca0e05e4SDmitry Monakhov 
6026957153fcSJan Kara 	if (!inode || !igrab(inode))
60270b268590SAmir Goldstein 		goto out;
60280b268590SAmir Goldstein 
6029957153fcSJan Kara 	err = dquot_quota_off(sb, type);
6030964edf66SJan Kara 	if (err || ext4_has_feature_quota(sb))
6031957153fcSJan Kara 		goto out_put;
6032957153fcSJan Kara 
6033957153fcSJan Kara 	inode_lock(inode);
603461a92987SJan Kara 	/*
603561a92987SJan Kara 	 * Update modification times of quota files when userspace can
603661a92987SJan Kara 	 * start looking at them. If we fail, we return success anyway since
603761a92987SJan Kara 	 * this is not a hard failure and quotas are already disabled.
603861a92987SJan Kara 	 */
60399924a92aSTheodore Ts'o 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
604021f97697SJan Kara 	if (IS_ERR(handle))
6041957153fcSJan Kara 		goto out_unlock;
6042957153fcSJan Kara 	EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6043957153fcSJan Kara 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
6044eeca7ea1SDeepa Dinamani 	inode->i_mtime = inode->i_ctime = current_time(inode);
604521f97697SJan Kara 	ext4_mark_inode_dirty(handle, inode);
604621f97697SJan Kara 	ext4_journal_stop(handle);
6047957153fcSJan Kara out_unlock:
6048957153fcSJan Kara 	inode_unlock(inode);
6049957153fcSJan Kara out_put:
6050964edf66SJan Kara 	lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
6051957153fcSJan Kara 	iput(inode);
6052957153fcSJan Kara 	return err;
605321f97697SJan Kara out:
6054ca0e05e4SDmitry Monakhov 	return dquot_quota_off(sb, type);
6055ca0e05e4SDmitry Monakhov }
6056ca0e05e4SDmitry Monakhov 
6057ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford
6058ac27a0ecSDave Kleikamp  * acquiring the locks... As quota files are never truncated and quota code
6059ac27a0ecSDave Kleikamp  * itself serializes the operations (and no one else should touch the files)
6060ac27a0ecSDave Kleikamp  * we don't have to be afraid of races */
6061617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
6062ac27a0ecSDave Kleikamp 			       size_t len, loff_t off)
6063ac27a0ecSDave Kleikamp {
6064ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6065725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
6066ac27a0ecSDave Kleikamp 	int offset = off & (sb->s_blocksize - 1);
6067ac27a0ecSDave Kleikamp 	int tocopy;
6068ac27a0ecSDave Kleikamp 	size_t toread;
6069ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6070ac27a0ecSDave Kleikamp 	loff_t i_size = i_size_read(inode);
6071ac27a0ecSDave Kleikamp 
6072ac27a0ecSDave Kleikamp 	if (off > i_size)
6073ac27a0ecSDave Kleikamp 		return 0;
6074ac27a0ecSDave Kleikamp 	if (off+len > i_size)
6075ac27a0ecSDave Kleikamp 		len = i_size-off;
6076ac27a0ecSDave Kleikamp 	toread = len;
6077ac27a0ecSDave Kleikamp 	while (toread > 0) {
6078ac27a0ecSDave Kleikamp 		tocopy = sb->s_blocksize - offset < toread ?
6079ac27a0ecSDave Kleikamp 				sb->s_blocksize - offset : toread;
60801c215028STheodore Ts'o 		bh = ext4_bread(NULL, inode, blk, 0);
60811c215028STheodore Ts'o 		if (IS_ERR(bh))
60821c215028STheodore Ts'o 			return PTR_ERR(bh);
6083ac27a0ecSDave Kleikamp 		if (!bh)	/* A hole? */
6084ac27a0ecSDave Kleikamp 			memset(data, 0, tocopy);
6085ac27a0ecSDave Kleikamp 		else
6086ac27a0ecSDave Kleikamp 			memcpy(data, bh->b_data+offset, tocopy);
6087ac27a0ecSDave Kleikamp 		brelse(bh);
6088ac27a0ecSDave Kleikamp 		offset = 0;
6089ac27a0ecSDave Kleikamp 		toread -= tocopy;
6090ac27a0ecSDave Kleikamp 		data += tocopy;
6091ac27a0ecSDave Kleikamp 		blk++;
6092ac27a0ecSDave Kleikamp 	}
6093ac27a0ecSDave Kleikamp 	return len;
6094ac27a0ecSDave Kleikamp }
6095ac27a0ecSDave Kleikamp 
6096ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has
6097ac27a0ecSDave Kleikamp  * enough credits) */
6098617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type,
6099ac27a0ecSDave Kleikamp 				const char *data, size_t len, loff_t off)
6100ac27a0ecSDave Kleikamp {
6101ac27a0ecSDave Kleikamp 	struct inode *inode = sb_dqopt(sb)->files[type];
6102725d26d3SAneesh Kumar K.V 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
61031c215028STheodore Ts'o 	int err, offset = off & (sb->s_blocksize - 1);
6104c5e298aeSTheodore Ts'o 	int retries = 0;
6105ac27a0ecSDave Kleikamp 	struct buffer_head *bh;
6106ac27a0ecSDave Kleikamp 	handle_t *handle = journal_current_handle();
6107ac27a0ecSDave Kleikamp 
61080390131bSFrank Mayhar 	if (EXT4_SB(sb)->s_journal && !handle) {
6109b31e1552SEric Sandeen 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6110b31e1552SEric Sandeen 			" cancelled because transaction is not started",
61119c3013e9SJan Kara 			(unsigned long long)off, (unsigned long long)len);
61129c3013e9SJan Kara 		return -EIO;
61139c3013e9SJan Kara 	}
611467eeb568SDmitry Monakhov 	/*
611567eeb568SDmitry Monakhov 	 * Since we account only one data block in transaction credits,
611667eeb568SDmitry Monakhov 	 * then it is impossible to cross a block boundary.
611767eeb568SDmitry Monakhov 	 */
611867eeb568SDmitry Monakhov 	if (sb->s_blocksize - offset < len) {
611967eeb568SDmitry Monakhov 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
612067eeb568SDmitry Monakhov 			" cancelled because not block aligned",
612167eeb568SDmitry Monakhov 			(unsigned long long)off, (unsigned long long)len);
612267eeb568SDmitry Monakhov 		return -EIO;
612367eeb568SDmitry Monakhov 	}
612467eeb568SDmitry Monakhov 
6125c5e298aeSTheodore Ts'o 	do {
6126c5e298aeSTheodore Ts'o 		bh = ext4_bread(handle, inode, blk,
6127c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_CREATE |
6128c5e298aeSTheodore Ts'o 				EXT4_GET_BLOCKS_METADATA_NOFAIL);
612945586c70SMasahiro Yamada 	} while (PTR_ERR(bh) == -ENOSPC &&
6130c5e298aeSTheodore Ts'o 		 ext4_should_retry_alloc(inode->i_sb, &retries));
61311c215028STheodore Ts'o 	if (IS_ERR(bh))
61321c215028STheodore Ts'o 		return PTR_ERR(bh);
6133ac27a0ecSDave Kleikamp 	if (!bh)
6134ac27a0ecSDave Kleikamp 		goto out;
61355d601255Sliang xie 	BUFFER_TRACE(bh, "get write access");
6136617ba13bSMingming Cao 	err = ext4_journal_get_write_access(handle, bh);
6137ac27a0ecSDave Kleikamp 	if (err) {
6138ac27a0ecSDave Kleikamp 		brelse(bh);
61391c215028STheodore Ts'o 		return err;
6140ac27a0ecSDave Kleikamp 	}
6141ac27a0ecSDave Kleikamp 	lock_buffer(bh);
614267eeb568SDmitry Monakhov 	memcpy(bh->b_data+offset, data, len);
6143ac27a0ecSDave Kleikamp 	flush_dcache_page(bh->b_page);
6144ac27a0ecSDave Kleikamp 	unlock_buffer(bh);
61450390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
6146ac27a0ecSDave Kleikamp 	brelse(bh);
6147ac27a0ecSDave Kleikamp out:
614867eeb568SDmitry Monakhov 	if (inode->i_size < off + len) {
614967eeb568SDmitry Monakhov 		i_size_write(inode, off + len);
6150617ba13bSMingming Cao 		EXT4_I(inode)->i_disksize = inode->i_size;
6151617ba13bSMingming Cao 		ext4_mark_inode_dirty(handle, inode);
615221f97697SJan Kara 	}
615367eeb568SDmitry Monakhov 	return len;
6154ac27a0ecSDave Kleikamp }
6155ac27a0ecSDave Kleikamp #endif
6156ac27a0ecSDave Kleikamp 
6157152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6158152a0836SAl Viro 		       const char *dev_name, void *data)
6159ac27a0ecSDave Kleikamp {
6160152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
6161ac27a0ecSDave Kleikamp }
6162ac27a0ecSDave Kleikamp 
6163c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
616424b58424STheodore Ts'o static inline void register_as_ext2(void)
616524b58424STheodore Ts'o {
616624b58424STheodore Ts'o 	int err = register_filesystem(&ext2_fs_type);
616724b58424STheodore Ts'o 	if (err)
616824b58424STheodore Ts'o 		printk(KERN_WARNING
616924b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
617024b58424STheodore Ts'o }
617124b58424STheodore Ts'o 
617224b58424STheodore Ts'o static inline void unregister_as_ext2(void)
617324b58424STheodore Ts'o {
617424b58424STheodore Ts'o 	unregister_filesystem(&ext2_fs_type);
617524b58424STheodore Ts'o }
61762035e776STheodore Ts'o 
61772035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb)
61782035e776STheodore Ts'o {
6179e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_incompat_features(sb))
61802035e776STheodore Ts'o 		return 0;
6181bc98a42cSDavid Howells 	if (sb_rdonly(sb))
61822035e776STheodore Ts'o 		return 1;
6183e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext2_ro_compat_features(sb))
61842035e776STheodore Ts'o 		return 0;
61852035e776STheodore Ts'o 	return 1;
61862035e776STheodore Ts'o }
618724b58424STheodore Ts'o #else
618824b58424STheodore Ts'o static inline void register_as_ext2(void) { }
618924b58424STheodore Ts'o static inline void unregister_as_ext2(void) { }
61902035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
619124b58424STheodore Ts'o #endif
619224b58424STheodore Ts'o 
619324b58424STheodore Ts'o static inline void register_as_ext3(void)
619424b58424STheodore Ts'o {
619524b58424STheodore Ts'o 	int err = register_filesystem(&ext3_fs_type);
619624b58424STheodore Ts'o 	if (err)
619724b58424STheodore Ts'o 		printk(KERN_WARNING
619824b58424STheodore Ts'o 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
619924b58424STheodore Ts'o }
620024b58424STheodore Ts'o 
620124b58424STheodore Ts'o static inline void unregister_as_ext3(void)
620224b58424STheodore Ts'o {
620324b58424STheodore Ts'o 	unregister_filesystem(&ext3_fs_type);
620424b58424STheodore Ts'o }
62052035e776STheodore Ts'o 
62062035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb)
62072035e776STheodore Ts'o {
6208e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_incompat_features(sb))
62092035e776STheodore Ts'o 		return 0;
6210e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_journal(sb))
62112035e776STheodore Ts'o 		return 0;
6212bc98a42cSDavid Howells 	if (sb_rdonly(sb))
62132035e776STheodore Ts'o 		return 1;
6214e2b911c5SDarrick J. Wong 	if (ext4_has_unknown_ext3_ro_compat_features(sb))
62152035e776STheodore Ts'o 		return 0;
62162035e776STheodore Ts'o 	return 1;
62172035e776STheodore Ts'o }
621824b58424STheodore Ts'o 
621903010a33STheodore Ts'o static struct file_system_type ext4_fs_type = {
6220ac27a0ecSDave Kleikamp 	.owner		= THIS_MODULE,
622103010a33STheodore Ts'o 	.name		= "ext4",
6222152a0836SAl Viro 	.mount		= ext4_mount,
6223ac27a0ecSDave Kleikamp 	.kill_sb	= kill_block_super,
6224ac27a0ecSDave Kleikamp 	.fs_flags	= FS_REQUIRES_DEV,
6225ac27a0ecSDave Kleikamp };
62267f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4");
6227ac27a0ecSDave Kleikamp 
6228e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */
6229e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
6230e9e3bcecSEric Sandeen 
62315dabfc78STheodore Ts'o static int __init ext4_init_fs(void)
6232ac27a0ecSDave Kleikamp {
6233e9e3bcecSEric Sandeen 	int i, err;
6234c9de560dSAlex Tomas 
6235e294a537STheodore Ts'o 	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
623607c0c5d8SAl Viro 	ext4_li_info = NULL;
623707c0c5d8SAl Viro 	mutex_init(&ext4_li_mtx);
623807c0c5d8SAl Viro 
62399a4c8019SCarlos Maiolino 	/* Build-time check for flags consistency */
624012e9b892SDmitry Monakhov 	ext4_check_flag_values();
6241e9e3bcecSEric Sandeen 
6242e142d052SJan Kara 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
6243e9e3bcecSEric Sandeen 		init_waitqueue_head(&ext4__ioend_wq[i]);
6244e9e3bcecSEric Sandeen 
624551865fdaSZheng Liu 	err = ext4_init_es();
62466fd058f7STheodore Ts'o 	if (err)
62476fd058f7STheodore Ts'o 		return err;
624851865fdaSZheng Liu 
62491dc0aa46SEric Whitney 	err = ext4_init_pending();
62501dc0aa46SEric Whitney 	if (err)
625122cfe4b4SEric Biggers 		goto out7;
625222cfe4b4SEric Biggers 
625322cfe4b4SEric Biggers 	err = ext4_init_post_read_processing();
625422cfe4b4SEric Biggers 	if (err)
62551dc0aa46SEric Whitney 		goto out6;
62561dc0aa46SEric Whitney 
625751865fdaSZheng Liu 	err = ext4_init_pageio();
625851865fdaSZheng Liu 	if (err)
6259b5799018STheodore Ts'o 		goto out5;
626051865fdaSZheng Liu 
62615dabfc78STheodore Ts'o 	err = ext4_init_system_zone();
6262bd2d0210STheodore Ts'o 	if (err)
6263b5799018STheodore Ts'o 		goto out4;
6264857ac889SLukas Czerner 
6265b5799018STheodore Ts'o 	err = ext4_init_sysfs();
6266dd68314cSTheodore Ts'o 	if (err)
6267b5799018STheodore Ts'o 		goto out3;
6268857ac889SLukas Czerner 
62695dabfc78STheodore Ts'o 	err = ext4_init_mballoc();
6270ac27a0ecSDave Kleikamp 	if (err)
6271c9de560dSAlex Tomas 		goto out2;
6272ac27a0ecSDave Kleikamp 	err = init_inodecache();
6273ac27a0ecSDave Kleikamp 	if (err)
6274ac27a0ecSDave Kleikamp 		goto out1;
627524b58424STheodore Ts'o 	register_as_ext3();
62762035e776STheodore Ts'o 	register_as_ext2();
627703010a33STheodore Ts'o 	err = register_filesystem(&ext4_fs_type);
6278ac27a0ecSDave Kleikamp 	if (err)
6279ac27a0ecSDave Kleikamp 		goto out;
6280bfff6873SLukas Czerner 
6281ac27a0ecSDave Kleikamp 	return 0;
6282ac27a0ecSDave Kleikamp out:
628324b58424STheodore Ts'o 	unregister_as_ext2();
628424b58424STheodore Ts'o 	unregister_as_ext3();
6285ac27a0ecSDave Kleikamp 	destroy_inodecache();
6286ac27a0ecSDave Kleikamp out1:
62875dabfc78STheodore Ts'o 	ext4_exit_mballoc();
62889c191f70ST Makphaibulchoke out2:
6289b5799018STheodore Ts'o 	ext4_exit_sysfs();
6290b5799018STheodore Ts'o out3:
6291dd68314cSTheodore Ts'o 	ext4_exit_system_zone();
6292b5799018STheodore Ts'o out4:
62935dabfc78STheodore Ts'o 	ext4_exit_pageio();
6294b5799018STheodore Ts'o out5:
629522cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
62961dc0aa46SEric Whitney out6:
629722cfe4b4SEric Biggers 	ext4_exit_pending();
629822cfe4b4SEric Biggers out7:
629951865fdaSZheng Liu 	ext4_exit_es();
630051865fdaSZheng Liu 
6301ac27a0ecSDave Kleikamp 	return err;
6302ac27a0ecSDave Kleikamp }
6303ac27a0ecSDave Kleikamp 
63045dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void)
6305ac27a0ecSDave Kleikamp {
6306bfff6873SLukas Czerner 	ext4_destroy_lazyinit_thread();
630724b58424STheodore Ts'o 	unregister_as_ext2();
630824b58424STheodore Ts'o 	unregister_as_ext3();
630903010a33STheodore Ts'o 	unregister_filesystem(&ext4_fs_type);
6310ac27a0ecSDave Kleikamp 	destroy_inodecache();
63115dabfc78STheodore Ts'o 	ext4_exit_mballoc();
6312b5799018STheodore Ts'o 	ext4_exit_sysfs();
63135dabfc78STheodore Ts'o 	ext4_exit_system_zone();
63145dabfc78STheodore Ts'o 	ext4_exit_pageio();
631522cfe4b4SEric Biggers 	ext4_exit_post_read_processing();
6316dd12ed14SEric Sandeen 	ext4_exit_es();
63171dc0aa46SEric Whitney 	ext4_exit_pending();
6318ac27a0ecSDave Kleikamp }
6319ac27a0ecSDave Kleikamp 
6320ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
632183982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem");
6322ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL");
63237ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c");
63245dabfc78STheodore Ts'o module_init(ext4_init_fs)
63255dabfc78STheodore Ts'o module_exit(ext4_exit_fs)
6326